forked from LIJI32/SameBoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.c
2785 lines (2504 loc) · 91.1 KB
/
gui.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
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 <OpenDialog/open_dialog.h>
#include <SDL.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include "utils.h"
#include "gui.h"
#include "font.h"
#include "audio/audio.h"
static const SDL_Color gui_palette[4] = {{8, 24, 16,}, {57, 97, 57,}, {132, 165, 99}, {198, 222, 140}};
static uint32_t gui_palette_native[4];
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
SDL_Texture *texture = NULL;
SDL_PixelFormat *pixel_format = NULL;
enum pending_command pending_command;
unsigned command_parameter;
char *dropped_state_file = NULL;
static char **custom_palettes;
static unsigned n_custom_palettes;
#ifdef __APPLE__
#define MODIFIER_NAME " " CMD_STRING
#else
#define MODIFIER_NAME CTRL_STRING
#endif
shader_t shader;
static SDL_Rect rect;
static unsigned factor;
static SDL_Surface *converted_background = NULL;
void render_texture(void *pixels, void *previous)
{
if (renderer) {
if (pixels) {
SDL_UpdateTexture(texture, NULL, pixels, GB_get_screen_width(&gb) * sizeof (uint32_t));
}
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
}
else {
static void *_pixels = NULL;
if (pixels) {
_pixels = pixels;
}
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
GB_frame_blending_mode_t mode = configuration.blending_mode;
if (!previous) {
mode = GB_FRAME_BLENDING_MODE_DISABLED;
}
else if (mode == GB_FRAME_BLENDING_MODE_ACCURATE) {
if (GB_is_sgb(&gb)) {
mode = GB_FRAME_BLENDING_MODE_SIMPLE;
}
else {
mode = GB_is_odd_frame(&gb)? GB_FRAME_BLENDING_MODE_ACCURATE_ODD : GB_FRAME_BLENDING_MODE_ACCURATE_EVEN;
}
}
render_bitmap_with_shader(&shader, _pixels, previous,
GB_get_screen_width(&gb), GB_get_screen_height(&gb),
rect.x, rect.y, rect.w, rect.h,
mode);
SDL_GL_SwapWindow(window);
}
}
static const char *help[] = {
"Keyboard Shortcuts:\n"
" Open Menu: Escape\n"
" Open ROM: " MODIFIER_NAME "+O\n"
" Reset: " MODIFIER_NAME "+R\n"
" Pause: " MODIFIER_NAME "+P\n"
" Save state: " MODIFIER_NAME "+(0-9)\n"
" Load state: " MODIFIER_NAME "+" SHIFT_STRING "+(0-9)\n"
" Toggle Fullscreen " MODIFIER_NAME "+F\n"
#ifdef __APPLE__
" Mute/Unmute: " MODIFIER_NAME "+" SHIFT_STRING "+M\n"
#else
" Mute/Unmute: " MODIFIER_NAME "+M\n"
#endif
" Toggle channel: " ALT_STRING "+(1-4)\n"
" Break Debugger: " CTRL_STRING "+C",
"\n"
"SameBoy\n"
"Version " GB_VERSION "\n\n"
"Copyright " COPYRIGHT_STRING " 2015-" GB_COPYRIGHT_YEAR "\n"
"Lior Halphon\n\n"
"Licensed under the MIT\n"
"license, see LICENSE for\n"
"more details."
};
void update_viewport(void)
{
int win_width, win_height;
SDL_GL_GetDrawableSize(window, &win_width, &win_height);
int logical_width, logical_height;
SDL_GetWindowSize(window, &logical_width, &logical_height);
factor = win_width / logical_width;
double x_factor = win_width / (double) GB_get_screen_width(&gb);
double y_factor = win_height / (double) GB_get_screen_height(&gb);
if (configuration.scaling_mode == GB_SDL_SCALING_INTEGER_FACTOR) {
x_factor = (unsigned)(x_factor);
y_factor = (unsigned)(y_factor);
}
if (configuration.scaling_mode != GB_SDL_SCALING_ENTIRE_WINDOW) {
if (x_factor > y_factor) {
x_factor = y_factor;
}
else {
y_factor = x_factor;
}
}
unsigned new_width = x_factor * GB_get_screen_width(&gb);
unsigned new_height = y_factor * GB_get_screen_height(&gb);
rect = (SDL_Rect){(win_width - new_width) / 2, (win_height - new_height) /2,
new_width, new_height};
if (renderer) {
SDL_RenderSetViewport(renderer, &rect);
}
else {
glViewport(rect.x, rect.y, rect.w, rect.h);
}
}
static void rescale_window(void)
{
SDL_SetWindowSize(window, GB_get_screen_width(&gb) * configuration.default_scale, GB_get_screen_height(&gb) * configuration.default_scale);
}
static void draw_char(uint32_t *buffer, unsigned width, unsigned height, unsigned char ch, uint32_t color, uint32_t *mask_top, uint32_t *mask_bottom)
{
if (ch < ' ' || ch > font_max) {
ch = '?';
}
// Huge dirty hack
if ((signed char)ch == CHECKBOX_ON_STRING[0] || (signed char)ch == CHECKBOX_OFF_STRING[0]) {
if (color == gui_palette_native[3]) {
color = gui_palette_native[0];
}
else if (color == gui_palette_native[0]) {
color = gui_palette_native[3];
ch = CHECKBOX_OFF_STRING[0];
}
}
uint8_t *data = &font[(ch - ' ') * GLYPH_WIDTH * GLYPH_HEIGHT];
for (unsigned y = GLYPH_HEIGHT; y--;) {
for (unsigned x = GLYPH_WIDTH; x--;) {
if (*(data++) && buffer >= mask_top && buffer < mask_bottom) {
(*buffer) = color;
}
buffer++;
}
buffer += width - GLYPH_WIDTH;
}
}
static signed scroll = 0;
static void draw_unbordered_text(uint32_t *buffer, unsigned width, unsigned height, unsigned x, signed y, const char *string, uint32_t color, bool is_osd)
{
if (!is_osd) {
y -= scroll;
}
unsigned orig_x = x;
unsigned y_offset = is_osd? 0 : (GB_get_screen_height(&gb) - 144) / 2;
while (*string) {
if (*string == '\n') {
x = orig_x;
y += GLYPH_HEIGHT + 4;
string++;
continue;
}
if (x > width - GLYPH_WIDTH) {
break;
}
draw_char(&buffer[(signed)(x + width * y)], width, height, *string, color, &buffer[width * y_offset], &buffer[width * (is_osd? GB_get_screen_height(&gb) : y_offset + 144)]);
x += GLYPH_WIDTH;
string++;
}
}
void draw_text(uint32_t *buffer, unsigned width, unsigned height, unsigned x, signed y, const char *string, uint32_t color, uint32_t border, bool is_osd)
{
draw_unbordered_text(buffer, width, height, x - 1, y, string, border, is_osd);
draw_unbordered_text(buffer, width, height, x + 1, y, string, border, is_osd);
draw_unbordered_text(buffer, width, height, x, y - 1, string, border, is_osd);
draw_unbordered_text(buffer, width, height, x, y + 1, string, border, is_osd);
draw_unbordered_text(buffer, width, height, x, y, string, color, is_osd);
}
const char *osd_text = NULL;
unsigned osd_countdown = 0;
unsigned osd_text_lines = 1;
void show_osd_text(const char *text)
{
osd_text_lines = 1;
osd_text = text;
osd_countdown = 30;
while (*text++) {
if (*text == '\n') {
osd_text_lines++;
osd_countdown += 30;
}
}
}
enum style {
STYLE_LEFT,
STYLE_INDENT,
STYLE_CENTER,
STYLE_SELECTION,
STYLE_ARROWS,
};
static void draw_styled_text(uint32_t *buffer, unsigned width, unsigned height, unsigned y, const char *string, uint32_t color, uint32_t border, enum style style)
{
unsigned x = GLYPH_WIDTH * 2;
if (style == STYLE_CENTER || style == STYLE_ARROWS) {
x = width / 2 - (unsigned) strlen(string) * GLYPH_WIDTH / 2;
}
else if (style == STYLE_LEFT) {
x = 6;
}
draw_text(buffer, width, height, x, y, string, color, border, false);
switch (style) {
case STYLE_SELECTION:
draw_text(buffer, width, height, x - GLYPH_WIDTH, y, SELECTION_STRING, color, border, false);
break;
case STYLE_ARROWS:
draw_text(buffer, width, height, x - GLYPH_WIDTH, y, LEFT_ARROW_STRING, color, border, false);
draw_text(buffer, width, height, width - x, y, RIGHT_ARROW_STRING, color, border, false);
break;
default:
break;
}
}
struct menu_item {
const char *string;
void (*handler)(unsigned);
const char *(*value_getter)(unsigned);
void (*backwards_handler)(unsigned);
};
static const struct menu_item *current_menu = NULL;
static const struct menu_item *root_menu = NULL;
static unsigned menu_height;
static unsigned scrollbar_size;
static bool mouse_scroling = false;
static unsigned current_selection = 0;
static enum {
SHOWING_DROP_MESSAGE,
SHOWING_MENU,
SHOWING_HELP,
WAITING_FOR_KEY,
WAITING_FOR_JBUTTON,
TEXT_INPUT,
} gui_state;
static char text_input_title[26];
static char text_input_title2[26];
static char text_input[26];
static void (*text_input_callback)(char ch) = NULL;
static unsigned joypad_configuration_progress = 0;
static uint8_t joypad_axis_temp;
static void item_exit(unsigned index)
{
pending_command = GB_SDL_QUIT_COMMAND;
}
static unsigned current_help_page = 0;
static void item_help(unsigned index)
{
current_help_page = 0;
gui_state = SHOWING_HELP;
}
static void about(unsigned index)
{
current_help_page = 1;
gui_state = SHOWING_HELP;
}
static void enter_emulation_menu(unsigned index);
static void enter_graphics_menu(unsigned index);
static void enter_keyboard_menu(unsigned index);
static void enter_joypad_menu(unsigned index);
static void enter_audio_menu(unsigned index);
static void enter_controls_menu(unsigned index);
static void enter_help_menu(unsigned index);
static void enter_options_menu(unsigned index);
static void toggle_audio_recording(unsigned index);
extern void set_filename(const char *new_filename, typeof(free) *new_free_function);
static void nop(unsigned index){}
static void open_rom(unsigned index)
{
char *filename = do_open_rom_dialog();
if (filename) {
set_filename(filename, free);
pending_command = GB_SDL_NEW_FILE_COMMAND;
}
}
static void cart_swap(unsigned index)
{
char *filename = do_open_rom_dialog();
if (filename) {
set_filename(filename, free);
pending_command = GB_SDL_CART_SWAP_COMMAND;
}
}
static void recalculate_menu_height(void)
{
menu_height = 24;
scrollbar_size = 0;
if (gui_state == SHOWING_MENU) {
for (const struct menu_item *item = current_menu; item->string; item++) {
menu_height += 12;
if (item->backwards_handler) {
menu_height += 12;
}
}
}
if (menu_height > 144) {
scrollbar_size = 144 * 140 / menu_height;
}
}
#if SDL_COMPILEDVERSION < 2014
int SDL_OpenURL(const char *url)
{
char *string = NULL;
#ifdef __APPLE__
asprintf(&string, "open '%s'", url);
#else
#ifdef _WIN32
asprintf(&string, "explorer '%s'", url);
#else
asprintf(&string, "xdg-open '%s'", url);
#endif
#endif
int ret = system(string);
free(string);
return ret;
}
#endif
static char audio_recording_menu_item[] = "Start Audio Recording";
static void sponsor(unsigned index)
{
SDL_OpenURL("https://github.com/sponsors/LIJI32");
}
static void debugger_help(unsigned index)
{
SDL_OpenURL("https://sameboy.github.io/debugger/");
}
static void return_to_root_menu(unsigned index)
{
current_menu = root_menu;
current_selection = 0;
scroll = 0;
recalculate_menu_height();
}
static const struct menu_item options_menu[] = {
{"Emulation Options", enter_emulation_menu},
{"Graphic Options", enter_graphics_menu},
{"Audio Options", enter_audio_menu},
{"Control Options", enter_controls_menu},
{"Back", return_to_root_menu},
{NULL,}
};
static void enter_options_menu(unsigned index)
{
current_menu = options_menu;
current_selection = 0;
scroll = 0;
recalculate_menu_height();
}
static const GB_cheat_t *current_cheat = NULL;
extern struct menu_item modify_cheat_menu[];
static void save_cheats(void)
{
extern char *filename;
size_t path_length = strlen(filename);
char cheat_path[path_length + 5];
replace_extension(filename, path_length, cheat_path, ".cht");
GB_save_cheats(&gb, cheat_path);
}
static void rename_callback(char ch)
{
if (ch == '\b' && text_input[0]) {
text_input[strlen(text_input) - 1] = 0;
return;
}
if (ch == '\n') {
GB_update_cheat(&gb,
current_cheat,
text_input,
current_cheat->address,
current_cheat->bank,
current_cheat->value,
current_cheat->old_value,
current_cheat->use_old_value,
current_cheat->enabled);
modify_cheat_menu[0].string = current_cheat->description;
gui_state = SHOWING_MENU;
save_cheats();
SDL_StopTextInput();
return;
}
if (ch < ' ') return;
size_t len = strlen(text_input);
if (len < 21) {
text_input[len] = ch;
text_input[len + 1] = 0;
}
}
static void rename_cheat(unsigned index)
{
strcpy(text_input_title, "Rename Cheat");
text_input_title2[0] = 0;
memcpy(text_input, current_cheat->description, 24);
text_input[24] = 0;
gui_state = TEXT_INPUT;
text_input_callback = rename_callback;
SDL_StartTextInput();
GB_update_cheat(&gb,
current_cheat,
current_cheat->description,
current_cheat->address,
current_cheat->bank,
current_cheat->value,
current_cheat->old_value,
current_cheat->use_old_value,
current_cheat->enabled);
save_cheats();
}
static void toggle_cheat(unsigned index)
{
GB_update_cheat(&gb,
current_cheat,
current_cheat->description,
current_cheat->address,
current_cheat->bank,
current_cheat->value,
current_cheat->old_value,
current_cheat->use_old_value,
!current_cheat->enabled);
save_cheats();
}
static const char *active_cheat_checkbox(unsigned index)
{
return current_cheat->enabled? CHECKBOX_ON_STRING : CHECKBOX_OFF_STRING;
}
static const char *get_cheat_address(unsigned index)
{
static char ret[12];
if (current_cheat->bank == GB_CHEAT_ANY_BANK) {
sprintf(ret, "$%04X", current_cheat->address);
}
else {
sprintf(ret, "$%02X:$%04X", current_cheat->bank, current_cheat->address);
}
return ret;
}
static void change_cheat_address_callback(char ch)
{
if (ch == '\b' && text_input[1]) {
size_t len = strlen(text_input);
text_input[len - 1] = 0;
if (text_input[len - 2] == ':') {
text_input[len - 2] = 0;
}
return;
}
if (ch == '\n') {
uint16_t bank = GB_CHEAT_ANY_BANK;
uint16_t address = 0;
const char *s = text_input + 1;
while (*s) {
if (*s == ':') {
bank = address;
address = 0;
}
else if (*s >= '0' && *s <= '9'){
address *= 0x10;
address += *s - '0';
}
else if (*s >= 'A' && *s <= 'F'){
address *= 0x10;
address += *s - 'A' + 10;
}
s++;
}
GB_update_cheat(&gb,
current_cheat,
current_cheat->description,
address,
bank,
current_cheat->value,
current_cheat->old_value,
current_cheat->use_old_value,
current_cheat->enabled);
save_cheats();
gui_state = SHOWING_MENU;
SDL_StopTextInput();
return;
}
size_t len = strlen(text_input);
if (len >= 12) return;
if (ch == ':' && (len >= 2) && !strchr(text_input, ':')) {
text_input[len] = ':';
text_input[len + 1] = '$';
text_input[len + 2] = 0;
return;
}
ch = toupper(ch);
if (!isxdigit(ch)) return;
unsigned digit_count = 0;
const char *s = text_input + 1;
while (*s) {
if (*s == ':') {
s += 2;
digit_count = 0;
}
else {
s++;
digit_count++;
}
}
if (digit_count == 4) return;
text_input[len] = ch;
text_input[len + 1] = 0;
}
static void change_cheat_address(unsigned index)
{
strcpy(text_input_title, "Enter Cheat Address");
text_input_title2[0] = 0;
strcpy(text_input, get_cheat_address(0));
gui_state = TEXT_INPUT;
text_input_callback = change_cheat_address_callback;
SDL_StartTextInput();
}
static const char *get_cheat_value(unsigned index)
{
static char ret[4];
sprintf(ret, "$%02X", current_cheat->value);
return ret;
}
static void change_cheat_value_callback(char ch)
{
if (ch == '\b' && text_input[1]) {
size_t len = strlen(text_input);
text_input[len - 1] = 0;
return;
}
if (ch == '\n') {
uint8_t value = 0;
const char *s = text_input + 1;
while (*s) {
if (*s >= '0' && *s <= '9'){
value *= 0x10;
value += *s - '0';
}
else if (*s >= 'A' && *s <= 'F'){
value *= 0x10;
value += *s - 'A' + 10;
}
s++;
}
GB_update_cheat(&gb,
current_cheat,
current_cheat->description,
current_cheat->address,
current_cheat->bank,
value,
current_cheat->old_value,
current_cheat->use_old_value,
current_cheat->enabled);
save_cheats();
gui_state = SHOWING_MENU;
SDL_StopTextInput();
return;
}
if (!isxdigit(ch)) return;
ch = toupper(ch);
size_t len = strlen(text_input);
if (len == 3) {
text_input[1] = text_input[2];
text_input[2] = ch;
return;
}
text_input[len] = ch;
text_input[len + 1] = 0;
}
static void change_cheat_value(unsigned index)
{
strcpy(text_input_title, "Enter Cheat Value");
text_input_title2[0] = 0;
strcpy(text_input, get_cheat_value(0));
gui_state = TEXT_INPUT;
text_input_callback = change_cheat_value_callback;
SDL_StartTextInput();
}
static const char *get_cheat_old_value(unsigned index)
{
if (!current_cheat->use_old_value) {
return "Any";
}
static char ret[4];
sprintf(ret, "$%02X", current_cheat->old_value);
return ret;
}
static void change_cheat_old_value_callback(char ch)
{
if (ch == '\b' && strcmp(text_input, "Any") != 0) {
size_t len = strlen(text_input);
if (len == 2) {
strcpy(text_input, "Any");
return;
}
text_input[len - 1] = 0;
return;
}
if (ch == '\n') {
uint8_t value = 0;
bool use_old_value = strcmp(text_input, "Any") != 0;
if (use_old_value) {
const char *s = text_input + 1;
while (*s) {
if (*s >= '0' && *s <= '9'){
value *= 0x10;
value += *s - '0';
}
else if (*s >= 'A' && *s <= 'F'){
value *= 0x10;
value += *s - 'A' + 10;
}
s++;
}
}
GB_update_cheat(&gb,
current_cheat,
current_cheat->description,
current_cheat->address,
current_cheat->bank,
current_cheat->value,
value,
use_old_value,
current_cheat->enabled);
save_cheats();
gui_state = SHOWING_MENU;
SDL_StopTextInput();
return;
}
if (!isxdigit(ch)) return;
ch = toupper(ch);
if (strcmp(text_input, "Any") == 0) {
strcpy(text_input, "$");
}
size_t len = strlen(text_input);
if (len == 3) {
text_input[1] = text_input[2];
text_input[2] = ch;
return;
}
text_input[len] = ch;
text_input[len + 1] = 0;
}
static void change_cheat_old_value(unsigned index)
{
strcpy(text_input_title, "Enter Cheat Old Value");
text_input_title2[0] = 0;
strcpy(text_input, get_cheat_old_value(0));
gui_state = TEXT_INPUT;
text_input_callback = change_cheat_old_value_callback;
SDL_StartTextInput();
}
static void enter_cheats_menu(unsigned index);
static void delete_cheat(unsigned index)
{
GB_remove_cheat(&gb, current_cheat);
save_cheats();
enter_cheats_menu(0);
}
struct menu_item modify_cheat_menu[] = {
{"", rename_cheat},
{"Enable", toggle_cheat, active_cheat_checkbox},
{"Address:", change_cheat_address, get_cheat_address},
{"Value:", change_cheat_value, get_cheat_value},
{"Old Value:", change_cheat_old_value, get_cheat_old_value},
{"Delete Cheat", delete_cheat},
{"Back", enter_cheats_menu},
{NULL,}
};
static void toggle_cheats(unsigned index)
{
GB_set_cheats_enabled(&gb, !GB_cheats_enabled(&gb));
}
static void add_cheat(unsigned index)
{
current_cheat = GB_add_cheat(&gb, "New Cheat", 0, 0, 0, 0, false, true);
modify_cheat_menu[0].string = current_cheat->description;
current_menu = modify_cheat_menu;
current_selection = 0;
scroll = 0;
save_cheats();
}
static void import_cheat_callback(char ch)
{
if (ch == '\b' && text_input[0]) {
size_t len = strlen(text_input);
text_input[len - 1] = 0;
return;
}
if (ch == '\n') {
if (!text_input[0]) {
gui_state = SHOWING_MENU;
SDL_StopTextInput();
return;
}
current_cheat = GB_import_cheat(&gb, text_input, "Imported Cheat", true);
if (current_cheat) {
gui_state = SHOWING_MENU;
modify_cheat_menu[0].string = current_cheat->description;
current_menu = modify_cheat_menu;
current_selection = 0;
scroll = 0;
save_cheats();
SDL_StopTextInput();
return;
}
strcpy(text_input_title, "Invalid Code.");
strcpy(text_input_title2, "Press Enter to Cancel");
text_input[0] = 0;
return;
}
if (ch != '-' && !isxdigit(ch)) return;
ch = toupper(ch);
size_t len = strlen(text_input);
if (len >= 12) {
return;
}
text_input[len] = ch;
text_input[len + 1] = 0;
if (text_input_title[0] != 'E') {
strcpy(text_input_title, "Enter a GameShark");
strcpy(text_input_title2, "or GameGenie Code");
}
}
static void import_cheat(unsigned index)
{
strcpy(text_input_title, "Enter a GameShark");
strcpy(text_input_title2, "or GameGenie Code");
text_input[0] = 0;
gui_state = TEXT_INPUT;
text_input_callback = import_cheat_callback;
save_cheats();
SDL_StartTextInput();
}
static void modify_cheat(unsigned index)
{
const GB_cheat_t *const *cheats = GB_get_cheats(&gb, NULL);
current_cheat = cheats[index - 3];
modify_cheat_menu[0].string = current_cheat->description;
current_menu = modify_cheat_menu;
current_selection = 0;
scroll = 0;
}
static const char *checkbox_for_cheat(unsigned index)
{
const GB_cheat_t *const *cheats = GB_get_cheats(&gb, NULL);
return cheats[index - 3]->enabled? CHECKBOX_ON_STRING : CHECKBOX_OFF_STRING;
}
static const char *cheats_global_checkbox(unsigned index)
{
return GB_cheats_enabled(&gb)? CHECKBOX_ON_STRING : CHECKBOX_OFF_STRING;
}
static void enter_cheats_menu(unsigned index)
{
struct menu_item *cheats_menu = NULL;
if (cheats_menu) {
free(cheats_menu);
}
size_t cheat_count;
const GB_cheat_t *const *cheats = GB_get_cheats(&gb, &cheat_count);
cheats_menu = calloc(cheat_count + 5, sizeof(struct menu_item));
cheats_menu[0] = (struct menu_item){"New Cheat", add_cheat};
cheats_menu[1] = (struct menu_item){"Import Cheat", import_cheat};
cheats_menu[2] = (struct menu_item){"Enable Cheats", toggle_cheats, cheats_global_checkbox};
for (size_t i = 0; i < cheat_count; i++) {
cheats_menu[i + 3] = (struct menu_item){cheats[i]->description, modify_cheat, checkbox_for_cheat};
}
cheats_menu[cheat_count + 3] = (struct menu_item){"Back", return_to_root_menu};
current_menu = cheats_menu;
current_selection = 0;
scroll = 0;
recalculate_menu_height();
}
static const struct menu_item paused_menu[] = {
{"Resume", NULL},
{"Open ROM", open_rom},
{"Hot Swap Cartridge", cart_swap},
{"Options", enter_options_menu},
{"Cheats", enter_cheats_menu},
{audio_recording_menu_item, toggle_audio_recording},
{"Help & About", enter_help_menu},
{"Sponsor SameBoy", sponsor},
{"Quit SameBoy", item_exit},
{NULL,}
};
static struct menu_item nonpaused_menu[sizeof(paused_menu) / sizeof(paused_menu[0]) - 3];
static void __attribute__((constructor)) build_nonpaused_menu(void)
{
const struct menu_item *in = paused_menu;
struct menu_item *out = nonpaused_menu;
while (in->string) {
if (in->handler == NULL || in->handler == cart_swap || in->handler == enter_cheats_menu) {
in++;
continue;
}
*out = *in;
out++;
in++;
}
}
static const struct menu_item help_menu[] = {
{"Shortcuts", item_help},
{"Debugger Help", debugger_help},
{"About SameBoy", about},
{"Back", return_to_root_menu},
{NULL,}
};
static void enter_help_menu(unsigned index)
{
current_menu = help_menu;
current_selection = 0;
scroll = 0;
recalculate_menu_height();
}
static void cycle_model(unsigned index)
{
configuration.model++;
if (configuration.model == MODEL_MAX) {
configuration.model = 0;
}
pending_command = GB_SDL_RESET_COMMAND;
}
static void cycle_model_backwards(unsigned index)
{
if (configuration.model == 0) {
configuration.model = MODEL_MAX;
}
configuration.model--;
pending_command = GB_SDL_RESET_COMMAND;
}
static const char *current_model_string(unsigned index)
{
return GB_inline_const(const char *[], {"Game Boy", "Game Boy Color", "Game Boy Advance", "Super Game Boy", "Game Boy Pocket"})
[configuration.model];
}
static void cycle_cgb_revision(unsigned index)
{
if (configuration.cgb_revision == GB_MODEL_CGB_E - GB_MODEL_CGB_0) {
configuration.cgb_revision = 0;
}
else {
configuration.cgb_revision++;
}
pending_command = GB_SDL_RESET_COMMAND;
}
static void cycle_cgb_revision_backwards(unsigned index)
{
if (configuration.cgb_revision == 0) {
configuration.cgb_revision = GB_MODEL_CGB_E - GB_MODEL_CGB_0;
}
else {
configuration.cgb_revision--;
}
pending_command = GB_SDL_RESET_COMMAND;
}
static const char *current_cgb_revision_string(unsigned index)
{
return GB_inline_const(const char *[], {
"CPU CGB 0 (Exp.)",
"CPU CGB A (Exp.)",
"CPU CGB B (Exp.)",
"CPU CGB C (Exp.)",
"CPU CGB D",
"CPU CGB E",
})
[configuration.cgb_revision];
}
static void cycle_sgb_revision(unsigned index)
{
configuration.sgb_revision++;
if (configuration.sgb_revision == SGB_MAX) {
configuration.sgb_revision = 0;