소스 검색

Fix framerate

Framerate implementation was depending on CPU clock.
DricomDragon 4 년 전
부모
커밋
b48fa34e5c
1개의 변경된 파일39개의 추가작업 그리고 38개의 파일을 삭제
  1. 39 38
      eventLoop.cpp

+ 39 - 38
eventLoop.cpp

@@ -2,7 +2,7 @@
 
 int eventLoop(SDL_Surface* ecran)
 {
-	int check(0);//Variable d'erreur; 0=Ok, -1=probleme
+	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));
@@ -17,7 +17,7 @@ int eventLoop(SDL_Surface* ecran)
 	if (!mort)//Recherche d'erreurs
 	{
 		printf("Unable to load bitmap: %s\n", SDL_GetError());
-		check=-1;
+		check = -1;
 		return check;
 	}
 
@@ -32,23 +32,26 @@ int eventLoop(SDL_Surface* ecran)
 	SDL_ShowCursor(SDL_DISABLE);
 	//Gérance du temps
 	int tempsPrecedent(0);
-	int tempsActuel(0);
+	SDL_Event event;
+
 	while (!done && optionsFire::continuer)
 	{
-		// message processing loop
-		SDL_Event event;
-		SDL_PollEvent(&event);
 		// activation du fps (fréquence à 50Hz)
-		tempsActuel=SDL_GetTicks();
-		if (tempsActuel-tempsPrecedent>20)
-		{//Fps
+		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;
+					optionsFire::continuer = false;
 					done = true;
 					break;
 				case SDL_KEYDOWN:
@@ -75,12 +78,12 @@ int eventLoop(SDL_Surface* ecran)
 					switch (event.button.button)
 					{
 						case SDL_BUTTON_LEFT:
-							if(latence==true)
+							if(latence == true)
 							{
 								Cliquable::getFire(event.button.x,event.button.y,joueur.getTypeWeapon());
 								joueur.tir();
 							}
-							latence=false;//Empeche de tirer en continu.
+							latence = false;//Empeche de tirer en continu.
 							break;
 					}//SDL_MOUSEBUTTONDOWN
 					break;
@@ -88,37 +91,35 @@ int eventLoop(SDL_Surface* ecran)
 					switch (event.button.button)
 					{
 						case SDL_BUTTON_LEFT:
-							latence=true;
+							latence = true;
 							break;
 					}
 
 
-			} //EVENT end switch event loop
-			// DRAWING STARTS HERE
+			}
+		} //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);
+		// clear screen
+		SDL_FillRect(ecran, 0, SDL_MapRGB(ecran->format, 0, 0, 0));
+		// draw bitmap
+		SDL_BlitSurface(optionsFire::level, 0, ecran, &position);
 
-			//Gestion du temps
-			tempsActuel=tempsPrecedent;
-			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();
-		}//End fps
+		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);
@@ -127,7 +128,7 @@ int eventLoop(SDL_Surface* ecran)
 
 	// free loaded bitmap
 	SDL_FreeSurface(optionsFire::level);//Fond
-	optionsFire::level=0;//On pointe le pointeur sur lui, pour la securité
+	optionsFire::level = 0;//On pointe le pointeur sur lui, pour la securité
 
 	return check;
 }