149 lines
3.0 KiB
C++
149 lines
3.0 KiB
C++
/* [DESTRUCTUR] Destruction de l'animation
|
|
=========================================================*/
|
|
xSpriteAnimation::~xSpriteAnimation(){
|
|
|
|
SDL_DestroyTexture( _texture );
|
|
_manager = NULL;
|
|
_manager = NULL;
|
|
|
|
// On supprime les frames
|
|
_frames.erase( _frames.begin(), _frames.end() );
|
|
}
|
|
|
|
|
|
/* [CONSTRUCTOR] Construction de l'animation (chargement)
|
|
=========================================================*/
|
|
xSpriteAnimation::xSpriteAnimation(xManager *manager, const char *url, SDL_Rect dest)
|
|
: xSprite( manager, url ){
|
|
|
|
// On definit le viewport
|
|
this->dimensions(dest);
|
|
}
|
|
|
|
|
|
/* [CONSTRUCTOR] Construction de l'animation a partir d'une texture existante
|
|
=========================================================*/
|
|
xSpriteAnimation::xSpriteAnimation(xManager *manager, SDL_Texture *t, SDL_Rect dest)
|
|
: xSprite( manager, t) {
|
|
|
|
// On definit le viewport
|
|
this->dimensions(dest);
|
|
}
|
|
|
|
|
|
/* [ADDFRAME] Ajout d'une frame d'animation
|
|
=========================================================*/
|
|
void xSpriteAnimation::addFrame(SDL_Rect clip){
|
|
|
|
// On ajoute une frame
|
|
_frames.push_back( (SDL_Rect){
|
|
clip.x,
|
|
clip.y,
|
|
clip.w,
|
|
clip.h
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* [CLEAR] Supprime toutes les frames
|
|
=========================================================*/
|
|
void xSpriteAnimation::clear(){
|
|
_frames.erase(_frames.begin(), _frames.end());
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [PUSH] Ajoute au rendu
|
|
=========================================================*/
|
|
void xSpriteAnimation::push(string index){
|
|
_index = index;
|
|
|
|
// Gestion erreur si aucune frame
|
|
if( _frames.size() == 0 )
|
|
return;
|
|
|
|
_src = _frames[0];
|
|
|
|
/* (1) On ajoute le sprite au rendu */
|
|
_manager->push(index, (xSprite*)this);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [TRHEAD] Process de l'animation
|
|
=========================================================*/
|
|
void xSpriteAnimationProcess(xSpriteAnimation *xSA, int t, int flags){
|
|
int timer = 0;
|
|
bool way = true;
|
|
int start = 0;
|
|
int stop = xSA->_frames.size();
|
|
|
|
while( flags&SPRITE_ANIM_INFINITE ){
|
|
|
|
/* (1) Pour chaque sprite */
|
|
for( int i = start ; i != xSA->_frames.size() ; i+=(way?1:-1) ){
|
|
timer = SDL_GetTicks();
|
|
|
|
|
|
// On met a jour la frame (_src uniquement)
|
|
xSA->_src = xSA->_frames.at(i);
|
|
|
|
xSA->manager()->update();
|
|
|
|
|
|
if( SDL_GetTicks()-timer < t )
|
|
SDL_Delay( t - (SDL_GetTicks()-timer) );
|
|
}
|
|
|
|
/* (2) Gestion des flags */
|
|
|
|
// SPRITE_ANIM_REVERSE
|
|
if( flags&SPRITE_ANIM_REVERSE ){
|
|
way = !way;
|
|
start = (way) ? 0 : xSA->_frames.size()-1;
|
|
stop = (way) ? xSA->_frames.size()-1 : 0;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
/* (n) On termine le thread */
|
|
return;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* [START] Ajoute l'animation au rendu
|
|
=========================================================*/
|
|
void xSpriteAnimation::start(int t, int flags){
|
|
_timeout = t;
|
|
_flags = flags;
|
|
|
|
/* (1) 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(){
|
|
/* (1) On arrete l'animation */
|
|
delete _animation;
|
|
|
|
// On nettoie le pointeur
|
|
_animation = NULL;
|
|
} |