diff --git a/SDL#4/exe b/SDL#4/exe index ce808ce..690c584 100755 Binary files a/SDL#4/exe and b/SDL#4/exe differ diff --git a/SDL#4/main.h b/SDL#4/main.h index 851d5d5..8f6bc4f 100644 --- a/SDL#4/main.h +++ b/SDL#4/main.h @@ -10,8 +10,12 @@ /* [LIB] Externes =========================================================*/ + class xSprite; #include "xSDL.h" // Librairie perso #include "xMario.h" // Elements utiles au jeu + + #include "xSDL.cpp" // Librairie perso + #include "xMario.cpp" // Elements utiles au jeu /* [NS] Namespace =========================================================*/ diff --git a/SDL#4/main.o b/SDL#4/main.o index 2a10f60..700522c 100644 Binary files a/SDL#4/main.o and b/SDL#4/main.o differ diff --git a/SDL#4/todo.md b/SDL#4/todo.md index 1152db6..d87f692 100644 --- a/SDL#4/todo.md +++ b/SDL#4/todo.md @@ -1,6 +1,5 @@ A FAIRE ======= -- [ ] @pushsprites Ajout de Sprites et non uniquement de SDL_Textures au xManager - [x][ ] Gestion du saut unique ou double (limitation) @@ -15,6 +14,7 @@ EN COURS FAIT ==== +- [x] @pushsprites Ajout de Sprites et non uniquement de SDL_Textures au xManager - [x] Permettre a move() de renvoyer qqch - [x] Gestion velocite pour deplacement - [x] Gestion de l'acceleration diff --git a/SDL#4/xMario.cpp b/SDL#4/xMario.cpp new file mode 100644 index 0000000..6f17b25 --- /dev/null +++ b/SDL#4/xMario.cpp @@ -0,0 +1,19 @@ +#ifdef DEF_XMARIO_H + + #ifndef DEF_XMARIO_CPP + + #define DEF_XMARIO_CPP + + /* [BODIES] Inclusion des .cpp des sous-libs + =========================================================*/ + #include "xMario/xMarioMario.cpp" + #include "xMario/xMarioGrass.cpp" + #include "xMario/xMarioGreenShell.cpp" + #include "xMario/xMarioBloc.cpp" + + #include "xMario/BreakableBloc/xMarioMysteryBloc.cpp" + #include "xMario/BreakableBloc/xMarioBrick.cpp" + + #endif + +#endif \ No newline at end of file diff --git a/SDL#4/xMario.h b/SDL#4/xMario.h index daf58fe..ae6d06a 100644 --- a/SDL#4/xMario.h +++ b/SDL#4/xMario.h @@ -21,15 +21,4 @@ #include "xMario/BreakableBloc/xMarioMysteryBloc.h" #include "xMario/BreakableBloc/xMarioBrick.h" - - /* [BODIES] Inclusion des .cpp des sous-libs - =========================================================*/ - #include "xMario/xMarioMario.cpp" - #include "xMario/xMarioGrass.cpp" - #include "xMario/xMarioGreenShell.cpp" - #include "xMario/xMarioBloc.cpp" - - #include "xMario/BreakableBloc/xMarioMysteryBloc.cpp" - #include "xMario/BreakableBloc/xMarioBrick.cpp" - #endif \ No newline at end of file diff --git a/SDL#4/xMario/xMarioMario.cpp b/SDL#4/xMario/xMarioMario.cpp index dbb22e0..132ff38 100644 --- a/SDL#4/xMario/xMarioMario.cpp +++ b/SDL#4/xMario/xMarioMario.cpp @@ -175,7 +175,7 @@ void xMarioMario::velocity(double x, double y){ bool xMarioMario::onFloor(){ - return _manager->hit(_texture, 0, 1); + return _manager->hit((xSprite*)this, 0, 1); } diff --git a/SDL#4/xSDL.cpp b/SDL#4/xSDL.cpp new file mode 100644 index 0000000..79e0104 --- /dev/null +++ b/SDL#4/xSDL.cpp @@ -0,0 +1,16 @@ +#ifdef DEF_XSDL_H + + #ifndef DEF_XSDL_CPP + + #define DEF_XSDL_CPP + + /* [BODIES] Inclusion des .cpp des sous-libs + =========================================================*/ + #include "xSDL/xManager.cpp" + #include "xSDL/xSprite.cpp" + #include "xSDL/xSpriteGroup.cpp" + #include "xSDL/xSpriteAnimation.cpp" + + #endif + +#endif \ No newline at end of file diff --git a/SDL#4/xSDL.h b/SDL#4/xSDL.h index 4e17a06..b46d427 100644 --- a/SDL#4/xSDL.h +++ b/SDL#4/xSDL.h @@ -29,12 +29,4 @@ #include "xSDL/xSpriteGroup.h" #include "xSDL/xSpriteAnimation.h" - - /* [BODIES] Inclusion des .cpp des sous-libs - =========================================================*/ - #include "xSDL/xManager.cpp" - #include "xSDL/xSprite.cpp" - #include "xSDL/xSpriteGroup.cpp" - #include "xSDL/xSpriteAnimation.cpp" - #endif \ No newline at end of file diff --git a/SDL#4/xSDL/xManager.cpp b/SDL#4/xSDL/xManager.cpp index 954fccc..e9f3070 100644 --- a/SDL#4/xSDL/xManager.cpp +++ b/SDL#4/xSDL/xManager.cpp @@ -202,7 +202,7 @@ bool xManager::collide(SDL_Rect a, SDL_Rect b){ /* [HIT] Retourne si une texture est en collision avec une autre =========================================================*/ -bool xManager::hit(SDL_Texture *current, int movex, int movey){ +bool xManager::hit(xSprite* current, int movex, int movey){ if( !this->status() ) return true; // Anti conflit inter-thread @@ -223,12 +223,9 @@ bool xManager::hit(SDL_Texture *current, int movex, int movey){ } - SDL_Rect a = (SDL_Rect){ - (*_dst[xIndex]).x + movex, - (*_dst[xIndex]).y + movey, - (*_dst[xIndex]).w, - (*_dst[xIndex]).h - }; + SDL_Rect a = *current->dst(); + a.x += movex; + a.y += movey; /* (2) On regarde si en dehors de la fenetre */ @@ -249,13 +246,13 @@ bool xManager::hit(SDL_Texture *current, int movex, int movey){ if( _sprites[i] != current ){ // On verifie que le sprite n'entre pas en collision - if( this->collide(a, *_dst[i]) ){ + if( this->collide(a, *(_sprites[i])->dst()) ){ // DEBUG // if( i != 35 ) // cerr << "locked by sprite " << i << endl; - _debug = *_dst[i]; + _debug = *(_sprites[i])->dst(); // _debug = (SDL_Rect){547-1, 531-1, 2, 2}; // cout << "YES collision" << endl; @@ -283,26 +280,26 @@ bool xManager::hit(string current, int movex, int movey){ _mutex_hit.lock(); /* (1) On recupere le SDL_Rect destination du sprite courant */ - SDL_Texture *texture = NULL; - texture = this->getTexture(current); + xSprite *sprite = NULL; + sprite = this->get(current); // Gestion erreur - if( texture == NULL ){ + if( sprite == NULL ){ _mutex_hit.unlock(); return false; } // Retour du resultat _mutex_hit.unlock(); - return this->hit(texture, movex, movey); + return this->hit(sprite, movex, movey); } -/* [GETTEXTURE] Renvoie la texture +/* [GET] Renvoie le sprite =========================================================*/ -SDL_Texture *xManager::getTexture(string index){ +xSprite *xManager::get(string index){ if( !this->status() ) return NULL; @@ -315,47 +312,16 @@ SDL_Texture *xManager::getTexture(string index){ } -/* [GETSRC] Renvoie le SDL_Rect source -=========================================================*/ -SDL_Rect *xManager::getSrc(string index){ - if( !this->status() ) return NULL; - - // On cherche la texture avec l'index - for( int i = 0 ; i < _indexes.size() ; i++ ) - if( _indexes[i] == index ) - return _src[i]; - - return NULL; -} - - -/* [GETDST] Renvoie le SDL_Rect destination -=========================================================*/ -SDL_Rect *xManager::getDst(string index){ - if( !this->status() ) return NULL; - - // On cherche la texture avec l'index - for( int i = 0 ; i < _indexes.size() ; i++ ) - if( _indexes[i] == index ) - return _dst[i]; - - return NULL; -} - - - /* [PUSH] Ajoute une texture au rendu principal =========================================================*/ -void xManager::push(string index, SDL_Texture *t, SDL_Rect *src, SDL_Rect *dst){ +void xManager::push(string index, xSprite* sprite){ if( !this->status() ) return; // On bloque l'acces inter-thread _mutex_push.lock(); _indexes.push_back( index ); - _sprites.push_back( t ); - _src.push_back( src ); - _dst.push_back( dst ); + _sprites.push_back( sprite ); // On debloque l'acces _mutex_push.unlock(); @@ -383,8 +349,6 @@ void xManager::pull(string index){ // On supprime la texture et ses dimensions _indexes.erase( _indexes.begin() + xIndex ); _sprites.erase( _sprites.begin() + xIndex ); - _src.erase( _src.begin() + xIndex ); - _dst.erase( _dst.begin() + xIndex ); // On debloque l'acces _mutex_pull.unlock(); @@ -393,7 +357,7 @@ void xManager::pull(string index){ /* [PULL] Retire une texture du rendu principal =========================================================*/ -void xManager::pull(SDL_Texture *t){ +void xManager::pull(xSprite* sprite){ if( !this->status() ) return; // On bloque l'acces inter-thread @@ -403,7 +367,7 @@ void xManager::pull(SDL_Texture *t){ int xIndex = -1; for( int i = 0 ; i < _sprites.size() ; i++ ) - if( _sprites[i] == t ) xIndex = i; + if( _sprites[i] == sprite ) xIndex = i; // Si on a rien trouve if( xIndex == -1 ) @@ -412,8 +376,6 @@ void xManager::pull(SDL_Texture *t){ // On supprime la texture et ses dimensions _indexes.erase( _indexes.begin() + xIndex ); _sprites.erase( _sprites.begin() + xIndex ); - _src.erase( _src.begin() + xIndex ); - _dst.erase( _dst.begin() + xIndex ); // On debloque l'acces _mutex_pull.unlock(); @@ -474,7 +436,12 @@ void xManager::update(){ /* (4) On ajoute toutes les textures pushees */ for( int i = 0 ; i < _sprites.size() ; i++ ) - SDL_RenderCopy(_renderer, _sprites[i], _src[i], _dst[i]); + SDL_RenderCopy( + _renderer, + _sprites[i]->texture(), + _sprites[i]->src(), + _sprites[i]->dst() + ); // DEBUG _debug = (SDL_Rect){_debug.x-1, _debug.y-1, _debug.w+2, _debug.h+2}; @@ -537,6 +504,6 @@ void xManager::manageEvents(SDL_Event *event){ void xManager::debug(){ for( int i = 0 ; i < _sprites.size() ; i++ ){ cerr << "INDEX: " << _indexes[i] << " AT " << i << endl; - cerr << " (" << (*_dst[i]).x <<","<<(*_dst[i]).y<<") -> (" << (*_dst[i]).w << ", " << (*_dst[i]).h << ")" << endl; + cerr << " (" << (*_sprites[i]->dst()).x <<","<<(*_sprites[i]->dst()).y<<") -> (" << (*_sprites[i]->dst()).w << ", " << (*_sprites[i]->dst()).h << ")" << endl; } } \ No newline at end of file diff --git a/SDL#4/xSDL/xManager.h b/SDL#4/xSDL/xManager.h index ef3eb29..2c908f4 100644 --- a/SDL#4/xSDL/xManager.h +++ b/SDL#4/xSDL/xManager.h @@ -14,16 +14,14 @@ bool collide(SDL_Rect a, SDL_Rect b); // Collision entre 2 SDL_Rect - bool hit(SDL_Texture *current, int movex=0, int movey=0); // Gestion des collisions - bool hit(string current, int movex=0, int movey=0); // Gestion des collisions + bool hit(xSprite *current, int movex=0, int movey=0); // Gestion des collisions + bool hit(string current, int movex=0, int movey=0); // Gestion des collisions - SDL_Texture *getTexture(string index); - SDL_Rect *getSrc(string index); - SDL_Rect *getDst(string index); + xSprite* get(string index); // retourne une sprite - void push(string index, SDL_Texture *t, SDL_Rect *origin, SDL_Rect *dest); + void push(string index, xSprite* sprite); void pull(string index); - void pull(SDL_Texture *t); + void pull(xSprite *sprite); void update(); @@ -60,9 +58,7 @@ // Gestion des textures vector _indexes; - vector _sprites; - vector _src; - vector _dst; + vector _sprites; // Protection thread-safe mutex _mutex_push; diff --git a/SDL#4/xSDL/xSprite.cpp b/SDL#4/xSDL/xSprite.cpp index 33d88bb..0ce29dc 100644 --- a/SDL#4/xSDL/xSprite.cpp +++ b/SDL#4/xSDL/xSprite.cpp @@ -77,7 +77,7 @@ vector xSprite::move(SDL_Rect newpos){ vector result; - if( !_manager->hit(_texture, newpos.x, newpos.y) ){ + if( !_manager->hit(this, newpos.x, newpos.y) ){ if( newpos.x != 0 ) _dst.x = newpos.x; @@ -133,7 +133,7 @@ vector xSprite::move(int x, int y){ while( incrx!=0 || incry!=0 ){ /* (3) Si on peut aller a la destination */ - if( !_manager->hit(_texture, incrx, incry) ){ + if( !_manager->hit(this, incrx, incry) ){ _dst.x += incrx; _dst.y += incry; @@ -165,7 +165,7 @@ vector xSprite::move(int x, int y){ return result; // On arrete de chercher } - // if( !_manager->hit(_texture, 0, 1) ){ + // if( !_manager->hit(this, 0, 1) ){ // cerr << "locked from (" << _dst.x << ", " << _dst.y << ") to (" << incrx << ", " << incry << ")" << endl; // } } @@ -221,7 +221,7 @@ void xSprite::dimensions(SDL_Rect r, SDL_Rect clip){ void xSprite::push(string index){ _index = index; - _manager->push(index, _texture, &_src, &_dst); + _manager->push(index, this); } /* [PULL] Retire une sprite du rendu @@ -235,7 +235,7 @@ void xSprite::pull(string index){ /* [PULL] Retire une sprite du rendu =========================================================*/ void xSprite::pull(){ - _manager->pull( _texture ); + _manager->pull( this ); } /* [UPDATE] Mise a jour du rendu diff --git a/SDL#4/xSDL/xSpriteAnimation.cpp b/SDL#4/xSDL/xSpriteAnimation.cpp index 6f22a0d..b77f676 100644 --- a/SDL#4/xSDL/xSpriteAnimation.cpp +++ b/SDL#4/xSDL/xSpriteAnimation.cpp @@ -70,7 +70,7 @@ void xSpriteAnimation::push(string index){ _src = _frames[0]; /* (1) On ajoute le sprite au rendu */ - _manager->push(index, _texture, &_src, &_dst); + _manager->push(index, (xSprite*)this); }