-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
frontend.cpp
2901 lines (2336 loc) · 89.5 KB
/
frontend.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
#include "frontend.h"
#include <algorithm>
#include <array>
#include <cerrno>
#include <charconv>
#include <climits> // For INT_MAX.
#include <cstddef>
#include <filesystem>
#include <format>
#include <forward_list>
#include <functional>
#include <iterator>
#include <list>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
#include "SDL.h"
#include "clownmdemu-frontend-common/clownmdemu/clowncommon/clowncommon.h"
#include "clownmdemu-frontend-common/clownmdemu/clownmdemu.h"
#include "libraries/imgui/imgui.h"
#include "libraries/imgui/backends/imgui_impl_sdl2.h"
#include "libraries/imgui/backends/imgui_impl_sdlrenderer2.h"
#include "libraries/inih/ini.h"
#include "inconsolata-regular.h"
#include "noto-sans-regular.h"
#include "cd-reader.h"
#include "debug-fm.h"
#include "debug-frontend.h"
#include "debug-log.h"
#include "debug-m68k.h"
#include "debug-memory.h"
#include "debug-pcm.h"
#include "debug-psg.h"
#include "debug-vdp.h"
#include "debug-z80.h"
#include "disassembler.h"
#include "emulator-instance.h"
#include "file-utilities.h"
#include "window.h"
#ifndef __EMSCRIPTEN__
#define FILE_PATH_SUPPORT
#endif
#define VERSION "v0.7.1"
static constexpr unsigned int INITIAL_WINDOW_WIDTH = 320 * 2;
static constexpr unsigned int INITIAL_WINDOW_HEIGHT = 224 * 2;
static constexpr unsigned int FRAMEBUFFER_WIDTH = 320;
static constexpr unsigned int FRAMEBUFFER_HEIGHT = 240 * 2; // *2 because of double-resolution mode.
static constexpr char DEFAULT_TITLE[] = "clownmdemu";
struct Input
{
unsigned int bound_joypad = 0;
std::array<unsigned char, CLOWNMDEMU_BUTTON_MAX> buttons = {0};
unsigned char fast_forward = 0;
unsigned char rewind = 0;
};
struct ControllerInput
{
SDL_JoystickID joystick_instance_id;
Sint16 left_stick_x = 0;
Sint16 left_stick_y = 0;
std::array<bool, 4> left_stick = {false};
std::array<bool, 4> dpad = {false};
bool left_trigger = false;
bool right_trigger = false;
Input input;
ControllerInput(const SDL_JoystickID joystick_instance_id) : joystick_instance_id(joystick_instance_id) {}
};
#ifdef FILE_PATH_SUPPORT
struct RecentSoftware
{
bool is_cd_file;
std::filesystem::path path;
};
#endif
enum InputBinding
{
INPUT_BINDING_NONE,
INPUT_BINDING_CONTROLLER_UP,
INPUT_BINDING_CONTROLLER_DOWN,
INPUT_BINDING_CONTROLLER_LEFT,
INPUT_BINDING_CONTROLLER_RIGHT,
INPUT_BINDING_CONTROLLER_A,
INPUT_BINDING_CONTROLLER_B,
INPUT_BINDING_CONTROLLER_C,
INPUT_BINDING_CONTROLLER_X,
INPUT_BINDING_CONTROLLER_Y,
INPUT_BINDING_CONTROLLER_Z,
INPUT_BINDING_CONTROLLER_START,
INPUT_BINDING_CONTROLLER_MODE,
INPUT_BINDING_PAUSE,
INPUT_BINDING_RESET,
INPUT_BINDING_FAST_FORWARD,
#ifdef CLOWNMDEMU_FRONTEND_REWINDING
INPUT_BINDING_REWIND,
#endif
INPUT_BINDING_QUICK_SAVE_STATE,
INPUT_BINDING_QUICK_LOAD_STATE,
INPUT_BINDING_TOGGLE_FULLSCREEN,
INPUT_BINDING_TOGGLE_CONTROL_PAD,
INPUT_BINDING__TOTAL
};
static Input keyboard_input;
static std::forward_list<ControllerInput> controller_input_list;
static std::array<InputBinding, SDL_NUM_SCANCODES> keyboard_bindings; // TODO: `SDL_NUM_SCANCODES` is an internal macro, so use something standard!
static std::array<InputBinding, SDL_NUM_SCANCODES> keyboard_bindings_cached; // TODO: `SDL_NUM_SCANCODES` is an internal macro, so use something standard!
static std::array<bool, SDL_NUM_SCANCODES> key_pressed; // TODO: `SDL_NUM_SCANCODES` is an internal macro, so use something standard!
#ifdef FILE_PATH_SUPPORT
static std::list<RecentSoftware> recent_software_list;
#endif
static std::filesystem::path drag_and_drop_filename;
static bool emulator_has_focus; // Used for deciding when to pass inputs to the emulator.
static bool emulator_paused;
static bool emulator_frame_advance;
static bool quick_save_exists;
static EmulatorInstance::State quick_save_state;
static float dpi_scale;
static unsigned int frame_counter;
static ImFont *monospace_font;
static bool use_vsync;
static bool integer_screen_scaling;
static bool tall_double_resolution_mode;
static ImGuiStyle style_backup;
static Frontend::FrameRateCallback frame_rate_callback;
static DebugLog debug_log(dpi_scale, monospace_font);
static FileUtilities file_utilities(debug_log);
static std::optional<Window> window;
static std::optional<EmulatorInstance> emulator;
static std::optional<DebugFM> debug_fm;
static std::optional<DebugFrontend> debug_frontend;
static std::optional<DebugM68k> debug_m68k;
static std::optional<DebugMemory> debug_memory;
static std::optional<DebugPCM> debug_pcm;
static std::optional<DebugPSG> debug_psg;
static std::optional<DebugVDP> debug_vdp;
static std::optional<DebugZ80> debug_z80;
// Manages whether the program exits or not.
static bool quit;
// Used for tracking when to pop the emulation display out into its own little window.
static bool pop_out;
static bool debug_log_active;
static bool debug_frontend_active;
static bool m68k_status;
static bool mcd_m68k_status;
static bool z80_status;
static bool m68k_ram_viewer;
static bool z80_ram_viewer;
static bool word_ram_viewer;
static bool prg_ram_viewer;
static bool wave_ram_viewer;
static bool vdp_registers;
static bool sprite_list;
static bool sprite_viewer;
static bool window_plane_viewer;
static bool plane_a_viewer;
static bool plane_b_viewer;
static bool vram_viewer;
static bool cram_viewer;
static bool fm_status;
static bool psg_status;
static bool pcm_status;
static bool other_status;
static bool debugging_toggles_menu;
static bool disassembler;
static bool options_menu;
static bool about_menu;
#ifndef NDEBUG
static bool dear_imgui_demo_window;
#endif
static bool emulator_on, emulator_running;
///////////
// Fonts //
///////////
static constexpr float UNSCALED_FONT_SIZE = 16.0f;
static unsigned int CalculateFontSize()
{
// Note that we are purposefully flooring, as Dear ImGui's docs recommend.
return static_cast<unsigned int>(UNSCALED_FONT_SIZE * dpi_scale);
}
static void ReloadFonts(const unsigned int font_size)
{
ImGuiIO &io = ImGui::GetIO();
io.Fonts->Clear();
ImGui_ImplSDLRenderer2_DestroyFontsTexture();
ImFontConfig font_cfg = ImFontConfig();
SDL_snprintf(font_cfg.Name, IM_ARRAYSIZE(font_cfg.Name), "Noto Sans Regular, %upx", font_size);
io.Fonts->AddFontFromMemoryCompressedTTF(noto_sans_regular_compressed_data, noto_sans_regular_compressed_size, static_cast<float>(font_size), &font_cfg);
SDL_snprintf(font_cfg.Name, IM_ARRAYSIZE(font_cfg.Name), "Inconsolata Regular, %upx", font_size);
monospace_font = io.Fonts->AddFontFromMemoryCompressedTTF(inconsolata_regular_compressed_data, inconsolata_regular_compressed_size, static_cast<float>(font_size), &font_cfg);
}
////////////////////////////
// Emulator Functionality //
////////////////////////////
static cc_bool ReadInputCallback(const cc_u8f player_id, const ClownMDEmu_Button button_id)
{
SDL_assert(player_id < 2);
cc_bool value = cc_false;
// Don't use inputs that are for Dear ImGui
if (emulator_has_focus)
{
// First, try to obtain the input from the keyboard.
if (keyboard_input.bound_joypad == player_id)
value |= keyboard_input.buttons[button_id] != 0 ? cc_true : cc_false;
}
if ((ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) == 0)
{
// Then, try to obtain the input from the controllers.
for (const auto &controller_input : controller_input_list)
if (controller_input.input.bound_joypad == player_id)
value |= controller_input.input.buttons[button_id] != 0 ? cc_true : cc_false;
}
return value;
}
#ifdef FILE_PATH_SUPPORT
static void AddToRecentSoftware(const std::filesystem::path &path, const bool is_cd_file, const bool add_to_end)
{
// If the path already exists in the list, then move it to the start of the list.
for (auto recent_software = recent_software_list.begin(); recent_software != recent_software_list.end(); ++recent_software)
{
if (recent_software->path == path)
{
if (recent_software != recent_software_list.begin())
recent_software_list.splice(recent_software_list.begin(), recent_software_list, recent_software, std::next(recent_software));
return;
}
}
// If the list is 10 items long, then discard the last item before we add a new one.
if (recent_software_list.size() == 10)
recent_software_list.pop_back();
// Add the file to the list of recent software.
if (add_to_end)
recent_software_list.emplace_back(is_cd_file, path);
else
recent_software_list.emplace_front(is_cd_file, path);
}
#endif
// Manages whether the emulator runs at a higher speed or not.
static bool fast_forward_in_progress;
static void UpdateFastForwardStatus()
{
const bool previous_fast_forward_in_progress = fast_forward_in_progress;
fast_forward_in_progress = keyboard_input.fast_forward;
for (const auto &controller_input : controller_input_list)
fast_forward_in_progress |= controller_input.input.fast_forward != 0;
if (previous_fast_forward_in_progress != fast_forward_in_progress)
{
// Disable V-sync so that 60Hz displays aren't locked to 1x speed while fast-forwarding
if (use_vsync)
SDL_RenderSetVSync(window->GetRenderer(), !fast_forward_in_progress);
}
}
#ifdef CLOWNMDEMU_FRONTEND_REWINDING
static void UpdateRewindStatus()
{
bool will_rewind = keyboard_input.rewind;
for (const auto &controller_input : controller_input_list)
will_rewind |= controller_input.input.rewind != 0;
emulator->Rewind(will_rewind);
}
#endif
//////////////////////////
// Upscaled Framebuffer //
//////////////////////////
static SDL_Texture *framebuffer_texture_upscaled;
static unsigned int framebuffer_texture_upscaled_width;
static unsigned int framebuffer_texture_upscaled_height;
static void RecreateUpscaledFramebuffer(const unsigned int display_width, const unsigned int display_height)
{
static unsigned int previous_framebuffer_size_factor = 0;
const unsigned int framebuffer_size_factor = std::max(1u, std::min(CC_DIVIDE_CEILING(display_width, 640), CC_DIVIDE_CEILING(display_height, 480)));
if (framebuffer_texture_upscaled == nullptr || framebuffer_size_factor != previous_framebuffer_size_factor)
{
previous_framebuffer_size_factor = framebuffer_size_factor;
framebuffer_texture_upscaled_width = 640 * framebuffer_size_factor;
framebuffer_texture_upscaled_height = 480 * framebuffer_size_factor;
SDL_DestroyTexture(framebuffer_texture_upscaled); // It should be safe to pass nullptr to this
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
framebuffer_texture_upscaled = SDL_CreateTexture(window->GetRenderer(), SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, framebuffer_texture_upscaled_width, framebuffer_texture_upscaled_height);
if (framebuffer_texture_upscaled == nullptr)
{
debug_log.Log("SDL_CreateTexture failed with the following message - '%s'", SDL_GetError());
}
else
{
// Disable blending, since we don't need it
if (SDL_SetTextureBlendMode(framebuffer_texture_upscaled, SDL_BLENDMODE_NONE) < 0)
debug_log.Log("SDL_SetTextureBlendMode failed with the following message - '%s'", SDL_GetError());
}
}
}
static bool GetUpscaledFramebufferSize(unsigned int &width, unsigned int &height)
{
if (framebuffer_texture_upscaled == nullptr)
return false;
SDL_QueryTexture(framebuffer_texture_upscaled, nullptr, nullptr, reinterpret_cast<int*>(&width), reinterpret_cast<int*>(&height));
return true;
}
///////////
// Misc. //
///////////
static void SetWindowTitleToSoftwareName()
{
const std::string name = emulator->GetSoftwareName();
// Use the default title if the ROM does not provide a name.
// Micro Machines is one game that lacks a name.
SDL_SetWindowTitle(window->GetSDLWindow(), name.empty() ? DEFAULT_TITLE : name.c_str());
}
static void LoadCartridgeFile(const std::vector<unsigned char> &&file_buffer)
{
quick_save_exists = false;
emulator->LoadCartridgeFile(std::move(file_buffer));
SetWindowTitleToSoftwareName();
}
static bool LoadCartridgeFile([[maybe_unused]] const std::filesystem::path* const path, const SDL::RWops &file)
{
std::vector<unsigned char> file_buffer;
if (!file_utilities.LoadFileToBuffer(file_buffer, file))
{
debug_log.Log("Could not load the cartridge file");
window->ShowErrorMessageBox("Failed to load the cartridge file.");
return false;
}
#ifdef FILE_PATH_SUPPORT
if (path != nullptr)
AddToRecentSoftware(*path, false, false);
#endif
LoadCartridgeFile(std::move(file_buffer));
return true;
}
static bool LoadCartridgeFile(const std::filesystem::path &path)
{
const SDL::RWops file = SDL::RWFromFile(path, "rb");
if (file == nullptr)
{
debug_log.Log("Could not load the cartridge file");
window->ShowErrorMessageBox("Failed to load the cartridge file.");
return false;
}
return LoadCartridgeFile(&path, file);
}
static bool LoadCDFile(const std::filesystem::path* const path, SDL::RWops &&file)
{
#ifdef FILE_PATH_SUPPORT
if (path != nullptr)
AddToRecentSoftware(*path, true, false);
#else
static_cast<void>(path);
#endif
// Load the CD.
if (!emulator->LoadCDFile(std::move(file), *path))
return false;
SetWindowTitleToSoftwareName();
return true;
}
#ifdef FILE_PATH_SUPPORT
static bool LoadCDFile(const std::filesystem::path &path)
{
SDL::RWops file = SDL::RWFromFile(path, "rb");
if (file == nullptr)
{
debug_log.Log("Could not load the CD file");
window->ShowErrorMessageBox("Failed to load the CD file.");
return false;
}
return LoadCDFile(&path, std::move(file));
}
static bool LoadSoftwareFile(const bool is_cd_file, const std::filesystem::path &path)
{
if (is_cd_file)
return LoadCDFile(path);
else
return LoadCartridgeFile(path);
}
#endif
static bool LoadSaveState(const std::vector<unsigned char> &file_buffer)
{
if (!emulator->LoadSaveStateFile(file_buffer))
{
debug_log.Log("Could not load save state file");
window->ShowErrorMessageBox("Could not load save state file.");
return false;
}
emulator_paused = false;
return true;
}
static bool LoadSaveState(const SDL::RWops &file)
{
std::vector<unsigned char> file_buffer;
if (!file_utilities.LoadFileToBuffer(file_buffer, file))
{
debug_log.Log("Could not load save state file");
window->ShowErrorMessageBox("Could not load save state file.");
return false;
}
return LoadSaveState(file_buffer);
}
#ifdef FILE_PATH_SUPPORT
static bool CreateSaveState(const std::filesystem::path &path)
{
bool success = true;
const SDL::RWops file = SDL::RWFromFile(path, "wb");
if (file == nullptr || !emulator->WriteSaveStateFile(file))
{
debug_log.Log("Could not create save state file");
window->ShowErrorMessageBox("Could not create save state file.");
success = false;
}
return success;
}
#endif
static void SetAudioPALMode(const bool enabled)
{
frame_rate_callback(enabled);
emulator->SetPALMode(enabled);
}
static std::filesystem::path ConfigPath()
{
const auto path_cstr = SDL::Pointer<char>(SDL_GetPrefPath("clownacy", "clownmdemu-frontend"));
if (path_cstr == nullptr)
return {};
const std::filesystem::path path(path_cstr.get());
return path;
}
static std::filesystem::path GetConfigurationFilePath()
{
return ConfigPath()/"configuration.ini";
}
static std::filesystem::path GetDearImGuiSettingsFilePath()
{
return ConfigPath()/"dear-imgui-settings.ini";
}
/////////////
// Tooltip //
/////////////
static void DoToolTip(const std::string &text)
{
if (ImGui::IsItemHovered())
{
ImGui::BeginTooltip();
ImGui::TextUnformatted(&text.front(), &text.back());
ImGui::EndTooltip();
}
}
/////////
// INI //
/////////
static char* INIReadCallback(char* const buffer, const int length, void* const user)
{
const SDL::RWops* const file = static_cast<const SDL::RWops*>(user);
int i = 0;
while (i < length - 1)
{
char character;
if (SDL_RWread(file->get(), &character, 1, 1) == 0)
{
if (i == 0)
return 0;
break;
}
buffer[i] = character;
++i;
if (character == '\n')
break;
}
buffer[i] = '\0';
return buffer;
}
static int INIParseCallback([[maybe_unused]] void* const user_cstr, const char* const section_cstr, const char* const name_cstr, const char* const value_cstr)
{
const std::string_view section(section_cstr);
const std::string_view name(name_cstr);
const std::string_view value(value_cstr);
if (section == "Miscellaneous")
{
const bool state = value == "on";
if (name == "vsync")
use_vsync = state;
else if (name == "integer-screen-scaling")
integer_screen_scaling = state;
else if (name == "tall-interlace-mode-2")
tall_double_resolution_mode = state;
else if (name == "low-pass-filter")
emulator->SetLowPassFilter(state);
else if (name == "pal")
SetAudioPALMode(state);
else if (name == "japanese")
emulator->SetDomestic(state);
#ifdef FILE_PICKER_POSIX
else if (name == "last-directory")
file_utilities.last_file_dialog_directory = value;
else if (name == "prefer-kdialog")
file_utilities.prefer_kdialog = state;
#endif
}
else if (section == "Keyboard Bindings")
{
unsigned int scancode_integer;
const auto scancode_integer_result = std::from_chars(&name.front(), &name.back() + 1, scancode_integer, 10);
if (scancode_integer_result.ec == std::errc{} && scancode_integer_result.ptr == &name.back() + 1 && scancode_integer < SDL_NUM_SCANCODES)
{
const SDL_Scancode scancode = static_cast<SDL_Scancode>(scancode_integer);
unsigned int binding_index;
const auto binding_index_result = std::from_chars(&value.front(), &value.back() + 1, binding_index, 10);
InputBinding input_binding = INPUT_BINDING_NONE;
if (binding_index_result.ec == std::errc() && binding_index_result.ptr == &value.back() + 1)
{
// Legacy numerical input bindings.
static const std::array input_bindings = {
INPUT_BINDING_NONE,
INPUT_BINDING_CONTROLLER_UP,
INPUT_BINDING_CONTROLLER_DOWN,
INPUT_BINDING_CONTROLLER_LEFT,
INPUT_BINDING_CONTROLLER_RIGHT,
INPUT_BINDING_CONTROLLER_A,
INPUT_BINDING_CONTROLLER_B,
INPUT_BINDING_CONTROLLER_C,
INPUT_BINDING_CONTROLLER_START,
INPUT_BINDING_PAUSE,
INPUT_BINDING_RESET,
INPUT_BINDING_FAST_FORWARD,
#ifdef CLOWNMDEMU_FRONTEND_REWINDING
INPUT_BINDING_REWIND,
#else
INPUT_BINDING_NONE,
#endif
INPUT_BINDING_QUICK_SAVE_STATE,
INPUT_BINDING_QUICK_LOAD_STATE,
INPUT_BINDING_TOGGLE_FULLSCREEN,
INPUT_BINDING_TOGGLE_CONTROL_PAD
};
if (binding_index < input_bindings.size())
input_binding = input_bindings[binding_index];
}
else
{
if (value == "INPUT_BINDING_CONTROLLER_UP")
input_binding = INPUT_BINDING_CONTROLLER_UP;
else if (value == "INPUT_BINDING_CONTROLLER_DOWN")
input_binding = INPUT_BINDING_CONTROLLER_DOWN;
else if (value == "INPUT_BINDING_CONTROLLER_LEFT")
input_binding = INPUT_BINDING_CONTROLLER_LEFT;
else if (value == "INPUT_BINDING_CONTROLLER_RIGHT")
input_binding = INPUT_BINDING_CONTROLLER_RIGHT;
else if (value == "INPUT_BINDING_CONTROLLER_A")
input_binding = INPUT_BINDING_CONTROLLER_A;
else if (value == "INPUT_BINDING_CONTROLLER_B")
input_binding = INPUT_BINDING_CONTROLLER_B;
else if (value == "INPUT_BINDING_CONTROLLER_C")
input_binding = INPUT_BINDING_CONTROLLER_C;
else if (value == "INPUT_BINDING_CONTROLLER_X")
input_binding = INPUT_BINDING_CONTROLLER_X;
else if (value == "INPUT_BINDING_CONTROLLER_Y")
input_binding = INPUT_BINDING_CONTROLLER_Y;
else if (value == "INPUT_BINDING_CONTROLLER_Z")
input_binding = INPUT_BINDING_CONTROLLER_Z;
else if (value == "INPUT_BINDING_CONTROLLER_START")
input_binding = INPUT_BINDING_CONTROLLER_START;
else if (value == "INPUT_BINDING_CONTROLLER_MODE")
input_binding = INPUT_BINDING_CONTROLLER_MODE;
else if (value == "INPUT_BINDING_PAUSE")
input_binding = INPUT_BINDING_PAUSE;
else if (value == "INPUT_BINDING_RESET")
input_binding = INPUT_BINDING_RESET;
else if (value == "INPUT_BINDING_FAST_FORWARD")
input_binding = INPUT_BINDING_FAST_FORWARD;
#ifdef CLOWNMDEMU_FRONTEND_REWINDING
else if (value == "INPUT_BINDING_REWIND")
input_binding = INPUT_BINDING_REWIND;
#endif
else if (value == "INPUT_BINDING_QUICK_SAVE_STATE")
input_binding = INPUT_BINDING_QUICK_SAVE_STATE;
else if (value == "INPUT_BINDING_QUICK_LOAD_STATE")
input_binding = INPUT_BINDING_QUICK_LOAD_STATE;
else if (value == "INPUT_BINDING_TOGGLE_FULLSCREEN")
input_binding = INPUT_BINDING_TOGGLE_FULLSCREEN;
else if (value == "INPUT_BINDING_TOGGLE_CONTROL_PAD")
input_binding = INPUT_BINDING_TOGGLE_CONTROL_PAD;
}
keyboard_bindings[scancode] = input_binding;
}
}
#ifdef FILE_PATH_SUPPORT
else if (section == "Recent Software")
{
static bool is_cd_file = false;
if (name == "cd")
{
is_cd_file = value == "true";
}
else if (name == "path")
{
if (!value.empty())
AddToRecentSoftware(value, is_cd_file, true);
}
}
#endif
return 1;
}
///////////////////
// Configuration //
///////////////////
static void LoadConfiguration()
{
// Set default settings.
// Default V-sync.
const int display_index = SDL_GetWindowDisplayIndex(window->GetSDLWindow());
if (display_index >= 0)
{
SDL_DisplayMode display_mode;
if (SDL_GetCurrentDisplayMode(display_index, &display_mode) == 0)
{
// Enable V-sync on displays with an FPS of a multiple of 60.
use_vsync = display_mode.refresh_rate % 60 == 0;
}
}
// Default other settings.
emulator->SetLowPassFilter(true);
integer_screen_scaling = false;
tall_double_resolution_mode = false;
emulator->SetDomestic(false);
SetAudioPALMode(false);
const SDL::RWops file = SDL::RWFromFile(GetConfigurationFilePath(), "r");
// Load the configuration file, overwriting the above settings.
if (file == nullptr || ini_parse_stream(INIReadCallback, const_cast<SDL::RWops*>(&file), INIParseCallback, nullptr) != 0)
{
// Failed to read configuration file: set defaults key bindings.
for (std::size_t i = 0; i < keyboard_bindings.size(); ++i)
keyboard_bindings[i] = INPUT_BINDING_NONE;
keyboard_bindings[SDL_SCANCODE_UP] = INPUT_BINDING_CONTROLLER_UP;
keyboard_bindings[SDL_SCANCODE_DOWN] = INPUT_BINDING_CONTROLLER_DOWN;
keyboard_bindings[SDL_SCANCODE_LEFT] = INPUT_BINDING_CONTROLLER_LEFT;
keyboard_bindings[SDL_SCANCODE_RIGHT] = INPUT_BINDING_CONTROLLER_RIGHT;
keyboard_bindings[SDL_SCANCODE_Z] = INPUT_BINDING_CONTROLLER_A;
keyboard_bindings[SDL_SCANCODE_X] = INPUT_BINDING_CONTROLLER_B;
keyboard_bindings[SDL_SCANCODE_C] = INPUT_BINDING_CONTROLLER_C;
keyboard_bindings[SDL_SCANCODE_A] = INPUT_BINDING_CONTROLLER_X;
keyboard_bindings[SDL_SCANCODE_S] = INPUT_BINDING_CONTROLLER_Y;
keyboard_bindings[SDL_SCANCODE_D] = INPUT_BINDING_CONTROLLER_Z;
keyboard_bindings[SDL_SCANCODE_RETURN] = INPUT_BINDING_CONTROLLER_START;
keyboard_bindings[SDL_SCANCODE_BACKSPACE] = INPUT_BINDING_CONTROLLER_MODE;
keyboard_bindings[SDL_GetScancodeFromKey(SDLK_PAUSE)] = INPUT_BINDING_PAUSE;
keyboard_bindings[SDL_GetScancodeFromKey(SDLK_F11)] = INPUT_BINDING_TOGGLE_FULLSCREEN;
keyboard_bindings[SDL_GetScancodeFromKey(SDLK_TAB)] = INPUT_BINDING_RESET;
keyboard_bindings[SDL_GetScancodeFromKey(SDLK_F1)] = INPUT_BINDING_TOGGLE_CONTROL_PAD;
keyboard_bindings[SDL_GetScancodeFromKey(SDLK_F5)] = INPUT_BINDING_QUICK_SAVE_STATE;
keyboard_bindings[SDL_GetScancodeFromKey(SDLK_F9)] = INPUT_BINDING_QUICK_LOAD_STATE;
keyboard_bindings[SDL_SCANCODE_SPACE] = INPUT_BINDING_FAST_FORWARD;
#ifdef CLOWNMDEMU_FRONTEND_REWINDING
keyboard_bindings[SDL_SCANCODE_R] = INPUT_BINDING_REWIND;
#endif
}
// Apply the V-sync setting, now that it's been decided.
SDL_RenderSetVSync(window->GetRenderer(), use_vsync);
}
static void SaveConfiguration()
{
// Save configuration file:
const SDL::RWops file = SDL::RWFromFile(GetConfigurationFilePath(), "w");
if (file == nullptr)
{
debug_log.Log("Could not open configuration file for writing.");
}
else
{
#define PRINT_STRING(FILE, STRING) SDL_RWwrite(FILE, STRING, sizeof(STRING) - 1, 1)
// Save keyboard bindings.
PRINT_STRING(file.get(), "[Miscellaneous]\n");
#define PRINT_BOOLEAN_OPTION(FILE, NAME, VARIABLE) \
PRINT_STRING(FILE, NAME " = "); \
if (VARIABLE) \
PRINT_STRING(FILE, "on\n"); \
else \
PRINT_STRING(FILE, "off\n");
PRINT_BOOLEAN_OPTION(file.get(), "vsync", use_vsync);
PRINT_BOOLEAN_OPTION(file.get(), "integer-screen-scaling", integer_screen_scaling);
PRINT_BOOLEAN_OPTION(file.get(), "tall-interlace-mode-2", tall_double_resolution_mode);
PRINT_BOOLEAN_OPTION(file.get(), "low-pass-filter", emulator->GetLowPassFilter());
PRINT_BOOLEAN_OPTION(file.get(), "pal", emulator->GetPALMode());
PRINT_BOOLEAN_OPTION(file.get(), "japanese", emulator->GetDomestic());
#ifdef FILE_PICKER_POSIX
if (!file_utilities.last_file_dialog_directory.empty())
{
PRINT_STRING(file.get(), "last-directory = ");
const std::string last_file_dialog_directory = file_utilities.last_file_dialog_directory.string();
SDL_RWwrite(file.get(), last_file_dialog_directory.data(), last_file_dialog_directory.size(), 1);
PRINT_STRING(file.get(), "\n");
}
PRINT_BOOLEAN_OPTION(file.get(), "prefer-kdialog", file_utilities.prefer_kdialog);
#endif
// Save keyboard bindings.
PRINT_STRING(file.get(), "\n[Keyboard Bindings]\n");
for (std::size_t i = 0; i < keyboard_bindings.size(); ++i)
{
if (keyboard_bindings[i] != INPUT_BINDING_NONE)
{
const char *binding_string;
// Default, to shut up potential warnings.
binding_string = "INPUT_BINDING_NONE";
switch (keyboard_bindings[i])
{
case INPUT_BINDING_NONE:
binding_string = "INPUT_BINDING_NONE";
break;
case INPUT_BINDING_CONTROLLER_UP:
binding_string = "INPUT_BINDING_CONTROLLER_UP";
break;
case INPUT_BINDING_CONTROLLER_DOWN:
binding_string = "INPUT_BINDING_CONTROLLER_DOWN";
break;
case INPUT_BINDING_CONTROLLER_LEFT:
binding_string = "INPUT_BINDING_CONTROLLER_LEFT";
break;
case INPUT_BINDING_CONTROLLER_RIGHT:
binding_string = "INPUT_BINDING_CONTROLLER_RIGHT";
break;
case INPUT_BINDING_CONTROLLER_A:
binding_string = "INPUT_BINDING_CONTROLLER_A";
break;
case INPUT_BINDING_CONTROLLER_B:
binding_string = "INPUT_BINDING_CONTROLLER_B";
break;
case INPUT_BINDING_CONTROLLER_C:
binding_string = "INPUT_BINDING_CONTROLLER_C";
break;
case INPUT_BINDING_CONTROLLER_X:
binding_string = "INPUT_BINDING_CONTROLLER_X";
break;
case INPUT_BINDING_CONTROLLER_Y:
binding_string = "INPUT_BINDING_CONTROLLER_Y";
break;
case INPUT_BINDING_CONTROLLER_Z:
binding_string = "INPUT_BINDING_CONTROLLER_Z";
break;
case INPUT_BINDING_CONTROLLER_START:
binding_string = "INPUT_BINDING_CONTROLLER_START";
break;
case INPUT_BINDING_CONTROLLER_MODE:
binding_string = "INPUT_BINDING_CONTROLLER_MODE";
break;
case INPUT_BINDING_PAUSE:
binding_string = "INPUT_BINDING_PAUSE";
break;
case INPUT_BINDING_RESET:
binding_string = "INPUT_BINDING_RESET";
break;
case INPUT_BINDING_FAST_FORWARD:
binding_string = "INPUT_BINDING_FAST_FORWARD";
break;
#ifdef CLOWNMDEMU_FRONTEND_REWINDING
case INPUT_BINDING_REWIND:
binding_string = "INPUT_BINDING_REWIND";
break;
#endif
case INPUT_BINDING_QUICK_SAVE_STATE:
binding_string = "INPUT_BINDING_QUICK_SAVE_STATE";
break;
case INPUT_BINDING_QUICK_LOAD_STATE:
binding_string = "INPUT_BINDING_QUICK_LOAD_STATE";
break;
case INPUT_BINDING_TOGGLE_FULLSCREEN:
binding_string = "INPUT_BINDING_TOGGLE_FULLSCREEN";
break;
case INPUT_BINDING_TOGGLE_CONTROL_PAD:
binding_string = "INPUT_BINDING_TOGGLE_CONTROL_PAD";
break;
case INPUT_BINDING__TOTAL:
SDL_assert(false);
break;
}
const std::string buffer = std::format("{} = {}\n", i, binding_string);
SDL_RWwrite(file.get(), buffer.data(), buffer.size(), 1);
}
}
#ifdef FILE_PATH_SUPPORT
// Save recent software paths.
PRINT_STRING(file.get(), "\n[Recent Software]\n");
for (const auto &recent_software : recent_software_list)
{
PRINT_STRING(file.get(), "cd = ");
if (recent_software.is_cd_file)
PRINT_STRING(file.get(), "true\n");
else
PRINT_STRING(file.get(), "false\n");
PRINT_STRING(file.get(), "path = ");
const auto path_string = recent_software.path.string();
SDL_RWwrite(file.get(), path_string.data(), 1, path_string.length());
PRINT_STRING(file.get(), "\n");
}
#endif
}
}
///////////////////
// Main function //
///////////////////
static void PreEventStuff()
{
emulator_on = emulator->IsCartridgeFileLoaded() || emulator->IsCDFileLoaded();
emulator_running = emulator_on && (!emulator_paused || emulator_frame_advance) && !file_utilities.IsDialogOpen()
#ifdef CLOWNMDEMU_FRONTEND_REWINDING
&& !emulator->RewindingExhausted()
#endif
;
emulator_frame_advance = false;
}
bool Frontend::Initialise(const int argc, char** const argv, const FrameRateCallback &frame_rate_callback_param)
{
frame_rate_callback = frame_rate_callback_param;
// Enable high-DPI support on Windows because SDL2 is bad at being a platform abstraction library
SDL_SetHint("SDL_WINDOWS_DPI_SCALING", "1");
#if !SDL_VERSION_ATLEAST(2,30,3)
// Force the window icon to the proper icon group, as the default icon
// size chosen by older versions of SDL does not suit high-DPI display.
// https://github.com/libsdl-org/SDL/pull/9429