Automatically disable VSync on displays lower than 60Hz

This commit is contained in:
Lior Halphon
2023-12-23 16:34:40 +02:00
parent b516f9a3ee
commit e89df2df43
3 changed files with 39 additions and 1 deletions

View File

@@ -2132,6 +2132,20 @@ void convert_mouse_coordinates(signed *x, signed *y)
} }
} }
void update_swap_interval(void)
{
SDL_DisplayMode mode;
SDL_GetCurrentDisplayMode(SDL_GetWindowDisplayIndex(window), &mode);
if (mode.refresh_rate >= 60) {
if (SDL_GL_SetSwapInterval(1)) {
SDL_GL_SetSwapInterval(0);
}
}
else {
SDL_GL_SetSwapInterval(0);
}
}
void run_gui(bool is_running) void run_gui(bool is_running)
{ {
SDL_ShowCursor(SDL_ENABLE); SDL_ShowCursor(SDL_ENABLE);
@@ -2355,6 +2369,9 @@ void run_gui(bool is_running)
} }
} }
switch (event.type) { switch (event.type) {
case SDL_DISPLAYEVENT:
update_swap_interval();
break;
case SDL_QUIT: { case SDL_QUIT: {
if (!is_running) { if (!is_running) {
exit(0); exit(0);
@@ -2370,6 +2387,13 @@ void run_gui(bool is_running)
update_viewport(); update_viewport();
render_texture(NULL, NULL); render_texture(NULL, NULL);
} }
if (event.window.type == SDL_WINDOWEVENT_MOVED
#if SDL_COMPILEDVERSION > 2018
|| event.window.type == SDL_WINDOWEVENT_DISPLAY_CHANGED
#endif
) {
update_swap_interval();
}
break; break;
} }
case SDL_DROPFILE: { case SDL_DROPFILE: {
@@ -2514,6 +2538,7 @@ void run_gui(bool is_running)
else { else {
SDL_SetWindowFullscreen(window, 0); SDL_SetWindowFullscreen(window, 0);
} }
update_swap_interval();
update_viewport(); update_viewport();
} }
else if (event_hotkey_code(&event) == SDL_SCANCODE_O) { else if (event_hotkey_code(&event) == SDL_SCANCODE_O) {

View File

@@ -66,3 +66,4 @@ extern unsigned osd_countdown;
extern unsigned osd_text_lines; extern unsigned osd_text_lines;
void convert_mouse_coordinates(signed *x, signed *y); void convert_mouse_coordinates(signed *x, signed *y);
const GB_palette_t *current_dmg_palette(void); const GB_palette_t *current_dmg_palette(void);
void update_swap_interval(void);

View File

@@ -207,6 +207,9 @@ static void handle_events(GB_gameboy_t *gb)
SDL_Event event; SDL_Event event;
while (SDL_PollEvent(&event)) { while (SDL_PollEvent(&event)) {
switch (event.type) { switch (event.type) {
case SDL_DISPLAYEVENT:
update_swap_interval();
break;
case SDL_QUIT: case SDL_QUIT:
pending_command = GB_SDL_QUIT_COMMAND; pending_command = GB_SDL_QUIT_COMMAND;
break; break;
@@ -227,6 +230,13 @@ static void handle_events(GB_gameboy_t *gb)
if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) { if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
update_viewport(); update_viewport();
} }
if (event.window.type == SDL_WINDOWEVENT_MOVED
#if SDL_COMPILEDVERSION > 2018
|| event.window.type == SDL_WINDOWEVENT_DISPLAY_CHANGED
#endif
) {
update_swap_interval();
}
break; break;
} }
case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONDOWN:
@@ -428,12 +438,13 @@ static void handle_events(GB_gameboy_t *gb)
case SDL_SCANCODE_F: case SDL_SCANCODE_F:
if (event.key.keysym.mod & MODIFIER) { if (event.key.keysym.mod & MODIFIER) {
if ((SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN_DESKTOP) == false) { if (!(SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN_DESKTOP)) {
SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP); SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP);
} }
else { else {
SDL_SetWindowFullscreen(window, 0); SDL_SetWindowFullscreen(window, 0);
} }
update_swap_interval();
update_viewport(); update_viewport();
} }
break; break;
@@ -1128,6 +1139,7 @@ int main(int argc, char **argv)
if (gl_context) { if (gl_context) {
glGetIntegerv(GL_MAJOR_VERSION, &major); glGetIntegerv(GL_MAJOR_VERSION, &major);
glGetIntegerv(GL_MINOR_VERSION, &minor); glGetIntegerv(GL_MINOR_VERSION, &minor);
update_swap_interval();
} }
if (gl_context && major * 0x100 + minor < 0x302) { if (gl_context && major * 0x100 + minor < 0x302) {