diff --git a/SDL#4/exe b/SDL#4/exe index 434e4a3..a29cf50 100755 Binary files a/SDL#4/exe and b/SDL#4/exe differ diff --git a/SDL#4/main.cpp b/SDL#4/main.cpp index d2dfdb8..6340571 100644 --- a/SDL#4/main.cpp +++ b/SDL#4/main.cpp @@ -31,12 +31,12 @@ int main(int argc, char* argv[]) { xSpriteAnimation mario_mystery_block( mgr, "src/mario_crop.png", - (SDL_Rect){0, 0, 20, 20} + (SDL_Rect){0, 0, 50, 50} ); - mario_mystery_block.addFrame(4, 2); - mario_mystery_block.addFrame(5, 2); - mario_mystery_block.addFrame(6, 2); - mario_mystery_block.addFrame(7, 2); + mario_mystery_block.addFrame( (SDL_Rect){4*19, 2*20, 20, 20} ); + mario_mystery_block.addFrame( (SDL_Rect){5*19, 2*20, 20, 20} ); + mario_mystery_block.addFrame( (SDL_Rect){6*19, 2*20, 20, 20} ); + mario_mystery_block.addFrame( (SDL_Rect){7*19, 2*20, 20, 20} ); mario_mystery_block.start(200, SPRITE_ANIM_INFINITE); @@ -44,12 +44,12 @@ int main(int argc, char* argv[]) { xSpriteAnimation mario_green_shell( mgr, "src/mario_crop.png", - (SDL_Rect){50, 50, 20, 20} + (SDL_Rect){50, 0, 50, 50} ); - mario_green_shell.addFrame(4, 11); - mario_green_shell.addFrame(5, 11); - mario_green_shell.addFrame(6, 11); - mario_green_shell.addFrame(7, 11); + mario_green_shell.addFrame( (SDL_Rect){4*19, 210, 20, 20} ); + mario_green_shell.addFrame( (SDL_Rect){5*19, 210, 20, 20} ); + mario_green_shell.addFrame( (SDL_Rect){6*19, 210, 20, 20} ); + mario_green_shell.addFrame( (SDL_Rect){7*19, 210, 20, 20} ); mario_green_shell.start(200, SPRITE_ANIM_INFINITE); @@ -64,13 +64,14 @@ int main(int argc, char* argv[]) { running = true; while(running){ + // Gestion d'un evenement mgr->waitEvent(SDL_QUIT, &quitEventHandler); // cout << "Main loop" << endl; - mgr->update(); // Gestion des FPS (vitesse de la boucle) - mgr->manageFps(); + mgr->manageFps(); + mgr->update(); // Mise a jour du rendu } @@ -87,6 +88,6 @@ int main(int argc, char* argv[]) { void quitEventHandler(SDL_Event *e){ - cout << "Ferme" << endl; + cout << "Exiting program" << endl; running = false; } \ No newline at end of file diff --git a/SDL#4/main.o b/SDL#4/main.o index fcfa594..1f71867 100644 Binary files a/SDL#4/main.o and b/SDL#4/main.o differ diff --git a/SDL#4/mario_ground.jpg b/SDL#4/mario_ground.jpg new file mode 100644 index 0000000..89c691a Binary files /dev/null and b/SDL#4/mario_ground.jpg differ diff --git a/SDL#4/xSDL/xSpriteAnimation.cpp b/SDL#4/xSDL/xSpriteAnimation.cpp index 4afa64c..3845500 100644 --- a/SDL#4/xSDL/xSpriteAnimation.cpp +++ b/SDL#4/xSDL/xSpriteAnimation.cpp @@ -13,7 +13,7 @@ xSpriteAnimation::xSpriteAnimation(xManager *manager, const char *url, SDL_Rect _manager = manager; _texture = NULL; - _clip = viewport; + _viewport = viewport; /* (2) On charge le spritesheet */ @@ -27,19 +27,22 @@ xSpriteAnimation::xSpriteAnimation(xManager *manager, const char *url, SDL_Rect /* [ADDFRAME] Ajout d'une frame d'animation =========================================================*/ -void xSpriteAnimation::addFrame(float x, float y){ +void xSpriteAnimation::addFrame(SDL_Rect clip){ // On ajoute une frame _frames.push_back( (SDL_Rect){ - (int) x*_clip.w, - (int) y*_clip.h, - (int) _clip.w, - (int) _clip.h - }); + clip.x, + clip.y, + clip.w, + clip.h + } ); } +/* [MANAGER] Retourne le manager +=========================================================*/ +xManager *xSpriteAnimation::manager(){ return _manager; } @@ -62,6 +65,8 @@ void xSpriteAnimationProcess(xSpriteAnimation *xSA, int t, int flags){ // On met a jour la frame xSA->_frame = xSA->_frames[i]; + // xSA->manager()->update(); + if( SDL_GetTicks()-timer < t ) SDL_Delay( t - (SDL_GetTicks()-timer) ); @@ -93,7 +98,7 @@ void xSpriteAnimationProcess(xSpriteAnimation *xSA, int t, int flags){ =========================================================*/ void xSpriteAnimation::start(int t, int flags){ /* (1) On ajoute le sprite au rendu */ - _manager->push(_texture, &_frame, &_clip); + _manager->push(_texture, &_frame, &_viewport); /* (2) On lance l'animation */ _animation = new thread(xSpriteAnimationProcess, this, t, flags); @@ -107,5 +112,9 @@ void xSpriteAnimation::start(int t, int flags){ /* [STOP] Arrete l'animation =========================================================*/ void xSpriteAnimation::stop(){ + /* (1) On arrete l'animation */ delete _animation; + + /* (2) On retire le sprite du rendu */ + _manager->pull(_texture); } \ No newline at end of file diff --git a/SDL#4/xSDL/xSpriteAnimation.h b/SDL#4/xSDL/xSpriteAnimation.h index 0a362bb..958d6ac 100644 --- a/SDL#4/xSDL/xSpriteAnimation.h +++ b/SDL#4/xSDL/xSpriteAnimation.h @@ -8,18 +8,23 @@ xSpriteAnimation(xManager *manager, const char *url, SDL_Rect viewport); // Spritesheet avec taille de chaque sprite ~xSpriteAnimation(); - void addFrame(float x=0.0, float y=0.0); // Ajoute une frame en fonction des coordonnees + void addFrame(SDL_Rect clip); // Ajoute une frame en fonction des coordonnees + // GETTER + xManager *manager(); + + // Gestion de l'animation void start(int t, int flags=SPRITE_ANIM_ONCE); void stop(); + private: xManager *_manager; SDL_Texture *_texture; - // Taille unitaire d'une sprite - SDL_Rect _clip; + // Position de l'animation + SDL_Rect _viewport; // Contiendra les frames vector _frames;