From ff07a6b73e97f37e7bca93ca177a6ca03cf672bb Mon Sep 17 00:00:00 2001 From: avolkov Date: Sun, 22 Nov 2009 23:40:56 +0000 Subject: [PATCH] Cleanup of input wait functions and looped AnyButtonPress() uses git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@3343 8092fc87-c524-0410-9efc-e669fe64eaf9 --- sc2/src/uqm/comm.c | 3 +- sc2/src/uqm/controls.h | 13 +++- sc2/src/uqm/encount.c | 15 +---- sc2/src/uqm/fmv.c | 9 +-- sc2/src/uqm/hyper.c | 2 +- sc2/src/uqm/planets/report.c | 28 +++------ sc2/src/uqm/planets/scan.c | 55 ++++++++--------- sc2/src/uqm/save.c | 10 +--- sc2/src/uqm/util.c | 112 ++++++++++++++++++++--------------- 9 files changed, 117 insertions(+), 130 deletions(-) diff --git a/sc2/src/uqm/comm.c b/sc2/src/uqm/comm.c index 88550d5da..a98b6c11a 100644 --- a/sc2/src/uqm/comm.c +++ b/sc2/src/uqm/comm.c @@ -678,8 +678,7 @@ DoTalkSegue (COUNT wait_track) static void FlushTalkSegue (void) { - WaitForNoInput (ONE_SECOND / 2); - FlushInput (); + WaitForNoInput (ONE_SECOND / 2, TRUE); // Wait until the animation task stops "talking" do diff --git a/sc2/src/uqm/controls.h b/sc2/src/uqm/controls.h index 28195adbf..6653c83aa 100644 --- a/sc2/src/uqm/controls.h +++ b/sc2/src/uqm/controls.h @@ -21,7 +21,7 @@ #include "libs/compiler.h" #include "libs/strlib.h" - +#include "libs/timelib.h" // Enumerated type for controls enum { @@ -102,9 +102,16 @@ void ResetKeyRepeat (void); BOOLEAN PauseGame (void); void SleepGame (void); BOOLEAN DoConfirmExit (void); -BOOLEAN WaitAnyButtonOrQuit (BOOLEAN CheckSpecial); -void WaitForNoInput (SIZE Duration); BOOLEAN ConfirmExit (void); + +#define WAIT_INFINITE ((TimePeriod)-1) +BOOLEAN WaitForAnyButton (BOOLEAN newButton, TimePeriod duration, + BOOLEAN resetInput); +BOOLEAN WaitForAnyButtonUntil (BOOLEAN newButton, TimeCount timeOut, + BOOLEAN resetInput); +BOOLEAN WaitForNoInput (TimePeriod duration, BOOLEAN resetInput); +BOOLEAN WaitForNoInputUntil (TimeCount timeOut, BOOLEAN resetInput); + void DoPopupWindow(const char *msg); void DoInput (void *pInputState, BOOLEAN resetInput); diff --git a/sc2/src/uqm/encount.c b/sc2/src/uqm/encount.c index 8292cefde..43011e0fc 100644 --- a/sc2/src/uqm/encount.c +++ b/sc2/src/uqm/encount.c @@ -437,7 +437,6 @@ UninitEncounter (void) else { BOOLEAN Sleepy; - DWORD Time; SIZE VictoryState; COUNT RecycleAmount = 0; SIZE i; @@ -625,7 +624,7 @@ UninitEncounter (void) if (Sleepy) { - Time = GetTimeCounter (); + TimeCount Time = GetTimeCounter (); for (j = 0; j < NUM_SHIP_FADES; ++j) { UnlockMutex (GraphicsLock); @@ -664,12 +663,8 @@ UninitEncounter (void) DestroyDrawable (ReleaseDrawable (s.frame)); #endif /* NEVER */ - FlushInput (); - Time = GetTimeCounter () + (ONE_SECOND * 3); UnlockMutex (GraphicsLock); - // TODO: handle rapid quit - while (!(AnyButtonPress (TRUE)) && GetTimeCounter () < Time) - TaskSwitch (); + WaitForAnyButton (TRUE, ONE_SECOND * 3, FALSE); LockMutex (GraphicsLock); if (!CurrentInputState.key[PlayerControls[0]][KEY_ESCAPE]) { @@ -696,12 +691,8 @@ UninitEncounter (void) str2 = GAME_STRING (ENCOUNTER_STRING_BASE + 7); // "Scavenged" DrawFadeText (str1, str2, TRUE, &scavenge_r); - Time = GetTimeCounter () + ONE_SECOND * 2; UnlockMutex (GraphicsLock); - // TODO: handle rapid quit - while (!(AnyButtonPress (TRUE)) - && GetTimeCounter () < Time) - TaskSwitch (); + WaitForAnyButton (TRUE, ONE_SECOND * 2, FALSE); LockMutex (GraphicsLock); if (!CurrentInputState.key[PlayerControls[0]][KEY_ESCAPE]) DrawFadeText (str1, str2, FALSE, &scavenge_r); diff --git a/sc2/src/uqm/fmv.c b/sc2/src/uqm/fmv.c index 8ef9751b6..06a8445a1 100644 --- a/sc2/src/uqm/fmv.c +++ b/sc2/src/uqm/fmv.c @@ -87,7 +87,6 @@ SplashScreen (void (* DoProcessing)(DWORD TimeOut)) BYTE xform_buf[1]; STAMP s; DWORD TimeOut; - BOOLEAN InputState; xform_buf[0] = FadeAllToBlack; SleepThreadUntil (XFormColorMap ( @@ -117,13 +116,7 @@ SplashScreen (void (* DoProcessing)(DWORD TimeOut)) * with the proper operation of the quit operation. * --Michael */ - TimeOut += ONE_SECOND * 3; - while (!(InputState = AnyButtonPress (FALSE)) && - (GetTimeCounter () <= TimeOut) && - !(GLOBAL (CurrentActivity) & CHECK_ABORT)) - { - TaskSwitch (); - } + WaitForAnyButton (FALSE, ONE_SECOND * 3, TRUE); if (GLOBAL (CurrentActivity) & CHECK_ABORT) { return; diff --git a/sc2/src/uqm/hyper.c b/sc2/src/uqm/hyper.c index 3dd06996a..66617a368 100644 --- a/sc2/src/uqm/hyper.c +++ b/sc2/src/uqm/hyper.c @@ -1634,7 +1634,7 @@ UnbatchGraphics (); { ClearSISRect (CLEAR_SIS_RADAR); UnlockMutex (GraphicsLock); - WaitForNoInput (ONE_SECOND / 2); + WaitForNoInput (ONE_SECOND / 2, FALSE); LockMutex (GraphicsLock); } diff --git a/sc2/src/uqm/planets/report.c b/sc2/src/uqm/planets/report.c index d9a61e2b0..ef17e81d5 100644 --- a/sc2/src/uqm/planets/report.c +++ b/sc2/src/uqm/planets/report.c @@ -151,7 +151,7 @@ MakeReport (SOUND ReadOutSounds, UNICODE *pStr, COUNT StrLen) word_chars = utf8StringCountN (t.pStr, pStr); if ((col_cells += word_chars) <= NUM_CELL_COLS) { - DWORD TimeOut; + TimeCount TimeOut; if (StrLen -= word_chars) --StrLen; @@ -181,22 +181,12 @@ MakeReport (SOUND ReadOutSounds, UNICODE *pStr, COUNT StrLen) if (word_chars == 0) TimeOut += ONE_SECOND / 20; - TaskSwitch (); - while (GetTimeCounter () < TimeOut) + if (WaitForAnyButtonUntil (TRUE, TimeOut, FALSE)) { - if (ButtonState) - { - if (!AnyButtonPress (TRUE)) - ButtonState = 0; - } - else if (AnyButtonPress (TRUE)) - { - Sleepy = FALSE; - LockMutex (GraphicsLock); - BatchGraphics (); - break; - } - TaskSwitch(); + Sleepy = FALSE; + // We draw the whole thing at once after this + LockMutex (GraphicsLock); + BatchGraphics (); } } t.pStr = pNextStr; @@ -219,7 +209,7 @@ MakeReport (SOUND ReadOutSounds, UNICODE *pStr, COUNT StrLen) UnlockMutex (GraphicsLock); } - if (WaitAnyButtonOrQuit (TRUE)) + if (!WaitForAnyButton (TRUE, WAIT_INFINITE, FALSE)) break; InitPageCell: @@ -289,9 +279,7 @@ DoDiscoveryReport (SOUND ReadOutSounds) DestroyContext (context); UnlockMutex (GraphicsLock); - FlushInput (); - while (AnyButtonPress (TRUE)) - TaskSwitch (); + WaitForNoInput (WAIT_INFINITE, TRUE); LockMutex (GraphicsLock); } diff --git a/sc2/src/uqm/planets/scan.c b/sc2/src/uqm/planets/scan.c index 20c9fb1c1..6d77c3eda 100644 --- a/sc2/src/uqm/planets/scan.c +++ b/sc2/src/uqm/planets/scan.c @@ -928,7 +928,10 @@ callGenerateForScanType (SOLARSYS_STATE *solarSys, PLANET_DESC *world, static BOOLEAN DoScan (MENU_STATE *pMS) { - DWORD TimeIn, WaitTime; +#define SCAN_DURATION (ONE_SECOND * 7 / 4) +// NUM_FLASH_COLORS for flashing blips; 1 for the final frame +#define SCAN_LINES (MAP_HEIGHT + NUM_FLASH_COLORS + 1) +#define SCAN_LINE_WAIT (SCAN_DURATION / SCAN_LINES) BOOLEAN select, cancel; select = PulsedInputState.menu[KEY_MENU_SELECT]; @@ -959,7 +962,6 @@ DoScan (MENU_STATE *pMS) { BYTE min_scan, max_scan; RECT r; - BOOLEAN PressState, ButtonState; if (pMS->CurState == DISPATCH_SHUTTLE) { @@ -1030,7 +1032,7 @@ DoScan (MENU_STATE *pMS) max_scan = BIOLOGICAL_SCAN; } - do + for ( ; min_scan <= max_scan; ++min_scan) { TEXT t; SWORD i; @@ -1078,6 +1080,7 @@ DoScan (MENU_STATE *pMS) { DWORD rgb; + TimeCount TimeOut; switch (min_scan) { @@ -1092,45 +1095,43 @@ DoScan (MENU_STATE *pMS) break; } + // Draw a virgin surface LockMutex (GraphicsLock); BatchGraphics (); DrawPlanet (0, 0, 0, 0); UnbatchGraphics (); UnlockMutex (GraphicsLock); - PressState = AnyButtonPress (TRUE); - WaitTime = (ONE_SECOND << 1) / MAP_HEIGHT; -// LockMutex (GraphicsLock); - TimeIn = GetTimeCounter (); - for (i = 0; i < MAP_HEIGHT + NUM_FLASH_COLORS + 1; i++) + // Draw the scan slowly line by line + TimeOut = GetTimeCounter (); + for (i = 0; i < SCAN_LINES; i++) { - ButtonState = AnyButtonPress (TRUE); - if (PressState) - { - PressState = ButtonState; - ButtonState = FALSE; - } - if (ButtonState) - i = -i; + TimeOut += SCAN_LINE_WAIT; + if (WaitForAnyButtonUntil (TRUE, TimeOut, FALSE)) + break; + LockMutex (GraphicsLock); BatchGraphics (); DrawPlanet (0, 0, i, rgb); - if (i < 0) - i = MAP_HEIGHT + NUM_FLASH_COLORS; - if (pMS->delta_item) - DrawScannedStuff (i, min_scan); + DrawScannedStuff (i, min_scan); UnbatchGraphics (); UnlockMutex (GraphicsLock); -// FlushGraphics (); - SleepThreadUntil (TimeIn + WaitTime); - TimeIn = GetTimeCounter (); } -// UnlockMutex (GraphicsLock); + + if (i < SCAN_LINES) + { // Aborted by a keypress; draw in finished state + LockMutex (GraphicsLock); + BatchGraphics (); + // dy < 0 means "from dy to the end" + DrawPlanet (0, 0, -i, rgb); + DrawScannedStuff (SCAN_LINES - 1, min_scan); + UnbatchGraphics (); + UnlockMutex (GraphicsLock); + } + pSolarSysState->Tint_rgb = 0; - } - - } while (++min_scan <= max_scan); + } LockMutex (GraphicsLock); SetContext (SpaceContext); diff --git a/sc2/src/uqm/save.c b/sc2/src/uqm/save.c index 2e60eb612..fd98b3261 100644 --- a/sc2/src/uqm/save.c +++ b/sc2/src/uqm/save.c @@ -606,15 +606,7 @@ SaveProblem (void) FlushGraphics (); UnlockMutex (GraphicsLock); - while (AnyButtonPress (FALSE)) - ; - do - { - TaskSwitch (); - UpdateInputState (); - } while (!(PulsedInputState.menu[KEY_MENU_SELECT] || - PulsedInputState.menu[KEY_MENU_SPECIAL] || - (GLOBAL (CurrentActivity) & CHECK_ABORT))); + WaitForAnyButton (TRUE, WAIT_INFINITE, FALSE); LockMutex (GraphicsLock); BatchGraphics (); diff --git a/sc2/src/uqm/util.c b/sc2/src/uqm/util.c index c57d3d4cd..5d3f18ae5 100644 --- a/sc2/src/uqm/util.c +++ b/sc2/src/uqm/util.c @@ -111,39 +111,6 @@ SeedRandomNumbers (void) return (cur_time); } -void -WaitForNoInput (SIZE Duration) -{ - BOOLEAN PressState; - - PressState = AnyButtonPress (FALSE); - if (Duration < 0) - { - if (PressState) - return; - Duration = -Duration; - } - else if (!PressState) - return; - - { - DWORD TimeOut; - BOOLEAN ButtonState; - - TimeOut = GetTimeCounter () + Duration; - do - { - ButtonState = AnyButtonPress (FALSE); - if (PressState) - { - PressState = ButtonState; - ButtonState = 0; - } - } while (!ButtonState && - (TaskSwitch (), GetTimeCounter ()) <= TimeOut); - } -} - BOOLEAN PauseGame (void) { @@ -215,8 +182,7 @@ PauseGame (void) SetFrameHot (Screen, OldHot); SetContext (OldContext); - WaitForNoInput (ONE_SECOND / 4); - FlushInput (); + WaitForNoInput (ONE_SECOND / 4, TRUE); if (PlayingTrack ()) ResumeTrack (); @@ -228,22 +194,73 @@ PauseGame (void) return (TRUE); } -// Waits for a new button to be pressed -// and returns TRUE if Quit was selected +// Waits for a button to be pressed +// Returns TRUE if the wait succeeded (found input) +// FALSE if timed out or game aborted BOOLEAN -WaitAnyButtonOrQuit (BOOLEAN CheckSpecial) +WaitForAnyButtonUntil (BOOLEAN newButton, TimeCount timeOut, + BOOLEAN resetInput) { - while (AnyButtonPress (TRUE)) - TaskSwitch (); + BOOLEAN buttonPressed; - while (!AnyButtonPress (TRUE) && - !(GLOBAL (CurrentActivity) & CHECK_ABORT)) - TaskSwitch (); + if (newButton && !WaitForNoInputUntil (timeOut, FALSE)) + return FALSE; - /* Satisfy unused parameter */ - (void) CheckSpecial; + buttonPressed = AnyButtonPress (TRUE); + while (!buttonPressed + && (timeOut == WAIT_INFINITE || GetTimeCounter () < timeOut) + && !(GLOBAL (CurrentActivity) & CHECK_ABORT) + && !QuitPosted) + { + SleepThread (ONE_SECOND / 40); + buttonPressed = AnyButtonPress (TRUE); + } - return (GLOBAL (CurrentActivity) & CHECK_ABORT) != 0; + if (resetInput) + FlushInput (); + + return buttonPressed; +} + +BOOLEAN +WaitForAnyButton (BOOLEAN newButton, TimePeriod duration, BOOLEAN resetInput) +{ + TimeCount timeOut = duration; + if (duration != WAIT_INFINITE) + timeOut += GetTimeCounter (); + return WaitForAnyButtonUntil (newButton, timeOut, resetInput); +} + +// Returns TRUE if the wait succeeded (found no input) +// FALSE if timed out or game aborted +BOOLEAN +WaitForNoInputUntil (TimeCount timeOut, BOOLEAN resetInput) +{ + BOOLEAN buttonPressed; + + buttonPressed = AnyButtonPress (TRUE); + while (buttonPressed + && (timeOut == WAIT_INFINITE || GetTimeCounter () < timeOut) + && !(GLOBAL (CurrentActivity) & CHECK_ABORT) + && !QuitPosted) + { + SleepThread (ONE_SECOND / 40); + buttonPressed = AnyButtonPress (TRUE); + } + + if (resetInput) + FlushInput (); + + return !buttonPressed; +} + +BOOLEAN +WaitForNoInput (TimePeriod duration, BOOLEAN resetInput) +{ + TimeCount timeOut = duration; + if (duration != WAIT_INFINITE) + timeOut += GetTimeCounter (); + return WaitForNoInputUntil (timeOut, resetInput); } // Stops game clock and music thread and minimizes interrupts/cycles @@ -268,8 +285,7 @@ SleepGame (void) log_add (log_Debug, "Game is waking up"); - WaitForNoInput (ONE_SECOND / 10); - FlushInput (); + WaitForNoInput (ONE_SECOND / 10, TRUE); ResumeMusic ();