/
panel.cpp
2473 lines (2055 loc) · 74.3 KB
/
panel.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 <cstdlib>
#include <cmath>
#include <cstring>
#include <vector>
#include <wx/dcbuffer.h>
#include <SDL_joystick.h>
#include "version.h"
#include "../common/ConfigManager.h"
#include "../common/Patch.h"
#include "../gb/gbPrinter.h"
#include "../gba/RTC.h"
#include "../gba/agbprint.h"
#include "../sdl/text.h"
#include "drawing.h"
#include "filters.h"
#include "wxvbam.h"
int emulating;
IMPLEMENT_DYNAMIC_CLASS(GameArea, wxPanel)
GameArea::GameArea()
: wxPanel()
, panel(NULL)
, emusys(NULL)
, was_paused(false)
, rewind_time(0)
, do_rewind(false)
, rewind_mem(0)
, loaded(IMAGE_UNKNOWN)
, basic_width(GBAWidth)
, basic_height(GBAHeight)
, fullscreen(false)
, paused(false)
, pointer_blanked(false)
, mouse_active_time(0)
{
SetSizer(new wxBoxSizer(wxVERTICAL));
// all renderers prefer 32-bit
// well, "simple" prefers 24-bit, but that's not available for filters
systemColorDepth = 32;
hq2x_init(32);
Init_2xSaI(32);
}
void GameArea::LoadGame(const wxString& name)
{
rom_scene_rls = wxT("-");
rom_scene_rls_name = wxT("-");
rom_name = wxT("");
// fex just crashes if file does not exist and it's compressed,
// so check first
wxFileName fnfn(name);
bool badfile = !fnfn.IsFileReadable();
// if path was relative, look for it before giving up
if (badfile && !fnfn.IsAbsolute()) {
wxString rp = fnfn.GetPath();
// can't really decide which dir to use, so try GBA first, then GB
if (!wxGetApp().GetAbsolutePath(gopts.gba_rom_dir).empty()) {
fnfn.SetPath(wxGetApp().GetAbsolutePath(gopts.gba_rom_dir) + wxT('/') + rp);
badfile = !fnfn.IsFileReadable();
}
if (badfile && !wxGetApp().GetAbsolutePath(gopts.gb_rom_dir).empty()) {
fnfn.SetPath(wxGetApp().GetAbsolutePath(gopts.gb_rom_dir) + wxT('/') + rp);
badfile = !fnfn.IsFileReadable();
}
if (badfile && !wxGetApp().GetAbsolutePath(gopts.gbc_rom_dir).empty()) {
fnfn.SetPath(wxGetApp().GetAbsolutePath(gopts.gbc_rom_dir) + wxT('/') + rp);
badfile = !fnfn.IsFileReadable();
}
}
// auto-conversion of wxCharBuffer to const char * seems broken
// so save underlying wxCharBuffer (or create one of none is used)
wxCharBuffer fnb(fnfn.GetFullPath().mb_fn_str());
const char* fn = fnb.data();
IMAGE_TYPE t = badfile ? IMAGE_UNKNOWN : utilFindType(fn);
if (t == IMAGE_UNKNOWN) {
wxString s;
s.Printf(_("%s is not a valid ROM file"), name.c_str());
wxMessageDialog dlg(GetParent(), s, _("Problem loading file"), wxOK | wxICON_ERROR);
dlg.ShowModal();
return;
}
{
wxFileConfig* cfg = wxGetApp().cfg;
if (!gopts.recent_freeze) {
gopts.recent->AddFileToHistory(name);
wxGetApp().frame->SetRecentAccels();
cfg->SetPath(wxT("/Recent"));
gopts.recent->Save(*cfg);
cfg->SetPath(wxT("/"));
cfg->Flush();
}
}
UnloadGame();
// strip extension from actual game file name
// FIXME: save actual file name of archive so patches & cheats can
// be loaded from archive
// FIXME: if archive name does not match game name, prepend archive
// name to uniquify game name
loaded_game = fnfn;
loaded_game.ClearExt();
loaded_game.MakeAbsolute();
// load patch, if enabled
// note that it is difficult to load from archive due to
// ../common/Patch.cpp depending on opening the file itself and doing
// lots of seeking & such. The only way would be to copy the patch
// out to a temporary file and load it (and can't just use
// AssignTempFileName because it needs correct extension)
// too much trouble for now, though
bool loadpatch = autoPatch;
wxFileName pfn = loaded_game;
int ovSaveType = 0;
if (loadpatch) {
// SetExt may strip something off by accident, so append to text instead
// pfn.SetExt(wxT("ips")
pfn.SetFullName(pfn.GetFullName() + wxT(".ips"));
if (!pfn.IsFileReadable()) {
pfn.SetExt(wxT("ups"));
if (!pfn.IsFileReadable()) {
pfn.SetExt(wxT("ppf"));
loadpatch = pfn.IsFileReadable();
}
}
}
if (t == IMAGE_GB) {
if (!gbLoadRom(fn)) {
wxString s;
s.Printf(_("Unable to load Game Boy ROM %s"), name.c_str());
wxMessageDialog dlg(GetParent(), s, _("Problem loading file"), wxOK | wxICON_ERROR);
dlg.ShowModal();
return;
}
rom_size = gbRomSize;
if (loadpatch) {
int size = rom_size;
// auto-conversion of wxCharBuffer to const char * seems broken
// so save underlying wxCharBuffer (or create one of none is used)
wxCharBuffer pfnb(pfn.GetFullPath().mb_fn_str());
applyPatch(pfnb.data(), &gbRom, &size);
if (size != (int)rom_size)
gbUpdateSizes();
rom_size = size;
}
// start sound; this must happen before CPU stuff
gb_effects_config.enabled = gopts.gb_effects_config_enabled;
gb_effects_config.surround = gopts.gb_effects_config_surround;
gb_effects_config.echo = (float)gopts.gb_echo / 100.0;
gb_effects_config.stereo = (float)gopts.gb_stereo / 100.0;
gbSoundSetDeclicking(gopts.gb_declick);
soundInit();
soundSetEnable(gopts.sound_en);
gbSoundSetSampleRate(!gopts.sound_qual ? 48000 : 44100 / (1 << (gopts.sound_qual - 1)));
soundSetVolume((float)gopts.sound_vol / 100.0);
// this **MUST** be called **AFTER** setting sample rate because the core calls soundInit()
soundSetThrottle(throttle);
gbGetHardwareType();
bool use_bios = false;
// auto-conversion of wxCharBuffer to const char * seems broken
// so save underlying wxCharBuffer (or create one of none is used)
const char* fn = NULL;
wxCharBuffer fnb;
if (gbCgbMode) {
use_bios = useBiosFileGBC;
fnb = gopts.gbc_bios.mb_fn_str();
} else {
use_bios = useBiosFileGB;
fnb = gopts.gb_bios.mb_fn_str();
}
fn = fnb.data();
gbCPUInit(fn, use_bios);
if (use_bios && !useBios) {
wxLogError(_("Could not load BIOS %s"), (gbCgbMode ? gopts.gbc_bios : gopts.gb_bios).c_str());
// could clear use flag & file name now, but better to force
// user to do it
}
gbReset();
if (gbBorderOn) {
basic_width = gbBorderLineSkip = SGBWidth;
basic_height = SGBHeight;
gbBorderColumnSkip = (SGBWidth - GBWidth) / 2;
gbBorderRowSkip = (SGBHeight - GBHeight) / 2;
} else {
basic_width = gbBorderLineSkip = GBWidth;
basic_height = GBHeight;
gbBorderColumnSkip = gbBorderRowSkip = 0;
}
emusys = &GBSystem;
} else /* if(t == IMAGE_GBA) */
{
if (!(rom_size = CPULoadRom(fn))) {
wxString s;
s.Printf(_("Unable to load Game Boy Advance ROM %s"), name.c_str());
wxMessageDialog dlg(GetParent(), s, _("Problem loading file"), wxOK | wxICON_ERROR);
dlg.ShowModal();
return;
}
rom_crc32 = crc32(0L, rom, rom_size);
if (loadpatch) {
// don't use real rom size or it might try to resize rom[]
// instead, use known size of rom[]
int size = 0x2000000;
// auto-conversion of wxCharBuffer to const char * seems broken
// so save underlying wxCharBuffer (or create one of none is used)
wxCharBuffer pfnb(pfn.GetFullPath().mb_fn_str());
applyPatch(pfnb.data(), &rom, &size);
// that means we no longer really know rom_size either <sigh>
}
wxFileConfig* cfg = wxGetApp().overrides;
wxString id = wxString((const char*)&rom[0xac], wxConvLibc, 4);
if (cfg->HasGroup(id)) {
cfg->SetPath(id);
rtcEnable(cfg->Read(wxT("rtcEnabled"), rtcEnabled));
int fsz = cfg->Read(wxT("flashSize"), (long)0);
if (fsz != 0x10000 && fsz != 0x20000)
fsz = 0x10000 << winFlashSize;
flashSetSize(fsz);
ovSaveType = cfg->Read(wxT("saveType"), cpuSaveType);
if (ovSaveType < 0 || ovSaveType > 5)
ovSaveType = 0;
if (ovSaveType == 0)
utilGBAFindSave(rom_size);
else
saveType = ovSaveType;
mirroringEnable = cfg->Read(wxT("mirroringEnabled"), (long)1);
cfg->SetPath(wxT("/"));
} else {
rtcEnable(rtcEnabled);
flashSetSize(0x10000 << winFlashSize);
if (cpuSaveType < 0 || cpuSaveType > 5)
cpuSaveType = 0;
if (cpuSaveType == 0)
utilGBAFindSave(rom_size);
else
saveType = cpuSaveType;
mirroringEnable = false;
}
doMirroring(mirroringEnable);
// start sound; this must happen before CPU stuff
soundInit();
soundSetEnable(gopts.sound_en);
soundSetSampleRate(!gopts.sound_qual ? 48000 : 44100 / (1 << (gopts.sound_qual - 1)));
soundSetVolume((float)gopts.sound_vol / 100.0);
// this **MUST** be called **AFTER** setting sample rate because the core calls soundInit()
soundSetThrottle(throttle);
soundFiltering = (float)gopts.gba_sound_filter / 100.0f;
CPUInit(gopts.gba_bios.mb_fn_str(), useBiosFileGBA);
if (useBiosFileGBA && !useBios) {
wxLogError(_("Could not load BIOS %s"), gopts.gba_bios.c_str());
// could clear use flag & file name now, but better to force
// user to do it
}
CPUReset();
basic_width = GBAWidth;
basic_height = GBAHeight;
emusys = &GBASystem;
}
if (fullScreen)
GameArea::ShowFullScreen(true);
loaded = t;
SetFrameTitle();
SetFocus();
// Use custom geometry
AdjustSize(false);
emulating = true;
was_paused = true;
MainFrame* mf = wxGetApp().frame;
mf->SetJoystick();
mf->cmd_enable &= ~(CMDEN_GB | CMDEN_GBA);
mf->cmd_enable |= ONLOAD_CMDEN;
mf->cmd_enable |= loaded == IMAGE_GB ? CMDEN_GB : (CMDEN_GBA | CMDEN_NGDB_GBA);
mf->enable_menus();
#if (defined __WIN32__ || defined _WIN32)
#ifndef NO_LINK
gbSerialFunction = gbStartLink;
#else
gbSerialFunction = NULL;
#endif
#endif
// probably only need to do this for GB carts
if (winGbPrinterEnabled)
gbSerialFunction = gbPrinterSend;
// probably only need to do this for GBA carts
agbPrintEnable(agbPrint);
// set frame skip based on ROM type
systemFrameSkip = frameSkip;
if (systemFrameSkip < 0)
systemFrameSkip = 0;
// load battery and/or saved state
recompute_dirs();
mf->update_state_ts(true);
bool did_autoload = gopts.autoload_state ? LoadState() : false;
if (!did_autoload || skipSaveGameBattery) {
wxString bname = loaded_game.GetFullName();
#ifndef NO_LINK
// MakeInstanceFilename doesn't do wxString, so just add slave ID here
int playerId = GetLinkPlayerId();
if (playerId >= 0) {
bname.append(wxT('-'));
bname.append(wxChar(wxT('1') + playerId));
}
#endif
bname.append(wxT(".sav"));
wxFileName bat(batdir, bname);
fnb = bat.GetFullPath().mb_fn_str();
if (emusys->emuReadBattery(fnb.data())) {
wxString msg;
msg.Printf(_("Loaded battery %s"), bat.GetFullPath().c_str());
systemScreenMessage(msg);
if (cpuSaveType == 0 && ovSaveType == 0 && t == IMAGE_GBA) {
switch (bat.GetSize().GetValue()) {
case 0x200:
case 0x2000:
saveType = GBA_SAVE_EEPROM;
break;
case 0x8000:
saveType = GBA_SAVE_SRAM;
break;
case 0x10000:
if (saveType == GBA_SAVE_EEPROM || saveType == GBA_SAVE_SRAM)
break;
break;
case 0x20000:
saveType = GBA_SAVE_FLASH;
flashSetSize(bat.GetSize().GetValue());
break;
default:
break;
}
SetSaveType(saveType);
}
}
// forget old save writes
systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED;
}
// do an immediate rewind save
// even if loaded from state file: not smart enough yet to just
// do a reset or load from state file when # rewinds == 0
do_rewind = gopts.rewind_interval > 0;
// FIXME: backup battery file (useful if game name conflict)
cheats_dirty = (did_autoload && !skipSaveGameCheats) || (loaded == IMAGE_GB ? gbCheatNumber > 0 : cheatsNumber > 0);
if (gopts.autoload_cheats && (!did_autoload || skipSaveGameCheats)) {
wxFileName cfn = loaded_game;
// SetExt may strip something off by accident, so append to text instead
cfn.SetFullName(cfn.GetFullName() + wxT(".clt"));
if (cfn.IsFileReadable()) {
bool cld;
if (loaded == IMAGE_GB)
cld = gbCheatsLoadCheatList(cfn.GetFullPath().mb_fn_str());
else
cld = cheatsLoadCheatList(cfn.GetFullPath().mb_fn_str());
if (cld) {
systemScreenMessage(_("Loaded cheats"));
cheats_dirty = false;
}
}
}
#ifndef NO_LINK
if (gopts.link_auto) {
linkMode = mf->GetConfiguredLinkMode();
BootLink(linkMode, gopts.link_host.mb_str(wxConvUTF8), linkTimeout, linkHacks, linkNumPlayers);
}
#endif
}
void GameArea::SetFrameTitle()
{
wxString tit = wxT("");
if (loaded != IMAGE_UNKNOWN) {
tit.append(loaded_game.GetFullName());
tit.append(wxT(" - "));
}
tit.append(wxT("VisualBoyAdvance-M "));
#ifndef FINAL_BUILD
tit.append(_(VERSION));
#endif
#ifndef NO_LINK
int playerId = GetLinkPlayerId();
if (playerId >= 0) {
tit.append(_(" player "));
tit.append(wxChar(wxT('1') + playerId));
}
#endif
wxGetApp().frame->SetTitle(tit);
}
void GameArea::recompute_dirs()
{
batdir = gopts.battery_dir;
if (!batdir.size()) {
batdir = loaded_game.GetPathWithSep();
} else {
batdir = wxGetApp().GetAbsolutePath(gopts.battery_dir);
}
if (!wxIsWritable(batdir)) {
batdir = wxGetApp().GetDataDir();
}
statedir = gopts.state_dir;
if (!statedir.size()) {
statedir = loaded_game.GetPathWithSep();
} else {
statedir = wxGetApp().GetAbsolutePath(gopts.state_dir);
}
if (!wxIsWritable(statedir)) {
statedir = wxGetApp().GetDataDir();
}
}
void GameArea::UnloadGame(bool destruct)
{
if (!emulating)
return;
// last opportunity to autosave cheats
if (gopts.autoload_cheats && cheats_dirty) {
wxFileName cfn = loaded_game;
// SetExt may strip something off by accident, so append to text instead
cfn.SetFullName(cfn.GetFullName() + wxT(".clt"));
if (loaded == IMAGE_GB) {
if (!gbCheatNumber)
wxRemoveFile(cfn.GetFullPath());
else
gbCheatsSaveCheatList(cfn.GetFullPath().mb_fn_str());
} else {
if (!cheatsNumber)
wxRemoveFile(cfn.GetFullPath());
else
cheatsSaveCheatList(cfn.GetFullPath().mb_fn_str());
}
}
// if timer was counting down for save, go ahead and save
// this might not be safe, though..
if (systemSaveUpdateCounter > SYSTEM_SAVE_NOT_UPDATED) {
SaveBattery();
}
MainFrame* mf = wxGetApp().frame;
#ifndef NO_FFMPEG
snd_rec.Stop();
vid_rec.Stop();
#endif
systemStopGameRecording();
systemStopGamePlayback();
debugger = false;
remoteCleanUp();
mf->cmd_enable |= CMDEN_NGDB_ANY;
if (loaded == IMAGE_GB) {
gbCleanUp();
gbCheatRemoveAll();
} else if (loaded == IMAGE_GBA) {
CPUCleanUp();
cheatsDeleteAll(false);
}
emulating = false;
loaded = IMAGE_UNKNOWN;
emusys = NULL;
soundShutdown();
if (destruct)
return;
// in destructor, panel should be auto-deleted by wx since all panels
// are derived from a window attached as child to GameArea
if (panel)
panel->Destroy();
panel = NULL;
// close any game-related viewer windows
// in destructor, viewer windows are in process of being deleted anyway
while (!mf->popups.empty())
mf->popups.front()->Close(true);
// remaining items are GUI updates that should not be needed in destructor
SetFrameTitle();
mf->cmd_enable &= UNLOAD_CMDEN_KEEP;
mf->update_state_ts(true);
mf->enable_menus();
mf->SetJoystick();
mf->ResetCheatSearch();
if (rewind_mem)
num_rewind_states = 0;
}
bool GameArea::LoadState()
{
int slot = wxGetApp().frame->newest_state_slot();
if (slot < 1)
return false;
return LoadState(slot);
}
bool GameArea::LoadState(int slot)
{
wxString fname;
fname.Printf(SAVESLOT_FMT, game_name().c_str(), slot);
return LoadState(wxFileName(statedir, fname));
}
bool GameArea::LoadState(const wxFileName& fname)
{
// FIXME: first save to backup state if not backup state
bool ret = emusys->emuReadState(fname.GetFullPath().mb_fn_str());
if (ret && num_rewind_states) {
MainFrame* mf = wxGetApp().frame;
mf->cmd_enable &= ~CMDEN_REWIND;
mf->enable_menus();
num_rewind_states = 0;
// do an immediate rewind save
// even if loaded from state file: not smart enough yet to just
// do a reset or load from state file when # rewinds == 0
do_rewind = true;
rewind_time = gopts.rewind_interval * 6;
}
if (ret) {
// forget old save writes
systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED;
// no point in blending after abrupt change
InterframeCleanup();
// frame rate calc should probably reset as well
was_paused = true;
// save state had a screen frame, so draw it
systemDrawScreen();
}
wxString msg;
msg.Printf(ret ? _("Loaded state %s") : _("Error loading state %s"),
fname.GetFullPath().c_str());
systemScreenMessage(msg);
return ret;
}
bool GameArea::SaveState()
{
return SaveState(wxGetApp().frame->oldest_state_slot());
}
bool GameArea::SaveState(int slot)
{
wxString fname;
fname.Printf(SAVESLOT_FMT, game_name().c_str(), slot);
return SaveState(wxFileName(statedir, fname));
}
bool GameArea::SaveState(const wxFileName& fname)
{
// FIXME: first copy to backup state if not backup state
bool ret = emusys->emuWriteState(fname.GetFullPath().mb_fn_str());
wxGetApp().frame->update_state_ts(true);
wxString msg;
msg.Printf(ret ? _("Saved state %s") : _("Error saving state %s"),
fname.GetFullPath().c_str());
systemScreenMessage(msg);
return ret;
}
void GameArea::SaveBattery()
{
// MakeInstanceFilename doesn't do wxString, so just add slave ID here
wxString bname = game_name();
#ifndef NO_LINK
int playerId = GetLinkPlayerId();
if (playerId >= 0) {
bname.append(wxT('-'));
bname.append(wxChar(wxT('1') + playerId));
}
#endif
bname.append(wxT(".sav"));
wxFileName bat(batdir, bname);
bat.Mkdir(0777, wxPATH_MKDIR_FULL);
wxString fn = bat.GetFullPath();
// auto-conversion of wxCharBuffer to const char * seems broken
// so save underlying wxCharBuffer (or create one of none is used)
wxCharBuffer fnb = fn.mb_fn_str();
// FIXME: add option to support ring of backups
// of course some games just write battery way too often for such
// a thing to be useful
if (!emusys->emuWriteBattery(fnb.data()))
wxLogError(_("Error writing battery %s"), fn.c_str());
systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED;
}
void GameArea::AddBorder()
{
if (basic_width != GBWidth)
return;
basic_width = SGBWidth;
basic_height = SGBHeight;
gbBorderLineSkip = SGBWidth;
gbBorderColumnSkip = (SGBWidth - GBWidth) / 2;
gbBorderRowSkip = (SGBHeight - GBHeight) / 2;
AdjustSize(false);
wxGetApp().frame->Fit();
GetSizer()->Detach(panel->GetWindow());
if (panel)
panel->Destroy();
panel = NULL;
}
void GameArea::DelBorder()
{
if (basic_width != SGBWidth)
return;
basic_width = GBWidth;
basic_height = GBHeight;
gbBorderLineSkip = GBWidth;
gbBorderColumnSkip = gbBorderRowSkip = 0;
AdjustSize(false);
wxGetApp().frame->Fit();
GetSizer()->Detach(panel->GetWindow());
if (panel)
panel->Destroy();
panel = NULL;
}
void GameArea::AdjustMinSize()
{
wxWindow* frame = wxGetApp().frame;
double hidpi_scale_factor = HiDPIScaleFactor();
// note: could safely set min size to 1x or less regardless of video_scale
// but setting it to scaled size makes resizing to default easier
wxSize sz((std::ceil(basic_width * gopts.video_scale) / hidpi_scale_factor),
(std::ceil(basic_height * gopts.video_scale) / hidpi_scale_factor));
SetMinSize(sz);
#if wxCHECK_VERSION(2, 8, 8)
sz = frame->ClientToWindowSize(sz);
#else
sz += frame->GetSize() - frame->GetClientSize();
#endif
frame->SetMinSize(sz);
}
void GameArea::LowerMinSize()
{
wxWindow* frame = wxGetApp().frame;
double hidpi_scale_factor = HiDPIScaleFactor();
wxSize sz(std::ceil(basic_width / hidpi_scale_factor),
std::ceil(basic_height / hidpi_scale_factor));
SetMinSize(sz);
// do not take decorations into account
frame->SetMinSize(sz);
}
void GameArea::AdjustSize(bool force)
{
AdjustMinSize();
if (fullscreen)
return;
double hidpi_scale_factor = HiDPIScaleFactor();
const wxSize newsz((std::ceil(basic_width * gopts.video_scale) / hidpi_scale_factor),
(std::ceil(basic_height * gopts.video_scale) / hidpi_scale_factor));
if (!force) {
wxSize sz = GetClientSize();
if (sz.GetWidth() >= newsz.GetWidth() && sz.GetHeight() >= newsz.GetHeight())
return;
}
SetSize(newsz);
GetParent()->SetClientSize(newsz);
wxGetApp().frame->Fit();
}
void GameArea::ShowFullScreen(bool full)
{
if (full == fullscreen) {
// in case the tlw somehow lost its mind, force it to proper mode
if (wxGetApp().frame->IsFullScreen() != fullscreen)
wxGetApp().frame->ShowFullScreen(full);
return;
}
// on Mac maximize is native fullscreen, so ignore fullscreen requests
#ifdef __WXMAC__
if (full && main_frame->IsMaximized()) return;
#endif
fullscreen = full;
// just in case screen mode is going to change, go ahead and preemptively
// delete panel to be recreated immediately after resize
if (panel) {
panel->Destroy();
panel = NULL;
}
// Windows does not restore old window size/pos
// at least under Wine
// so store them before entering fullscreen
static bool cursz_valid = false;
static wxSize cursz;
static wxPoint curpos;
MainFrame* tlw = wxGetApp().frame;
int dno = wxDisplay::GetFromWindow(tlw);
if (!full) {
if (gopts.fs_mode.w && gopts.fs_mode.h) {
wxDisplay d(dno);
d.ChangeMode(wxDefaultVideoMode);
}
tlw->ShowFullScreen(false);
if (!cursz_valid) {
curpos = wxDefaultPosition;
cursz = tlw->GetMinSize();
}
tlw->SetSize(cursz);
tlw->SetPosition(curpos);
AdjustMinSize();
} else {
// close all non-modal dialogs
while (!tlw->popups.empty())
tlw->popups.front()->Close();
// Some kbd accels can send a menu open event without a close event,
// this happens on Mac in HiDPI mode for the fullscreen toggle accel.
main_frame->SetMenusOpened(false);
// mouse stays blank whenever full-screen
HidePointer();
cursz_valid = true;
cursz = tlw->GetSize();
curpos = tlw->GetPosition();
LowerMinSize();
if (gopts.fs_mode.w && gopts.fs_mode.h) {
// grglflargm
// stupid wx does not do fullscreen properly when video mode is
// changed under X11. Since it does not use xrandr to switch, it
// just changes the video mode without resizing the desktop. It
// still uses the original desktop size for fullscreen, though.
// It also seems to stick the top-left corner wherever it pleases,
// so I can't just resize the panel and expect it to show up.
// ^(*&^^&!!!! maybe just disable this code altogether on UNIX
// most 3d cards are fine with full screen size, anyway.
wxDisplay d(dno);
if (!d.ChangeMode(gopts.fs_mode)) {
wxLogInfo(_("Fullscreen mode %dx%d-%d@%d not supported; looking for another"),
gopts.fs_mode.w, gopts.fs_mode.h, gopts.fs_mode.bpp, gopts.fs_mode.refresh);
// specifying a mode may not work with bpp/rate of 0
// in particular, unix does Matches() in wrong direction
wxArrayVideoModes vm = d.GetModes();
int best_mode = -1;
size_t i;
for (i = 0; i < vm.size(); i++) {
if (vm[i].w != gopts.fs_mode.w || vm[i].h != gopts.fs_mode.h)
continue;
int bpp = vm[i].bpp;
if (gopts.fs_mode.bpp && bpp == gopts.fs_mode.bpp)
break;
if (!gopts.fs_mode.bpp && bpp == 32) {
gopts.fs_mode.bpp = 32;
break;
}
int bm_bpp = best_mode < 0 ? 0 : vm[i].bpp;
if (bpp == 32 && bm_bpp != 32)
best_mode = i;
else if (bpp == 24 && bm_bpp < 24)
best_mode = i;
else if (bpp == 16 && bm_bpp < 24 && bm_bpp != 16)
best_mode = i;
else if (!best_mode)
best_mode = i;
}
if (i == vm.size() && best_mode >= 0)
i = best_mode;
if (i == vm.size()) {
wxLogWarning(_("Fullscreen mode %dx%d-%d@%d not supported"),
gopts.fs_mode.w, gopts.fs_mode.h, gopts.fs_mode.bpp, gopts.fs_mode.refresh);
gopts.fs_mode = wxVideoMode();
for (i = 0; i < vm.size(); i++)
wxLogInfo(_("Valid mode: %dx%d-%d@%d"), vm[i].w,
vm[i].h, vm[i].bpp,
vm[i].refresh);
} else {
gopts.fs_mode.bpp = vm[i].bpp;
gopts.fs_mode.refresh = vm[i].refresh;
}
wxLogInfo(_("Chose mode %dx%d-%d@%d"),
gopts.fs_mode.w, gopts.fs_mode.h, gopts.fs_mode.bpp, gopts.fs_mode.refresh);
if (!d.ChangeMode(gopts.fs_mode)) {
wxLogWarning(_("Failed to change mode to %dx%d-%d@%d"),
gopts.fs_mode.w, gopts.fs_mode.h, gopts.fs_mode.bpp, gopts.fs_mode.refresh);
gopts.fs_mode.w = 0;
}
}
}
tlw->ShowFullScreen(true);
}
}
GameArea::~GameArea()
{
UnloadGame(true);
if (rewind_mem)
free(rewind_mem);
if (gopts.fs_mode.w && gopts.fs_mode.h && fullscreen) {
MainFrame* tlw = wxGetApp().frame;
int dno = wxDisplay::GetFromWindow(tlw);
wxDisplay d(dno);
d.ChangeMode(wxDefaultVideoMode);
}
}
void GameArea::Pause()
{
if (paused)
return;
// don't pause when linked
#ifndef NO_LINK
if (GetLinkMode() != LINK_DISCONNECTED)
return;
#endif
paused = was_paused = true;
if (loaded != IMAGE_UNKNOWN)
soundPause();
}
void GameArea::Resume()
{
if (!paused)
return;
paused = false;
SetExtraStyle(GetExtraStyle() | wxWS_EX_PROCESS_IDLE);
if (loaded != IMAGE_UNKNOWN)
soundResume();
SetFocus();
}
void GameArea::OnIdle(wxIdleEvent& event)
{
wxString pl = wxGetApp().pending_load;
if (pl.size()) {
// sometimes this gets into a loop if LoadGame() called before
// clearing pending_load. weird.
wxGetApp().pending_load = wxEmptyString;
LoadGame(pl);
MainFrame* mf = wxGetApp().frame;
if (gdbBreakOnLoad)
mf->GDBBreak();
if (debugger && loaded != IMAGE_GBA) {
wxLogError(_("Not a valid GBA cartridge"));
UnloadGame();
}
}
// stupid wx doesn't resize to screen size
// forcing it this way just puts it in an infinite loop, though
// with wx trying to resize/reposition to what it thinks is full screen
// every time it detects a manual resize like this
if (gopts.fs_mode.w && gopts.fs_mode.h && fullscreen) {
wxSize sz = GetSize();
if (sz.GetWidth() != gopts.fs_mode.w || sz.GetHeight() != gopts.fs_mode.h) {
wxGetApp().frame->Move(0, 84);
SetSize(gopts.fs_mode.w, gopts.fs_mode.h);
}
}
if (!emusys)
return;
if (!panel) {
switch (gopts.render_method) {
case RND_SIMPLE:
panel = new BasicDrawingPanel(this, basic_width, basic_height);
break;
#ifdef __WXMAC__
case RND_QUARTZ2D:
panel = new Quartz2DDrawingPanel(this, basic_width, basic_height);
break;
#endif
#ifndef NO_OGL
case RND_OPENGL: