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;
|
2016-03-13 13:53:39 +00:00
|
|
|
}
|
|
|
|
|
2016-03-12 23:22:28 +00:00
|
|
|
|
2016-03-13 13:53:39 +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){
|
2016-03-13 17:33:47 +00:00
|
|
|
|
2016-03-13 19:36:16 +00:00
|
|
|
if( !_manager->hit(_texture, newpos.x, newpos.y) ){
|
2016-03-13 17:33:47 +00:00
|
|
|
|
2016-03-13 19:36:16 +00:00
|
|
|
if( newpos.x != 0 )
|
|
|
|
_viewport.x = newpos.x;
|
2016-03-13 17:33:47 +00:00
|
|
|
|
2016-03-13 19:36:16 +00:00
|
|
|
if( newpos.y != 0 )
|
|
|
|
_viewport.y = newpos.y;
|
|
|
|
|
|
|
|
if( newpos.w != 0 )
|
|
|
|
_viewport.w = newpos.w;
|
|
|
|
|
|
|
|
if( newpos.h != 0)
|
|
|
|
_viewport.h = newpos.h;
|
|
|
|
|
|
|
|
}
|
2016-03-13 17:33:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* [MOVE] Deplacement de la position/taille du sprite
|
|
|
|
=========================================================*/
|
|
|
|
void xSpriteAnimation::move(int x, int y){
|
2016-03-13 19:36:16 +00:00
|
|
|
|
|
|
|
if( !_manager->hit(_texture, x, y) ){
|
|
|
|
_viewport.x += x;
|
|
|
|
_viewport.y += y;
|
|
|
|
}
|
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-13 09:08:07 +00:00
|
|
|
/* [MANAGER] Retourne le manager
|
|
|
|
=========================================================*/
|
|
|
|
xManager *xSpriteAnimation::manager(){ return _manager; }
|
2016-03-12 23:22:28 +00:00
|
|
|
|
|
|
|
|
2016-03-13 17:33:47 +00:00
|
|
|
/* [VIEWPORT] Retourne le viewport
|
|
|
|
=========================================================*/
|
|
|
|
SDL_Rect *xSpriteAnimation::viewport(){ return &_viewport; }
|
|
|
|
|
|
|
|
|
2016-03-12 23:22:28 +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-13 19:36:16 +00:00
|
|
|
/* (1) On ajoute le sprite au rendu */
|
|
|
|
_manager->push(index, _texture, &_frame, &_viewport);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* [PULL] Retire du rendu
|
|
|
|
=========================================================*/
|
|
|
|
void xSpriteAnimation::pull(string index){
|
2016-03-13 23:06:33 +00:00
|
|
|
_index = index;
|
|
|
|
|
2016-03-13 19:36:16 +00:00
|
|
|
/* (2) On retire le sprite du rendu */
|
|
|
|
_manager->pull(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* [PULL] Retire du rendu
|
|
|
|
=========================================================*/
|
|
|
|
void xSpriteAnimation::pull(){
|
|
|
|
/* (2) On retire le sprite du rendu */
|
|
|
|
_manager->pull(_texture);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
xSA->_frame = xSA->_frames[i];
|
|
|
|
|
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-13 17:33:47 +00:00
|
|
|
void xSpriteAnimation::start(string index, int t, int flags){
|
2016-03-13 23:06:33 +00:00
|
|
|
_index = index;
|
|
|
|
_timeout = t;
|
|
|
|
_flags = flags;
|
|
|
|
|
2016-03-13 19:36:16 +00:00
|
|
|
this->push(index);
|
2016-03-12 23:22:28 +00:00
|
|
|
|
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(){
|
|
|
|
this->pull();
|
|
|
|
|
2016-03-13 09:08:07 +00:00
|
|
|
/* (1) On arrete l'animation */
|
2016-03-12 23:22:28 +00:00
|
|
|
delete _animation;
|
|
|
|
}
|