FLTK: Better frame timing

This commit is contained in:
rdanbrook 2021-12-11 10:04:28 -05:00
parent 2227e73632
commit 58645c0da5
3 changed files with 38 additions and 11 deletions

View file

@ -55,6 +55,7 @@ static Fl_Menu_Bar *menubar;
static NstGlArea *glarea;
static NstChtWindow *chtwin;
static NstConfWindow *confwin;
static int fps = 60;
extern int loaded;
@ -69,6 +70,22 @@ extern nstpaths_t nstpaths;
extern bool (*nst_archive_select)(const char*, char*, size_t);
static int fltkui_refreshrate(void) {
// Get the screen refresh rate using an SDL window
int refresh = 60;
SDL_Window *sdlwin;
sdlwin = SDL_CreateWindow(
"refreshrate",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
1, 1, SDL_WINDOW_HIDDEN
);
SDL_DisplayMode dm;
SDL_GetCurrentDisplayMode(SDL_GetWindowDisplayIndex(sdlwin), &dm);
refresh = dm.refresh_rate;
SDL_DestroyWindow(sdlwin);
return refresh;
}
static void fltkui_cheats(Fl_Widget* w, void* userdata) {
if (!loaded) { return; }
chtwin->refresh();
@ -94,7 +111,10 @@ static void fltkui_rom_open(Fl_Widget* w, void* userdata) {
if (fc.filename()) {
loaded = nst_load(fc.filename());
nstwin->label(nstpaths.gamename);
if (loaded) { nst_play(); }
if (loaded) {
nst_play();
fps = nst_pal() ? 50 : 60;
}
}
break;
}
@ -463,11 +483,13 @@ int main(int argc, char *argv[]) {
nst_set_callbacks();
// Initialize SDL Audio and Joystick
if (SDL_Init(SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) < 0) {
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) < 0) {
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
return 1;
}
int refreshrate = fltkui_refreshrate();
// Set archive handler function pointer
nst_archive_select = &fltkui_archive_select;
@ -507,6 +529,10 @@ int main(int argc, char *argv[]) {
video_init();
int frames = 0;
int framefrags = 0;
fps = nst_pal() ? 50 : 60;
while (true) {
Fl::check();
if (!nstwin->shown()) {
@ -528,7 +554,15 @@ int main(int argc, char *argv[]) {
}
}
nst_emuloop();
frames = (fps / refreshrate);
framefrags += fps % refreshrate;
if (framefrags >= refreshrate) {
frames++;
framefrags -= refreshrate;
}
for (int i = 0; i < frames; i++) { nst_emuloop(); }
glarea->redraw();
}

View file

@ -806,12 +806,6 @@ void nst_state_quickload(int slot) {
nst_state_load(slotpath);
}
int nst_timing_runframes() {
// Calculate how many emulation frames to run
if (ffspeed) { return conf.timing_ffspeed; }
return 1;
}
void nst_timing_set_ffspeed() {
// Set the framerate to the fast-forward speed
ffspeed = true;
@ -847,7 +841,7 @@ void nst_emuloop() {
nst_input_turbo_pulse(cNstPads);
// Execute frames
for (int i = 0; i < nst_timing_runframes(); i++) {
for (int i = 0; i < (ffspeed ? conf.timing_ffspeed : 1); i++) {
emulator.Execute(cNstVideo, cNstSound, cNstPads);
}
}

View file

@ -93,7 +93,6 @@ void nst_state_quicksave(int isvst);
void nst_state_quickload(int isvst);
// Timing
int nst_timing_runframes();
void nst_timing_set_ffspeed();
void nst_timing_set_default();