- [x] Optimisation de la correction apres saut
- [x] Correction du move qui fait friser parfois - [x] Permettre a move() de renvoyer qqch
This commit is contained in:
parent
49e3a04a4c
commit
29d8287d87
Binary file not shown.
Before Width: | Height: | Size: 33 KiB |
File diff suppressed because it is too large
Load Diff
|
@ -49,32 +49,41 @@ int main(int argc, char* argv[]) {
|
|||
=========================================================*/
|
||||
// On cree une coquille verte
|
||||
// xMarioGreenShell gs(mgr, 5, 20-3);
|
||||
// gs.start("green-sheel", 100, SPRITE_ANIM_INFINITE);
|
||||
// gs.push("green-sheel");
|
||||
// gs.start(100, SPRITE_ANIM_INFINITE);
|
||||
|
||||
// On cree une brique
|
||||
xMarioBrick mbr1(mgr, 4, 20-5);
|
||||
xMarioBrick mbr1(mgr, 4, 20-6);
|
||||
mbr1.push("brick1");
|
||||
|
||||
// On cree un bloc mystere
|
||||
xMarioMysteryBloc mb(mgr, 5, 20-5);
|
||||
xMarioMysteryBloc mb(mgr, 5, 20-6);
|
||||
mb.push("mystery-bloc");
|
||||
mb.start(150, SPRITE_ANIM_INFINITE);
|
||||
|
||||
// On cree une brique
|
||||
xMarioBrick mbr2(mgr, 6, 20-5);
|
||||
xMarioBrick mbr2(mgr, 6, 20-6);
|
||||
mbr2.push("brick2");
|
||||
|
||||
|
||||
xMarioMysteryBloc mb1(mgr, 15, 20-5);
|
||||
xMarioMysteryBloc mb1(mgr, 15, 20-6);
|
||||
mb1.push("mystery-bloc2");
|
||||
mb1.start(150, SPRITE_ANIM_INFINITE);
|
||||
|
||||
xMarioMysteryBloc mb2(mgr, 17, 20-5);
|
||||
mb2.push("mystery-bloc3");
|
||||
mb2.start(150, SPRITE_ANIM_INFINITE);
|
||||
|
||||
xMarioMysteryBloc mb3(mgr, 19, 20-4);
|
||||
mb3.push("mystery-bloc4");
|
||||
mb3.start(150, SPRITE_ANIM_INFINITE);
|
||||
|
||||
// On cree un bloc normal
|
||||
// xMarioBloc bl(mgr, (SDL_Rect){0, 20-2, 10, 3});
|
||||
// xMarioBloc bl(mgr, (SDL_Rect){16, 20-5, 1, 1});
|
||||
// bl.push("bloc-bottom-left");
|
||||
|
||||
// On cree mario
|
||||
mario = new xMarioMario(mgr, 5, 20-3);
|
||||
mario = new xMarioMario(mgr, 17, 20-3);
|
||||
mario->push("mario");
|
||||
mario->start(100, SPRITE_ANIM_INFINITE);
|
||||
|
||||
|
@ -90,8 +99,10 @@ int main(int argc, char* argv[]) {
|
|||
mgr->attachEvent(SDL_KEYUP, &keyupEventHandler);
|
||||
mgr->attachEvent(SDL_QUIT, &quitEventHandler);
|
||||
|
||||
mgr->debug();
|
||||
|
||||
// Boucle de traitement
|
||||
mgr->update(); mgr->update();
|
||||
mgr->update();
|
||||
mgr->manageFps(FPS);
|
||||
while(running){
|
||||
|
||||
|
@ -109,9 +120,18 @@ int main(int argc, char* argv[]) {
|
|||
mario->moveFromVelocity();
|
||||
|
||||
|
||||
if( mgr->hit("mystery-bloc", 0, 9) ) mb.jump();
|
||||
if( mgr->hit("mystery-bloc", 0, 1) ) mb.jump();
|
||||
else mb.unjump();
|
||||
|
||||
if( mgr->hit("mystery-bloc2", 0, 1) ) mb1.jump();
|
||||
else mb1.unjump();
|
||||
|
||||
if( mgr->hit("mystery-bloc3", 0, 1) ) mb2.jump();
|
||||
else mb2.unjump();
|
||||
|
||||
if( mgr->hit("mystery-bloc4", 0, 1) ) mb3.jump();
|
||||
else mb3.unjump();
|
||||
|
||||
mgr->manageFps(); // Gestion des FPS (speed)
|
||||
mgr->update(); // Mise a jour du rendu
|
||||
}
|
||||
|
|
|
@ -22,7 +22,7 @@
|
|||
#define BLOC_WIDTH 32
|
||||
#define BLOC_HEIGHT 20
|
||||
|
||||
#define FPS 60
|
||||
#define FPS 40
|
||||
|
||||
/* [FONCTIONS] Fonctions du corps
|
||||
=========================================================*/
|
||||
|
|
BIN
SDL#4/main.o
BIN
SDL#4/main.o
Binary file not shown.
|
@ -5,11 +5,15 @@ A FAIRE
|
|||
|
||||
EN COURS
|
||||
========
|
||||
- [ ] Optimisation/Correction des collisions
|
||||
- [ ] Optimisation/Correction des collisions (hit + move)
|
||||
- [ ] Erreur de hit()
|
||||
- [x] Optimisation de la correction apres saut
|
||||
- [..] Correction du move qui fait friser parfois
|
||||
- [ ] Liberation memoire car lag
|
||||
- [...] Gestion sprites de mario en fonction mouvement
|
||||
|
||||
- [ ] Permettre a move() de renvoyer qqch
|
||||
|
||||
FAIT
|
||||
====
|
||||
- [x] Gestion velocite pour deplacement
|
||||
|
|
|
@ -31,10 +31,10 @@ void xMarioMysteryBloc::jump(){
|
|||
|
||||
if( _defaultrect.y == _dst.y && SDL_GetTicks()-_lastjump > 300 ){
|
||||
|
||||
_dst.x = _defaultrect.x - _defaultrect.w*.1/2;
|
||||
_dst.y = _defaultrect.y-10 - _defaultrect.h*.1/2;
|
||||
_dst.w = _defaultrect.w * 1.1;
|
||||
_dst.h = _defaultrect.h * 1.1;
|
||||
_dst.x = _defaultrect.x;
|
||||
_dst.y = _defaultrect.y-10;
|
||||
_dst.w = _defaultrect.w;
|
||||
_dst.h = _defaultrect.h;
|
||||
|
||||
this->move(_dst);
|
||||
|
||||
|
|
|
@ -23,7 +23,7 @@ xMarioMario::xMarioMario(xManager *m, int x, int y)
|
|||
|
||||
_gravity = 19;
|
||||
|
||||
// Constantes de velocite sur X
|
||||
// Constantes de mouvement sur X
|
||||
_velocity[0] = 0.0;
|
||||
_mult[0] = 10;
|
||||
_dec[0] = .7;
|
||||
|
@ -31,7 +31,7 @@ xMarioMario::xMarioMario(xManager *m, int x, int y)
|
|||
_min_vel[0] = 0.1;
|
||||
_max_vel[0] = 10;
|
||||
|
||||
// Constantes de velocite sur Y
|
||||
// Constantes de mouvement sur Y
|
||||
_velocity[1] = 0.0;
|
||||
_mult[1] = 50;
|
||||
_dec[1] = .3;
|
||||
|
@ -39,47 +39,43 @@ xMarioMario::xMarioMario(xManager *m, int x, int y)
|
|||
_min_vel[1] = 0.2;
|
||||
_max_vel[1] = 100;
|
||||
|
||||
/**/// this->addFrame( (SDL_Rect){2, 0, 19, 29} );
|
||||
// this->addFrame( (SDL_Rect){33, 0, 19, 29} );
|
||||
// this->addFrame( (SDL_Rect){62, 0, 19, 29} );
|
||||
/**/// this->addFrame( (SDL_Rect){93, 0, 19, 29} );
|
||||
// this->addFrame( (SDL_Rect){122, 0, 19, 29} );
|
||||
// this->addFrame( (SDL_Rect){122, 0, 19, 29} );
|
||||
// this->addFrame( (SDL_Rect){153, 0, 19, 29} );
|
||||
// this->addFrame( (SDL_Rect){182, 0, 19, 29} );
|
||||
// this->addFrame( (SDL_Rect){213, 0, 19, 29} );
|
||||
// On definit les frames de l'animation par defaut
|
||||
this->addFrame( (SDL_Rect){238, 0, 19, 29} );
|
||||
// this->addFrame( (SDL_Rect){269, 0, 19, 29} );
|
||||
// this->addFrame( (SDL_Rect){298, 0, 19, 29} );
|
||||
this->addFrame( (SDL_Rect){329, 0, 19, 29} );
|
||||
this->addFrame( (SDL_Rect){329, 0, 19, 29} );
|
||||
|
||||
// /* (1) On definit les clip de chaque frame */
|
||||
// this->addFrame( (SDL_Rect){21, 0, 18, 32} );
|
||||
// this->addFrame( (SDL_Rect){42, 0, 18, 32} );
|
||||
// this->addFrame( (SDL_Rect){63, 0, 18, 32} );
|
||||
// this->addFrame( (SDL_Rect){82, 0, 18, 32} );
|
||||
// this->addFrame( (SDL_Rect){103, 0, 18, 32} );
|
||||
// this->addFrame( (SDL_Rect){125, 0, 18, 32} );
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* [MOVEFROMVELOCITY] Applique la velocite au deplacement
|
||||
=========================================================*/
|
||||
void xMarioMario::moveFromVelocity(){
|
||||
void xMarioMario::moveFromVelocity(){
|
||||
|
||||
/* (1) Si aucune collision, on deplace */
|
||||
vector<int> after = this->move(_velocity[0], _velocity[1]);
|
||||
|
||||
// after[0] -> sens sur x de la collision
|
||||
// after[1] -> sens sur y de la collision
|
||||
|
||||
if( after[2] != ((int)_velocity[0]) )
|
||||
cerr << "collision X -> " << _velocity[0] << " to " << after[2] << endl;
|
||||
|
||||
if( after[3] != ((int)_velocity[1]) )
|
||||
cerr << "collision Y -> " << _velocity[1] << " to " << after[3] << endl;
|
||||
|
||||
/* (2) On modifie la velocite en fonction des collisions */
|
||||
_velocity[0] = (double) after[0];
|
||||
_velocity[1] = (double) after[1];
|
||||
|
||||
/* (2) Si aucune collision, on deplace */
|
||||
this->move(_velocity[0], _velocity[1]);
|
||||
cout << "x -> " << _velocity[0] << endl;
|
||||
cout << "y -> " << _velocity[1] << endl;
|
||||
|
||||
/* (3) Modification du sprite en fonction du mouvement */
|
||||
this->turn();
|
||||
|
||||
|
||||
/* (1) Gestion de touche encore enfoncee */
|
||||
/* (4) Gestion de touche encore enfoncee */
|
||||
if( _left ) this->velocity(-1, 0);
|
||||
else if( _right ) this->velocity(1, 0);
|
||||
// diminution de l'acceleration quand on lache <- ET ->
|
||||
else _velocity[0] *= ( 1 - _dec[0] );
|
||||
|
||||
// Si touche haut
|
||||
|
@ -92,31 +88,33 @@ void xMarioMario::moveFromVelocity(){
|
|||
}else
|
||||
_jumps = 0;
|
||||
|
||||
/* (4) On diminue la velocite (decceleration) */
|
||||
|
||||
/* (5) On diminue la gravite */
|
||||
_velocity[1] *= ( 1 - _dec[1] );
|
||||
|
||||
|
||||
/* (5) Gestion de la gravite */
|
||||
/* (6) Gestion de la gravite */
|
||||
if( !this->onFloor() )
|
||||
this->move(0, _gravity);
|
||||
|
||||
/* (6) Si velocite sous borne min */
|
||||
|
||||
/* (7) Si velocite sous borne min, on met a 0 */
|
||||
if( abs(_velocity[0]) < _min_vel[0] ) // sur x
|
||||
_velocity[0] = 0;
|
||||
|
||||
if( abs(_velocity[1]) < _min_vel[1] ) // sur y (gravite)
|
||||
if( abs(_velocity[1]) < _min_vel[1] ) // sur y
|
||||
_velocity[1] = 0;
|
||||
|
||||
|
||||
/* (7) Gestion du temps */
|
||||
// if( abs(_velocity[0]) > 0 )
|
||||
// cout << "x -> " << _velocity[0] << endl;
|
||||
/* (8) Gestion du temps */
|
||||
if( abs(_velocity[0]) > 0 )
|
||||
cerr << "x -> " << _velocity[0] << endl;
|
||||
|
||||
// if( abs(_velocity[1]) > 0 )
|
||||
// cout << "y -> " << _velocity[1] << endl;
|
||||
if( abs(_velocity[1]) > 0 )
|
||||
cerr << "y -> " << _velocity[1] << endl;
|
||||
|
||||
// cout << endl;
|
||||
usleep(20000);
|
||||
// usleep(20000);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -8,6 +8,8 @@ xManager::xManager(const char *t, int w, int h){
|
|||
_renderer = NULL;
|
||||
_texture = NULL;
|
||||
|
||||
_debug = (SDL_Rect){0, 0, 0, 0};
|
||||
|
||||
// Initialisation des sous-sys. SDL
|
||||
SDL_Init( SDL_INIT_EVERYTHING );
|
||||
|
||||
|
@ -28,7 +30,7 @@ xManager::xManager(const char *t, int w, int h){
|
|||
|
||||
|
||||
// On enregistre les dimensions de la fenetre
|
||||
_winrect.x = _winrect.y = 0;
|
||||
_winrect.x = 0; _winrect.y = 0;
|
||||
_winrect.w = w; _winrect.h = h;
|
||||
SDL_GetWindowSize(_window, &_winrect.w, &_winrect.h);
|
||||
|
||||
|
@ -92,85 +94,13 @@ bool xManager::setImage(const char *url){
|
|||
|
||||
|
||||
|
||||
/* [HIT] Retourne si une texture est en collision avec une autre
|
||||
=========================================================*/
|
||||
bool xManager::hit(SDL_Texture *current, int movex, int movey){
|
||||
// Anti conflit inter-thread
|
||||
_mutex_hit.lock();
|
||||
|
||||
|
||||
/* (1) On recupere le SDL_Rect destination du sprite courant */
|
||||
int xIndex = -1;
|
||||
for( int i = 0 ; i < _sprites.size() ; i++ )
|
||||
if( _sprites[i] == current )
|
||||
xIndex = i;
|
||||
|
||||
if( xIndex == -1 ){
|
||||
_mutex_hit.unlock();
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
SDL_Rect a = (SDL_Rect){
|
||||
(*_dst[xIndex]).x+movex,
|
||||
(*_dst[xIndex]).y+movey,
|
||||
(*_dst[xIndex]).w,
|
||||
(*_dst[xIndex]).h
|
||||
};
|
||||
SDL_Rect b;
|
||||
|
||||
/* (2) On regarde si en dehors de la fenetre */
|
||||
for( int y = a.y ; y < a.y+a.h ; y++ )
|
||||
for( int x = a.x ; x < a.x+a.w ; x++ )
|
||||
if( x < _winrect.x || x > _winrect.x+_winrect.w || y < _winrect.y || y>_winrect.y+_winrect.h ){
|
||||
// On debloque la ressource
|
||||
_mutex_hit.unlock();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/* (3) On compare avec toutes les autres textures */
|
||||
for( int i = 0 ; i < _sprites.size() ; i++ ){
|
||||
|
||||
// Si c'est pas le sprite courant
|
||||
if( _sprites[i] != current ){
|
||||
|
||||
// taille du sprite en cours
|
||||
b.x = (*_dst[i]).x;
|
||||
b.y = (*_dst[i]).y;
|
||||
b.w = (*_dst[i]).w;
|
||||
b.h = (*_dst[i]).h;
|
||||
|
||||
for( int y = a.y ; y < a.y+a.h ; y++ )
|
||||
for( int x = a.x ; x < a.x+a.w ; x++ )
|
||||
if( x>=b.x && x<=b.x+b.w && y>=b.y && y<=b.y+b.h ){
|
||||
// On debloque la ressource
|
||||
_mutex_hit.unlock();
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// On debloque la ressource
|
||||
_mutex_hit.unlock();
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* [HIT] Retourne si une texture est en collision avec une autre
|
||||
=========================================================*/
|
||||
// bool xManager::hit(SDL_Texture *current, int movex, int movey){
|
||||
// // Anti conflit inter-thread
|
||||
// _mutex_hit.lock();
|
||||
|
||||
|
||||
|
||||
// /* (1) On recupere le SDL_Rect destination du sprite courant */
|
||||
// int xIndex = -1;
|
||||
// for( int i = 0 ; i < _sprites.size() ; i++ )
|
||||
|
@ -189,20 +119,17 @@ bool xManager::hit(SDL_Texture *current, int movex, int movey){
|
|||
// (*_dst[xIndex]).w,
|
||||
// (*_dst[xIndex]).h
|
||||
// };
|
||||
|
||||
|
||||
// SDL_Rect b = _winrect;
|
||||
// SDL_Rect b;
|
||||
|
||||
// /* (2) On regarde si en dehors de la fenetre */
|
||||
// if( !(a.x > b.x + b.w ) // Pas inclus a droite
|
||||
// && !(a.x < b.x ) // Pas inclus a gauche
|
||||
// && !(a.y < b.y ) // Pas inclus en haut
|
||||
// && !(a.y > b.y + b.h ) // Pas inclus en bas
|
||||
// ){
|
||||
// _mutex_hit.unlock();
|
||||
// return true;
|
||||
// }
|
||||
|
||||
// for( int y = a.y ; y < a.y+a.h ; y++ )
|
||||
// for( int x = a.x ; x < a.x+a.w ; x++ )
|
||||
// if( x < _winrect.x || x > _winrect.x+_winrect.w || y < _winrect.y || y>_winrect.y+_winrect.h ){
|
||||
// // On debloque la ressource
|
||||
// _mutex_hit.unlock();
|
||||
// return true;
|
||||
// }
|
||||
|
||||
|
||||
// /* (3) On compare avec toutes les autres textures */
|
||||
// for( int i = 0 ; i < _sprites.size() ; i++ ){
|
||||
|
@ -216,29 +143,115 @@ bool xManager::hit(SDL_Texture *current, int movex, int movey){
|
|||
// b.w = (*_dst[i]).w;
|
||||
// b.h = (*_dst[i]).h;
|
||||
|
||||
// // On verifie que le sprite n'entre pas en collision
|
||||
// if( !( (a.x >= b.x + b.w ) // Pas inclus a droite
|
||||
// || (a.x+a.w <= b.x ) // Pas inclus a gauche
|
||||
// || (a.y+a.h <= b.y ) // Pas inclus en haut
|
||||
// || (a.y >= b.y + b.h ) // Pas inclus en bas
|
||||
// ) ){
|
||||
// cout << "YES collision" << endl;
|
||||
// _mutex_hit.unlock();
|
||||
// return true;
|
||||
// }
|
||||
// for( int y = a.y ; y < a.y+a.h ; y++ )
|
||||
// for( int x = a.x ; x < a.x+a.w ; x++ )
|
||||
// if( x>=b.x && x<=b.x+b.w && y>=b.y && y<=b.y+b.h ){
|
||||
// // On debloque la ressource
|
||||
// _mutex_hit.unlock();
|
||||
// return true;
|
||||
// }
|
||||
|
||||
// }
|
||||
|
||||
// }
|
||||
|
||||
// cout << "NO collision" << endl;
|
||||
|
||||
// // On debloque la ressource
|
||||
// _mutex_hit.unlock();
|
||||
// return false;
|
||||
// }
|
||||
|
||||
|
||||
|
||||
/* [COLLIDE] Retourne si 2 objets sont en collision
|
||||
=========================================================*/
|
||||
bool xManager::collide(SDL_Rect a, SDL_Rect b){
|
||||
bool notCollide =
|
||||
(a.x >= b.x+b.w ) // Inclus a droite
|
||||
|| (a.x+a.w <= b.x ) // Inclus a gauche
|
||||
|| (a.y >= b.y+b.h ) // Inclus en haut
|
||||
|| (a.y+a.h <= b.y ); // Inclus en bas
|
||||
|
||||
return !notCollide;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* [HIT] Retourne si une texture est en collision avec une autre
|
||||
=========================================================*/
|
||||
bool xManager::hit(SDL_Texture *current, int movex, int movey){
|
||||
// Anti conflit inter-thread
|
||||
_mutex_hit.lock();
|
||||
|
||||
|
||||
/* (1) On recupere le SDL_Rect destination du sprite courant */
|
||||
int xIndex = -1;
|
||||
for( int i = 0 ; i < _sprites.size() ; i++ )
|
||||
if( _sprites[i] == current ){
|
||||
xIndex = i;
|
||||
break;
|
||||
}
|
||||
|
||||
if( xIndex == -1 ){
|
||||
_mutex_hit.unlock();
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
SDL_Rect a = (SDL_Rect){
|
||||
(*_dst[xIndex]).x + movex,
|
||||
(*_dst[xIndex]).y + movey,
|
||||
(*_dst[xIndex]).w,
|
||||
(*_dst[xIndex]).h
|
||||
};
|
||||
|
||||
|
||||
/* (2) On regarde si en dehors de la fenetre */
|
||||
if( (a.x < _winrect.x ) // Inclus a droite
|
||||
|| (a.x+a.w > _winrect.x+_winrect.w ) // Inclus a gauche
|
||||
|| (a.y < _winrect.y ) // Inclus en haut
|
||||
|| (a.y+a.h > _winrect.y+_winrect.h ) // Inclus en bas
|
||||
){
|
||||
_mutex_hit.unlock();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/* (3) On compare avec toutes les autres textures */
|
||||
for( int i = 0 ; i < _sprites.size() ; i++ ){
|
||||
|
||||
// Si c'est pas le sprite courant
|
||||
if( _sprites[i] != current ){
|
||||
|
||||
// On verifie que le sprite n'entre pas en collision
|
||||
if( this->collide(a, *_dst[i]) ){
|
||||
|
||||
// DEBUG
|
||||
if( i != 35 )
|
||||
cerr << "locked by sprite " << i << endl;
|
||||
|
||||
_debug = *_dst[i];
|
||||
// _debug = (SDL_Rect){547-1, 531-1, 2, 2};
|
||||
|
||||
// cout << "YES collision" << endl;
|
||||
_mutex_hit.unlock();
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
// On debloque la ressource
|
||||
_mutex_hit.unlock();
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/* [HIT] Retourne si une texture est en collision avec une autre
|
||||
=========================================================*/
|
||||
bool xManager::hit(string current, int movex, int movey){
|
||||
|
@ -424,6 +437,9 @@ void xManager::update(){
|
|||
for( int i = 0 ; i < _sprites.size() ; i++ )
|
||||
SDL_RenderCopy(_renderer, _sprites[i], _src[i], _dst[i]);
|
||||
|
||||
// DEBUG
|
||||
_debug = (SDL_Rect){_debug.x-1, _debug.y-1, _debug.w+2, _debug.h+2};
|
||||
SDL_RenderDrawRect(_renderer, &_debug);
|
||||
|
||||
/* (n) On affiche le resultat */
|
||||
SDL_RenderPresent(_renderer);
|
||||
|
@ -451,4 +467,33 @@ void xManager::manageEvents(SDL_Event *event){
|
|||
for( int i = 0 ; i < _events.size() ; i ++ )
|
||||
if( event->type == _events[i] ) // si type ok
|
||||
(*_handlers[i])(event); // on execute le handler
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* [DEBUG] Visualisation des donnees
|
||||
=========================================================*/
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -12,6 +12,8 @@
|
|||
bool setBackground(Uint8 r=0xff, Uint8 g=0xff, Uint8 b=0xff, Uint8 a=0xff);
|
||||
bool setImage(const char *url);
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
@ -33,6 +35,10 @@
|
|||
void manageEvents(SDL_Event* event);
|
||||
|
||||
|
||||
// DEBUG
|
||||
void debug();
|
||||
|
||||
|
||||
private:
|
||||
// gestion FPS
|
||||
Uint32 _lasttick;
|
||||
|
@ -65,6 +71,9 @@
|
|||
mutex _mutex_hit;
|
||||
|
||||
|
||||
// DEBUG
|
||||
SDL_Rect _debug;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
|
@ -73,7 +73,9 @@ xSprite::xSprite(xManager *m, SDL_Texture *t){
|
|||
|
||||
/* [MOVE] Modification de la position/taille du sprite
|
||||
=========================================================*/
|
||||
void xSprite::move(SDL_Rect newpos){
|
||||
vector<int> xSprite::move(SDL_Rect newpos){
|
||||
vector<int> result;
|
||||
|
||||
|
||||
if( !_manager->hit(_texture, newpos.x, newpos.y) ){
|
||||
|
||||
|
@ -88,50 +90,92 @@ void xSprite::move(SDL_Rect newpos){
|
|||
|
||||
if( newpos.h != 0)
|
||||
_dst.h = newpos.h;
|
||||
|
||||
}
|
||||
|
||||
result.push_back( newpos.x );
|
||||
result.push_back( newpos.y );
|
||||
result.push_back( newpos.w );
|
||||
result.push_back( newpos.h );
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/* [MOVE] Deplacement de la position/taille du sprite
|
||||
=========================================================*/
|
||||
void xSprite::move(int x, int y){
|
||||
// renvoie un tableau d'au moins 4 entiers
|
||||
// 1 -> -1 ou 1 en fonction de la direction de la collision sur x
|
||||
// 2 -> -1 ou 1 en fonction de la direction de la collision sur y
|
||||
// 3 -> le deplacement effectif sur x
|
||||
// 4 -> le deplacement effectif sur y
|
||||
//
|
||||
vector<int> xSprite::move(int x, int y){
|
||||
// Protection inter-thread
|
||||
_mutex_move.lock();
|
||||
|
||||
|
||||
vector<int> result;
|
||||
result.push_back(0);
|
||||
result.push_back(0);
|
||||
result.push_back(0);
|
||||
result.push_back(0);
|
||||
|
||||
|
||||
/* (1) Variables utiles */
|
||||
int incrx = x;
|
||||
int incry = y;
|
||||
bool moveY = true;
|
||||
int incrx = x;
|
||||
int incry = y;
|
||||
bool moveFasterOnX = abs(x) >= abs(y);
|
||||
|
||||
int signofx = (x==0) ? 0 : x / abs(x);
|
||||
int signofy = (y==0) ? 0 : y / abs(y);
|
||||
int signofx = (x==0) ? 0 : x / abs(x);
|
||||
int signofy = (y==0) ? 0 : y / abs(y);
|
||||
|
||||
|
||||
/* (2) Tant qu'on n'a pas bouge */
|
||||
while( incrx != 0 || incry != 0 ){
|
||||
/* (2) Tant qu'on peut bouger (ni x ni y ne vaut 0) */
|
||||
while( incrx!=0 || incry!=0 ){
|
||||
|
||||
/* (3) Si on peut aller a la destination */
|
||||
if( !_manager->hit(_texture, incrx, incry) ){
|
||||
_dst.x += incrx;
|
||||
_dst.y += incry;
|
||||
return;
|
||||
|
||||
cerr << ">>> not locked <<<" << endl;
|
||||
|
||||
result[0] = incrx;
|
||||
result[1] = incry;
|
||||
|
||||
_mutex_move.unlock();
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/* (4) Sinon, on decremente les deplacements 'alternativement' */
|
||||
if( moveFasterOnX ){ // Si on a plus de mouvement horizontal
|
||||
if( signofx != 0 && incrx != 0 ) // si la vel. sur x n'est pas nulle
|
||||
incrx -= signofx; // on diminue la vel. sur x
|
||||
else if( signofy != 0 && incry != 0 ) // sinon si vel. sur y n'est pas nulle
|
||||
incry -= signofy; // on diminue la vel. sur y
|
||||
else // sinon, velocite nulle
|
||||
return result; // On arrete de chercher
|
||||
|
||||
}else{ // Si on a plus de mouvement vertical
|
||||
if( signofy != 0 && incry != 0 ) // si la vel. sur y n'est pas nulle
|
||||
incry -= signofy; // on diminue la vel. sur y
|
||||
else if( signofx != 0 && incrx != 0 ) // sinon si vel. sur x n'est pas nulle
|
||||
incrx -= signofx; // on diminue la vel. sur x
|
||||
else // sinon, velocite nulle
|
||||
return result; // On arrete de chercher
|
||||
}
|
||||
|
||||
|
||||
// cout << "NO" << endl;
|
||||
|
||||
/* (4) Sinon, on decremente les deplacements alternativement */
|
||||
if( moveY )
|
||||
incry -= signofy;
|
||||
else
|
||||
incrx -= signofx;
|
||||
|
||||
moveY = !moveY;
|
||||
// if( !_manager->hit(_texture, 0, 1) )
|
||||
// cerr << "locked from (" << _dst.x << ", " << _dst.y << ") to (" << incrx << ", " << incry << ")" << endl;
|
||||
}
|
||||
|
||||
|
||||
// Mise a jour si on a bouge
|
||||
// if( incrx != 0 && incry != 0 )
|
||||
_manager->update();
|
||||
// retour
|
||||
result[0] = incrx;
|
||||
result[1] = incry;
|
||||
|
||||
_mutex_move.unlock();
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -12,8 +12,8 @@
|
|||
~xSprite();
|
||||
|
||||
|
||||
void move(SDL_Rect newpos); // Deplace le sprite
|
||||
void move(int x, int y); // Deplace le sprite
|
||||
vector<int> move(SDL_Rect newpos); // Deplace le sprite
|
||||
vector<int> move(int x, int y); // Deplace le sprite
|
||||
|
||||
void dimensions(); // Dimensions par defaut
|
||||
void dimensions(SDL_Rect r); // Dimensions sortie
|
||||
|
@ -42,6 +42,9 @@
|
|||
// Enregistre le dernier index utilise
|
||||
string _index;
|
||||
|
||||
// Protection inter-thread
|
||||
mutex _mutex_move;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
|
@ -93,7 +93,7 @@ void xSpriteAnimationProcess(xSpriteAnimation *xSA, int t, int flags){
|
|||
timer = SDL_GetTicks();
|
||||
|
||||
|
||||
// On met a jour la frame
|
||||
// On met a jour la frame (_src uniquement)
|
||||
xSA->_src = xSA->_frames.at(i);
|
||||
|
||||
xSA->manager()->update();
|
||||
|
|
Loading…
Reference in New Issue