249 lines
4.4 KiB
C++
249 lines
4.4 KiB
C++
#include "main.h"
|
|
|
|
// On cree un sdl-toplevel statique
|
|
static xManager *mgr = NULL;
|
|
|
|
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 g1(mgr, (SDL_Rect){-1, 20-4, 32+2, 5} );
|
|
g1.push("grass1");
|
|
|
|
// On cree un bout du terrain
|
|
xMarioBloc b1(mgr, (SDL_Rect){10, 20-6, 14, 1});
|
|
b1.push("bloc1");
|
|
|
|
xMarioBloc b2(mgr, (SDL_Rect){23, 20-7, 1, 1});
|
|
b2.push("bloc2");
|
|
|
|
|
|
|
|
/* [3] Gestion des blocs
|
|
=========================================================*/
|
|
// On cree une coquille verte
|
|
xMarioGreenShell gs(mgr, 20-8, 20-9);
|
|
gs.push("green-sheel");
|
|
gs.start(80, SPRITE_ANIM_INFINITE);
|
|
|
|
// On cree une brique
|
|
xMarioBrick mBrick(mgr, 3, 20-7, 10); // 10 sauts
|
|
mBrick.push("brick");
|
|
|
|
xMarioBrick mBrick2(mgr, 10, 20-7, 5); // 5 sauts
|
|
mBrick2.push("brick2");
|
|
|
|
// On cree un bloc mystere
|
|
xMarioMysteryBloc mMystery(mgr, 5, 20-7, 2); // 2 sauts
|
|
mMystery.push("mystery-bloc");
|
|
mMystery.start(150, SPRITE_ANIM_INFINITE);
|
|
|
|
|
|
|
|
|
|
// On cree mario
|
|
mario = new xMarioMario(mgr, 17, 20-7);
|
|
mario->push("mario");
|
|
mario->start(100, SPRITE_ANIM_INFINITE);
|
|
|
|
|
|
|
|
|
|
/* [n-2] Boucle infinie
|
|
=========================================================*/
|
|
|
|
// Gestion des evenements
|
|
SDL_Event event;
|
|
mgr->attachEvent(SDL_KEYDOWN, &keydownEventHandler); // Evenement appui clavier
|
|
mgr->attachEvent(SDL_KEYUP, &keyupEventHandler); // Evenement relache clavier
|
|
mgr->attachEvent(SDL_QUIT, &quitEventHandler); // Evenement QUITTER
|
|
|
|
mgr->debug();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Boucle de traitement
|
|
mgr->update();
|
|
mgr->manageFps(FPS);
|
|
mgr->state = 0;
|
|
while(mgr->state==0){
|
|
|
|
// Gestion des evenements
|
|
while( SDL_PollEvent(&event) != 0 )
|
|
mgr->manageEvents(&event);
|
|
|
|
// Deplacement de Mario
|
|
mario->moveFromVelocity();
|
|
|
|
// Deplacement coquille verte
|
|
gs.moveFromVelocity();
|
|
|
|
// Mise a jour du rendu
|
|
mgr->manageFps(); // Gestion des FPS (speed)
|
|
mgr->update(); // Mise a jour du rendu
|
|
|
|
}
|
|
|
|
|
|
/* [n-1] Gestion de l'arret
|
|
=========================================================*/
|
|
if( mgr->state == 2 ){
|
|
mgr->clearAll(); // On vire tout
|
|
mgr->setBackground(0, 0, 0); // Fond noir
|
|
mgr->setImage("src/game_over.jpg"); // Image GAME OVER
|
|
|
|
mgr->update(); // Mise a jour du rendu
|
|
usleep(100*1000); // Attente 3 sec
|
|
}
|
|
|
|
|
|
|
|
|
|
/* [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;
|
|
mgr->state = 1;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 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){
|
|
|
|
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;
|
|
}
|
|
|
|
} |