SDL#2 Animation finie

This commit is contained in:
xdrm-brackets 2016-03-11 13:42:48 +01:00
parent 7a43c382a5
commit b07dcebf5b
10 changed files with 129 additions and 58 deletions

View File

@ -1,3 +1,9 @@
/* [DESTRUCTOR] Destruction de la surface
=========================================================*/
Sprite::~Sprite(){
SDL_FreeSurface( _surface );
}
/* [CONSTRUCTOR] Construction de la surface vide
=========================================================*/
@ -136,22 +142,43 @@ Sprite* SpriteGroup::get(int i){
/* [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 timer = 0;
int step = 1;
int start = 0;
int stop = length;
/* (1) Pour chaque sprite */
for( int i = 0 ; i < length ; i++ ){
timer = SDL_GetTicks();
while( flags&SPRITE_ANIM_INFINITE ){
// On ajoute le sprite
sg->_sprites[i]->appendTo( SDL_GetWindowSurface(win) );
/* (1) Pour chaque sprite */
for( int i = start ; i != stop ; i+=step ){
timer = SDL_GetTicks();
// On met a jour le rendu
SDL_UpdateWindowSurface( win );
// On utilise le sprite de clear pour couvrir les calques inferieurs
if( clear != NULL )
clear->appendTo( SDL_GetWindowSurface(win) );
// On ajoute le sprite
sg->_sprites[i]->appendTo( SDL_GetWindowSurface(win) );
// On met a jour le rendu
SDL_UpdateWindowSurface( win );
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;
}
if( SDL_GetTicks()-timer < t )
SDL_Delay( t - (SDL_GetTicks()-timer) );
}
@ -164,8 +191,8 @@ void SpriteGroupAnimation(SDL_Window *win, SpriteGroup *sg, int t){
/* [ANIMATE] Modifie le Sprite dans l'ordre du SpriteGroup
=========================================================*/
thread* SpriteGroup::animate(SDL_Window *win, int t){
_animation = new thread(SpriteGroupAnimation, win, this, t);
thread* SpriteGroup::animate(SDL_Window *win, int t, Sprite *clear, int flags){
_animation = new thread(SpriteGroupAnimation, win, this, t ,clear, flags);
return _animation;
}

View File

@ -11,7 +11,9 @@
/* [LIBS] Externes
=========================================================*/
#define SPRITE_ANIM_ONCE 0x1
#define SPRITE_ANIM_INFINITE 0x10
#define SPRITE_ANIM_REVERSE 0x100
/* [NS] Namespaces
=========================================================*/
@ -24,10 +26,10 @@
Sprite(SDL_Rect r);
Sprite(const int rgb[], SDL_Rect r);
Sprite(const char url[], SDL_Rect r, SDL_Rect clip);
~Sprite();
void setImage(const char url[], int ox=0, int oy=0);
// ~Sprite();
void appendTo(SDL_Surface *dest);
// GETTERS
@ -51,14 +53,14 @@
Sprite* get(int i);
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:
vector<Sprite*> _sprites;
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 );
};

BIN
SDL#2/exe

Binary file not shown.

View File

@ -27,60 +27,102 @@ int main(int argc, char* argv[]) {
/* [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};
Sprite *coin_background = new Sprite(
Sprite *mb_background = new Sprite(
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 */
SpriteGroup animated_coin;
animated_coin.add( new Sprite(
"src/coins.jpg",
(SDL_Rect){0, 0, 180, 200},
(SDL_Rect){57, 50, 180, 200}
) );
// On ajoute un fond entre chaque pour cacher si chgt taille
animated_coin.add( coin_background );
/* (2) On enregistre les frames de l'animation du bloc mystere */
SpriteGroup animated_mystery_bloc;
animated_coin.add( new Sprite(
"src/coins.jpg",
(SDL_Rect){0, 0, 180, 200},
(SDL_Rect){57+200, 50, 180, 200}
) );
animated_coin.add( coin_background );
for( int i = 0 ; i < 4 ; i++ ){
animated_coin.add( new Sprite(
"src/coins.jpg",
(SDL_Rect){30, 0, 150, 200},
(SDL_Rect){57+200*2, 50, 180, 200}
) );
animated_coin.add( coin_background );
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}
) );
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 );
/* (3) On enregistre les frames de l'animation d'une carapace verte */
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}
) );
}
/* (4) On enregistre les frames de l'animation d'une carapace rouge */
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}
) );
}
/* [5] On lance l'animation en parallele
/* [5] On lance les animation en parallele
=========================================================*/
thread* anim = animated_coin.animate(mgr->window(), 500 );
anim->detach(); // On laisse le thread tourner
delete anim; // On le supprime ensuite
vector<thread*> animations(0);
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){
mgr->waitEvent(SDL_QUIT, &quitEventHandler);
cout << "Main loop" << endl;
// cout << "Main loop" << endl;
// Gestion des FPS (vitesse de la boucle)
mgr->manageFps();

Binary file not shown.

BIN
SDL#2/src/bloc.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

BIN
SDL#2/src/bloc_crop.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

BIN
SDL#2/src/mario.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

BIN
SDL#2/src/mario_crop.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 142 KiB

BIN
SDL#2/src/walking.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 53 KiB