- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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

Go to the documentation of this file.

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