Skip to content

Commit 05bbb8b

Browse files
committed
fix runahead frames not being cleared on loadstate / rewind, fix rewind buffer and emu fps being ticked when frame is skipped, add runahead lazy to args.
1 parent 4617119 commit 05bbb8b

3 files changed

Lines changed: 20 additions & 6 deletions

File tree

src/platforms/sdl3/app.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,8 @@ typedef struct {
144144

145145
bool rewind_push_new_frame(App* app);
146146
void emulator_update_texture_pixels(App* app, const void* pixel_buffer);
147+
148+
// this should be called when:
149+
// - a new rom is loaded.
150+
// - a savestate is loaded.
151+
void runahead_clear_frames(App* app);

src/platforms/sdl3/main.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,9 @@ static void on_rom_load(App* app) {
456456

457457
// reset speed to default.
458458
on_set_speed(app, SPEED_DEFAULT_INDEX);
459+
460+
// reset runahead buffer.
461+
runahead_clear_frames(app);
459462
}
460463

461464
static void mgb_on_file_callback(void* user, const char* file_name, enum CallbackType type, bool result) {
@@ -490,6 +493,7 @@ static void mgb_on_file_callback(void* user, const char* file_name, enum Callbac
490493
case CallbackType_LOAD_STATE:
491494
if (result) {
492495
on_set_rewind(app, false);
496+
runahead_clear_frames(app);
493497
text_popup_push(TextPopupType_INFO, "Loaded State");
494498
} else {
495499
text_popup_push(TextPopupType_ERROR, "Failed to load state");
@@ -618,6 +622,10 @@ static uint32_t core_colour_callback(void* user, uint8_t r, uint8_t g, uint8_t b
618622
static void core_vblank_callback(void* user, uint32_t overscan_colour) {
619623
App* app = user;
620624

625+
if (SMS_get_skip_frame(&app->sms)) {
626+
return;
627+
}
628+
621629
SDL_LockMutex(app->timer_shared_data.mutex);
622630
app->timer_shared_data.vblank_counter++;
623631
SDL_UnlockMutex(app->timer_shared_data.mutex);
@@ -629,10 +637,6 @@ static void core_vblank_callback(void* user, uint32_t overscan_colour) {
629637
app->rewind_counter--;
630638
}
631639

632-
if (SMS_get_skip_frame(&app->sms)) {
633-
return;
634-
}
635-
636640
if (app->pending_frame && app->speed_index == SPEED_DEFAULT_INDEX) {
637641
SDL_Log("dropping frame\n");
638642
}
@@ -1168,7 +1172,7 @@ static bool runahead_is_enabled(const App* app) {
11681172

11691173
// clears frame count so that all new frames must be generated.
11701174
// this should be called on input change, loadstate and loadrom.
1171-
static void runahead_clear_frames(App* app) {
1175+
void runahead_clear_frames(App* app) {
11721176
app->runahead.count = 0;
11731177
}
11741178

@@ -1355,6 +1359,7 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) {
13551359
bool fullscreen = false;
13561360
bool loadstate = false;
13571361
bool overscan_fill = true;
1362+
bool runahead_lazy = false;
13581363

13591364
int arg_index = 1;
13601365
for (;;) {
@@ -1396,7 +1401,6 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) {
13961401
}
13971402
}
13981403
else {
1399-
// while (!(arg_result = args_parse(&arg_index, argc, argv, ARGS_META, SDL_arraysize(ARGS_META), &arg_data))) {
14001404
switch (ARGS_META[arg_data.meta_index].id) {
14011405
case ArgsId_help:
14021406
show_help = true;
@@ -1446,6 +1450,9 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) {
14461450
case ArgsId_runahead:
14471451
runahead = arg_data.value.i;
14481452
break;
1453+
case ArgsId_runahead_lazy:
1454+
runahead_lazy = true;
1455+
break;
14491456
}
14501457
}
14511458
}
@@ -1644,6 +1651,7 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) {
16441651
text_popup_push(TextPopupType_INFO, "Press CTRL+O to load ROM");
16451652
}
16461653

1654+
app->runahead.lazy = runahead_lazy;
16471655
runahead_init(app, runahead);
16481656

16491657
app->focus = SDL_GetWindowFlags(app->window) & SDL_WINDOW_INPUT_FOCUS;

src/platforms/sdl3/rewind_bar.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ void rewind_bar_button(App* app, enum RewindBarButton button) {
102102

103103
// load savestate and disable the menu bar.
104104
SMS_loadstate(&app->sms, app->rewind_state_buffer, app->rewind_state_buffer_size, &app->rewind_state_config);
105+
runahead_clear_frames(app);
105106
rewind_bar_set_open_internal(app, false, false);
106107
break;
107108

0 commit comments

Comments
 (0)