-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmenu.cpp
More file actions
5166 lines (4948 loc) · 195 KB
/
Copy pathmenu.cpp
File metadata and controls
5166 lines (4948 loc) · 195 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
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 <stdio.h>
#include <string.h>
#include <memory>
#include "pico.h"
#include "pico/stdlib.h"
#include "pico/rand.h"
#include "hardware/flash.h"
#include "hardware/watchdog.h"
#include "hardware/divider.h"
#include "pico/bootrom.h"
#include "tusb.h"
#include "FrensHelpers.h"
#include "FrensFonts.h"
#include "gamepad.h"
#include "RomLister.h"
#include "recentgames.h"
#include "menu.h"
#include "nespad.h"
#include "wiipad.h"
#include "menu_settings.h"
#include "usb_msc.h"
#include "font_8x8.h"
#include "settings.h"
#include "ffwrappers.h"
#include "vumeter.h"
#include "DefaultSS.h"
#include <stdint.h>
#include "wavplayer.h"
const int8_t *g_settings_visibility;
const uint8_t *g_available_screen_modes;
// FDS disk-swap hooks. Null in builds (or ROM contexts) where FDS is
// not active; the menu option is also kept hidden in those cases via
// g_settings_visibility[MOPT_FDS_DISK_SWAP].
static const MenuFdsHooks *s_fdsHooks = nullptr;
void menuSetFdsHooks(const MenuFdsHooks *hooks) { s_fdsHooks = hooks; }
// LEFT/RIGHT preview the disk choice without committing — A on the
// option commits (or triggers Reset). -1 means "not initialised yet,
// use the live current side". Set to "current side" each time the
// menu opens.
static int s_fdsPendingChoice = -1;
#if !HSTX
#define CC(x) (((x >> 1) & 15) | (((x >> 6) & 15) << 4) | (((x >> 11) & 15) << 8))
const __UINT16_TYPE__ NesMenuPalette[64] = {
CC(0x39ce), CC(0x1071), CC(0x0015), CC(0x2013), CC(0x440e), CC(0x5402), CC(0x5000), CC(0x3c20),
CC(0x20a0), CC(0x0100), CC(0x0140), CC(0x00e2), CC(0x0ceb), CC(0x0000), CC(0x0000), CC(0x0000),
CC(0x5ef7), CC(0x01dd), CC(0x10fd), CC(0x401e), CC(0x5c17), CC(0x700b), CC(0x6ca0), CC(0x6521),
CC(0x45c0), CC(0x0240), CC(0x02a0), CC(0x0247), CC(0x0211), CC(0x0000), CC(0x0000), CC(0x0000),
CC(0x7fff), CC(0x1eff), CC(0x2e5f), CC(0x223f), CC(0x79ff), CC(0x7dd6), CC(0x7dcc), CC(0x7e67),
CC(0x7ae7), CC(0x4342), CC(0x2769), CC(0x2ff3), CC(0x03bb), CC(0x0000), CC(0x0000), CC(0x0000),
CC(0x7fff), CC(0x579f), CC(0x635f), CC(0x6b3f), CC(0x7f1f), CC(0x7f1b), CC(0x7ef6), CC(0x7f75),
CC(0x7f94), CC(0x73f4), CC(0x57d7), CC(0x5bf9), CC(0x4ffe), CC(0x0000), CC(0x0000), CC(0x0000)};
#else // TODO
#define CC(c) (((c & 0xf8) >> 3) | ((c & 0xf800) >> 6) | ((c & 0xf80000) >> 9))
const __UINT16_TYPE__ NesMenuPalette[64] = {
CC(0x626262), CC(0x001C95), CC(0x1904AC), CC(0x42009D),
CC(0x61006B), CC(0x6E0025), CC(0x650500), CC(0x491E00),
CC(0x223700), CC(0x004900), CC(0x004F00), CC(0x004816),
CC(0x00355E), CC(0x000000), CC(0x000000), CC(0x000000),
CC(0xABABAB), CC(0x0C4EDB), CC(0x3D2EFF), CC(0x7115F3),
CC(0x9B0BB9), CC(0xB01262), CC(0xA92704), CC(0x894600),
CC(0x576600), CC(0x237F00), CC(0x008900), CC(0x008332),
CC(0x006D90), CC(0x000000), CC(0x000000), CC(0x000000),
CC(0xFFFFFF), CC(0x57A5FF), CC(0x8287FF), CC(0xB46DFF),
CC(0xDF60FF), CC(0xF863C6), CC(0xF8746D), CC(0xDE9020),
CC(0xB3AE00), CC(0x81C800), CC(0x56D522), CC(0x3DD36F),
CC(0x3EC1C8), CC(0x4E4E4E), CC(0x000000), CC(0x000000),
CC(0xFFFFFF), CC(0xBEE0FF), CC(0xCDD4FF), CC(0xE0CAFF),
CC(0xF1C4FF), CC(0xFCC4EF), CC(0xFDCACE), CC(0xF5D4AF),
CC(0xE6DF9C), CC(0xD3E99A), CC(0xC2EFA8), CC(0xB7EFC4),
CC(0xB6EAE5), CC(0xB8B8B8), CC(0x000000), CC(0x000000)};
#endif
// Define the artwork directory and file formats
#if !HSTX
#define ARTWORKFILE "/metadata/%s/images/%d/%c/%s.444"
#else
#define ARTWORKFILE "/metadata/%s/images/%d/%c/%s.555"
#endif
#define METADDATAFILE "/metadata/%s/descr/%c/%s.txt"
int NesMenuPaletteItems = sizeof(NesMenuPalette) / sizeof(NesMenuPalette[0]);
const static char *connectedGamePadName[2];
const static char *connectedGamePadShortName[2];
#define SCREENBUFCELLS SCREEN_ROWS *SCREEN_COLS
charCell *screenBuffer;
static char *selectedRomOrFolder;
static bool errorInSavingRom = false;
static char *globalErrorMessage;
// Path picked in the recently played list, owned by menu() (heap, not stack -
// this is FF_MAX_LFN + 1 bytes). Shared with showSettingsMenu, which can open
// the same list. nullptr disables the feature rather than crashing.
static char *recentLaunchPath = nullptr;
// Set by startRom when the rom is already in flash and verified, so menu() can
// return to the emulator instead of rebooting. Only ever true when
// START_FLASHED_ROM_WITHOUT_REBOOT is enabled.
static bool skipRebootAfterMenu = false;
// static bool artworkEnabled = false;
static uint8_t crcOffset = 0; // Default offset for CRC calculation
#define LONG_PRESS_TRESHOLD (500)
#define REPEAT_DELAY (40)
static char buttonLabel1[2]; // e.g., "A", "B", "X", "O"
static char buttonLabel2[2]; // e.g., "A", "B", "
static char line[41];
static char valueBuf[16]; // separate buffer for numeric values
static bool exitMenu = false;
static bool settingsActive = false;
static WORD *WorkLineRom = nullptr;
#if PICO_RP2350
// Track current WAV playback path and state while in the menu
static char lastWavPath[FF_MAX_LFN] = {0};
#endif
#if !HSTX
// static BYTE *WorkLineRom8 = nullptr;
void RomSelect_SetLineBuffer(WORD *p, WORD size)
{
WorkLineRom = p;
}
#endif
static constexpr int LEFT = 1 << 6;
static constexpr int RIGHT = 1 << 7;
static constexpr int UP = 1 << 4;
static constexpr int DOWN = 1 << 5;
static constexpr int SELECT = 1 << 2;
static constexpr int START = 1 << 3;
static constexpr int A = 1 << 0;
static constexpr int B = 1 << 1;
static constexpr int X = 1 << 8;
static constexpr int Y = 1 << 9;
// Menu bits for one GPIO port. A NES pad shifts out its buttons in menu order
// already (bit0=A, bit1=B, bit2=Select, ...), so that word is used as-is, the
// way this port has always worked. A SNES pad puts B and Y where a NES pad has
// A and B, and its A and X two bytes further up, so its four face buttons are
// named rather than taken positionally: A chooses and B goes back, matching USB
// and Wii Classic pads instead of moving "choose" onto B.
//
// Only a SNES pad ever drives bits 8-11, so a pad that has not proven itself one
// keeps the NES order - correct for a NES pad (which announces itself every
// frame through its ID nibble) and for a SNES->NES adapter cable, which reports
// NES buttons in NES order. A real SNES pad settles it on the first A press.
static inline int nespadMenuBits(uint16_t ext, uint8_t type)
{
if (type != NESPAD_TYPE_SNES)
{
return (int)(ext & 0xFF);
}
int v = ext & (SELECT | START | UP | DOWN | LEFT | RIGHT); // same bits on both pads
if (ext & (1u << 8)) v |= A;
if (ext & (1u << 0)) v |= B;
if (ext & (1u << 9)) v |= X; // "button 3": opens the recently played list
if (ext & (1u << 1)) v |= Y;
return v;
}
void resetColors(int prevfgColor, int prevbgColor)
{
for (auto i = 0; i < SCREENBUFCELLS; i++)
{
if (screenBuffer[i].fgcolor == prevfgColor)
{
screenBuffer[i].fgcolor = settings.fgcolor;
}
if (screenBuffer[i].bgcolor == prevbgColor)
{
screenBuffer[i].bgcolor = settings.bgcolor;
}
}
}
void getButtonLabels(char *buttonLabel1, char *buttonLabel2)
{
auto &gp = io::getCurrentGamePadState(0);
if (strcmp(gp.GamePadName, "Dual Shock 4") == 0 || strcmp(gp.GamePadName, "Dual Sense") == 0 || strcmp(gp.GamePadName, "PSClassic") == 0)
{
strcpy(buttonLabel1, "O");
strcpy(buttonLabel2, "X");
}
else if (strcmp(gp.GamePadName, "XInput") == 0 || strncmp(gp.GamePadName, "Genesis", 7) == 0 || strcmp(gp.GamePadName, "MDArcade") == 0)
{
strcpy(buttonLabel1, "B");
strcpy(buttonLabel2, "A");
}
else if (strcmp(gp.GamePadName, "Keyboard") == 0)
{
strcpy(buttonLabel1, "X");
strcpy(buttonLabel2, "Z");
}
else
{
strcpy(buttonLabel1, "A");
strcpy(buttonLabel2, "B");
}
}
static bool isArtWorkEnabled()
{
char PATH[FF_MAX_LFN];
FILINFO fi;
static bool artworkEnabled = false;
static FrensSettings::emulators lastEmulatorType = FrensSettings::emulators::MULTI;
FrensSettings::emulators currentEmulatorType = FrensSettings::getEmulatorType();
if (lastEmulatorType == currentEmulatorType)
{
return artworkEnabled;
}
PATH[0] = 0;
const char *emulator = FrensSettings::getEmulatorTypeString();
switch (currentEmulatorType)
{
case FrensSettings::emulators::NES:
snprintf(PATH, sizeof(PATH), "/Metadata/%s/Images/160/D/D0E96F6B.444", emulator);
break;
case FrensSettings::emulators::SMS:
snprintf(PATH, sizeof(PATH), "/Metadata/%s/Images/160/6/6A5A1E39.444", emulator);
break;
case FrensSettings::emulators::GENESIS:
snprintf(PATH, sizeof(PATH), "/Metadata/%s/Images/160/5/56976261.444", emulator);
break;
case FrensSettings::emulators::GAMEBOY:
snprintf(PATH, sizeof(PATH), "/Metadata/%s/Images/160/0/00A9001E.444", emulator);
break;
case FrensSettings::emulators::PCE:
snprintf(PATH, sizeof(PATH), "/Metadata/%s/Images/160/5/599EAD9B.444", emulator);
break;
case FrensSettings::emulators::O2EM:
snprintf(PATH, sizeof(PATH), "/Metadata/%s/Images/160/0/084EE035.444", emulator);
break;
case FrensSettings::emulators::SNES:
snprintf(PATH, sizeof(PATH), "/Metadata/%s/Images/160/0/00FAD8FD.444", emulator);
break;
case FrensSettings::emulators::TI99:
// The other emulators probe for one known title's artwork file. There is no
// published TI-99/4A metadata pack to pick a CRC from yet, so probe the image
// directory itself - present only when a pack has been installed.
snprintf(PATH, sizeof(PATH), "/Metadata/%s/Images/160", emulator);
break;
default:
return false;
}
lastEmulatorType = FrensSettings::getEmulatorType();
if (PATH[0])
{
printf("Checking for artwork at: %s\n", PATH);
FRESULT res = f_stat(PATH, &fi);
artworkEnabled = (res == FR_OK);
// printf("Artwork %s for %s\n", exists ? "enabled" : "not found", emulator);
}
return artworkEnabled;
}
int Menu_LoadFrame()
{
//Frens::waitForVSync();
Frens::PaceFrames60fps(false, true);
#if NES_PIN_CLK != -1
nespad_read_start();
#endif
auto count =
#if !HSTX
dvi_->getFrameCounter();
#else
hstx_getframecounter();
#endif
Frens::pollHeadPhoneJack();
auto onOff = hw_divider_s32_quotient_inlined(count, 60) & 1;
Frens::blinkLed(onOff);
#if NES_PIN_CLK != -1
nespad_read_finish(); // Sets global nespad_state var
#endif
tuh_task();
#if !HSTX && 0
if (Frens::isFrameBufferUsed())
{
Frens::markFrameReadyForReendering(true);
}
#endif
// https://github.com/fhoedemakers/pico-genesisPlus/issues/10
// Initialize the Wii Pad here if delayed start is enabled, after the DAC has been initialized.
#if WIIPAD_DELAYED_START and WII_PIN_SDA >= 0 and WII_PIN_SCL >= 0
// check only every 60 frames.
if (!wiipad_is_connected() && onOff)
{
wiipad_begin();
}
#endif
// play audio stream if active and not paused
wavplayer::pump(wavplayer::sample_rate() / 60);
return count;
}
bool resetScreenSaver = false;
void RomSelect_PadState(DWORD *pdwPad1, bool ignorepushed = false)
{
static uint32_t longpressTreshold = 0;
static uint32_t previousTime = Frens::time_ms();
uint32_t currentTime = Frens::time_ms();
uint32_t delta;
int prevBgColor = settings.bgcolor;
int prevFgColor = settings.fgcolor;
static DWORD prevButtons{};
auto &gp = io::getCurrentGamePadState(0);
auto &gp2 = io::getCurrentGamePadState(1);
uint32_t combinedButtons = gp.buttons | gp2.buttons;
connectedGamePadName[0] = gp.GamePadName;
connectedGamePadName[1] = gp2.GamePadName;
connectedGamePadShortName[0] = gp.GamePadShortName;
connectedGamePadShortName[1] = gp2.GamePadShortName;
int v = (combinedButtons & io::GamePadState::Button::LEFT ? LEFT : 0) |
(combinedButtons & io::GamePadState::Button::RIGHT ? RIGHT : 0) |
(combinedButtons & io::GamePadState::Button::UP ? UP : 0) |
(combinedButtons & io::GamePadState::Button::DOWN ? DOWN : 0) |
(combinedButtons & io::GamePadState::Button::A ? A : 0) |
(combinedButtons & io::GamePadState::Button::B ? B : 0) |
(combinedButtons & io::GamePadState::Button::SELECT ? SELECT : 0) |
(combinedButtons & io::GamePadState::Button::START ? START : 0) |
// Genesis pads report their C button as Button::C, not Button::X.
// Both are "button 3" as far as the menu is concerned (X on SNES,
// Y on XInput, Triangle on PlayStation, C on Genesis), so either
// one opens the recently played list. On the original 3-button
// Genesis Mini pad C doubles as SELECT (hid_app.cpp), and the
// browser tests SELECT first, so there it still opens the settings
// menu - unchanged, and never both at once.
(combinedButtons & (io::GamePadState::Button::X | io::GamePadState::Button::C) ? X : 0) |
(combinedButtons & io::GamePadState::Button::Y ? Y : 0) |
0;
#if NES_PIN_CLK != -1
v |= nespadMenuBits(nespad_states_ext[0], nespad_padtype[0]);
#endif
#if NES_PIN_CLK_1 != -1
v |= nespadMenuBits(nespad_states_ext[1], nespad_padtype[1]);
#endif
#if WII_PIN_SDA >= 0 and WII_PIN_SCL >= 0
v |= wiipad_read();
#endif
delta = currentTime - previousTime;
previousTime = currentTime;
if (v & (UP | DOWN | LEFT | RIGHT))
{
longpressTreshold += delta;
}
else
{
longpressTreshold = 0;
}
*pdwPad1 = 0;
unsigned long pushed;
auto p1 = v;
if (ignorepushed == false)
{
pushed = v & ~prevButtons;
}
else
{
pushed = v;
}
// SELECT no longer changes colors directly; it opens the options menu in the main loop.
if ( p1 & SELECT )
{
#if HSTX
//printf("SELECT pressed, opening options menu\n");
if (pushed & A){
v = p1 =pushed = 0; // Clear all inputs to prevent accidental menu navigation after resetting to DVI mode
if (!settings.flags.useDVIModeForHDMI) {
printf("SELECT + A detected, defaulting to DVI\n");
settings.flags.useDVIModeForHDMI = 1; // Force DVI
FrensSettings::savesettings();
exitMenu = true; // Signal to exit menu after saving settings
}
}
#endif
}
if (pushed || longpressTreshold > LONG_PRESS_TRESHOLD)
{
if (!pushed)
{
if (longpressTreshold > LONG_PRESS_TRESHOLD)
{
longpressTreshold = LONG_PRESS_TRESHOLD - REPEAT_DELAY;
}
}
*pdwPad1 = v;
if (v != 0)
{
resetScreenSaver = true;
}
}
prevButtons = p1;
}
void RomSelect_DrawLine(int line, int selectedRow, int pixelsToSkip = 0)
{
WORD fgcolor, bgcolor;
auto pixelRow = WorkLineRom + pixelsToSkip;
// calculate first char column index from pixelstoskip
auto firstCharColumnIndex = (pixelsToSkip % SCREENWIDTH) / FONT_CHAR_WIDTH;
for (auto i = 0; i < SCREEN_COLS; ++i)
{
if (i < firstCharColumnIndex)
{
continue; // skip out of bounds
}
int charIndex = i + line / FONT_CHAR_HEIGHT * SCREEN_COLS;
int row = charIndex / SCREEN_COLS;
uint c = screenBuffer[charIndex].charvalue;
if (row == selectedRow)
{
fgcolor = settingsActive ? NesMenuPalette[CWHITE] : NesMenuPalette[settings.bgcolor];
bgcolor = settingsActive ? NesMenuPalette[CBLACK] : NesMenuPalette[settings.fgcolor];
}
else
{
fgcolor = NesMenuPalette[screenBuffer[charIndex].fgcolor];
bgcolor = NesMenuPalette[screenBuffer[charIndex].bgcolor];
}
int rowInChar = line % FONT_CHAR_HEIGHT;
char fontSlice = getcharslicefrom8x8font(c, rowInChar); // font_8x8[(c - FONT_FIRST_ASCII) + (rowInChar)*FONT_N_CHARS];
for (auto bit = 0; bit < 8; bit++)
{
if (fontSlice & 1)
{
*pixelRow = fgcolor;
}
else
{
*pixelRow = bgcolor;
}
fontSlice >>= 1;
pixelRow++;
}
}
return;
}
/// @brief Renders a single 320-pixel scanline into the active video line buffer.
/// Optionally blends (actually overwrites) an image row before drawing text.
/// Text glyphs are only drawn when not in screensaver (image moving) mode.
/// @param scanline Absolute scanline index (0..SCREENHEIGHT-1).
/// @param selectedRow Menu row index that is currently selected (for inverted colors); pass -1 for no selection.
/// @param w Image width in pixels (0 disables image drawing). Must be 1..SCREENWIDTH if imagebuffer != nullptr.
/// @param h Image height in pixels. Must be 1..SCREENHEIGHT if imagebuffer != nullptr.
/// @param imagebuffer Pointer to packed 16-bit pixel data (layout: row-major, RGB444/555 depending on build).
/// @param imagex Horizontal start position (column) where the image is placed (0-based).
/// @param imagey Vertical start position (scanline) where the top of the image is placed.
/// When imagex or imagey are non‑zero the function treats this as screensaver mode and
/// suppresses menu text drawing for lines overlapped or reserved by the image.
/// Algorithm:
/// 1 Acquire destination line buffer (framebuffer or DVI line buffer).
/// 2 If an image is active:
/// - Clear the line (only when image is moving: imagex || imagey) to prevent artifacts.
/// - If current scanline is within image vertical bounds, memcpy the corresponding image row.
/// - Reserve horizontal offset (offset = w) so text starts after image when image at top area (<120px).
/// 3 If not in screensaver mode (imagex==0 && imagey==0) draw text glyphs via RomSelect_DrawLine(),
/// passing offset so text can start after embedded image when used for metadata screens.
/// 4 Submit the populated line buffer back to the video subsystem when not using full framebuffer.
/// Notes:
/// - Safety checks ensure w/h are within screen bounds before treating imagebuffer as valid.
/// - Color mapping differs when useFrameBuffer is true (raw palette indices) versus false (lookup table).
/// - Clearing only moving-image lines reduces flicker on first static metadata image display.
/// - offset logic prevents garbled text when small images (<120px high) occupy left side.
/// Performance:
/// - memcpy used for image row copy (w * sizeof(uint16_t) bytes).
/// - Glyph rendering loops over SCREEN_COLS (character cells) * 8 pixels horizontally.
/// Edge cases:
/// - Invalid image dimensions: image ignored; only text drawn.
/// - scanline outside imagey..imagey+h: only text (unless reserved offset for early lines).
void drawline(int scanline, int selectedRow, int w = 0, int h = 0, uint16_t *imagebuffer = nullptr, int imagex = 0, int imagey = 0)
{
#if !HSTX
dvi::DVI::LineBuffer *b = nullptr;
#if FRAMEBUFFERISPOSSIBLE
if (Frens::isFrameBufferUsed())
{
WorkLineRom = &Frens::framebuffer[scanline * SCREENWIDTH];
}
else
{
#endif
b = dvi_->getLineBuffer();
WorkLineRom = b->data();
#if FRAMEBUFFERISPOSSIBLE
}
#endif
#else
WorkLineRom = hstx_getlineFromFramebuffer(scanline);
#endif // !HSTX
auto offset = 0;
bool validImage = (imagebuffer != nullptr) && (w > 0 && w <= SCREENWIDTH && h > 0 && h <= SCREENHEIGHT);
if (validImage)
{
// avoid flicker on first line in metadata screen
// clear line only when image is moving (screensaver)
if (imagex || imagey)
{
memset(WorkLineRom, 0, SCREENWIDTH * sizeof(WORD));
}
if (scanline >= imagey && scanline < imagey + h)
{
// printf("Drawing image at scanline %d, imagey %d, h %d imagey + h %d\n", scanline, imagey, h, imagey + h);
// copy image row into worklinerom
auto rowOffset = (scanline - imagey) * w;
memcpy(WorkLineRom + imagex, imagebuffer + rowOffset, w * sizeof(uint16_t));
offset = w;
}
else
{
// avoid garbeled text when image is smaller than 120 pixels high
if (scanline < 120)
{
offset = w;
}
}
}
// Only show text when not in screensaver mode (imagex and imagey are 0)
if (imagex == 0 && imagey == 0)
{
RomSelect_DrawLine(scanline, selectedRow, offset);
}
#if !HSTX
#if FRAMEBUFFERISPOSSIBLE
if (!Frens::isFrameBufferUsed())
{
#endif
dvi_->setLineBuffer(scanline, b);
#if FRAMEBUFFERISPOSSIBLE
}
#endif
#endif
}
void putText(int x, int y, const char *text, int fgcolor, int bgcolor, bool wraplines, int offset)
{
if (text != nullptr)
{
int cur_x = x;
int cur_y = y;
auto index = cur_y * SCREEN_COLS + cur_x;
auto maxLen = strlen(text);
bool lastWasSpace = false;
while (index < SCREENBUFCELLS && *text && maxLen > 0)
{
if (wraplines && !isspace(*text))
{
// Word wrapping: find length of next word
const char *word_start = text;
int word_len = 0;
while (word_start[word_len] && !isspace(word_start[word_len]))
{
word_len++;
}
// If word doesn't fit, move to next line
if (cur_x + word_len > SCREEN_COLS && cur_x != 0)
{
cur_x = offset;
cur_y++;
index = cur_y * SCREEN_COLS + cur_x;
if (index >= SCREENBUFCELLS)
break;
}
// Write the word
for (int i = 0; i < word_len && index < SCREENBUFCELLS && maxLen > 0; i++)
{
char ch = *text++;
if ((unsigned char)ch < 32 || (unsigned char)ch > 126)
ch = ' ';
screenBuffer[index].charvalue = (ch == '_' ? ' ' : ch);
screenBuffer[index].fgcolor = fgcolor;
screenBuffer[index].bgcolor = bgcolor;
cur_x++;
maxLen--;
lastWasSpace = false;
index = cur_y * SCREEN_COLS + cur_x;
}
// Write any following spaces (collapse consecutive)
while (*text && isspace(*text) && index < SCREENBUFCELLS && maxLen > 0)
{
if (!lastWasSpace)
{
char ch = *text;
if ((unsigned char)ch < 32 || (unsigned char)ch > 126)
ch = ' ';
screenBuffer[index].charvalue = (ch == '_' ? ' ' : ch);
screenBuffer[index].fgcolor = fgcolor;
screenBuffer[index].bgcolor = bgcolor;
cur_x++;
maxLen--;
lastWasSpace = true;
if (cur_x >= SCREEN_COLS)
{
cur_x = offset;
cur_y++;
}
index = cur_y * SCREEN_COLS + cur_x;
}
text++;
}
}
else
{
char ch = *text++;
if ((unsigned char)ch < 32 || (unsigned char)ch > 126)
ch = ' ';
if (isspace(ch))
{
if (lastWasSpace)
continue;
lastWasSpace = true;
}
else
{
lastWasSpace = false;
}
screenBuffer[index].charvalue = (ch == '_' ? ' ' : ch);
screenBuffer[index].fgcolor = fgcolor;
screenBuffer[index].bgcolor = bgcolor;
cur_x++;
maxLen--;
if (cur_x >= SCREEN_COLS)
{
if (wraplines)
{
cur_x = offset;
cur_y++;
}
else
{
break; // Stop writing if wraplines is false
}
}
index = cur_y * SCREEN_COLS + cur_x;
}
}
}
}
void DrawScreen(int selectedRow, int w = 0, int h = 0, uint16_t *imagebuffer = nullptr, int imagex = 0, int imagey = 0)
{
const char *spaces = " ";
char tmpstr[24];
char s[SCREEN_COLS + 1];
char buttonLabel1[2];
char buttonLabel2[2];
getButtonLabels(buttonLabel1, buttonLabel2);
if (selectedRow != -1)
{
if (EXT_AUDIO_DACERROR())
{
putText(1, ENDROW + 3, "Dac Initialization Failed", CRED, CWHITE);
}
putText(SCREEN_COLS / 2 - strlen(spaces) / 2, SCREEN_ROWS - 1, spaces, settings.bgcolor, settings.bgcolor);
if ( connectedGamePadShortName[0] != nullptr && connectedGamePadShortName[1] != nullptr)
{
snprintf(tmpstr, sizeof(tmpstr), "%s/%s", connectedGamePadShortName[0], connectedGamePadShortName[1]);
}
else
{
if (connectedGamePadName[0] != nullptr)
{
snprintf(tmpstr, sizeof(tmpstr), "%s", connectedGamePadName[0]);
}
else
{
if (connectedGamePadName[1] != nullptr)
{
snprintf(tmpstr, sizeof(tmpstr), "%s", connectedGamePadName[1]);
}
else {
snprintf(tmpstr, sizeof(tmpstr), "No USB GamePad");
}
}
}
putText(SCREEN_COLS / 2 - strlen(tmpstr) / 2, SCREEN_ROWS - 1, tmpstr, CBLUE, CWHITE);
snprintf(s, sizeof(s), "%c%dK %c%c",
Frens::isPsramEnabled() ? 'P' : 'F',
maxRomSize / 1024,
WIIPAD_IS_CONNECTED() ? 'W' : ' ',
EXT_AUDIO_IS_ENABLED ? (USE_PICO_EXTRAS_I2S ? 'E' : 'L') : ' ');
putText(1, SCREEN_ROWS - 1, s, settings.fgcolor, settings.bgcolor);
snprintf(s, sizeof(s), "%s:Open %s:Back", buttonLabel1, buttonLabel2);
putText(1, ENDROW + 2, s, settings.fgcolor, settings.bgcolor);
bool artworkEnabled = isArtWorkEnabled();
if (artworkEnabled)
{
strcpy(s, "START:Info");
putText(17, ENDROW + 2, s, settings.fgcolor, settings.bgcolor);
}
int optionsRow = artworkEnabled ? ENDROW + 3 : ENDROW + 2;
if (strcmp(connectedGamePadName[0], "Genesis Mini 2") == 0 || strcmp(connectedGamePadName[0], "MDArcade") == 0)
{
strcpy(s, "Mode:Settings");
}
else
{
if (strncmp(connectedGamePadName[0] , "Genesis", 7) == 0)
{
strcpy(s, "C:Settings");
}
else
{
strcpy(s, "SELECT:Settings" );
}
}
putText(17, optionsRow, s, settings.fgcolor, settings.bgcolor);
}
for (auto line = 0; line < 240; line++)
{
drawline(line, selectedRow, w, h, imagebuffer, imagex, imagey);
}
}
void ClearScreen(int color)
{
for (auto i = 0; i < SCREENBUFCELLS; i++)
{
screenBuffer[i].bgcolor = color;
screenBuffer[i].fgcolor = color;
screenBuffer[i].charvalue = ' ';
}
}
inline void showhdmilabel()
{
short fgcolor = settingsActive ? CBLACK : settings.fgcolor;
short bgcolor = settingsActive ? CWHITE : settings.bgcolor;
#if HSTX
if (video_output_get_dvi_mode())
{
putText(SCREEN_COLS - 4, 0, "DVI", fgcolor, bgcolor);
}
else
{
putText(SCREEN_COLS - 5, 0, "HDMI", fgcolor, bgcolor);
}
#else
putText(SCREEN_COLS - 5, 0, "HDMI", fgcolor, bgcolor);
#endif
}
char *menutitle = nullptr;
// Returns SWVERSION, or build date/time as "DD/MM[/YY] HH:MM" when SWVERSION is "VX.X".
static const char *getVersionString(char *buf, size_t bufsize, bool showYear = false)
{
if (strcmp(SWVERSION, "VX.X") == 0) {
const char *months = "JanFebMarAprMayJunJulAugSepOctNovDec";
const char *d = __DATE__;
const char *t = __TIME__;
int day = (d[4] == ' ' ? 0 : (d[4] - '0') * 10) + (d[5] - '0');
int m = 0;
for (int i = 0; i < 12; i++) {
if (months[i * 3] == d[0] && months[i * 3 + 1] == d[1] && months[i * 3 + 2] == d[2]) {
m = i + 1;
break;
}
}
if (showYear)
snprintf(buf, bufsize, "%02d/%02d/%.2s %.5s", day, m, d + 9, t);
else
snprintf(buf, bufsize, "%02d/%02d %.5s", day, m, t);
} else {
snprintf(buf, bufsize, "%s", SWVERSION);
}
return buf;
}
void displayRoms(Frens::RomLister &romlister, int startIndex)
{
char buffer[ROMLISTER_MAXPATH + 4];
char s[SCREEN_COLS + 1];
auto y = STARTROW;
auto entries = romlister.GetEntries();
ClearScreen(settings.bgcolor);
snprintf(s, sizeof(s), "- %s -", menutitle);
putText(SCREEN_COLS / 2 - strlen(s) / 2, 0, s, settings.fgcolor, settings.bgcolor);
snprintf(buffer, sizeof(buffer), "%uMHZ", clock_get_hz(clk_sys) / 1000000);
showhdmilabel();
putText(1, 0, buffer, settings.fgcolor, settings.bgcolor);
strcpy(s, "Choose a rom to play:");
putText(SCREEN_COLS / 2 - strlen(s) / 2, 1, s, settings.fgcolor, settings.bgcolor);
// strcpy(s, "---------------------");
// putText(SCREEN_COLS / 2 - strlen(s) / 2, 1, s, fgcolor, bgcolor);
for (int i = 1; i < SCREEN_COLS - 1; i++)
{
putText(i, STARTROW - 1, "-", settings.fgcolor, settings.bgcolor);
}
for (int i = 1; i < SCREEN_COLS - 1; i++)
{
putText(i, ENDROW + 1, "-", settings.fgcolor, settings.bgcolor);
}
// strcpy(s, "A Select, B Back");
// putText(1, ENDROW + 2, s, settings.fgcolor, settings.bgcolor);
putText(SCREEN_COLS - strlen(PICOHWNAME_) - 1, ENDROW + 2, PICOHWNAME_, settings.fgcolor, settings.bgcolor);
{
char versionStr[30];
getVersionString(versionStr, sizeof(versionStr));
putText(SCREEN_COLS - strlen(versionStr) - 1, SCREEN_ROWS - 1, versionStr, settings.fgcolor, settings.bgcolor);
}
// putText(SCREEN_COLS / 2 - strlen(picoType()) / 2, SCREEN_ROWS - 2, picoType(), fgcolor, bgcolor);
for (auto index = startIndex; index < romlister.Count(); index++)
{
if (y <= ENDROW)
{
auto info = entries[index];
if (info.IsDirectory)
{
// snprintf(buffer, sizeof(buffer), "D %s", info.Path);
snprintf(buffer, SCREEN_COLS - 1, "D %s", info.Path);
}
else
{
// snprintf(buffer, sizeof(buffer), "R %s", info.Path);
snprintf(buffer, SCREEN_COLS - 1, "R %s", info.Path);
}
putText(1, y, buffer, settings.fgcolor, settings.bgcolor);
y++;
}
}
}
static inline void drawAllLines(int selected)
{
for (int lineNr = 0; lineNr < 240; ++lineNr)
{
drawline(lineNr, selected);
}
}
void waitForNoButtonPress()
{
DWORD PAD1_Latch;
while (true)
{
DrawScreen(-1);
Menu_LoadFrame();
RomSelect_PadState(&PAD1_Latch);
if (PAD1_Latch == 0)
{
return;
}
}
}
void menuPumpBlankFrames(int count)
{
#if !HSTX
int margintop = dvi_->getBlankSettings().top;
int marginbottom = dvi_->getBlankSettings().bottom;
scaleMode8_7_ = Frens::applyScreenMode(ScreenMode::NOSCANLINE_1_1);
dvi_->getBlankSettings().top = 0;
dvi_->getBlankSettings().bottom = 0;
#endif
for (int i = 0; i < count; i++)
{
#if HSTX
memset(hstx_getframebuffer(), 0, SCREENWIDTH * SCREENHEIGHT * sizeof(WORD));
#else
#if FRAMEBUFFERISPOSSIBLE
if (Frens::isFrameBufferUsed())
{
memset(Frens::framebuffer, 0, SCREENWIDTH * SCREENHEIGHT * sizeof(WORD));
}
else
{
#endif
for (int line = 0; line < SCREENHEIGHT; line++)
{
auto b = dvi_->getLineBuffer();
memset(b->data(), 0, SCREENWIDTH * sizeof(uint16_t));
dvi_->setLineBuffer(line, b);
}
#if FRAMEBUFFERISPOSSIBLE
}
#endif
#endif
Menu_LoadFrame();
}
#if !HSTX
scaleMode8_7_ = Frens::applyScreenMode(settings.screenMode);
// Reset the screen mode to the original settings
// Do not reset the margins when framebuffer is used, this will lock up the display driver
// Margins will be handled by the framebuffer.
if (!Frens::isFrameBufferUsed())
{
dvi_->getBlankSettings().top = margintop;
dvi_->getBlankSettings().bottom = marginbottom;
}
#endif
}
static inline int centerColClamped(int textLen)
{
int col = (SCREEN_COLS - textLen) / 2;
return col < 0 ? 0 : col;
}
static void showMessageBox(const char *message1, unsigned short fgcolor, const char *message2, const char *message3)
{
ClearScreen(settings.bgcolor);
waitForNoButtonPress();
int row = SCREEN_ROWS / 2 - 1;
putText(centerColClamped(strlen(message1)), row, message1, fgcolor, settings.bgcolor);
if (message2)
{
row += 2;
putText(centerColClamped(strlen(message2)), row, message2, settings.fgcolor, settings.bgcolor);
}
if (message3)
{
row += 2;
putText(centerColClamped(strlen(message3)), row, message3, settings.fgcolor, settings.bgcolor);
}
DWORD waitPad;
do
{
drawAllLines(-1);
RomSelect_PadState(&waitPad);
Menu_LoadFrame();
} while (!waitPad);
}
static void showMessageBox(const char *message1, unsigned short fgcolor)
{
const char *defaultMessage = "Press any button to continue.";
showMessageBox(message1, fgcolor, defaultMessage, nullptr);
}
static void showMessageBox(const char *message1, int fgcolor, const char *message2)
{
const char *defaultMessage = "Press any button to continue.";
showMessageBox(message1, fgcolor, message2, defaultMessage);
}
static bool showDialogYesNo(const char *message)
{
char tmpMsg[10];
ClearScreen(settings.bgcolor);
int row = SCREEN_ROWS / 2 - 1;
putText(centerColClamped(strlen(message)), row, message, settings.fgcolor, settings.bgcolor);
getButtonLabels(buttonLabel1, buttonLabel2);
snprintf(tmpMsg, sizeof(tmpMsg), "%s:Yes", buttonLabel1);
const char *optionNo = buttonLabel2;
row += 2;
putText(centerColClamped(strlen(tmpMsg)), row, tmpMsg, settings.fgcolor, settings.bgcolor);
row += 1;
snprintf(tmpMsg, sizeof(tmpMsg), "%s:No_", buttonLabel2);
putText(centerColClamped(strlen(tmpMsg)), row, tmpMsg, settings.fgcolor, settings.bgcolor);
waitForNoButtonPress();
DWORD waitPad;
while (true)
{
drawAllLines(-1);
RomSelect_PadState(&waitPad);
Menu_LoadFrame();
if (waitPad & A)
{
return true;
}
else if (waitPad & B)