2016-03-12 23:22:28 +00:00
|
|
|
/* [DESTRUCTUR] Destruction de l'animation
|
|
|
|
=========================================================*/
|
|
|
|
xSpriteAnimation::~xSpriteAnimation(){
|
2016-03-14 18:28:18 +00:00
|
|
|
|
2016-03-12 23:22:28 +00:00
|
|
|
SDL_DestroyTexture( _texture );
|
|
|
|
_manager = NULL;
|
2016-03-14 18:28:18 +00:00
|
|
|
_manager = NULL;
|
2016-03-13 13:53:39 +00:00
|
|
|
|
2016-03-14 18:28:18 +00:00
|
|
|
// On supprime les frames
|
|
|
|
_frames.erase( _frames.begin(), _frames.end() );
|
2016-03-13 13:53:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-14 18:28:18 +00:00
|
|
|
/* [CONSTRUCTOR] Construction de l'animation (chargement)
|
2016-03-13 13:53:39 +00:00
|
|
|
=========================================================*/
|
2016-03-14 18:28:18 +00:00
|
|
|
xSpriteAnimation::xSpriteAnimation(xManager *manager, const char *url, SDL_Rect dest)
|
|
|
|
: xSprite( manager, url ){
|
2016-03-13 17:33:47 +00:00
|
|
|
|
2016-03-14 18:28:18 +00:00
|
|
|
// On definit le viewport
|
|
|
|
this->dimensions(dest);
|
2016-03-13 17:33:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-14 18:28:18 +00:00
|
|
|
/* [CONSTRUCTOR] Construction de l'animation a partir d'une texture existante
|
2016-03-13 17:33:47 +00:00
|
|
|
=========================================================*/
|
2016-03-14 18:28:18 +00:00
|
|
|
xSpriteAnimation::xSpriteAnimation(xManager *manager, SDL_Texture *t, SDL_Rect dest)
|
|
|
|
: xSprite( manager, t) {
|
2016-03-14 15:10:21 +00:00
|
|
|
|
2016-03-14 18:28:18 +00:00
|
|
|
// On definit le viewport
|
|
|
|
this->dimensions(dest);
|
2016-03-12 23:22:28 +00:00
|
|
|
}
|
|
|
|
|
2016-03-13 13:53:39 +00:00
|
|
|
|
2016-03-12 23:22:28 +00:00
|
|
|
/* [ADDFRAME] Ajout d'une frame d'animation
|
|
|
|
=========================================================*/
|
2016-03-13 09:08:07 +00:00
|
|
|
void xSpriteAnimation::addFrame(SDL_Rect clip){
|
2016-03-12 23:22:28 +00:00
|
|
|
|
|
|
|
// On ajoute une frame
|
|
|
|
_frames.push_back( (SDL_Rect){
|
2016-03-13 09:08:07 +00:00
|
|
|
clip.x,
|
|
|
|
clip.y,
|
|
|
|
clip.w,
|
|
|
|
clip.h
|
|
|
|
} );
|
2016-03-12 23:22:28 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-14 15:10:21 +00:00
|
|
|
|
|
|
|
/* [CLEAR] Supprime toutes les frames
|
|
|
|
=========================================================*/
|
|
|
|
void xSpriteAnimation::clear(){
|
2016-03-14 18:28:18 +00:00
|
|
|
_frames.erase(_frames.begin(), _frames.end());
|
2016-03-14 15:10:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-13 19:36:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [PUSH] Ajoute au rendu
|
|
|
|
=========================================================*/
|
|
|
|
void xSpriteAnimation::push(string index){
|
2016-03-13 23:06:33 +00:00
|
|
|
_index = index;
|
|
|
|
|
2016-03-14 15:10:21 +00:00
|
|
|
// Gestion erreur si aucune frame
|
|
|
|
if( _frames.size() == 0 )
|
|
|
|
return;
|
|
|
|
|
2016-03-14 18:28:18 +00:00
|
|
|
_src = _frames[0];
|
2016-03-14 15:10:21 +00:00
|
|
|
|
2016-03-13 19:36:16 +00:00
|
|
|
/* (1) On ajoute le sprite au rendu */
|
2016-03-15 23:15:18 +00:00
|
|
|
_manager->push(index, (xSprite*)this);
|
2016-03-13 19:36:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-03-12 23:22:28 +00:00
|
|
|
/* [TRHEAD] Process de l'animation
|
|
|
|
=========================================================*/
|
|
|
|
void xSpriteAnimationProcess(xSpriteAnimation *xSA, int t, int flags){
|
|
|
|
int timer = 0;
|
2016-03-14 12:14:35 +00:00
|
|
|
bool way = true;
|
2016-03-12 23:22:28 +00:00
|
|
|
int start = 0;
|
2016-03-13 23:06:33 +00:00
|
|
|
int stop = xSA->_frames.size();
|
2016-03-12 23:22:28 +00:00
|
|
|
|
|
|
|
while( flags&SPRITE_ANIM_INFINITE ){
|
|
|
|
|
|
|
|
/* (1) Pour chaque sprite */
|
2016-03-14 12:14:35 +00:00
|
|
|
for( int i = start ; i != xSA->_frames.size() ; i+=(way?1:-1) ){
|
2016-03-12 23:22:28 +00:00
|
|
|
timer = SDL_GetTicks();
|
|
|
|
|
|
|
|
|
2016-03-15 21:56:39 +00:00
|
|
|
// On met a jour la frame (_src uniquement)
|
2016-03-14 18:28:18 +00:00
|
|
|
xSA->_src = xSA->_frames.at(i);
|
2016-03-12 23:22:28 +00:00
|
|
|
|
2016-03-13 13:53:39 +00:00
|
|
|
xSA->manager()->update();
|
2016-03-13 09:08:07 +00:00
|
|
|
|
2016-03-12 23:22:28 +00:00
|
|
|
|
|
|
|
if( SDL_GetTicks()-timer < t )
|
|
|
|
SDL_Delay( t - (SDL_GetTicks()-timer) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (2) Gestion des flags */
|
|
|
|
|
|
|
|
// SPRITE_ANIM_REVERSE
|
|
|
|
if( flags&SPRITE_ANIM_REVERSE ){
|
2016-03-14 12:14:35 +00:00
|
|
|
way = !way;
|
|
|
|
start = (way) ? 0 : xSA->_frames.size()-1;
|
|
|
|
stop = (way) ? xSA->_frames.size()-1 : 0;
|
2016-03-12 23:22:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* (n) On termine le thread */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [START] Ajoute l'animation au rendu
|
|
|
|
=========================================================*/
|
2016-03-14 15:10:21 +00:00
|
|
|
void xSpriteAnimation::start(int t, int flags){
|
2016-03-13 23:06:33 +00:00
|
|
|
_timeout = t;
|
|
|
|
_flags = flags;
|
|
|
|
|
2016-03-13 19:36:16 +00:00
|
|
|
/* (1) On lance l'animation */
|
2016-03-12 23:22:28 +00:00
|
|
|
_animation = new thread(xSpriteAnimationProcess, this, t, flags);
|
|
|
|
|
|
|
|
// On attends pas le thread
|
|
|
|
_animation->detach();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [STOP] Arrete l'animation
|
|
|
|
=========================================================*/
|
2016-03-13 19:36:16 +00:00
|
|
|
void xSpriteAnimation::stop(){
|
2016-03-13 09:08:07 +00:00
|
|
|
/* (1) On arrete l'animation */
|
2016-03-12 23:22:28 +00:00
|
|
|
delete _animation;
|
2016-03-14 18:28:18 +00:00
|
|
|
|
|
|
|
// On nettoie le pointeur
|
|
|
|
_animation = NULL;
|
2016-03-12 23:22:28 +00:00
|
|
|
}
|