From e89a21e95b3cb84d1515c0fb3378ce902a48c1c8 Mon Sep 17 00:00:00 2001 From: Holger Schemel Date: Thu, 21 Jul 2022 15:51:43 +0200 Subject: [PATCH] added delay counter data type and adjusted related functions --- src/anim.c | 14 +++++++------- src/editor.c | 7 ++++--- src/events.c | 19 ++++++++---------- src/game.c | 29 ++++++++++++++-------------- src/game.h | 2 +- src/game_mm/export.h | 8 ++++---- src/game_mm/mm_game.c | 39 +++++++++++++++++++------------------ src/game_mm/mm_tools.c | 35 ++++++++++++++------------------- src/init.c | 12 ++++++------ src/libgame/gadgets.c | 9 ++++----- src/libgame/misc.c | 44 ++++++++++++++++++++++++------------------ src/libgame/misc.h | 17 ++++++++-------- src/libgame/sdl.c | 20 +++++++++---------- src/libgame/sound.c | 4 ++-- src/libgame/system.c | 12 ++++++------ src/libgame/system.h | 6 ++---- src/libgame/types.h | 7 +++++++ src/screens.c | 33 ++++++++++++++----------------- src/tape.c | 10 ++++------ src/tools.c | 37 ++++++++++++++++++----------------- 20 files changed, 182 insertions(+), 182 deletions(-) diff --git a/src/anim.c b/src/anim.c index 7f6691f0..aa005263 100644 --- a/src/anim.c +++ b/src/anim.c @@ -124,7 +124,8 @@ struct GlobalAnimPartControlInfo unsigned int initial_anim_sync_frame; unsigned int anim_random_frame; - unsigned int step_delay, step_delay_value; + + DelayCounter step_delay; int init_delay_counter; int anim_delay_counter; @@ -436,8 +437,8 @@ static void InitToonControls(void) part->initial_anim_sync_frame = 0; part->anim_random_frame = -1; - part->step_delay = 0; - part->step_delay_value = graphic_info[control].step_delay; + part->step_delay.count = 0; + part->step_delay.value = graphic_info[control].step_delay; part->state = ANIM_STATE_INACTIVE; part->last_anim_status = -1; @@ -543,8 +544,8 @@ static void InitGlobalAnimControls(void) part->initial_anim_sync_frame = 0; part->anim_random_frame = -1; - part->step_delay = 0; - part->step_delay_value = graphic_info[control].step_delay; + part->step_delay.count = 0; + part->step_delay.value = graphic_info[control].step_delay; part->state = ANIM_STATE_INACTIVE; part->last_anim_status = -1; @@ -1531,8 +1532,7 @@ static int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part, // special case to prevent expiring loop sounds when playing PlayGlobalAnimSoundIfLoop(part); - if (!DelayReachedExt(&part->step_delay, part->step_delay_value, - anim_sync_frame)) + if (!DelayReachedExt(&part->step_delay, anim_sync_frame)) return ANIM_STATE_RUNNING; #if 0 diff --git a/src/editor.c b/src/editor.c index e5d95670..bf16751f 100644 --- a/src/editor.c +++ b/src/editor.c @@ -14524,11 +14524,12 @@ static void HandleLevelEditorIdle_Properties(void) int element_border = graphic_info[IMG_EDITOR_ELEMENT_BORDER].border_size; int x = editor.settings.element_graphic.x + element_border; int y = editor.settings.element_graphic.y + element_border; - static unsigned int action_delay = 0; - unsigned int action_delay_value = GameFrameDelay; + static DelayCounter action_delay = { 0 }; int i; - if (!DelayReached(&action_delay, action_delay_value)) + action_delay.value = GameFrameDelay; + + if (!DelayReached(&action_delay)) return; for (i = 0; i < ED_NUM_SELECTBOX; i++) diff --git a/src/events.c b/src/events.c index 4a31bcd6..f73e4884 100644 --- a/src/events.c +++ b/src/events.c @@ -36,8 +36,7 @@ static boolean cursor_inside_playfield = FALSE; static int cursor_mode_last = CURSOR_DEFAULT; -static unsigned int special_cursor_delay = 0; -static unsigned int special_cursor_delay_value = 1000; +static DelayCounter special_cursor_delay = { 1000 }; static boolean special_cursor_enabled = FALSE; static boolean stop_processing_events = FALSE; @@ -211,8 +210,7 @@ void StopProcessingEvents(void) static void HandleEvents(void) { Event event; - unsigned int event_frame_delay = 0; - unsigned int event_frame_delay_value = GAME_FRAME_DELAY; + DelayCounter event_frame_delay = { GAME_FRAME_DELAY }; ResetDelayCounter(&event_frame_delay); @@ -277,7 +275,7 @@ static void HandleEvents(void) ResetDelayCounter(&event_frame_delay); // do not handle events for longer than standard frame delay period - if (DelayReached(&event_frame_delay, event_frame_delay_value)) + if (DelayReached(&event_frame_delay)) break; // do not handle any further events if triggered by a special flag @@ -330,7 +328,7 @@ static void HandleMouseCursor(void) // when showing title screens, hide mouse pointer (if not moved) if (gfx.cursor_mode != CURSOR_NONE && - DelayReached(&special_cursor_delay, special_cursor_delay_value)) + DelayReached(&special_cursor_delay)) { SetMouseCursor(CURSOR_NONE); } @@ -347,7 +345,7 @@ static void HandleMouseCursor(void) if (gfx.cursor_mode != CURSOR_PLAYFIELD && cursor_inside_playfield && special_cursor_enabled && - DelayReached(&special_cursor_delay, special_cursor_delay_value)) + DelayReached(&special_cursor_delay)) { SetMouseCursor(CURSOR_PLAYFIELD); } @@ -2589,8 +2587,7 @@ static int HandleJoystickForAllPlayers(void) void HandleJoystick(void) { - static unsigned int joytest_delay = 0; - static unsigned int joytest_delay_value = GADGET_FRAME_DELAY; + static DelayCounter joytest_delay = { GADGET_FRAME_DELAY }; static int joytest_last = 0; int delay_value_first = GADGET_FRAME_DELAY_FIRST; int delay_value = GADGET_FRAME_DELAY; @@ -2648,7 +2645,7 @@ void HandleJoystick(void) if (dx || dy || button) SetPlayfieldMouseCursorEnabled(TRUE); - if (joytest && !button && !DelayReached(&joytest_delay, joytest_delay_value)) + if (joytest && !button && !DelayReached(&joytest_delay)) { // delay joystick/keyboard actions if axes/keys continually pressed newbutton = dx = dy = 0; @@ -2656,7 +2653,7 @@ void HandleJoystick(void) else { // first start with longer delay, then continue with shorter delay - joytest_delay_value = + joytest_delay.value = (use_delay_value_first ? delay_value_first : delay_value); } diff --git a/src/game.c b/src/game.c index ce452e67..aa7d824d 100644 --- a/src/game.c +++ b/src/game.c @@ -3680,7 +3680,8 @@ void InitGame(void) player->can_fall_into_acid = CAN_MOVE_INTO_ACID(player->element_nr); - player->actual_frame_counter = 0; + player->actual_frame_counter.count = 0; + player->actual_frame_counter.value = 1; player->step_counter = 0; @@ -9139,17 +9140,16 @@ static void AmoebaToDiamondBD(int ax, int ay, int new_element) static void AmoebaGrowing(int x, int y) { - static unsigned int sound_delay = 0; - static unsigned int sound_delay_value = 0; + static DelayCounter sound_delay = { 0 }; if (!MovDelay[x][y]) // start new growing cycle { MovDelay[x][y] = 7; - if (DelayReached(&sound_delay, sound_delay_value)) + if (DelayReached(&sound_delay)) { PlayLevelSoundElementAction(x, y, Store[x][y], ACTION_GROWING); - sound_delay_value = 30; + sound_delay.value = 30; } } @@ -9175,15 +9175,14 @@ static void AmoebaGrowing(int x, int y) static void AmoebaShrinking(int x, int y) { - static unsigned int sound_delay = 0; - static unsigned int sound_delay_value = 0; + static DelayCounter sound_delay = { 0 }; if (!MovDelay[x][y]) // start new shrinking cycle { MovDelay[x][y] = 7; - if (DelayReached(&sound_delay, sound_delay_value)) - sound_delay_value = 30; + if (DelayReached(&sound_delay)) + sound_delay.value = 30; } if (MovDelay[x][y]) // wait some time before shrinking @@ -13196,7 +13195,7 @@ void ScrollPlayer(struct PlayerInfo *player, int mode) if (mode == SCROLL_INIT) { - player->actual_frame_counter = FrameCounter; + player->actual_frame_counter.count = FrameCounter; player->GfxPos = move_stepsize * (player->MovPos / move_stepsize); if ((player->block_last_field || player->block_delay_adjustment > 0) && @@ -13225,7 +13224,7 @@ void ScrollPlayer(struct PlayerInfo *player, int mode) if (player->MovPos != 0) // player has not yet reached destination return; } - else if (!FrameReached(&player->actual_frame_counter, 1)) + else if (!FrameReached(&player->actual_frame_counter)) return; if (player->MovPos != 0) @@ -13348,20 +13347,22 @@ void ScrollPlayer(struct PlayerInfo *player, int mode) void ScrollScreen(struct PlayerInfo *player, int mode) { - static unsigned int screen_frame_counter = 0; + static DelayCounter screen_frame_counter = { 0 }; if (mode == SCROLL_INIT) { // set scrolling step size according to actual player's moving speed ScrollStepSize = TILEX / player->move_delay_value; - screen_frame_counter = FrameCounter; + screen_frame_counter.count = FrameCounter; + screen_frame_counter.value = 1; + ScreenMovDir = player->MovDir; ScreenMovPos = player->MovPos; ScreenGfxPos = ScrollStepSize * (ScreenMovPos / ScrollStepSize); return; } - else if (!FrameReached(&screen_frame_counter, 1)) + else if (!FrameReached(&screen_frame_counter)) return; if (ScreenMovPos) diff --git a/src/game.h b/src/game.h index 600485ea..26c186fe 100644 --- a/src/game.h +++ b/src/game.h @@ -379,7 +379,7 @@ struct PlayerInfo int push_delay; int push_delay_value; - unsigned int actual_frame_counter; + DelayCounter actual_frame_counter; int drop_delay; int drop_pressed_delay; diff --git a/src/game_mm/export.h b/src/game_mm/export.h index 4a63b249..710d2ee9 100644 --- a/src/game_mm/export.h +++ b/src/game_mm/export.h @@ -193,10 +193,10 @@ struct EngineSnapshotInfo_MM int hold_x, hold_y; int pacman_nr; - unsigned int rotate_delay; - unsigned int pacman_delay; - unsigned int energy_delay; - unsigned int overload_delay; + DelayCounter rotate_delay; + DelayCounter pacman_delay; + DelayCounter energy_delay; + DelayCounter overload_delay; }; diff --git a/src/game_mm/mm_game.c b/src/game_mm/mm_game.c index c97a7d3b..0403795f 100644 --- a/src/game_mm/mm_game.c +++ b/src/game_mm/mm_game.c @@ -111,10 +111,10 @@ static int hold_x = -1, hold_y = -1; static int pacman_nr = -1; // various game engine delay counters -static unsigned int rotate_delay = 0; -static unsigned int pacman_delay = 0; -static unsigned int energy_delay = 0; -static unsigned int overload_delay = 0; +static DelayCounter rotate_delay = { AUTO_ROTATE_DELAY }; +static DelayCounter pacman_delay = { PACMAN_MOVE_DELAY }; +static DelayCounter energy_delay = { ENERGY_DELAY }; +static DelayCounter overload_delay = { 0 }; // element masks for scanning pixels of MM elements static const char mm_masks[10][16][16 + 1] = @@ -637,10 +637,10 @@ void InitGameEngine_MM(void) CT = Ct = 0; - rotate_delay = 0; - pacman_delay = 0; - energy_delay = 0; - overload_delay = 0; + rotate_delay.count = 0; + pacman_delay.count = 0; + energy_delay.count = 0; + overload_delay.count = 0; ClickElement(-1, -1, -1); @@ -2709,8 +2709,7 @@ static void ContinueMoving_MM(int x, int y) boolean ClickElement(int x, int y, int button) { - static unsigned int click_delay = 0; - static int click_delay_value = CLICK_DELAY; + static DelayCounter click_delay = { CLICK_DELAY }; static boolean new_button = TRUE; boolean element_clicked = FALSE; int element; @@ -2718,8 +2717,8 @@ boolean ClickElement(int x, int y, int button) if (button == -1) { // initialize static variables - click_delay = 0; - click_delay_value = CLICK_DELAY; + click_delay.count = 0; + click_delay.value = CLICK_DELAY; new_button = TRUE; return FALSE; @@ -2732,7 +2731,7 @@ boolean ClickElement(int x, int y, int button) if (button == MB_RELEASED) { new_button = TRUE; - click_delay_value = CLICK_DELAY; + click_delay.value = CLICK_DELAY; // release eventually hold auto-rotating mirror RotateMirror(x, y, MB_RELEASED); @@ -2740,7 +2739,7 @@ boolean ClickElement(int x, int y, int button) return FALSE; } - if (!FrameReached(&click_delay, click_delay_value) && !new_button) + if (!FrameReached(&click_delay) && !new_button) return FALSE; if (button == MB_MIDDLEBUTTON) // middle button has no function @@ -2827,7 +2826,7 @@ boolean ClickElement(int x, int y, int button) element_clicked = TRUE; } - click_delay_value = (new_button ? CLICK_DELAY_FIRST : CLICK_DELAY); + click_delay.value = (new_button ? CLICK_DELAY_FIRST : CLICK_DELAY); new_button = FALSE; return element_clicked; @@ -2932,7 +2931,7 @@ static void AutoRotateMirrors(void) { int x, y; - if (!FrameReached(&rotate_delay, AUTO_ROTATE_DELAY)) + if (!FrameReached(&rotate_delay)) return; for (x = 0; x < lev_fieldx; x++) @@ -3100,7 +3099,7 @@ static void GameActions_MM_Ext(struct MouseActionInfo action, boolean warp_mode) CT = FrameCounter; - if (game_mm.num_pacman && FrameReached(&pacman_delay, PACMAN_MOVE_DELAY)) + if (game_mm.num_pacman && FrameReached(&pacman_delay)) { MovePacMen(); @@ -3111,7 +3110,7 @@ static void GameActions_MM_Ext(struct MouseActionInfo action, boolean warp_mode) } } - if (FrameReached(&energy_delay, ENERGY_DELAY)) + if (FrameReached(&energy_delay)) { if (game_mm.energy_left > 0) { @@ -3177,9 +3176,11 @@ static void GameActions_MM_Ext(struct MouseActionInfo action, boolean warp_mode) !IS_WALL_AMOEBA(element)) return; + overload_delay.value = HEALTH_DELAY(laser.overloaded); + if (((laser.overloaded && laser.overload_value < MAX_LASER_OVERLOAD) || (!laser.overloaded && laser.overload_value > 0)) && - FrameReached(&overload_delay, HEALTH_DELAY(laser.overloaded))) + FrameReached(&overload_delay)) { if (laser.overloaded) laser.overload_value++; diff --git a/src/game_mm/mm_tools.c b/src/game_mm/mm_tools.c index 21ad0814..044c83a3 100644 --- a/src/game_mm/mm_tools.c +++ b/src/game_mm/mm_tools.c @@ -899,16 +899,11 @@ static void DrawTileCursor_Xsn(int draw_target) static boolean started = FALSE; static boolean active = FALSE; static boolean debug = FALSE; - static unsigned int check_delay = 0; - static unsigned int start_delay = 0; - static unsigned int growth_delay = 0; - static unsigned int update_delay = 0; - static unsigned int change_delay = 0; - static unsigned int check_delay_value = XSN_CHECK_DELAY * 1000; - static unsigned int start_delay_value = 0; - static unsigned int growth_delay_value = 0; - static unsigned int update_delay_value = 0; - static unsigned int change_delay_value = 0; + static DelayCounter check_delay = { XSN_CHECK_DELAY * 1000 }; + static DelayCounter start_delay = { 0 }; + static DelayCounter growth_delay = { 0 }; + static DelayCounter update_delay = { 0 }; + static DelayCounter change_delay = { 0 }; static int percent = 0; static int debug_value = 0; boolean reinitialize = FALSE; @@ -918,7 +913,7 @@ static void DrawTileCursor_Xsn(int draw_target) if (draw_target != DRAW_TO_SCREEN) return; - if (DelayReached(&check_delay, check_delay_value)) + if (DelayReached(&check_delay)) { percent = (debug ? debug_value * 100 / XSN_DEBUG_STEPS : xsn_percent()); @@ -996,7 +991,7 @@ static void DrawTileCursor_Xsn(int draw_target) if (!active_last) { - start_delay_value = (debug || setup.debug.xsn_mode == TRUE ? 0 : + start_delay.value = (debug || setup.debug.xsn_mode == TRUE ? 0 : (XSN_START_DELAY + XSN_RND(XSN_START_DELAY)) * 1000); started = FALSE; @@ -1072,12 +1067,12 @@ static void DrawTileCursor_Xsn(int draw_target) if (!started) { - if (!DelayReached(&start_delay, start_delay_value)) + if (!DelayReached(&start_delay)) return; - update_delay_value = XSN_UPDATE_DELAY; - growth_delay_value = XSN_GROWTH_DELAY * 1000; - change_delay_value = XSN_CHANGE_DELAY * 1000; + update_delay.value = XSN_UPDATE_DELAY; + growth_delay.value = XSN_GROWTH_DELAY * 1000; + change_delay.value = XSN_CHANGE_DELAY * 1000; ResetDelayCounter(&growth_delay); ResetDelayCounter(&update_delay); @@ -1088,24 +1083,24 @@ static void DrawTileCursor_Xsn(int draw_target) if (xsn.num_items < xsn.max_items) { - if (DelayReached(&growth_delay, growth_delay_value)) + if (DelayReached(&growth_delay)) { xsn.num_items += XSN_RND(XSN_GROWTH_RATE * 2); xsn.num_items = MIN(xsn.num_items, xsn.max_items); } } - if (DelayReached(&update_delay, update_delay_value)) + if (DelayReached(&update_delay)) { for (i = 0; i < xsn.num_items; i++) xsn_update_item(i); } - if (DelayReached(&change_delay, change_delay_value)) + if (DelayReached(&change_delay)) { xsn_update_change(); - change_delay_value = xsn.change_delay * 1000; + change_delay.value = xsn.change_delay * 1000; } int xsn_alpha_dx = (gfx.mouse_y > xsn.area_ysize - xsn.max_height ? diff --git a/src/init.c b/src/init.c index 727dcf30..7f9821bf 100644 --- a/src/init.c +++ b/src/init.c @@ -138,11 +138,12 @@ static void DrawInitAnim(boolean only_when_loading) game_status == GAME_MODE_LOADING ? &init_last.busy : &init_last.busy_playfield); - static unsigned int action_delay = 0; - unsigned int action_delay_value = GameFrameDelay; + static DelayCounter action_delay = { 0 }; int sync_frame = FrameCounter; int x, y; + action_delay.value = GameFrameDelay; + // prevent OS (Windows) from complaining about program not responding CheckQuitEvent(); @@ -152,7 +153,7 @@ static void DrawInitAnim(boolean only_when_loading) if (image_initial[graphic].bitmap == NULL || window == NULL) return; - if (!DelayReached(&action_delay, action_delay_value)) + if (!DelayReached(&action_delay)) return; if (busy->x == -1) @@ -6610,8 +6611,7 @@ void OpenAll(void) static boolean WaitForApiThreads(void) { - unsigned int thread_delay = 0; - unsigned int thread_delay_value = 10000; + DelayCounter thread_delay = { 10000 }; if (program.api_thread_count == 0) return TRUE; @@ -6627,7 +6627,7 @@ static boolean WaitForApiThreads(void) // wait for threads to finish (and fail on timeout) while (program.api_thread_count > 0) { - if (DelayReached(&thread_delay, thread_delay_value)) + if (DelayReached(&thread_delay)) { Error("failed waiting for threads - TIMEOUT"); diff --git a/src/libgame/gadgets.c b/src/libgame/gadgets.c index e788b754..94fe7455 100644 --- a/src/libgame/gadgets.c +++ b/src/libgame/gadgets.c @@ -1629,8 +1629,7 @@ void ClickOnGadget(struct GadgetInfo *gi, int button) boolean HandleGadgets(int mx, int my, int button) { - static unsigned int pressed_delay = 0; - static unsigned int pressed_delay_value = GADGET_FRAME_DELAY; + static DelayCounter pressed_delay = { GADGET_FRAME_DELAY }; static int last_button = 0; static int last_mx = 0, last_my = 0; static int pressed_mx = 0, pressed_my = 0; @@ -1756,7 +1755,7 @@ boolean HandleGadgets(int mx, int my, int button) (button != 0 && last_gi != NULL && new_gi == last_gi); gadget_pressed_delay_reached = - DelayReached(&pressed_delay, pressed_delay_value); + DelayReached(&pressed_delay); gadget_released = (release_event && last_gi != NULL); gadget_released_inside = (gadget_released && new_gi == last_gi); @@ -1937,12 +1936,12 @@ boolean HandleGadgets(int mx, int my, int button) ResetDelayCounter(&pressed_delay); // start gadget delay with longer delay after first click on gadget - pressed_delay_value = GADGET_FRAME_DELAY_FIRST; + pressed_delay.value = GADGET_FRAME_DELAY_FIRST; } else // gadget hold pressed for some time { // after first repeated gadget click, continue with shorter delay value - pressed_delay_value = GADGET_FRAME_DELAY; + pressed_delay.value = GADGET_FRAME_DELAY; } if (gi->type & GD_TYPE_SCROLLBAR && !gadget_dragging) diff --git a/src/libgame/misc.c b/src/libgame/misc.c index 7b82420c..7c3e04fc 100644 --- a/src/libgame/misc.c +++ b/src/libgame/misc.c @@ -641,8 +641,8 @@ void Delay(unsigned int delay) // Sleep specified number of milliseconds sleep_milliseconds(delay); } -boolean DelayReachedExt(unsigned int *counter_var, unsigned int delay, - unsigned int actual_counter) +boolean DelayReachedExt2(unsigned int *counter_var, unsigned int delay, + unsigned int actual_counter) { if (actual_counter >= *counter_var && actual_counter < *counter_var + delay) @@ -653,34 +653,40 @@ boolean DelayReachedExt(unsigned int *counter_var, unsigned int delay, return TRUE; } -boolean FrameReached(unsigned int *frame_counter_var, unsigned int frame_delay) +boolean DelayReachedExt(DelayCounter *counter, unsigned int actual_counter) { - return DelayReachedExt(frame_counter_var, frame_delay, FrameCounter); + return DelayReachedExt2(&counter->count, counter->value, actual_counter); } -boolean DelayReached(unsigned int *counter_var, unsigned int delay) +boolean FrameReached(DelayCounter *counter) { - return DelayReachedExt(counter_var, delay, Counter()); + return DelayReachedExt(counter, FrameCounter); } -void ResetDelayCounterExt(unsigned int *counter_var, - unsigned int actual_counter) +boolean DelayReached(DelayCounter *counter) { - DelayReachedExt(counter_var, 0, actual_counter); + return DelayReachedExt(counter, Counter()); } -void ResetFrameCounter(unsigned int *frame_counter_var) +void ResetDelayCounterExt(DelayCounter *counter, unsigned int actual_counter) { - FrameReached(frame_counter_var, 0); + DelayReachedExt2(&counter->count, 0, actual_counter); } -void ResetDelayCounter(unsigned int *counter_var) +void ResetFrameCounter(DelayCounter *counter) { - DelayReached(counter_var, 0); + ResetDelayCounterExt(counter, FrameCounter); } -int WaitUntilDelayReached(unsigned int *counter_var, unsigned int delay) +void ResetDelayCounter(DelayCounter *counter) { + ResetDelayCounterExt(counter, Counter()); +} + +int WaitUntilDelayReached(DelayCounter *counter) +{ + unsigned int *counter_var = &counter->count; + unsigned int delay = counter->value; unsigned int actual_counter; int skip_frames = 0; @@ -711,22 +717,22 @@ int WaitUntilDelayReached(unsigned int *counter_var, unsigned int delay) return skip_frames; } -void SkipUntilDelayReached(unsigned int *counter_var, unsigned int delay, +void SkipUntilDelayReached(DelayCounter *counter, int *loop_var, int last_loop_value) { - int skip_frames = WaitUntilDelayReached(counter_var, delay); + int skip_frames = WaitUntilDelayReached(counter); #if 0 #if DEBUG if (skip_frames) Debug("internal:SkipUntilDelayReached", "%d: %d ms -> SKIP %d FRAME(S) [%d ms]", - *loop_var, delay, - skip_frames, skip_frames * delay); + *loop_var, counter->value, + skip_frames, skip_frames * counter->value); else Debug("internal:SkipUntilDelayReached", "%d: %d ms", - *loop_var, delay); + *loop_var, counter->value); #endif #endif diff --git a/src/libgame/misc.h b/src/libgame/misc.h index 38e4edfa..23c30e08 100644 --- a/src/libgame/misc.h +++ b/src/libgame/misc.h @@ -134,14 +134,15 @@ char *getUUID(void); void InitCounter(void); unsigned int Counter(void); void Delay(unsigned int); -boolean DelayReachedExt(unsigned int *, unsigned int, unsigned int); -boolean FrameReached(unsigned int *, unsigned int); -boolean DelayReached(unsigned int *, unsigned int); -void ResetDelayCounterExt(unsigned int *, unsigned int); -void ResetFrameCounter(unsigned int *); -void ResetDelayCounter(unsigned int *); -int WaitUntilDelayReached(unsigned int *, unsigned int); -void SkipUntilDelayReached(unsigned int *, unsigned int, int *, int); +boolean DelayReachedExt2(unsigned int *, unsigned int, unsigned int); +boolean DelayReachedExt(DelayCounter *, unsigned int); +boolean FrameReached(DelayCounter *); +boolean DelayReached(DelayCounter *); +void ResetDelayCounterExt(DelayCounter *, unsigned int); +void ResetFrameCounter(DelayCounter *); +void ResetDelayCounter(DelayCounter *); +int WaitUntilDelayReached(DelayCounter *); +void SkipUntilDelayReached(DelayCounter *, int *, int); unsigned int init_random_number(int, int); unsigned int get_random_number(int, int); diff --git a/src/libgame/sdl.c b/src/libgame/sdl.c index 5be4c0dc..f586bd8b 100644 --- a/src/libgame/sdl.c +++ b/src/libgame/sdl.c @@ -69,12 +69,11 @@ static void FinalizeScreen(int draw_target) static void UpdateScreenExt(SDL_Rect *rect, boolean with_frame_delay) { - static unsigned int update_screen_delay = 0; - unsigned int update_screen_delay_value = 50; // (milliseconds) + static DelayCounter update_screen_delay = { 50 }; // (milliseconds) SDL_Surface *screen = backbuffer->surface; if (limit_screen_updates && - !DelayReached(&update_screen_delay, update_screen_delay_value)) + !DelayReached(&update_screen_delay)) return; LimitScreenUpdates(FALSE); @@ -154,24 +153,23 @@ static void UpdateScreenExt(SDL_Rect *rect, boolean with_frame_delay) SDL_Rect src_rect_up = { 0, 0, video.width, video.height }; SDL_Rect dst_rect_up = dst_rect_screen; - if (video.shifted_up || video.shifted_up_delay) + if (video.shifted_up || video.shifted_up_delay.count) { int time_current = SDL_GetTicks(); int pos = video.shifted_up_pos; int pos_last = video.shifted_up_pos_last; - if (!DelayReachedExt(&video.shifted_up_delay, video.shifted_up_delay_value, - time_current)) + if (!DelayReachedExt(&video.shifted_up_delay, time_current)) { - int delay = time_current - video.shifted_up_delay; - int delay_value = video.shifted_up_delay_value; + int delay_count = time_current - video.shifted_up_delay.count; + int delay_value = video.shifted_up_delay.value; - pos = pos_last + (pos - pos_last) * delay / delay_value; + pos = pos_last + (pos - pos_last) * delay_count / delay_value; } else { video.shifted_up_pos_last = pos; - video.shifted_up_delay = 0; + video.shifted_up_delay.count = 0; } src_rect_up.y = pos; @@ -225,7 +223,7 @@ static void UpdateScreenExt(SDL_Rect *rect, boolean with_frame_delay) // global synchronization point of the game to align video frame delay if (with_frame_delay) - WaitUntilDelayReached(&video.frame_delay, video.frame_delay_value); + WaitUntilDelayReached(&video.frame_delay); video.frame_counter++; diff --git a/src/libgame/sound.c b/src/libgame/sound.c index 12b01c38..35c0a246 100644 --- a/src/libgame/sound.c +++ b/src/libgame/sound.c @@ -166,8 +166,8 @@ static boolean Mixer_ChannelExpired(int channel) if (expire_loop_sounds && IS_LOOP(mixer[channel]) && !IS_MUSIC(mixer[channel]) && - DelayReached(&mixer[channel].playing_starttime, - SOUND_LOOP_EXPIRATION_TIME)) + DelayReachedExt2(&mixer[channel].playing_starttime, + SOUND_LOOP_EXPIRATION_TIME, Counter())) return TRUE; if (!Mix_Playing(channel)) diff --git a/src/libgame/system.c b/src/libgame/system.c index c41fbef2..83bbe67a 100644 --- a/src/libgame/system.c +++ b/src/libgame/system.c @@ -567,14 +567,14 @@ void InitVideoBuffer(int width, int height, int depth, boolean fullscreen) video.window_scaling_available = WINDOW_SCALING_STATUS; video.frame_counter = 0; - video.frame_delay = 0; - video.frame_delay_value = GAME_FRAME_DELAY; + video.frame_delay.count = 0; + video.frame_delay.value = GAME_FRAME_DELAY; video.shifted_up = FALSE; video.shifted_up_pos = 0; video.shifted_up_pos_last = 0; - video.shifted_up_delay = 0; - video.shifted_up_delay_value = ONE_SECOND_DELAY / 4; + video.shifted_up_delay.count = 0; + video.shifted_up_delay.value = ONE_SECOND_DELAY / 4; SDLInitVideoBuffer(fullscreen); @@ -1112,12 +1112,12 @@ boolean SetVideoMode(boolean fullscreen) void SetVideoFrameDelay(unsigned int frame_delay_value) { - video.frame_delay_value = frame_delay_value; + video.frame_delay.value = frame_delay_value; } unsigned int GetVideoFrameDelay(void) { - return video.frame_delay_value; + return video.frame_delay.value; } boolean ChangeVideoModeIfNeeded(boolean fullscreen) diff --git a/src/libgame/system.h b/src/libgame/system.h index d2aa685f..cf2b0ca6 100644 --- a/src/libgame/system.h +++ b/src/libgame/system.h @@ -1125,14 +1125,12 @@ struct VideoSystemInfo int vsync_mode; unsigned int frame_counter; - unsigned int frame_delay; - unsigned int frame_delay_value; + DelayCounter frame_delay; boolean shifted_up; int shifted_up_pos; int shifted_up_pos_last; - unsigned int shifted_up_delay; - unsigned int shifted_up_delay_value; + DelayCounter shifted_up_delay; boolean initialized; }; diff --git a/src/libgame/types.h b/src/libgame/types.h index 9fb00ec7..5129f1c8 100644 --- a/src/libgame/types.h +++ b/src/libgame/types.h @@ -76,4 +76,11 @@ struct ListNode }; typedef struct ListNode ListNode; +struct DelayCounter +{ + unsigned int value; + unsigned int count; +}; +typedef struct DelayCounter DelayCounter; + #endif // TYPES_H diff --git a/src/screens.c b/src/screens.c index 576f1ff0..809d7c2d 100644 --- a/src/screens.c +++ b/src/screens.c @@ -1872,7 +1872,7 @@ static unsigned int getAutoDelayCounter(struct TitleFadingInfo *fi) static boolean TitleAutoDelayReached(unsigned int *counter_var, struct TitleFadingInfo *fi) { - return DelayReachedExt(counter_var, fi->auto_delay, getAutoDelayCounter(fi)); + return DelayReachedExt2(counter_var, fi->auto_delay, getAutoDelayCounter(fi)); } static void ResetTitleAutoDelay(unsigned int *counter_var, @@ -3161,13 +3161,15 @@ static void DrawInfoScreen_Elements(void) void HandleInfoScreen_Elements(int dx, int dy, int button) { - static unsigned int info_delay = 0; + static DelayCounter info_delay = { 0 }; static int num_anims; static int num_pages; static int page; int anims_per_page = NUM_INFO_ELEMENTS_ON_SCREEN; int i; + info_delay.value = GameFrameDelay; + if (button == MB_MENU_INITIALIZE) { boolean new_element = TRUE; @@ -3230,7 +3232,7 @@ void HandleInfoScreen_Elements(int dx, int dy, int button) } else { - if (DelayReached(&info_delay, GameFrameDelay)) + if (DelayReached(&info_delay)) if (page < num_pages) DrawInfoScreen_HelpAnim(page * anims_per_page, num_anims, FALSE); @@ -8456,8 +8458,7 @@ static boolean CustomizeKeyboardMain(int player_nr) while (!finished) { Event event; - unsigned int event_frame_delay = 0; - unsigned int event_frame_delay_value = GAME_FRAME_DELAY; + DelayCounter event_frame_delay = { GAME_FRAME_DELAY }; // reset frame delay counter directly after updating screen ResetDelayCounter(&event_frame_delay); @@ -8535,7 +8536,7 @@ static boolean CustomizeKeyboardMain(int player_nr) } // do not handle events for longer than standard frame delay period - if (DelayReached(&event_frame_delay, event_frame_delay_value)) + if (DelayReached(&event_frame_delay)) break; } @@ -8559,8 +8560,7 @@ void CustomizeKeyboard(int player_nr) int font_height = getFontHeight(font_nr); int ypos1 = SYSIZE / 2 - font_height * 2; int ypos2 = SYSIZE / 2 - font_height * 1; - unsigned int wait_frame_delay = 0; - unsigned int wait_frame_delay_value = 2000; + DelayCounter wait_frame_delay = { 2000 }; ResetDelayCounter(&wait_frame_delay); @@ -8569,7 +8569,7 @@ void CustomizeKeyboard(int player_nr) DrawTextSCentered(ypos1, font_nr, "Keyboard"); DrawTextSCentered(ypos2, font_nr, "configured!"); - while (!DelayReached(&wait_frame_delay, wait_frame_delay_value)) + while (!DelayReached(&wait_frame_delay)) BackToFront(); ClearEventQueue(); @@ -8770,8 +8770,7 @@ static boolean ConfigureJoystickMapButtonsAndAxes(SDL_Joystick *joystick) screen_initialized = TRUE; - unsigned int event_frame_delay = 0; - unsigned int event_frame_delay_value = GAME_FRAME_DELAY; + DelayCounter event_frame_delay = { GAME_FRAME_DELAY }; // reset frame delay counter directly after updating screen ResetDelayCounter(&event_frame_delay); @@ -8909,7 +8908,7 @@ static boolean ConfigureJoystickMapButtonsAndAxes(SDL_Joystick *joystick) } // do not handle events for longer than standard frame delay period - if (DelayReached(&event_frame_delay, event_frame_delay_value)) + if (DelayReached(&event_frame_delay)) break; } } @@ -8993,8 +8992,7 @@ void ConfigureJoystick(int player_nr) int font_height = getFontHeight(font_nr); int ypos1 = SYSIZE / 2 - font_height * 2; int ypos2 = SYSIZE / 2 - font_height * 1; - unsigned int wait_frame_delay = 0; - unsigned int wait_frame_delay_value = 2000; + DelayCounter wait_frame_delay = { 2000 }; ResetDelayCounter(&wait_frame_delay); @@ -9005,7 +9003,7 @@ void ConfigureJoystick(int player_nr) DrawTextSCentered(ypos1, font_nr, message1); DrawTextSCentered(ypos2, font_nr, message2); - while (!DelayReached(&wait_frame_delay, wait_frame_delay_value)) + while (!DelayReached(&wait_frame_delay)) BackToFront(); ClearEventQueue(); @@ -9323,8 +9321,7 @@ void ConfigureVirtualButtons(void) int font_height = getFontHeight(font_nr); int ypos1 = SYSIZE / 2 - font_height * 2; int ypos2 = SYSIZE / 2 - font_height * 1; - unsigned int wait_frame_delay = 0; - unsigned int wait_frame_delay_value = 2000; + DelayCounter wait_frame_delay = { 2000 }; ResetDelayCounter(&wait_frame_delay); @@ -9333,7 +9330,7 @@ void ConfigureVirtualButtons(void) DrawTextSCentered(ypos1, font_nr, "Virtual buttons"); DrawTextSCentered(ypos2, font_nr, "configured!"); - while (!DelayReached(&wait_frame_delay, wait_frame_delay_value)) + while (!DelayReached(&wait_frame_delay)) BackToFront(); ClearEventQueue(); diff --git a/src/tape.c b/src/tape.c index cd07263f..3fb22a8d 100644 --- a/src/tape.c +++ b/src/tape.c @@ -1380,15 +1380,14 @@ boolean PlaySolutionTape(void) static boolean PlayScoreTape_WaitForDownload(void) { - unsigned int download_delay = 0; - unsigned int download_delay_value = 10000; + DelayCounter download_delay = { 10000 }; ResetDelayCounter(&download_delay); // wait for score tape to be successfully downloaded (and fail on timeout) while (!server_scores.tape_downloaded) { - if (DelayReached(&download_delay, download_delay_value)) + if (DelayReached(&download_delay)) return FALSE; UPDATE_BUSY_STATE_NOT_LOADING(); @@ -1578,15 +1577,14 @@ static void AutoPlayTapes_SetScoreEntry(int score, int time) static boolean AutoPlayTapes_WaitForUpload(void) { - unsigned int upload_delay = 0; - unsigned int upload_delay_value = 10000; + DelayCounter upload_delay = { 10000 }; ResetDelayCounter(&upload_delay); // wait for score tape to be successfully uploaded (and fail on timeout) while (!server_scores.uploaded) { - if (DelayReached(&upload_delay, upload_delay_value)) + if (DelayReached(&upload_delay)) { PrintNoLog("\r"); Print("- uploading score tape to score server - TIMEOUT.\n"); diff --git a/src/tools.c b/src/tools.c index 79cf0885..d00b01d4 100644 --- a/src/tools.c +++ b/src/tools.c @@ -2886,9 +2886,9 @@ static void AnimateEnvelope(int envelope_nr, int anim_mode, int action) int mask_mode = (src_bitmap != NULL ? BLIT_MASKED : BLIT_ON_BACKGROUND); boolean ffwd_delay = (tape.playing && tape.fast_forward); boolean no_delay = (tape.warp_forward); - unsigned int anim_delay = 0; int frame_delay_value = (ffwd_delay ? FfwdFrameDelay : GameFrameDelay); int anim_delay_value = MAX(1, (no_delay ? 0 : frame_delay_value) / 2); + DelayCounter anim_delay = { anim_delay_value }; int font_nr = FONT_ENVELOPE_1 + envelope_nr; int font_width = getFontWidth(font_nr); int font_height = getFontHeight(font_nr); @@ -2934,7 +2934,7 @@ static void AnimateEnvelope(int envelope_nr, int anim_mode, int action) redraw_mask |= REDRAW_FIELD; BackToFront(); - SkipUntilDelayReached(&anim_delay, anim_delay_value, &i, last_frame); + SkipUntilDelayReached(&anim_delay, &i, last_frame); } ClearAutoRepeatKeyEvents(); @@ -3204,7 +3204,7 @@ static void AnimateEnvelopeRequest(int anim_mode, int action) boolean no_delay = (tape.warp_forward); int delay_value = (ffwd_delay ? delay_value_fast : delay_value_normal); int anim_delay_value = MAX(1, (no_delay ? 0 : delay_value + 500 * 0) / 2); - unsigned int anim_delay = 0; + DelayCounter anim_delay = { anim_delay_value }; int tile_size = MAX(request.step_offset, 1); int max_xsize = request.width / tile_size; @@ -3277,7 +3277,7 @@ static void AnimateEnvelopeRequest(int anim_mode, int action) BackToFront(); - SkipUntilDelayReached(&anim_delay, anim_delay_value, &i, last_frame); + SkipUntilDelayReached(&anim_delay, &i, last_frame); } ClearAutoRepeatKeyEvents(); @@ -3548,15 +3548,17 @@ static void DrawPreviewLevelInfo(int mode) static void DrawPreviewLevelExt(boolean restart) { - static unsigned int scroll_delay = 0; - static unsigned int label_delay = 0; + static DelayCounter scroll_delay = { 0 }; + static DelayCounter label_delay = { 0 }; static int from_x, from_y, scroll_direction; static int label_state, label_counter; - unsigned int scroll_delay_value = preview.step_delay; boolean show_level_border = (BorderElement != EL_EMPTY); int level_xsize = lev_fieldx + (show_level_border ? 2 : 0); int level_ysize = lev_fieldy + (show_level_border ? 2 : 0); + scroll_delay.value = preview.step_delay; + label_delay.value = MICROLEVEL_LABEL_DELAY; + if (restart) { from_x = 0; @@ -3610,7 +3612,7 @@ static void DrawPreviewLevelExt(boolean restart) // scroll preview level, if needed if (preview.anim_mode != ANIM_NONE && (level_xsize > preview.xsize || level_ysize > preview.ysize) && - DelayReached(&scroll_delay, scroll_delay_value)) + DelayReached(&scroll_delay)) { switch (scroll_direction) { @@ -3668,7 +3670,7 @@ static void DrawPreviewLevelExt(boolean restart) if (!strEqual(level.name, NAMELESS_LEVEL_NAME) && !strEqual(level.author, ANONYMOUS_NAME) && !strEqual(level.author, leveldir_current->name) && - DelayReached(&label_delay, MICROLEVEL_LABEL_DELAY)) + DelayReached(&label_delay)) { int max_label_counter = 23; @@ -5352,8 +5354,7 @@ unsigned int MoveDoor(unsigned int door_state) }; static int door1 = DOOR_CLOSE_1; static int door2 = DOOR_CLOSE_2; - unsigned int door_delay = 0; - unsigned int door_delay_value; + DelayCounter door_delay = { 0 }; int i; if (door_state == DOOR_GET_STATE) @@ -5468,7 +5469,7 @@ unsigned int MoveDoor(unsigned int door_state) num_move_steps = max_move_delay / max_step_delay; num_move_steps_doors_only = max_move_delay_doors_only / max_step_delay; - door_delay_value = max_step_delay; + door_delay.value = max_step_delay; if ((door_state & DOOR_NO_DELAY) || setup.quick_doors) { @@ -5551,7 +5552,7 @@ unsigned int MoveDoor(unsigned int door_state) { int k2_door = (door_opening ? k : num_move_steps_doors_only - k - 1); int kk_door = MAX(0, k2_door); - int sync_frame = kk_door * door_delay_value; + int sync_frame = kk_door * door_delay.value; int frame = getGraphicAnimationFrame(dpc->graphic, sync_frame); getFixedGraphicSource(dpc->graphic, frame, &bitmap, @@ -5660,7 +5661,7 @@ unsigned int MoveDoor(unsigned int door_state) { BackToFront(); - SkipUntilDelayReached(&door_delay, door_delay_value, &k, last_frame); + SkipUntilDelayReached(&door_delay, &k, last_frame); // prevent OS (Windows) from complaining about program not responding CheckQuitEvent(); @@ -5674,13 +5675,13 @@ unsigned int MoveDoor(unsigned int door_state) { // wait for specified door action post delay if (door_state & DOOR_ACTION_1 && door_state & DOOR_ACTION_2) - door_delay_value = MAX(door_1.post_delay, door_2.post_delay); + door_delay.value = MAX(door_1.post_delay, door_2.post_delay); else if (door_state & DOOR_ACTION_1) - door_delay_value = door_1.post_delay; + door_delay.value = door_1.post_delay; else if (door_state & DOOR_ACTION_2) - door_delay_value = door_2.post_delay; + door_delay.value = door_2.post_delay; - while (!DelayReached(&door_delay, door_delay_value)) + while (!DelayReached(&door_delay)) BackToFront(); } } -- 2.34.1