-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathmojozork-sdl3.c
More file actions
481 lines (419 loc) · 18.3 KB
/
mojozork-sdl3.c
File metadata and controls
481 lines (419 loc) · 18.3 KB
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/**
* MojoZork; a simple, just-for-fun implementation of Infocom's Z-Machine.
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
#define SDL_MAIN_USE_CALLBACKS 1
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
#ifdef SDL_PLATFORM_EMSCRIPTEN
#include <emscripten.h>
#endif
#define DRAW_OWN_MOUSE_CURSOR 0
#include "mojozork-libretro.c" // yeah, this is nuts. This app just implements enough of a libretro host to run the libretro core, compiled inline.
static const char *visual_styles[] = { "Standard", "AppleII", "MS-DOS", "Commodore-64" }; // !!! FIXME: move to the libretro core, and build out the cvar from this list, to unify things?
static SDL_Window *window = NULL;
static SDL_Renderer *renderer = NULL;
static SDL_Texture *texture = NULL;
static SDL_Gamepad *gamepad = NULL;
static char *prefpath = NULL;
static float dpimult = 1.0f;
static const char *style = NULL;
static bool retro_init_called = false;
static retro_frame_time_callback_t frame_time_callback_impl = NULL;
static retro_keyboard_event_t keyboard_event_impl = NULL;
static bool game_loaded = false;
static bool visual_style_updated = false;
static SDL_MouseButtonFlags current_mouse_buttons;
static int current_mouse_x, current_mouse_y;
static int prev_mouse_x, prev_mouse_y;
static int mouse_wheel_accumulator;
static SDL_AppResult panic(const char *title, const char *msg)
{
char msgcpy[256];
SDL_strlcpy(msgcpy, msg, sizeof (msgcpy)); // in case this is SDL_GetError() and the buffer changes.
msg = msgcpy;
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%s: %s", title, msg);
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, title, msg, window);
return SDL_APP_FAILURE;
}
static SDL_AppResult usage(void)
{
return panic("USAGE", "<gamedata_path> [--style=STYLENAME]");
}
static void RETRO_CALLCONV log_entry_point(enum retro_log_level level, const char *fmt, ...)
{
SDL_LogPriority prio = SDL_LOG_PRIORITY_INFO;
switch (level) {
#define PRIO(x) case RETRO_LOG_##x: prio = SDL_LOG_PRIORITY_##x; break
PRIO(DEBUG);
PRIO(INFO);
PRIO(WARN);
PRIO(ERROR);
#undef PRIO
default: break;
}
va_list ap;
va_start(ap, fmt);
SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION, prio, fmt, ap);
va_end(ap);
}
static bool RETRO_CALLCONV environment_entry_point(unsigned cmd, void *data)
{
switch (cmd) {
case RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE:
*((bool *) data) = visual_style_updated;
visual_style_updated = false;
return true;
case RETRO_ENVIRONMENT_GET_LOG_INTERFACE:
((struct retro_log_callback *) data)->log = log_entry_point;
return true;
case RETRO_ENVIRONMENT_GET_VARIABLE: {
struct retro_variable *var = (struct retro_variable *) data;
if (SDL_strcmp(var->key, "style") == 0) {
var->value = style;
return true;
}
var->value = NULL;
return false;
}
case RETRO_ENVIRONMENT_SET_PIXEL_FORMAT:
if (*((enum retro_pixel_format *) data) == RETRO_PIXEL_FORMAT_RGB565) {
return true;
}
return false;
case RETRO_ENVIRONMENT_GET_CAN_DUPE:
*((bool *) data) = true;
return true;
case RETRO_ENVIRONMENT_SET_FRAME_TIME_CALLBACK: {
const struct retro_frame_time_callback *ftcb = (const struct retro_frame_time_callback *) data;
frame_time_callback_impl = ftcb->callback;
char hintstr[64];
SDL_snprintf(hintstr, sizeof (hintstr), "%f", (1.0 / ((double) ftcb->reference)) * 1000000.0);
SDL_SetHint(SDL_HINT_MAIN_CALLBACK_RATE, hintstr);
return true;
}
case RETRO_ENVIRONMENT_SET_KEYBOARD_CALLBACK:
keyboard_event_impl = ((struct retro_keyboard_callback *) data)->callback;
return true;
case RETRO_ENVIRONMENT_GET_SAVE_DIRECTORY:
*((const char **) data) = prefpath;
return true;
}
return false;
}
static int16_t RETRO_CALLCONV input_state_entry_point(unsigned port, unsigned device, unsigned index, unsigned id)
{
SDL_assert(port == 0);
SDL_assert(index == 0);
int16_t retval = 0;
switch (device) {
case RETRO_DEVICE_JOYPAD:
if (gamepad) {
switch (id) {
case RETRO_DEVICE_ID_JOYPAD_UP: return SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_DPAD_UP) ? 1 : 0;
case RETRO_DEVICE_ID_JOYPAD_DOWN: return SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_DPAD_DOWN) ? 1 : 0;
case RETRO_DEVICE_ID_JOYPAD_LEFT: return SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_DPAD_LEFT) ? 1 : 0;
case RETRO_DEVICE_ID_JOYPAD_RIGHT: return SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_DPAD_RIGHT) ? 1 : 0;
case RETRO_DEVICE_ID_JOYPAD_A: return SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_SOUTH) ? 1 : 0;
case RETRO_DEVICE_ID_JOYPAD_B: return SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_EAST) ? 1 : 0;
case RETRO_DEVICE_ID_JOYPAD_X: return SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_WEST) ? 1 : 0;
case RETRO_DEVICE_ID_JOYPAD_Y: return SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_NORTH) ? 1 : 0;
case RETRO_DEVICE_ID_JOYPAD_SELECT: return SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_BACK) ? 1 : 0;
case RETRO_DEVICE_ID_JOYPAD_START: return SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_START) ? 1 : 0;
case RETRO_DEVICE_ID_JOYPAD_L: return SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER) ? 1 : 0;
case RETRO_DEVICE_ID_JOYPAD_L2: return (SDL_GetGamepadAxis(gamepad, SDL_GAMEPAD_AXIS_LEFT_TRIGGER) > 20000) ? 1 : 0;
case RETRO_DEVICE_ID_JOYPAD_L3: return SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_LEFT_STICK) ? 1 : 0;
case RETRO_DEVICE_ID_JOYPAD_R: return SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER) ? 1 : 0;
case RETRO_DEVICE_ID_JOYPAD_R2: return (SDL_GetGamepadAxis(gamepad, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER) > 20000) ? 1 : 0;
case RETRO_DEVICE_ID_JOYPAD_R3: return SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_RIGHT_STICK) ? 1 : 0;
break;
}
}
break;
case RETRO_DEVICE_MOUSE: {
switch (id) {
case RETRO_DEVICE_ID_MOUSE_X: retval = (int16_t) (current_mouse_x - prev_mouse_x); prev_mouse_x = current_mouse_x; break;
case RETRO_DEVICE_ID_MOUSE_Y: retval = (int16_t) (current_mouse_y - prev_mouse_y); prev_mouse_y = current_mouse_y; break;
case RETRO_DEVICE_ID_MOUSE_LEFT: retval = (current_mouse_buttons & SDL_BUTTON_LMASK) ? 1 : 0; break;
case RETRO_DEVICE_ID_MOUSE_WHEELUP: if (mouse_wheel_accumulator > 0) { retval = (int16_t) mouse_wheel_accumulator; mouse_wheel_accumulator = 0; } break;
case RETRO_DEVICE_ID_MOUSE_WHEELDOWN: if (mouse_wheel_accumulator < 0) { retval = (int16_t) -mouse_wheel_accumulator; mouse_wheel_accumulator = 0; } break;
break;
}
break;
}
case RETRO_DEVICE_POINTER:
switch (id) {
case RETRO_DEVICE_ID_POINTER_X:
case RETRO_DEVICE_ID_POINTER_Y:
// !!! FIXME: implement me.
break;
}
break;
}
return retval;
}
static void RETRO_CALLCONV input_poll_entry_point(void)
{
// !!! FIXME: poll devices, decide if there was a state change.
}
static void RETRO_CALLCONV video_refresh_entry_point(const void *data, unsigned width, unsigned height, size_t pitch)
{
if (data != NULL) { // NULL==duplicate frame, don't update the existing texture. We'll draw the next frame with what we already have.
const SDL_Rect rect = { 0, 0, width, height };
SDL_UpdateTexture(texture, &rect, data, (int) pitch);
}
}
static void load_game(const char *gamefname)
{
size_t gamedatalen = 0;
void *gamedata = SDL_LoadFile(gamefname, &gamedatalen);
if (!gamedata) {
panic("Couldn't load game file", SDL_GetError());
return;
}
game_loaded = false;
if (!retro_init_called) {
retro_init();
retro_init_called = true;
}
struct retro_game_info gameinfo;
SDL_zero(gameinfo);
gameinfo.path = gamefname;
gameinfo.data = gamedata;
gameinfo.size = gamedatalen;
if (retro_load_game(&gameinfo)) {
game_loaded = true;
} else {
panic("Couldn't load game file", "Game data is apparently corrupt/invalid");
}
SDL_free(gamedata);
}
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
{
retro_api_version();
struct retro_system_info sysinfo;
SDL_zero(sysinfo);
retro_get_system_info(&sysinfo); // strictly speaking, this is called in the wrong sequence verses how libretro normally does it.
SDL_SetAppMetadata("MojoZork SDL3", sysinfo.library_version ? sysinfo.library_version : "0.1", "org.icculus.mojozork");
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD)) {
return panic("SDL_Init failed!", SDL_GetError());
}
const char *gamefname = NULL;
for (int i = 1; i < argc; i++) {
const char *arg = argv[i];
if ((arg[0] == '-') && (arg[1] == '-')) { // command line option?
arg += 2;
if (SDL_strncmp(arg, "style=", 6) == 0) {
style = arg + 6;
} else {
return usage();
}
} else if (gamefname == NULL) {
gamefname = arg;
} else {
return usage();
}
}
bool valid_style = false;
if (style) {
for (int i = 0; i < SDL_arraysize(visual_styles); i++) {
if (SDL_strcasecmp(style, visual_styles[i]) == 0) {
style = visual_styles[i];
valid_style = true;
break;
}
}
}
if (!valid_style) {
style = visual_styles[0];
}
prefpath = SDL_GetPrefPath("icculus.org", "MojoZork");
#ifdef SDL_PLATFORM_EMSCRIPTEN
MAIN_THREAD_EM_ASM({
var persistent_path = UTF8ToString($0);
FS.mount(IDBFS, {}, persistent_path);
FS.syncfs(true, function(err) {
if (err) {
console.log("WARNING: Failed to populate persistent store. Save games likely lost?");
}
});
}, prefpath);
#endif
// no audio at the moment.
//retro_set_audio_sample(retro_audio_sample_t cb) { audio_cb = cb; }
//retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { audio_batch_cb = cb; }
retro_set_input_poll(input_poll_entry_point);
retro_set_input_state(input_state_entry_point);
retro_set_video_refresh(video_refresh_entry_point);
retro_set_environment(environment_entry_point);
if (gamefname) {
load_game(gamefname);
}
struct retro_system_av_info avinfo;
SDL_zero(avinfo);
retro_get_system_av_info(&avinfo);
dpimult = SDL_GetDisplayContentScale(SDL_GetPrimaryDisplay());
if (!SDL_CreateWindowAndRenderer("MojoZork", (int) (avinfo.geometry.base_width * dpimult), (int) (avinfo.geometry.base_height * dpimult), SDL_WINDOW_RESIZABLE, &window, &renderer)) {
return panic("Couldn't create window/renderer", SDL_GetError());
}
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB565, SDL_TEXTUREACCESS_STREAMING, avinfo.geometry.base_width, avinfo.geometry.base_height);
if (!texture) {
return panic("Couldn't create texture", SDL_GetError());
}
SDL_StartTextInput(window); // assume a keyboard until otherwise proven.
SDL_SetRenderLogicalPresentation(renderer, avinfo.geometry.base_width, avinfo.geometry.base_height, SDL_LOGICAL_PRESENTATION_LETTERBOX);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
return SDL_APP_CONTINUE;
}
static void SDLCALL open_on_main_thread(void *userdata)
{
if (userdata) {
load_game((const char *) userdata);
SDL_free(userdata);
}
}
static void SDLCALL file_open_callback(void *userdata, const char * const *filelist, int filter)
{
if (!filelist || !*filelist) {
return; // cancelled or error, ignore it.
}
SDL_RunOnMainThread(open_on_main_thread, SDL_strdup(*filelist), false);
}
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
{
if (event->type == SDL_EVENT_QUIT) {
return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
} else if ( ((event->type == SDL_EVENT_KEY_UP) || (event->type == SDL_EVENT_KEY_DOWN)) && keyboard_event_impl ) {
if ((event->key.key == SDLK_F3) && event->key.down) {
SDL_ShowOpenFileDialog(file_open_callback, NULL, window, NULL, 0, NULL, false);
return SDL_APP_CONTINUE;
} else if ((event->key.key == SDLK_F6) && event->key.down) {
for (int i = 0; i < SDL_arraysize(visual_styles); i++) {
if (SDL_strcmp(style, visual_styles[i]) == 0) {
style = visual_styles[(i + 1) % SDL_arraysize(visual_styles)];
visual_style_updated = true;
return SDL_APP_CONTINUE;
}
}
// uh...shouldn't have gotten here...?
style = visual_styles[0];
visual_style_updated = true;
return SDL_APP_CONTINUE;
}
// we only care about an extremely small set of keys for MojoZork, and we're not going to use text input events atm. It's 1980s input tech!
unsigned int keycode = 0;
switch (event->key.key) {
case SDLK_UP: keycode = RETROK_UP; break;
case SDLK_DOWN: keycode = RETROK_DOWN; break;
case SDLK_PAGEUP: keycode = RETROK_PAGEUP; break;
case SDLK_PAGEDOWN: keycode = RETROK_PAGEDOWN; break;
case SDLK_BACKSPACE: keycode = RETROK_BACKSPACE; break;
case SDLK_KP_ENTER: keycode = RETROK_KP_ENTER; break;
case SDLK_RETURN: keycode = RETROK_RETURN; break;
default: break;
}
if (keycode) {
keyboard_event_impl(event->key.down, keycode, 0, 0);
}
} else if ((event->type == SDL_EVENT_TEXT_INPUT) && keyboard_event_impl) {
const char *text = event->text.text;
Uint32 ch;
while ((ch = SDL_StepUTF8(&text, NULL)) != 0) {
if ((ch >= 32) && (ch <= 127)) {
keyboard_event_impl(true, 0, (unsigned int) ch, 0);
keyboard_event_impl(false, 0, (unsigned int) ch, 0);
}
}
} else if (event->type == SDL_EVENT_MOUSE_WHEEL) {
if (event->wheel.integer_y) {
mouse_wheel_accumulator += event->wheel.integer_y;
}
} else if (event->type == SDL_EVENT_GAMEPAD_ADDED) {
if (!gamepad) {
gamepad = SDL_OpenGamepad(event->gdevice.which);
if (!gamepad) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to open newly-connected gamepad: %s", SDL_GetError());
}
}
} else if (event->type == SDL_EVENT_GAMEPAD_REMOVED) {
if (gamepad && (SDL_GetGamepadID(gamepad) == event->gdevice.which)) {
SDL_CloseGamepad(gamepad);
gamepad = NULL;
}
} else if (event->type == SDL_EVENT_DROP_FILE) {
load_game((const char *) event->drop.data);
} else if (event->type == SDL_EVENT_WINDOW_DISPLAY_SCALE_CHANGED) {
const float newdpimult = SDL_GetWindowDisplayScale(window);
if (newdpimult != dpimult) {
int w, h;
SDL_GetWindowSizeInPixels(window, &w, &h);
w = (int) ((((float) w) / dpimult) * newdpimult);
h = (int) ((((float) h) / dpimult) * newdpimult);
dpimult = newdpimult;
SDL_SetWindowSize(window, w, h);
}
}
return SDL_APP_CONTINUE; /* carry on with the program! */
}
SDL_AppResult SDL_AppIterate(void *appstate)
{
static Uint64 last_iteration_ns = 0;
const Uint64 now_ns = SDL_GetTicksNS();
if (frame_time_callback_impl) {
const Uint64 diff_ns = last_iteration_ns ? (now_ns - last_iteration_ns) : 0;
frame_time_callback_impl((retro_usec_t) (diff_ns / 1000));
}
last_iteration_ns = now_ns;
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
if (game_loaded) {
float fx, fy;
current_mouse_buttons = SDL_GetMouseState(&fx, &fy);
current_mouse_x = (int) fx;
current_mouse_y = (int) fy;
retro_run();
SDL_RenderTexture(renderer, texture, NULL, NULL);
if (save_game_written) { // this is kinda a hack, breaking the libretro core code separation. Maybe provide a VFS?
save_game_written = false;
#ifdef SDL_PLATFORM_EMSCRIPTEN
MAIN_THREAD_EM_ASM({
FS.syncfs(false, function (err) {
if (err) {
console.log("Uhoh, couldn't commit persistent data to write dir!");
console.log("Save games, etc, MIGHT BE LOST!");
}
});
});
#endif
}
} else {
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
#ifdef SDL_PLATFORM_EMSCRIPTEN
const char *msg = "Drag/drop a file to load a Z-Machine program.";
#else
const char *msg = "Press F3 (or drag/drop) to load a Z-Machine program.";
#endif
float y = (480 - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * 3)) / 2.0f;
float x = (640 - (SDL_strlen(msg) * SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE)) / 2.0f;
SDL_RenderDebugText(renderer, x, y, msg);
msg = "Switch between visual styles during the game by pressing F6.";
y += (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * 2);
x = (640 - (SDL_strlen(msg) * SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE)) / 2.0f;
SDL_RenderDebugText(renderer, x, y, msg);
}
SDL_RenderPresent(renderer);
return SDL_APP_CONTINUE; /* carry on with the program! */
}
void SDL_AppQuit(void *appstate, SDL_AppResult result)
{
if (retro_init_called) {
retro_deinit();
}
SDL_CloseGamepad(gamepad);
SDL_DestroyTexture(texture);
SDL_free(prefpath);
}
// end of mojozork-sdl.c ...