-
-
Notifications
You must be signed in to change notification settings - Fork 185
/
Copy pathlibretro.cpp
3711 lines (3265 loc) · 124 KB
/
libretro.cpp
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This file is part of Flycast.
Flycast is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
Flycast is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Flycast. If not, see <https://www.gnu.org/licenses/>.
*/
#include <cstdio>
#include <cstdarg>
#include <math.h>
#include "types.h"
#ifndef _WIN32
#include <sys/time.h>
#endif
#include <mutex>
#ifdef __SWITCH__
#include <stdlib.h>
#include <string.h>
#include "nswitch.h"
#endif
#include <sys/stat.h>
#include <file/file_path.h>
#include <libretro.h>
#if defined(HAVE_OPENGL) || defined(HAVE_OPENGLES)
#include <glsm/glsm.h>
#include "wsi/gl_context.h"
#endif
#ifdef HAVE_VULKAN
#include "rend/vulkan/vulkan_context.h"
#include <libretro_vulkan.h>
#endif
#ifdef HAVE_D3D11
#include <libretro_d3d.h>
#include "rend/dx11/dx11context_lr.h"
#endif
#include "emulator.h"
#include "hw/sh4/sh4_mem.h"
#include "hw/sh4/sh4_sched.h"
#include "hw/sh4/dyna/blockmanager.h"
#include "keyboard_map.h"
#include "hw/maple/maple_cfg.h"
#include "hw/maple/maple_if.h"
#include "hw/maple/maple_cfg.h"
#include "hw/pvr/spg.h"
#include "hw/naomi/naomi_cart.h"
#include "hw/naomi/card_reader.h"
#include "imgread/common.h"
#include "LogManager.h"
#include "cheats.h"
#include "rend/osd.h"
#include "cfg/option.h"
#include "version.h"
#include "rend/transform_matrix.h"
#include "oslib/oslib.h"
constexpr char slash = path_default_slash_c();
#define RETRO_DEVICE_TWINSTICK RETRO_DEVICE_SUBCLASS( RETRO_DEVICE_JOYPAD, 1 )
#define RETRO_DEVICE_TWINSTICK_SATURN RETRO_DEVICE_SUBCLASS( RETRO_DEVICE_JOYPAD, 2 )
#define RETRO_DEVICE_ASCIISTICK RETRO_DEVICE_SUBCLASS( RETRO_DEVICE_JOYPAD, 3 )
#define RETRO_DEVICE_MARACAS RETRO_DEVICE_SUBCLASS( RETRO_DEVICE_JOYPAD, 4 )
#define RETRO_DEVICE_FISHING RETRO_DEVICE_SUBCLASS( RETRO_DEVICE_JOYPAD, 5 )
#define RETRO_DEVICE_POPNMUSIC RETRO_DEVICE_SUBCLASS( RETRO_DEVICE_JOYPAD, 6 )
#define RETRO_DEVICE_RACING RETRO_DEVICE_SUBCLASS( RETRO_DEVICE_JOYPAD, 7 )
#define RETRO_DEVICE_DENSHA RETRO_DEVICE_SUBCLASS( RETRO_DEVICE_JOYPAD, 8 )
#define RETRO_ENVIRONMENT_RETROARCH_START_BLOCK 0x800000
#define RETRO_ENVIRONMENT_SET_SAVE_STATE_IN_BACKGROUND (2 | RETRO_ENVIRONMENT_RETROARCH_START_BLOCK)
/* bool * --
* Boolean value that tells the front end to save states in the
* background or not.
*/
#define RETRO_ENVIRONMENT_POLL_TYPE_OVERRIDE (4 | RETRO_ENVIRONMENT_RETROARCH_START_BLOCK)
/* unsigned * --
* Tells the frontend to override the poll type behavior.
* Allows the frontend to influence the polling behavior of the
* frontend.
*
* Will be unset when retro_unload_game is called.
*
* 0 - Don't Care, no changes, frontend still determines polling type behavior.
* 1 - Early
* 2 - Normal
* 3 - Late
*/
#include "libretro_core_option_defines.h"
#include "libretro_core_options.h"
#include "vmu_xhair.h"
extern void retro_audio_init(void);
extern void retro_audio_deinit(void);
extern void retro_audio_flush_buffer(void);
extern void retro_audio_upload(void);
std::string arcadeFlashPath;
static bool boot_to_bios;
static bool devices_need_refresh = false;
static int device_type[4] = {-1,-1,-1,-1};
static int astick_deadzone = 0;
static int trigger_deadzone = 0;
static bool digital_triggers = false;
static bool allow_service_buttons = false;
static bool haveCardReader;
static bool libretro_supports_bitmasks = false;
static bool categoriesSupported = false;
static bool platformIsDreamcast = true;
static bool platformIsArcade = false;
static bool threadedRenderingEnabled = true;
static bool oitEnabled = false;
#if defined(HAVE_OIT) || defined(HAVE_VULKAN) || defined(HAVE_D3D11)
static bool perPixelChecked = false;
#endif
static bool autoSkipFrameEnabled = false;
#ifdef _OPENMP
static bool textureUpscaleEnabled = false;
#endif
static bool vmuScreenSettingsShown = true;
static bool lightgunSettingsShown = true;
u32 kcode[4] = {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF};
u16 rt[4];
u16 lt[4];
u16 lt2[4];
u16 rt2[4];
u32 vks[4];
s16 joyx[4], joyy[4];
s16 joyrx[4], joyry[4];
s16 joy3x[4], joy3y[4];
// Mouse buttons
// bit 0: Button C
// bit 1: Right button (B)
// bit 2: Left button (A)
// bit 3: Wheel button
u8 mo_buttons[4] = { 0xFF, 0xFF, 0xFF, 0xFF };
// Relative mouse coordinates [-512:511]
float mo_x_delta[4];
float mo_y_delta[4];
float mo_wheel_delta[4];
std::mutex relPosMutex;
// Absolute mouse coordinates
// Range [0:639] [0:479]
// but may be outside this range if the pointer is offscreen or outside the 4:3 window.
s32 mo_x_abs[4];
s32 mo_y_abs[4];
static u32 vib_stop_time[4];
static double vib_strength[4];
static double vib_delta[4];
unsigned per_content_vmus = 0;
static bool first_run = true;
static bool rotate_screen;
static bool rotate_game;
static int framebufferWidth;
static int framebufferHeight;
static int maxFramebufferWidth;
static int maxFramebufferHeight;
static float framebufferAspectRatio = 4.f / 3.f;
float libretro_expected_audio_samples_per_run;
unsigned libretro_vsync_swap_interval = 1;
bool libretro_detect_vsync_swap_interval = false;
static retro_perf_callback perf_cb;
static retro_get_cpu_features_t perf_get_cpu_features_cb;
// Callbacks
static retro_log_printf_t log_cb;
static retro_video_refresh_t video_cb;
static retro_input_poll_t poll_cb;
static retro_input_state_t input_cb;
retro_audio_sample_batch_t audio_batch_cb;
retro_environment_t environ_cb;
static retro_rumble_interface rumble;
static void refresh_devices(bool first_startup);
static void init_disk_control_interface();
static bool read_m3u(const char *file);
static void updateVibration(u32 port, float power, float inclination, u32 durationMs);
static std::string game_data;
static char g_base_name[128];
static char game_dir[1024];
char game_dir_no_slash[1024];
char vmu_dir_no_slash[PATH_MAX];
char content_name[PATH_MAX];
static char g_roms_dir[PATH_MAX];
static std::mutex mtx_serialization;
static bool gl_ctx_resetting = false;
static bool is_dupe;
static u64 startTime;
// Disk swapping
static struct retro_disk_control_callback retro_disk_control_cb;
static struct retro_disk_control_ext_callback retro_disk_control_ext_cb;
static unsigned disk_initial_index = 0;
static std::string disk_initial_path;
static unsigned disk_index = 0;
static std::vector<std::string> disk_paths;
static std::vector<std::string> disk_labels;
static bool disc_tray_open = false;
static bool set_variable_visibility(void);
void retro_set_video_refresh(retro_video_refresh_t cb)
{
video_cb = cb;
}
void retro_set_audio_sample(retro_audio_sample_t cb)
{
// Nothing to do here
}
void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb)
{
audio_batch_cb = cb;
}
void retro_set_input_poll(retro_input_poll_t cb)
{
poll_cb = cb;
}
void retro_set_input_state(retro_input_state_t cb)
{
input_cb = cb;
}
static void input_set_deadzone_stick(int percent)
{
if (percent >= 0 && percent <= 100)
astick_deadzone = (int)(percent * 0.01f * 0x8000);
}
static void input_set_deadzone_trigger(int percent)
{
if (percent >= 0 && percent <= 100)
trigger_deadzone = (int)(percent * 0.01f * 0x8000);
}
void retro_set_environment(retro_environment_t cb)
{
environ_cb = cb;
// An annoyance: retro_set_environment() can be called
// multiple times, and depending upon the current frontend
// state various environment callbacks may be disabled.
// This means the reported 'categories_supported' status
// may change on subsequent iterations. We therefore have
// to record whether 'categories_supported' is true on any
// iteration, and latch the result
bool optionCategoriesSupported = false;
libretro_set_core_options(environ_cb, &optionCategoriesSupported);
categoriesSupported |= optionCategoriesSupported;
struct retro_core_options_update_display_callback update_display_cb;
update_display_cb.callback = set_variable_visibility;
environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_UPDATE_DISPLAY_CALLBACK, &update_display_cb);
static const struct retro_controller_description ports_default[] =
{
{ "Controller", RETRO_DEVICE_JOYPAD },
{ "Arcade Stick", RETRO_DEVICE_ASCIISTICK },
{ "Keyboard", RETRO_DEVICE_KEYBOARD },
{ "Mouse", RETRO_DEVICE_MOUSE },
{ "Light Gun", RETRO_DEVICE_LIGHTGUN },
{ "Twin Stick", RETRO_DEVICE_TWINSTICK },
{ "Saturn Twin-Stick", RETRO_DEVICE_TWINSTICK_SATURN },
{ "Pointer", RETRO_DEVICE_POINTER },
{ "Maracas", RETRO_DEVICE_MARACAS },
{ "Fishing Controller", RETRO_DEVICE_FISHING },
{ "Pop'n Music", RETRO_DEVICE_POPNMUSIC },
{ "Race Controller", RETRO_DEVICE_RACING },
{ "Densha de Go!", RETRO_DEVICE_DENSHA },
{ 0 },
};
static const struct retro_controller_info ports[] = {
{ ports_default, 13 },
{ ports_default, 13 },
{ ports_default, 13 },
{ ports_default, 13 },
{ 0 },
};
environ_cb(RETRO_ENVIRONMENT_SET_CONTROLLER_INFO, (void*)ports);
}
static void retro_keyboard_event(bool down, unsigned keycode, uint32_t character, uint16_t key_modifiers);
// Now comes the interesting stuff
void retro_init()
{
static bool emuInited;
// Logging
struct retro_log_callback log;
if (environ_cb(RETRO_ENVIRONMENT_GET_LOG_INTERFACE, &log))
log_cb = log.log;
else
log_cb = NULL;
LogManager::Init((void *)log_cb);
NOTICE_LOG(BOOT, "retro_init");
if (environ_cb(RETRO_ENVIRONMENT_GET_PERF_INTERFACE, &perf_cb))
perf_get_cpu_features_cb = perf_cb.get_cpu_features;
else
perf_get_cpu_features_cb = NULL;
// Set color mode
unsigned color_mode = RETRO_PIXEL_FORMAT_XRGB8888;
environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &color_mode);
init_kb_map();
struct retro_keyboard_callback kb_callback = { &retro_keyboard_event };
environ_cb(RETRO_ENVIRONMENT_SET_KEYBOARD_CALLBACK, &kb_callback);
if (environ_cb(RETRO_ENVIRONMENT_GET_INPUT_BITMASKS, NULL))
libretro_supports_bitmasks = true;
init_disk_control_interface();
retro_audio_init();
#if defined(__APPLE__)
char *data_dir = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_SAVE_DIRECTORY, &data_dir) && data_dir)
set_user_data_dir(std::string(data_dir) + "/");
#endif
if (!addrspace::reserve())
ERROR_LOG(VMEM, "Cannot reserve memory space");
os_InstallFaultHandler();
MapleConfigMap::UpdateVibration = updateVibration;
#if defined(__APPLE__) || (defined(__GNUC__) && defined(__linux__) && !defined(__ANDROID__))
if (!emuInited)
#else
(void)emuInited;
#endif
emu.init();
emuInited = true;
}
void retro_deinit()
{
INFO_LOG(COMMON, "retro_deinit");
first_run = true;
//When auto-save states are enabled this is needed to prevent the core from shutting down before
//any save state actions are still running - which results in partial saves
{
std::lock_guard<std::mutex> lock(mtx_serialization);
}
os_UninstallFaultHandler();
#if defined(__APPLE__) || (defined(__GNUC__) && defined(__linux__) && !defined(__ANDROID__))
addrspace::release();
#else
emu.term();
#endif
libretro_supports_bitmasks = false;
categoriesSupported = false;
platformIsDreamcast = true;
platformIsArcade = false;
threadedRenderingEnabled = true;
oitEnabled = false;
autoSkipFrameEnabled = false;
#ifdef _OPENMP
textureUpscaleEnabled = false;
#endif
vmuScreenSettingsShown = true;
lightgunSettingsShown = true;
libretro_vsync_swap_interval = 1;
libretro_detect_vsync_swap_interval = false;
LogManager::Shutdown();
retro_audio_deinit();
}
static bool set_variable_visibility(void)
{
struct retro_core_option_display option_display;
struct retro_variable var;
bool updated = false;
bool platformWasDreamcast = platformIsDreamcast;
bool platformWasArcade = platformIsArcade;
platformIsDreamcast = settings.platform.isConsole();
platformIsArcade = settings.platform.isArcade();
// Show/hide platform-dependent options
if (first_run || (platformIsDreamcast != platformWasDreamcast) || (platformIsArcade != platformWasArcade))
{
// Show/hide NAOMI/Atomiswave options
option_display.visible = platformIsArcade;
option_display.key = CORE_OPTION_NAME "_allow_service_buttons";
environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display);
option_display.visible = settings.platform.isNaomi();
option_display.key = CORE_OPTION_NAME "_force_freeplay";
environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display);
// Show/hide Dreamcast options
option_display.visible = platformIsDreamcast;
option_display.key = CORE_OPTION_NAME "_boot_to_bios";
environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display);
option_display.key = CORE_OPTION_NAME "_hle_bios";
environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display);
option_display.key = CORE_OPTION_NAME "_gdrom_fast_loading";
environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display);
option_display.key = CORE_OPTION_NAME "_cable_type";
environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display);
option_display.key = CORE_OPTION_NAME "_broadcast";
environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display);
option_display.key = CORE_OPTION_NAME "_language";
environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display);
option_display.key = CORE_OPTION_NAME "_force_wince";
environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display);
option_display.key = CORE_OPTION_NAME "_per_content_vmus";
environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display);
option_display.key = CORE_OPTION_NAME "_dc_32mb_mod";
environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display);
option_display.key = CORE_OPTION_NAME "_vmu_sound";
environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display);
option_display.visible = platformIsDreamcast || settings.platform.isAtomiswave();
option_display.key = CORE_OPTION_NAME "_emulate_bba";
environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display);
option_display.key = CORE_OPTION_NAME "_upnp";
environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display);
vmuScreenSettingsShown = option_display.visible;
for (unsigned i = 0; i < 4; i++)
{
char key[256];
option_display.key = key;
snprintf(key, sizeof(key), "%s%u%s", CORE_OPTION_NAME "_vmu", i + 1, "_screen_display");
environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display);
snprintf(key, sizeof(key), "%s%u%s", CORE_OPTION_NAME "_vmu", i + 1, "_screen_position");
environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display);
snprintf(key, sizeof(key), "%s%u%s", CORE_OPTION_NAME "_vmu", i + 1, "_screen_size_mult");
environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display);
snprintf(key, sizeof(key), "%s%u%s", CORE_OPTION_NAME "_vmu", i + 1, "_pixel_on_color");
environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display);
snprintf(key, sizeof(key), "%s%u%s", CORE_OPTION_NAME "_vmu", i + 1, "_pixel_off_color");
environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display);
snprintf(key, sizeof(key), "%s%u%s", CORE_OPTION_NAME "_vmu", i + 1, "_screen_opacity");
environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display);
}
// Show/hide manual option visibility toggles
// > Only show if categories are not supported
option_display.visible = platformIsDreamcast && !categoriesSupported;
option_display.key = CORE_OPTION_NAME "_show_vmu_screen_settings";
environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display);
updated = true;
}
// Show/hide additional manual option visibility toggles
// > Only show if categories are not supported
if (first_run)
{
option_display.visible = !categoriesSupported;
option_display.key = CORE_OPTION_NAME "_show_lightgun_settings";
environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display);
updated = true;
}
// Show/hide settings-dependent options
// Only for threaded renderer
bool threadedRenderingWasEnabled = threadedRenderingEnabled;
threadedRenderingEnabled = true;
var.key = CORE_OPTION_NAME "_threaded_rendering";
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value && !strcmp(var.value, "disabled"))
threadedRenderingEnabled = false;
if (first_run || (threadedRenderingEnabled != threadedRenderingWasEnabled))
{
option_display.visible = threadedRenderingEnabled;
option_display.key = CORE_OPTION_NAME "_auto_skip_frame";
environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display);
updated = true;
}
#if defined(HAVE_OIT) || defined(HAVE_VULKAN) || defined(HAVE_D3D11)
// Only for per-pixel renderers
bool oitWasEnabled = oitEnabled;
oitEnabled = false;
var.key = CORE_OPTION_NAME "_alpha_sorting";
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value && !strcmp(var.value, "per-pixel (accurate)"))
oitEnabled = true;
if (first_run || (oitEnabled != oitWasEnabled))
{
option_display.visible = oitEnabled;
option_display.key = CORE_OPTION_NAME "_oit_abuffer_size";
environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display);
option_display.key = CORE_OPTION_NAME "_oit_layers";
environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display);
updated = true;
}
#endif
#ifdef _OPENMP
// Only if texture upscaling is enabled
bool textureUpscaleWasEnabled = textureUpscaleEnabled;
textureUpscaleEnabled = false;
var.key = CORE_OPTION_NAME "_texupscale";
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value && strcmp(var.value, "1"))
textureUpscaleEnabled = true;
if (first_run || (textureUpscaleEnabled != textureUpscaleWasEnabled))
{
option_display.visible = textureUpscaleEnabled;
option_display.key = CORE_OPTION_NAME "_texupscale_max_filtered_texture_size";
environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display);
updated = true;
}
#endif
// Only if automatic frame skipping is disabled
bool autoSkipFrameWasEnabled = autoSkipFrameEnabled;
autoSkipFrameEnabled = false;
var.key = CORE_OPTION_NAME "_auto_skip_frame";
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value && strcmp(var.value, "disabled"))
autoSkipFrameEnabled = true;
if (first_run ||
(autoSkipFrameEnabled != autoSkipFrameWasEnabled) ||
(threadedRenderingEnabled != threadedRenderingWasEnabled))
{
option_display.visible = (!autoSkipFrameEnabled || !threadedRenderingEnabled);
option_display.key = CORE_OPTION_NAME "_detect_vsync_swap_interval";
environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display);
updated = true;
}
// Show/hide expansion slots options
if (devices_need_refresh)
{
for (int i = 0; i < 4; i++)
{
char key[64] = {0};
option_display.key = key;
// Show expansion slot 1 options only for DC with these devices
option_display.visible = platformIsDreamcast
&& (config::MapleMainDevices[i] == MDT_SegaController
|| config::MapleMainDevices[i] == MDT_LightGun
|| config::MapleMainDevices[i] == MDT_TwinStick
|| config::MapleMainDevices[i] == MDT_AsciiStick
|| config::MapleMainDevices[i] == MDT_RacingController);
snprintf(key, sizeof(key), CORE_OPTION_NAME "_device_port%d_slot1", i + 1);
environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display);
// Only the regular controller has 2 expansion slots
option_display.visible = platformIsDreamcast && config::MapleMainDevices[i] == MDT_SegaController;
snprintf(key, sizeof(key), CORE_OPTION_NAME "_device_port%d_slot2", i + 1);
environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display);
}
updated = true;
}
// If categories are supported, no further action is required
if (categoriesSupported)
return updated;
// Show/hide VMU screen options
bool vmuScreenSettingsWereShown = vmuScreenSettingsShown;
if (platformIsDreamcast)
{
vmuScreenSettingsShown = true;
var.key = CORE_OPTION_NAME "_show_vmu_screen_settings";
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value && !strcmp(var.value, "disabled"))
vmuScreenSettingsShown = false;
}
else
vmuScreenSettingsShown = false;
if (first_run || (vmuScreenSettingsShown != vmuScreenSettingsWereShown))
{
option_display.visible = vmuScreenSettingsShown;
for (unsigned i = 0; i < 4; i++)
{
char key[256];
option_display.key = key;
snprintf(key, sizeof(key), "%s%u%s", CORE_OPTION_NAME "_vmu", i + 1, "_screen_display");
environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display);
snprintf(key, sizeof(key), "%s%u%s", CORE_OPTION_NAME "_vmu", i + 1, "_screen_position");
environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display);
snprintf(key, sizeof(key), "%s%u%s", CORE_OPTION_NAME "_vmu", i + 1, "_screen_size_mult");
environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display);
snprintf(key, sizeof(key), "%s%u%s", CORE_OPTION_NAME "_vmu", i + 1, "_pixel_on_color");
environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display);
snprintf(key, sizeof(key), "%s%u%s", CORE_OPTION_NAME "_vmu", i + 1, "_pixel_off_color");
environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display);
snprintf(key, sizeof(key), "%s%u%s", CORE_OPTION_NAME "_vmu", i + 1, "_screen_opacity");
environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display);
}
updated = true;
}
// Show/hide light gun options
bool lightgunSettingsWereShown = lightgunSettingsShown;
lightgunSettingsShown = true;
var.key = CORE_OPTION_NAME "_show_lightgun_settings";
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value && !strcmp(var.value, "disabled"))
lightgunSettingsShown = false;
if (first_run || (lightgunSettingsShown != lightgunSettingsWereShown))
{
option_display.visible = lightgunSettingsShown;
option_display.key = CORE_OPTION_NAME "_lightgun_crosshair_size_scaling";
environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display);
for (unsigned i = 0; i < 4; i++)
{
char key[256];
option_display.key = key;
snprintf(key, sizeof(key), "%s%u%s", CORE_OPTION_NAME "_lightgun", i + 1, "_crosshair");
environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display);
}
updated = true;
}
return updated;
}
static void setGameGeometry(retro_game_geometry& geometry)
{
geometry.aspect_ratio = framebufferAspectRatio;
if (rotate_screen)
geometry.aspect_ratio = 1 / geometry.aspect_ratio;
geometry.max_width = std::max(framebufferHeight * 16 / 9, framebufferWidth);
geometry.max_height = geometry.max_width;
// Avoid gigantic window size at startup
geometry.base_width = 640;
geometry.base_height = 480;
}
void setAVInfo(retro_system_av_info& avinfo)
{
double sample_rate = 44100.0;
double fps = SPG_CONTROL.isNTSC() ? 59.94 : SPG_CONTROL.isPAL() ? 50.0 : 60.0;
setGameGeometry(avinfo.geometry);
avinfo.timing.sample_rate = sample_rate;
avinfo.timing.fps = fps / (double)libretro_vsync_swap_interval;
libretro_expected_audio_samples_per_run = sample_rate / fps;
}
void retro_resize_renderer(int w, int h, float aspectRatio)
{
if (w == framebufferWidth && h == framebufferHeight && aspectRatio == framebufferAspectRatio)
return;
framebufferWidth = w;
framebufferHeight = h;
framebufferAspectRatio = aspectRatio;
bool avinfoNeeded = framebufferHeight > maxFramebufferHeight || framebufferWidth > maxFramebufferWidth;
maxFramebufferHeight = std::max(maxFramebufferHeight, framebufferHeight);
maxFramebufferWidth = std::max(maxFramebufferWidth, framebufferWidth);
if (avinfoNeeded)
{
retro_system_av_info avinfo;
setAVInfo(avinfo);
environ_cb(RETRO_ENVIRONMENT_SET_SYSTEM_AV_INFO, &avinfo);
}
else
{
retro_game_geometry geometry;
setGameGeometry(geometry);
environ_cb(RETRO_ENVIRONMENT_SET_GEOMETRY, &geometry);
}
}
static void setRotation()
{
int rotation = 0;
if (rotate_game)
{
if (!rotate_screen)
rotation = 1;
rotate_screen = !rotate_screen;
}
else
{
if (rotate_screen)
rotation = 3;
}
environ_cb(RETRO_ENVIRONMENT_SET_ROTATION, &rotation);
}
static void update_variables(bool first_startup)
{
bool wasThreadedRendering = config::ThreadedRendering;
bool prevRotateScreen = rotate_screen;
bool prevDetectVsyncSwapInterval = libretro_detect_vsync_swap_interval;
bool emulateBba = config::EmulateBBA;
MapleDeviceType MapleExpansionDevicesPrev[2] = { MDT_None, MDT_None };
config::Settings::instance().setRetroEnvironment(environ_cb);
config::Settings::instance().setOptionDefinitions(option_defs_us);
config::Settings::instance().load(false);
retro_variable var;
var.key = CORE_OPTION_NAME "_per_content_vmus";
unsigned previous_per_content_vmus = per_content_vmus;
per_content_vmus = 0;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
{
if (!strcmp("VMU A1", var.value))
per_content_vmus = 1;
else if (!strcmp("All VMUs", var.value))
per_content_vmus = 2;
}
if (!first_startup && per_content_vmus != previous_per_content_vmus
&& settings.platform.isConsole())
{
// Recreate the VMUs so that the save location is taken into account.
// Don't do this at startup because we don't know the system type yet
// and the VMUs haven't been created anyway
maple_ReconnectDevices();
}
var.key = CORE_OPTION_NAME "_screen_rotation";
rotate_screen = false;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value && !strcmp("vertical", var.value))
rotate_screen = true;
var.key = CORE_OPTION_NAME "_internal_resolution";
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
{
char str[100];
snprintf(str, sizeof(str), "%s", var.value);
char *pch = strtok(str, "x");
pch = strtok(NULL, "x");
if (pch != nullptr)
config::RenderResolution = strtoul(pch, NULL, 0);
DEBUG_LOG(COMMON, "Got height: %u", (int)config::RenderResolution);
}
var.key = CORE_OPTION_NAME "_boot_to_bios";
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
{
if (!strcmp(var.value, "enabled"))
boot_to_bios = true;
else if (!strcmp(var.value, "disabled"))
boot_to_bios = false;
}
else
boot_to_bios = false;
var.key = CORE_OPTION_NAME "_alpha_sorting";
var.value = nullptr;
RenderType previous_renderer = config::RendererType;
environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var);
if (var.value != nullptr && !strcmp(var.value, "per-pixel (accurate)"))
{
switch (config::RendererType)
{
case RenderType::Vulkan:
config::RendererType = RenderType::Vulkan_OIT;
break;
case RenderType::DirectX11:
config::RendererType = RenderType::DirectX11_OIT;
break;
case RenderType::OpenGL:
config::RendererType = RenderType::OpenGL_OIT;
break;
default:
break;
}
config::PerStripSorting = false; // Not used
}
else
{
switch (config::RendererType)
{
case RenderType::Vulkan_OIT:
config::RendererType = RenderType::Vulkan;
break;
case RenderType::DirectX11_OIT:
config::RendererType = RenderType::DirectX11;
break;
case RenderType::OpenGL_OIT:
config::RendererType = RenderType::OpenGL;
break;
default:
break;
}
config::PerStripSorting = var.value != nullptr && !strcmp(var.value, "per-strip (fast, least accurate)");
}
if (!first_startup && previous_renderer != config::RendererType) {
rend_term_renderer();
rend_init_renderer();
}
#if defined(HAVE_OIT) || defined(HAVE_VULKAN) || defined(HAVE_D3D11)
var.key = CORE_OPTION_NAME "_oit_abuffer_size";
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
{
if (!strcmp(var.value, "512MB"))
config::PixelBufferSize = 0x20000000u;
else if (!strcmp(var.value, "1GB"))
config::PixelBufferSize = 0x40000000u;
else if (!strcmp(var.value, "2GB"))
config::PixelBufferSize = 0x7ff00000u;
else if (!strcmp(var.value, "4GB"))
config::PixelBufferSize = 0xFFFFFFFFu;
else
config::PixelBufferSize = 0x20000000u;
}
else
config::PixelBufferSize = 0x20000000u;
#endif
if ((config::AutoSkipFrame != 0) && config::ThreadedRendering)
libretro_detect_vsync_swap_interval = false;
else
{
var.key = CORE_OPTION_NAME "_detect_vsync_swap_interval";
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
{
if (!strcmp(var.value, "enabled"))
libretro_detect_vsync_swap_interval = true;
else if (!strcmp(var.value, "disabled"))
libretro_detect_vsync_swap_interval = false;
}
else
libretro_detect_vsync_swap_interval = false;
}
if (first_startup)
{
if (config::ThreadedRendering)
{
bool save_state_in_background = true ;
unsigned poll_type_early = 1; /* POLL_TYPE_EARLY */
environ_cb(RETRO_ENVIRONMENT_SET_SAVE_STATE_IN_BACKGROUND, &save_state_in_background);
environ_cb(RETRO_ENVIRONMENT_POLL_TYPE_OVERRIDE, &poll_type_early);
}
config::Cable = 3;
var.key = CORE_OPTION_NAME "_cable_type";
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
{
if (!strcmp("VGA", var.value))
config::Cable = 0;
else if (!strcmp("TV (RGB)", var.value))
config::Cable = 2;
}
}
var.key = CORE_OPTION_NAME "_analog_stick_deadzone";
var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
input_set_deadzone_stick( atoi( var.value ) );
var.key = CORE_OPTION_NAME "_trigger_deadzone";
var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
input_set_deadzone_trigger( atoi( var.value ) );
var.key = CORE_OPTION_NAME "_digital_triggers";
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
{
if (!strcmp("enabled", var.value))
digital_triggers = true;
else
digital_triggers = false;
}
else
digital_triggers = false;
var.key = CORE_OPTION_NAME "_allow_service_buttons";
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
{
if (!strcmp("enabled", var.value))
allow_service_buttons = true;
else
allow_service_buttons = false;
}
else
allow_service_buttons = false;
var.key = CORE_OPTION_NAME "_lightgun_crosshair_size_scaling";
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
lightgun_crosshair_size = (float)LIGHTGUN_CROSSHAIR_SIZE * std::stof(var.value) / 100.f;
else
lightgun_crosshair_size = (float)LIGHTGUN_CROSSHAIR_SIZE;
char key[256];
key[0] = '\0';
var.key = key ;
for (int i = 0 ; i < 4 ; i++)
{
if (!first_startup && settings.platform.isConsole())
{
MapleExpansionDevicesPrev[0] = config::MapleExpansionDevices[i][0];
MapleExpansionDevicesPrev[1] = config::MapleExpansionDevices[i][1];
// Check slot options for these devices only, anything else has no slot
if (config::MapleMainDevices[i] == MDT_SegaController
|| config::MapleMainDevices[i] == MDT_LightGun
|| config::MapleMainDevices[i] == MDT_TwinStick
|| config::MapleMainDevices[i] == MDT_AsciiStick
|| config::MapleMainDevices[i] == MDT_RacingController)
{
for (int slot = 0; slot < 2; slot++)
{
// Only regular controller has a 2nd slot
if (slot == 1 && config::MapleMainDevices[i] != MDT_SegaController)
{
config::MapleExpansionDevices[i][1] = MDT_None;
continue;
}
snprintf(key, sizeof(key), CORE_OPTION_NAME "_device_port%d_slot%d", i + 1, slot + 1);
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
{
if (!strcmp("VMU", var.value))
config::MapleExpansionDevices[i][slot] = MDT_SegaVMU;
else if (!strcmp("Purupuru", var.value))
config::MapleExpansionDevices[i][slot] = MDT_PurupuruPack;
else if (!strcmp("None", var.value))
config::MapleExpansionDevices[i][slot] = MDT_None;
}
else if (slot == 0) // Default to VMU in case the above is false somehow
config::MapleExpansionDevices[i][0] = MDT_SegaVMU;
else if (slot == 1) // Default to Purupuru
config::MapleExpansionDevices[i][1] = MDT_PurupuruPack;
}
}
else
{
config::MapleExpansionDevices[i][0] = MDT_None;
config::MapleExpansionDevices[i][1] = MDT_None;
}
if (MapleExpansionDevicesPrev[0] != config::MapleExpansionDevices[i][0]
|| MapleExpansionDevicesPrev[1] != config::MapleExpansionDevices[i][1])
devices_need_refresh = true;
}
lightgun_params[i].offscreen = true;
lightgun_params[i].x = 0;
lightgun_params[i].y = 0;
lightgun_params[i].dirty = true;
lightgun_params[i].colour = LIGHTGUN_COLOR_OFF;
snprintf(key, sizeof(key), CORE_OPTION_NAME "_lightgun%d_crosshair", i+1) ;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value )
{
if (!strcmp("disabled", var.value))
lightgun_params[i].colour = LIGHTGUN_COLOR_OFF;
else if (!strcmp("White", var.value))