forked from sm64pc/sm64ex
-
Notifications
You must be signed in to change notification settings - Fork 43
/
pc_main.c
326 lines (267 loc) · 8.1 KB
/
pc_main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include <stdlib.h>
#include <stdio.h>
#ifdef TARGET_WEB
#include <emscripten.h>
#include <emscripten/html5.h>
#endif
#include "sm64.h"
#include "game/memory.h"
#include "audio/external.h"
#include "gfx/gfx_pc.h"
#include "gfx/gfx_opengl.h"
#include "gfx/gfx_direct3d11.h"
#include "gfx/gfx_direct3d12.h"
#include "gfx/gfx_rt64.h"
#include "gfx/gfx_dxgi.h"
#include "gfx/gfx_sdl.h"
#include "audio/audio_api.h"
#include "audio/audio_sdl.h"
#include "audio/audio_null.h"
#include "pc_main.h"
#include "cliopts.h"
#include "configfile.h"
#include "controller/controller_api.h"
#include "controller/controller_keyboard.h"
#include "fs/fs.h"
#include "game/game_init.h"
#include "game/main.h"
#include "game/thread6.h"
#ifdef DISCORDRPC
#include "pc/discord/discordrpc.h"
#endif
#ifdef TARGET_SWITCH
#include "nx/m_nx.h"
#endif
#include "text/text-loader.h"
OSMesg D_80339BEC;
OSMesgQueue gSIEventMesgQueue;
s8 gResetTimer;
s8 D_8032C648;
s8 gDebugLevelSelect;
s8 gShowProfiler;
s8 gShowDebugText;
s32 gRumblePakPfs;
struct RumbleData gRumbleDataQueue[3];
struct StructSH8031D9B0 gCurrRumbleSettings;
static struct AudioAPI *audio_api;
static struct GfxWindowManagerAPI *wm_api;
static struct GfxRenderingAPI *rendering_api;
extern void gfx_run(Gfx *commands);
extern void thread5_game_loop(void *arg);
extern void create_next_audio_buffer(s16 *samples, u32 num_samples);
void game_loop_one_iteration(void);
void dispatch_audio_sptask(struct SPTask *spTask) {
}
void set_vblank_handler(s32 index, struct VblankHandler *handler, OSMesgQueue *queue, OSMesg *msg) {
}
static bool inited = false;
static inline void patch_interpolations(void) {
extern void mtx_patch_interpolated(void);
extern void patch_screen_transition_interpolated(void);
extern void patch_title_screen_scales(void);
extern void patch_interpolated_dialog(void);
extern void patch_interpolated_hud(void);
extern void patch_interpolated_paintings(void);
extern void patch_interpolated_bubble_particles(void);
extern void patch_interpolated_snow_particles(void);
mtx_patch_interpolated();
patch_screen_transition_interpolated();
patch_title_screen_scales();
patch_interpolated_dialog();
patch_interpolated_hud();
patch_interpolated_paintings();
patch_interpolated_bubble_particles();
patch_interpolated_snow_particles();
}
#include "game/display.h" // for gGlobalTimer
void send_display_list(struct SPTask *spTask) {
if (!inited) return;
#ifndef RAPI_RT64
if (!config60FPS)
#endif
{
patch_interpolations();
}
gfx_run((Gfx *)spTask->task.t.data_ptr);
}
#ifdef VERSION_EU
#define SAMPLES_HIGH 656
#define SAMPLES_LOW 640
#else
#define SAMPLES_HIGH 544
#define SAMPLES_LOW 528
#endif
void produce_one_frame(void) {
gfx_start_frame();
const f32 master_mod = (f32)configMasterVolume / 127.0f;
set_sequence_player_volume(SEQ_PLAYER_LEVEL, ((f32)configMusicVolume / 127.0f * master_mod) * !configMusicMute);
set_sequence_player_volume(SEQ_PLAYER_SFX, ((f32)configSfxVolume / 127.0f * master_mod) * !configSfxMute);
set_sequence_player_volume(SEQ_PLAYER_ENV, ((f32)configEnvVolume / 127.0f * master_mod) * !configEnvMute);
game_loop_one_iteration();
thread6_rumble_loop(NULL);
#ifdef TARGET_SWITCH
controller_nx_rumble_loop();
#endif
int samples_left = audio_api->buffered();
u32 num_audio_samples = samples_left < audio_api->get_desired_buffered() ? SAMPLES_HIGH : SAMPLES_LOW;
//printf("Audio samples: %d %u\n", samples_left, num_audio_samples);
s16 audio_buffer[SAMPLES_HIGH * 2 * 2];
for (int i = 0; i < 2; i++) {
/*if (audio_cnt-- == 0) {
audio_cnt = 2;
}
u32 num_audio_samples = audio_cnt < 2 ? 528 : 544;*/
create_next_audio_buffer(audio_buffer + i * (num_audio_samples * 2), num_audio_samples);
}
//printf("Audio samples before submitting: %d\n", audio_api->buffered());
audio_api->play((u8 *)audio_buffer, 2 * num_audio_samples * 4);
gfx_end_frame();
#ifndef RAPI_RT64
if (config60FPS) {
gfx_start_frame();
patch_interpolations();
send_display_list(gGfxSPTask);
gfx_end_frame();
}
#endif
}
void audio_shutdown(void) {
if (audio_api) {
if (audio_api->shutdown) audio_api->shutdown();
audio_api = NULL;
}
}
void game_deinit(void) {
#ifdef DISCORDRPC
discord_shutdown();
#endif
configfile_save(configfile_name());
controller_shutdown();
audio_shutdown();
gfx_shutdown();
dealloc_dialog_pool();
inited = false;
}
void game_exit(void) {
game_deinit();
#ifndef TARGET_WEB
exit(0);
#endif
}
#ifdef TARGET_WEB
static void em_main_loop(void) {
}
static void request_anim_frame(void (*func)(double time)) {
EM_ASM(requestAnimationFrame(function(time) {
dynCall("vd", $0, [time]);
}), func);
}
static void on_anim_frame(double time) {
static double target_time;
time *= 0.03; // milliseconds to frame count (33.333 ms -> 1)
if (time >= target_time + 10.0) {
// We are lagging 10 frames behind, probably due to coming back after inactivity,
// so reset, with a small margin to avoid potential jitter later.
target_time = time - 0.010;
}
for (int i = 0; i < 2; i++) {
// If refresh rate is 15 Hz or something we might need to generate two frames
if (time >= target_time) {
produce_one_frame();
target_time = target_time + 1.0;
}
}
if (inited) // only continue if the init flag is still set
request_anim_frame(on_anim_frame);
}
#endif
void main_func(char *argv[]) {
static u64 pool[0x165000/8 / 4 * sizeof(void *)];
main_pool_init(pool, pool + sizeof(pool) / sizeof(pool[0]));
gEffectsMemoryPool = mem_pool_init(0x4000, MEMORY_POOL_LEFT);
const char *gamedir = gCLIOpts.GameDir[0] ? gCLIOpts.GameDir : FS_BASEDIR;
const char *userpath = gCLIOpts.SavePath[0] ? gCLIOpts.SavePath : sys_user_path();
fs_init(sys_ropaths, gamedir, userpath);
configfile_load(configfile_name());
alloc_dialog_pool(argv[0], gamedir);
if (gCLIOpts.FullScreen == 1)
configWindow.fullscreen = true;
else if (gCLIOpts.FullScreen == 2)
configWindow.fullscreen = false;
#if defined(WAPI_SDL1) || defined(WAPI_SDL2)
wm_api = &gfx_sdl;
#elif defined(WAPI_DXGI)
wm_api = &gfx_dxgi;
#else
#error No window API!
#endif
#if defined(RAPI_D3D11)
rendering_api = &gfx_direct3d11_api;
# define RAPI_NAME "DirectX 11"
#elif defined(RAPI_D3D12)
rendering_api = &gfx_direct3d12_api;
# define RAPI_NAME "DirectX 12"
#elif defined(RAPI_GL) || defined(RAPI_GL_LEGACY)
rendering_api = &gfx_opengl_api;
# ifdef USE_GLES
# define RAPI_NAME "OpenGL ES"
# else
# define RAPI_NAME "OpenGL"
# endif
#elif defined(RAPI_RT64)
# define RAPI_NAME "RT64"
wm_api = &gfx_rt64_wapi;
rendering_api = &gfx_rt64_rapi;
#else
#error No rendering API!
#endif
char window_title[96] =
"Super Mario 64 Render96ex (" RAPI_NAME ")"
#ifdef NIGHTLY
" nightly " GIT_HASH
#else
" " GIT_HASH
#endif
;
gfx_init(wm_api, rendering_api, window_title);
wm_api->set_keyboard_callbacks(keyboard_on_key_down, keyboard_on_key_up, keyboard_on_all_keys_up);
if (audio_api == NULL && audio_sdl.init())
audio_api = &audio_sdl;
if (audio_api == NULL) {
audio_api = &audio_null;
}
audio_init();
sound_init();
thread5_game_loop(NULL);
inited = true;
// precache data if needed
if (configPrecacheRes) {
fprintf(stdout, "precaching data\n");
fflush(stdout);
gfx_precache_textures();
}
#ifdef DISCORDRPC
discord_init();
#endif
#ifdef TARGET_WEB
emscripten_set_main_loop(em_main_loop, 0, 0);
request_anim_frame(on_anim_frame);
#else
while (true) {
wm_api->main_loop(produce_one_frame);
#ifdef DISCORDRPC
discord_update_rich_presence();
#endif
}
#endif
}
int main(int argc, char *argv[]) {
#ifdef TARGET_SWITCH
initNX();
#endif
parse_cli_opts(argc, argv);
main_func(argv);
#ifdef TARGET_SWITCH
exitNX();
#endif
}