From 64dd586df8e67ebcc14dea6ae6a1091f8cff6cdb Mon Sep 17 00:00:00 2001 From: MeirGavish Date: Thu, 6 Nov 2025 12:15:07 +0200 Subject: [PATCH] Optimized score lerping by changing the number of steps to a power of 2 (#211) --- source/game.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/source/game.c b/source/game.c index 8abaa40..2fd2d0f 100644 --- a/source/game.c +++ b/source/game.c @@ -1600,6 +1600,11 @@ static void game_playing_process_hand_select_input() } } +/* This needs to stay a power of 2 and small enough + * for the lerping to be done before the next hand is drawn. + */ +#define NUM_SCORE_LERP_STEPS 32 + static void game_playing_process_input_and_state() { if (hand_state == HAND_SELECT) @@ -1624,8 +1629,13 @@ static void game_playing_process_input_and_state() } else if (play_state == PLAY_ENDED) { - lerped_temp_score -= int2fx(temp_score * get_game_speed())/ 40; - lerped_score += int2fx(temp_score * get_game_speed()) / 40; + /* Using fixed point in case the score is lower than NUM_SCORE_LERP_STEPS and then + * then the division rounds it down to 0 and it's never added to the total. + * The operation is equivalent to + * fxdiv(int2fx(temp_score * get_game_speed()), int2fx(NUM_SCORE_LERP_STEPS)) + */ + lerped_temp_score -= int2fx(temp_score * get_game_speed()) / NUM_SCORE_LERP_STEPS; + lerped_score += int2fx(temp_score * get_game_speed()) / NUM_SCORE_LERP_STEPS; if (lerped_temp_score > 0) {