- [x] Gestion de la gravite
- [x] Erreur a corriger pour xSpriteAnimation on doit faire start() mais push() puis start() (qui est equivalent) ne marche pas
|
@ -53,8 +53,8 @@ int main(int argc, char* argv[]) {
|
|||
|
||||
// On cree un bloc mystere
|
||||
xMarioMysteryBloc mb(mgr, 5, 20-5);
|
||||
// mb.push("mystery-bloc");
|
||||
mb.start("mystery-bloc", 150, SPRITE_ANIM_INFINITE);
|
||||
mb.push("mystery-bloc");
|
||||
mb.start(150, SPRITE_ANIM_INFINITE);
|
||||
|
||||
// On cree un bloc normal
|
||||
// xMarioBloc bl(mgr, (SDL_Rect){0, 20-2, 10, 3});
|
||||
|
@ -62,8 +62,8 @@ int main(int argc, char* argv[]) {
|
|||
|
||||
// On cree mario
|
||||
mario = new xMarioMario(mgr, 5, 20-3);
|
||||
// mb.push("mario");
|
||||
mario->start("mario", 100, SPRITE_ANIM_INFINITE);
|
||||
mario->push("mario");
|
||||
mario->start(100, SPRITE_ANIM_INFINITE);
|
||||
|
||||
|
||||
|
||||
|
@ -76,10 +76,6 @@ int main(int argc, char* argv[]) {
|
|||
mgr->attachEvent(SDL_KEYDOWN, &keydownEventHandler);
|
||||
mgr->attachEvent(SDL_KEYUP, &keyupEventHandler);
|
||||
mgr->attachEvent(SDL_QUIT, &quitEventHandler);
|
||||
mario->_left = false;
|
||||
mario->_right = false;
|
||||
mario->_up = false;
|
||||
mario->_down = false;
|
||||
|
||||
// Boucle de traitement
|
||||
mgr->update(); mgr->update();
|
||||
|
@ -92,14 +88,15 @@ int main(int argc, char* argv[]) {
|
|||
mgr->manageEvents(&event);
|
||||
|
||||
// Gestion de la gravite
|
||||
if( !mario->onFloor() && SDL_GetTicks() % 10 < 5 )
|
||||
mario->velocity(0, 1.0);
|
||||
// if( !mario->onFloor() && SDL_GetTicks() % 10 < 5 )
|
||||
// mario->velocity(0, 1.0);
|
||||
|
||||
|
||||
// Deplacement
|
||||
mario->moveFromVelocity();
|
||||
|
||||
|
||||
if( mgr->hit("mystery-bloc", 0, 7) ) mb.jump();
|
||||
if( mgr->hit("mystery-bloc", 0, 9) ) mb.jump();
|
||||
else mb.unjump();
|
||||
|
||||
mgr->manageFps(); // Gestion des FPS (speed)
|
||||
|
@ -179,7 +176,7 @@ void keydownEventHandler(SDL_Event *e){
|
|||
|
||||
case SDLK_DOWN:
|
||||
if( !mario->_down ){
|
||||
mario->_down = true;
|
||||
// mario->_down = true;
|
||||
// mario->velocity(0, 1.0);
|
||||
}
|
||||
break;
|
||||
|
@ -225,7 +222,7 @@ void keyupEventHandler(SDL_Event *e){
|
|||
break;
|
||||
|
||||
case SDLK_DOWN:
|
||||
mario->_down = false;
|
||||
// mario->_down = false;
|
||||
break;
|
||||
|
||||
default:
|
||||
|
|
BIN
SDL#4/main.o
|
@ -1,7 +1,7 @@
|
|||
A FAIRE
|
||||
=======
|
||||
- [ ] Gestion du saut unique ou double (limitation)
|
||||
- [ ] Erreur a corriger pour xSpriteAnimation on doit faire start() mais push() puis start() (qui est equivalent) ne marche pas
|
||||
- [ ] Optimisation de update() pas dans boucle infinie, juste dans push/pull avec limitation FPS
|
||||
- [x][ ] Gestion du saut unique ou double (limitation)
|
||||
- [ ] Gestion de l'acceleration
|
||||
- [ ] Gestion de la gravite
|
||||
|
||||
|
@ -10,10 +10,12 @@ EN COURS
|
|||
========
|
||||
- [ ] Erreur modification de frames d'une xAnimation, restent tjs les anciennes
|
||||
-> creer methode dans xAnimation
|
||||
- [ ] Gestion velocite pour deplacement
|
||||
- [...] Gestion velocite pour deplacement
|
||||
|
||||
FAIT
|
||||
====
|
||||
- [x] Gestion de la gravite
|
||||
- [x] Erreur a corriger pour xSpriteAnimation on doit faire start() mais push() puis start() (qui est equivalent) ne marche pas
|
||||
- [x] Gestion des collisions
|
||||
- [x] Index literaux pour ajouter au manager
|
||||
- [x] Auto-texture pour le bloc mystere
|
||||
|
|
|
@ -12,40 +12,43 @@ xMarioMario::xMarioMario(xManager *m, int x, int y)
|
|||
}
|
||||
){
|
||||
|
||||
_left = NULL;
|
||||
_right = NULL;
|
||||
_up = NULL;
|
||||
_down = NULL;
|
||||
_left = false;
|
||||
_right = false;
|
||||
_up = false;
|
||||
_down = false;
|
||||
_jumps = 0;
|
||||
|
||||
_gravity = 13;
|
||||
|
||||
// Constantes de velocite sur X
|
||||
_velocity[0] = 0.0;
|
||||
_mult[0] = 10;
|
||||
_dec[0] = 0.9;
|
||||
_acc[0] = 3.5;
|
||||
_acc[0] = 5;
|
||||
_min_vel[0] = 0.1;
|
||||
_max_vel[0] = 40;
|
||||
|
||||
// Constantes de velocite sur Y
|
||||
_velocity[1] = 0.0;
|
||||
_mult[1] = 20;
|
||||
_mult[1] = 50;
|
||||
_dec[1] = 1;
|
||||
_acc[1] = 1.9;
|
||||
_acc[1] = 10;
|
||||
_min_vel[1] = 0.1;
|
||||
_max_vel[1] = 50;
|
||||
_max_vel[1] = 100;
|
||||
|
||||
this->addFrame( (SDL_Rect){2, 0, 19, 29} );
|
||||
/**/// 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){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} );
|
||||
/**///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){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} );
|
||||
|
@ -62,34 +65,48 @@ xMarioMario::xMarioMario(xManager *m, int x, int y)
|
|||
=========================================================*/
|
||||
void xMarioMario::moveFromVelocity(){
|
||||
|
||||
// Gestion de touche encore enfoncee
|
||||
if( SDL_GetTicks() % 10 < 8 ){
|
||||
/* (1) Gestion de touche encore enfoncee */
|
||||
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);
|
||||
// Si touche haut
|
||||
if( _up ){
|
||||
// Si au sol et premier saut ou autre saut (pour la hauteur)
|
||||
if( this->onFloor() && _jumps == 0 || _jumps == 1 ){
|
||||
this->velocity(0, -1);
|
||||
_jumps++;
|
||||
}
|
||||
}else if( _down )
|
||||
this->velocity(0, 1);
|
||||
else
|
||||
_jumps = 0;
|
||||
|
||||
|
||||
|
||||
// Si aucune collision, on deplace
|
||||
if( !_manager->hit(_texture, _velocity[0], _velocity[1]) )
|
||||
/* (2) Si aucune collision, on deplace */
|
||||
this->move(_velocity[0], _velocity[1]);
|
||||
|
||||
// On diminue la _velocite
|
||||
|
||||
/* (3) On diminue la velocite (decceleration) */
|
||||
_velocity[0] *= ( 1 - _dec[0] );
|
||||
_velocity[1] *= ( 1 - _dec[1] );
|
||||
|
||||
// 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;
|
||||
/* (4) Gestion de la gravite */
|
||||
if( !this->onFloor() )
|
||||
this->move(0, 13);
|
||||
|
||||
// _manager->update();
|
||||
/* (5) Si velocite sous borne min */
|
||||
if( _velocity[0] < _min_vel[0] ) // sur x
|
||||
_velocity[0] = 0;
|
||||
|
||||
// usleep(10000);
|
||||
if( _velocity[1] < _min_vel[1] ) // sur y
|
||||
_velocity[1] = 0;
|
||||
|
||||
|
||||
/* (6) Gestion du temps */
|
||||
// if( _velocity[0]*_velocity[1] != 0 )
|
||||
// cout << _velocity[0] << " - " << _velocity[1] << endl;
|
||||
usleep(1);
|
||||
}
|
||||
|
||||
|
||||
|
@ -106,30 +123,32 @@ double xMarioMario::velocity(bool way){
|
|||
void xMarioMario::velocity(double x, double y){
|
||||
double last[] = { _velocity[0], _velocity[1] };
|
||||
|
||||
// Si meme sens, on accele
|
||||
if( last[0]*x > 0 )
|
||||
/* (1) Gestion velocite axe X */
|
||||
if( last[0]*x > 0 ) // Si meme sens, on accelere
|
||||
_velocity[0] *= _acc[0];
|
||||
else{ // sinon changement sens
|
||||
else{
|
||||
_velocity[0] += x * _mult[0];
|
||||
|
||||
// Gestion des frames
|
||||
if( _velocity[0] > 0 ) this->turnRight();
|
||||
else this->turnLeft();
|
||||
if( _velocity[0] > 0 ) // vers droite
|
||||
this->turnRight();
|
||||
else
|
||||
this->turnLeft();
|
||||
}
|
||||
|
||||
if( last[1]*y > 0 )
|
||||
|
||||
/* (2) Gestion velocite axe Y */
|
||||
if( last[1]*y > 0 ) // Si meme sens, on accelere
|
||||
_velocity[1] *= _acc[1];
|
||||
else // sinon changement sens
|
||||
else
|
||||
_velocity[1] += y * _mult[1];
|
||||
|
||||
|
||||
|
||||
// On retaille la velocite
|
||||
if( abs(_velocity[0]) > _max_vel[0] )
|
||||
/* (3) On borne la velocite aux max */
|
||||
if( abs(_velocity[0]) > _max_vel[0] ) // Si max x
|
||||
_velocity[0] = _max_vel[0] * (_velocity[0] / abs(_velocity[0]) );
|
||||
|
||||
if( abs(_velocity[1]) > _max_vel[1]*BLOC_SIZE )
|
||||
if( abs(_velocity[1]) > _max_vel[1] ) // Si max y
|
||||
_velocity[1] = _max_vel[1] * (_velocity[1] / abs(_velocity[1]) );
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -145,17 +164,15 @@ bool xMarioMario::onFloor(){
|
|||
=========================================================*/
|
||||
void xMarioMario::turnLeft(){
|
||||
this->stop();
|
||||
|
||||
for( int i = 0 ; i < _frames.size() ; i++ )
|
||||
_frames.erase(_frames.begin()+i);
|
||||
_frames.clear();
|
||||
|
||||
this->pull();
|
||||
this->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);
|
||||
this->push(_index);
|
||||
this->start(_timeout, _flags);
|
||||
|
||||
}
|
||||
|
||||
|
@ -165,17 +182,15 @@ void xMarioMario::turnLeft(){
|
|||
=========================================================*/
|
||||
void xMarioMario::turnRight(){
|
||||
this->stop();
|
||||
|
||||
for( int i = 0 ; i < _frames.size() ; i++ )
|
||||
_frames.erase(_frames.begin()+i);
|
||||
_frames.clear();
|
||||
|
||||
this->pull();
|
||||
this->clear();
|
||||
|
||||
this->addFrame( (SDL_Rect){238, 0, 19, 29} );
|
||||
this->addFrame( (SDL_Rect){329, 0, 19, 29} );
|
||||
|
||||
|
||||
// On ajoute au rendu
|
||||
this->start(_index, _timeout, _flags);
|
||||
this->push(_index);
|
||||
this->start(_timeout, _flags);
|
||||
|
||||
}
|
|
@ -35,10 +35,13 @@
|
|||
bool _up;
|
||||
bool _down;
|
||||
|
||||
int _jumps;
|
||||
|
||||
|
||||
private:
|
||||
Uint32 _lastmove;
|
||||
double _velocity[2];
|
||||
double _gravity;
|
||||
|
||||
double _mult[2];
|
||||
double _min_vel[2];
|
||||
|
|
|
@ -95,14 +95,14 @@ void xMarioMysteryBloc::active(bool active){
|
|||
this->addFrame( (SDL_Rect){0, 48, 16, 16} );
|
||||
|
||||
// On ajoute au rendu
|
||||
this->start(_index, _timeout, _flags);
|
||||
this->push(_index);
|
||||
|
||||
}else{
|
||||
|
||||
_frames.clear();
|
||||
// for( int i = 0 ; i < _frames.size() ; i++ )
|
||||
// _frames.erase(_frames.begin()+i);
|
||||
this->stop();
|
||||
this->pull();
|
||||
|
||||
this->addFrame( (SDL_Rect){0, 64, 16, 16} );
|
||||
this->addFrame( (SDL_Rect){0, 64, 16, 16} );
|
||||
|
|
|
@ -69,11 +69,33 @@ void xSpriteAnimation::move(SDL_Rect newpos){
|
|||
/* [MOVE] Deplacement de la position/taille du sprite
|
||||
=========================================================*/
|
||||
void xSpriteAnimation::move(int x, int y){
|
||||
/* (1) Variables utiles */
|
||||
int incrx = x;
|
||||
int incry = y;
|
||||
bool moveY = true;
|
||||
|
||||
if( !_manager->hit(_texture, x, y) ){
|
||||
_viewport.x += x;
|
||||
_viewport.y += 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 ){
|
||||
|
||||
/* (3) Si on peut aller a la destination */
|
||||
if( !_manager->hit(_texture, incrx, incry) ){
|
||||
_viewport.x += incrx;
|
||||
_viewport.y += incry;
|
||||
return;
|
||||
}
|
||||
|
||||
/* (4) Sinon, on decremente les deplacements alternativement */
|
||||
if( moveY )
|
||||
incry -= signofy;
|
||||
else
|
||||
incrx -= signofx;
|
||||
|
||||
moveY = !moveY;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -93,6 +115,18 @@ void xSpriteAnimation::addFrame(SDL_Rect clip){
|
|||
}
|
||||
|
||||
|
||||
|
||||
/* [CLEAR] Supprime toutes les frames
|
||||
=========================================================*/
|
||||
void xSpriteAnimation::clear(){
|
||||
|
||||
for( int i = 0 ; i < _frames.size() ; i++ )
|
||||
_frames.erase(_frames.begin() + i);
|
||||
|
||||
cout << "NB: " << _frames.size() << endl;
|
||||
}
|
||||
|
||||
|
||||
/* [MANAGER] Retourne le manager
|
||||
=========================================================*/
|
||||
xManager *xSpriteAnimation::manager(){ return _manager; }
|
||||
|
@ -115,6 +149,12 @@ SDL_Rect *xSpriteAnimation::viewport(){ return &_viewport; }
|
|||
void xSpriteAnimation::push(string index){
|
||||
_index = index;
|
||||
|
||||
// Gestion erreur si aucune frame
|
||||
if( _frames.size() == 0 )
|
||||
return;
|
||||
|
||||
_frame = _frames[0];
|
||||
|
||||
/* (1) On ajoute le sprite au rendu */
|
||||
_manager->push(index, _texture, &_frame, &_viewport);
|
||||
}
|
||||
|
@ -190,13 +230,10 @@ void xSpriteAnimationProcess(xSpriteAnimation *xSA, int t, int flags){
|
|||
|
||||
/* [START] Ajoute l'animation au rendu
|
||||
=========================================================*/
|
||||
void xSpriteAnimation::start(string index, int t, int flags){
|
||||
_index = index;
|
||||
void xSpriteAnimation::start(int t, int flags){
|
||||
_timeout = t;
|
||||
_flags = flags;
|
||||
|
||||
this->push(index);
|
||||
|
||||
/* (1) On lance l'animation */
|
||||
_animation = new thread(xSpriteAnimationProcess, this, t, flags);
|
||||
|
||||
|
@ -209,8 +246,6 @@ void xSpriteAnimation::start(string index, int t, int flags){
|
|||
/* [STOP] Arrete l'animation
|
||||
=========================================================*/
|
||||
void xSpriteAnimation::stop(){
|
||||
this->pull();
|
||||
|
||||
/* (1) On arrete l'animation */
|
||||
delete _animation;
|
||||
}
|
|
@ -13,6 +13,7 @@
|
|||
void move(int x, int y); // Deplace l'animation
|
||||
|
||||
void addFrame(SDL_Rect clip); // Ajoute une frame en fonction des coordonnees
|
||||
void clear(); // Supprime les frames
|
||||
|
||||
// GETTER
|
||||
xManager *manager();
|
||||
|
@ -24,7 +25,7 @@
|
|||
void pull(); // Retrait du rendu
|
||||
|
||||
// Gestion de l'animation
|
||||
void start(string index, int t, int flags=SPRITE_ANIM_ONCE);
|
||||
void start(int t, int flags=SPRITE_ANIM_ONCE);
|
||||
void stop();
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,232 @@
|
|||
#include "main.h"
|
||||
|
||||
// On cree un sdl-toplevel statique
|
||||
static xManager *mgr = NULL;
|
||||
static bool running = true;
|
||||
|
||||
static xMarioMario *mario = NULL;
|
||||
|
||||
static bool left_move = false;
|
||||
static bool right_move = false;
|
||||
static bool up_move = false;
|
||||
static bool down_move = false;
|
||||
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
srand(time(0));
|
||||
|
||||
/* [0] Initialisation du manager + Creation de la fenetre
|
||||
=========================================================*/
|
||||
mgr = new xManager("Ma fenetre SDL", BLOC_WIDTH*BLOC_SIZE, BLOC_HEIGHT*BLOC_SIZE);
|
||||
|
||||
// Gestion erreur
|
||||
if( !mgr->status() ){
|
||||
cout << "[INIT] -> " << SDL_GetError() << endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Couleur de fond
|
||||
mgr->setBackground(0, 96, 183);
|
||||
mgr->setImage("src/bg1.png");
|
||||
|
||||
/* [2] On definit le terrain
|
||||
=========================================================*/
|
||||
// On cree un bout du terrain
|
||||
xMarioGrass btmleft(mgr, (SDL_Rect){-1, 20-2, 10, 3} );
|
||||
btmleft.push("bottom-left");
|
||||
|
||||
// On cree un bout du terrain
|
||||
xMarioGrass btmcenter(mgr, (SDL_Rect){13, 20-3, 10, 3} );
|
||||
btmcenter.push("bottom-center");
|
||||
|
||||
|
||||
xMarioGrass floattcenter(mgr, (SDL_Rect){10, 10, 5, 5} );
|
||||
floattcenter.push("float-center");
|
||||
|
||||
|
||||
|
||||
/* [3] Gestion des animations (blocs animes)
|
||||
=========================================================*/
|
||||
// On cree une coquille verte
|
||||
// xMarioGreenShell gs(mgr, 5, 20-3);
|
||||
// gs.start("green-sheel", 100, SPRITE_ANIM_INFINITE);
|
||||
|
||||
// On cree un bloc mystere
|
||||
xMarioMysteryBloc mb(mgr, 5, 20-5);
|
||||
mb.push("mystery-bloc");
|
||||
mb.start(150, SPRITE_ANIM_INFINITE);
|
||||
|
||||
// On cree un bloc normal
|
||||
// xMarioBloc bl(mgr, (SDL_Rect){0, 20-2, 10, 3});
|
||||
// bl.push("bloc-bottom-left");
|
||||
|
||||
// On cree mario
|
||||
mario = new xMarioMario(mgr, 5, 20-3);
|
||||
mario->push("mario");
|
||||
// mario->start("mario", 100, SPRITE_ANIM_INFINITE);
|
||||
|
||||
|
||||
|
||||
|
||||
/* [n-1] Boucle infinie
|
||||
=========================================================*/
|
||||
|
||||
// Gestion des evenements
|
||||
SDL_Event event;
|
||||
mgr->attachEvent(SDL_KEYDOWN, &keydownEventHandler);
|
||||
mgr->attachEvent(SDL_KEYUP, &keyupEventHandler);
|
||||
mgr->attachEvent(SDL_QUIT, &quitEventHandler);
|
||||
|
||||
// Boucle de traitement
|
||||
mgr->update(); mgr->update();
|
||||
mgr->manageFps(FPS);
|
||||
while(running){
|
||||
|
||||
|
||||
// Gestion des evenements
|
||||
while( SDL_PollEvent(&event) != 0 )
|
||||
mgr->manageEvents(&event);
|
||||
|
||||
// Gestion de la gravite
|
||||
// if( !mario->onFloor() && SDL_GetTicks() % 10 < 5 )
|
||||
// mario->velocity(0, 1.0);
|
||||
|
||||
// Deplacement
|
||||
mario->moveFromVelocity();
|
||||
|
||||
|
||||
if( mgr->hit("mystery-bloc", 0, 9) ) mb.jump();
|
||||
else mb.unjump();
|
||||
|
||||
mgr->manageFps(); // Gestion des FPS (speed)
|
||||
mgr->update(); // Mise a jour du rendu
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* [n] Fin d'execution
|
||||
=========================================================*/
|
||||
delete mgr;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* GESTION DE QUAND ON QUITTE LA FENETRE
|
||||
*
|
||||
* @e<SDL_Event*> 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<SDL_Event*> Pointeur sur l'evenement appelant
|
||||
*
|
||||
*/
|
||||
void keydownEventHandler(SDL_Event *e){
|
||||
|
||||
switch( (*e).key.keysym.sym ){
|
||||
case SDLK_UP:
|
||||
if( !mario->_up ){
|
||||
mario->_up = true;
|
||||
// mario->velocity(0, -40.0);
|
||||
}
|
||||
break;
|
||||
|
||||
case SDLK_LEFT:
|
||||
if( !mario->_left ){
|
||||
mario->_left = true;
|
||||
// mario->velocity(-15.0, 0);
|
||||
}
|
||||
break;
|
||||
|
||||
case SDLK_RIGHT:
|
||||
if( !mario->_right ){
|
||||
mario->_right = true;
|
||||
// mario->velocity(15.0, 0);
|
||||
}
|
||||
break;
|
||||
|
||||
case SDLK_DOWN:
|
||||
if( !mario->_down ){
|
||||
// mario->_down = true;
|
||||
// mario->velocity(0, 1.0);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
cout << "UNUSED KEY" << endl;
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* GESTION DE QUAND ON APPUIE SUR FLECHE BAS
|
||||
*
|
||||
* @e<SDL_Event*> Pointeur sur l'evenement appelant
|
||||
*
|
||||
*/
|
||||
void keyupEventHandler(SDL_Event *e){
|
||||
SDL_Rect *mRect = mario->viewport();
|
||||
|
||||
|
||||
switch( (*e).key.keysym.sym ){
|
||||
case SDLK_UP:
|
||||
mario->_up = false;
|
||||
break;
|
||||
|
||||
case SDLK_LEFT:
|
||||
mario->_left = false;
|
||||
break;
|
||||
|
||||
case SDLK_RIGHT:
|
||||
mario->_right = false;
|
||||
break;
|
||||
|
||||
case SDLK_DOWN:
|
||||
// mario->_down = false;
|
||||
break;
|
||||
|
||||
default:
|
||||
cout << "PRESSED" << endl;
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
#ifndef DEF_MAIN_H
|
||||
|
||||
#define DEF_MAIN_H
|
||||
|
||||
/* [LIB] Internes
|
||||
=========================================================*/
|
||||
#include <iostream>
|
||||
#include <ctime>
|
||||
#include <thread>
|
||||
|
||||
/* [LIB] Externes
|
||||
=========================================================*/
|
||||
#include "xSDL.h" // Librairie perso
|
||||
#include "xMario.h" // Elements utiles au jeu
|
||||
|
||||
/* [NS] Namespace
|
||||
=========================================================*/
|
||||
using namespace std;
|
||||
|
||||
/* [CONST] Constantes et enumerations
|
||||
=========================================================*/
|
||||
#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);
|
||||
void keyupEventHandler(SDL_Event *e);
|
||||
|
||||
#endif
|
|
@ -0,0 +1,41 @@
|
|||
.PHONY: init, clean, mrproper
|
||||
CC=g++
|
||||
FLAGS=-pthread -std=c++11 `pkg-config sdl2 --cflags --libs` -l SDL2_image
|
||||
|
||||
# INIT > STRUCTURE DE FICHIERS POUR LES EXECUTABLES
|
||||
init: clean
|
||||
mkdir dep.o
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# EXECUTABLE > DEPENDANCES DE L'EXECUTABLE
|
||||
all: init main.o clean
|
||||
rm -r dep.o
|
||||
$(CC) main.o -o exe $(FLAGS)
|
||||
|
||||
# AMORCE > PROGRAMME PRINCIPAL
|
||||
main.o: main.cpp
|
||||
$(CC) -c $< -o main.o $(FLAGS)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# RESET > SUPPRESSION DES FICHIERS
|
||||
clean:
|
||||
touch init.o
|
||||
rm -r *.o
|
||||
|
||||
# RESET FOR REBUILD > SUPPRESSION DE L'EXECUTABLE
|
||||
mrproper:
|
||||
rm exe
|
After Width: | Height: | Size: 8.5 KiB |
After Width: | Height: | Size: 400 KiB |
After Width: | Height: | Size: 192 KiB |
After Width: | Height: | Size: 140 KiB |
After Width: | Height: | Size: 2.0 MiB |
After Width: | Height: | Size: 18 KiB |
After Width: | Height: | Size: 120 KiB |
After Width: | Height: | Size: 55 KiB |
After Width: | Height: | Size: 2.1 KiB |
|
@ -0,0 +1,22 @@
|
|||
A FAIRE
|
||||
=======
|
||||
- [ ] Optimisation de update() pas dans boucle infinie, juste dans push/pull avec limitation FPS
|
||||
- [ ] Gestion du saut unique ou double (limitation)
|
||||
- [ ] Erreur a corriger pour xSpriteAnimation on doit faire start() mais push() puis start() (qui est equivalent) ne marche pas
|
||||
- [ ] Gestion de l'acceleration
|
||||
- [ ] 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
|
||||
====
|
||||
- [x] Gestion des collisions
|
||||
- [x] Index literaux pour ajouter au manager
|
||||
- [x] Auto-texture pour le bloc mystere
|
||||
- [x] Auto-texture pour le mario (avec mvmts)
|
||||
- [x] Auto-texture pour le sol (grass)
|
|
@ -0,0 +1,31 @@
|
|||
#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/xMarioMario.h"
|
||||
#include "xMario/xMarioGrass.h"
|
||||
#include "xMario/xMarioGreenShell.h"
|
||||
#include "xMario/xMarioMysteryBloc.h"
|
||||
#include "xMario/xMarioBloc.h"
|
||||
|
||||
|
||||
/* [BODIES] Inclusion des .cpp des sous-libs
|
||||
=========================================================*/
|
||||
#include "xMario/xMarioMario.cpp"
|
||||
#include "xMario/xMarioGrass.cpp"
|
||||
#include "xMario/xMarioGreenShell.cpp"
|
||||
#include "xMario/xMarioMysteryBloc.cpp"
|
||||
#include "xMario/xMarioBloc.cpp"
|
||||
|
||||
#endif
|
|
@ -0,0 +1,36 @@
|
|||
/* [CONSTRUCTOR] Construction d'un xMarioBloc
|
||||
=========================================================*/
|
||||
xMarioBloc::xMarioBloc(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/blocs.png");
|
||||
|
||||
|
||||
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) );
|
||||
|
||||
this->get(index)->dimensions(
|
||||
(SDL_Rect){BLOC_SIZE*x, BLOC_SIZE*y, BLOC_SIZE, BLOC_SIZE},
|
||||
(SDL_Rect){0, 153, 16, 16}
|
||||
);
|
||||
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
#ifndef DEF_XMARIOBLOC_H
|
||||
|
||||
#define DEF_XMARIOBLOC_H
|
||||
|
||||
/* [DEF] Definition de la classe
|
||||
=========================================================*/
|
||||
class xMarioBloc : public xSpriteGroup{
|
||||
|
||||
public:
|
||||
xMarioBloc(xManager *m, SDL_Rect rect);
|
||||
|
||||
private:
|
||||
xManager *_manager;
|
||||
SDL_Texture *_spritesheet; // Contiendra la texture
|
||||
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,104 @@
|
|||
/* [CONSTRUCTOR] Construction d'un xMarioGrass
|
||||
=========================================================*/
|
||||
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/ground.png");
|
||||
|
||||
|
||||
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){137, 99, 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){171, 99, 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){137, 133, 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){171, 133, 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){137, 116, 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){171, 116, 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){154, 99, 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){137, 184, 16, 16}
|
||||
);
|
||||
}
|
||||
|
||||
// INSIDE
|
||||
else{
|
||||
this->get(index)->dimensions(
|
||||
(SDL_Rect){BLOC_SIZE*x, BLOC_SIZE*y, BLOC_SIZE, BLOC_SIZE},
|
||||
(SDL_Rect){137, 167, 16, 16}
|
||||
);
|
||||
}
|
||||
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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
|
|
@ -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} );
|
||||
}
|
|
@ -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
|
|
@ -0,0 +1,192 @@
|
|||
/* [CONSTRUCTOR] Construction d'un xMarioMario
|
||||
=========================================================*/
|
||||
xMarioMario::xMarioMario(xManager *m, int x, int y)
|
||||
: xSpriteAnimation(
|
||||
m,
|
||||
"src/mario.png",
|
||||
(SDL_Rect){
|
||||
(int) (BLOC_SIZE*x+BLOC_SIZE*.1),
|
||||
BLOC_SIZE*y,
|
||||
(int) (BLOC_SIZE*.8),
|
||||
BLOC_SIZE
|
||||
}
|
||||
){
|
||||
|
||||
_left = false;
|
||||
_right = false;
|
||||
_up = false;
|
||||
_down = false;
|
||||
_jumps = 0;
|
||||
|
||||
_gravity = 13;
|
||||
|
||||
// Constantes de velocite sur X
|
||||
_velocity[0] = 0.0;
|
||||
_mult[0] = 10;
|
||||
_dec[0] = 0.9;
|
||||
_acc[0] = 2;
|
||||
_min_vel[0] = 0.1;
|
||||
_max_vel[0] = 40;
|
||||
|
||||
// Constantes de velocite sur Y
|
||||
_velocity[1] = 0.0;
|
||||
_mult[1] = 40;
|
||||
_dec[1] = 1;
|
||||
_acc[1] = 10;
|
||||
_min_vel[1] = 0.1;
|
||||
_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} );
|
||||
// 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} );
|
||||
|
||||
// /* (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(){
|
||||
|
||||
/* (1) Gestion de touche encore enfoncee */
|
||||
if( _left ) this->velocity(-1, 0);
|
||||
else if( _right ) this->velocity(1, 0);
|
||||
|
||||
if( _up ){
|
||||
if( _jumps < 3 ){
|
||||
this->velocity(0, -1);
|
||||
_jumps++;
|
||||
}
|
||||
}else if( _down )
|
||||
this->velocity(0, 1);
|
||||
else
|
||||
_jumps = 0;
|
||||
|
||||
|
||||
/* (2) Si aucune collision, on deplace */
|
||||
if( !_manager->hit(_texture, _velocity[0], _velocity[1]) )
|
||||
this->move(_velocity[0], _velocity[1]);
|
||||
|
||||
|
||||
/* (3) On diminue la velocite (decceleration) */
|
||||
_velocity[0] *= ( 1 - _dec[0] );
|
||||
_velocity[1] *= ( 1 - _dec[1] );
|
||||
|
||||
|
||||
/* (4) Gestion de la gravite */
|
||||
if( !this->onFloor() )
|
||||
_velocity[1] = 13;
|
||||
|
||||
/* (5) Si velocite sous borne min */
|
||||
if( _velocity[0] < _min_vel[0] ) // sur x
|
||||
_velocity[0] = 0;
|
||||
|
||||
if( _velocity[1] < _min_vel[1] ) // sur y
|
||||
_velocity[1] = 0;
|
||||
|
||||
|
||||
/* (6) Gestion du temps */
|
||||
cout << _velocity[0] << " - " << _velocity[1] << endl;
|
||||
usleep(1);
|
||||
}
|
||||
|
||||
|
||||
/* [VELOCITY] Retourne un pointeur sur la velocite
|
||||
=========================================================*/
|
||||
double xMarioMario::velocity(bool way){
|
||||
// (way) ? HORIZONTAL : VERTICAL
|
||||
return (way) ? _velocity[0] : _velocity[1];
|
||||
}
|
||||
|
||||
|
||||
/* [VELOCITY] Modifie la velocite
|
||||
=========================================================*/
|
||||
void xMarioMario::velocity(double x, double y){
|
||||
double last[] = { _velocity[0], _velocity[1] };
|
||||
|
||||
/* (1) Gestion velocite axe X */
|
||||
if( last[0]*x > 0 ) // Si meme sens, on accelere
|
||||
_velocity[0] *= _acc[0];
|
||||
else
|
||||
_velocity[0] += x * _mult[0];
|
||||
|
||||
|
||||
/* (2) Gestion velocite axe Y */
|
||||
if( last[1]*y > 0 ) // Si meme sens, on accelere
|
||||
_velocity[1] *= _acc[1];
|
||||
else
|
||||
_velocity[1] += y * _mult[1];
|
||||
|
||||
/* (3) On borne la velocite aux max */
|
||||
if( abs(_velocity[0]) > _max_vel[0] ) // Si max x
|
||||
_velocity[0] = _max_vel[0] * (_velocity[0] / abs(_velocity[0]) );
|
||||
|
||||
if( abs(_velocity[1]) > _max_vel[1] ) // Si max y
|
||||
_velocity[1] = _max_vel[1] * (_velocity[1] / abs(_velocity[1]) );
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
bool xMarioMario::onFloor(){
|
||||
return _manager->hit(_texture, 0, 1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* [TURNLEFT] Charge le sprite vers la gauche
|
||||
=========================================================*/
|
||||
void xMarioMario::turnLeft(){
|
||||
this->stop();
|
||||
|
||||
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(_timeout, _flags);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* [TURNRIGHT] Charge le sprite vers la droite
|
||||
=========================================================*/
|
||||
void xMarioMario::turnRight(){
|
||||
this->stop();
|
||||
|
||||
for( int i = 0 ; i < _frames.size() ; i++ )
|
||||
_frames.erase(_frames.begin()+i);
|
||||
_frames.clear();
|
||||
|
||||
|
||||
this->addFrame( (SDL_Rect){238, 0, 19, 29} );
|
||||
this->addFrame( (SDL_Rect){329, 0, 19, 29} );
|
||||
|
||||
|
||||
// On ajoute au rendu
|
||||
this->start(_timeout, _flags);
|
||||
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
#ifndef 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{
|
||||
|
||||
public:
|
||||
xMarioMario(xManager *manager, int x, int y); // Spritesheet avec taille de chaque sprite
|
||||
|
||||
void moveFromVelocity();
|
||||
|
||||
// Changement de frames
|
||||
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;
|
||||
|
||||
int _jumps;
|
||||
|
||||
|
||||
private:
|
||||
Uint32 _lastmove;
|
||||
double _velocity[2];
|
||||
double _gravity;
|
||||
|
||||
double _mult[2];
|
||||
double _min_vel[2];
|
||||
double _max_vel[2];
|
||||
double _acc[2];
|
||||
double _dec[2];
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,118 @@
|
|||
/* [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} );
|
||||
|
||||
_defaultrect = _viewport;
|
||||
_lastjump = SDL_GetTicks();
|
||||
_jumps = 0;
|
||||
_active = true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* [JUMP] Animation d'activation (quand saut)
|
||||
=========================================================*/
|
||||
void xMarioMysteryBloc::jump(){
|
||||
if( !this->active() )
|
||||
return;
|
||||
|
||||
if( _defaultrect.y == _viewport.y && SDL_GetTicks()-_lastjump > 300 ){
|
||||
|
||||
_viewport.x = _defaultrect.x - _defaultrect.w*.1/2;
|
||||
_viewport.y = _defaultrect.y-10 - _defaultrect.h*.1/2;
|
||||
_viewport.w = _defaultrect.w * 1.1;
|
||||
_viewport.h = _defaultrect.h * 1.1;
|
||||
|
||||
this->move(_viewport);
|
||||
|
||||
_lastjump = SDL_GetTicks();
|
||||
|
||||
_jumps++;
|
||||
|
||||
if( _jumps >= 6 )
|
||||
this->active(false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* [UNJUMP] Animation d'activation (quand saut)
|
||||
=========================================================*/
|
||||
void xMarioMysteryBloc::unjump(){
|
||||
if( _defaultrect.y != _viewport.y && SDL_GetTicks()-_lastjump > 100 ){
|
||||
|
||||
_viewport.x = _defaultrect.x;
|
||||
_viewport.y = _defaultrect.y;
|
||||
_viewport.w = _defaultrect.w;
|
||||
_viewport.h = _defaultrect.h;
|
||||
|
||||
this->move(_viewport);
|
||||
|
||||
_lastjump = SDL_GetTicks();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* [ACTIVE] Retourne l'etat du bouton
|
||||
=========================================================*/
|
||||
bool xMarioMysteryBloc::active(){
|
||||
return _active;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* [ACTIVE] Gestion du caractere "actif" du bouton
|
||||
=========================================================*/
|
||||
void xMarioMysteryBloc::active(bool active){
|
||||
_active = active;
|
||||
|
||||
|
||||
if( active ){
|
||||
|
||||
|
||||
_frames.clear();
|
||||
// for( int i = 0 ; i < _frames.size() ; i++ )
|
||||
// _frames.erase(_frames.begin()+i);
|
||||
this->pull();
|
||||
|
||||
|
||||
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} );
|
||||
|
||||
// On ajoute au rendu
|
||||
this->start(_timeout, _flags);
|
||||
|
||||
}else{
|
||||
|
||||
_frames.clear();
|
||||
// for( int i = 0 ; i < _frames.size() ; 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} );
|
||||
|
||||
// On ajoute au rendu
|
||||
this->push(_index);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
#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
|
||||
|
||||
void jump(); // Effectue l'animation d'activation
|
||||
void unjump(); // Effectue l'animation d'activation
|
||||
|
||||
bool active(); // Retourne si le bloc est actif ou non
|
||||
void active(bool active); // Active ou non le bloc
|
||||
|
||||
private:
|
||||
bool _active;
|
||||
int _jumps;
|
||||
Uint32 _lastjump;
|
||||
SDL_Rect _defaultrect;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,40 @@
|
|||
#ifndef DEF_XSDL_H
|
||||
|
||||
#define DEF_XSDL_H
|
||||
|
||||
/* [LIBS] Internes
|
||||
=========================================================*/
|
||||
#include "SDL.h"
|
||||
#include "SDL_image.h"
|
||||
#include <vector>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <cmath>
|
||||
#include <unistd.h>
|
||||
|
||||
/* [DEF] Definitions basiques
|
||||
=========================================================*/
|
||||
#define SPRITE_ANIM_ONCE 0x1
|
||||
#define SPRITE_ANIM_INFINITE 0x10
|
||||
#define SPRITE_ANIM_REVERSE 0x100
|
||||
|
||||
/* [NS] Namespace
|
||||
=========================================================*/
|
||||
using namespace std;
|
||||
|
||||
/* [HEADERS] Inclusion des .h des sous-libs
|
||||
=========================================================*/
|
||||
#include "xSDL/xManager.h"
|
||||
#include "xSDL/xSprite.h"
|
||||
#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
|
|
@ -0,0 +1,413 @@
|
|||
/* [CONSTRUCTOR] Constructeur de la classe
|
||||
=========================================================*/
|
||||
xManager::xManager(const char *t, int w, int h){
|
||||
// default values
|
||||
_lasttick = SDL_GetTicks();
|
||||
_fpstime = 1000/60;
|
||||
_window = NULL;
|
||||
_renderer = NULL;
|
||||
_texture = NULL;
|
||||
|
||||
// Initialisation des sous-sys. SDL
|
||||
SDL_Init( SDL_INIT_EVERYTHING );
|
||||
|
||||
|
||||
// Creation de la fenetre
|
||||
_window = SDL_CreateWindow(
|
||||
t,
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
w,
|
||||
h,
|
||||
SDL_WINDOW_SHOWN
|
||||
);
|
||||
|
||||
// Gestion erreur
|
||||
if( _window == NULL )
|
||||
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,
|
||||
-1,
|
||||
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
/* [DESTROYER] Destructeur de la classe
|
||||
=========================================================*/
|
||||
xManager::~xManager(){
|
||||
SDL_DestroyTexture(_texture);
|
||||
SDL_DestroyRenderer(_renderer);
|
||||
SDL_DestroyWindow( _window );
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
/* [STATUS] Retourne le status
|
||||
=========================================================*/
|
||||
bool xManager::status(){
|
||||
return _window != NULL && _renderer != NULL;
|
||||
}
|
||||
|
||||
|
||||
/* [WINDOW] Retourne la fenetre
|
||||
=========================================================*/
|
||||
SDL_Window* xManager::window(){ return _window; }
|
||||
|
||||
|
||||
/* [SCREEN] Retourne la fenetre
|
||||
=========================================================*/
|
||||
SDL_Renderer* xManager::renderer(){ return _renderer; }
|
||||
|
||||
|
||||
/* [SETBACKGROUND] Modifie la couleur de fond
|
||||
=========================================================*/
|
||||
bool xManager::setBackground(Uint8 r, Uint8 g, Uint8 b, Uint8 a){
|
||||
|
||||
SDL_SetRenderDrawColor( _renderer, r, g, b, a );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/* [SETIMAGE] Met une image en fond
|
||||
=========================================================*/
|
||||
bool xManager::setImage(const char *url){
|
||||
|
||||
// On cree la texture associee
|
||||
_texture = IMG_LoadTexture( _renderer, url );
|
||||
|
||||
return _texture != NULL;
|
||||
}
|
||||
|
||||
|
||||
/* [HIT] Retourne si une texture est en collision avec une autre
|
||||
=========================================================*/
|
||||
bool xManager::hit(string current, int movex, int movey){
|
||||
// Anti conflit inter-thread
|
||||
_mutex_hit.lock();
|
||||
|
||||
/* (1) On recupere le SDL_Rect destination du sprite courant */
|
||||
SDL_Rect *cRect = this->getDst(current);
|
||||
|
||||
// Gestion erreur
|
||||
if( cRect == NULL ){
|
||||
_mutex_hit.unlock();
|
||||
return false;
|
||||
}
|
||||
|
||||
SDL_Rect r = (SDL_Rect){
|
||||
(*cRect).x+movex,
|
||||
(*cRect).y+movey,
|
||||
(*cRect).w,
|
||||
(*cRect).h
|
||||
};
|
||||
SDL_Rect c;
|
||||
|
||||
/* (2) On regarde si en dehors de la fenetre */
|
||||
for( int y = r.y ; y < r.y+r.h ; y++ )
|
||||
for( int x = r.x ; x < r.x+r.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 < _indexes.size() ; i++ ){
|
||||
|
||||
// Si c'est pas le sprite courant
|
||||
if( _indexes[i] != current ){
|
||||
|
||||
// taille du sprite en cours
|
||||
c.x = (*_dst[i]).x;
|
||||
c.y = (*_dst[i]).y;
|
||||
c.w = (*_dst[i]).w;
|
||||
c.h = (*_dst[i]).h;
|
||||
|
||||
for( int y = r.y ; y < r.y+r.h ; y++ )
|
||||
for( int x = r.x ; x < r.x+r.w ; x++ )
|
||||
if( x>=c.x && x<=c.x+c.w && y>=c.y && y<=c.y+c.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++ )
|
||||
if( _sprites[i] == current )
|
||||
xIndex = i;
|
||||
|
||||
if( xIndex == -1 ){
|
||||
_mutex_hit.unlock();
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
SDL_Rect r = (SDL_Rect){
|
||||
(*_dst[xIndex]).x+movex,
|
||||
(*_dst[xIndex]).y+movey,
|
||||
(*_dst[xIndex]).w,
|
||||
(*_dst[xIndex]).h
|
||||
};
|
||||
SDL_Rect c;
|
||||
|
||||
/* (2) On regarde si en dehors de la fenetre */
|
||||
for( int y = r.y ; y < r.y+r.h ; y++ )
|
||||
for( int x = r.x ; x < r.x+r.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
|
||||
c.x = (*_dst[i]).x;
|
||||
c.y = (*_dst[i]).y;
|
||||
c.w = (*_dst[i]).w;
|
||||
c.h = (*_dst[i]).h;
|
||||
|
||||
for( int y = r.y ; y < r.y+r.h ; y++ )
|
||||
for( int x = r.x ; x < r.x+r.w ; x++ )
|
||||
if( x>=c.x && x<=c.x+c.w && y>=c.y && y<=c.y+c.h ){
|
||||
// On debloque la ressource
|
||||
_mutex_hit.unlock();
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// On debloque la ressource
|
||||
_mutex_hit.unlock();
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/* [GETTEXTURE] Renvoie la texture
|
||||
=========================================================*/
|
||||
SDL_Texture *xManager::getTexture(string index){
|
||||
|
||||
// On cherche la texture avec l'index
|
||||
for( int i = 0 ; i < _indexes.size() ; i++ )
|
||||
if( _indexes[i] == index )
|
||||
return _sprites[i];
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
/* [GETSRC] Renvoie le SDL_Rect source
|
||||
=========================================================*/
|
||||
SDL_Rect *xManager::getSrc(string index){
|
||||
|
||||
// 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){
|
||||
|
||||
// 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){
|
||||
// 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 );
|
||||
|
||||
// On debloque l'acces
|
||||
_mutex_push.unlock();
|
||||
}
|
||||
|
||||
|
||||
/* [PULL] Retire une texture du rendu principal
|
||||
=========================================================*/
|
||||
void xManager::pull(string index){
|
||||
// On bloque l'acces inter-thread
|
||||
_mutex_pull.lock();
|
||||
|
||||
// On cherche l'indice de la texture
|
||||
int xIndex = -1;
|
||||
|
||||
for( int i = 0 ; i < _indexes.size() ; i++ )
|
||||
if( _indexes[i] == index ) xIndex = i;
|
||||
|
||||
// Si on a rien trouve
|
||||
if( xIndex == -1 )
|
||||
return;
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
|
||||
/* [PULL] Retire une texture du rendu principal
|
||||
=========================================================*/
|
||||
void xManager::pull(SDL_Texture *t){
|
||||
// On bloque l'acces inter-thread
|
||||
_mutex_pull.lock();
|
||||
|
||||
// On cherche l'indice de la texture
|
||||
int xIndex = -1;
|
||||
|
||||
for( int i = 0 ; i < _sprites.size() ; i++ )
|
||||
if( _sprites[i] == t ) xIndex = i;
|
||||
|
||||
// Si on a rien trouve
|
||||
if( xIndex == -1 )
|
||||
return;
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* [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_update.lock();
|
||||
|
||||
/* (1) On efface le rendu */
|
||||
SDL_RenderClear(_renderer);
|
||||
|
||||
|
||||
/* (2) On applique la couleur de fond */
|
||||
SDL_RenderDrawRect(_renderer, &_winrect);
|
||||
|
||||
|
||||
/* (3) On ajoute le rendu principal (si existe) */
|
||||
if( _texture != NULL)
|
||||
SDL_RenderCopy(_renderer, _texture, NULL, NULL);
|
||||
|
||||
|
||||
/* (4) On ajoute toutes les textures pushees */
|
||||
for( int i = 0 ; i < _sprites.size() ; i++ )
|
||||
SDL_RenderCopy(_renderer, _sprites[i], _src[i], _dst[i]);
|
||||
|
||||
|
||||
/* (n) On affiche le resultat */
|
||||
SDL_RenderPresent(_renderer);
|
||||
|
||||
// On debloque l'acces
|
||||
_mutex_update.unlock();
|
||||
}
|
||||
|
||||
/* [ATTACHEVENT] Ajoute une fonction a un type d'evenement
|
||||
=========================================================*/
|
||||
void xManager::attachEvent(SDL_EventType t, void(*handler)(SDL_Event*)){
|
||||
|
||||
// On attache le type d'evenement a la fonction
|
||||
_events.push_back( t );
|
||||
_handlers.push_back( handler );
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/* [MANAGEEVENTS] Gestion des evenements
|
||||
=========================================================*/
|
||||
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
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
#ifndef DEF_XMANAGER_H
|
||||
|
||||
#define DEF_XMANAGER_H
|
||||
class xManager{
|
||||
|
||||
public:
|
||||
xManager(const char *t, int w, int h);
|
||||
~xManager();
|
||||
SDL_Window *window();
|
||||
SDL_Renderer *renderer();
|
||||
bool status();
|
||||
bool setBackground(Uint8 r=0xff, Uint8 g=0xff, Uint8 b=0xff, Uint8 a=0xff);
|
||||
bool setImage(const char *url);
|
||||
|
||||
bool hit(string current, int movex=0, int movey=0); // Gestion des collisions
|
||||
bool hit(SDL_Texture *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);
|
||||
|
||||
void push(string index, SDL_Texture *t, SDL_Rect *origin, SDL_Rect *dest);
|
||||
void pull(string index);
|
||||
void pull(SDL_Texture *t);
|
||||
|
||||
void update();
|
||||
|
||||
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<SDL_EventType> _events;
|
||||
vector<void(*)(SDL_Event*)> _handlers;
|
||||
|
||||
// status de l'initialisation
|
||||
bool _status;
|
||||
|
||||
// Elements utiles
|
||||
SDL_Window *_window;
|
||||
SDL_Rect _winrect;
|
||||
|
||||
SDL_Renderer *_renderer;
|
||||
SDL_Texture *_texture;
|
||||
|
||||
// Gestion des textures
|
||||
vector<string> _indexes;
|
||||
vector<SDL_Texture*> _sprites;
|
||||
vector<SDL_Rect*> _src;
|
||||
vector<SDL_Rect*> _dst;
|
||||
|
||||
// Protection thread-safe
|
||||
mutex _mutex_push;
|
||||
mutex _mutex_pull;
|
||||
mutex _mutex_update;
|
||||
mutex _mutex_hit;
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,144 @@
|
|||
/* [DESTRUCTOR] Destruction de la surface
|
||||
=========================================================*/
|
||||
xSprite::~xSprite(){
|
||||
SDL_DestroyTexture( _texture );
|
||||
_manager = NULL;
|
||||
}
|
||||
|
||||
|
||||
/* [CONSTRUCTOR] Construction de la surface vide
|
||||
=========================================================*/
|
||||
xSprite::xSprite(xManager *m){
|
||||
_manager = m;
|
||||
_texture = NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* [CONSTRUCTOR] Construction de la surface avec couleur
|
||||
=========================================================*/
|
||||
xSprite::xSprite(xManager *m, const int rgb[]){
|
||||
_manager = m;
|
||||
_texture = NULL;
|
||||
|
||||
SDL_Surface *surf = SDL_CreateRGBSurface(0, 0, 0, 32, 0, 0, 0, rgb[3]);
|
||||
|
||||
// On recupere la couleur
|
||||
Uint32 color = SDL_MapRGBA( surf->format, rgb[0], rgb[1], rgb[2], rgb[3]);
|
||||
|
||||
|
||||
// On cree la texture a partir de la surface
|
||||
_texture = SDL_CreateTextureFromSurface(_manager->renderer(), surf);
|
||||
|
||||
|
||||
// On libere la surface inutile maintenant
|
||||
SDL_FreeSurface( surf );
|
||||
surf = NULL;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* [CONSTRUCTOR] Construction de la surface avec image
|
||||
=========================================================*/
|
||||
xSprite::xSprite(xManager *m, const char *url){
|
||||
|
||||
_manager = m;
|
||||
_texture = NULL;
|
||||
|
||||
/* (1) On cree la texture directement */
|
||||
_texture = IMG_LoadTexture(_manager->renderer(), url);
|
||||
|
||||
// Gestion erreur
|
||||
if( _texture == NULL )
|
||||
return;
|
||||
}
|
||||
|
||||
/* [CONSTRUCTOR] Constructions avec copie de texture
|
||||
=========================================================*/
|
||||
xSprite::xSprite(xManager *m, SDL_Texture *t){
|
||||
_manager = m;
|
||||
_texture = t;
|
||||
|
||||
// Gestion erreur
|
||||
if( _texture == NULL )
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/* [DIMENSIONS] Definition des dimensions par defaut
|
||||
=========================================================*/
|
||||
void xSprite::dimensions(){
|
||||
/* (1) On recupere les informations de la texture */
|
||||
int w, h;
|
||||
SDL_QueryTexture(_texture, NULL, NULL, &w, &h);
|
||||
|
||||
|
||||
/* (2) On definit les dimensions par defaut */
|
||||
_dst = (SDL_Rect){0, 0, w, h};
|
||||
_src = (SDL_Rect){0, 0, w, h};
|
||||
}
|
||||
|
||||
|
||||
/* [DIMENSIONS] Definition des dimensions de la texture
|
||||
=========================================================*/
|
||||
void xSprite::dimensions(SDL_Rect r){
|
||||
/* (1) On recupere les informations de la texture */
|
||||
int w, h;
|
||||
SDL_QueryTexture(_texture, NULL, NULL, &w, &h);
|
||||
|
||||
|
||||
/* (2) On definit les dimensions */
|
||||
_dst = (SDL_Rect){r.x, r.y, r.w, r.h};
|
||||
_src = (SDL_Rect){0, 0, w, h};
|
||||
}
|
||||
|
||||
/* [DIMENSIONS] Definition des dimensions de la texture+clip
|
||||
=========================================================*/
|
||||
void xSprite::dimensions(SDL_Rect r, SDL_Rect clip){
|
||||
/* (1) On definit les dimensions */
|
||||
_dst = (SDL_Rect){r.x, r.y, r.w, r.h};
|
||||
_src = (SDL_Rect){clip.x, clip.y, clip.w, clip.h};
|
||||
}
|
||||
|
||||
/* [PUSH] Ajoute le xSprite au rendu
|
||||
=========================================================*/
|
||||
void xSprite::push(string index){
|
||||
_manager->push(index, _texture, &_src, &_dst);
|
||||
}
|
||||
|
||||
/* [PULL] Retire une sprite du rendu
|
||||
=========================================================*/
|
||||
void xSprite::pull(string index){
|
||||
_manager->pull( index );
|
||||
}
|
||||
|
||||
/* [PULL] Retire une sprite du rendu
|
||||
=========================================================*/
|
||||
void xSprite::pull(){
|
||||
_manager->pull( _texture );
|
||||
}
|
||||
|
||||
/* [UPDATE] Mise a jour du rendu
|
||||
=========================================================*/
|
||||
void xSprite::update(){
|
||||
_manager->update();
|
||||
}
|
||||
|
||||
|
||||
/* [TEXTURE] Retourne la texture
|
||||
=========================================================*/
|
||||
SDL_Texture *xSprite::texture(){ return _texture; }
|
||||
|
||||
/* [MANAGER] Retourne le manager
|
||||
=========================================================*/
|
||||
xManager *xSprite::manager(){ return _manager; }
|
||||
|
||||
|
||||
/* [DST] Retourne le SDL_Rect de destination
|
||||
=========================================================*/
|
||||
SDL_Rect *xSprite::dst(){ return &_dst; }
|
||||
|
||||
/* [SRC] Retourne le SDL_Rect source
|
||||
=========================================================*/
|
||||
SDL_Rect *xSprite::src(){ return &_src; }
|
|
@ -0,0 +1,40 @@
|
|||
#ifndef DEF_XSPRITE_H
|
||||
|
||||
#define DEF_XSPRITE_H
|
||||
|
||||
class xSprite{
|
||||
|
||||
public:
|
||||
xSprite(xManager *m); // Sprite vide
|
||||
xSprite(xManager *m, const int rgb[]); // Sprite couleur
|
||||
xSprite(xManager *m, const char *url); // Sprite image
|
||||
xSprite(xManager *m, SDL_Texture *t); // Sprite texture
|
||||
~xSprite();
|
||||
|
||||
void dimensions(); // Dimensions par defaut
|
||||
void dimensions(SDL_Rect r); // Dimensions sortie
|
||||
void dimensions(SDL_Rect r, SDL_Rect clip); // Dimensions in/out
|
||||
|
||||
void push(string index); // Ajoute a l'affichage
|
||||
void pull(string index); // Retire de l'affichage
|
||||
void pull(); // Retire de l'affichage
|
||||
|
||||
void update(); // Fait renmonter la mise a jour du manager
|
||||
|
||||
// GETTERS
|
||||
SDL_Texture *texture();
|
||||
xManager *manager();
|
||||
|
||||
SDL_Rect *dst();
|
||||
SDL_Rect *src();
|
||||
|
||||
private:
|
||||
xManager *_manager;
|
||||
SDL_Texture *_texture;
|
||||
|
||||
SDL_Rect _dst;
|
||||
SDL_Rect _src;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,218 @@
|
|||
/* [DESTRUCTUR] Destruction de l'animation
|
||||
=========================================================*/
|
||||
xSpriteAnimation::~xSpriteAnimation(){
|
||||
SDL_DestroyTexture( _texture );
|
||||
_manager = NULL;
|
||||
_texture = NULL;
|
||||
}
|
||||
|
||||
/* [CONSTRUCTOR] Construction de l'animation (chargement)
|
||||
=========================================================*/
|
||||
xSpriteAnimation::xSpriteAnimation(xManager *manager, const char *url, SDL_Rect viewport){
|
||||
/* (1) Definition des attributs */
|
||||
_manager = manager;
|
||||
_texture = NULL;
|
||||
|
||||
_viewport = viewport;
|
||||
|
||||
|
||||
/* (2) On charge le spritesheet */
|
||||
_texture = IMG_LoadTexture( _manager->renderer(), url );
|
||||
|
||||
// Gestion erreur
|
||||
if( _texture == NULL )
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/* [CONSTRUCTOR] Construction de l'animation a partir d'une texture existante
|
||||
=========================================================*/
|
||||
xSpriteAnimation::xSpriteAnimation(xManager *manager, SDL_Texture *t, SDL_Rect viewport){
|
||||
/* (1) Definition des attributs */
|
||||
_manager = manager;
|
||||
_texture = NULL;
|
||||
|
||||
_viewport = viewport;
|
||||
|
||||
|
||||
/* (2) On charge le spritesheet */
|
||||
_texture = t;
|
||||
|
||||
// Gestion erreur
|
||||
if( _texture == NULL )
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/* [MOVE] Modification de la position/taille du sprite
|
||||
=========================================================*/
|
||||
void xSpriteAnimation::move(SDL_Rect newpos){
|
||||
|
||||
if( !_manager->hit(_texture, newpos.x, newpos.y) ){
|
||||
|
||||
if( newpos.x != 0 )
|
||||
_viewport.x = newpos.x;
|
||||
|
||||
if( newpos.y != 0 )
|
||||
_viewport.y = newpos.y;
|
||||
|
||||
if( newpos.w != 0 )
|
||||
_viewport.w = newpos.w;
|
||||
|
||||
if( newpos.h != 0)
|
||||
_viewport.h = newpos.h;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* [MOVE] Deplacement de la position/taille du sprite
|
||||
=========================================================*/
|
||||
void xSpriteAnimation::move(int x, int y){
|
||||
|
||||
if( !_manager->hit(_texture, x, y) ){
|
||||
_viewport.x += x;
|
||||
_viewport.y += y;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* [ADDFRAME] Ajout d'une frame d'animation
|
||||
=========================================================*/
|
||||
void xSpriteAnimation::addFrame(SDL_Rect clip){
|
||||
|
||||
// On ajoute une frame
|
||||
_frames.push_back( (SDL_Rect){
|
||||
clip.x,
|
||||
clip.y,
|
||||
clip.w,
|
||||
clip.h
|
||||
} );
|
||||
|
||||
}
|
||||
|
||||
|
||||
/* [MANAGER] Retourne le manager
|
||||
=========================================================*/
|
||||
xManager *xSpriteAnimation::manager(){ return _manager; }
|
||||
|
||||
|
||||
/* [VIEWPORT] Retourne le viewport
|
||||
=========================================================*/
|
||||
SDL_Rect *xSpriteAnimation::viewport(){ return &_viewport; }
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* [PUSH] Ajoute au rendu
|
||||
=========================================================*/
|
||||
void xSpriteAnimation::push(string index){
|
||||
_index = index;
|
||||
|
||||
// On definit la frame
|
||||
_frame = _frames[0];
|
||||
|
||||
/* (1) On ajoute le sprite au rendu */
|
||||
_manager->push(index, _texture, &_frame, &_viewport);
|
||||
}
|
||||
|
||||
|
||||
/* [PULL] Retire du rendu
|
||||
=========================================================*/
|
||||
void xSpriteAnimation::pull(string index){
|
||||
_index = index;
|
||||
|
||||
/* (2) On retire le sprite du rendu */
|
||||
_manager->pull(index);
|
||||
}
|
||||
|
||||
|
||||
/* [PULL] Retire du rendu
|
||||
=========================================================*/
|
||||
void xSpriteAnimation::pull(){
|
||||
/* (2) On retire le sprite du rendu */
|
||||
_manager->pull(_texture);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* [TRHEAD] Process de l'animation
|
||||
=========================================================*/
|
||||
void xSpriteAnimationProcess(xSpriteAnimation *xSA, int t, int flags){
|
||||
int timer = 0;
|
||||
bool way = true;
|
||||
int start = 0;
|
||||
int stop = xSA->_frames.size();
|
||||
|
||||
while( flags&SPRITE_ANIM_INFINITE ){
|
||||
|
||||
/* (1) Pour chaque sprite */
|
||||
for( int i = start ; i != xSA->_frames.size() ; i+=(way?1:-1) ){
|
||||
timer = SDL_GetTicks();
|
||||
|
||||
|
||||
// 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) );
|
||||
}
|
||||
|
||||
/* (2) Gestion des flags */
|
||||
|
||||
// SPRITE_ANIM_REVERSE
|
||||
if( flags&SPRITE_ANIM_REVERSE ){
|
||||
way = !way;
|
||||
start = (way) ? 0 : xSA->_frames.size()-1;
|
||||
stop = (way) ? xSA->_frames.size()-1 : 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/* (n) On termine le thread */
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* [START] Ajoute l'animation au rendu
|
||||
=========================================================*/
|
||||
void xSpriteAnimation::start(int t, int flags){
|
||||
_timeout = t;
|
||||
_flags = flags;
|
||||
|
||||
// this->push(index);
|
||||
|
||||
/* (1) On lance l'animation */
|
||||
_animation = new thread(xSpriteAnimationProcess, this, t, flags);
|
||||
|
||||
// On attends pas le thread
|
||||
_animation->detach();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* [STOP] Arrete l'animation
|
||||
=========================================================*/
|
||||
void xSpriteAnimation::stop(){
|
||||
this->pull();
|
||||
|
||||
/* (1) On arrete l'animation */
|
||||
delete _animation;
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
#ifndef DEF_XSPRITEANIMATION_H
|
||||
|
||||
#define DEF_XSPRITEANIMATION_H
|
||||
|
||||
class xSpriteAnimation{
|
||||
|
||||
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 move(int x, int y); // Deplace l'animation
|
||||
|
||||
void addFrame(SDL_Rect clip); // Ajoute une frame en fonction des coordonnees
|
||||
|
||||
// GETTER
|
||||
xManager *manager();
|
||||
SDL_Rect *viewport();
|
||||
|
||||
// Gestion de l'ajout au rendu
|
||||
void push(string index); // Ajout au rendu
|
||||
void pull(string index); // Retrait du rendu
|
||||
void pull(); // Retrait du rendu
|
||||
|
||||
// Gestion de l'animation
|
||||
void start(int t, int flags=SPRITE_ANIM_ONCE);
|
||||
void stop();
|
||||
|
||||
|
||||
|
||||
|
||||
protected:
|
||||
xManager *_manager;
|
||||
SDL_Texture *_texture;
|
||||
|
||||
// Position de l'animation
|
||||
SDL_Rect _viewport;
|
||||
|
||||
// Contiendra les frames
|
||||
vector<SDL_Rect> _frames;
|
||||
SDL_Rect _frame; // Frame courante
|
||||
|
||||
// Nom
|
||||
string _index;
|
||||
int _timeout;
|
||||
int _flags;
|
||||
|
||||
// Contiendra le thread de l'animation
|
||||
thread *_animation;
|
||||
friend void xSpriteAnimationProcess(xSpriteAnimation *xSA, int t, int flags );
|
||||
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,97 @@
|
|||
/* [CONSTRUCTOR] Initialisation de la liste de xSprite
|
||||
=========================================================*/
|
||||
xSpriteGroup::xSpriteGroup(){
|
||||
}
|
||||
|
||||
/* [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
|
||||
=========================================================*/
|
||||
void xSpriteGroup::add(xSprite *s){
|
||||
_sprites.push_back( s );
|
||||
}
|
||||
|
||||
/* [REMOVE] Suppression d'un xSprite du groupe
|
||||
=========================================================*/
|
||||
void xSpriteGroup::remove(xSprite *s){
|
||||
int index = -1; // on cherche l'indice du sprite
|
||||
|
||||
// On parcours la liste pour trouver l'indice
|
||||
for( int i = 0 ; i < _sprites.size() ; i++ )
|
||||
if( _sprites[i] == s ) index = i;
|
||||
|
||||
// Si on a pas trouve l'indice
|
||||
if( index == -1 ) return;
|
||||
|
||||
// On supprime le sprite de la liste
|
||||
_sprites.erase(_sprites.begin() + index );
|
||||
|
||||
}
|
||||
|
||||
/* [PUSH] Ajoute tous les xSprite du groupe a une surface parente
|
||||
=========================================================*/
|
||||
void xSpriteGroup::push(string index){
|
||||
_index = index;
|
||||
|
||||
string newIndex;
|
||||
|
||||
for( int i = 0 ; i < _sprites.size() ; i++ ){
|
||||
newIndex = index;
|
||||
newIndex += to_string(i);
|
||||
_sprites[i]->push(newIndex);
|
||||
}
|
||||
}
|
||||
|
||||
/* [PULL] Retire une sprite de la surface parents
|
||||
=========================================================*/
|
||||
void xSpriteGroup::pull(string index){
|
||||
_index = index;
|
||||
|
||||
string newIndex;
|
||||
|
||||
for( int i = 0 ; i < _sprites.size() ; i++ ){
|
||||
newIndex = index;
|
||||
newIndex += to_string(i);
|
||||
_sprites[i]->pull(newIndex);
|
||||
}
|
||||
}
|
||||
|
||||
/* [PULL] Retire une sprite de la surface parents
|
||||
=========================================================*/
|
||||
void xSpriteGroup::pull(){
|
||||
for( int i = 0 ; i < _sprites.size() ; i++ )
|
||||
_sprites[i]->pull();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* [UPDATE] Mise a jour du rendu
|
||||
=========================================================*/
|
||||
void xSpriteGroup::update(){
|
||||
if( _sprites.size() > 0 )
|
||||
_sprites[0]->update();
|
||||
}
|
||||
|
||||
|
||||
/* [GET] Retourne le xSprite d'index donne
|
||||
=========================================================*/
|
||||
xSprite* xSpriteGroup::get(int i){
|
||||
return _sprites[i];
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
#ifndef DEF_XSPRITEGROUP_H
|
||||
|
||||
#define DEF_XSPRITEGROUP_H
|
||||
|
||||
|
||||
class xSpriteGroup{
|
||||
|
||||
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);
|
||||
|
||||
void push(string index); // Ajoute les sprites a l'affichage
|
||||
void pull(string index); // Retire les sprites de l'affichage
|
||||
void pull(); // Retire les sprites de l'affichage
|
||||
|
||||
void update(); // Fait renmonter la mise a jour du manager
|
||||
|
||||
protected:
|
||||
vector<xSprite*> _sprites;
|
||||
|
||||
string _index;
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif
|