From 74211a0825cb5b9efd20c40c4bc2308c6c25fa92 Mon Sep 17 00:00:00 2001 From: Allanis Date: Tue, 19 Feb 2013 11:37:38 +0000 Subject: [PATCH] [Add] Pause game when window is not focused. Introduced a small 10ms delay when paused to lower CPU usage. --- src/main.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/main.c b/src/main.c index 43e6e06..e6ebed4 100644 --- a/src/main.c +++ b/src/main.c @@ -148,6 +148,15 @@ int main(int argc, char** argv) { while(SDL_PollEvent(&event)) { 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. } @@ -192,6 +201,8 @@ static void fps_control(void) { // dt in ms/1000. dt = (double)(SDL_GetTicks() - gtime) / 1000.; gtime = SDL_GetTicks(); + + if(paused) SDL_Delay(10); // Drop paused FPS to be nice to the CPU. // If the fps is limited.. if((max_fps != 0) && (dt < 1./max_fps)) {