2016-03-12 23:22:28 +00:00
|
|
|
#include "main.h"
|
|
|
|
|
|
|
|
// On cree un sdl-toplevel statique
|
|
|
|
static xManager *mgr = NULL;
|
|
|
|
static bool running = true;
|
|
|
|
|
2016-03-13 17:33:47 +00:00
|
|
|
static xMarioMario *mario = NULL;
|
2016-03-12 23:22:28 +00:00
|
|
|
|
2016-03-13 19:36:16 +00:00
|
|
|
static bool left_move = false;
|
|
|
|
static bool right_move = false;
|
|
|
|
static bool up_move = false;
|
|
|
|
static bool down_move = false;
|
|
|
|
|
2016-03-12 23:22:28 +00:00
|
|
|
|
|
|
|
int main(int argc, char* argv[]) {
|
|
|
|
srand(time(0));
|
|
|
|
|
|
|
|
/* [0] Initialisation du manager + Creation de la fenetre
|
|
|
|
=========================================================*/
|
2016-03-13 13:53:39 +00:00
|
|
|
mgr = new xManager("Ma fenetre SDL", BLOC_WIDTH*BLOC_SIZE, BLOC_HEIGHT*BLOC_SIZE);
|
2016-03-12 23:22:28 +00:00
|
|
|
|
|
|
|
// Gestion erreur
|
|
|
|
if( !mgr->status() ){
|
|
|
|
cout << "[INIT] -> " << SDL_GetError() << endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-03-13 13:53:39 +00:00
|
|
|
// Couleur de fond
|
|
|
|
mgr->setBackground(0, 96, 183);
|
|
|
|
mgr->setImage("src/bg1.png");
|
2016-03-12 23:22:28 +00:00
|
|
|
|
2016-03-13 13:53:39 +00:00
|
|
|
/* [2] On definit le terrain
|
2016-03-12 23:22:28 +00:00
|
|
|
=========================================================*/
|
2016-03-13 13:53:39 +00:00
|
|
|
// On cree un bout du terrain
|
|
|
|
xMarioGrass btmleft(mgr, (SDL_Rect){-1, 20-2, 10, 3} );
|
2016-03-13 17:33:47 +00:00
|
|
|
btmleft.push("bottom-left");
|
2016-03-12 23:22:28 +00:00
|
|
|
|
2016-03-13 19:36:16 +00:00
|
|
|
// On cree un bout du terrain
|
2016-03-14 18:28:18 +00:00
|
|
|
xMarioGrass btmcenter(mgr, (SDL_Rect){12, 20-2, 10, 3} );
|
2016-03-13 19:36:16 +00:00
|
|
|
btmcenter.push("bottom-center");
|
|
|
|
|
2016-03-12 23:22:28 +00:00
|
|
|
|
2016-03-16 09:17:28 +00:00
|
|
|
// xMarioGrass floattcenter(mgr, (SDL_Rect){14, 20-5, 1, 1} );
|
|
|
|
// floattcenter.push("float-top");
|
2016-03-13 14:22:14 +00:00
|
|
|
|
|
|
|
|
2016-03-12 23:22:28 +00:00
|
|
|
|
2016-03-13 13:53:39 +00:00
|
|
|
/* [3] Gestion des animations (blocs animes)
|
|
|
|
=========================================================*/
|
|
|
|
// On cree une coquille verte
|
2016-03-16 09:17:28 +00:00
|
|
|
xMarioGreenShell gs(mgr, 5, 20-3);
|
|
|
|
gs.push("green-sheel");
|
|
|
|
gs.start(100, SPRITE_ANIM_INFINITE);
|
2016-03-12 23:22:28 +00:00
|
|
|
|
2016-03-14 18:28:18 +00:00
|
|
|
// On cree une brique
|
2016-03-16 11:08:22 +00:00
|
|
|
xMarioBrick mbr1(mgr, 4, 20-6, 1); // 1 saut
|
2016-03-14 18:28:18 +00:00
|
|
|
mbr1.push("brick1");
|
|
|
|
|
2016-03-13 13:53:39 +00:00
|
|
|
// On cree un bloc mystere
|
2016-03-16 11:08:22 +00:00
|
|
|
xMarioMysteryBloc mb(mgr, 5, 20-6, 2); // 2 sauts
|
2016-03-14 15:10:21 +00:00
|
|
|
mb.push("mystery-bloc");
|
|
|
|
mb.start(150, SPRITE_ANIM_INFINITE);
|
2016-03-12 23:22:28 +00:00
|
|
|
|
2016-03-14 18:28:18 +00:00
|
|
|
// On cree une brique
|
2016-03-16 11:08:22 +00:00
|
|
|
xMarioBrick mbr2(mgr, 6, 20-6, 2); // 2 sauts
|
2016-03-14 18:28:18 +00:00
|
|
|
mbr2.push("brick2");
|
|
|
|
|
|
|
|
|
2016-03-16 11:08:22 +00:00
|
|
|
xMarioMysteryBloc mb1(mgr, 15, 20-6, 1); // 1 saut
|
2016-03-14 18:28:18 +00:00
|
|
|
mb1.push("mystery-bloc2");
|
|
|
|
mb1.start(150, SPRITE_ANIM_INFINITE);
|
|
|
|
|
2016-03-16 11:08:22 +00:00
|
|
|
xMarioMysteryBloc mb2(mgr, 17, 20-5, 1); // 1 saut
|
2016-03-15 21:56:39 +00:00
|
|
|
mb2.push("mystery-bloc3");
|
|
|
|
mb2.start(150, SPRITE_ANIM_INFINITE);
|
|
|
|
|
2016-03-16 11:08:22 +00:00
|
|
|
xMarioMysteryBloc mb3(mgr, 19, 20-4, 6); // 6 sauts
|
2016-03-15 21:56:39 +00:00
|
|
|
mb3.push("mystery-bloc4");
|
|
|
|
mb3.start(150, SPRITE_ANIM_INFINITE);
|
|
|
|
|
2016-03-13 14:22:14 +00:00
|
|
|
// On cree un bloc normal
|
2016-03-15 21:56:39 +00:00
|
|
|
// xMarioBloc bl(mgr, (SDL_Rect){16, 20-5, 1, 1});
|
2016-03-13 17:33:47 +00:00
|
|
|
// bl.push("bloc-bottom-left");
|
|
|
|
|
|
|
|
// On cree mario
|
2016-03-15 21:56:39 +00:00
|
|
|
mario = new xMarioMario(mgr, 17, 20-3);
|
2016-03-14 15:10:21 +00:00
|
|
|
mario->push("mario");
|
|
|
|
mario->start(100, SPRITE_ANIM_INFINITE);
|
2016-03-12 23:22:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [n-1] Boucle infinie
|
|
|
|
=========================================================*/
|
2016-03-13 13:53:39 +00:00
|
|
|
|
|
|
|
// Gestion des evenements
|
|
|
|
SDL_Event event;
|
|
|
|
mgr->attachEvent(SDL_KEYDOWN, &keydownEventHandler);
|
2016-03-13 19:36:16 +00:00
|
|
|
mgr->attachEvent(SDL_KEYUP, &keyupEventHandler);
|
2016-03-13 13:53:39 +00:00
|
|
|
mgr->attachEvent(SDL_QUIT, &quitEventHandler);
|
|
|
|
|
2016-03-15 21:56:39 +00:00
|
|
|
mgr->debug();
|
|
|
|
|
2016-03-15 22:54:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-03-13 13:53:39 +00:00
|
|
|
// Boucle de traitement
|
2016-03-15 21:56:39 +00:00
|
|
|
mgr->update();
|
2016-03-12 23:22:28 +00:00
|
|
|
mgr->manageFps(FPS);
|
|
|
|
while(running){
|
|
|
|
|
|
|
|
|
2016-03-13 13:53:39 +00:00
|
|
|
// Gestion des evenements
|
|
|
|
while( SDL_PollEvent(&event) != 0 )
|
|
|
|
mgr->manageEvents(&event);
|
2016-03-12 23:22:28 +00:00
|
|
|
|
2016-03-13 19:36:16 +00:00
|
|
|
// Gestion de la gravite
|
2016-03-14 15:10:21 +00:00
|
|
|
// if( !mario->onFloor() && SDL_GetTicks() % 10 < 5 )
|
|
|
|
// mario->velocity(0, 1.0);
|
|
|
|
|
2016-03-13 19:36:16 +00:00
|
|
|
|
2016-03-14 12:14:35 +00:00
|
|
|
// Deplacement
|
|
|
|
mario->moveFromVelocity();
|
|
|
|
|
|
|
|
|
2016-03-16 11:08:22 +00:00
|
|
|
|
2016-03-16 12:56:52 +00:00
|
|
|
// if( mgr->hit("brick1", 0, 1) )mbr1.jump();
|
|
|
|
mbr1.unjump();
|
|
|
|
// if( mgr->hit("brick2", 0, 1) ) mbr2.jump();
|
|
|
|
mbr2.unjump();
|
2016-03-16 11:08:22 +00:00
|
|
|
|
|
|
|
|
2016-03-16 12:56:52 +00:00
|
|
|
// if( mgr->hit("mystery-bloc", 0, 1) )mb.jump();
|
|
|
|
mb.unjump();
|
2016-03-13 13:53:39 +00:00
|
|
|
|
2016-03-16 12:56:52 +00:00
|
|
|
// if( mgr->hit("mystery-bloc2", 0, 1) )mb1.jump();
|
|
|
|
mb1.unjump();
|
2016-03-15 21:56:39 +00:00
|
|
|
|
2016-03-16 12:56:52 +00:00
|
|
|
// if( mgr->hit("mystery-bloc3", 0, 1) )mb2.jump();
|
|
|
|
mb2.unjump();
|
2016-03-15 21:56:39 +00:00
|
|
|
|
2016-03-16 12:56:52 +00:00
|
|
|
// if( mgr->hit("mystery-bloc4", 0, 1) ) mb3.jump();
|
|
|
|
mb3.unjump();
|
2016-03-15 21:56:39 +00:00
|
|
|
|
2016-03-13 13:53:39 +00:00
|
|
|
mgr->manageFps(); // Gestion des FPS (speed)
|
2016-03-13 09:08:07 +00:00
|
|
|
mgr->update(); // Mise a jour du rendu
|
2016-03-12 23:22:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [n] Fin d'execution
|
|
|
|
=========================================================*/
|
|
|
|
delete mgr;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-13 19:36:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-03-13 13:53:39 +00:00
|
|
|
/* GESTION DE QUAND ON QUITTE LA FENETRE
|
|
|
|
*
|
|
|
|
* @e<SDL_Event*> Pointeur sur l'evenement appelant
|
|
|
|
*
|
|
|
|
*/
|
2016-03-12 23:22:28 +00:00
|
|
|
void quitEventHandler(SDL_Event *e){
|
2016-03-13 09:08:07 +00:00
|
|
|
cout << "Exiting program" << endl;
|
2016-03-12 23:22:28 +00:00
|
|
|
running = false;
|
2016-03-13 13:53:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-03-13 19:36:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-03-13 13:53:39 +00:00
|
|
|
/* GESTION DE QUAND ON APPUIE SUR FLECHE BAS
|
|
|
|
*
|
|
|
|
* @e<SDL_Event*> Pointeur sur l'evenement appelant
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void keydownEventHandler(SDL_Event *e){
|
2016-03-13 17:33:47 +00:00
|
|
|
|
|
|
|
switch( (*e).key.keysym.sym ){
|
|
|
|
case SDLK_UP:
|
2016-03-14 12:14:35 +00:00
|
|
|
if( !mario->_up ){
|
|
|
|
mario->_up = true;
|
|
|
|
// mario->velocity(0, -40.0);
|
|
|
|
}
|
2016-03-13 19:36:16 +00:00
|
|
|
break;
|
2016-03-13 17:33:47 +00:00
|
|
|
|
2016-03-13 19:36:16 +00:00
|
|
|
case SDLK_LEFT:
|
2016-03-14 12:14:35 +00:00
|
|
|
if( !mario->_left ){
|
|
|
|
mario->_left = true;
|
|
|
|
// mario->velocity(-15.0, 0);
|
|
|
|
}
|
2016-03-13 19:36:16 +00:00
|
|
|
break;
|
2016-03-13 17:33:47 +00:00
|
|
|
|
2016-03-13 19:36:16 +00:00
|
|
|
case SDLK_RIGHT:
|
2016-03-14 12:14:35 +00:00
|
|
|
if( !mario->_right ){
|
|
|
|
mario->_right = true;
|
|
|
|
// mario->velocity(15.0, 0);
|
|
|
|
}
|
2016-03-13 19:36:16 +00:00
|
|
|
break;
|
2016-03-13 17:33:47 +00:00
|
|
|
|
2016-03-13 19:36:16 +00:00
|
|
|
case SDLK_DOWN:
|
2016-03-14 12:14:35 +00:00
|
|
|
if( !mario->_down ){
|
2016-03-14 15:10:21 +00:00
|
|
|
// mario->_down = true;
|
2016-03-14 12:14:35 +00:00
|
|
|
// mario->velocity(0, 1.0);
|
|
|
|
}
|
2016-03-13 19:36:16 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
cout << "UNUSED KEY" << endl;
|
2016-03-13 17:33:47 +00:00
|
|
|
break;
|
2016-03-13 19:36:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-03-13 17:33:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2016-03-13 19:36:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* GESTION DE QUAND ON APPUIE SUR FLECHE BAS
|
|
|
|
*
|
|
|
|
* @e<SDL_Event*> Pointeur sur l'evenement appelant
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void keyupEventHandler(SDL_Event *e){
|
|
|
|
|
|
|
|
switch( (*e).key.keysym.sym ){
|
|
|
|
case SDLK_UP:
|
2016-03-14 12:14:35 +00:00
|
|
|
mario->_up = false;
|
2016-03-13 19:36:16 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SDLK_LEFT:
|
2016-03-14 12:14:35 +00:00
|
|
|
mario->_left = false;
|
2016-03-13 17:33:47 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SDLK_RIGHT:
|
2016-03-14 12:14:35 +00:00
|
|
|
mario->_right = false;
|
2016-03-13 17:33:47 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SDLK_DOWN:
|
2016-03-14 15:10:21 +00:00
|
|
|
// mario->_down = false;
|
2016-03-13 17:33:47 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
cout << "PRESSED" << endl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-03-12 23:22:28 +00:00
|
|
|
}
|