From 1c8eb84c7b4ff3760598d648e6a6a8144d1bbed9 Mon Sep 17 00:00:00 2001 From: Rob Loach Date: Mon, 20 May 2024 02:37:36 -0400 Subject: [PATCH] libretro: Replace clock() with frame time callback (#2589) --- src/system/libretro/tic80_libretro.c | 41 ++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/src/system/libretro/tic80_libretro.c b/src/system/libretro/tic80_libretro.c index 062e5d1bb..67f47b2e6 100644 --- a/src/system/libretro/tic80_libretro.c +++ b/src/system/libretro/tic80_libretro.c @@ -35,6 +35,9 @@ static retro_input_state_t input_state_cb; #define RETRO_BASE_POINTER_SPEED_DPAD 1.6f #define RETRO_SLOW_MOUSE_FACTOR_ANALOG 0.3f #define RETRO_SLOW_MOUSE_FACTOR_DPAD 0.4f +#ifndef TIC80_FREQUENCY +#define TIC80_FREQUENCY 1000000 +#endif enum pointer_device_type { @@ -74,23 +77,28 @@ struct tic80_state int mouseHideTimer; int mouseHideTimerStart; tic80* tic; + retro_usec_t frameTime; }; static struct tic80_state* state; /** - * TIC-80 callback; Request counter + * TIC-80 callback; Request counter. */ static u64 tic80_libretro_counter() { - return clock(); + if (state == NULL) { + return 0; + } + + return (u64)state->frameTime; } /** - * TIC-80 callback; Request freq + * TIC-80 callback; Request frequency. */ -static u64 tic80_libretro_freq() +static u64 tic80_libretro_frequency() { - return CLOCKS_PER_SEC; + return TIC80_FREQUENCY; } /** @@ -145,6 +153,17 @@ void tic80_libretro_fallback_log(enum retro_log_level level, const char *fmt, .. va_end(va); } +/** + * libretro callback; Called to indicate how much time has passed since last retro_run(). + */ +void tic80_libretro_frame_time(retro_usec_t usec) { + if (state == NULL) { + return; + } + + state->frameTime = usec; +} + /** * libretro callback; Global initialization. */ @@ -838,7 +857,7 @@ void tic80_libretro_update(tic80* game) tic80_libretro_update_keyboard(&state->input.keyboard); // Update the game state. - tic80_tick(game, state->input, tic80_libretro_counter, tic80_libretro_freq); + tic80_tick(game, state->input, tic80_libretro_counter, tic80_libretro_frequency); tic80_sound(game); } @@ -1038,6 +1057,16 @@ RETRO_API bool retro_load_game(const struct retro_game_info *info) return false; } + // Set up the frame time callback. + struct retro_frame_time_callback frame_time = { + .callback = tic80_libretro_frame_time, + .reference = TIC80_FREQUENCY / TIC80_FRAMERATE, + }; + if (!environ_cb(RETRO_ENVIRONMENT_SET_FRAME_TIME_CALLBACK, &frame_time)) { + log_cb(RETRO_LOG_ERROR, "[TIC-80] Failed to set frame time callback.\n"); + return false; + } + // Set up the TIC-80 environment. #if RETRO_IS_BIG_ENDIAN state->tic = tic80_create(TIC80_SAMPLERATE, TIC80_PIXEL_COLOR_ARGB8888);