lab.cpp/SDL#4/xSDL/xSpriteAnimation.cpp

149 lines
3.0 KiB
C++
Raw Permalink Normal View History

2016-03-12 23:22:28 +00:00
/* [DESTRUCTUR] Destruction de l'animation
=========================================================*/
xSpriteAnimation::~xSpriteAnimation(){
2016-03-12 23:22:28 +00:00
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);
2016-03-12 23:22:28 +00:00
}
2016-03-12 23:22:28 +00:00
/* [ADDFRAME] Ajout d'une frame d'animation
=========================================================*/
void xSpriteAnimation::addFrame(SDL_Rect clip){
2016-03-12 23:22:28 +00:00
// On ajoute une frame
_frames.push_back( (SDL_Rect){
clip.x,
clip.y,
clip.w,
clip.h
} );
2016-03-12 23:22:28 +00:00
}
/* [CLEAR] Supprime toutes les frames
=========================================================*/
void xSpriteAnimation::clear(){
_frames.erase(_frames.begin(), _frames.end());
}
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;
// Gestion erreur si aucune frame
if( _frames.size() == 0 )
return;
_src = _frames[0];
2016-03-13 19:36:16 +00:00
/* (1) On ajoute le sprite au rendu */
_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();
// On met a jour la frame (_src uniquement)
xSA->_src = xSA->_frames.at(i);
2016-03-12 23:22:28 +00:00
xSA->manager()->update();
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
=========================================================*/
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(){
/* (1) On arrete l'animation */
2016-03-12 23:22:28 +00:00
delete _animation;
// On nettoie le pointeur
_animation = NULL;
2016-03-12 23:22:28 +00:00
}