diff --git a/SDL#2/Sprite.cpp b/SDL#2/Sprite.cpp index 92e8559..0b751e4 100644 --- a/SDL#2/Sprite.cpp +++ b/SDL#2/Sprite.cpp @@ -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(); - - // On ajoute le sprite - sg->_sprites[i]->appendTo( SDL_GetWindowSurface(win) ); + while( flags&SPRITE_ANIM_INFINITE ){ - // On met a jour le rendu - SDL_UpdateWindowSurface( win ); + /* (1) Pour chaque sprite */ + for( int i = start ; i != stop ; i+=step ){ + 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 + 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; } diff --git a/SDL#2/Sprite.h b/SDL#2/Sprite.h index 2a05f08..00c7ece 100644 --- a/SDL#2/Sprite.h +++ b/SDL#2/Sprite.h @@ -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 _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 ); }; diff --git a/SDL#2/exe b/SDL#2/exe index fc2517b..bcf5e96 100755 Binary files a/SDL#2/exe and b/SDL#2/exe differ diff --git a/SDL#2/main.cpp b/SDL#2/main.cpp index 70d36d5..96357a4 100644 --- a/SDL#2/main.cpp +++ b/SDL#2/main.cpp @@ -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 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(); diff --git a/SDL#2/main.o b/SDL#2/main.o index f2e048e..841a52b 100644 Binary files a/SDL#2/main.o and b/SDL#2/main.o differ diff --git a/SDL#2/src/bloc.png b/SDL#2/src/bloc.png new file mode 100644 index 0000000..6541d57 Binary files /dev/null and b/SDL#2/src/bloc.png differ diff --git a/SDL#2/src/bloc_crop.png b/SDL#2/src/bloc_crop.png new file mode 100644 index 0000000..ed44639 Binary files /dev/null and b/SDL#2/src/bloc_crop.png differ diff --git a/SDL#2/src/mario.jpg b/SDL#2/src/mario.jpg new file mode 100644 index 0000000..d6e7c23 Binary files /dev/null and b/SDL#2/src/mario.jpg differ diff --git a/SDL#2/src/mario_crop.png b/SDL#2/src/mario_crop.png new file mode 100644 index 0000000..da7b81f Binary files /dev/null and b/SDL#2/src/mario_crop.png differ diff --git a/SDL#2/src/walking.png b/SDL#2/src/walking.png new file mode 100644 index 0000000..1938f51 Binary files /dev/null and b/SDL#2/src/walking.png differ