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

170 lines
3.7 KiB
C++
Raw Normal View History

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;
_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;
}
2016-03-12 23:22:28 +00:00
/* [CONSTRUCTOR] Construction de l'animation a partir d'une texture existante
=========================================================*/
xSpriteAnimation::xSpriteAnimation(xManager *manager, SDL_Texture *t, SDL_Rect viewport){
/* (1) Definition des attributs */
_manager = manager;
_texture = NULL;
_viewport = viewport;
/* (2) On charge le spritesheet */
_texture = t;
// Gestion erreur
if( _texture == NULL )
return;
}
/* [MOVE] Modification de la position/taille du sprite
=========================================================*/
void xSpriteAnimation::move(SDL_Rect newpos){
if( newpos.x != 0 )
_viewport.x = newpos.x;
if( newpos.y != 0 )
_viewport.y = newpos.y;
if( newpos.w != 0 )
_viewport.w = newpos.w;
if( newpos.h != 0)
_viewport.h = newpos.h;
}
/* [MOVE] Deplacement de la position/taille du sprite
=========================================================*/
void xSpriteAnimation::move(int x, int y){
_viewport.x += x;
_viewport.y += y;
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
}
/* [MANAGER] Retourne le manager
=========================================================*/
xManager *xSpriteAnimation::manager(){ return _manager; }
2016-03-12 23:22:28 +00:00
/* [VIEWPORT] Retourne le viewport
=========================================================*/
SDL_Rect *xSpriteAnimation::viewport(){ return &_viewport; }
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];
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(string index, int t, int flags){
2016-03-12 23:22:28 +00:00
/* (1) On ajoute le sprite au rendu */
_manager->push(index, _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(string index){
/* (1) On arrete l'animation */
2016-03-12 23:22:28 +00:00
delete _animation;
/* (2) On retire le sprite du rendu */
_manager->pull(index, _texture);
2016-03-12 23:22:28 +00:00
}