From: <av...@us...> - 2009-11-22 23:41:05
|
Revision: 3343 http://sc2.svn.sourceforge.net/sc2/?rev=3343&view=rev Author: avolkov Date: 2009-11-22 23:40:56 +0000 (Sun, 22 Nov 2009) Log Message: ----------- Cleanup of input wait functions and looped AnyButtonPress() uses Modified Paths: -------------- trunk/sc2/src/uqm/comm.c trunk/sc2/src/uqm/controls.h trunk/sc2/src/uqm/encount.c trunk/sc2/src/uqm/fmv.c trunk/sc2/src/uqm/hyper.c trunk/sc2/src/uqm/planets/report.c trunk/sc2/src/uqm/planets/scan.c trunk/sc2/src/uqm/save.c trunk/sc2/src/uqm/util.c Modified: trunk/sc2/src/uqm/comm.c =================================================================== --- trunk/sc2/src/uqm/comm.c 2009-11-22 17:58:28 UTC (rev 3342) +++ trunk/sc2/src/uqm/comm.c 2009-11-22 23:40:56 UTC (rev 3343) @@ -678,8 +678,7 @@ static void FlushTalkSegue (void) { - WaitForNoInput (ONE_SECOND / 2); - FlushInput (); + WaitForNoInput (ONE_SECOND / 2, TRUE); // Wait until the animation task stops "talking" do Modified: trunk/sc2/src/uqm/controls.h =================================================================== --- trunk/sc2/src/uqm/controls.h 2009-11-22 17:58:28 UTC (rev 3342) +++ trunk/sc2/src/uqm/controls.h 2009-11-22 23:40:56 UTC (rev 3343) @@ -21,8 +21,8 @@ #include "libs/compiler.h" #include "libs/strlib.h" +#include "libs/timelib.h" - // Enumerated type for controls enum { KEY_UP, @@ -102,9 +102,16 @@ 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); Modified: trunk/sc2/src/uqm/encount.c =================================================================== --- trunk/sc2/src/uqm/encount.c 2009-11-22 17:58:28 UTC (rev 3342) +++ trunk/sc2/src/uqm/encount.c 2009-11-22 23:40:56 UTC (rev 3343) @@ -437,7 +437,6 @@ else { BOOLEAN Sleepy; - DWORD Time; SIZE VictoryState; COUNT RecycleAmount = 0; SIZE i; @@ -625,7 +624,7 @@ if (Sleepy) { - Time = GetTimeCounter (); + TimeCount Time = GetTimeCounter (); for (j = 0; j < NUM_SHIP_FADES; ++j) { UnlockMutex (GraphicsLock); @@ -664,12 +663,8 @@ 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 @@ 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); Modified: trunk/sc2/src/uqm/fmv.c =================================================================== --- trunk/sc2/src/uqm/fmv.c 2009-11-22 17:58:28 UTC (rev 3342) +++ trunk/sc2/src/uqm/fmv.c 2009-11-22 23:40:56 UTC (rev 3343) @@ -87,7 +87,6 @@ BYTE xform_buf[1]; STAMP s; DWORD TimeOut; - BOOLEAN InputState; xform_buf[0] = FadeAllToBlack; SleepThreadUntil (XFormColorMap ( @@ -117,13 +116,7 @@ * 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; Modified: trunk/sc2/src/uqm/hyper.c =================================================================== --- trunk/sc2/src/uqm/hyper.c 2009-11-22 17:58:28 UTC (rev 3342) +++ trunk/sc2/src/uqm/hyper.c 2009-11-22 23:40:56 UTC (rev 3343) @@ -1634,7 +1634,7 @@ { ClearSISRect (CLEAR_SIS_RADAR); UnlockMutex (GraphicsLock); - WaitForNoInput (ONE_SECOND / 2); + WaitForNoInput (ONE_SECOND / 2, FALSE); LockMutex (GraphicsLock); } Modified: trunk/sc2/src/uqm/planets/report.c =================================================================== --- trunk/sc2/src/uqm/planets/report.c 2009-11-22 17:58:28 UTC (rev 3342) +++ trunk/sc2/src/uqm/planets/report.c 2009-11-22 23:40:56 UTC (rev 3343) @@ -151,7 +151,7 @@ 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 @@ 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 @@ UnlockMutex (GraphicsLock); } - if (WaitAnyButtonOrQuit (TRUE)) + if (!WaitForAnyButton (TRUE, WAIT_INFINITE, FALSE)) break; InitPageCell: @@ -289,9 +279,7 @@ DestroyContext (context); UnlockMutex (GraphicsLock); - FlushInput (); - while (AnyButtonPress (TRUE)) - TaskSwitch (); + WaitForNoInput (WAIT_INFINITE, TRUE); LockMutex (GraphicsLock); } Modified: trunk/sc2/src/uqm/planets/scan.c =================================================================== --- trunk/sc2/src/uqm/planets/scan.c 2009-11-22 17:58:28 UTC (rev 3342) +++ trunk/sc2/src/uqm/planets/scan.c 2009-11-22 23:40:56 UTC (rev 3343) @@ -928,7 +928,10 @@ 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 @@ { BYTE min_scan, max_scan; RECT r; - BOOLEAN PressState, ButtonState; if (pMS->CurState == DISPATCH_SHUTTLE) { @@ -1030,7 +1032,7 @@ max_scan = BIOLOGICAL_SCAN; } - do + for ( ; min_scan <= max_scan; ++min_scan) { TEXT t; SWORD i; @@ -1078,6 +1080,7 @@ { DWORD rgb; + TimeCount TimeOut; switch (min_scan) { @@ -1092,46 +1095,44 @@ 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); r.corner.x = 0; Modified: trunk/sc2/src/uqm/save.c =================================================================== --- trunk/sc2/src/uqm/save.c 2009-11-22 17:58:28 UTC (rev 3342) +++ trunk/sc2/src/uqm/save.c 2009-11-22 23:40:56 UTC (rev 3343) @@ -606,15 +606,7 @@ 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 (); Modified: trunk/sc2/src/uqm/util.c =================================================================== --- trunk/sc2/src/uqm/util.c 2009-11-22 17:58:28 UTC (rev 3342) +++ trunk/sc2/src/uqm/util.c 2009-11-22 23:40:56 UTC (rev 3343) @@ -111,39 +111,6 @@ 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 @@ SetFrameHot (Screen, OldHot); SetContext (OldContext); - WaitForNoInput (ONE_SECOND / 4); - FlushInput (); + WaitForNoInput (ONE_SECOND / 4, TRUE); if (PlayingTrack ()) ResumeTrack (); @@ -228,24 +194,75 @@ 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 // based on value of global GameActive variable // See similar sleep state for main thread in uqm.c:main() @@ -268,8 +285,7 @@ log_add (log_Debug, "Game is waking up"); - WaitForNoInput (ONE_SECOND / 10); - FlushInput (); + WaitForNoInput (ONE_SECOND / 10, TRUE); ResumeMusic (); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |