diff --git a/source/frontends/common2/programoptions.h b/source/frontends/common2/programoptions.h index ee20fe781..764c50fef 100644 --- a/source/frontends/common2/programoptions.h +++ b/source/frontends/common2/programoptions.h @@ -56,7 +56,7 @@ namespace common2 bool imgui = true; // use imgui renderer std::optional geometry; // must be initialised with defaults bool aspectRatio = false; // preserve aspect ratio - int glSwapInterval = 1; // SDL_GL_SetSwapInterval + int glSwapInterval = -1; // SDL_GL_SetSwapInterval std::optional gameControllerIndex; std::string gameControllerMappingFile; std::string audioDeviceName; diff --git a/source/frontends/sdl/main.cpp b/source/frontends/sdl/main.cpp index 704eb90bd..1fdd363db 100644 --- a/source/frontends/sdl/main.cpp +++ b/source/frontends/sdl/main.cpp @@ -73,7 +73,7 @@ void run_sdl(int argc, const char * argv []) frame = std::make_shared(options); } - std::cerr << "Default GL swap interval: " << sa2::compat::getGLSwapInterval() << std::endl; + std::cerr << "GL swap interval: " << sa2::compat::getGLSwapInterval() << std::endl; const common2::CommonInitialisation init(frame, paddle, options); diff --git a/source/frontends/sdl/sdlcompat.cpp b/source/frontends/sdl/sdlcompat.cpp index 93795ccad..9eedf9e1d 100644 --- a/source/frontends/sdl/sdlcompat.cpp +++ b/source/frontends/sdl/sdlcompat.cpp @@ -77,7 +77,17 @@ namespace sa2 // I am not sure whether we should worry about SDL_PROP_RENDERER_CREATE_PRESENT_VSYNC_NUMBER const char * name = index >= 0 ? SDL_GetRenderDriver(index) : nullptr; - return SDL_CreateRenderer(window, name); + SDL_PropertiesID props = SDL_CreateProperties(); + SDL_SetProperty(props, SDL_PROP_RENDERER_CREATE_WINDOW_POINTER, window); + SDL_SetBooleanProperty(props, SDL_PROP_RENDERER_CREATE_PRESENT_VSYNC_NUMBER, SDL_TRUE); + if (index >= 0) + { + SDL_SetStringProperty(props, SDL_PROP_RENDERER_CREATE_NAME_STRING, SDL_GetRenderDriver(index)); + } + + SDL_Renderer *renderer = SDL_CreateRendererWithProperties(props); + SDL_DestroyProperties(props); + return renderer; } #else diff --git a/source/frontends/sdl/sdlframe.cpp b/source/frontends/sdl/sdlframe.cpp index e7d10caec..21e030f06 100644 --- a/source/frontends/sdl/sdlframe.cpp +++ b/source/frontends/sdl/sdlframe.cpp @@ -145,23 +145,27 @@ namespace sa2 void SDLFrame::SetGLSynchronisation(const common2::EmulatorOptions & options) { - const int defaultGLSwap = compat::getGLSwapInterval(); - if (defaultGLSwap == 0) - { - // sane default - mySynchroniseWithTimer = true; - myTargetGLSwap = 0; - } - else if (options.syncWithTimer) + std::vector intervalsToTry; + + if (!options.syncWithTimer) { - mySynchroniseWithTimer = true; - myTargetGLSwap = 0; + intervalsToTry.push_back(options.glSwapInterval); + intervalsToTry.push_back(std::abs(options.glSwapInterval)); + intervalsToTry.push_back(-1); + intervalsToTry.push_back(1); } - else + // fallback + intervalsToTry.push_back(0); + + const auto it = std::find_if(intervalsToTry.begin(), intervalsToTry.end(), setGLSwapInterval); + + if (it == intervalsToTry.end()) { - mySynchroniseWithTimer = false; - myTargetGLSwap = options.glSwapInterval; + throw std::runtime_error(decorateSDLError("SDL_GL_SetSwapInterval")); } + + myTargetGLSwap = *it; + mySynchroniseWithTimer = (myTargetGLSwap == 0) && options.syncWithTimer; } void SDLFrame::End() @@ -176,12 +180,19 @@ namespace sa2 } } - void SDLFrame::setGLSwapInterval(const int interval) + bool SDLFrame::setGLSwapInterval(const int interval) + { + const int res = SDL_GL_SetSwapInterval(interval); + return res == 0; + } + + + void SDLFrame::changeGLSwapInterval(const int interval) { const int current = compat::getGLSwapInterval(); // in QEMU with GL_RENDERER: llvmpipe (LLVM 12.0.0, 256 bits) // SDL_GL_SetSwapInterval() always fails - if (interval != current && SDL_GL_SetSwapInterval(interval)) + if (interval != current && !setGLSwapInterval(interval)) { throw std::runtime_error(decorateSDLError("SDL_GL_SetSwapInterval")); } diff --git a/source/frontends/sdl/sdlframe.h b/source/frontends/sdl/sdlframe.h index dc37e5004..c57b915ab 100644 --- a/source/frontends/sdl/sdlframe.h +++ b/source/frontends/sdl/sdlframe.h @@ -40,7 +40,8 @@ namespace sa2 void SaveSnapshot(); - static void setGLSwapInterval(const int interval); + static bool setGLSwapInterval(const int interval); + static void changeGLSwapInterval(const int interval); protected: void SetApplicationIcon();