Main Page | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

CSSprite Class Reference

#include <CSSprite.h>

Inheritance diagram for CSSprite:

CSMessageListener CSMessageDispatchable

Detailed Description

This file defines CSSprite:

CSSprite can only be created thru a loader (CSSpriteLoader). CSSprite are defined by (now ONLY thru) an xml-File, like: DTD:

<!ELEMENT SPRITE (ID,(ACTION)+,DEFAULT_ACTION,LAYER_POSITION,Y_POSITION)> <!ELEMENT ID (#PCDATA)> <!ELEMENT ACTION (#PCDATA)> <!ELEMENT DEFAULT_ACTION (#PCDATA)> <!ELEMENT LAYER_POSITION (#PCDATA)> <!ELEMENT Y_POSITION (#PCDATA)>
Example:

<SPRITE> <ID>PACMAN_SPRITE</ID> <ACTION>action\\pac_down.xml</ACTION> <ACTION>action\\pac_up.xml</ACTION> <ACTION>action\\pac_left.xml</ACTION> <ACTION>action\\pac_right.xml</ACTION> <ACTION>action\\pac_down_stay.xml</ACTION> <ACTION>action\\pac_up_stay.xml</ACTION> <ACTION>action\\pac_right_stay.xml</ACTION> <ACTION>action\\pac_left_stay.xml</ACTION> <ACTION>action\\pac_death.xml</ACTION> <DEFAULT_ACTION>action\\pac_right.xml</DEFAULT_ACTION> <LAYER_POSITION>0</LAYER_POSITION> <Y_POSITION>0</Y_POSITION> </SPRITE>
ID - supposed a unique identifier

ACTION - many actions are possible! describes the action to be loaded, parameter gives the ini-file for a CSAction

DEFAULT_ACTION - an action that is used as a default, if another actions ends. This default is overriden by the DEFAULT_NEXT_ACTION setting of the current active action. The default action should not appear listed in the 'normal' action setting. (no double entries)

LAYER_POSITION - sprite lies above layer (and under each higher layer) Y_POSITION - within that layer a sprite can have a y position so that one sprite can be above another within one layer TODO: right now the squence of the sprite is used, this parameter is still ignored!

Methods: virtual void next(); virtual void display(); SpriteState buildState(); // only for compatibility issues void resetState(SpriteState &state); virtual void reactOnMessage(CSMessage *message);// MessageListener implementation void setSecPerFrame(float secPerFrame); void setDisplayOffset(int xOffset, int yOffset);// where on the screen is 0,0 of our displaying void setWorldPosition(int x, int y); // where in the world will we start drawing the screen unsigned int checkCollisionPixels(CSSprite *other); // Collision detection, returns # of pixels unsigned int checkCollisionPixels(CSTileMap *map); // Collision detection, returns # of pixels unsigned int checkCollisionPixels(CSTile *tile); // Collision detection, returns # of pixels bool checkCollision(CSSprite *other); // Collision detection, returns boolean bool checkCollision(CSTileMap *map); // Collision detection, returns boolean bool checkCollision(CSTile *tile); // Collision detection, returns boolean std::vector<CSTile*> getCollidingTiles(CSTileMap *map); // return all tiles, that have pixels colliding with sprite void setSpeedY(int speed); // overrides values in animation void setSpeedX(int speed); // overrides values in animation void resetSpeed(); // returns to speed values of animation (default) SpriteState getState() void setState(const SpriteState &state) int getLayer() int getLayerY() void clearNavigator() void setNavigator(CSNavigator *navigator) unsigned int getDisplayXLower() unsigned int getDisplayYLower() unsigned int getDisplayXUpper() // pos + width of sprite unsigned int getDisplayYUpper() // pos + height of sprite unsigned int getXPos() unsigned int getYPos()

Supported messages: (see Message.h) Messages of type; ACTION_MESSAGE

CSSprite also implements: CSMessageListener
the sprite class has (in opposite to action an aniamtion) state dependent data -> the sprite state should at some point it be neccessary to have state information outside of sprites - than we must remove it here, than the sprite class will (as action and aniamtion) behave as a local singleton... (and change next(), display())

Definition at line 252 of file CSSprite.h.

Public Member Functions

 CSSprite (const CSSprite &Sprite)
virtual ~CSSprite ()
virtual std::string getType ()
virtual void next ()
virtual void display ()
SpriteState buildState ()
 only for compatibility issues

void resetState (SpriteState &state)
virtual void reactOnMessage (CSMessage *message)
 MessageListener implementation.

void setSecPerFrame (float secPerFrame)
void setDisplayOffset (int xOffset, int yOffset)
 where on the screen is 0,0 of our displaying

void setWorldPosition (int x, int y)
 where in the world will we start drawing the screen

unsigned int checkCollisionPixels (CSSprite *other)
 Collision detection, returns # of pixels.

unsigned int checkCollisionPixels (CSTileMap *map)
 Collision detection, returns # of pixels.

unsigned int checkCollisionPixels (CSTile *tile)
 Collision detection, returns # of pixels.

bool checkCollision (CSSprite *other)
 Collision detection, returns boolean.

bool checkCollision (CSTileMap *map)
 Collision detection, returns boolean.

bool checkCollision (CSTile *tile)
 Collision detection, returns boolean.

std::vector< CSTile * > getCollidingTiles (CSTileMap *map)
 return all tiles, that have pixels colliding with sprite

void setSpeedY (int speed)
 overrides values in animation

void setSpeedX (int speed)
 overrides values in animation

void adjustSpeed (int speed)
 overrides values in animation

void resetSpeed ()
 returns to speed values of animation (default)

void setNavigator (CSNavigator *navigator)
CSNavigatorgetNavigator ()
void setAlpha (int alpha)
 inlines

unsigned int getWorldPos ()
SpriteState getState ()
void setState (const SpriteState &state)
void setLayer (int z)
int getLayer ()
int getLayerY ()
void clearNavigator ()
void resetNavigator ()
int getMaxWidth ()
int getMaxHeight ()
int getSpriteType ()
int getSubtype ()
int getCurrentStateId ()
std::string getFilename ()
std::string getId ()
void setPosition (int x, int y, unsigned int z)
int getUserState (void)
void setUserState (int userState)
SDL_Surface * getScaledSprite (double factor)
const char * getCurrentActionId ()
int getCurrentActionType ()
unsigned int getDisplayXLower ()
unsigned int getDisplayYLower ()
unsigned int getDisplayXUpper ()
unsigned int getDisplayYUpper ()
int getXPos ()
int getYPos ()
void setActive (bool active)

Static Public Attributes

const char * CLASS = "CSSprite"


Constructor & Destructor Documentation

CSSprite::CSSprite const CSSprite Sprite  ) 
 

Definition at line 28 of file CSSprite.cpp.

References LOG_ENTER, LOG_EXIT, mFilename, and mId.

00029 {
00030     static char *functionName="CSSprite";
00031     LOG_ENTER 
00032     initialize();
00033     mId = strdup(sprite.mId);
00034     mFilename = sprite.mFilename;
00035 
00036     LOG_EXIT
00037 }

CSSprite::~CSSprite  )  [virtual]
 

Definition at line 122 of file CSSprite.cpp.

References LOG_ENTER, and LOG_EXIT.

00123 {
00124     static char *functionName="~CSSprite";
00125     LOG_ENTER 
00126     ActionBundleMap::iterator iter = actionMap.begin();
00127     while (iter != actionMap.end())
00128     {
00129         delete iter->second;
00130         iter++;
00131     }
00132     actionMap.clear();
00133 
00134     if (mId != 0)
00135     {
00136         free(mId);
00137     }
00138     LOG_EXIT
00139 }


Member Function Documentation

virtual std::string CSSprite::getType  )  [inline, virtual]
 

Reimplemented from CSMessageDispatchable.

Definition at line 290 of file CSSprite.h.

References CLASS.

00290 {return (std::string) CLASS;}

void CSSprite::next  )  [virtual]
 

Definition at line 263 of file CSSprite.cpp.

References CSAction::getDefaultAction(), CSAction::isAction(), ActionBundle::mActions, SpriteState::mActionState, ActionState::mAnimationState, SpriteState::mCurrentAction, SpriteState::mCurrentBundle, ActionBundle::mDefaultAction, AnimationState::mDisplayParams, CSNavigator::navigate(), CSAction::next(), CSMessageDispatchable::sendMessage(), and CSAction::startAction().

Referenced by CSTileMap::update().

00264 {
00265     static char *functionName="next";
00266     if (mNavigator != 0)
00267     {
00268         mNavigator->navigate(&(mState.mActionState.mAnimationState.mDisplayParams));
00269     }
00270 
00271     if (!mActive)
00272     {
00273         return;
00274     }
00275 
00276     if (mState.mCurrentAction == 0)
00277     {
00278         return;
00279     }
00280 
00281     if (!mState.mCurrentAction->next(mState.mActionState))
00282     {
00283         sendMessage(MESSAGE_SPRITE_ACTION_CHANGE);
00284         if (mState.mCurrentAction->getDefaultAction() != 0)
00285         {
00286             for (CSActions::iterator iter = mState.mCurrentBundle->mActions->begin(); iter != mState.mCurrentBundle->mActions->end(); iter++)
00287             {
00288                 CSAction *action = *iter;
00289                 if (action->isAction(mState.mCurrentAction->getDefaultAction()))
00290                 {
00291                     mState.mCurrentAction = action;
00292                     mState.mCurrentAction->startAction(mState.mActionState);
00293                     next();// next action invoked at once
00294                     return;
00295                 }
00296             }
00297         }
00298         mState.mCurrentAction = mState.mCurrentBundle->mDefaultAction;
00299         mState.mCurrentAction->startAction(mState.mActionState);
00300         next(); 
00301     }
00302 }

Here is the call graph for this function:

void CSSprite::display  )  [virtual]
 

Definition at line 305 of file CSSprite.cpp.

References CSAction::display(), SpriteState::mActionState, and SpriteState::mCurrentAction.

Referenced by CSTileMap::display().

00306 {
00307     static char *functionName="display";
00308     if (!mActive)
00309     {
00310         return;
00311     }
00312     if (mState.mCurrentAction == 0)
00313     {
00314         return;
00315     }
00316     mState.mCurrentAction->display(mState.mActionState);
00317 }

Here is the call graph for this function:

SpriteState CSSprite::buildState  ) 
 

only for compatibility issues

Definition at line 204 of file CSSprite.cpp.

References CSAction::buildState(), SpriteState::mActionState, SpriteState::mCurrentAction, SpriteState::mCurrentBundle, SpriteState::mCurrentBundleId, and ActionBundle::mDefaultAction.

00205 {
00206     static char *functionName="buildState";
00207     SpriteState state;
00208     state.mCurrentBundleId = mDefaultBundle;
00209 
00210     ActionBundleMap::iterator iter = actionMap.find(state.mCurrentBundleId);
00211     if (iter != actionMap.end())
00212     {
00213         state.mCurrentBundle = (iter->second);
00214         state.mCurrentAction = state.mCurrentBundle->mDefaultAction;
00215     }
00216     else
00217     {
00218         state.mCurrentBundle = 0;
00219         state.mCurrentAction = 0;
00220     }
00221             
00222     if (state.mCurrentAction)
00223     {
00224         state.mActionState = state.mCurrentAction->buildState();
00225     }
00226     return state;
00227 }

Here is the call graph for this function:

void CSSprite::resetState SpriteState state  ) 
 

Definition at line 232 of file CSSprite.cpp.

References SpriteState::mActionState, SpriteState::mCurrentAction, SpriteState::mCurrentBundle, SpriteState::mCurrentBundleId, ActionBundle::mDefaultAction, and CSAction::resetState().

00233 {
00234     static char *functionName="resetState";
00235     state.mCurrentBundleId = mDefaultBundle;
00236     ActionBundleMap::iterator iter = actionMap.find(state.mCurrentBundleId);
00237     if (iter != actionMap.end())
00238     {
00239         state.mCurrentBundle = iter->second;
00240         state.mCurrentAction = state.mCurrentBundle->mDefaultAction;
00241     }
00242     else
00243     {
00244         state.mCurrentBundle = 0;
00245         state.mCurrentAction = 0;
00246     }
00247     if (state.mCurrentAction)
00248     {
00249         state.mCurrentAction->resetState(state.mActionState);
00250     }
00251 }

Here is the call graph for this function:

void CSSprite::reactOnMessage CSMessage message  )  [virtual]
 

MessageListener implementation.

Implements CSMessageListener.

Definition at line 864 of file CSSprite.cpp.

References ACTION_MESSAGE, DEFAULT_MESSAGE, CSAction::getDefaultAction(), CSAction::getEnvoker(), CSAction::getId(), CSMessage::getSubtype(), CSMessage::getType(), CSAction::isAction(), CSAction::isBreakable(), ActionBundle::mActions, SpriteState::mActionState, SpriteState::mCurrentAction, SpriteState::mCurrentBundle, CSMessage::setSuccess(), SPRITE_MESSAGE, SPRITE_STATE_CHANGE_MESSAGE, CSAction::startNextAction(), and SpriteMessage::state.

00865 {
00866     static char *functionName="reactOnMessage";
00867     switch (message->getType())
00868     {
00869         case SPRITE_MESSAGE:
00870         {
00871             if (message->getSubtype() == SPRITE_STATE_CHANGE_MESSAGE)
00872             {
00873                 SpriteMessage *sMessage = (SpriteMessage *) message;
00874                 sMessage->setSuccess(changeStateTo(sMessage->state));
00875             }
00876 
00877             break;
00878         }
00879         case ACTION_MESSAGE:
00880         {
00881             // look for an action that
00882             // has as its envoker the subtype of the message!
00883             if (message->getSubtype() == DEFAULT_MESSAGE)
00884             {
00885                 if (mState.mCurrentAction->getDefaultAction() != 0)
00886                 {
00887                     for (CSActions::iterator iter = mState.mCurrentBundle->mActions->begin(); iter != mState.mCurrentBundle->mActions->end(); iter++)
00888                     {
00889                         CSAction *action = *iter;
00890                         if (action->isAction(mState.mCurrentAction->getDefaultAction()))
00891                         {
00892                             // next display NEXT round
00893                             // since we are in eventHandling now!
00894                             // wrong time to start drawing!
00895                             mState.mCurrentAction = action;
00896                             mState.mCurrentAction->startNextAction(mState.mActionState);
00897                             return;
00898                         }
00899                     }
00900                 }
00901                 return;
00902             }
00903             if (mState.mCurrentAction->isBreakable(mState.mActionState))
00904             {
00905                 for (CSActions::iterator iter = mState.mCurrentBundle->mActions->begin(); iter != mState.mCurrentBundle->mActions->end(); iter++)
00906                 {
00907                     CSAction *action = *iter;
00908                     if (action->getEnvoker() == message->getSubtype())
00909                     {
00910                         if (action->isAction(mState.mCurrentAction->getId()))
00911                         {
00912                             return;
00913                         }
00914                         mState.mCurrentAction = action;
00915                         // next display NEXT round
00916                         // since we are in eventHandling now!
00917                         //wrong time to start drawing!
00918                         mState.mCurrentAction->startNextAction(mState.mActionState);
00919                         return;
00920                     }
00921                 }
00922             }
00923         }
00924     }
00925 }

Here is the call graph for this function:

void CSSprite::setSecPerFrame float  secPerFrame  ) 
 

Definition at line 141 of file CSSprite.cpp.

References LOG_ENTER, LOG_EXIT, and CSAction::setSecPerFrame().

Referenced by CSWorld::setSecPerFrame().

00142 {
00143     static char *functionName="setSecPerFrame";
00144     LOG_ENTER 
00145     for (ActionBundleMap::iterator iter2 = actionMap.begin(); iter2 !=actionMap.end(); iter2++)
00146     {
00147         for (CSActions::iterator iter = iter2->second->mActions->begin(); iter != iter2->second->mActions->end(); iter++)
00148         {
00149             CSAction *action = *iter;
00150             action->setSecPerFrame(secPerFrame);
00151         }
00152     }
00153     LOG_EXIT
00154 }

Here is the call graph for this function:

void CSSprite::setDisplayOffset int  xOffset,
int  yOffset
 

where on the screen is 0,0 of our displaying

Definition at line 321 of file CSSprite.cpp.

References SpriteState::mActionState, ActionState::mAnimationState, AnimationState::mDisplayParams, CSDisplayParams::mXDisplayStart, and CSDisplayParams::mYDisplayStart.

Referenced by CSTileMap::addSprite(), and CSTileMap::setDisplayOffset().

00322 {
00323     static char *functionName="setDisplayOffset";
00324     mState.mActionState.mAnimationState.mDisplayParams.mXDisplayStart = xOffset;
00325     mState.mActionState.mAnimationState.mDisplayParams.mYDisplayStart = yOffset;
00326 }

void CSSprite::setWorldPosition int  x,
int  y
 

where in the world will we start drawing the screen

Definition at line 330 of file CSSprite.cpp.

References SpriteState::mActionState, ActionState::mAnimationState, AnimationState::mDisplayParams, CSDisplayParams::mXWorldStart, and CSDisplayParams::mYWorldStart.

Referenced by CSWorld::setWorldPosition().

00331 {
00332     static char *functionName="setWorldPosition";
00333     mState.mActionState.mAnimationState.mDisplayParams.mXWorldStart = x;
00334     mState.mActionState.mAnimationState.mDisplayParams.mYWorldStart = y;
00335 }

unsigned int CSSprite::checkCollisionPixels CSSprite other  ) 
 

Collision detection, returns # of pixels.

Definition at line 381 of file CSSprite.cpp.

References CSPicture::getMaxX(), CSPicture::getMaxY(), CSPicture::getPixelProjectionCount(), CSPicture::getProjection(), SpriteState::mActionState, ActionState::mAnimationState, AnimationState::mDisplayParams, AnimationState::mDisplayPicture, mState, CSDisplayParams::mXPos, and CSDisplayParams::mYPos.

Referenced by checkCollisionPixels(), and CSGame::checkCollisions().

00382 {
00383     static char *functionName="checkCollisionPixels";
00384     // values of this
00385     int myX, myY, myW,myH;
00386     CSPicture *myPicture;
00387     
00388     // values of other
00389     int oX,oY, oW,oH;
00390     CSPicture *oPicture;
00391 
00392     myPicture = mState.mActionState.mAnimationState.mDisplayPicture;
00393     if (myPicture == 0) 
00394     {
00395         return 0; // sprite was never displayed!
00396     }
00397     myX = mState.mActionState.mAnimationState.mDisplayParams.mXPos;
00398     myY = mState.mActionState.mAnimationState.mDisplayParams.mYPos;
00399     myW = myPicture->getMaxX();
00400     myH = myPicture->getMaxY();
00401 
00402     oPicture = other->mState.mActionState.mAnimationState.mDisplayPicture;
00403     if (oPicture == 0) 
00404     {
00405         return 0; // sprite was never displayed!
00406     }
00407     oX = other->mState.mActionState.mAnimationState.mDisplayParams.mXPos;
00408     oY = other->mState.mActionState.mAnimationState.mDisplayParams.mYPos;
00409     oW = oPicture->getMaxX();
00410     oH = oPicture->getMaxY();
00411 
00412     if ((myX < oX) && (myX + myW < oX)) 
00413     {
00414         return 0;   // i am x smaller
00415     }
00416     if ((myX > oX) && (myX > oX + oW)) 
00417     {
00418         return 0;   // i am x greater
00419     }
00420     if ((myY < oY) && (myY + myH < oY)) 
00421     {
00422         return 0;   // i am y smaller
00423     }
00424     if ((myY > oY) && (myY > oY + oH)) 
00425     {
00426         return 0;   // i am y greater
00427     }
00428 
00429     if ((myPicture->getPixelProjectionCount() == 0) || (oPicture->getPixelProjectionCount() == 0))
00430     {
00431         return 0;
00432     }
00433 
00434     // sprites overlap in some way - now count the pixels
00435     // use pixel projection of current displayPicture(s)
00436     unsigned char **myProjection = myPicture->getProjection();
00437     unsigned char **oProjection = oPicture->getProjection();
00438 
00439     // now get the "rectangle" where the to sprites overlap
00440     // nad check only there!
00441     int xStart, xEnd;
00442     int yStart, yEnd;
00443     int offsetOx;
00444     int offsetOy;
00445     unsigned int count = 0;
00446     if (myX < oX) xStart = oX-myX;
00447     else xStart = 0;
00448     if (myX+myW > oX+oW) xEnd = myW - ((myX+myW) - (oX+oW));
00449     else xEnd = myW;
00450 
00451     if (myY < oY) yStart = oY-myY;
00452     else yStart = 0;
00453     if (myY+myH > oY+oH) yEnd = myH - ((myY+myH) - (oY+oH));
00454     else yEnd = myH;
00455 
00456     offsetOx = myX-oX;
00457     offsetOy = myY-oY;
00458 
00459 
00460     /*
00461     printf("\nyStart: %i", yStart);
00462     printf("\nxStart: %i", xStart);
00463     printf("\nyEnd: %i", yEnd);
00464     printf("\nxEnd: %i", xEnd);
00465     printf("\noffsetOy: %i", offsetOy);
00466     printf("\noffsetOx: %i", offsetOx);
00467     */
00468 
00469     for (int y = yStart; y < yEnd; y++)
00470     {
00471         for (int x = xStart; x < xEnd; x++)
00472         {
00473             if ((myProjection[y][x] == 1) && (oProjection[y+offsetOy][x+offsetOx] == 1))
00474             {
00475                 count++;
00476             }
00477         }
00478     }
00479 /*
00480     printf("\n");
00481     for (y = yStart; y < yEnd; y++)
00482     {
00483         for (int x = xStart; x < xEnd; x++)
00484         {
00485             if (myProjection[y][x] == 1) 
00486                 printf("1");
00487             else
00488                 printf("0");
00489         }
00490         printf("\n");
00491     }
00492 
00493     printf("\n");
00494     for (y = yStart; y < yEnd; y++)
00495     {
00496         for (int x = xStart; x < xEnd; x++)
00497         {
00498             if (oProjection[y+offsetOy][x+offsetOx] == 1) 
00499                 printf("1");
00500             else
00501                 printf("0");
00502         }
00503         printf("\n");
00504     }
00505 */
00506     return count;
00507 }

Here is the call graph for this function:

unsigned int CSSprite::checkCollisionPixels CSTileMap map  ) 
 

Collision detection, returns # of pixels.

Definition at line 509 of file CSSprite.cpp.

References checkCollisionPixels(), CSTiles, CSPicture::getMaxX(), CSPicture::getMaxY(), CSTileMap::getTouchingTiles(), SpriteState::mActionState, ActionState::mAnimationState, AnimationState::mDisplayParams, AnimationState::mDisplayPicture, CSDisplayParams::mXPos, and CSDisplayParams::mYPos.

00510 {
00511     static char *functionName="checkCollisionPixels";
00512     // first get a list of all tiles, that are touched by the sprite
00513     int count = 0;
00514     int myX, myY, myW,myH;
00515     CSPicture *myPicture;
00516     myPicture = mState.mActionState.mAnimationState.mDisplayPicture;
00517     if (myPicture == 0) 
00518     {
00519         return 0; // sprite was never displayed!
00520     }
00521     myX = mState.mActionState.mAnimationState.mDisplayParams.mXPos;
00522     myY = mState.mActionState.mAnimationState.mDisplayParams.mYPos;
00523     myW = myPicture->getMaxX();
00524     myH = myPicture->getMaxY();
00525 
00526     CSTiles tiles = map->getTouchingTiles(myX,myY,myW,myH);
00527     for (CSTiles::iterator iter = tiles.begin(); iter != tiles.end(); iter++)
00528     {
00529     // than check them one by one
00530         count += checkCollisionPixels(*iter);
00531     }
00532     return count;
00533 }

Here is the call graph for this function:

unsigned int CSSprite::checkCollisionPixels CSTile tile  ) 
 

Collision detection, returns # of pixels.

Definition at line 535 of file CSSprite.cpp.

References CSTile::getAnimationState(), CSTile::getHeight(), CSPicture::getMaxX(), CSPicture::getMaxY(), CSPicture::getPixelProjectionCount(), CSPicture::getProjection(), CSTile::getWitdh(), CSTile::getX(), CSTile::getY(), SpriteState::mActionState, ActionState::mAnimationState, AnimationState::mDisplayParams, AnimationState::mDisplayPicture, AnimationState::mPicture, CSDisplayParams::mXPos, and CSDisplayParams::mYPos.

00536 {
00537     static char *functionName="checkCollisionPixels";
00538     unsigned int count = 0;
00539 
00540     // values of this
00541     int myX, myY, myW,myH;
00542     CSPicture *myPicture;
00543     
00544     // values of tile
00545     myPicture = mState.mActionState.mAnimationState.mDisplayPicture;
00546     if (myPicture == 0) 
00547     {
00548         return 0; // sprite was never displayed!
00549     }
00550     myX = mState.mActionState.mAnimationState.mDisplayParams.mXPos;
00551     myY = mState.mActionState.mAnimationState.mDisplayParams.mYPos;
00552     myW = myPicture->getMaxX();
00553     myH = myPicture->getMaxY();
00554 
00555     int oX,oY, oW,oH;
00556     CSPicture *oPicture;
00557     oPicture = tile->getAnimationState()->mPicture;
00558     if (oPicture == 0) 
00559     {
00560         return 0; // sprite was never displayed!
00561     }
00562     oY = tile->getY();
00563     oH = tile->getHeight();
00564     oX = tile->getX();
00565     oW = tile->getWitdh();
00566 
00567     if ((myPicture->getPixelProjectionCount() == 0) || (oPicture->getPixelProjectionCount() == 0))
00568     {
00569         return 0;
00570     }
00571 
00572     // pictures overlap in some way - now count the pixels
00573     // use pixel projection of current displayPicture(s)
00574     unsigned char **myProjection = myPicture->getProjection();
00575     unsigned char **oProjection = oPicture->getProjection();
00576 
00577     // now get the "rectangle" where the to pictures overlap
00578     // and check only there!
00579     int xStart, xEnd;
00580     int yStart, yEnd;
00581     int offsetOx;
00582     int offsetOy;
00583     if (myX < oX) xStart = oX-myX;
00584     else xStart = 0;
00585     if (myX+myW > oX+oW) xEnd = myW - ((myX+myW) - (oX+oW));
00586     else xEnd = myW;
00587 
00588     if (myY < oY) yStart = oY-myY;
00589     else yStart = 0;
00590     if (myY+myH > oY+oH) yEnd = myH - ((myY+myH) - (oY+oH));
00591     else yEnd = myH;
00592 
00593     offsetOx = myX-oX;
00594     offsetOy = myY-oY;
00595     /*
00596     printf("\nyStart: %i", yStart);
00597     printf("\nxStart: %i", xStart);
00598     printf("\nyEnd: %i", yEnd);
00599     printf("\nxEnd: %i", xEnd);
00600     printf("\noffsetOy: %i", offsetOy);
00601     printf("\noffsetOx: %i", offsetOx);
00602     */
00603 
00604     for (int y = yStart; y < yEnd; y++)
00605     {
00606         for (int x = xStart; x < xEnd; x++)
00607         {
00608             if ((myProjection[y][x] == 1) && (oProjection[y+offsetOy][x+offsetOx] == 1))
00609             {
00610                 count++;
00611             }
00612         }
00613     }
00614 /*
00615     printf("\n");
00616     for (y = yStart; y < yEnd; y++)
00617     {
00618         for (int x = xStart; x < xEnd; x++)
00619         {
00620             if (myProjection[y][x] == 1) 
00621                 printf("1");
00622             else
00623                 printf("0");
00624         }
00625         printf("\n");
00626     }
00627 
00628     printf("\n");
00629     for (y = yStart; y < yEnd; y++)
00630     {
00631         for (int x = xStart; x < xEnd; x++)
00632         {
00633             if (oProjection[y+offsetOy][x+offsetOx] == 1) 
00634                 printf("1");
00635             else
00636                 printf("0");
00637         }
00638         printf("\n");
00639     }
00640 */ 
00641     return count;
00642 }

Here is the call graph for this function:

bool CSSprite::checkCollision CSSprite other  ) 
 

Collision detection, returns boolean.

Definition at line 644 of file CSSprite.cpp.

References CSPicture::getMaxX(), CSPicture::getMaxY(), CSPicture::getPixelProjectionCount(), CSPicture::getProjection(), SpriteState::mActionState, ActionState::mAnimationState, AnimationState::mDisplayParams, AnimationState::mDisplayPicture, mState, CSDisplayParams::mXPos, and CSDisplayParams::mYPos.

Referenced by checkCollision(), CSGame::checkCollisions(), and getCollidingTiles().

00645 {
00646     static char *functionName="checkCollision";
00647     // values of this
00648     int myX, myY, myW,myH;
00649     CSPicture *myPicture;
00650 
00651     myPicture = mState.mActionState.mAnimationState.mDisplayPicture;
00652     if (myPicture == 0) return 0; 
00653     {
00654         return 0; // sprite was never displayed!
00655     }
00656     myX = mState.mActionState.mAnimationState.mDisplayParams.mXPos;
00657     myY = mState.mActionState.mAnimationState.mDisplayParams.mYPos;
00658     myW = myPicture->getMaxX();
00659     myH = myPicture->getMaxY();
00660 
00661     // values of other
00662     int oX,oY, oW,oH;
00663     CSPicture *oPicture;
00664     oPicture = other->mState.mActionState.mAnimationState.mDisplayPicture;
00665     if (oPicture == 0) 
00666     {
00667         return 0; // sprite was never displayed!
00668     }
00669     oX = other->mState.mActionState.mAnimationState.mDisplayParams.mXPos;
00670     oY = other->mState.mActionState.mAnimationState.mDisplayParams.mYPos;
00671     oW = oPicture->getMaxX();
00672     oH = oPicture->getMaxY();
00673 
00674     if ((myX < oX) && (myX + myW < oX)) 
00675     {
00676         return 0;   // i am x smaller
00677     }
00678     if ((myX > oX) && (myX > oX + oW)) 
00679     {
00680         return 0;   // i am x greater
00681     }
00682     if ((myY < oY) && (myY + myH < oY)) 
00683     {
00684         return 0;   // i am y smaller
00685     }
00686     if ((myY > oY) && (myY > oY + oH)) 
00687     {
00688         return 0;   // i am y greater
00689     }
00690 
00691     if ((myPicture->getPixelProjectionCount() == 0) || (oPicture->getPixelProjectionCount() == 0))
00692     {
00693         return false;
00694     }
00695 
00696     // sprites overlap in some way - check if collide
00697     // use pixel projection of current displayPicture(s)
00698     unsigned char **myProjection = myPicture->getProjection();
00699     unsigned char **oProjection = oPicture->getProjection();
00700 
00701     // now get the "rectangle" where the to sprites overlap
00702     // and check only there!
00703     int xStart, xEnd;
00704     int yStart, yEnd;
00705     int offsetOx;
00706     int offsetOy;
00707     if (myX < oX) xStart = oX-myX;
00708     else xStart = 0;
00709     if (myX+myW > oX+oW) xEnd = myW - ((myX+myW) - (oX+oW));
00710     else xEnd = myW;
00711 
00712     if (myY < oY) yStart = oY-myY;
00713     else yStart = 0;
00714     if (myY+myH > oY+oH) yEnd = myH - ((myY+myH) - (oY+oH));
00715     else yEnd = myH;
00716 
00717     offsetOx = myX-oX;
00718     offsetOy = myY-oY;
00719 
00720     for (int y = yStart; y < yEnd; y++)
00721     {
00722         for (int x = xStart; x < xEnd; x++)
00723         {
00724             if ((myProjection[y][x] == 1) && (oProjection[y+offsetOy][x+offsetOx] == 1))
00725             {
00726                 return true;
00727             }
00728         }
00729     }
00730     return false;
00731 }

Here is the call graph for this function:

bool CSSprite::checkCollision CSTileMap map  ) 
 

Collision detection, returns boolean.

Definition at line 733 of file CSSprite.cpp.

References checkCollision(), CSTiles, CSPicture::getMaxX(), CSPicture::getMaxY(), CSTileMap::getTouchingTiles(), SpriteState::mActionState, ActionState::mAnimationState, AnimationState::mDisplayParams, AnimationState::mDisplayPicture, CSDisplayParams::mXPos, and CSDisplayParams::mYPos.

00734 {
00735     static char *functionName="checkCollision";
00736     // first get a list of all tiles, that are touched by the sprite
00737     int myX, myY, myW,myH;
00738     CSPicture *myPicture;
00739     myPicture = mState.mActionState.mAnimationState.mDisplayPicture;
00740     if (myPicture == 0) 
00741     {
00742         return 0; // sprite was never displayed!
00743     }
00744     myX = mState.mActionState.mAnimationState.mDisplayParams.mXPos;
00745     myY = mState.mActionState.mAnimationState.mDisplayParams.mYPos;
00746     myW = myPicture->getMaxX();
00747     myH = myPicture->getMaxY();
00748 
00749     CSTiles tiles = map->getTouchingTiles(myX,myY,myW,myH);
00750     for (CSTiles::iterator iter = tiles.begin(); iter != tiles.end(); iter++)
00751     {
00752         if (checkCollision(*iter))
00753         {
00754             return true;
00755         }
00756     }
00757     return false;
00758 }

Here is the call graph for this function:

bool CSSprite::checkCollision CSTile tile  ) 
 

Collision detection, returns boolean.

Definition at line 760 of file CSSprite.cpp.

References CSTile::getAnimationState(), CSTile::getHeight(), CSPicture::getMaxX(), CSPicture::getMaxY(), CSPicture::getPixelProjectionCount(), CSPicture::getProjection(), CSTile::getWitdh(), CSTile::getX(), CSTile::getY(), SpriteState::mActionState, ActionState::mAnimationState, AnimationState::mDisplayParams, AnimationState::mDisplayPicture, AnimationState::mPicture, CSDisplayParams::mXPos, and CSDisplayParams::mYPos.

00761 {
00762     static char *functionName="checkCollision";
00763     // values of this
00764     CSPicture *myPicture;
00765     int myX, myY, myW,myH;
00766     
00767     myPicture = mState.mActionState.mAnimationState.mDisplayPicture;
00768     if (myPicture == 0) 
00769     {
00770         return 0; // sprite was never displayed!
00771     }
00772     myX = mState.mActionState.mAnimationState.mDisplayParams.mXPos;
00773     myY = mState.mActionState.mAnimationState.mDisplayParams.mYPos;
00774     myW = myPicture->getMaxX();
00775     myH = myPicture->getMaxY();
00776 
00777     // values of tile
00778     CSPicture *oPicture;
00779     int oX,oY, oW,oH;
00780     oPicture = tile->getAnimationState()->mPicture;
00781     if (oPicture == 0) 
00782     {
00783         return 0; // sprite was never displayed!
00784     }
00785     oY = tile->getY();
00786     oH = tile->getHeight();
00787     oX = tile->getX();
00788     oW = tile->getWitdh();
00789 
00790     if ((myPicture->getPixelProjectionCount() == 0) || (oPicture->getPixelProjectionCount() == 0))
00791     {
00792         return false;
00793     }
00794 
00795     // sprites overlap in some way - check if collide
00796     // use pixel projection of current displayPicture(s)
00797     unsigned char **myProjection = myPicture->getProjection();
00798     unsigned char **oProjection = oPicture->getProjection();
00799 
00800     // now get the "rectangle" where the to sprites overlap
00801     // and check only there!
00802     int xStart, xEnd;
00803     int yStart, yEnd;
00804     int offsetOx;
00805     int offsetOy;
00806     if (myX < oX) xStart = oX-myX;
00807     else xStart = 0;
00808     if (myX+myW > oX+oW) xEnd = myW - ((myX+myW) - (oX+oW));
00809     else xEnd = myW;
00810 
00811     if (myY < oY) yStart = oY-myY;
00812     else yStart = 0;
00813     if (myY+myH > oY+oH) yEnd = myH - ((myY+myH) - (oY+oH));
00814     else yEnd = myH;
00815 
00816     offsetOx = myX-oX;
00817     offsetOy = myY-oY;
00818 
00819     for (int y = yStart; y < yEnd; y++)
00820     {
00821         for (int x = xStart; x < xEnd; x++)
00822         {
00823             if ((myProjection[y][x] == 1) && (oProjection[y+offsetOy][x+offsetOx] == 1))
00824             {
00825                 return true;
00826             }
00827         }
00828     }
00829     return false;
00830 }

Here is the call graph for this function:

CSTiles CSSprite::getCollidingTiles CSTileMap map  ) 
 

return all tiles, that have pixels colliding with sprite

Definition at line 832 of file CSSprite.cpp.

References checkCollision(), CSTiles, CSPicture::getMaxX(), CSPicture::getMaxY(), CSTileMap::getTouchingTiles(), SpriteState::mActionState, ActionState::mAnimationState, AnimationState::mDisplayParams, AnimationState::mDisplayPicture, CSDisplayParams::mXPos, and CSDisplayParams::mYPos.

Referenced by CSGame::checkCollisions().

00833 {
00834     static char *functionName="getCollidingTiles";
00835     // first get a list of all tiles, that are touched by the sprite
00836     int myX, myY, myW,myH;
00837     CSPicture *myPicture;
00838     myPicture = mState.mActionState.mAnimationState.mDisplayPicture;
00839     if (myPicture == 0) 
00840     {
00841         return 0; // sprite was never displayed!
00842     }
00843     myX = mState.mActionState.mAnimationState.mDisplayParams.mXPos;
00844     myY = mState.mActionState.mAnimationState.mDisplayParams.mYPos;
00845     myW = myPicture->getMaxX();
00846     myH = myPicture->getMaxY();
00847 
00848     CSTiles tiles = map->getTouchingTiles(myX,myY,myW,myH);
00849     CSTiles::iterator iter = tiles.begin();
00850     while (iter != tiles.end())
00851     {
00852         if (!checkCollision(*iter))
00853         {
00854             iter = tiles.erase(iter);
00855         }
00856         else
00857         {
00858             iter++;
00859         }
00860     }
00861     return tiles;
00862 }

Here is the call graph for this function:

void CSSprite::setSpeedY int  speed  ) 
 

overrides values in animation

Definition at line 344 of file CSSprite.cpp.

References SpriteState::mActionState, ActionState::mAnimation, ActionState::mAnimationState, and CSAnimation::setSpeedY().

00345 {
00346     static char *functionName="setSpeedY";
00347     mState.mActionState.mAnimation->setSpeedY(speed,mState.mActionState.mAnimationState);
00348 }

Here is the call graph for this function:

void CSSprite::setSpeedX int  speed  ) 
 

overrides values in animation

Definition at line 351 of file CSSprite.cpp.

References SpriteState::mActionState, ActionState::mAnimation, ActionState::mAnimationState, and CSAnimation::setSpeedX().

00352 {
00353     static char *functionName="setSpeedX";
00354     mState.mActionState.mAnimation->setSpeedX(speed,mState.mActionState.mAnimationState);   
00355 }

Here is the call graph for this function:

void CSSprite::adjustSpeed int  speed  ) 
 

overrides values in animation

Definition at line 358 of file CSSprite.cpp.

References CSAnimation::adjustSpeed(), SpriteState::mActionState, ActionState::mAnimation, and ActionState::mAnimationState.

Referenced by CSGame::disableSpriteMovement(), and CSGame::initLevel().

00359 {
00360     static char *functionName="adjustSpeed";
00361     mState.mActionState.mAnimation->adjustSpeed(speed,mState.mActionState.mAnimationState);
00362 }

Here is the call graph for this function:

void CSSprite::resetSpeed  ) 
 

returns to speed values of animation (default)

Definition at line 365 of file CSSprite.cpp.

References SpriteState::mActionState, ActionState::mAnimation, ActionState::mAnimationState, and CSAnimation::resetSpeed().

00366 {
00367     static char *functionName="resetSpeed";
00368     mState.mActionState.mAnimation->resetSpeed(mState.mActionState.mAnimationState);
00369 }

Here is the call graph for this function:

void CSSprite::setNavigator CSNavigator navigator  ) 
 

Definition at line 337 of file CSSprite.cpp.

References CSNavigator::setSprite().

00338 {
00339     static char *functionName="setNavigator";
00340     mNavigator = navigator;
00341     navigator->setSprite(this);
00342 }

Here is the call graph for this function:

CSNavigator* CSSprite::getNavigator  )  [inline]
 

Definition at line 316 of file CSSprite.h.

00316 {return mNavigator;}

void CSSprite::setAlpha int  alpha  ) 
 

inlines

Definition at line 156 of file CSSprite.cpp.

References LOG_ENTER, LOG_EXIT, and CSAction::setAlpha().

00157 {
00158     static char *functionName="setAlpha";
00159     LOG_ENTER 
00160     for (ActionBundleMap::iterator iter2 = actionMap.begin(); iter2 !=actionMap.end(); iter2++)
00161     {
00162         for (CSActions::iterator iter = iter2->second->mActions->begin(); iter != iter2->second->mActions->end(); iter++)
00163         {
00164             CSAction *action = *iter;
00165             action->setAlpha(alpha);
00166         }
00167     }
00168     LOG_EXIT
00169 }

Here is the call graph for this function:

unsigned int CSSprite::getWorldPos  )  [inline]
 

Definition at line 320 of file CSSprite.h.

Referenced by CSTileMap::addScaledSprites(), CSTileMap::display(), CSGame::initLevel(), CSTileMap::setActive(), and CSTileMap::update().

00320 {return mWorldPos;}

SpriteState CSSprite::getState  )  [inline]
 

Definition at line 321 of file CSSprite.h.

Referenced by CSTileMap::addScaledSprites().

00321 {return mState;}

void CSSprite::setState const SpriteState state  )  [inline]
 

Definition at line 322 of file CSSprite.h.

00322 {mState = state;}

void CSSprite::setLayer int  z  )  [inline]
 

Definition at line 323 of file CSSprite.h.

00323 {layer_position = z;}

int CSSprite::getLayer  )  [inline]
 

Definition at line 324 of file CSSprite.h.

Referenced by CSWorld::addSprite(), and CSWorld::removeSprite().

00324 {return layer_position;}

int CSSprite::getLayerY  )  [inline]
 

Definition at line 325 of file CSSprite.h.

00325 {return y_position;}

void CSSprite::clearNavigator  )  [inline]
 

Definition at line 326 of file CSSprite.h.

00326 {mNavigator = 0;}

void CSSprite::resetNavigator  ) 
 

Definition at line 252 of file CSSprite.cpp.

References CSNavigator::reset().

00253 {
00254     static char *functionName="resetNavigator";
00255     if (mNavigator!=0) 
00256     {
00257         mNavigator->reset();
00258     }
00259 }

Here is the call graph for this function:

int CSSprite::getMaxWidth  )  [inline]
 

Definition at line 328 of file CSSprite.h.

Referenced by CSGame::checkOutOfBounds(), and CSWorld::displayScaled().

00328 {return mWidth;}

int CSSprite::getMaxHeight  )  [inline]
 

Definition at line 329 of file CSSprite.h.

Referenced by CSGame::checkOutOfBounds(), and CSWorld::displayScaled().

00329 {return mHeight;}

int CSSprite::getSpriteType  )  [inline]
 

Definition at line 330 of file CSSprite.h.

00330 {return mType;}

int CSSprite::getSubtype  )  [inline]
 

Definition at line 331 of file CSSprite.h.

00331 {return mSubtype;}

int CSSprite::getCurrentStateId  )  [inline]
 

Definition at line 332 of file CSSprite.h.

References SpriteState::mCurrentBundleId.

00332 {return mState.mCurrentBundleId;}

std::string CSSprite::getFilename  )  [inline]
 

Definition at line 333 of file CSSprite.h.

Referenced by CSGame::getLevelDataNum().

00333 {return mFilename;}

std::string CSSprite::getId  )  [inline]
 

Definition at line 334 of file CSSprite.h.

00334 {return mId;}

void CSSprite::setPosition int  x,
int  y,
unsigned int  z
 

Definition at line 371 of file CSSprite.cpp.

References SpriteState::mActionState, ActionState::mAnimationState, AnimationState::mDisplayParams, AnimationState::mXPos, CSDisplayParams::mXPos, AnimationState::mYPos, and CSDisplayParams::mYPos.

Referenced by CSGame::initLevel(), and CSGame::resetSprites().

00372 {
00373     static char *functionName="setPosition";
00374     mWorldPos = z;
00375     mState.mActionState.mAnimationState.mDisplayParams.mXPos = x;
00376     mState.mActionState.mAnimationState.mDisplayParams.mYPos = y;
00377     mState.mActionState.mAnimationState.mXPos = x;
00378     mState.mActionState.mAnimationState.mYPos = y;
00379 }

int CSSprite::getUserState void   )  [inline]
 

Definition at line 337 of file CSSprite.h.

References SpriteState::mUserState.

00337 {return mState.mUserState;}

void CSSprite::setUserState int  userState  )  [inline]
 

Definition at line 338 of file CSSprite.h.

References SpriteState::mUserState.

00338 {mState.mUserState = userState;}

SDL_Surface* CSSprite::getScaledSprite double  factor  )  [inline]
 

Definition at line 340 of file CSSprite.h.

References CSAnimation::getScaledAnimation(), SpriteState::mActionState, ActionState::mAnimation, and ActionState::mAnimationState.

Referenced by CSTileMap::addScaledSprites().

00341         {
00342             return mState.mActionState.mAnimation->getScaledAnimation(mState.mActionState.mAnimationState, factor);
00343         }

Here is the call graph for this function:

const char* CSSprite::getCurrentActionId  )  [inline]
 

Definition at line 345 of file CSSprite.h.

References CSAction::getId(), and SpriteState::mCurrentAction.

00346         {
00347             return mState.mCurrentAction->getId();
00348         }

Here is the call graph for this function:

int CSSprite::getCurrentActionType  )  [inline]
 

return x start of surface coordinates of where the < the sprite will be drawn during next display

Definition at line 349 of file CSSprite.h.

References CSAction::getActionType(), and SpriteState::mCurrentAction.

Here is the call graph for this function:

unsigned int CSSprite::getDisplayXLower  )  [inline]
 

return y start of surface coordinates of where the < the sprite will be drawn during next display

Definition at line 357 of file CSSprite.h.

References SpriteState::mActionState, ActionState::mAnimationState, AnimationState::mDisplayParams, CSDisplayParams::mXDisplayStart, CSDisplayParams::mXPos, and CSDisplayParams::mXWorldStart.

Referenced by getDisplayXUpper().

unsigned int CSSprite::getDisplayYLower  )  [inline]
 

return x end of surface coordinates of where the < the sprite will be drawn during next display

Definition at line 368 of file CSSprite.h.

References SpriteState::mActionState, ActionState::mAnimationState, AnimationState::mDisplayParams, CSDisplayParams::mYDisplayStart, CSDisplayParams::mYPos, and CSDisplayParams::mYWorldStart.

Referenced by getDisplayYUpper().

unsigned int CSSprite::getDisplayXUpper  )  [inline]
 

return y end of surface coordinates of where the < the sprite will be drawn during next display

Definition at line 379 of file CSSprite.h.

References getDisplayXLower().

Here is the call graph for this function:

unsigned int CSSprite::getDisplayYUpper  )  [inline]
 

return the world x-coordinate of sprite that will be used during < the next display

Definition at line 388 of file CSSprite.h.

References getDisplayYLower().

Here is the call graph for this function:

int CSSprite::getXPos  )  [inline]
 

return the world y-coordinate of sprite that will be used during < the next display

Definition at line 397 of file CSSprite.h.

References SpriteState::mActionState, ActionState::mAnimationState, AnimationState::mDisplayParams, and CSDisplayParams::mXPos.

Referenced by CSGame::checkOutOfBounds(), and CSWorld::displayScaled().

int CSSprite::getYPos  )  [inline]
 

Definition at line 404 of file CSSprite.h.

References SpriteState::mActionState, ActionState::mAnimationState, AnimationState::mDisplayParams, and CSDisplayParams::mYPos.

Referenced by CSGame::checkOutOfBounds(), and CSWorld::displayScaled().

00405         {
00406             return mState.mActionState.mAnimationState.mDisplayParams.mYPos;
00407         }

void CSSprite::setActive bool  active  )  [inline]
 

Parameters:
active  pos + width of sprite pos + height of sprite

Definition at line 408 of file CSSprite.h.

Referenced by CSTileMap::setActive().

00409         {
00410             mActive = active;
00411         }


Field Documentation

const char * CSSprite::CLASS = "CSSprite" [static]
 

Reimplemented from CSMessageDispatchable.

Definition at line 13 of file CSSprite.cpp.

Referenced by getType().


Generated on Wed Jul 14 00:45:07 2004 for CSLib by doxygen 1.3.6