2016-03-11 15:17:57 +00:00
|
|
|
/* [DESTRUCTOR] Destruction de la surface
|
|
|
|
=========================================================*/
|
|
|
|
Sprite::~Sprite(){
|
2016-03-11 23:10:38 +00:00
|
|
|
SDL_DestroyTexture( _texture );
|
|
|
|
_manager = NULL;
|
2016-03-11 15:17:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* [CONSTRUCTOR] Construction de la surface vide
|
|
|
|
=========================================================*/
|
2016-03-11 23:10:38 +00:00
|
|
|
Sprite::Sprite(sdltl *m){
|
|
|
|
_manager = m;
|
|
|
|
_texture = NULL;
|
2016-03-11 15:17:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [CONSTRUCTOR] Construction de la surface avec couleur
|
|
|
|
=========================================================*/
|
2016-03-11 23:10:38 +00:00
|
|
|
Sprite::Sprite(sdltl *m, const int rgb[]){
|
|
|
|
_manager = m;
|
|
|
|
_texture = NULL;
|
|
|
|
|
|
|
|
|
2016-03-11 15:17:57 +00:00
|
|
|
cout << "a: [" << (sizeof(rgb)/sizeof(*rgb)) << "]" << endl;
|
|
|
|
|
2016-03-11 23:10:38 +00:00
|
|
|
SDL_Surface *surf = SDL_CreateRGBSurface(0, 0, 0, 32, 0, 0, 0, rgb[3]);
|
2016-03-11 15:17:57 +00:00
|
|
|
|
|
|
|
// On recupere la couleur
|
2016-03-11 23:10:38 +00:00
|
|
|
Uint32 color = SDL_MapRGBA( surf->format, rgb[0], rgb[1], rgb[2], rgb[3]);
|
|
|
|
|
2016-03-11 15:17:57 +00:00
|
|
|
|
2016-03-11 23:10:38 +00:00
|
|
|
// On cree la texture a partir de la surface
|
|
|
|
_texture = SDL_CreateTextureFromSurface(_manager->renderer(), surf);
|
2016-03-11 15:17:57 +00:00
|
|
|
|
|
|
|
|
2016-03-11 23:10:38 +00:00
|
|
|
// On libere la surface inutile maintenant
|
|
|
|
SDL_FreeSurface( surf );
|
|
|
|
surf = NULL;
|
|
|
|
|
2016-03-11 15:17:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [CONSTRUCTOR] Construction de la surface avec image
|
|
|
|
=========================================================*/
|
2016-03-11 23:10:38 +00:00
|
|
|
Sprite::Sprite(sdltl *m, const char *url){
|
|
|
|
|
|
|
|
_manager = m;
|
|
|
|
_texture = NULL;
|
2016-03-11 15:17:57 +00:00
|
|
|
|
|
|
|
/* (1) On cree la surface (avec image) */
|
2016-03-11 23:10:38 +00:00
|
|
|
_texture = IMG_LoadTexture(_manager->renderer(), url);
|
2016-03-11 15:17:57 +00:00
|
|
|
|
|
|
|
// Gestion erreur
|
2016-03-11 23:10:38 +00:00
|
|
|
if( _texture == NULL )
|
2016-03-11 15:17:57 +00:00
|
|
|
return;
|
2016-03-11 23:10:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* [DIMENSIONS] Definition des dimensions par defaut
|
|
|
|
=========================================================*/
|
|
|
|
void Sprite::dimensions(){
|
|
|
|
cout <<"A"<<endl;
|
2016-03-11 15:17:57 +00:00
|
|
|
|
2016-03-11 23:10:38 +00:00
|
|
|
/* (1) On recupere les informations de la texture */
|
|
|
|
int w, h;
|
|
|
|
SDL_QueryTexture(_texture, NULL, NULL, &w, &h);
|
2016-03-11 15:17:57 +00:00
|
|
|
|
|
|
|
|
2016-03-11 23:10:38 +00:00
|
|
|
/* (2) On definit les dimensions par defaut */
|
|
|
|
_dst = (SDL_Rect){0, 0, w, h};
|
|
|
|
_src = (SDL_Rect){0, 0, w, h};
|
2016-03-11 15:17:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-11 23:10:38 +00:00
|
|
|
/* [DIMENSIONS] Definition des dimensions de la texture
|
|
|
|
=========================================================*/
|
|
|
|
void Sprite::dimensions(SDL_Rect r){
|
|
|
|
cout <<"B"<<endl;
|
|
|
|
|
|
|
|
/* (1) On recupere les informations de la texture */
|
|
|
|
int w, h;
|
|
|
|
SDL_QueryTexture(_texture, NULL, NULL, &w, &h);
|
2016-03-11 15:17:57 +00:00
|
|
|
|
2016-03-11 23:10:38 +00:00
|
|
|
|
|
|
|
/* (2) On definit les dimensions */
|
|
|
|
_dst = (SDL_Rect){r.x, r.y, r.w, r.h};
|
|
|
|
_src = (SDL_Rect){0, 0, w, h};
|
|
|
|
}
|
|
|
|
|
|
|
|
/* [DIMENSIONS] Definition des dimensions de la texture+clip
|
2016-03-11 15:17:57 +00:00
|
|
|
=========================================================*/
|
2016-03-11 23:10:38 +00:00
|
|
|
void Sprite::dimensions(SDL_Rect r, SDL_Rect clip){
|
|
|
|
cout <<"C"<<endl;
|
|
|
|
|
|
|
|
/* (1) On definit les dimensions */
|
|
|
|
_dst = (SDL_Rect){r.x, r.y, r.w, r.h};
|
|
|
|
_src = (SDL_Rect){clip.x, clip.y, clip.w, clip.h};
|
|
|
|
}
|
2016-03-11 15:17:57 +00:00
|
|
|
|
2016-03-11 23:10:38 +00:00
|
|
|
/* [PUSH] Ajoute le Sprite au rendu
|
|
|
|
=========================================================*/
|
|
|
|
void Sprite::push(){
|
|
|
|
_manager->push(_texture, &_src, &_dst);
|
2016-03-11 15:17:57 +00:00
|
|
|
}
|
|
|
|
|
2016-03-11 23:10:38 +00:00
|
|
|
/* [PULL] Retire une sprite du rendu
|
2016-03-11 15:17:57 +00:00
|
|
|
=========================================================*/
|
2016-03-11 23:10:38 +00:00
|
|
|
void Sprite::pull(){
|
|
|
|
_manager->pull( _texture );
|
2016-03-11 15:17:57 +00:00
|
|
|
}
|
|
|
|
|
2016-03-11 23:10:38 +00:00
|
|
|
/* [UPDATE] Mise a jour du rendu
|
2016-03-11 15:17:57 +00:00
|
|
|
=========================================================*/
|
2016-03-11 23:10:38 +00:00
|
|
|
void Sprite::update(){
|
|
|
|
_manager->update();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* [TEXTURE] Retourne la texture
|
|
|
|
=========================================================*/
|
|
|
|
SDL_Texture *Sprite::texture(){ return _texture; }
|
|
|
|
|
|
|
|
/* [MANAGER] Retourne le manager
|
|
|
|
=========================================================*/
|
|
|
|
sdltl *Sprite::manager(){ return _manager; }
|
2016-03-11 15:17:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************/
|
|
|
|
/*********** SPRITEGROUP **********/
|
|
|
|
/**********************************/
|
|
|
|
/* [CONSTRUCTOR] Initialisation de la liste de Sprite
|
|
|
|
=========================================================*/
|
|
|
|
SpriteGroup::SpriteGroup(){
|
|
|
|
// _sprites = new vector<Sprite*>(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* [ADD] Ajoute un Sprite au groupe
|
|
|
|
=========================================================*/
|
|
|
|
void SpriteGroup::add(Sprite *s){
|
|
|
|
_sprites.push_back( s );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* [REMOVE] Suppression d'un Sprite du groupe
|
|
|
|
=========================================================*/
|
|
|
|
void SpriteGroup::remove(Sprite *s){
|
|
|
|
int index = -1; // on cherche l'indice du sprite
|
|
|
|
|
|
|
|
// On parcours la liste pour trouver l'indice
|
|
|
|
for( int i = 0 ; i < _sprites.size() ; i++ )
|
|
|
|
if( _sprites[i] == s ) index = i;
|
|
|
|
|
|
|
|
// Si on a pas trouve l'indice
|
|
|
|
if( index == -1 ) return;
|
|
|
|
|
|
|
|
// On supprime le sprite de la liste
|
|
|
|
_sprites.erase(_sprites.begin() + index );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-11 23:10:38 +00:00
|
|
|
/* [PUSH] Ajoute tous les Sprite du groupe a une surface parente
|
2016-03-11 15:17:57 +00:00
|
|
|
=========================================================*/
|
2016-03-11 23:10:38 +00:00
|
|
|
void SpriteGroup::push(){
|
2016-03-11 15:17:57 +00:00
|
|
|
for( int i = 0 ; i < _sprites.size() ; i++ )
|
2016-03-11 23:10:38 +00:00
|
|
|
_sprites[i]->push();
|
2016-03-11 15:17:57 +00:00
|
|
|
}
|
|
|
|
|
2016-03-11 23:10:38 +00:00
|
|
|
/* [PULL] Retire une sprite de la surface parents
|
2016-03-11 15:17:57 +00:00
|
|
|
=========================================================*/
|
2016-03-11 23:10:38 +00:00
|
|
|
void SpriteGroup::pull(){
|
2016-03-11 15:17:57 +00:00
|
|
|
for( int i = 0 ; i < _sprites.size() ; i++ )
|
2016-03-11 23:10:38 +00:00
|
|
|
_sprites[i]->pull();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [UPDATE] Mise a jour du rendu
|
|
|
|
=========================================================*/
|
|
|
|
void SpriteGroup::update(){
|
|
|
|
if( _sprites.size() > 0 )
|
|
|
|
_sprites[0]->update();
|
2016-03-11 15:17:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* [GET] Retourne le Sprite d'index donne
|
|
|
|
=========================================================*/
|
|
|
|
Sprite* SpriteGroup::get(int i){
|
|
|
|
return _sprites[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [TRHEAD] Process de l'animation
|
|
|
|
=========================================================*/
|
2016-03-11 23:10:38 +00:00
|
|
|
void SpriteGroupAnimation(SpriteGroup *sg, int t, int flags){
|
2016-03-11 15:17:57 +00:00
|
|
|
int length = sg->_sprites.size();
|
|
|
|
int timer = 0;
|
|
|
|
int step = 1;
|
|
|
|
int start = 0;
|
|
|
|
int stop = length;
|
|
|
|
int lastindex = -1;
|
|
|
|
|
|
|
|
while( flags&SPRITE_ANIM_INFINITE ){
|
|
|
|
|
|
|
|
/* (1) Pour chaque sprite */
|
|
|
|
for( int i = start ; i != stop ; i+=step ){
|
|
|
|
timer = SDL_GetTicks();
|
|
|
|
|
|
|
|
|
|
|
|
// On retire le sprite precedent
|
|
|
|
if( lastindex > -1 )
|
2016-03-11 23:10:38 +00:00
|
|
|
sg->get(lastindex)->pull();
|
2016-03-11 15:17:57 +00:00
|
|
|
|
|
|
|
// On ajoute le sprite
|
2016-03-11 23:10:38 +00:00
|
|
|
sg->get(i)->push();
|
2016-03-11 15:17:57 +00:00
|
|
|
|
|
|
|
// On met a jour le rendu
|
2016-03-11 23:10:38 +00:00
|
|
|
// sg->get(i)->update();
|
2016-03-11 15:17:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
if( SDL_GetTicks()-timer < t )
|
|
|
|
SDL_Delay( t - (SDL_GetTicks()-timer) );
|
|
|
|
|
|
|
|
lastindex = i;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [ANIMATE] Modifie le Sprite dans l'ordre du SpriteGroup
|
|
|
|
=========================================================*/
|
2016-03-11 23:10:38 +00:00
|
|
|
thread* SpriteGroup::animate(int t, int flags){
|
|
|
|
_animation = new thread(SpriteGroupAnimation, this, t, flags);
|
2016-03-11 15:17:57 +00:00
|
|
|
|
|
|
|
// On attends pas le thread
|
|
|
|
_animation->detach();
|
|
|
|
|
|
|
|
return _animation;
|
|
|
|
}
|
|
|
|
|
|
|
|
|