/* [DESTRUCTUR] Destruction de l'animation =========================================================*/ xSpriteAnimation::~xSpriteAnimation(){ SDL_DestroyTexture( _texture ); _manager = NULL; _texture = NULL; } /* [CONSTRUCTOR] Construction de l'animation (chargement) =========================================================*/ xSpriteAnimation::xSpriteAnimation(xManager *manager, const char *url, SDL_Rect viewport){ /* (1) Definition des attributs */ _manager = manager; _texture = NULL; _clip = viewport; /* (2) On charge le spritesheet */ _texture = IMG_LoadTexture( _manager->renderer(), url ); // Gestion erreur if( _texture == NULL ) return; } /* [ADDFRAME] Ajout d'une frame d'animation =========================================================*/ void xSpriteAnimation::addFrame(float x, float y){ // On ajoute une frame _frames.push_back( (SDL_Rect){ (int) x*_clip.w, (int) y*_clip.h, (int) _clip.w, (int) _clip.h }); } /* [TRHEAD] Process de l'animation =========================================================*/ void xSpriteAnimationProcess(xSpriteAnimation *xSA, int t, int flags){ int length = xSA->_frames.size(); int timer = 0; int step = 1; int start = 0; int stop = length; while( flags&SPRITE_ANIM_INFINITE ){ /* (1) Pour chaque sprite */ for( int i = start ; i != stop ; i+=step ){ timer = SDL_GetTicks(); // On met a jour la frame xSA->_frame = xSA->_frames[i]; if( SDL_GetTicks()-timer < t ) SDL_Delay( t - (SDL_GetTicks()-timer) ); } /* (2) Gestion des flags */ // SPRITE_ANIM_REVERSE if( flags&SPRITE_ANIM_REVERSE ){ step *= -1; start = (step==1) ? 0 : length-1; stop = (step==1) ? length-1 : 0; } } /* (n) On termine le thread */ return; } /* [START] Ajoute l'animation au rendu =========================================================*/ void xSpriteAnimation::start(int t, int flags){ /* (1) On ajoute le sprite au rendu */ _manager->push(_texture, &_frame, &_clip); /* (2) On lance l'animation */ _animation = new thread(xSpriteAnimationProcess, this, t, flags); // On attends pas le thread _animation->detach(); } /* [STOP] Arrete l'animation =========================================================*/ void xSpriteAnimation::stop(){ delete _animation; }