lab.cpp/SDL#2/main.cpp

159 lines
3.6 KiB
C++
Raw Permalink Normal View History

2016-03-10 23:00:26 +00:00
#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
2016-03-10 23:00:26 +00:00
=========================================================*/
2016-03-11 12:42:48 +00:00
/* (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};
2016-03-11 12:42:48 +00:00
Sprite *mb_background = new Sprite(
white,
2016-03-11 12:42:48 +00:00
(SDL_Rect){0, 0, 20, 20}
);
2016-03-10 23:00:26 +00:00
2016-03-11 12:42:48 +00:00
Sprite *gs_background = new Sprite(
white,
(SDL_Rect){100, 0, 20, 20}
);
2016-03-10 23:00:26 +00:00
2016-03-11 12:42:48 +00:00
Sprite *rs_background = new Sprite(
white,
(SDL_Rect){150, 0, 20, 20}
);
2016-03-11 12:42:48 +00:00
/* (2) On enregistre les frames de l'animation du bloc mystere */
SpriteGroup animated_mystery_bloc;
2016-03-11 12:42:48 +00:00
for( int i = 0 ; i < 4 ; i++ ){
2016-03-11 12:42:48 +00:00
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}
) );
}
2016-03-10 23:00:26 +00:00
2016-03-11 12:42:48 +00:00
/* (4) On enregistre les frames de l'animation d'une carapace rouge */
SpriteGroup animated_red_shell;
2016-03-10 23:00:26 +00:00
2016-03-11 12:42:48 +00:00
for( int i = 0 ; i < 4 ; i++ ){
2016-03-10 23:00:26 +00:00
2016-03-11 12:42:48 +00:00
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}
) );
2016-03-10 23:00:26 +00:00
2016-03-11 12:42:48 +00:00
}
/* [5] On lance les animation en parallele
=========================================================*/
2016-03-11 12:42:48 +00:00
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
2016-03-10 23:00:26 +00:00
/* [n-1] Boucle infinie
=========================================================*/
mgr->manageFps(FPS);
running = true;
while(running){
mgr->waitEvent(SDL_QUIT, &quitEventHandler);
2016-03-11 12:42:48 +00:00
// cout << "Main loop" << endl;
2016-03-10 23:00:26 +00:00
// 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;
}