From a32dc3daa0ce4f3f16674021198f194386212543 Mon Sep 17 00:00:00 2001 From: Eidolon Date: Sun, 26 Feb 2023 17:57:44 -0600 Subject: [PATCH] Fix software vsync This pre-SDL 2.0.18 hack from the interp branch is no longer needed when building with a new enough SDL version. This makes vid_wait toggleable at all times if SDL is at least 2.0.18. --- src/sdl/i_video.c | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/src/sdl/i_video.c b/src/sdl/i_video.c index 6e971a5d8..47d41ede5 100644 --- a/src/sdl/i_video.c +++ b/src/sdl/i_video.c @@ -102,8 +102,10 @@ rendermode_t chosenrendermode = render_none; // set by command line arguments boolean highcolor = false; +static void VidWaitChanged(void); + // synchronize page flipping with screen refresh -consvar_t cv_vidwait = CVAR_INIT ("vid_wait", "On", CV_SAVE, CV_OnOff, NULL); +consvar_t cv_vidwait = CVAR_INIT ("vid_wait", "On", CV_SAVE | CV_CALL, CV_OnOff, VidWaitChanged); static consvar_t cv_stretch = CVAR_INIT ("stretch", "Off", CV_SAVE|CV_NOSHOWHELP, CV_OnOff, NULL); static consvar_t cv_alwaysgrabmouse = CVAR_INIT ("alwaysgrabmouse", "Off", CV_SAVE, CV_OnOff, NULL); @@ -274,6 +276,22 @@ static void SDLSetMode(INT32 width, INT32 height, SDL_bool fullscreen, SDL_bool } } +static void VidWaitChanged(void) +{ + if (renderer && rendermode == render_soft) + { +#if SDL_VERSION_ATLEAST(2, 0, 18) + SDL_RenderSetVSync(renderer, cv_vidwait.value ? 1 : 0); +#endif + } +#ifdef HWRENDER + else if (rendermode == render_opengl && sdlglcontext != NULL && SDL_GL_GetCurrentContext() == sdlglcontext) + { + SDL_GL_SetSwapInterval(cv_vidwait.value ? 1 : 0); + } +#endif +} + static INT32 Impl_SDL_Scancode_To_Keycode(SDL_Scancode code) { if (code >= SDL_SCANCODE_A && code <= SDL_SCANCODE_Z) @@ -1476,15 +1494,19 @@ static SDL_bool Impl_CreateContext(void) int flags = 0; // Use this to set SDL_RENDERER_* flags now if (usesdl2soft) flags |= SDL_RENDERER_SOFTWARE; -#if 0 - // This shit is BROKEN. - // - The version of SDL we're using cannot toggle VSync at runtime. We'll need a new SDL version implemented to have this work properly. - // - cv_vidwait is initialized before config is loaded, so it's forced to default value at runtime, and forced off when switching. The config loading code would need restructured. - // - With both this & frame interpolation on, I_FinishUpdate takes x10 longer. At this point, it is simpler to use a standard FPS cap. - // So you can probably guess why I'm kinda over this, I'm just disabling it. else if (cv_vidwait.value) + { +#if SDL_VERSION_ATLEAST(2, 0, 18) + // If SDL is new enough, we can turn off vsync later. flags |= SDL_RENDERER_PRESENTVSYNC; +#else + // However, if it isn't, we should just silently turn vid_wait off + // This is because the renderer will be created before the config + // is read and vid_wait is set from the user's preferences, and thus + // vid_wait will have no effect. + CV_StealthSetValue(cv_vidwait, 0); #endif + } if (!renderer) renderer = SDL_CreateRenderer(window, -1, flags); @@ -1571,6 +1593,7 @@ boolean VID_CheckRenderer(void) else if (vid.glstate == VID_GL_LIBRARY_ERROR) rendererchanged = false; } + else #endif if (!contextcreated)