159 lines
3.6 KiB
C++
159 lines
3.6 KiB
C++
#include "main.h"
|
|
|
|
// On cree un sdl-toplevel statique
|
|
static sdltl *mgr = NULL;
|
|
static bool running = true;
|
|
|
|
|
|
|
|
int main(int argc, char* argv[]) {
|
|
srand(time(0));
|
|
|
|
/* [0] Initialisation de SDL
|
|
=========================================================*/
|
|
mgr = new sdltl("Ma fenetre SDL", WIN_WIDTH, WIN_HEIGHT);
|
|
|
|
/* [1] Creation de la fenetre
|
|
=========================================================*/
|
|
if( !mgr->status() ) cout << "Erreur: " << SDL_GetError() << endl;
|
|
|
|
|
|
/* [3] On definit le background color
|
|
=========================================================*/
|
|
mgr->setBackground(255, 255, 255);
|
|
// bool imageLoaded = mgr->setImage( "src/1.bmp" );
|
|
|
|
|
|
|
|
/* [4] On ajoute Une animation
|
|
=========================================================*/
|
|
/* (1) On cree les fonds pour couvrir les frames en arriere plan */
|
|
int winSize[2];
|
|
SDL_GetWindowSize( mgr->window(), &winSize[0], &winSize[1]);
|
|
int white[] = {255, 255, 255};
|
|
Sprite *mb_background = new Sprite(
|
|
white,
|
|
(SDL_Rect){0, 0, 20, 20}
|
|
);
|
|
|
|
Sprite *gs_background = new Sprite(
|
|
white,
|
|
(SDL_Rect){100, 0, 20, 20}
|
|
);
|
|
|
|
Sprite *rs_background = new Sprite(
|
|
white,
|
|
(SDL_Rect){150, 0, 20, 20}
|
|
);
|
|
|
|
|
|
/* (2) On enregistre les frames de l'animation du bloc mystere */
|
|
SpriteGroup animated_mystery_bloc;
|
|
|
|
for( int i = 0 ; i < 4 ; i++ ){
|
|
|
|
animated_mystery_bloc.add( new Sprite(
|
|
"src/mario_crop.png",
|
|
(SDL_Rect){0, 0, 20, 20},
|
|
(SDL_Rect){4*19+19*i, 2*20, 20, 20}
|
|
) );
|
|
|
|
}
|
|
|
|
/* (3) On enregistre les frames de l'animation d'une carapace verte */
|
|
SpriteGroup animated_green_shell;
|
|
|
|
for( int i = 0 ; i < 4 ; i++ ){
|
|
|
|
animated_green_shell.add( new Sprite(
|
|
"src/mario_crop.png",
|
|
(SDL_Rect){100, 0, 20, 20},
|
|
(SDL_Rect){4*19+19*i, 11*19, 20, 20}
|
|
) );
|
|
|
|
}
|
|
|
|
/* (4) On enregistre les frames de l'animation d'une carapace rouge */
|
|
SpriteGroup animated_red_shell;
|
|
|
|
for( int i = 0 ; i < 4 ; i++ ){
|
|
|
|
animated_red_shell.add( new Sprite(
|
|
"src/mario_crop.png",
|
|
(SDL_Rect){150, 0, 20, 20},
|
|
(SDL_Rect){2+19*i, 11*19, 20, 20}
|
|
) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [5] On lance les animation en parallele
|
|
=========================================================*/
|
|
vector<thread*> animations(0);
|
|
|
|
animations.push_back( animated_mystery_bloc.animate(
|
|
mgr->window(), // fenetre a mettre a jour
|
|
200, // delai entre-frames
|
|
mb_background , // Sprite de reset
|
|
SPRITE_ANIM_INFINITE // FLAGS
|
|
) );
|
|
animations[0]->detach(); // On laisse le thread tourner
|
|
delete animations[0]; // On le supprime ensuite
|
|
|
|
|
|
animations.push_back( animated_green_shell.animate(
|
|
mgr->window(), // fenetre a mettre a jour
|
|
100, // delai entre-frames
|
|
gs_background, // Sprite de reset
|
|
SPRITE_ANIM_INFINITE // FLAGS
|
|
) );
|
|
animations[1]->detach(); // On laisse le thread tourner
|
|
delete animations[1]; // On le supprime ensuite
|
|
|
|
|
|
animations.push_back( animated_red_shell.animate(
|
|
mgr->window(), // fenetre a mettre a jour
|
|
100, // delai entre-frames
|
|
rs_background, // Sprite de reset
|
|
SPRITE_ANIM_INFINITE // FLAGS
|
|
) );
|
|
animations[2]->detach(); // On laisse le thread tourner
|
|
delete animations[2]; // On le supprime ensuite
|
|
|
|
|
|
|
|
|
|
/* [n-1] Boucle infinie
|
|
=========================================================*/
|
|
mgr->manageFps(FPS);
|
|
running = true;
|
|
while(running){
|
|
mgr->waitEvent(SDL_QUIT, &quitEventHandler);
|
|
|
|
// cout << "Main loop" << endl;
|
|
|
|
// Gestion des FPS (vitesse de la boucle)
|
|
mgr->manageFps();
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [n] Fin d'execution
|
|
=========================================================*/
|
|
delete mgr;
|
|
return 0;
|
|
}
|
|
|
|
|
|
void quitEventHandler(SDL_Event *e){
|
|
cout << "Ferme" << endl;
|
|
running = false;
|
|
} |