From d18773e73723063d1bbbd6b5e1a1dd7b8bc66140 Mon Sep 17 00:00:00 2001 From: avolkov Date: Sat, 7 Nov 2009 01:05:08 +0000 Subject: [PATCH] Added Battle frame callback; Moved blinking AUTO-PILOT message out of the game clock task git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@3285 8092fc87-c524-0410-9efc-e669fe64eaf9 --- sc2/src/uqm/battle.c | 7 +- sc2/src/uqm/battle.h | 7 +- sc2/src/uqm/clock.c | 66 ---------------- sc2/src/uqm/encount.c | 2 +- sc2/src/uqm/melee.c | 2 +- sc2/src/uqm/planets/solarsys.c | 4 + sc2/src/uqm/sis.c | 134 +++++++++++++++++++++++---------- sc2/src/uqm/sis.h | 2 + sc2/src/uqm/starcon.c | 11 ++- 9 files changed, 124 insertions(+), 111 deletions(-) diff --git a/sc2/src/uqm/battle.c b/sc2/src/uqm/battle.c index 84d55e958..fae6e2aef 100644 --- a/sc2/src/uqm/battle.c +++ b/sc2/src/uqm/battle.c @@ -331,6 +331,10 @@ DoBattle (BATTLE_STATE *bs) return FALSE; } + // Call the callback function, if set + if (bs->frame_cb) + bs->frame_cb (); + battle_speed = HIBYTE (nth_frame); if (battle_speed == (BYTE)~0) { // maximum speed, nothing rendered at all @@ -393,7 +397,7 @@ selectAllShips (SIZE num_ships) } BOOLEAN -Battle (void) +Battle (BattleFrameCallback *callback) { SIZE num_ships; @@ -463,6 +467,7 @@ Battle (void) } #endif /* NETPLAY */ bs.InputFunc = DoBattle; + bs.frame_cb = callback; bs.first_time = (BOOLEAN)(LOBYTE (GLOBAL (CurrentActivity)) == IN_HYPERSPACE); diff --git a/sc2/src/uqm/battle.h b/sc2/src/uqm/battle.h index 330d385d7..de43242c4 100644 --- a/sc2/src/uqm/battle.h +++ b/sc2/src/uqm/battle.h @@ -26,11 +26,16 @@ typedef DWORD BattleFrameCounter; #include "init.h" // For NUM_SIDES +// The callback function is called on every battle frame +// with GraphicsLock *not* held +typedef void (BattleFrameCallback) (void); + typedef struct battlestate_struct { BOOLEAN (*InputFunc) (struct battlestate_struct *pInputState); COUNT MenuRepeatDelay; BOOLEAN first_time; DWORD NextTime; + BattleFrameCallback *frame_cb; } BATTLE_STATE; extern BYTE battle_counter[NUM_SIDES]; @@ -45,7 +50,7 @@ COUNT GetPlayerOrder (COUNT i); # define GetPlayerOrder(i) (i) #endif -BOOLEAN Battle (void); +BOOLEAN Battle (BattleFrameCallback *); #define BATTLE_FRAME_RATE (ONE_SECOND / 24) diff --git a/sc2/src/uqm/clock.c b/sc2/src/uqm/clock.c index 5a5523e02..c8516d13b 100644 --- a/sc2/src/uqm/clock.c +++ b/sc2/src/uqm/clock.c @@ -61,32 +61,13 @@ DaysInMonth (COUNT month, COUNT year) static int clock_task_func(void* data) { - BOOLEAN LastPilot; - DWORD LastTime; - DWORD cycle_index, delay_count; - static const COLOR cycle_tab[] = - { - BUILD_COLOR (MAKE_RGB15 (0x0A, 0x14, 0x18), 0x5B), - BUILD_COLOR (MAKE_RGB15 (0x06, 0x10, 0x16), 0x5C), - BUILD_COLOR (MAKE_RGB15 (0x03, 0x0E, 0x14), 0x5D), - BUILD_COLOR (MAKE_RGB15 (0x02, 0x0C, 0x11), 0x5E), - BUILD_COLOR (MAKE_RGB15 (0x01, 0x0B, 0x0F), 0x5F), - BUILD_COLOR (MAKE_RGB15 (0x01, 0x09, 0x0D), 0x60), - BUILD_COLOR (MAKE_RGB15 (0x00, 0x07, 0x0B), 0x61), - }; -#define NUM_CYCLES (sizeof (cycle_tab) / sizeof (cycle_tab[0])) -#define NUM_DELAYS (ONE_SECOND * 3 / 40) // 9 @ 120 ticks/second Task task = (Task) data; - LastPilot = FALSE; - LastTime = 0; - cycle_index = delay_count = 0; while (GLOBAL (GameClock).day_in_ticks == 0 && !Task_ReadState (task, TASK_EXIT)) TaskSwitch (); while (!Task_ReadState (task, TASK_EXIT)) { - BOOLEAN OnAutoPilot; DWORD TimeIn; /* use semaphore so that time passage @@ -140,53 +121,6 @@ clock_task_func(void* data) UnlockMutex (GraphicsLock); } - OnAutoPilot = (BOOLEAN)( - (GLOBAL (autopilot.x) != ~0 - && GLOBAL (autopilot.y) != ~0) - || GLOBAL_SIS (FuelOnBoard) == 0 - ); - if (OnAutoPilot || OnAutoPilot != LastPilot) - { - DWORD num_ticks; - - LockMutex (GraphicsLock); - num_ticks = GetTimeCounter () - LastTime; - if (!OnAutoPilot) - { - DrawSISMessage (NULL); - cycle_index = delay_count = 0; - } - else if (delay_count > num_ticks) - { - delay_count -= num_ticks; - } - else - { - if (!(GLOBAL (CurrentActivity) & (CHECK_ABORT)) - && GLOBAL_SIS (CrewEnlisted) != (COUNT)~0) - { - // 2002/11/30 this additional 'if' fixes autopilot indicator blinking on combat/starmap - // TODO: is there a better (more exact) way of determining if player is in starmap menu or not? - if (LOBYTE (GLOBAL (CurrentActivity)) != IN_ENCOUNTER && - (!pMenuState || (pMenuState && pMenuState->InputFunc == DoFlagshipCommands))) - { - CONTEXT OldContext; - - OldContext = SetContext (OffScreenContext); - SetContextForeGroundColor (cycle_tab[cycle_index]); - DrawSISMessage ((UNICODE *)~0L); - SetContext (OldContext); - } - } - - cycle_index = (cycle_index + 1) % NUM_CYCLES; - delay_count = NUM_DELAYS; - } - UnlockMutex (GraphicsLock); - - LastPilot = OnAutoPilot; - LastTime += num_ticks; - } ClearSemaphore (GLOBAL (GameClock.clock_sem)); SleepThreadUntil (TimeIn + ONE_SECOND / 120); diff --git a/sc2/src/uqm/encount.c b/sc2/src/uqm/encount.c index 2f4fbc20f..9bbd69428 100644 --- a/sc2/src/uqm/encount.c +++ b/sc2/src/uqm/encount.c @@ -767,7 +767,7 @@ EncounterBattle (void) GameSounds = CaptureSound (LoadSound (GAME_SOUNDS)); UnlockMutex (GraphicsLock); - Battle (); + Battle (NULL); LockMutex (GraphicsLock); DestroySound (ReleaseSound (GameSounds)); diff --git a/sc2/src/uqm/melee.c b/sc2/src/uqm/melee.c index aabcc244f..afa560ccf 100644 --- a/sc2/src/uqm/melee.c +++ b/sc2/src/uqm/melee.c @@ -1730,7 +1730,7 @@ StartMelee (MELEE_STATE *pMS) load_gravity_well ((BYTE)((COUNT)TFB_Random () % NUMBER_OF_PLANET_TYPES)); - Battle (); + Battle (NULL); free_gravity_well (); ClearPlayerInputAll (); diff --git a/sc2/src/uqm/planets/solarsys.c b/sc2/src/uqm/planets/solarsys.c index 68124ae40..c76563f82 100644 --- a/sc2/src/uqm/planets/solarsys.c +++ b/sc2/src/uqm/planets/solarsys.c @@ -1089,6 +1089,8 @@ static DWORD IP_next_time; void IP_reset (void) { + DrawAutoPilotMessage (TRUE); + if (LastActivity != CHECK_LOAD) { IP_input_state = 0; @@ -1222,6 +1224,8 @@ IP_frame (void) UnbatchGraphics (); } + DrawAutoPilotMessage (FALSE); + UnbatchGraphics (); if (draw_sys_flags & UNBATCH_SYS) diff --git a/sc2/src/uqm/sis.c b/sc2/src/uqm/sis.c index 14b174779..e28eaf4e6 100644 --- a/sc2/src/uqm/sis.c +++ b/sc2/src/uqm/sis.c @@ -185,53 +185,38 @@ DrawSISMessageEx (const UNICODE *pStr, SIZE CurPos, SIZE ExPos, COUNT flags) SetContextBackGroundColor ( BUILD_COLOR (MAKE_RGB15 (0x00, 0x00, 0x14), 0x01)); - if (pStr == (UNICODE *)~0L) + if (pStr == 0) { - if (GLOBAL_SIS (FuelOnBoard) == 0) + switch (LOBYTE (GLOBAL (CurrentActivity))) { - pStr = GAME_STRING (NAVIGATION_STRING_BASE + 2); - // "OUT OF FUEL" - } - else - { - pStr = GAME_STRING (NAVIGATION_STRING_BASE + 3); - // "AUTO-PILOT" + default: + case IN_ENCOUNTER: + pStr = ""; + break; + case IN_LAST_BATTLE: + case IN_INTERPLANETARY: + GetClusterName (CurStarDescPtr, buf); + pStr = buf; + break; + case IN_HYPERSPACE: + if (GET_GAME_STATE (ARILOU_SPACE_SIDE) <= 1) + { + pStr = GAME_STRING (NAVIGATION_STRING_BASE); + // "HyperSpace" + } + else + { + pStr = GAME_STRING (NAVIGATION_STRING_BASE + 1); + // "QuasiSpace" + } + break; } + } - else - { - if (pStr == 0) - { - switch (LOBYTE (GLOBAL (CurrentActivity))) - { - default: - case IN_ENCOUNTER: - pStr = ""; - break; - case IN_LAST_BATTLE: - case IN_INTERPLANETARY: - GetClusterName (CurStarDescPtr, buf); - pStr = buf; - break; - case IN_HYPERSPACE: - if (GET_GAME_STATE (ARILOU_SPACE_SIDE) <= 1) - { - pStr = GAME_STRING (NAVIGATION_STRING_BASE); - // "HyperSpace" - } - else - { - pStr = GAME_STRING (NAVIGATION_STRING_BASE + 1); - // "QuasiSpace" - } - break; - } - - } + if (!(flags & DSME_MYCOLOR)) SetContextForeGroundColor ( BUILD_COLOR (MAKE_RGB15 (0x1B, 0x00, 0x1B), 0x33)); - } t.baseline.y = SIS_MESSAGE_HEIGHT - 2; t.pStr = pStr; @@ -1299,6 +1284,75 @@ CountSISPieces (BYTE piece_type) return (num_pieces); } +void +DrawAutoPilotMessage (BOOLEAN Reset) +{ + static BOOLEAN LastPilot = FALSE; + static TimeCount NextTime = 0; + static DWORD cycle_index = 0; + BOOLEAN OnAutoPilot; + + static const COLOR cycle_tab[] = + { + BUILD_COLOR (MAKE_RGB15 (0x0A, 0x14, 0x18), 0x5B), + BUILD_COLOR (MAKE_RGB15 (0x06, 0x10, 0x16), 0x5C), + BUILD_COLOR (MAKE_RGB15 (0x03, 0x0E, 0x14), 0x5D), + BUILD_COLOR (MAKE_RGB15 (0x02, 0x0C, 0x11), 0x5E), + BUILD_COLOR (MAKE_RGB15 (0x01, 0x0B, 0x0F), 0x5F), + BUILD_COLOR (MAKE_RGB15 (0x01, 0x09, 0x0D), 0x60), + BUILD_COLOR (MAKE_RGB15 (0x00, 0x07, 0x0B), 0x61), + }; +#define NUM_CYCLES (sizeof (cycle_tab) / sizeof (cycle_tab[0])) +#define BLINK_RATE (ONE_SECOND * 3 / 40) // 9 @ 120 ticks/second + + + if (Reset) + { // Just a reset, not drawing + LastPilot = FALSE; + return; + } + + OnAutoPilot = (GLOBAL (autopilot.x) != ~0 && GLOBAL (autopilot.y) != ~0) + || GLOBAL_SIS (FuelOnBoard) == 0; + + if (OnAutoPilot || LastPilot) + { + if (!OnAutoPilot) + { // AutiPilot aborted -- clear the AUTO-PILOT message + DrawSISMessage (NULL); + cycle_index = 0; + } + else if (GetTimeCounter () >= NextTime) + { + if (!(GLOBAL (CurrentActivity) & CHECK_ABORT) + && GLOBAL_SIS (CrewEnlisted) != (COUNT)~0) + { + CONTEXT OldContext; + + OldContext = SetContext (OffScreenContext); + SetContextForeGroundColor (cycle_tab[cycle_index]); + if (GLOBAL_SIS (FuelOnBoard) == 0) + { + DrawSISMessageEx (GAME_STRING (NAVIGATION_STRING_BASE + 2), + -1, -1, DSME_MYCOLOR); // "OUT OF FUEL" + } + else + { + DrawSISMessageEx (GAME_STRING (NAVIGATION_STRING_BASE + 3), + -1, -1, DSME_MYCOLOR); // "AUTO-PILOT" + } + SetContext (OldContext); + } + + cycle_index = (cycle_index + 1) % NUM_CYCLES; + NextTime = GetTimeCounter () + BLINK_RATE; + } + + LastPilot = OnAutoPilot; + } +} + + Task flash_task = 0; RECT flash_rect; static FRAME flash_screen_frame = 0; diff --git a/sc2/src/uqm/sis.h b/sc2/src/uqm/sis.h index 0dfb3c611..08b38c53e 100644 --- a/sc2/src/uqm/sis.h +++ b/sc2/src/uqm/sis.h @@ -272,6 +272,7 @@ extern BOOLEAN DrawSISMessageEx (const UNICODE *pStr, SIZE CurPos, #define DSME_SETFR (1 << 0) #define DSME_CLEARFR (1 << 1) #define DSME_BLOCKCUR (1 << 2) +#define DSME_MYCOLOR (1 << 3) extern void DrawSISMessage (const UNICODE *pStr); extern void DrawGameDate (void); extern void DateToString (unsigned char *buf, size_t bufLen, @@ -282,6 +283,7 @@ extern void DrawStorageBays (BOOLEAN Refresh); extern void GetGaugeRect (RECT *pRect, BOOLEAN IsCrewRect); extern void DrawFlagshipStats (void); extern void SaveFlagshipState (void); +void DrawAutoPilotMessage (BOOLEAN Reset); extern void DeltaSISGauges (SIZE crew_delta, SIZE fuel_delta, int resunit_delta); diff --git a/sc2/src/uqm/starcon.c b/sc2/src/uqm/starcon.c index 55f00dfc6..1d607ebba 100644 --- a/sc2/src/uqm/starcon.c +++ b/sc2/src/uqm/starcon.c @@ -86,6 +86,14 @@ arilou_gate_task(void *data) return 0; } +static void +on_battle_frame (void) +{ + LockMutex (GraphicsLock); + DrawAutoPilotMessage (FALSE); + UnlockMutex (GraphicsLock); +} + static void BackgroundInitKernel (DWORD TimeOut) { @@ -269,7 +277,8 @@ while (--ac > 0) TaskSwitch (); - Battle (); + DrawAutoPilotMessage (TRUE); + Battle (&on_battle_frame); if (ArilouTask) Task_SetState (ArilouTask, TASK_EXIT); }