|
| 1 | +/* |
| 2 | + * Geargrafx - PC Engine / TurboGrafx Emulator |
| 3 | + * Copyright (C) 2024 Ignacio Sanchez |
| 4 | +
|
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * any later version. |
| 9 | +
|
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | +
|
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see http://www.gnu.org/licenses/ |
| 17 | + * |
| 18 | + */ |
| 19 | + |
| 20 | +#include <SDL.h> |
| 21 | +#include "imgui.h" |
| 22 | +#include "imgui_impl_sdl2.h" |
| 23 | +#include "geargrafx.h" |
| 24 | +#include "config.h" |
| 25 | +#include "gui.h" |
| 26 | +#include "ogl_renderer.h" |
| 27 | +#include "emu.h" |
| 28 | +#include "application.h" |
| 29 | + |
| 30 | +#define DISPLAY_IMPORT |
| 31 | +#include "display.h" |
| 32 | + |
| 33 | +static Uint64 frame_time_start = 0; |
| 34 | +static Uint64 frame_time_end = 0; |
| 35 | +static int monitor_refresh_rate = 60; |
| 36 | +static int vsync_frames_per_emu_frame = 1; |
| 37 | +static int vsync_frame_counter = 0; |
| 38 | + |
| 39 | +void display_begin_frame(void) |
| 40 | +{ |
| 41 | + frame_time_start = SDL_GetPerformanceCounter(); |
| 42 | +} |
| 43 | + |
| 44 | +void display_render(void) |
| 45 | +{ |
| 46 | + ogl_renderer_begin_render(); |
| 47 | + ImGui_ImplSDL2_NewFrame(); |
| 48 | + gui_render(); |
| 49 | + ogl_renderer_render(); |
| 50 | + ogl_renderer_end_render(); |
| 51 | + |
| 52 | + SDL_GL_SwapWindow(application_sdl_window); |
| 53 | +} |
| 54 | + |
| 55 | +void display_frame_throttle(void) |
| 56 | +{ |
| 57 | + frame_time_end = SDL_GetPerformanceCounter(); |
| 58 | + |
| 59 | + if (emu_is_empty() || emu_is_paused() || emu_is_debug_idle() || !emu_is_audio_open() || config_emulator.ffwd) |
| 60 | + { |
| 61 | + Uint64 count_per_sec = SDL_GetPerformanceFrequency(); |
| 62 | + float elapsed = (float)(frame_time_end - frame_time_start) / (float)count_per_sec; |
| 63 | + elapsed *= 1000.0f; |
| 64 | + |
| 65 | + float min = 16.666f; |
| 66 | + |
| 67 | + if (config_emulator.ffwd) |
| 68 | + { |
| 69 | + switch (config_emulator.ffwd_speed) |
| 70 | + { |
| 71 | + case 0: |
| 72 | + min = 16.666f / 1.5f; |
| 73 | + break; |
| 74 | + case 1: |
| 75 | + min = 16.666f / 2.0f; |
| 76 | + break; |
| 77 | + case 2: |
| 78 | + min = 16.666f / 2.5f; |
| 79 | + break; |
| 80 | + case 3: |
| 81 | + min = 16.666f / 3.0f; |
| 82 | + break; |
| 83 | + default: |
| 84 | + min = 0.0f; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + if (elapsed < min) |
| 89 | + SDL_Delay((Uint32)(min - elapsed)); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +bool display_should_run_emu_frame(void) |
| 94 | +{ |
| 95 | + if (config_video.sync && vsync_frames_per_emu_frame > 1 && !config_emulator.ffwd) |
| 96 | + { |
| 97 | + bool should_run = (vsync_frame_counter == 0); |
| 98 | + vsync_frame_counter++; |
| 99 | + if (vsync_frame_counter >= vsync_frames_per_emu_frame) |
| 100 | + vsync_frame_counter = 0; |
| 101 | + return should_run; |
| 102 | + } |
| 103 | + |
| 104 | + return true; |
| 105 | +} |
| 106 | + |
| 107 | +void display_update_frame_pacing(void) |
| 108 | +{ |
| 109 | + int display = SDL_GetWindowDisplayIndex(application_sdl_window); |
| 110 | + |
| 111 | + if (display < 0) |
| 112 | + { |
| 113 | + Log("SDL Error: SDL_GetWindowDisplayIndex - %s", SDL_GetError()); |
| 114 | + SDL_ClearError(); |
| 115 | + display = 0; |
| 116 | + } |
| 117 | + |
| 118 | + SDL_DisplayMode mode; |
| 119 | + if (SDL_GetCurrentDisplayMode(display, &mode) == 0 && mode.refresh_rate > 0) |
| 120 | + monitor_refresh_rate = mode.refresh_rate; |
| 121 | + else |
| 122 | + { |
| 123 | + Log("SDL Error: SDL_GetCurrentDisplayMode - %s", SDL_GetError()); |
| 124 | + SDL_ClearError(); |
| 125 | + monitor_refresh_rate = 60; |
| 126 | + } |
| 127 | + |
| 128 | + const int emu_fps = 60; |
| 129 | + |
| 130 | + if (monitor_refresh_rate <= emu_fps + 5) |
| 131 | + vsync_frames_per_emu_frame = 1; |
| 132 | + else |
| 133 | + vsync_frames_per_emu_frame = (monitor_refresh_rate + emu_fps / 2) / emu_fps; |
| 134 | + |
| 135 | + vsync_frames_per_emu_frame = CLAMP(vsync_frames_per_emu_frame, 1, 8); |
| 136 | + |
| 137 | + vsync_frame_counter = 0; |
| 138 | + |
| 139 | + Debug("Monitor refresh rate: %d Hz, vsync frames per emu frame: %d", monitor_refresh_rate, vsync_frames_per_emu_frame); |
| 140 | +} |
| 141 | + |
| 142 | +void display_recreate_gl_context(void) |
| 143 | +{ |
| 144 | + ogl_renderer_destroy(); |
| 145 | + ImGui_ImplSDL2_Shutdown(); |
| 146 | + |
| 147 | + SDL_GLContext old_context = display_gl_context; |
| 148 | + display_gl_context = SDL_GL_CreateContext(application_sdl_window); |
| 149 | + |
| 150 | + if (display_gl_context) |
| 151 | + { |
| 152 | + SDL_GL_MakeCurrent(application_sdl_window, display_gl_context); |
| 153 | + SDL_GL_DeleteContext(old_context); |
| 154 | + SDL_GL_SetSwapInterval(1); |
| 155 | + ImGui_ImplSDL2_InitForOpenGL(application_sdl_window, display_gl_context); |
| 156 | + ogl_renderer_init(); |
| 157 | + display_update_frame_pacing(); |
| 158 | + } |
| 159 | +} |
0 commit comments