diff --git a/emulator/Main.h b/emulator/Main.h index 7404545..5b261ef 100644 --- a/emulator/Main.h +++ b/emulator/Main.h @@ -26,6 +26,8 @@ extern double g_dFramesPercent; extern bool g_okDebugCpuPpu; +extern bool g_okVsyncSwitchable; + extern uint32_t* m_bits; // Screen buffer extern ImTextureID g_ScreenTextureID; @@ -79,6 +81,8 @@ void Settings_SetHardFilePath(int slot, LPCTSTR sFilePath); void Settings_GetHardFilePath(int slot, LPTSTR buffer); void Settings_SetScreenViewMode(int mode); int Settings_GetScreenViewMode(); +BOOL Settings_GetScreenVsync(); +void Settings_SetScreenVsync(BOOL flag); void Settings_SetScreenshotMode(int mode); int Settings_GetScreenshotMode(); BOOL Settings_GetDebugCpuPpu(); @@ -150,6 +154,8 @@ void Settings_SetColor(ColorIndices colorIndex, COLORREF color); //extern bool Option_ShowHelp; extern int Option_AutoBoot; +extern void SetVSync(); + ////////////////////////////////////////////////////////////////////// // Widgets diff --git a/emulator/MainWindow.cpp b/emulator/MainWindow.cpp index fa2585c..2b80d61 100644 --- a/emulator/MainWindow.cpp +++ b/emulator/MainWindow.cpp @@ -420,6 +420,16 @@ void ControlView_ImGuiWidget() ImGui::SeparatorText("ImGui"); ImGui::TextDisabled("FPS: %.1f", ImGui::GetIO().Framerate); + if (g_okVsyncSwitchable) + { + bool vsync = Settings_GetScreenVsync(); + if (ImGui::Checkbox("VSync", &vsync)) + { + Settings_SetScreenVsync(vsync); + SetVSync(); + } + } + #ifdef _DEBUG ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state #endif diff --git a/emulator/Settings.cpp b/emulator/Settings.cpp index 3bffcf0..6967b5f 100644 --- a/emulator/Settings.cpp +++ b/emulator/Settings.cpp @@ -325,6 +325,8 @@ void Settings_SetCartridgeFilePath(int slot, LPCTSTR sFilePath) SETTINGS_GETSET_DWORD(ScreenViewMode, _T("ScreenViewMode"), int, 1); +SETTINGS_GETSET_DWORD(ScreenVsync, _T("ScreenVsync"), BOOL, FALSE); + SETTINGS_GETSET_DWORD(ScreenHeightMode, _T("ScreenHeightMode"), int, 0); SETTINGS_GETSET_DWORD(ScreenshotMode, _T("ScreenshotMode"), int, 1); diff --git a/emulator/main.cpp b/emulator/main.cpp index 8e81e8b..d186f7d 100644 --- a/emulator/main.cpp +++ b/emulator/main.cpp @@ -36,6 +36,8 @@ double g_dFramesPercent = 0.0; bool g_okDebugCpuPpu = false; +bool g_okVsyncSwitchable = false; + ////////////////////////////////////////////////////////////////////// @@ -157,6 +159,27 @@ bool IsFileExist(const char * fileName) return errno != ENOENT; } +void SetVSync() +{ + bool sync = Settings_GetScreenVsync(); + + typedef BOOL(APIENTRY* PFNWGLSWAPINTERVALPROC)(int); + PFNWGLSWAPINTERVALPROC wglSwapIntervalEXT = nullptr; + + const char* extensions = (char*)glGetString(GL_EXTENSIONS); + + wglSwapIntervalEXT = (PFNWGLSWAPINTERVALPROC)wglGetProcAddress("wglSwapIntervalEXT"); + + if (wglSwapIntervalEXT == nullptr) + { + g_okVsyncSwitchable = false; + return; + } + + wglSwapIntervalEXT(sync); + g_okVsyncSwitchable = true; +} + // Main code #ifdef _WIN32 int WinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPSTR /*lpCmdLine*/, int /*nShowCmd*/) @@ -254,6 +277,8 @@ int main(int, char**) g_ScreenTextureID = (void*)(intptr_t) screen_texture; + SetVSync(); // Set initial Vsync flag value + // Win32+GL needs specific hooks for viewport, as there are specific things needed to tie Win32 and GL api. if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) {