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.
This commit is contained in:
Daniel Gibson 2013-01-03 12:39:16 +01:00
parent 732d8987d3
commit a405b37f13
6 changed files with 42 additions and 15 deletions

View file

@ -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 );

View file

@ -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

View file

@ -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
}
/*

View file

@ -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;

View file

@ -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;
}
/*

View file

@ -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