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

Sprite Class Reference
[Game Objects and Event Loop]

The basic Sprite class itself. More...

#include <sprite.h>

Inheritance diagram for Sprite:

Inheritance graph
[legend]
Collaboration diagram for Sprite:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 Sprite ()
 Default constructor.

virtual ~Sprite ()
 Destructor, frees any owned data.

Overloadable virtual functions
Overload these functions to get non-default behaviors upon update, collision, frame advance, and SDL_USEREVENTs.

If you don't overload Clone(), the copy it produces won't have any data other than that handled by Sprite, and none of the custom behavior of the parent.

virtual void Update (Sint32 dtick)
 Calculates movement for the # of milliseconds.

virtual void OnCollide (const Sprite *spr, ColType index)
 Called upon collision with another sprite.

virtual int GetFrame ()
 Returns the current frame number.

virtual SpriteClone ()
 Returns a clone of the current sprite.

virtual void Event (int code, void *data1=NULL, void *data2=NULL)
 Called for SDL_USEREVENTs.

Position-related accessors
Position and velocity read and set operators.

SfxpCoord Pos () const
 Current fixed point position coordinates.

SfxpCoord Vel () const
 Current fixed point velocity vector.

void ReverseXVelocity ()
 Completely reverses X direction.

void ReverseYVelocity ()
 Completely reverses Y direction.

void Preserve ()
 Saves previous fixed point coordinates.

void MoveTo (Sfxp x, Sfxp y)
 Moves the sprite to a different position.

void SetVel (SfxpCoord velin)
 Sets the sprite's velocity.

Rect PrevRect () const
 Returns last frame's position rect.

Rect CurRect () const
 Returns current frame's position rect.

Sfxp VXfxp () const
 Returns fixed-point x velocity.

Sfxp VYfxp () const
 Returns fixed-point y velicity.

Sfxp Xfxp () const
 Returns fixed-point X position.

Sfxp Yfxp () const
 Returns fixed-point Y position.

Flag-related accessor functions
Read and set sprite's flags.

void Remove ()
 Marks a sprite as expired.

void Tag ()
 Marks a sprite as moved.

int OwnSurface () const
 Returns true if the sprite can free it's surface.

int OwnBitmask () const
 Returns true if the sprite can free it's collision bitmasks.

SpriteFlag SetFlags (const SpriteFlag flags)
 Sets flags in sprite.

int Deleteable () const
 this is true when the sprite is deleteable

int New () const
 This is true when the sprite is newly created, not having been drawn.

int Moved () const
 Returns whether movement flag's been set.

void Expire ()
 Marks the sprite as removable from a list.

int Expired () const
 Returns true if the sprite is removable from a list.

const SpriteFlag ClearFlags (const SpriteFlag flags)
 Clears flags in sprite.

const SpriteFlag GetFlags () const
 Returns flags in sprite.

Collision-related accessors
Functions to check collisions and collision masks etc.

bool CanCollide (Sprite *spr)
 Returns true if the 2 sprites collide, false otherwise.

bitmask * GetBitmask ()
 Returns the current collision bitmask.

Uint8 const  GetColIndex ()
 Returns the collision index mask.

ColType const  GetColLayer ()
 Returns the collision layer mask.

Graphics-related accessors
Functions for framerate, drawing layers, source rectangles, etc

void SetFrameRate (Sfxp frate)
 Sets the frame rate.

Sint16 DrawLayer () const
 Returns the sprite's drawing layer.

void CalcFrame ()
 Calculate the source-rect for the current frame #.

Sprite data initialization functions
Functions that SpriteLoader uses to initialize sprite data after the constructor is called.

void Draw (SDL_Surface *screen)
 Draws the sprite on the screen.

void Draw (SDL_Surface *screen, Rect portion)
 Draws part of a sprite on the screen.

void Init_Surface (SDL_Surface *surface=NULL, bool os_in=false)
 Initializes the sprite's surface member.

void Init_Layers (Sint16 layer=0)
 Initializes the sprite's drawing layer.

void Init_List (Sprite *previn=NULL, Sprite *nextin=NULL)
 Initializes the sprite's linked-list members.

void Init_Flags (SpriteFlag flags=0)
 Initializes the sprite's status flags.

void Init_Frames (int xframes=-1, int yframes=-1)
 Initializes the sprite's animation frames.

void Init_Collision (ColType c_index=0, ColType c_layer=0)
 Initializes the sprite's collision masks.

void Init_Bitmask (bool enable=false, bitmask **bmask_in=NULL)
 Initializes the sprite's collision masks.


Protected Attributes

SpriteFlag flag
 Status flags.

Collision attributes
Here's how collision layers work. When two sprite's areas of draw overlap, it checks if (spr1->clayer&spr2->cindex) is nonzero and if (spr2->clayer&spr1->cindex) is nonzero. If so, it checks the collision bitmask if any.

bitmask ** bmask
 Collision bitmasks.

ColType clayer
 Collision layer mask, the layers it can collide with.

ColType cindex
 Collision layer index, the collision layer inhabited.

Image Attributes
The attributes that describe the sprite's image, framerate, drawing order, and current frame.

Sfxp frame
 Current frame # in fixed point.

Sfxp framerate
 Current framerate in fixed point.

Rect frames
 x - x frames, y - y frames

SDL_Surface * surface
 Image surface.

Rect srcrect
 Source rectangle.

Sint16 drawlayer
 Higher #s get drawn first.

Motion attributes
Describes the sprite's current position, previous position, and current velocity.

SfxpCoord pos
 Current position coordinates.

SfxpCoord prevpos
 Previous position coordinates.

SfxpCoord vel
 Velocity vector.

Linked-list attributes
Used by SpriteList

Spriteprev
 Linked-list prev/next pointers.

Spritenext
 Linked-list prev/next pointers.


Friends

class SpriteList
 SpriteList has access to Sprite's private members.

class SpriteManager
 SpriteManager has access to Sprite's private members.


Detailed Description

Don't be intimidated. Sure, it looks really complicated, but it's not. The vast majority of the functions contained herein are simple convenience functions for accessing the sprite's internal data.

Here's how it works. Each sprite has a set of collision attributes, image attributes, and motion attributes. Collision attributes determine what other sprites it can collide with, image attributes determine the image frames and framerate, and motion attributes describe it's current position and velocity. The member variable definitions describe what they do in good detail.

Definition at line 116 of file sprite.h.


Constructor & Destructor Documentation

Sprite::Sprite  ) 
 

Default constructor. Initializes the sprite as a totally blank slate.

Definition at line 38 of file sprite.cpp.

References Init_Bitmask(), Init_Collision(), Init_Flags(), Init_Frames(), Init_Layers(), Init_List(), and Init_Surface().

Referenced by Clone().

Here is the call graph for this function:

Sprite::~Sprite  )  [virtual]
 

Destructor, frees any data the sprite 'owns'.

Definition at line 268 of file sprite.cpp.

References bmask, frames, OwnBitmask(), OwnSurface(), and surface.

Here is the call graph for this function:


Member Function Documentation

Sprite * Sprite::Clone  )  [virtual]
 

Generates an exact copy of the sprite. You'll need to overload this when you derive classes from Sprite.

Definition at line 61 of file sprite.cpp.

References ClearFlags(), Sprite(), SPRITE_OWNBITMASK, and SPRITE_OWNSURFACE.

Here is the call graph for this function:

void Sprite::Draw SDL_Surface *  screen,
Rect  portion
 

Partial-redraw algorithm. Can redraw PARTS of sprites instead of the whole thing. Optimize me!

Definition at line 297 of file sprite.cpp.

References Rect::Intersect(), pos, srcrect, surface, SfxpCoord::X(), and SfxpCoord::Y().

Here is the call graph for this function:

void Sprite::Draw SDL_Surface *  screen  ) 
 

Redraws the whole sprite.

Definition at line 321 of file sprite.cpp.

References pos, srcrect, surface, SfxpCoord::X(), and SfxpCoord::Y().

Referenced by SpriteManager::FastBlit(), SpriteManager::QuadBlit(), and SpriteManager::SimpleBlit().

Here is the call graph for this function:

void Sprite::Event int  code,
void *  data1 = NULL,
void *  data2 = NULL
[virtual]
 

Called in response to unhandled SDL_USEREVENTS by the game loop.

Definition at line 53 of file sprite.cpp.

Referenced by SpriteManager::SendEvent().

bitmask * Sprite::GetBitmask  ) 
 

Returns the sprite's current collision bitmask, if any.

Definition at line 334 of file sprite.cpp.

References bmask, frames, and GetFrame().

Referenced by CanCollide(), and MoveApart().

Here is the call graph for this function:

int Sprite::GetFrame  )  [virtual]
 

Gets the current frame #. Virtual to make it possible to do more complicated animations than just continuously incrementing the frame number

Definition at line 83 of file sprite.cpp.

References frame, and FROM_SFXP.

Referenced by GetBitmask().

void Sprite::Init_Bitmask bool  enable = false,
bitmask **  bmask_in = NULL
 

Initializes bitmasked-collision members and flags.

Definition at line 220 of file sprite.cpp.

References bmask, CalcFrame(), ClearFlags(), frame, frames, MakeBitMask(), SetFlags(), SPRITE_OWNBITMASK, srcrect, surface, and TO_SFXP.

Referenced by SpriteLoader::GetSprite(), and Sprite().

Here is the call graph for this function:

void Sprite::Init_Collision ColType  c_index = 0,
ColType  c_layer = 0
 

Configures the collision-layer attributes.

Definition at line 91 of file sprite.cpp.

References cindex, clayer, and ColType.

Referenced by SpriteLoader::GetSprite(), and Sprite().

void Sprite::Init_Flags SpriteFlag  flags = 0  ) 
 

Initializes flags.

Definition at line 181 of file sprite.cpp.

References flag, SPRITE_WAIT, and SpriteFlag.

Referenced by Sprite().

void Sprite::Init_Frames int  xframes = -1,
int  yframes = -1
 

Initializes frame information.

Definition at line 155 of file sprite.cpp.

References CalcFrame(), frame, framerate, frames, and surface.

Referenced by SpriteLoader::GetSprite(), and Sprite().

Here is the call graph for this function:

void Sprite::Init_Layers Sint16  layer = 0  ) 
 

Sets up the sprite's position in the drawing order.

Definition at line 260 of file sprite.cpp.

References drawlayer.

Referenced by SpriteLoader::GetSprite(), and Sprite().

void Sprite::Init_List Sprite previn = NULL,
Sprite nextin = NULL
 

Initializes a sprite's linked-list members for insertion into SpriteList.

Definition at line 211 of file sprite.cpp.

References next, and prev.

Referenced by Sprite().

void Sprite::Init_Surface SDL_Surface *  surf = NULL,
bool  os_in = false
 

Initializes surface and surface-ownership members and flags.

Definition at line 189 of file sprite.cpp.

References ClearFlags(), Rect::Set(), SetFlags(), SPRITE_OWNSURFACE, srcrect, and surface.

Referenced by SpriteLoader::GetSprite(), and Sprite().

Here is the call graph for this function:

void Sprite::OnCollide const Sprite spr,
ColType  index
[virtual]
 

Virtual function called whenever this sprite collides with another one.

Reimplemented in Bouncy.

Definition at line 74 of file sprite.cpp.

References ColType.

Referenced by SpriteManager::Move().

void Sprite::Update Sint32  dtick  )  [virtual]
 

Called every game tick. Moves the sprite, changes the frame number, and so forth. Overload it to do stuff that you want too.

Reimplemented in Bouncy.

Definition at line 101 of file sprite.cpp.

References CalcFrame(), frame, framerate, frames, FROM_SFXP, SfxpCoord::IntScalar(), SfxpCoord::MajorDigitsEqual(), SfxpCoord::Nonzero(), pos, prevpos, surface, Tag(), TO_SFXP, and vel.

Referenced by SpriteManager::Move(), and Bouncy::Update().

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:35 2003 for Spritelib by doxygen 1.3.4