123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- #include "eventLoop.h"
- int eventLoop(SDL_Surface* ecran)
- {
- int check(0);//Variable d'erreur; 0 = Ok, -1 = probleme
- // Charges des images
- SDL_Surface* mort = SDL_LoadBMP("Images/GameOver.bmp");
- SDL_SetColorKey(mort,SDL_SRCCOLORKEY,SDL_MapRGB(mort->format,0,255,255));
- Joueur joueur(ecran);
- joueur.setArme("Baroudeur");
- Bouton iconeOne(0,0,0,"Baroudeur", ecran, &joueur);//Précédent 0/404
- Bouton iconeTwo(0,76,1,"Usiane", ecran, &joueur);//Précédent 140/404
- Bouton iconeThree(0,152,2,"Triphaseur", ecran, &joueur);//Précédent 280/404
- ChampDeBataille arena(&joueur,ecran,10);
- if (!mort)//Recherche d'erreurs
- {
- printf("Unable to load bitmap: %s\n", SDL_GetError());
- check = -1;
- return check;
- }
- // Coordonnées des surfaces
- SDL_Rect position;
- position.x = 0;
- position.y = 0;
- //Mise au point du tir sans répétition due à la mémoire de l'evenement.
- bool latence(true);
- // program main loop
- bool done = false;
- SDL_ShowCursor(SDL_DISABLE);
- //Gérance du temps
- int tempsPrecedent(0);
- SDL_Event event;
- while (!done && optionsFire::continuer)
- {
- // activation du fps (fréquence à 50Hz)
- tempsPrecedent = SDL_GetTicks();
- if (SDL_GetTicks() - tempsPrecedent < 20)
- SDL_Delay(15 + tempsPrecedent - SDL_GetTicks());
- while (SDL_GetTicks() - tempsPrecedent < 20); // Block until next frame
- // message processing loop
- while(SDL_PollEvent(&event))
- {
- // check for messages
- switch (event.type)
- {
- // exit if the window is closed
- case SDL_QUIT:
- optionsFire::continuer = false;
- done = true;
- break;
- case SDL_KEYDOWN:
- switch(event.key.keysym.sym)
- {
- case SDLK_ESCAPE:
- done = true;
- break;
- case SDLK_b:
- joueur.setArme("Baroudeur");
- break;
- case SDLK_u:
- joueur.setArme("Usiane");
- break;
- case SDLK_t:
- joueur.setArme("Triphaseur");
- break;
- }
- break;
- case SDL_MOUSEMOTION:
- joueur.giveMousePosition(event.motion.x,event.motion.y);
- break;
- case SDL_MOUSEBUTTONDOWN:
- switch (event.button.button)
- {
- case SDL_BUTTON_LEFT:
- if(latence == true)
- {
- Cliquable::getFire(event.button.x,event.button.y,joueur.getTypeWeapon());
- joueur.tir();
- }
- latence = false;//Empeche de tirer en continu.
- break;
- }//SDL_MOUSEBUTTONDOWN
- break;
- case SDL_MOUSEBUTTONUP:
- switch (event.button.button)
- {
- case SDL_BUTTON_LEFT:
- latence = true;
- break;
- }
- }
- } //EVENT end switch event loop
- // DRAWING STARTS HERE
- // clear screen
- SDL_FillRect(ecran, 0, SDL_MapRGB(ecran->format, 0, 0, 0));
- // draw bitmap
- SDL_BlitSurface(optionsFire::level, 0, ecran, &position);
- if (joueur.estVivant())
- {
- iconeOne.afficher();
- iconeTwo.afficher();
- iconeThree.afficher();
- arena.afficher();
- joueur.afficher();
- }
- else
- {
- SDL_BlitSurface(mort, 0, ecran, &position);
- SDL_ShowCursor(SDL_ENABLE);
- }
- // DRAWING ENDS HERE
- Cliquable::upFire();
- // finally, update the screen :)
- SDL_Flip(ecran);
- } //MAIN end main loop
- // free loaded bitmap
- SDL_FreeSurface(optionsFire::level);//Fond
- optionsFire::level = 0;//On pointe le pointeur sur lui, pour la securité
- return check;
- }
|