Skip to content

Commit

Permalink
VSync on/off switch
Browse files Browse the repository at this point in the history
  • Loading branch information
nzeemin committed Jul 18, 2024
1 parent 0666b42 commit e7646ef
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
6 changes: 6 additions & 0 deletions emulator/Main.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -150,6 +154,8 @@ void Settings_SetColor(ColorIndices colorIndex, COLORREF color);
//extern bool Option_ShowHelp;
extern int Option_AutoBoot;

extern void SetVSync();


//////////////////////////////////////////////////////////////////////
// Widgets
Expand Down
10 changes: 10 additions & 0 deletions emulator/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions emulator/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
25 changes: 25 additions & 0 deletions emulator/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ double g_dFramesPercent = 0.0;

bool g_okDebugCpuPpu = false;

bool g_okVsyncSwitchable = false;


//////////////////////////////////////////////////////////////////////

Expand Down Expand Up @@ -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*/)
Expand Down Expand Up @@ -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)
{
Expand Down

0 comments on commit e7646ef

Please sign in to comment.