Skip to content

Commit cadb063

Browse files
committed
Refactor application in multiple modules
1 parent 377c739 commit cadb063

File tree

13 files changed

+1055
-787
lines changed

13 files changed

+1055
-787
lines changed

platforms/shared/desktop/application.cpp

Lines changed: 33 additions & 769 deletions
Large diffs are not rendered by default.

platforms/shared/desktop/application.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,7 @@
2929
#define EXTERN extern
3030
#endif
3131

32-
#define GAMEPAD_VBTN_AXIS_BASE 1000
33-
#define GAMEPAD_VBTN_AXIS_THRESHOLD 3000
34-
#define GAMEPAD_VBTN_L2 (GAMEPAD_VBTN_AXIS_BASE + SDL_CONTROLLER_AXIS_TRIGGERLEFT)
35-
#define GAMEPAD_VBTN_R2 (GAMEPAD_VBTN_AXIS_BASE + SDL_CONTROLLER_AXIS_TRIGGERRIGHT)
36-
3732
EXTERN SDL_Window* application_sdl_window;
38-
EXTERN SDL_GameController* application_gamepad[GG_MAX_GAMEPADS];
39-
EXTERN int application_added_gamepad_mappings;
40-
EXTERN int application_updated_gamepad_mappings;
4133
EXTERN float application_display_scale;
4234
EXTERN SDL_version application_sdl_build_version;
4335
EXTERN SDL_version application_sdl_link_version;
@@ -51,7 +43,6 @@ EXTERN void application_trigger_fullscreen(bool fullscreen);
5143
EXTERN void application_trigger_fit_to_content(int width, int height);
5244
EXTERN void application_set_vsync(bool enabled);
5345
EXTERN void application_update_title_with_rom(const char* rom);
54-
EXTERN void application_assign_gamepad(int slot, int device_index);
5546
EXTERN void application_input_pump(void);
5647
EXTERN bool application_check_single_instance(const char* rom_file, const char* symbol_file);
5748

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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+
}

platforms/shared/desktop/display.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
#ifndef DISPLAY_H
21+
#define DISPLAY_H
22+
23+
#include <SDL.h>
24+
25+
#ifdef DISPLAY_IMPORT
26+
#define EXTERN
27+
#else
28+
#define EXTERN extern
29+
#endif
30+
31+
EXTERN SDL_GLContext display_gl_context;
32+
33+
EXTERN void display_begin_frame(void);
34+
EXTERN void display_render(void);
35+
EXTERN void display_frame_throttle(void);
36+
EXTERN bool display_should_run_emu_frame(void);
37+
EXTERN void display_update_frame_pacing(void);
38+
EXTERN void display_recreate_gl_context(void);
39+
40+
#undef DISPLAY_IMPORT
41+
#undef EXTERN
42+
#endif /* DISPLAY_H */

0 commit comments

Comments
 (0)