2016-03-12 23:22:28 +00:00
|
|
|
/* [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;
|
|
|
|
|
2016-03-13 09:08:07 +00:00
|
|
|
_viewport = viewport;
|
2016-03-12 23:22:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* (2) On charge le spritesheet */
|
|
|
|
_texture = IMG_LoadTexture( _manager->renderer(), url );
|
|
|
|
|
|
|
|
// Gestion erreur
|
|
|
|
if( _texture == NULL )
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* [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-13 09:08:07 +00:00
|
|
|
/* [MANAGER] Retourne le manager
|
|
|
|
=========================================================*/
|
|
|
|
xManager *xSpriteAnimation::manager(){ return _manager; }
|
2016-03-12 23:22:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [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];
|
|
|
|
|
2016-03-13 09:08:07 +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 ){
|
|
|
|
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 */
|
2016-03-13 09:08:07 +00:00
|
|
|
_manager->push(_texture, &_frame, &_viewport);
|
2016-03-12 23:22:28 +00:00
|
|
|
|
|
|
|
/* (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(){
|
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-13 09:08:07 +00:00
|
|
|
|
|
|
|
/* (2) On retire le sprite du rendu */
|
|
|
|
_manager->pull(_texture);
|
2016-03-12 23:22:28 +00:00
|
|
|
}
|