SDL#2 Animation finie
This commit is contained in:
parent
7a43c382a5
commit
b07dcebf5b
|
@ -1,3 +1,9 @@
|
||||||
|
/* [DESTRUCTOR] Destruction de la surface
|
||||||
|
=========================================================*/
|
||||||
|
Sprite::~Sprite(){
|
||||||
|
SDL_FreeSurface( _surface );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/* [CONSTRUCTOR] Construction de la surface vide
|
/* [CONSTRUCTOR] Construction de la surface vide
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
|
@ -136,14 +142,23 @@ Sprite* SpriteGroup::get(int i){
|
||||||
|
|
||||||
/* [TRHEAD] Process de l'animation
|
/* [TRHEAD] Process de l'animation
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
void SpriteGroupAnimation(SDL_Window *win, SpriteGroup *sg, int t){
|
void SpriteGroupAnimation(SDL_Window *win, SpriteGroup *sg, int t, Sprite *clear, int flags){
|
||||||
int length = sg->_sprites.size();
|
int length = sg->_sprites.size();
|
||||||
int timer = 0;
|
int timer = 0;
|
||||||
|
int step = 1;
|
||||||
|
int start = 0;
|
||||||
|
int stop = length;
|
||||||
|
|
||||||
|
while( flags&SPRITE_ANIM_INFINITE ){
|
||||||
|
|
||||||
/* (1) Pour chaque sprite */
|
/* (1) Pour chaque sprite */
|
||||||
for( int i = 0 ; i < length ; i++ ){
|
for( int i = start ; i != stop ; i+=step ){
|
||||||
timer = SDL_GetTicks();
|
timer = SDL_GetTicks();
|
||||||
|
|
||||||
|
// On utilise le sprite de clear pour couvrir les calques inferieurs
|
||||||
|
if( clear != NULL )
|
||||||
|
clear->appendTo( SDL_GetWindowSurface(win) );
|
||||||
|
|
||||||
// On ajoute le sprite
|
// On ajoute le sprite
|
||||||
sg->_sprites[i]->appendTo( SDL_GetWindowSurface(win) );
|
sg->_sprites[i]->appendTo( SDL_GetWindowSurface(win) );
|
||||||
|
|
||||||
|
@ -152,6 +167,18 @@ void SpriteGroupAnimation(SDL_Window *win, SpriteGroup *sg, int t){
|
||||||
|
|
||||||
if( SDL_GetTicks()-timer < t )
|
if( SDL_GetTicks()-timer < t )
|
||||||
SDL_Delay( t - (SDL_GetTicks()-timer) );
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -164,8 +191,8 @@ void SpriteGroupAnimation(SDL_Window *win, SpriteGroup *sg, int t){
|
||||||
|
|
||||||
/* [ANIMATE] Modifie le Sprite dans l'ordre du SpriteGroup
|
/* [ANIMATE] Modifie le Sprite dans l'ordre du SpriteGroup
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
thread* SpriteGroup::animate(SDL_Window *win, int t){
|
thread* SpriteGroup::animate(SDL_Window *win, int t, Sprite *clear, int flags){
|
||||||
_animation = new thread(SpriteGroupAnimation, win, this, t);
|
_animation = new thread(SpriteGroupAnimation, win, this, t ,clear, flags);
|
||||||
|
|
||||||
return _animation;
|
return _animation;
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,9 @@
|
||||||
|
|
||||||
/* [LIBS] Externes
|
/* [LIBS] Externes
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
|
#define SPRITE_ANIM_ONCE 0x1
|
||||||
|
#define SPRITE_ANIM_INFINITE 0x10
|
||||||
|
#define SPRITE_ANIM_REVERSE 0x100
|
||||||
|
|
||||||
/* [NS] Namespaces
|
/* [NS] Namespaces
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
|
@ -24,10 +26,10 @@
|
||||||
Sprite(SDL_Rect r);
|
Sprite(SDL_Rect r);
|
||||||
Sprite(const int rgb[], SDL_Rect r);
|
Sprite(const int rgb[], SDL_Rect r);
|
||||||
Sprite(const char url[], SDL_Rect r, SDL_Rect clip);
|
Sprite(const char url[], SDL_Rect r, SDL_Rect clip);
|
||||||
|
~Sprite();
|
||||||
|
|
||||||
void setImage(const char url[], int ox=0, int oy=0);
|
void setImage(const char url[], int ox=0, int oy=0);
|
||||||
|
|
||||||
// ~Sprite();
|
|
||||||
void appendTo(SDL_Surface *dest);
|
void appendTo(SDL_Surface *dest);
|
||||||
|
|
||||||
// GETTERS
|
// GETTERS
|
||||||
|
@ -51,14 +53,14 @@
|
||||||
Sprite* get(int i);
|
Sprite* get(int i);
|
||||||
void appendTo(SDL_Surface *dest);
|
void appendTo(SDL_Surface *dest);
|
||||||
|
|
||||||
thread *animate(SDL_Window *win, int t);
|
thread *animate(SDL_Window *win, int t, Sprite *clear=NULL, int flags=0);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
vector<Sprite*> _sprites;
|
vector<Sprite*> _sprites;
|
||||||
thread *_animation;
|
thread *_animation;
|
||||||
|
|
||||||
|
|
||||||
friend void SpriteGroupAnimation(SDL_Window *win, SpriteGroup *sg, int t);
|
friend void SpriteGroupAnimation(SDL_Window *win, SpriteGroup *sg, int t, Sprite *clear=NULL, int flags=SPRITE_ANIM_ONCE );
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
120
SDL#2/main.cpp
120
SDL#2/main.cpp
|
@ -27,60 +27,102 @@ int main(int argc, char* argv[]) {
|
||||||
|
|
||||||
/* [4] On ajoute Une animation
|
/* [4] On ajoute Une animation
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
/* (1) On met un fond */
|
/* (1) On cree les fonds pour couvrir les frames en arriere plan */
|
||||||
|
int winSize[2];
|
||||||
|
SDL_GetWindowSize( mgr->window(), &winSize[0], &winSize[1]);
|
||||||
int white[] = {255, 255, 255};
|
int white[] = {255, 255, 255};
|
||||||
Sprite *coin_background = new Sprite(
|
Sprite *mb_background = new Sprite(
|
||||||
white,
|
white,
|
||||||
(SDL_Rect){0, 0, 180, 200}
|
(SDL_Rect){0, 0, 20, 20}
|
||||||
|
);
|
||||||
|
|
||||||
|
Sprite *gs_background = new Sprite(
|
||||||
|
white,
|
||||||
|
(SDL_Rect){100, 0, 20, 20}
|
||||||
|
);
|
||||||
|
|
||||||
|
Sprite *rs_background = new Sprite(
|
||||||
|
white,
|
||||||
|
(SDL_Rect){150, 0, 20, 20}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
/* (2) On enregistre les frames de l'animation */
|
/* (2) On enregistre les frames de l'animation du bloc mystere */
|
||||||
SpriteGroup animated_coin;
|
SpriteGroup animated_mystery_bloc;
|
||||||
animated_coin.add( new Sprite(
|
|
||||||
"src/coins.jpg",
|
for( int i = 0 ; i < 4 ; i++ ){
|
||||||
(SDL_Rect){0, 0, 180, 200},
|
|
||||||
(SDL_Rect){57, 50, 180, 200}
|
animated_mystery_bloc.add( new Sprite(
|
||||||
|
"src/mario_crop.png",
|
||||||
|
(SDL_Rect){0, 0, 20, 20},
|
||||||
|
(SDL_Rect){4*19+19*i, 2*20, 20, 20}
|
||||||
) );
|
) );
|
||||||
// On ajoute un fond entre chaque pour cacher si chgt taille
|
|
||||||
animated_coin.add( coin_background );
|
|
||||||
|
|
||||||
animated_coin.add( new Sprite(
|
}
|
||||||
"src/coins.jpg",
|
|
||||||
(SDL_Rect){0, 0, 180, 200},
|
/* (3) On enregistre les frames de l'animation d'une carapace verte */
|
||||||
(SDL_Rect){57+200, 50, 180, 200}
|
SpriteGroup animated_green_shell;
|
||||||
|
|
||||||
|
for( int i = 0 ; i < 4 ; i++ ){
|
||||||
|
|
||||||
|
animated_green_shell.add( new Sprite(
|
||||||
|
"src/mario_crop.png",
|
||||||
|
(SDL_Rect){100, 0, 20, 20},
|
||||||
|
(SDL_Rect){4*19+19*i, 11*19, 20, 20}
|
||||||
) );
|
) );
|
||||||
animated_coin.add( coin_background );
|
|
||||||
|
|
||||||
animated_coin.add( new Sprite(
|
}
|
||||||
"src/coins.jpg",
|
|
||||||
(SDL_Rect){30, 0, 150, 200},
|
/* (4) On enregistre les frames de l'animation d'une carapace rouge */
|
||||||
(SDL_Rect){57+200*2, 50, 180, 200}
|
SpriteGroup animated_red_shell;
|
||||||
|
|
||||||
|
for( int i = 0 ; i < 4 ; i++ ){
|
||||||
|
|
||||||
|
animated_red_shell.add( new Sprite(
|
||||||
|
"src/mario_crop.png",
|
||||||
|
(SDL_Rect){150, 0, 20, 20},
|
||||||
|
(SDL_Rect){2+19*i, 11*19, 20, 20}
|
||||||
) );
|
) );
|
||||||
animated_coin.add( coin_background );
|
|
||||||
|
|
||||||
animated_coin.add( new Sprite(
|
}
|
||||||
"src/coins.jpg",
|
|
||||||
(SDL_Rect){80, 0, 100, 200},
|
|
||||||
(SDL_Rect){57+200*3-30, 50, 180, 200}
|
|
||||||
) );
|
|
||||||
animated_coin.add( coin_background );
|
|
||||||
|
|
||||||
animated_coin.add( new Sprite(
|
|
||||||
"src/coins.jpg",
|
|
||||||
(SDL_Rect){30, 0, 150, 200},
|
|
||||||
(SDL_Rect){57+200*4+10, 50, 180, 200}
|
|
||||||
) );
|
|
||||||
animated_coin.add( coin_background );
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* [5] On lance l'animation en parallele
|
|
||||||
|
|
||||||
|
/* [5] On lance les animation en parallele
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
thread* anim = animated_coin.animate(mgr->window(), 500 );
|
vector<thread*> animations(0);
|
||||||
anim->detach(); // On laisse le thread tourner
|
|
||||||
delete anim; // On le supprime ensuite
|
animations.push_back( animated_mystery_bloc.animate(
|
||||||
|
mgr->window(), // fenetre a mettre a jour
|
||||||
|
200, // delai entre-frames
|
||||||
|
mb_background , // Sprite de reset
|
||||||
|
SPRITE_ANIM_INFINITE // FLAGS
|
||||||
|
) );
|
||||||
|
animations[0]->detach(); // On laisse le thread tourner
|
||||||
|
delete animations[0]; // On le supprime ensuite
|
||||||
|
|
||||||
|
|
||||||
|
animations.push_back( animated_green_shell.animate(
|
||||||
|
mgr->window(), // fenetre a mettre a jour
|
||||||
|
100, // delai entre-frames
|
||||||
|
gs_background, // Sprite de reset
|
||||||
|
SPRITE_ANIM_INFINITE // FLAGS
|
||||||
|
) );
|
||||||
|
animations[1]->detach(); // On laisse le thread tourner
|
||||||
|
delete animations[1]; // On le supprime ensuite
|
||||||
|
|
||||||
|
|
||||||
|
animations.push_back( animated_red_shell.animate(
|
||||||
|
mgr->window(), // fenetre a mettre a jour
|
||||||
|
100, // delai entre-frames
|
||||||
|
rs_background, // Sprite de reset
|
||||||
|
SPRITE_ANIM_INFINITE // FLAGS
|
||||||
|
) );
|
||||||
|
animations[2]->detach(); // On laisse le thread tourner
|
||||||
|
delete animations[2]; // On le supprime ensuite
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -92,7 +134,7 @@ int main(int argc, char* argv[]) {
|
||||||
while(running){
|
while(running){
|
||||||
mgr->waitEvent(SDL_QUIT, &quitEventHandler);
|
mgr->waitEvent(SDL_QUIT, &quitEventHandler);
|
||||||
|
|
||||||
cout << "Main loop" << endl;
|
// cout << "Main loop" << endl;
|
||||||
|
|
||||||
// Gestion des FPS (vitesse de la boucle)
|
// Gestion des FPS (vitesse de la boucle)
|
||||||
mgr->manageFps();
|
mgr->manageFps();
|
||||||
|
|
BIN
SDL#2/main.o
BIN
SDL#2/main.o
Binary file not shown.
Binary file not shown.
After Width: | Height: | Size: 13 KiB |
Binary file not shown.
After Width: | Height: | Size: 32 KiB |
Binary file not shown.
After Width: | Height: | Size: 33 KiB |
Binary file not shown.
After Width: | Height: | Size: 142 KiB |
Binary file not shown.
After Width: | Height: | Size: 53 KiB |
Loading…
Reference in New Issue