[Add] Pause game when window is not focused. Introduced a small 10ms delay when paused to lower CPU usage.
This commit is contained in:
parent
f66f644411
commit
74211a0825
11
src/main.c
11
src/main.c
@ -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.
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -193,6 +202,8 @@ static void fps_control(void) {
|
|||||||
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)) {
|
||||||
double delay = 1./max_fps - dt;
|
double delay = 1./max_fps - dt;
|
||||||
|
Loading…
Reference in New Issue
Block a user