[Add] Pause game when window is not focused. Introduced a small 10ms delay when paused to lower CPU usage.

This commit is contained in:
Allanis 2013-02-19 11:37:38 +00:00
parent f66f644411
commit 74211a0825

View File

@ -148,6 +148,15 @@ int main(int argc, char** argv) {
while(SDL_PollEvent(&event)) { while(SDL_PollEvent(&event)) {
if(event.type == SDL_QUIT) quit = 1; // Handle quit. if(event.type == SDL_QUIT) quit = 1; // Handle quit.
// Pause the game if it is unfocused.
if(event.type == SDL_ACTIVEEVENT) {
if(event.active.state != SDL_APPMOUSEFOCUS) {
// We don't need the mouse focus.
if((event.active.gain == 0) && !paused) pause();
else if((event.active.gain == 1) && paused) unpause();
}
}
input_handle(&event); // handles all the events the player keybinds. input_handle(&event); // handles all the events the player keybinds.
} }
@ -192,6 +201,8 @@ static void fps_control(void) {
// dt in ms/1000. // dt in ms/1000.
dt = (double)(SDL_GetTicks() - gtime) / 1000.; dt = (double)(SDL_GetTicks() - gtime) / 1000.;
gtime = SDL_GetTicks(); gtime = SDL_GetTicks();
if(paused) SDL_Delay(10); // Drop paused FPS to be nice to the CPU.
// If the fps is limited.. // If the fps is limited..
if((max_fps != 0) && (dt < 1./max_fps)) { if((max_fps != 0) && (dt < 1./max_fps)) {