corrections en cours + sprites animes

This commit is contained in:
xdrm-brackets 2016-03-14 13:14:35 +01:00
parent 8671bdd9a5
commit 18d07bef77
9 changed files with 201 additions and 235 deletions

BIN
SDL#4/exe

Binary file not shown.

View File

@ -76,10 +76,10 @@ int main(int argc, char* argv[]) {
mgr->attachEvent(SDL_KEYDOWN, &keydownEventHandler); mgr->attachEvent(SDL_KEYDOWN, &keydownEventHandler);
mgr->attachEvent(SDL_KEYUP, &keyupEventHandler); mgr->attachEvent(SDL_KEYUP, &keyupEventHandler);
mgr->attachEvent(SDL_QUIT, &quitEventHandler); mgr->attachEvent(SDL_QUIT, &quitEventHandler);
left_move = false; mario->_left = false;
right_move = false; mario->_right = false;
up_move = false; mario->_up = false;
down_move = false; mario->_down = false;
// Boucle de traitement // Boucle de traitement
mgr->update(); mgr->update(); mgr->update(); mgr->update();
@ -92,15 +92,16 @@ int main(int argc, char* argv[]) {
mgr->manageEvents(&event); mgr->manageEvents(&event);
// Gestion de la gravite // Gestion de la gravite
if( !mgr->hit("mario", 0, 5) ) if( !mario->onFloor() && SDL_GetTicks() % 10 < 5 )
mario->move(0, 5); mario->velocity(0, 1.0);
if( mgr->hit("mystery-bloc", 0, 7) ) // Deplacement
mb.jump(); mario->moveFromVelocity();
else
mb.unjump();
if( mgr->hit("mystery-bloc", 0, 7) ) mb.jump();
else mb.unjump();
mgr->manageFps(); // Gestion des FPS (speed) mgr->manageFps(); // Gestion des FPS (speed)
mgr->update(); // Mise a jour du rendu mgr->update(); // Mise a jour du rendu
} }
@ -156,23 +157,31 @@ void keydownEventHandler(SDL_Event *e){
switch( (*e).key.keysym.sym ){ switch( (*e).key.keysym.sym ){
case SDLK_UP: case SDLK_UP:
up_move = true; if( !mario->_up ){
mario->moveUp(&up_move); mario->_up = true;
// mario->velocity(0, -40.0);
}
break; break;
case SDLK_LEFT: case SDLK_LEFT:
left_move = true; if( !mario->_left ){
mario->moveLeft(&left_move); mario->_left = true;
// mario->velocity(-15.0, 0);
}
break; break;
case SDLK_RIGHT: case SDLK_RIGHT:
right_move = true; if( !mario->_right ){
mario->moveRight(&right_move); mario->_right = true;
// mario->velocity(15.0, 0);
}
break; break;
case SDLK_DOWN: case SDLK_DOWN:
// down_move = true; if( !mario->_down ){
// mario->moveDown(&down_move); mario->_down = true;
// mario->velocity(0, 1.0);
}
break; break;
default: default:
@ -204,23 +213,19 @@ void keyupEventHandler(SDL_Event *e){
switch( (*e).key.keysym.sym ){ switch( (*e).key.keysym.sym ){
case SDLK_UP: case SDLK_UP:
up_move = false; mario->_up = false;
break; break;
case SDLK_LEFT: case SDLK_LEFT:
left_move = false; mario->_left = false;
break; break;
case SDLK_RIGHT: case SDLK_RIGHT:
right_move = false; mario->_right = false;
break; break;
case SDLK_DOWN: case SDLK_DOWN:
// down_move = false; mario->_down = false;
break; break;
default: default:

Binary file not shown.

View File

@ -6,6 +6,11 @@ A FAIRE
- [ ] Gestion de la gravite - [ ] Gestion de la gravite
EN COURS
========
- [ ] Erreur modification de frames d'une xAnimation, restent tjs les anciennes
-> creer methode dans xAnimation
- [ ] Gestion velocite pour deplacement
FAIT FAIT
==== ====

View File

@ -17,20 +17,35 @@ xMarioMario::xMarioMario(xManager *m, int x, int y)
_up = NULL; _up = NULL;
_down = NULL; _down = NULL;
// Constantes de velocite sur X
_velocity[0] = 0.0;
_mult[0] = 10;
_dec[0] = 0.9;
_acc[0] = 3.5;
_min_vel[0] = 0.1;
_max_vel[0] = 40;
// this->addFrame( (SDL_Rect){2, 0, 19, 29} ); // Constantes de velocite sur Y
_velocity[1] = 0.0;
_mult[1] = 20;
_dec[1] = 1;
_acc[1] = 1.9;
_min_vel[1] = 0.1;
_max_vel[1] = 50;
this->addFrame( (SDL_Rect){2, 0, 19, 29} );
// this->addFrame( (SDL_Rect){33, 0, 19, 29} ); // this->addFrame( (SDL_Rect){33, 0, 19, 29} );
// this->addFrame( (SDL_Rect){62, 0, 19, 29} ); // this->addFrame( (SDL_Rect){62, 0, 19, 29} );
// this->addFrame( (SDL_Rect){93, 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){122, 0, 19, 29} ); // this->addFrame( (SDL_Rect){122, 0, 19, 29} );
// this->addFrame( (SDL_Rect){153, 0, 19, 29} ); // this->addFrame( (SDL_Rect){153, 0, 19, 29} );
// this->addFrame( (SDL_Rect){182, 0, 19, 29} ); // this->addFrame( (SDL_Rect){182, 0, 19, 29} );
// this->addFrame( (SDL_Rect){213, 0, 19, 29} ); // this->addFrame( (SDL_Rect){213, 0, 19, 29} );
this->addFrame( (SDL_Rect){238, 0, 19, 29} ); /**///this->addFrame( (SDL_Rect){238, 0, 19, 29} );
// this->addFrame( (SDL_Rect){269, 0, 19, 29} ); // this->addFrame( (SDL_Rect){269, 0, 19, 29} );
// this->addFrame( (SDL_Rect){298, 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 */ // /* (1) On definit les clip de chaque frame */
// this->addFrame( (SDL_Rect){21, 0, 18, 32} ); // this->addFrame( (SDL_Rect){21, 0, 18, 32} );
@ -43,210 +58,124 @@ xMarioMario::xMarioMario(xManager *m, int x, int y)
/* [XLEFTMOVEPROCESS] Traitement async de mouvement /* [MOVEFROMVELOCITY] Applique la velocite au deplacement
=========================================================*/ =========================================================*/
void xLeftMoveProcess(xMarioMario *m, bool *run){ void xMarioMario::moveFromVelocity(){
bool hasMoved = false;
int step = 3; // Gestion de touche encore enfoncee
if( SDL_GetTicks() % 10 < 8 ){
if( _left ) this->velocity(-1, 0);
else if( _right ) this->velocity(1, 0);
if( _up ) this->velocity(0, -1);
else if( _down ) this->velocity(0, 1);
}
while( *run ){
hasMoved = false;
step = 3;
// Tant qu'on a pas bouge et qu'on peut se deplacer
while( !hasMoved && step > 0 ){
// Si aucune collision, on deplace // Si aucune collision, on deplace
if( !m->manager()->hit("mario", -step, 0) ){ if( !_manager->hit(_texture, _velocity[0], _velocity[1]) )
m->move(-step, 0); this->move(_velocity[0], _velocity[1]);
hasMoved = true;
}
// on reduit la distance de mouvement // On diminue la _velocite
step--; _velocity[0] *= ( 1 - _dec[0] );
} _velocity[1] *= ( 1 - _dec[1] );
usleep(10000); // Gestion de velocite trop basse
if( abs(_velocity[0]) < _min_vel[0] ) _velocity[0] = 0;
if( abs(_velocity[1]) < _min_vel[1] ) _velocity[1] = 0;
} cout << floor(_velocity[0]) << " - " << floor(_velocity[1]) << endl;
// _manager->update();
// usleep(10000);
} }
/* [MOVELEFT] Deplacement vers la gauche
/* [VELOCITY] Retourne un pointeur sur la velocite
=========================================================*/ =========================================================*/
void xMarioMario::moveLeft(bool *run){ double xMarioMario::velocity(bool way){
if( _left != NULL ){ // (way) ? HORIZONTAL : VERTICAL
delete _left; return (way) ? _velocity[0] : _velocity[1];
_left = NULL;
}
// On lance le thread
_left = new thread(xLeftMoveProcess, this, run);
_left->detach();
} }
/* [VELOCITY] Modifie la velocite
/* [XRIGHTMOVEPROCESS] Traitement async de mouvement
=========================================================*/ =========================================================*/
void xRightMoveProcess(xMarioMario *m, bool *run){ void xMarioMario::velocity(double x, double y){
bool hasMoved = false; double last[] = { _velocity[0], _velocity[1] };
int step = 3;
// Si meme sens, on accele
if( last[0]*x > 0 )
_velocity[0] *= _acc[0];
else{ // sinon changement sens
_velocity[0] += x*_mult[0];
while( *run ){ // Gestion des frames
if( _velocity[0] > 0 ) this->turnRight();
hasMoved = false; else this->turnLeft();
step = 3;
// Tant qu'on a pas bouge et qu'on peut se deplacer
while( !hasMoved && step > 0 ){
// Si aucune collision, on deplace
if( !m->manager()->hit("mario", step, 0) ){
m->move(step, 0);
hasMoved = true;
} }
// on reduit la distance de mouvement if( last[1]*y > 0 )
step--; _velocity[1] *= _acc[1];
} else // sinon changement sens
_velocity[1] += y*_mult[1];
usleep(10000);
}
// On retaille la velocite
if( abs(_velocity[0]) > _max_vel[0] )
_velocity[0] = _max_vel[0] * (_velocity[0] / abs(_velocity[0]) );
if( abs(_velocity[1]) > _max_vel[1]*BLOC_SIZE )
_velocity[1] = _max_vel[1] * (_velocity[1] / abs(_velocity[1]) );
} }
/* [MOVERIGHT] Deplacement vers la droite
bool xMarioMario::onFloor(){
return _manager->hit(_texture, 0, 1);
}
/* [TURNLEFT] Charge le sprite vers la gauche
=========================================================*/ =========================================================*/
void xMarioMario::moveRight(bool *run){ void xMarioMario::turnLeft(){
if( _right != NULL ){ this->stop();
delete _right;
_right = NULL; for( int i = 0 ; i < _frames.size() ; i++ )
} _frames.erase(_frames.begin()+i);
_frames.clear();
this->addFrame( (SDL_Rect){2, 0, 19, 29} );
this->addFrame( (SDL_Rect){93, 0, 19, 29} );
// On ajoute au rendu
this->start(_index, _timeout, _flags);
// On lance le thread
_right = new thread(xRightMoveProcess, this, run);
_right->detach();
} }
/* [TURNRIGHT] Charge le sprite vers la droite
/* [XUPMOVEPROCESS] Traitement async de mouvement
=========================================================*/ =========================================================*/
void xUpMoveProcess(xMarioMario *m, bool *run){ void xMarioMario::turnRight(){
bool hasMoved = false; this->stop();
int step = 7;
for( int i = 0 ; i < _frames.size() ; i++ )
_frames.erase(_frames.begin()+i);
_frames.clear();
while( *run ){ this->addFrame( (SDL_Rect){238, 0, 19, 29} );
this->addFrame( (SDL_Rect){329, 0, 19, 29} );
hasMoved = false;
step = 7;
// Tant qu'on a pas bouge et qu'on peut se deplacer // On ajoute au rendu
while( !hasMoved && step > 0 ){ this->start(_index, _timeout, _flags);
// Si aucune collision, on deplace
if( !m->manager()->hit("mario", 0, -step) ){
m->move(0, -step);
hasMoved = true;
}
// on reduit la distance de mouvement
step--;
}
usleep(10000);
}
}
/* [MOVEUP] Deplacement vers le haut
=========================================================*/
void xMarioMario::moveUp(bool *run){
if( _up != NULL ){
delete _up;
_up = NULL;
}
// On lance le thread
_up = new thread(xUpMoveProcess, this, run);
_up->detach();
}
/* [XDOWNMOVEPROCESS] Traitement async de mouvement
=========================================================*/
void xDownMoveProcess(xMarioMario *m, bool *run){
bool hasMoved = false;
int step = 5;
while( *run ){
hasMoved = false;
step = 5;
// Tant qu'on a pas bouge et qu'on peut se deplacer
while( !hasMoved && step > 0 ){
// Si aucune collision, on deplace
if( !m->manager()->hit("mario", 0, step) ){
m->move(0, step);
hasMoved = true;
}
// on reduit la distance de mouvement
step--;
}
usleep(10000);
}
}
/* [MOVEDOWN] Deplacement vers le bas
=========================================================*/
void xMarioMario::moveDown(bool *run){
if( _down != NULL ){
delete _down;
_down = NULL;
}
// On lance le thread
_down = new thread(xDownMoveProcess, this, run);
_down->detach();
} }

View File

@ -2,31 +2,49 @@
#define DEF_XMARIOMARIO_H #define DEF_XMARIOMARIO_H
/* [CST] Constantes et enumeration
=========================================================*/
#define XMARIO_VEL_HOR true // velocite verticale
#define XMARIO_VEL_VER false // velocite horizontale
/* [DEF] Definition de la classe
=========================================================*/
class xMarioMario : public xSpriteAnimation{ class xMarioMario : public xSpriteAnimation{
public: public:
xMarioMario(xManager *manager, int x, int y); // Spritesheet avec taille de chaque sprite xMarioMario(xManager *manager, int x, int y); // Spritesheet avec taille de chaque sprite
void moveLeft(bool *run); // Deplacement a gauche void moveFromVelocity();
void moveRight(bool *run); // Deplacement a droite
void moveUp(bool *run); // Deplacement en haut // Changement de frames
void moveDown(bool *run); // Deplacement en bas void turnLeft();
void turnRight();
// GETTER
double velocity(bool way=XMARIO_VEL_HOR); // Recupere velocite
bool onFloor(); // Si mario est sur le sol
// SETTER
void velocity(double x=0.0, double y=0.0); // Modification de velocite
// Gestion du suivi du deplacement
bool _left;
bool _right;
bool _up;
bool _down;
private: private:
Uint32 _lastmove; Uint32 _lastmove;
float _acceleration; double _velocity[2];
// Gestion du deplacement double _mult[2];
thread *_left; double _min_vel[2];
thread *_right; double _max_vel[2];
thread *_up; double _acc[2];
thread *_down; double _dec[2];
// Fontions du thread
friend void xLeftMoveProcess(xMarioMario *m, bool *run);
friend void xRightMoveProcess(xMarioMario *m, bool *run);
friend void xUpMoveProcess(xMarioMario *m, bool *run);
friend void xDownMoveProcess(xMarioMario *m, bool *run);
}; };

View File

@ -51,9 +51,6 @@ void xMarioMysteryBloc::jump(){
/* [UNJUMP] Animation d'activation (quand saut) /* [UNJUMP] Animation d'activation (quand saut)
=========================================================*/ =========================================================*/
void xMarioMysteryBloc::unjump(){ void xMarioMysteryBloc::unjump(){
// if( !this->active() )
// return;
if( _defaultrect.y != _viewport.y && SDL_GetTicks()-_lastjump > 100 ){ if( _defaultrect.y != _viewport.y && SDL_GetTicks()-_lastjump > 100 ){
_viewport.x = _defaultrect.x; _viewport.x = _defaultrect.x;
@ -82,29 +79,40 @@ bool xMarioMysteryBloc::active(){
void xMarioMysteryBloc::active(bool active){ void xMarioMysteryBloc::active(bool active){
_active = active; _active = active;
this->stop();
if( active ){ if( active ){
_frames.clear(); _frames.clear();
// for( int i = 0 ; i < _frames.size() ; i++ ) // for( int i = 0 ; i < _frames.size() ; i++ )
// _frames.erase(_frames.begin()+i); // _frames.erase(_frames.begin()+i);
this->pull();
this->addFrame( (SDL_Rect){0, 0, 16, 16} ); this->addFrame( (SDL_Rect){0, 0, 16, 16} );
this->addFrame( (SDL_Rect){0, 16, 16, 16} ); this->addFrame( (SDL_Rect){0, 16, 16, 16} );
this->addFrame( (SDL_Rect){0, 32, 16, 16} ); this->addFrame( (SDL_Rect){0, 32, 16, 16} );
this->addFrame( (SDL_Rect){0, 48, 16, 16} ); this->addFrame( (SDL_Rect){0, 48, 16, 16} );
// On ajoute au rendu
this->start(_index, _timeout, _flags);
}else{ }else{
_frames.clear(); _frames.clear();
// for( int i = 0 ; i < _frames.size() ; i++ ) // for( int i = 0 ; i < _frames.size() ; i++ )
// _frames.erase(_frames.begin()+i); // _frames.erase(_frames.begin()+i);
this->stop();
this->addFrame( (SDL_Rect){0, 64, 16, 16} ); this->addFrame( (SDL_Rect){0, 64, 16, 16} );
this->addFrame( (SDL_Rect){0, 64, 16, 16} );
this->addFrame( (SDL_Rect){0, 64, 16, 16} );
this->addFrame( (SDL_Rect){0, 64, 16, 16} );
// On ajoute au rendu
this->push(_index);
} }
this->start(_index, _timeout, _flags);
} }

View File

@ -9,6 +9,7 @@
#include <vector> #include <vector>
#include <mutex> #include <mutex>
#include <thread> #include <thread>
#include <cmath>
#include <unistd.h> #include <unistd.h>
/* [DEF] Definitions basiques /* [DEF] Definitions basiques

View File

@ -147,14 +147,14 @@ void xSpriteAnimation::pull(){
=========================================================*/ =========================================================*/
void xSpriteAnimationProcess(xSpriteAnimation *xSA, int t, int flags){ void xSpriteAnimationProcess(xSpriteAnimation *xSA, int t, int flags){
int timer = 0; int timer = 0;
int step = 1; bool way = true;
int start = 0; int start = 0;
int stop = xSA->_frames.size(); int stop = xSA->_frames.size();
while( flags&SPRITE_ANIM_INFINITE ){ while( flags&SPRITE_ANIM_INFINITE ){
/* (1) Pour chaque sprite */ /* (1) Pour chaque sprite */
for( int i = start ; i != xSA->_frames.size() ; i+=step ){ for( int i = start ; i != xSA->_frames.size() ; i+=(way?1:-1) ){
timer = SDL_GetTicks(); timer = SDL_GetTicks();
@ -172,9 +172,9 @@ void xSpriteAnimationProcess(xSpriteAnimation *xSA, int t, int flags){
// SPRITE_ANIM_REVERSE // SPRITE_ANIM_REVERSE
if( flags&SPRITE_ANIM_REVERSE ){ if( flags&SPRITE_ANIM_REVERSE ){
step *= -1; way = !way;
start = (step==1) ? 0 : xSA->_frames.size()-1; start = (way) ? 0 : xSA->_frames.size()-1;
stop = (step==1) ? xSA->_frames.size()-1 : 0; stop = (way) ? xSA->_frames.size()-1 : 0;
} }
} }