From 572a4b698b524e58fb2e8d7dd44a49e861135cfa Mon Sep 17 00:00:00 2001 From: fgsfds Date: Thu, 9 Jul 2020 17:02:43 +0300 Subject: [PATCH] do vsync exactly like sm64-port does it maybe this will finally work better --- src/game/options_menu.c | 2 +- src/pc/configfile.c | 4 ++-- src/pc/configfile.h | 2 +- src/pc/gfx/gfx_sdl2.c | 19 +++++++++---------- 4 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/game/options_menu.c b/src/game/options_menu.c index 8ce169f0..56dba619 100644 --- a/src/game/options_menu.c +++ b/src/game/options_menu.c @@ -255,7 +255,7 @@ static struct Option optsControls[] = { static struct Option optsVideo[] = { DEF_OPT_TOGGLE( optsVideoStr[0], &configWindow.fullscreen ), - DEF_OPT_CHOICE( optsVideoStr[5], &configWindow.vsync, vsyncChoices ), + DEF_OPT_TOGGLE( optsVideoStr[5], &configWindow.vsync ), DEF_OPT_CHOICE( optsVideoStr[1], &configFiltering, filterChoices ), DEF_OPT_TOGGLE( optsVideoStr[7], &configHUD ), DEF_OPT_BUTTON( optsVideoStr[4], optvideo_reset_window ), diff --git a/src/pc/configfile.c b/src/pc/configfile.c index f55334f4..7411d4f8 100644 --- a/src/pc/configfile.c +++ b/src/pc/configfile.c @@ -100,7 +100,7 @@ static const struct ConfigOption options[] = { {.name = "window_y", .type = CONFIG_TYPE_UINT, .uintValue = &configWindow.y}, {.name = "window_w", .type = CONFIG_TYPE_UINT, .uintValue = &configWindow.w}, {.name = "window_h", .type = CONFIG_TYPE_UINT, .uintValue = &configWindow.h}, - {.name = "vsync", .type = CONFIG_TYPE_UINT, .uintValue = &configWindow.vsync}, + {.name = "vsync", .type = CONFIG_TYPE_BOOL, .boolValue = &configWindow.vsync}, {.name = "texture_filtering", .type = CONFIG_TYPE_UINT, .uintValue = &configFiltering}, {.name = "master_volume", .type = CONFIG_TYPE_UINT, .uintValue = &configMasterVolume}, {.name = "music_volume", .type = CONFIG_TYPE_UINT, .uintValue = &configMusicVolume}, @@ -268,7 +268,7 @@ void configfile_load(const char *filename) { case CONFIG_TYPE_BOOL: if (strcmp(tokens[1], "true") == 0) *option->boolValue = true; - else if (strcmp(tokens[1], "false") == 0) + else *option->boolValue = false; break; case CONFIG_TYPE_UINT: diff --git a/src/pc/configfile.h b/src/pc/configfile.h index 848a3494..b92ae7be 100644 --- a/src/pc/configfile.h +++ b/src/pc/configfile.h @@ -10,7 +10,7 @@ typedef struct { unsigned int x, y, w, h; - unsigned int vsync; + bool vsync; bool reset; bool fullscreen; bool exiting_fullscreen; diff --git a/src/pc/gfx/gfx_sdl2.c b/src/pc/gfx/gfx_sdl2.c index aef29780..8729e07d 100644 --- a/src/pc/gfx/gfx_sdl2.c +++ b/src/pc/gfx/gfx_sdl2.c @@ -53,8 +53,7 @@ static void (*kb_all_keys_up)(void) = NULL; // whether to use timer for frame control static bool use_timer = true; // time between consequtive game frames -static const Uint32 frame_time = 1000 / FRAMERATE; -static Uint32 frame_start = 0; +static const int frame_time = 1000 / FRAMERATE; const SDL_Scancode windows_scancode_table[] = { /* 0 1 2 3 4 5 6 7 */ @@ -138,8 +137,8 @@ int test_vsync(void) { return 0; } -static inline void gfx_sdl_set_vsync(int mode) { - if (mode > 1) { +static inline void gfx_sdl_set_vsync(const bool enabled) { + if (enabled) { // try to detect refresh rate SDL_GL_SetSwapInterval(1); const int vblanks = test_vsync(); @@ -149,13 +148,12 @@ static inline void gfx_sdl_set_vsync(int mode) { use_timer = false; return; } else { - printf("could not determine swap interval, falling back to one vblank + timer sync\n"); - mode = 1; + printf("could not determine swap interval, falling back to timer sync\n"); } } - SDL_GL_SetSwapInterval(mode); - use_timer = (mode <= 1); + use_timer = true; + SDL_GL_SetSwapInterval(0); } static void gfx_sdl_set_fullscreen(void) { @@ -324,14 +322,15 @@ static void gfx_sdl_set_keyboard_callbacks(kb_callback_t on_key_down, kb_callbac } static bool gfx_sdl_start_frame(void) { - frame_start = SDL_GetTicks(); return true; } static inline void sync_framerate_with_timer(void) { - const Uint32 elapsed = SDL_GetTicks() - frame_start; + static Uint32 last_time = 0; + const int elapsed = SDL_GetTicks() - last_time; if (elapsed < frame_time) SDL_Delay(frame_time - elapsed); + last_time += frame_time; } static void gfx_sdl_swap_buffers_begin(void) {