From 7647062619228de43625f3a292dde1011ba7abec Mon Sep 17 00:00:00 2001 From: fgsfds Date: Wed, 6 Dec 2023 17:11:21 +0100 Subject: [PATCH] port: hard cap FPS to 200 for now --- port/fast3d/gfx_sdl2.cpp | 12 ++++++++---- port/fast3d/gfx_window_manager_api.h | 2 +- port/include/video.h | 3 +++ port/src/video.c | 12 ++++++++++-- 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/port/fast3d/gfx_sdl2.cpp b/port/fast3d/gfx_sdl2.cpp index 274bf1230..07128bfcf 100644 --- a/port/fast3d/gfx_sdl2.cpp +++ b/port/fast3d/gfx_sdl2.cpp @@ -291,7 +291,7 @@ static void gfx_sdl_set_target_fps(int fps) { } static bool gfx_sdl_can_disable_vsync(void) { - return false; + return true; } static void *gfx_sdl_get_window_handle(void) { @@ -302,9 +302,13 @@ static void gfx_sdl_set_window_title(const char *title) { SDL_SetWindowTitle(wnd, title); } -static void gfx_sdl_set_swap_interval(int interval) { - SDL_GL_SetSwapInterval(interval); - vsync_enabled = (interval != 0); +static bool gfx_sdl_set_swap_interval(int interval) { + const bool success = SDL_GL_SetSwapInterval(interval) >= 0; + vsync_enabled = success && (interval != 0); + if (!success) { + sysLogPrintf(LOG_WARNING, "SDL: failed to set vsync %d: %s", interval, SDL_GetError()); + } + return success; } struct GfxWindowManagerAPI gfx_sdl = { diff --git a/port/fast3d/gfx_window_manager_api.h b/port/fast3d/gfx_window_manager_api.h index f4070f782..71886547e 100644 --- a/port/fast3d/gfx_window_manager_api.h +++ b/port/fast3d/gfx_window_manager_api.h @@ -22,7 +22,7 @@ struct GfxWindowManagerAPI { bool (*can_disable_vsync)(void); void *(*get_window_handle)(void); void (*set_window_title)(const char *); - void (*set_swap_interval)(int); + bool (*set_swap_interval)(int); }; #endif diff --git a/port/include/video.h b/port/include/video.h index ffd2c6728..2ebeda21e 100644 --- a/port/include/video.h +++ b/port/include/video.h @@ -4,6 +4,9 @@ #include #include +// maximum framerate; if the game runs faster than this, things will break +#define VIDEO_MAX_FPS 200 + s32 videoInit(void); void videoStartFrame(void); void videoSubmitCommands(Gfx *cmds); diff --git a/port/src/video.c b/port/src/video.c index 075546218..c0c2fb642 100644 --- a/port/src/video.c +++ b/port/src/video.c @@ -46,7 +46,15 @@ s32 videoInit(void) gfx_init(wmAPI, renderingAPI, "PD", vidFullscreen, vidWidth, vidHeight, 100, 100); - wmAPI->set_swap_interval(vidVsync); + if (!wmAPI->set_swap_interval(vidVsync)) { + vidVsync = 0; + } + + if (vidVsync == 0 && vidFramerateLimit == 0) { + // cap FPS if there's no vsync to prevent the game from exploding + vidFramerateLimit = VIDEO_MAX_FPS; + } + wmAPI->set_target_fps(vidFramerateLimit); // disabled because vsync is on renderingAPI->set_texture_filter((enum FilteringMode)texFilter); @@ -228,7 +236,7 @@ PD_CONSTRUCTOR static void videoConfigInit(void) configRegisterInt("Video.DefaultHeight", &vidHeight, 0, 32767); configRegisterInt("Video.VSync", &vidVsync, -1, 10); configRegisterInt("Video.FramebufferEffects", &vidFramebuffers, 0, 1); - configRegisterInt("Video.FramerateLimit", &vidFramerateLimit, 0, 10000); + configRegisterInt("Video.FramerateLimit", &vidFramerateLimit, 0, VIDEO_MAX_FPS); configRegisterInt("Video.MSAA", &vidMSAA, 1, 16); configRegisterInt("Video.TextureFilter", &texFilter, 0, 2); configRegisterInt("Video.TextureFilter2D", &texFilter2D, 0, 1);