From a405b37f13907a06f1e7b96fa562ab860092c081 Mon Sep 17 00:00:00 2001 From: Daniel Gibson Date: Thu, 3 Jan 2013 12:39:16 +0100 Subject: [PATCH] Pause when window loses focus, introduce com_pause If the window loses focus com_pause is set to 1, when it regains focus it's set to 0. The behaviour on Win32 stayed the same (the implementation is less hacky though) and Linux now matchces that. --- neo/framework/Common.cpp | 4 ++++ neo/framework/common_frame.cpp | 22 +++++++++++++++++----- neo/sys/posix/posix_session_local.cpp | 5 ++--- neo/sys/sdl/sdl_events.cpp | 18 ++++++++++++------ neo/sys/win32/win_session_local.cpp | 4 +++- neo/sys/win32/win_wndproc.cpp | 4 ++++ 6 files changed, 42 insertions(+), 15 deletions(-) diff --git a/neo/framework/Common.cpp b/neo/framework/Common.cpp index 62bfabb4..21ab1960 100644 --- a/neo/framework/Common.cpp +++ b/neo/framework/Common.cpp @@ -86,6 +86,10 @@ idCVar preload_CommonAssets( "preload_CommonAssets", "1", CVAR_SYSTEM | CVAR_BOO idCVar net_inviteOnly( "net_inviteOnly", "1", CVAR_BOOL | CVAR_ARCHIVE, "whether or not the private server you create allows friends to join or invite only" ); +// DG: add cvar for pause +idCVar com_pause( "com_pause", "0", CVAR_BOOL | CVAR_SYSTEM , "set to 1 to pause game, to 0 to unpause again" ); +// DG end + extern idCVar g_demoMode; idCVar com_engineHz( "com_engineHz", "60", CVAR_FLOAT | CVAR_ARCHIVE, "Frames per second the engine runs at", 10.0f, 1024.0f ); diff --git a/neo/framework/common_frame.cpp b/neo/framework/common_frame.cpp index a1968f50..a63f0053 100644 --- a/neo/framework/common_frame.cpp +++ b/neo/framework/common_frame.cpp @@ -438,6 +438,8 @@ void idCommonLocal::ProcessGameReturn( const gameReturn_t& ret ) extern idCVar com_forceGenericSIMD; +extern idCVar com_pause; + /* ================= idCommonLocal::Frame @@ -485,13 +487,16 @@ void idCommonLocal::Frame() // if the console or another gui is down, we don't need to hold the mouse cursor bool chatting = false; + // DG: Add pause from com_pause cvar // RB begin #if defined(USE_DOOMCLASSIC) - if( console->Active() || Dialog().IsDialogActive() || session->IsSystemUIShowing() || ( game && game->InhibitControls() && !IsPlayingDoomClassic() ) ) + if( console->Active() || Dialog().IsDialogActive() || session->IsSystemUIShowing() + || ( game && game->InhibitControls() && !IsPlayingDoomClassic() ) ) #else - if( console->Active() || Dialog().IsDialogActive() || session->IsSystemUIShowing() || ( game && game->InhibitControls() ) ) + if( com_pause.GetInteger() || console->Active() || Dialog().IsDialogActive() || session->IsSystemUIShowing() + || ( game && game->InhibitControls() ) ) #endif - // RB end + // RB end, DG end { Sys_GrabMouseCursor( false ); usercmdGen->InhibitUsercmd( INHIBIT_SESSION, true ); @@ -505,9 +510,16 @@ void idCommonLocal::Frame() // RB begin #if defined(USE_DOOMCLASSIC) - const bool pauseGame = ( !mapSpawned || ( !IsMultiplayer() && ( Dialog().IsDialogPausing() || session->IsSystemUIShowing() || ( game && game->Shell_IsActive() ) ) ) ) && !IsPlayingDoomClassic(); + const bool pauseGame = ( !mapSpawned + || ( !IsMultiplayer() + && ( Dialog().IsDialogPausing() || session->IsSystemUIShowing() + || ( game && game->Shell_IsActive() ) || com_pause.GetInteger() ) ) ) + && !IsPlayingDoomClassic(); #else - const bool pauseGame = ( !mapSpawned || ( !IsMultiplayer() && ( Dialog().IsDialogPausing() || session->IsSystemUIShowing() || ( game && game->Shell_IsActive() ) ) ) ); + const bool pauseGame = ( !mapSpawned + || ( !IsMultiplayer() + && ( Dialog().IsDialogPausing() || session->IsSystemUIShowing() + || ( game && game->Shell_IsActive() ) || com_pause.GetInteger() ) ) ); #endif // RB end diff --git a/neo/sys/posix/posix_session_local.cpp b/neo/sys/posix/posix_session_local.cpp index 404f4670..cca9802e 100644 --- a/neo/sys/posix/posix_session_local.cpp +++ b/neo/sys/posix/posix_session_local.cpp @@ -456,10 +456,9 @@ idSessionLocalWin::IsSystemUIShowing */ bool idSessionLocalWin::IsSystemUIShowing() const { - // RB: TODO track SDL_ACTIVEENT + // DG: pausing here when window is out of focus like originally done on windows is hacky + // it's done with com_pause now. return isSysUIShowing; - - //return !win32.activeApp || isSysUIShowing; // If the user alt+tabs away, treat it the same as bringing up the steam overlay } /* diff --git a/neo/sys/sdl/sdl_events.cpp b/neo/sys/sdl/sdl_events.cpp index dd0a4bbc..e0e4ca21 100644 --- a/neo/sys/sdl/sdl_events.cpp +++ b/neo/sys/sdl/sdl_events.cpp @@ -741,14 +741,18 @@ sysEvent_t Sys_GetEvent() newmod |= KMOD_CAPS; SDL_SetModState( ( SDL_Keymod )newmod ); - // DG: disabling the cursor is now done once in GLimp_Init() because it should always be disabled - GLimp_GrabInput( GRAB_ENABLE | GRAB_REENABLE ); + + // DG: un-pause the game when focus is gained, that also re-grabs the input + // disabling the cursor is now done once in GLimp_Init() because it should always be disabled + cvarSystem->SetCVarBool( "com_pause", false ); // DG end break; } case SDL_WINDOWEVENT_FOCUS_LOST: - GLimp_GrabInput( 0 ); + // DG: pause the game when focus is lost, that also un-grabs the input + cvarSystem->SetCVarBool( "com_pause", true ); + // DG end break; // DG: handle resizing and moving of window @@ -779,11 +783,13 @@ sysEvent_t Sys_GetEvent() #else case SDL_ACTIVEEVENT: { - int flags = 0; + // DG: (un-)pause the game when focus is gained, that also (un-)grabs the input + bool pause = true; if( ev.active.gain ) { - flags = GRAB_ENABLE | GRAB_REENABLE | GRAB_HIDECURSOR; + + pause = false; // unset modifier, in case alt-tab was used to leave window and ALT is still set // as that can cause fullscreen-toggling when pressing enter... @@ -795,7 +801,7 @@ sysEvent_t Sys_GetEvent() SDL_SetModState( ( SDLMod )newmod ); } - GLimp_GrabInput( flags ); + cvarSystem->SetCVarBool( "com_pause", pause ); } return res_none; diff --git a/neo/sys/win32/win_session_local.cpp b/neo/sys/win32/win_session_local.cpp index 3b175eb7..b76b065b 100644 --- a/neo/sys/win32/win_session_local.cpp +++ b/neo/sys/win32/win_session_local.cpp @@ -456,7 +456,9 @@ idSessionLocalWin::IsSystemUIShowing */ bool idSessionLocalWin::IsSystemUIShowing() const { - return !win32.activeApp || isSysUIShowing; // If the user alt+tabs away, treat it the same as bringing up the steam overlay + // DG: wtf, !win32.activeApp doesn't belong here, this is totally confusing and hacky. + // pause (when losing focus or invoking explicitly) is now handled properly by com_pause + return isSysUIShowing; } /* diff --git a/neo/sys/win32/win_wndproc.cpp b/neo/sys/win32/win_wndproc.cpp index 680b467e..5eec81eb 100644 --- a/neo/sys/win32/win_wndproc.cpp +++ b/neo/sys/win32/win_wndproc.cpp @@ -288,6 +288,10 @@ LONG WINAPI MainWndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) // start playing the game sound world soundSystem->SetMute( !win32.activeApp ); + // DG: set com_pause so game pauses when focus is lost + // and continues when focus is regained + cvarSystem->SetCVarBool( "com_pause", !win32.activeApp ); + // DG end // we do not actually grab or release the mouse here, // that will be done next time through the main loop