diff --git a/SDL#4/exe b/SDL#4/exe index a29cf50..19ee9fb 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 6340571..dc8615f 100644 --- a/SDL#4/main.cpp +++ b/SDL#4/main.cpp @@ -11,7 +11,7 @@ int main(int argc, char* argv[]) { /* [0] Initialisation du manager + Creation de la fenetre =========================================================*/ - mgr = new xManager("Ma fenetre SDL", WIN_WIDTH, WIN_HEIGHT); + mgr = new xManager("Ma fenetre SDL", BLOC_WIDTH*BLOC_SIZE, BLOC_HEIGHT*BLOC_SIZE); // Gestion erreur if( !mgr->status() ){ @@ -19,58 +19,54 @@ int main(int argc, char* argv[]) { return -1; } + // Couleur de fond + mgr->setBackground(0, 96, 183); + mgr->setImage("src/bg1.png"); - /* [2] On definit le background color + /* [2] On definit le terrain =========================================================*/ - mgr->setBackground(255, 0, 255, 150); - // mgr->setImage( "src/1.bmp" ); + // On cree un bout du terrain + xMarioGrass btmleft(mgr, (SDL_Rect){-1, 20-2, 10, 3} ); + btmleft.push(); - /* [3] Gestion des animations + + /* [3] Gestion des animations (blocs animes) =========================================================*/ - xSpriteAnimation mario_mystery_block( - mgr, - "src/mario_crop.png", - (SDL_Rect){0, 0, 50, 50} - ); - 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} ); + // On cree une coquille verte + // xMarioGreenShell gs(mgr, 5, 20-3); + // gs.start(100, SPRITE_ANIM_INFINITE); - mario_mystery_block.start(200, SPRITE_ANIM_INFINITE); + // On cree un bloc mystere + xMarioMysteryBloc mb(mgr, 5, 20-5); + mb.start(150, SPRITE_ANIM_INFINITE); - xSpriteAnimation mario_green_shell( - mgr, - "src/mario_crop.png", - (SDL_Rect){50, 0, 50, 50} - ); - 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); - /* [n-1] Boucle infinie =========================================================*/ - mgr->update(); + + // Gestion des evenements + SDL_Event event; + mgr->attachEvent(SDL_KEYDOWN, &keydownEventHandler); + mgr->attachEvent(SDL_QUIT, &quitEventHandler); + + // Boucle de traitement + mgr->update(); mgr->update(); mgr->manageFps(FPS); - running = true; while(running){ - // Gestion d'un evenement - mgr->waitEvent(SDL_QUIT, &quitEventHandler); - // cout << "Main loop" << endl; + // Gestion des evenements + while( SDL_PollEvent(&event) != 0 ) + mgr->manageEvents(&event); - // Gestion des FPS (vitesse de la boucle) - mgr->manageFps(); + + + mgr->manageFps(); // Gestion des FPS (speed) mgr->update(); // Mise a jour du rendu } @@ -87,7 +83,24 @@ int main(int argc, char* argv[]) { } +/* GESTION DE QUAND ON QUITTE LA FENETRE +* +* @e Pointeur sur l'evenement appelant +* +*/ void quitEventHandler(SDL_Event *e){ cout << "Exiting program" << endl; running = false; +} + + + + +/* GESTION DE QUAND ON APPUIE SUR FLECHE BAS +* +* @e Pointeur sur l'evenement appelant +* +*/ +void keydownEventHandler(SDL_Event *e){ + cout << "BAS" << endl; } \ No newline at end of file diff --git a/SDL#4/main.h b/SDL#4/main.h index 6e1be91..a904dbe 100644 --- a/SDL#4/main.h +++ b/SDL#4/main.h @@ -10,7 +10,8 @@ /* [LIB] Externes =========================================================*/ - #include "xSDL.h" // Librairie perso + #include "xSDL.h" // Librairie perso + #include "xMario.h" // Elements utiles au jeu /* [NS] Namespace =========================================================*/ @@ -18,13 +19,14 @@ /* [CONST] Constantes et enumerations =========================================================*/ - #define WIN_WIDTH 1200 - #define WIN_HEIGHT 800 + #define BLOC_WIDTH 32 + #define BLOC_HEIGHT 20 #define FPS 60 /* [FONCTIONS] Fonctions du corps =========================================================*/ void quitEventHandler(SDL_Event *e); + void keydownEventHandler(SDL_Event *e); #endif \ No newline at end of file diff --git a/SDL#4/main.o b/SDL#4/main.o index 1f71867..6b77791 100644 Binary files a/SDL#4/main.o and b/SDL#4/main.o differ diff --git a/SDL#4/src/bg1.png b/SDL#4/src/bg1.png new file mode 100644 index 0000000..83b53e2 Binary files /dev/null and b/SDL#4/src/bg1.png differ diff --git a/SDL#4/src/bg_grass.png b/SDL#4/src/bg_grass.png new file mode 100644 index 0000000..0ba3bdc Binary files /dev/null and b/SDL#4/src/bg_grass.png differ diff --git a/SDL#4/src/bloc.png b/SDL#4/src/bloc.png deleted file mode 100644 index 6541d57..0000000 Binary files a/SDL#4/src/bloc.png and /dev/null differ diff --git a/SDL#4/src/bloc_crop.png b/SDL#4/src/bloc_crop.png deleted file mode 100644 index ed44639..0000000 Binary files a/SDL#4/src/bloc_crop.png and /dev/null differ diff --git a/SDL#4/src/blocs.png b/SDL#4/src/blocs.png new file mode 100644 index 0000000..be0d152 Binary files /dev/null and b/SDL#4/src/blocs.png differ diff --git a/SDL#4/src/coins.jpg b/SDL#4/src/coins.jpg deleted file mode 100644 index db7b8fa..0000000 Binary files a/SDL#4/src/coins.jpg and /dev/null differ diff --git a/SDL#4/src/mario.jpg b/SDL#4/src/mario.jpg deleted file mode 100644 index d6e7c23..0000000 Binary files a/SDL#4/src/mario.jpg and /dev/null differ diff --git a/SDL#4/src/mario.png b/SDL#4/src/mario.png new file mode 100644 index 0000000..ddaf04f Binary files /dev/null and b/SDL#4/src/mario.png differ diff --git a/SDL#4/src/mario_blocs.gif b/SDL#4/src/mario_blocs.gif new file mode 100644 index 0000000..449408a Binary files /dev/null and b/SDL#4/src/mario_blocs.gif differ diff --git a/SDL#4/src/mario_crop.png b/SDL#4/src/mario_crop.png index da7b81f..f93e285 100644 Binary files a/SDL#4/src/mario_crop.png and b/SDL#4/src/mario_crop.png differ diff --git a/SDL#4/mario_ground.jpg b/SDL#4/src/mario_ground.jpg similarity index 100% rename from SDL#4/mario_ground.jpg rename to SDL#4/src/mario_ground.jpg diff --git a/SDL#4/src/myst_bloc.png b/SDL#4/src/myst_bloc.png new file mode 100644 index 0000000..3d288ea Binary files /dev/null and b/SDL#4/src/myst_bloc.png differ diff --git a/SDL#4/todo.md b/SDL#4/todo.md new file mode 100644 index 0000000..48277d1 --- /dev/null +++ b/SDL#4/todo.md @@ -0,0 +1,8 @@ +A FAIRE +======= +- [x] Auto-texture pour le sol (grass) + + + +FAIT +==== \ No newline at end of file diff --git a/SDL#4/xMario.h b/SDL#4/xMario.h new file mode 100644 index 0000000..13e0c0e --- /dev/null +++ b/SDL#4/xMario.h @@ -0,0 +1,27 @@ +#ifndef DEF_XMARIO_H + + #define DEF_XMARIO_H + + /* [LIBS] Internes + =========================================================*/ + // #include "xSDL.h" + + + /* [DEF] Constantes et enumerations + =========================================================*/ + #define BLOC_SIZE 32 + + /* [HEADERS] Inclusion des .h des sous-libs + =========================================================*/ + #include "xMario/xMarioGrass.h" + #include "xMario/xMarioGreenShell.h" + #include "xMario/xMarioMysteryBloc.h" + + + /* [BODIES] Inclusion des .cpp des sous-libs + =========================================================*/ + #include "xMario/xMarioGrass.cpp" + #include "xMario/xMarioGreenShell.cpp" + #include "xMario/xMarioMysteryBloc.cpp" + +#endif \ No newline at end of file diff --git a/SDL#4/xMario/xMarioGrass.cpp b/SDL#4/xMario/xMarioGrass.cpp new file mode 100644 index 0000000..3479bbb --- /dev/null +++ b/SDL#4/xMario/xMarioGrass.cpp @@ -0,0 +1,104 @@ +/* [CONSTRUCTOR] Construction d'un xSMarioGrass +=========================================================*/ +xMarioGrass::xMarioGrass(xManager *m, SDL_Rect rect){ + _manager = m; + + // Note: le rect correspond a un nombre de bloc + // On convertit le tout en blocs reels + int xMin = rect.x; + int xMax = rect.w + rect.x - 1; + + int yMin = rect.y; + int yMax = rect.h + rect.y - 1; + + + /* (1) On charge la texture */ + _spritesheet = IMG_LoadTexture(_manager->renderer(), "src/mario_ground.jpg"); + + + int index = 0; + + /* (2) On cree les plans (layers) */ + for( int y = yMin ; y <= yMax ; y++ ){ + for( int x = xMin ; x <= xMax ; x++ ){ + // On cree une copie du spritesheet + this->add( new xSprite(_manager, _spritesheet) ); + + + // TOP-LEFT + if( x == xMin && y == yMin ){ + this->get(index)->dimensions( + (SDL_Rect){BLOC_SIZE*x, BLOC_SIZE*y, BLOC_SIZE, BLOC_SIZE}, + (SDL_Rect){1, 0, 16, 16} + ); + } + + // TOP RIGHT + else if( x == xMax && y == yMin ){ + this->get(index)->dimensions( + (SDL_Rect){BLOC_SIZE*x, BLOC_SIZE*y, BLOC_SIZE, BLOC_SIZE}, + (SDL_Rect){35, 0, 16, 16} + ); + } + + // BOTTOM LEFT + else if( x == xMin && y == yMax ){ + this->get(index)->dimensions( + (SDL_Rect){BLOC_SIZE*x, BLOC_SIZE*y, BLOC_SIZE, BLOC_SIZE}, + (SDL_Rect){1, 34, 16, 16} + ); + } + + // BOTTOM RIGHT + else if( x == xMax && y == yMax ){ + this->get(index)->dimensions( + (SDL_Rect){BLOC_SIZE*x, BLOC_SIZE*y, BLOC_SIZE, BLOC_SIZE}, + (SDL_Rect){35, 34, 16, 16} + ); + } + + // LEFT + else if( x == xMin ){ + this->get(index)->dimensions( + (SDL_Rect){BLOC_SIZE*x, BLOC_SIZE*y, BLOC_SIZE, BLOC_SIZE}, + (SDL_Rect){1, 17, 16, 16} + ); + } + + // RIGHT + else if( x == xMax ){ + this->get(index)->dimensions( + (SDL_Rect){BLOC_SIZE*x, BLOC_SIZE*y, BLOC_SIZE, BLOC_SIZE}, + (SDL_Rect){35, 17, 16, 16} + ); + } + + // TOP + else if( y == yMin ){ + this->get(index)->dimensions( + (SDL_Rect){BLOC_SIZE*x, BLOC_SIZE*y, BLOC_SIZE, BLOC_SIZE}, + (SDL_Rect){18, 0, 16, 16} + ); + } + + // BOTTOM + else if( y == yMax ){ + this->get(index)->dimensions( + (SDL_Rect){BLOC_SIZE*x, BLOC_SIZE*y, BLOC_SIZE, BLOC_SIZE}, + (SDL_Rect){18, 34, 16, 16} + ); + } + + // INSIDE + else{ + this->get(index)->dimensions( + (SDL_Rect){BLOC_SIZE*x, BLOC_SIZE*y, BLOC_SIZE, BLOC_SIZE}, + (SDL_Rect){18, 17, 16, 16} + ); + } + + index++; + } + } + +} \ No newline at end of file diff --git a/SDL#4/xMario/xMarioGrass.h b/SDL#4/xMario/xMarioGrass.h new file mode 100644 index 0000000..53224bf --- /dev/null +++ b/SDL#4/xMario/xMarioGrass.h @@ -0,0 +1,18 @@ +#ifndef DEF_XMARIOGRASS_H + + #define DEF_XMARIOGRASS_H + + /* [DEF] Definition de la classe + =========================================================*/ + class xMarioGrass : public xSpriteGroup{ + + public: + xMarioGrass(xManager *m, SDL_Rect rect); + + private: + xManager *_manager; + SDL_Texture *_spritesheet; // Contiendra la texture + + }; + +#endif \ No newline at end of file diff --git a/SDL#4/xMario/xMarioGreenShell.cpp b/SDL#4/xMario/xMarioGreenShell.cpp new file mode 100644 index 0000000..ec99f5a --- /dev/null +++ b/SDL#4/xMario/xMarioGreenShell.cpp @@ -0,0 +1,15 @@ +/* [CONSTRUCTOR] Construction d'un xMarioGreenShell +=========================================================*/ +xMarioGreenShell::xMarioGreenShell(xManager *m, int x, int y) +: xSpriteAnimation( + m, + "src/mario_crop.png", + (SDL_Rect){BLOC_SIZE*x, BLOC_SIZE*y, BLOC_SIZE, BLOC_SIZE} +){ + + /* (2) On definit les clip de chaque frame */ + this->addFrame( (SDL_Rect){4*19, 210, 20, 20} ); + this->addFrame( (SDL_Rect){5*19, 210, 20, 20} ); + this->addFrame( (SDL_Rect){6*19, 210, 20, 20} ); + this->addFrame( (SDL_Rect){7*19, 210, 20, 20} ); +} \ No newline at end of file diff --git a/SDL#4/xMario/xMarioGreenShell.h b/SDL#4/xMario/xMarioGreenShell.h new file mode 100644 index 0000000..dce55a3 --- /dev/null +++ b/SDL#4/xMario/xMarioGreenShell.h @@ -0,0 +1,11 @@ +#ifndef DEF_XMARIOGREENSHELL_H + + #define DEF_XMARIOGREENSHELL_H + + class xMarioGreenShell : public xSpriteAnimation{ + + public: + xMarioGreenShell(xManager *manager, int x, int y); // Spritesheet avec taille de chaque sprite + }; + +#endif \ No newline at end of file diff --git a/SDL#4/xMario/xMarioMysteryBloc.cpp b/SDL#4/xMario/xMarioMysteryBloc.cpp new file mode 100644 index 0000000..cc493d6 --- /dev/null +++ b/SDL#4/xMario/xMarioMysteryBloc.cpp @@ -0,0 +1,15 @@ +/* [CONSTRUCTOR] Construction d'un xMarioGreenShell +=========================================================*/ +xMarioMysteryBloc::xMarioMysteryBloc(xManager *m, int x, int y) +: xSpriteAnimation( + m, + "src/myst_bloc.png", + (SDL_Rect){BLOC_SIZE*x, BLOC_SIZE*y, BLOC_SIZE, BLOC_SIZE} +){ + + /* (2) On definit les clip de chaque frame */ + this->addFrame( (SDL_Rect){0, 0, 16, 16} ); + this->addFrame( (SDL_Rect){0, 16, 16, 16} ); + this->addFrame( (SDL_Rect){0, 32, 16, 16} ); + this->addFrame( (SDL_Rect){0, 48, 16, 16} ); +} \ No newline at end of file diff --git a/SDL#4/xMario/xMarioMysteryBloc.h b/SDL#4/xMario/xMarioMysteryBloc.h new file mode 100644 index 0000000..697c889 --- /dev/null +++ b/SDL#4/xMario/xMarioMysteryBloc.h @@ -0,0 +1,11 @@ +#ifndef DEF_XMARIOMYSTERYBLOC_H + + #define DEF_XMARIOMYSTERYBLOC_H + + class xMarioMysteryBloc : public xSpriteAnimation{ + + public: + xMarioMysteryBloc(xManager *manager, int x, int y); // Spritesheet avec taille de chaque sprite + }; + +#endif \ No newline at end of file diff --git a/SDL#4/xSDL/xManager.cpp b/SDL#4/xSDL/xManager.cpp index 25a25d5..e9771a2 100644 --- a/SDL#4/xSDL/xManager.cpp +++ b/SDL#4/xSDL/xManager.cpp @@ -2,13 +2,12 @@ =========================================================*/ xManager::xManager(const char *t, int w, int h){ // default values - _lasttick = 0; + _lasttick = SDL_GetTicks(); _fpstime = 1000/60; _window = NULL; _renderer = NULL; _texture = NULL; - // Initialisation des sous-sys. SDL SDL_Init( SDL_INIT_EVERYTHING ); @@ -28,6 +27,10 @@ xManager::xManager(const char *t, int w, int h){ return; + // On enregistre les dimensions de la fenetre + _winrect.x = _winrect.y = 0; + SDL_GetWindowSize(_window, &_winrect.w, &_winrect.h); + // Creation du renderer _renderer = SDL_CreateRenderer( _window, @@ -125,20 +128,45 @@ void xManager::pull(SDL_Texture *t){ + + +/* [MANAGEFTP] Gestion de la vitesse de boucle +=========================================================*/ +void xManager::manageFps(const int fps){ + /* (1) Definition de fps */ + if( fps != 0 ) + _fpstime = 1000/fps; + + if( _lasttick == 0 ) + _lasttick = SDL_GetTicks()-_fpstime; + + /* (2) Utilisation en fin de boucle */ + + // 1 > Si trop rapide, on attends + if( SDL_GetTicks()-_lasttick < _fpstime ) + SDL_Delay( _fpstime - (SDL_GetTicks()-_lasttick) ); + + // On enregistre le temps actuel + _lasttick = SDL_GetTicks(); +} + + + + + /* [UPDATE] Mise a jour du rendu =========================================================*/ void xManager::update(){ // cout << "Update MAIN SPRITE +" << _sprites.size() << " added sprites.." << endl; - + // On bloque l'acces inter-thread + _mutex.lock(); /* (1) On efface le rendu */ SDL_RenderClear(_renderer); /* (2) On applique la couleur de fond */ - SDL_Rect dRect; // On recupere les dimensions de la fenetre - SDL_GetWindowSize(_window, &dRect.w, &dRect.h); - SDL_RenderDrawRect(_renderer, &dRect); + SDL_RenderDrawRect(_renderer, &_winrect); /* (3) On ajoute le rendu principal (si existe) */ @@ -154,44 +182,27 @@ void xManager::update(){ /* (n) On affiche le resultat */ SDL_RenderPresent(_renderer); + // On debloque l'acces + _mutex.unlock(); } -/* [WAITEVENT] Attends un evenement pour executer +/* [ATTACHEVENT] Ajoute une fonction a un type d'evenement =========================================================*/ -void xManager::waitEvent(SDL_EventType t, void(*handler)(SDL_Event*)){ - SDL_Event event; +void xManager::attachEvent(SDL_EventType t, void(*handler)(SDL_Event*)){ - // On attends un evenement - while( SDL_PollEvent(&event) ){ - - // quand evenement, si type = t - if( event.type == t ) - (*handler)(&event); - - } + // On attache le type d'evenement a la fonction + _events.push_back( t ); + _handlers.push_back( handler ); } -/* [MANAGEFTP] Gestion de la vitesse de boucle + + + +/* [MANAGEEVENTS] Gestion des evenements =========================================================*/ -void xManager::manageFps(const int fps){ - /* (1) Definition de fps */ - if( fps != 0 ) _fpstime = 1000/fps; - - /* (2) Initialisation timer */ - if( _lasttick == 0 ) - _lasttick = SDL_GetTicks(); - - /* (3) Utilisation en fin de WHILE() */ - - // 1 > Si trop rapide, on attends + definit _lasttick - else if( SDL_GetTicks()-_lasttick < _fpstime ){ - SDL_Delay( _fpstime - (SDL_GetTicks()-_lasttick) ); - - _lasttick = SDL_GetTicks() + _fpstime - (SDL_GetTicks()-_lasttick); - - // 2 > Temps ok - }else - _lasttick = SDL_GetTicks(); - - +void xManager::manageEvents(SDL_Event *event){ + // On lance les evenements en fonction de leur type + for( int i = 0 ; i < _events.size() ; i ++ ) + if( event->type == _events[i] ) // si type ok + (*_handlers[i])(event); // on execute le handler } \ No newline at end of file diff --git a/SDL#4/xSDL/xManager.h b/SDL#4/xSDL/xManager.h index 849d4b0..bc67a53 100644 --- a/SDL#4/xSDL/xManager.h +++ b/SDL#4/xSDL/xManager.h @@ -11,7 +11,6 @@ bool status(); bool setBackground(Uint8 r=0xff, Uint8 g=0xff, Uint8 b=0xff, Uint8 a=0xff); bool setImage(const char *url); - void waitEvent(SDL_EventType t, void(*handler)(SDL_Event*) ); void push(SDL_Texture *t, SDL_Rect *origin, SDL_Rect *dest); void pull(SDL_Texture *t); @@ -20,16 +19,28 @@ void manageFps(const int fps=0); + + // Gestion des evenements + void attachEvent(SDL_EventType t, void(*handler)(SDL_Event*) ); + void manageEvents(SDL_Event* event); + + private: // gestion FPS Uint32 _lasttick; Uint32 _fpstime; + // Gestion evenements + vector _events; + vector _handlers; + // status de l'initialisation bool _status; // Elements utiles SDL_Window *_window; + SDL_Rect _winrect; + SDL_Renderer *_renderer; SDL_Texture *_texture; diff --git a/SDL#4/xSDL/xSprite.cpp b/SDL#4/xSDL/xSprite.cpp index 53ae5ee..8eee2e4 100644 --- a/SDL#4/xSDL/xSprite.cpp +++ b/SDL#4/xSDL/xSprite.cpp @@ -21,9 +21,6 @@ xSprite::xSprite(xManager *m, const int rgb[]){ _manager = m; _texture = NULL; - - cout << "a: [" << (sizeof(rgb)/sizeof(*rgb)) << "]" << endl; - SDL_Surface *surf = SDL_CreateRGBSurface(0, 0, 0, 32, 0, 0, 0, rgb[3]); // On recupere la couleur @@ -52,14 +49,16 @@ xSprite::xSprite(xManager *m, const char *url){ /* (1) On cree la texture directement */ _texture = IMG_LoadTexture(_manager->renderer(), url); + // Gestion erreur + if( _texture == NULL ) + return; +} - /* (1bis) On cree la surface puis on la copie dans la texture */ - // SDL_Surface *surf = IMG_Load( url ); - - // _texture = SDL_CreateTextureFromSurface(_manager->renderer(), surf); - - // SDL_FreeSurface(surf); - // surf = NULL; +/* [CONSTRUCTOR] Constructions avec copie de texture +=========================================================*/ +xSprite::xSprite(xManager *m, SDL_Texture *t){ + _manager = m; + _texture = t; // Gestion erreur if( _texture == NULL ) @@ -70,8 +69,6 @@ xSprite::xSprite(xManager *m, const char *url){ /* [DIMENSIONS] Definition des dimensions par defaut =========================================================*/ void xSprite::dimensions(){ - cout <<"A"<_frame = xSA->_frames[i]; - // xSA->manager()->update(); + xSA->manager()->update(); if( SDL_GetTicks()-timer < t ) diff --git a/SDL#4/xSDL/xSpriteAnimation.h b/SDL#4/xSDL/xSpriteAnimation.h index 958d6ac..1808b63 100644 --- a/SDL#4/xSDL/xSpriteAnimation.h +++ b/SDL#4/xSDL/xSpriteAnimation.h @@ -6,8 +6,11 @@ public: xSpriteAnimation(xManager *manager, const char *url, SDL_Rect viewport); // Spritesheet avec taille de chaque sprite + xSpriteAnimation(xManager *manager, SDL_Texture *t, SDL_Rect viewport); // Spritesheet avec taille de chaque sprite ~xSpriteAnimation(); + void move(SDL_Rect newpos); // Deplace l'animation + void addFrame(SDL_Rect clip); // Ajoute une frame en fonction des coordonnees // GETTER @@ -19,7 +22,7 @@ - private: + protected: xManager *_manager; SDL_Texture *_texture; diff --git a/SDL#4/xSDL/xSpriteGroup.cpp b/SDL#4/xSDL/xSpriteGroup.cpp index d04c6de..f6269c6 100644 --- a/SDL#4/xSDL/xSpriteGroup.cpp +++ b/SDL#4/xSDL/xSpriteGroup.cpp @@ -1,7 +1,25 @@ /* [CONSTRUCTOR] Initialisation de la liste de xSprite =========================================================*/ xSpriteGroup::xSpriteGroup(){ - // _sprites = new vector(0); +} + +/* [DESTRUCTOR] Efface la liste de xSprite +=========================================================*/ +xSpriteGroup::~xSpriteGroup(){ + for( int i = _sprites.size()-1 ; i >= 0 ; i-- ) + _sprites.erase( _sprites.begin() + i ); +} + +/* [MOVE] Deplace toutes les sprites +=========================================================*/ +void xSpriteGroup::move(int x, int y){ + for( int i = 0 ; i < _sprites.size() ; i++ ){ + + SDL_Rect *dest = _sprites[i]->dst(); + + (*dest).x += x; + (*dest).y += y; + } } /* [ADD] Ajoute un xSprite au groupe diff --git a/SDL#4/xSDL/xSpriteGroup.h b/SDL#4/xSDL/xSpriteGroup.h index 3dbd9fd..db68819 100644 --- a/SDL#4/xSDL/xSpriteGroup.h +++ b/SDL#4/xSDL/xSpriteGroup.h @@ -7,6 +7,10 @@ public: xSpriteGroup(); + ~xSpriteGroup(); + + void move(int x, int y); // Deplace toutes les sprites + void add(xSprite *s); void remove(xSprite *s); xSprite* get(int i); @@ -16,7 +20,7 @@ void update(); // Fait renmonter la mise a jour du manager - private: + protected: vector _sprites; };