From ee211f3b747e236038032ce562039fe0a78e1b2c Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Sun, 19 Jan 2003 11:00:53 +0000 Subject: [PATCH] restore app priority to normal when we don't have focus and give up CPU each frame --- stepmania/src/StepMania.cpp | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/stepmania/src/StepMania.cpp b/stepmania/src/StepMania.cpp index f92aef5203..3f60f6cf80 100644 --- a/stepmania/src/StepMania.cpp +++ b/stepmania/src/StepMania.cpp @@ -71,6 +71,8 @@ HWND g_hWndMain = NULL; #endif +static bool g_bHasFocus = true; + #include @@ -165,17 +167,25 @@ static void SetIcon() SDL_FreeSurface(srf); } -static void BoostAppPri() +static bool ChangeAppPri() { if(PREFSMAN->m_iBoostAppPriority == 0) - return; + return false; /* If -1 and this is a debug build, don't. It makes the debugger sluggish. */ #ifdef DEBUG if(PREFSMAN->m_iBoostAppPriority == -1) - return; + return false; #endif + return true; +} + +static void BoostAppPri() +{ + if(!ChangeAppPri()) + return; + #ifdef WIN32 /* We just want a slight boost, so we don't skip needlessly if something happens * in the background. We don't really want to be high-priority--above normal should @@ -199,6 +209,14 @@ static void BoostAppPri() #endif } +static void RestoreAppPri() +{ + if(!ChangeAppPri()) + return; + + SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS); +} + int main(int argc, char* argv[]) { ChangeToDirOfExecutable(argv[0]); @@ -451,6 +469,14 @@ static void GameLoop() case SDL_VIDEORESIZE: DISPLAY->ResolutionChanged(event.resize.w, event.resize.h); break; + case SDL_ACTIVEEVENT: + g_bHasFocus = !!event.active.gain; + LOG->Trace("App %s focus", g_bHasFocus? "has":"doesn't have"); + + if(event.active.gain) + BoostAppPri(); + else + RestoreAppPri(); } } @@ -483,7 +509,10 @@ static void GameLoop() DISPLAY->Flip(); - ::Sleep( 0 ); // give some time to other processes and threads + if(g_bHasFocus) + ::Sleep( 0 ); // give some time to other processes and threads + else + ::Sleep( 2 ); // give more time to other processes and threads, but not so much that we skip sound } }