Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

SpriteManager Class Reference
[Game Objects and Event Loop]

Class to manage movement, collision, and drawing of sprites. More...

#include <spritemanager.h>

Collaboration diagram for SpriteManager:

Collaboration graph
[legend]
List of all members.

Public Member Functions

void SendEvent (int code, void *data1=NULL, void *data2=NULL)
 Calls the Event() member of every sprite with this data.

bool AddUpdateRect (SDL_Rect &r)
 Attempts to add the specified rect to the screen update list.

Constructors
SpriteManager has two constructors, one for dealing with preset video modes and backgrounds, and one for setting the video mode and such. The video-initializing constructor initializes SDL if it's needed.

 SpriteManager (Uint16 w, Uint16 h, Uint8 depth, Uint32 vflags)
 Constructor that sets screen mode.

 SpriteManager (Uint16 flags=0, SDL_Surface *screenin=NULL, const char *bgnd=NULL)
 Constructor - all parameters optional.

Contents-control functions
These do what they say they do. RemoveSprite does not delete the sprite, just removes it from the list.

bool AddSprite (Sprite *spr)
 Attempts to add a sprite to the list.

bool RemoveSprite (Sprite *spr)
 Attempts to remove a sprite from the list.

Overloadable virtual functions
These functions are stubs that you can overload to do what you want in response to SDL_USEREVENTs, unhandled SDL events, destruction, and in the game loop's free time. Event(), Idle(), and Handler() are all called by Run() while the game loop is running.

virtual int Event (int code, void *data1, void *data2)
 Handler function for SDL_USEREVENTs.

virtual void Idle ()
 Function called when event loop has time to kill.

virtual void Handler (SDL_Event *event)
 Function called for unhandled SDL events.

virtual ~SpriteManager ()
 Destructor, frees all data.

Event Loop functions
The four game-loop functions are called together by the Run() functions, or by whatever custom routine you devise for the same function. They need to be called in the order Preserve() Move() Draw() Update(). Move() can be called several times in succession.

SMRun_Return Run (Sint32 frametick)
 Runs the game event loop until SDL_QUIT.

SMRun_Return Run (Sint32 frametick, Sint32 gametick)
 Runs the game event loop with a guaranteed game tick.

void Preserve ()
 Preserves previous position of sprites.

void Move (Sint32 dtick)
 Calls the Update() member of every sprite.

void Draw ()
 Draws the sprites on the screen.

void Update ()
 Updates relevant portions of the screen.

Status Flag Functions
Routines to set, query, and clear various status flags in SpriteManager.

void Redraw ()
 Sets SM_TOTALREDRAW flag.

int Paused ()
 Returns nonzero if paused, zero if not.

void Pause ()
 Sets the SM_PAUSED flag.

void Unpause ()
 Clears the SM_PAUSED flag.

Background-access functions
These functions allow you to modify the background image from outside SpriteManager. Locks the mutex, to prevent another thread from using the surface while modification is in progress.

SDL_Surface * CheckOutBG ()
 Returns the background surface and locks the SpriteManager.

bool CheckInBG (SDL_Surface *bg_in)
 Checks the surface back in and unlocks the SpriteManager.

void DrawOnBG (SDL_Surface *src, SDL_Rect *srcrect, SDL_Rect *destrect)
 Blits specified portion of surface onto background image.


Protected Member Functions

void Lock ()
 Locks the mutex, to prevent simultaneous access.

void Unlock ()
 Unlocks the mutex, leaving the manager free for use.

Sprite Blitters
These are the different sprite blitters available. SimpleBlit() is called whenever a total redraw is needed. FastBlit() is more efficient, but still redraws every sprite instead of only ones that need updating. QuadBlit() is theoretically more efficient but still under test. SpriteLib uses QuadBlit() by default

void SimpleBlit ()
 Updates the entire screen.

void FastBlit ()
 Redraws only sprites and the areas under them.

void QuadBlit ()
 Redraws only things in disturbed quadrants.


Protected Attributes

Uint16 flag
 Status flags.

SDL_mutex * lock
 Mutex; locks during operations to make it threadsafe.

SDL_Surface * screen
 The surface that gets drawn to.

SDL_Surface * bg
 The background image.

SpriteList sprites
 Linked list of sprites.

RectList rects
 Big stack of rectangles.


Detailed Description

Class implementing a general purpose game loop. Handles motion of game objects; insertion, removal, and deletion of game objects; inter-object communication via SDL_USEREVENTs, framerate timing, and event handling.

Definition at line 76 of file spritemanager.h.


Constructor & Destructor Documentation

SpriteManager::SpriteManager Uint16  w,
Uint16  h,
Uint8  depth,
Uint32  vflags
 

Constructor capable of initializing SDL and setting it's own video mode. Fancy huh?

Definition at line 56 of file spritemanager.cpp.

References bg, flag, lock, screen, SM_DOUBLEBUF, SM_OWNBG, and SM_TOTALREDRAW.

SpriteManager::SpriteManager Uint16  flags = 0,
SDL_Surface *  screenin = NULL,
const char *  bgnd = NULL
 

General purpose constructor, taking pre-created video and background surfaces.

Definition at line 289 of file spritemanager.cpp.

References bg, flag, LoadConvertBMP(), lock, MAX_RECTS, screen, SM_OWNBG, and SM_TOTALREDRAW.

Here is the call graph for this function:

SpriteManager::~SpriteManager  )  [virtual]
 

Frees the background and mutex.

Definition at line 494 of file spritemanager.cpp.

References bg, flag, lock, Lock(), and SM_OWNBG.

Here is the call graph for this function:


Member Function Documentation

bool SpriteManager::AddSprite Sprite spr  ) 
 

Attempts to add a sprite.

Definition at line 333 of file spritemanager.cpp.

References SpriteList::Insert(), Lock(), sprites, and Unlock().

Referenced by main().

Here is the call graph for this function:

bool SpriteManager::CheckInBG SDL_Surface *  bg_in  ) 
 

Returns the background surface you checked out.

Definition at line 476 of file spritemanager.cpp.

References bg, and Unlock().

Here is the call graph for this function:

SDL_Surface * SpriteManager::CheckOutBG  ) 
 

Allows something outside SpriteManager to 'borrow' the background surface and doodle on it. SpriteManager will want it back, though, and game loops won't run until the surface is returned.

Definition at line 468 of file spritemanager.cpp.

References bg, and Lock().

Here is the call graph for this function:

void SpriteManager::Draw  ) 
 

Draws the new frame, but doesn't update it.

Definition at line 361 of file spritemanager.cpp.

References flag, QuadBlit(), SimpleBlit(), SM_DOUBLEBUF, and SM_TOTALREDRAW.

Referenced by Run().

Here is the call graph for this function:

void SpriteManager::DrawOnBG SDL_Surface *  src,
SDL_Rect *  srcrect,
SDL_Rect *  destrect
 

Lets you blit stuff directly onto the background.

Definition at line 93 of file spritemanager.cpp.

References bg, flag, Lock(), SM_TOTALREDRAW, and Unlock().

Referenced by main().

Here is the call graph for this function:

int SpriteManager::Event int  code,
void *  data1,
void *  data2
[virtual]
 

Unless this function returns 1, the SDL_USEREVENT is passed along to every sprite in the list.

Definition at line 34 of file spritemanager.cpp.

Referenced by Run().

void SpriteManager::FastBlit  )  [protected]
 

A simple but effective blitter for small numbers of sprites. Redraws every sprite, every frame, but doesn't need to redraw the entire background unlike SimpleBlit.

Definition at line 142 of file blitter.cpp.

References bg, Sprite::CurRect(), Sprite::Deleteable(), Sprite::Draw(), Sprite::Expired(), SpriteList::Extract(), SpriteList::Head(), Rect::Merge(), Sprite::next, Sprite::PrevRect(), RectList::Push(), rects, screen, and sprites.

Here is the call graph for this function:

void SpriteManager::Handler SDL_Event *  event  )  [virtual]
 

SDL events that the event loop doesn't know how to handle get passed along to here.

Definition at line 41 of file spritemanager.cpp.

Referenced by Run().

void SpriteManager::Idle  )  [virtual]
 

Game event loop calls this when it's not busy, can overload this to put in periodic checks and such

Definition at line 48 of file spritemanager.cpp.

Referenced by Run().

void SpriteManager::Move Sint32  dtick  ) 
 

Handles sprite movement, removal, deletion, and collision.

Definition at line 378 of file spritemanager.cpp.

References Sprite::CanCollide(), Sprite::ClearFlags(), Sprite::Expired(), Sprite::GetColIndex(), SpriteList::Head(), Lock(), Sprite::Moved(), Sprite::New(), Sprite::next, Sprite::OnCollide(), Sprite::SetFlags(), SPRITE_MOVED, SPRITE_WAIT, sprites, Unlock(), and Sprite::Update().

Referenced by Run().

Here is the call graph for this function:

void SpriteManager::Preserve  ) 
 

Iterates through the list and preserves the previous coordinates of every sprite.

Definition at line 274 of file spritemanager.cpp.

References SpriteList::Head(), Sprite::next, Sprite::Preserve(), and sprites.

Referenced by Run().

Here is the call graph for this function:

void SpriteManager::QuadBlit  )  [protected]
 

A theoretically more efficient blitter. It divides the screen into squares and only redraws the squares that have changed.

Definition at line 35 of file blitter.cpp.

References bg, Sprite::CurRect(), Sprite::Deleteable(), Sprite::Draw(), Sprite::Expired(), SpriteList::Extract(), SpriteList::Head(), Rect::Merge(), Sprite::Moved(), Rect::MoveTo(), Sprite::next, Sprite::PrevRect(), RectList::Push(), rects, screen, and sprites.

Referenced by Draw().

Here is the call graph for this function:

bool SpriteManager::RemoveSprite Sprite spr  ) 
 

This doesn't destroy the sprite, just removes it from the list.

Definition at line 347 of file spritemanager.cpp.

References SpriteList::Extract(), Lock(), sprites, and Unlock().

Here is the call graph for this function:

SMRun_Return SpriteManager::Run Sint32  frametick,
Sint32  gametick
 

Game Loop. The framerate can differ from the game tick, and the game tick is guaranteed to ALWAYS be 'gametick'. Can fail if you give it a gametick of zero, or the computer has insufficient power to calculate sprite movements as many times per second as you want.

Definition at line 125 of file spritemanager.cpp.

References Draw(), Event(), flag, Handler(), Idle(), Move(), Paused(), Preserve(), TickTimer::Reset(), SendEvent(), SM_RUNNING, SMRUN_OK, SMRun_Return, SMRUN_TIMESTEP, and Update().

Here is the call graph for this function:

SMRun_Return SpriteManager::Run Sint32  frametick  ) 
 

Runs the game event loop until an SDL_QUIT message is recieved. Calls nearly every other member while it's running, including the virtual Idle(), Event(), and Handler() functions that you can overload to do whatever you please.

Definition at line 209 of file spritemanager.cpp.

References Draw(), Event(), flag, Handler(), Idle(), Move(), Paused(), Preserve(), TickTimer::Reset(), SendEvent(), SM_RUNNING, SMRUN_OK, SMRun_Return, SMRUN_TIMESTEP, and Update().

Referenced by main().

Here is the call graph for this function:

void SpriteManager::SendEvent int  code,
void *  data1 = NULL,
void *  data2 = NULL
 

Sends SDL_USEREVENTs to EVERY sprite in the list.

Definition at line 107 of file spritemanager.cpp.

References Sprite::Event(), SpriteList::Head(), Sprite::next, and sprites.

Referenced by Run().

Here is the call graph for this function:

void SpriteManager::SimpleBlit  )  [protected]
 

Redraws the entire screen, a brute force blitter. Needs to be called every once in awhile, if the background changes and such.

Definition at line 107 of file blitter.cpp.

References bg, RectList::Clear(), Sprite::Deleteable(), Sprite::Draw(), Sprite::Expired(), SpriteList::Extract(), SpriteList::Head(), Sprite::next, RectList::Push(), rects, screen, and sprites.

Referenced by Draw().

Here is the call graph for this function:


The documentation for this class was generated from the following files:
Generated on Sat Oct 11 13:19:40 2003 for Spritelib by doxygen 1.3.4