- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# C:/Documents and Settings/jegan/Desktop/projectX/ShowOneSprite.cpp

Go to the documentation of this file.

    00001 #include "ShowOneSprite.h"
    00002
    00003 using namespace std;
    00004
    00005 ShowOneSprite::ShowOneSprite( std::string filename, List *spriteInfo )
    00006 :_index(0)
    00007 {
    00008         _filename = filename;
    00009
    00010         bool bBox = false;;
    00011
    00012         while(!spriteInfo->empty()) {
    00013                 List *nextSprite = spriteInfo->firstList();
    00014
    00015                 if(nextSprite->firstString() == "sprite") {
    00016                         List *spriteParams = nextSprite->rest();
    00017                         Sprite *sprite = NULL;
    00018
    00019                         while(!spriteParams->empty()) {
    00020                                 if(spriteParams->firstList()->firstString() == "filename") {
    00021                                         sprite = Sprite::loadSprite(spriteParams->firstList()->rest()->firstString());
    00022                                 }
    00023                                 else if(spriteParams->firstList()->firstString() == "origin" && sprite) {
    00024                                         List *boxInfo = spriteParams->firstList()->rest();
    00025
    00026                                         SDL_Rect rect = sprite->rect();
    00027
    00028                                         rect.x = boxInfo->firstInt();
    00029                                         boxInfo = boxInfo->rest();
    00030                                         rect.y = boxInfo->firstInt();
    00031
    00032                                         sprite->setRect(rect);
    00033                                         bBox = true;
    00034                                 }
    00035                                 spriteParams = spriteParams->rest();
    00036                         }
    00037
    00038                         _sprites.push_back(sprite);
    00039                 }
    00040                 else if(nextSprite->firstString() == "size") {
    00041                         _box.w = nextSprite->rest()->firstInt();
    00042                         _box.h = nextSprite->rest()->rest()->firstInt();
    00043                 }
    00044
    00045                 spriteInfo = spriteInfo->rest();
    00046         }
    00047
    00048
    00049         /*
    00050         if(!bBox) {
    00051                 _rect = _sprites[0]->rect();
    00052                 for(size_t i = 1; i < _sprites.size(); i++) {
    00053                         if(_sprites[i]->rect().h != _rect.h || _sprites[i]->rect().w != _rect.w) {
    00054                                 cerr << "Showone sprite not of consistent size: " << _sprites[i]->filename()
    00055                                         << " conflicts in " << _filename << endl;
    00056                         }
    00057                 }
    00058                 _box = _rect;
    00059         }
    00060         */
    00061 }
    00062
    00063 ShowOneSprite::~ShowOneSprite()
    00064 {
    00065         clean();
    00066 }
    00067
    00068 void ShowOneSprite::clean()
    00069 {
    00070         for(size_t i = 0; i < _createdSprites.size(); i++)
    00071                 delete _createdSprites[i];
    00072
    00073         _createdSprites.clear();
    00074 }
    00075
    00076 ShowOneSprite::ShowOneSprite(ShowOneSprite *sprite)
    00077 :Sprite(sprite),_index(0)
    00078 {
    00079         _sprites = sprite->_sprites;
    00080 }
    00081
    00082 void ShowOneSprite::draw(int x, int y, bool origin)
    00083 {
    00084         xDirection tempDir = _sprites[_index]->getDirection();
    00085         SDL_Rect tempBox = _sprites[ _index ]->box();
    00086         _sprites[_index]->setDirection(_direction);
    00087         _sprites[_index]->setBox( _box );
    00088         _sprites[_index]->draw(x, y, origin);
    00089         _sprites[_index]->setDirection(tempDir);
    00090         _sprites[_index]->setBox( tempBox );
    00091 }
    00092
    00093 Sprite *ShowOneSprite::instance()
    00094 {
    00095         Sprite *sprite = new ShowOneSprite(this);
    00096         _createdSprites.push_back(sprite);
    00097         return sprite;
    00098 }
    00099
    00100 void ShowOneSprite::reset()
    00101 {
    00102         for(size_t i = 0; i < _sprites.size(); i++)
    00103                 _sprites[i]->reset();
    00104 }
    00105
    00106 void ShowOneSprite::reset(int index)
    00107 {
    00108         _sprites[index]->reset();
    00109 }