/
sys.cpp
1320 lines (1056 loc) · 33.2 KB
/
sys.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 "../common/ConfigManager.h"
#include "../common/SoundSDL.h"
#include "wxvbam.h"
#include "SDL.h"
#include <wx/ffile.h>
#include <wx/generic/prntdlgg.h>
#include <wx/print.h>
#include <wx/printdlg.h>
// These should probably be in vbamcore
int systemVerbose;
int systemFrameSkip;
int systemRedShift;
int systemGreenShift;
int systemBlueShift;
int systemColorDepth;
uint16_t systemColorMap16[0x10000];
uint32_t systemColorMap32[0x10000];
#define gs555(x) (x | (x << 5) | (x << 10))
uint16_t systemGbPalette[24] = {
gs555(0x1f), gs555(0x15), gs555(0x0c), 0,
gs555(0x1f), gs555(0x15), gs555(0x0c), 0,
gs555(0x1f), gs555(0x15), gs555(0x0c), 0,
gs555(0x1f), gs555(0x15), gs555(0x0c), 0,
gs555(0x1f), gs555(0x15), gs555(0x0c), 0,
gs555(0x1f), gs555(0x15), gs555(0x0c), 0
};
int RGB_LOW_BITS_MASK;
// these are local, though.
int joypress[4], autofire, autohold;
static int sensorx[4], sensory[4], sensorz[4];
bool pause_next;
bool turbo;
// and this is from MFC interface
bool soundBufferLow;
void systemMessage(int id, const char* fmt, ...)
{
(void)id; // unused params
static char* buf = NULL;
static int buflen = 80;
va_list args;
if (!buf) {
buf = (char*)malloc(buflen);
if (!buf)
exit(1);
}
while (1) {
va_start(args, fmt);
int needsz = vsnprintf(buf, buflen, fmt, args);
va_end(args);
if (needsz < buflen)
break;
while (buflen <= needsz)
buflen *= 2;
free(buf);
buf = (char*)malloc(buflen);
if (!buf)
exit(1);
}
wxLogError(wxT("%s"), wxString(buf, wxConvLibc).c_str());
}
static int frames = 0;
void systemDrawScreen()
{
frames++;
MainFrame* mf = wxGetApp().frame;
mf->UpdateViewers();
// FIXME: Sm60FPS crap and sondBufferLow crap
GameArea* ga = mf->GetPanel();
#ifndef NO_FFMPEG
if (ga)
ga->AddFrame(pix);
#endif
if (ga && ga->panel)
ga->panel->DrawArea(&pix);
}
// record a game "movie"
// actually just game save state combined with a keystroke log
// doesn't work in GB "multiplayer" mode (only records default joypad)
//
// <name>.vmv = keystroke log; all values little-endian ints:
// <version>.32 = 1
// for every joypad change (init to 0) and once at end of movie {
// <timestamp>.32 = frames since start of movie
// <joypad>.32 = default joypad reading at that time
// }
// <name>.vm0 = saved state
wxFFile game_file;
bool game_recording, game_playback;
uint32_t game_frame;
uint32_t game_joypad;
void systemStartGameRecording(const wxString& fname)
{
GameArea* panel = wxGetApp().frame->GetPanel();
if (!panel || panel->game_type() == IMAGE_UNKNOWN || !panel->emusys->emuWriteState) {
wxLogError(_("No game in progress to record"));
return;
}
systemStopGamePlayback();
wxString fn = fname;
if (fn.size() < 4 || !wxString(fn.substr(fn.size() - 4)).IsSameAs(wxT(".vmv"), false))
fn.append(wxT(".vmv"));
uint32_t version = 1;
if (!game_file.Open(fn, wxT("wb")) || game_file.Write(&version, sizeof(version)) != sizeof(version)) {
wxLogError(_("Cannot open output file %s"), fname.c_str());
return;
}
fn[fn.size() - 1] = wxT('0');
if (!panel->emusys->emuWriteState(fn.mb_fn_str())) {
wxLogError(_("Error writing game recording"));
game_file.Close();
return;
}
game_frame = 0;
game_joypad = 0;
game_recording = true;
MainFrame* mf = wxGetApp().frame;
mf->cmd_enable &= ~(CMDEN_NGREC | CMDEN_GPLAY | CMDEN_NGPLAY);
mf->cmd_enable |= CMDEN_GREC;
mf->enable_menus();
}
void systemStopGameRecording()
{
if (!game_recording)
return;
if (game_file.Write(&game_frame, sizeof(game_frame)) != sizeof(game_frame) || game_file.Write(&game_joypad, sizeof(game_joypad)) != sizeof(game_joypad) || !game_file.Close())
wxLogError(_("Error writing game recording"));
game_recording = false;
MainFrame* mf = wxGetApp().frame;
mf->cmd_enable &= ~CMDEN_GREC;
mf->cmd_enable |= CMDEN_NGREC | CMDEN_NGPLAY;
mf->enable_menus();
}
uint32_t game_next_frame, game_next_joypad;
void systemStartGamePlayback(const wxString& fname)
{
GameArea* panel = wxGetApp().frame->GetPanel();
if (!panel || panel->game_type() == IMAGE_UNKNOWN || !panel->emusys->emuReadState) {
wxLogError(_("No game in progress to record"));
return;
}
if (game_recording) {
wxLogError(_("Cannot play game recording while recording"));
return;
}
systemStopGamePlayback();
wxString fn = fname;
if (fn.size() < 4 || !wxString(fn.substr(fn.size() - 4)).IsSameAs(wxT(".vmv"), false))
fn.append(wxT(".vmv"));
uint32_t version;
if (!game_file.Open(fn, wxT("rb")) || game_file.Read(&version, sizeof(version)) != sizeof(version) || wxUINT32_SWAP_ON_BE(version) != 1) {
wxLogError(_("Cannot open recording file %s"), fname.c_str());
return;
}
uint32_t gf, jp;
if (game_file.Read(&gf, sizeof(gf)) != sizeof(gf) || game_file.Read(&jp, sizeof(jp)) != sizeof(jp)) {
wxLogError(_("Error reading game recording"));
game_file.Close();
return;
}
game_next_frame = wxUINT32_SWAP_ON_BE(gf);
game_next_joypad = wxUINT32_SWAP_ON_BE(jp);
fn[fn.size() - 1] = wxT('0');
if (!panel->emusys->emuReadState(fn.mb_fn_str())) {
wxLogError(_("Error reading game recording"));
game_file.Close();
return;
}
game_frame = 0;
game_joypad = 0;
game_playback = true;
MainFrame* mf = wxGetApp().frame;
mf->cmd_enable &= ~(CMDEN_NGREC | CMDEN_GREC | CMDEN_NGPLAY);
mf->cmd_enable |= CMDEN_GPLAY;
mf->enable_menus();
}
void systemStopGamePlayback()
{
if (!game_playback)
return;
game_file.Close();
game_playback = false;
MainFrame* mf = wxGetApp().frame;
mf->cmd_enable &= ~CMDEN_GPLAY;
mf->cmd_enable |= CMDEN_NGREC | CMDEN_NGPLAY;
mf->enable_menus();
}
// updates the joystick data (done in background using wxSDLJoy)
bool systemReadJoypads()
{
return true;
}
// return information about the given joystick, -1 for default joystick
uint32_t systemReadJoypad(int joy)
{
if (joy < 0 || joy > 3)
joy = gopts.default_stick - 1;
uint32_t ret = joypress[joy];
if (turbo)
ret |= KEYM_SPEED;
uint32_t af = autofire;
if (ret & KEYM_AUTO_A) {
ret |= KEYM_A;
af |= KEYM_A;
}
if (ret & KEYM_AUTO_B) {
ret |= KEYM_B;
af |= KEYM_B;
}
uint32_t ah = autohold;
uint32_t ah_but = ah | ret;
if (ah_but)
{
ret ^= ah;
}
static int autofire_trigger = 1;
static bool autofire_state = true;
uint32_t af_but = af & ret;
if (af_but) {
if (!autofire_state)
ret &= ~af_but;
if (!--autofire_trigger) {
autofire_trigger = gopts.autofire_rate;
autofire_state = !autofire_state;
}
} else {
autofire_state = true;
autofire_trigger = gopts.autofire_rate;
}
// disallow opposite directionals simultaneously
ret &= ~((ret & (KEYM_LEFT | KEYM_DOWN | KEYM_MOTION_DOWN | KEYM_MOTION_RIGHT)) >> 1);
ret &= REALKEY_MASK;
if (game_recording) {
uint32_t rret = ret & ~(KEYM_SPEED | KEYM_CAPTURE);
if (rret != game_joypad) {
game_joypad = rret;
uint32_t gf = wxUINT32_SWAP_ON_BE(game_frame);
uint32_t jp = wxUINT32_SWAP_ON_BE(game_joypad);
if (game_file.Write(&gf, sizeof(gf)) != sizeof(gf) || game_file.Write(&jp, sizeof(jp)) != sizeof(jp)) {
game_file.Close();
game_recording = false;
wxLogError(_("Error writing game recording"));
}
}
} else if (game_playback) {
while (game_frame >= game_next_frame) {
game_joypad = game_next_joypad;
uint32_t gf, jp;
if (game_file.Read(&gf, sizeof(gf)) != sizeof(gf) || game_file.Read(&jp, sizeof(jp)) != sizeof(jp)) {
game_file.Close();
game_playback = false;
wxString msg(_("Playback ended"));
systemScreenMessage(msg);
break;
}
game_next_frame = wxUINT32_SWAP_ON_BE(gf);
game_next_joypad = wxUINT32_SWAP_ON_BE(jp);
}
ret = game_joypad;
}
return ret;
}
void systemShowSpeed(int speed)
{
MainFrame* f = wxGetApp().frame;
wxString s;
s.Printf(_("%d%%(%d, %d fps)"), speed, systemFrameSkip, frames * speed / 100);
switch (showSpeed) {
case SS_NONE:
f->GetPanel()->osdstat.clear();
break;
case SS_PERCENT:
f->GetPanel()->osdstat.Printf(_("%d%%"), speed);
break;
case SS_DETAILED:
f->GetPanel()->osdstat = s;
break;
}
wxGetApp().frame->SetStatusText(s, 1);
frames = 0;
}
int systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED;
void system10Frames(int rate)
{
GameArea* panel = wxGetApp().frame->GetPanel();
int fs = frameSkip;
if (fs < 0) {
// I don't know why this algorithm isn't in common somewhere
// as is, I copied it from SDL
static uint32_t prevclock = 0;
static int speedadj = 0;
uint32_t t = systemGetClock();
if (!panel->was_paused && prevclock && (t - prevclock) != (uint32_t)(10000 / rate)) {
int speed = t == prevclock ? 100 * 10000 / rate - (t - prevclock) : 100;
// why 98??
if (speed >= 98)
speedadj++;
else if (speed < 80)
speedadj -= (90 - speed) / 5;
else
speedadj--;
if (speedadj >= 3) {
speedadj = 0;
if (systemFrameSkip > 0)
systemFrameSkip--;
} else if (speedadj <= -2) {
speedadj += 2;
if (systemFrameSkip < 9)
systemFrameSkip++;
}
}
prevclock = t;
panel->was_paused = false;
}
if (gopts.rewind_interval) {
if (!panel->rewind_time)
panel->rewind_time = gopts.rewind_interval * 6;
else if (!--panel->rewind_time)
panel->do_rewind = true;
}
if (--systemSaveUpdateCounter == SYSTEM_SAVE_NOT_UPDATED)
panel->SaveBattery();
else if (systemSaveUpdateCounter < SYSTEM_SAVE_NOT_UPDATED)
systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED;
}
void systemFrame()
{
if (game_recording || game_playback)
game_frame++;
}
// technically, num is ignored in favor of finding the first
// available slot
void systemScreenCapture(int num)
{
GameArea* panel = wxGetApp().frame->GetPanel();
wxFileName fn = wxFileName(wxGetApp().frame->GetGamePath(gopts.scrshot_dir), wxEmptyString);
do {
wxString bfn;
bfn.Printf(wxT("%s%02d"), panel->game_name().c_str(),
num++);
if (captureFormat == 0)
bfn.append(wxT(".png"));
else // if(gopts.cap_format == 1)
bfn.append(wxT(".bmp"));
fn.SetFullName(bfn);
} while (fn.FileExists());
fn.Mkdir(0777, wxPATH_MKDIR_FULL);
if (captureFormat == 0)
panel->emusys->emuWritePNG(fn.GetFullPath().mb_fn_str());
else // if(gopts.cap_format == 1)
panel->emusys->emuWriteBMP(fn.GetFullPath().mb_fn_str());
wxString msg;
msg.Printf(_("Wrote snapshot %s"), fn.GetFullPath().c_str());
systemScreenMessage(msg);
}
void systemSaveOldest()
{
// I need to be implemented
}
void systemLoadRecent()
{
// I need to be implemented
}
uint32_t systemGetClock()
{
return wxGetApp().timer.Time();
}
void systemCartridgeRumble(bool) {}
static uint8_t sensorDarkness = 0xE8; // total darkness (including daylight on rainy days)
uint8_t systemGetSensorDarkness()
{
return sensorDarkness;
}
void systemUpdateSolarSensor()
{
uint8_t sun = 0x0; //sun = 0xE8 - 0xE8 (case 0 and default)
int level = sunBars / 10;
switch (level) {
case 1:
sun = 0xE8 - 0xE0;
break;
case 2:
sun = 0xE8 - 0xDA;
break;
case 3:
sun = 0xE8 - 0xD0;
break;
case 4:
sun = 0xE8 - 0xC8;
break;
case 5:
sun = 0xE8 - 0xC0;
break;
case 6:
sun = 0xE8 - 0xB0;
break;
case 7:
sun = 0xE8 - 0xA0;
break;
case 8:
sun = 0xE8 - 0x88;
break;
case 9:
sun = 0xE8 - 0x70;
break;
case 10:
sun = 0xE8 - 0x50;
break;
default:
break;
}
sensorDarkness = 0xE8 - sun;
}
void systemUpdateMotionSensor()
{
for (int i = 0; i < 4; i++) {
if (!sensorx[i])
sensorx[i] = 2047;
if (!sensory[i])
sensory[i] = 2047;
if (joypress[i] & KEYM_MOTION_LEFT) {
sunBars--;
if (sunBars < 1)
sunBars = 1;
sensorx[i] += 3;
if (sensorx[i] > 2197)
sensorx[i] = 2197;
if (sensorx[i] < 2047)
sensorx[i] = 2057;
} else if (joypress[i] & KEYM_MOTION_RIGHT) {
sunBars++;
if (sunBars > 100)
sunBars = 100;
sensorx[i] -= 3;
if (sensorx[i] < 1897)
sensorx[i] = 1897;
if (sensorx[i] > 2047)
sensorx[i] = 2037;
} else if (sensorx[i] > 2047) {
sensorx[i] -= 2;
if (sensorx[i] < 2047)
sensorx[i] = 2047;
} else {
sensorx[i] += 2;
if (sensorx[i] > 2047)
sensorx[i] = 2047;
}
if (joypress[i] & KEYM_MOTION_UP) {
sensory[i] += 3;
if (sensory[i] > 2197)
sensory[i] = 2197;
if (sensory[i] < 2047)
sensory[i] = 2057;
} else if (joypress[i] & KEYM_MOTION_DOWN) {
sensory[i] -= 3;
if (sensory[i] < 1897)
sensory[i] = 1897;
if (sensory[i] > 2047)
sensory[i] = 2037;
} else if (sensory[i] > 2047) {
sensory[i] -= 2;
if (sensory[i] < 2047)
sensory[i] = 2047;
} else {
sensory[i] += 2;
if (sensory[i] > 2047)
sensory[i] = 2047;
}
const int lowZ = -1800;
const int centerZ = 0;
const int highZ = 1800;
const int accelZ = 3;
if (joypress[i] & KEYM_MOTION_IN) {
sensorz[i] += accelZ;
if (sensorz[i] > highZ)
sensorz[i] = highZ;
if (sensorz[i] < centerZ)
sensorz[i] = centerZ + (accelZ * 300);
} else if (joypress[i] & KEYM_MOTION_OUT) {
sensorz[i] -= accelZ;
if (sensorz[i] < lowZ)
sensorz[i] = lowZ;
if (sensorz[i] > centerZ)
sensorz[i] = centerZ - (accelZ * 300);
} else if (sensorz[i] > centerZ) {
sensorz[i] -= (accelZ * 100);
if (sensorz[i] < centerZ)
sensorz[i] = centerZ;
} else {
sensorz[i] += (accelZ * 100);
if (sensorz[i] > centerZ)
sensorz[i] = centerZ;
}
}
systemUpdateSolarSensor();
}
int systemGetSensorX()
{
return sensorx[gopts.default_stick - 1];
}
int systemGetSensorY()
{
return sensory[gopts.default_stick - 1];
}
int systemGetSensorZ()
{
return sensorz[gopts.default_stick - 1] / 10;
}
class PrintDialog : public wxEvtHandler, public wxPrintout {
public:
PrintDialog(const uint16_t* data, int lines, bool cont);
~PrintDialog();
int ShowModal()
{
dlg->SetWindowStyle(wxCAPTION | wxRESIZE_BORDER);
if (gopts.keep_on_top)
dlg->SetWindowStyle(dlg->GetWindowStyle() | wxSTAY_ON_TOP);
else
dlg->SetWindowStyle(dlg->GetWindowStyle() & ~wxSTAY_ON_TOP);
CheckPointer(wxGetApp().frame);
return wxGetApp().frame->ShowModal(dlg);
}
private:
void DoSave(wxCommandEvent&);
void DoPrint(wxCommandEvent&);
void ChangeMag(wxCommandEvent&);
void ShowImg(wxPaintEvent&);
bool OnPrintPage(int pno);
void OnPreparePrinting();
bool HasPage(int pno) { return pno <= npw * nph; }
void GetPageInfo(int* minp, int* maxp, int* pfrom, int* pto)
{
*minp = 1;
*maxp = npw * nph;
*pfrom = 1;
*pto = 1;
}
wxDialog* dlg;
wxPanel* p;
wxImage img;
wxBitmap* bmp;
wxControlWithItems* mag;
static wxPrintData* printdata;
static wxPageSetupDialogData* pagedata;
wxRect margins;
int npw, nph;
DECLARE_CLASS(PrintDialog)
};
IMPLEMENT_CLASS(PrintDialog, wxEvtHandler)
PrintDialog::PrintDialog(const uint16_t* data, int lines, bool cont):
wxPrintout(wxGetApp().frame->GetPanel()->game_name() + wxT(" Printout")),
img(160, lines),
npw(1),
nph(1)
{
dlg = wxStaticCast(wxGetApp().frame->FindWindow(XRCID("GBPrinter")), wxDialog);
p = XRCCTRL(*dlg, "Preview", wxPanel);
wxScrolledWindow* pp = wxStaticCast(p->GetParent(), wxScrolledWindow);
wxSize sz(320, lines * 2);
p->SetSize(sz);
pp->SetVirtualSize(sz);
if (lines > 144)
sz.SetHeight(144 * 2);
pp->SetSizeHints(sz, sz); // keep sizer from messing with size
pp->SetSize(sz);
pp->SetScrollRate(1, 1);
// why is this so difficult?
// I want the display area to be 320x288, not the area w/ scrollbars
wxSize csz = pp->GetClientSize();
sz += sz - csz;
pp->SetSizeHints(sz, sz); // keep sizer from messing with size
pp->SetSize(sz);
dlg->Fit();
// what a waste. wxImage is brain-dead rgb24
data += 162; // top border
for (int y = 0; y < lines; y++) {
for (int x = 0; x < 160; x++) {
uint16_t d = *data++;
img.SetRGB(x, y, ((d >> 10) & 0x1f) << 3, ((d >> 5) & 0x1f) << 3,
(d & 0x1f) << 3);
}
data += 2; // rhs border
}
bmp = new wxBitmap(img.Scale(320, 2 * lines, wxIMAGE_QUALITY_HIGH));
p->Connect(wxEVT_PAINT, wxPaintEventHandler(PrintDialog::ShowImg),
NULL, this);
dlg->Connect(wxID_SAVE, wxEVT_COMMAND_BUTTON_CLICKED,
wxCommandEventHandler(PrintDialog::DoSave), NULL, this);
dlg->Connect(wxID_PRINT, wxEVT_COMMAND_BUTTON_CLICKED,
wxCommandEventHandler(PrintDialog::DoPrint), NULL, this);
dlg->Connect(XRCID("Magnification"), wxEVT_COMMAND_CHOICE_SELECTED,
wxCommandEventHandler(PrintDialog::ChangeMag), NULL, this);
mag = XRCCTRL(*dlg, "Magnification", wxControlWithItems);
mag->SetSelection(1);
wxWindow* w = dlg->FindWindow(cont ? wxID_OK : wxID_SAVE);
if (w)
w->SetFocus();
wxButton* cb = wxStaticCast(dlg->FindWindow(wxID_CANCEL), wxButton);
if (cb)
cb->SetLabel(_("&Discard"));
}
PrintDialog::~PrintDialog()
{
p->Disconnect(wxID_ANY, wxEVT_PAINT);
dlg->Disconnect(wxID_PRINT);
dlg->Disconnect(wxID_SAVE);
dlg->Disconnect(XRCID("Magnification"));
delete bmp;
}
void PrintDialog::ShowImg(wxPaintEvent& evt)
{
wxPaintDC dc(wxStaticCast(evt.GetEventObject(), wxWindow));
dc.DrawBitmap(*bmp, 0, 0);
}
void PrintDialog::ChangeMag(wxCommandEvent& evt)
{
(void)evt; // unused params
int m = mag->GetSelection() + 1;
wxScrolledWindow* pp = wxStaticCast(p->GetParent(), wxScrolledWindow);
wxSize sz(m * 160, m * img.GetHeight());
delete bmp;
bmp = new wxBitmap(img.Scale(sz.GetWidth(), sz.GetHeight(), wxIMAGE_QUALITY_HIGH));
p->SetSize(sz);
pp->SetVirtualSize(sz);
// pp->Refresh();
}
static wxString prsav_path;
void PrintDialog::DoSave(wxCommandEvent&)
{
wxString pats = _("Image files (*.bmp;*.jpg;*.png)|*.bmp;*.jpg;*.png|");
pats.append(wxALL_FILES);
wxString dn = wxGetApp().frame->GetPanel()->game_name();
if (captureFormat == 0)
dn.append(wxT(".png"));
else // if(gopts.cap_format == 1)
dn.append(wxT(".bmp"));
wxFileDialog fdlg(dlg, _("Save printer image to"), prsav_path, dn,
pats, wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
int ret = fdlg.ShowModal();
prsav_path = fdlg.GetDirectory();
if (ret != wxID_OK)
return;
wxString of = fdlg.GetPath();
int m = mag->GetSelection() + 1;
wxImage scimg = img.Scale(m * 160, m * img.GetHeight(), wxIMAGE_QUALITY_HIGH);
if (scimg.SaveFile(of)) {
wxString msg;
msg.Printf(_("Wrote printer output to %s"), of.c_str());
systemScreenMessage(msg);
wxButton* cb = wxStaticCast(dlg->FindWindow(wxID_CANCEL), wxButton);
if (cb) {
cb->SetLabel(_("&Close"));
cb->SetFocus();
}
}
}
wxPrintData* PrintDialog::printdata = NULL;
wxPageSetupDialogData* PrintDialog::pagedata = NULL;
void PrintDialog::OnPreparePrinting()
{
margins = GetLogicalPageMarginsRect(*pagedata);
// strange... y is always negative
margins.y = -margins.y;
int w = bmp->GetWidth();
int h = bmp->GetHeight();
npw = (w + margins.width - 1) / margins.width;
nph = (h + margins.height - 1) / margins.height;
}
bool PrintDialog::OnPrintPage(int pno)
{
wxDC* dc = GetDC();
dc->SetClippingRegion(margins);
int xoff = (pno - 1) % npw;
int yoff = (pno - 1) / npw;
xoff *= margins.width;
yoff *= margins.height;
dc->DrawBitmap(*bmp, margins.x - xoff, margins.y - yoff);
return true;
}
void PrintDialog::DoPrint(wxCommandEvent&)
{
if (!printdata)
printdata = new wxPrintData;
if (!pagedata)
pagedata = new wxPageSetupDialogData(*printdata);
// wxGTK-2.8.12/gnome-2.18.8: can't set margins in page setup
// wxMAC: need to pop up 2 dialogs to get margins
// why even bother using standard dialogs, then?
// well, maybe under msw, where generic dialog may not even exist...
#ifndef __WXMSW__
wxGenericPageSetupDialog psdlg2(dlg, pagedata);
psdlg2.ShowModal();
*printdata = psdlg2.GetPageSetupDialogData().GetPrintData();
*pagedata = psdlg2.GetPageSetupDialogData();
#else
wxPageSetupDialog psdlg(dlg, pagedata);
psdlg.ShowModal();
*printdata = psdlg.GetPageSetupDialogData().GetPrintData();
*pagedata = psdlg.GetPageSetupDialogData();
#ifdef __WXMAC__
// FIXME: is this necessary? useful? functional?
wxMacPageMarginsDialog pmdlg(dlg, pagedata);
pmdlg.ShowModal();
*printdata = pmdlg.GetPageSetupDialogData().GetPrintData();
*pagedata = pmdlg.GetPageSetupDialogData();
#endif
#endif
wxPrintDialogData prdlg(*printdata);
wxPrinter printer(&prdlg);
if (printer.Print(dlg, this)) {
wxString msg = _("Printed");
systemScreenMessage(msg);
wxButton* cb = wxStaticCast(dlg->FindWindow(wxID_CANCEL), wxButton);
if (cb) {
cb->SetLabel(_("&Close"));
cb->SetFocus();
}
*printdata = printer.GetPrintDialogData().GetPrintData();
}
}
void systemGbPrint(uint8_t* data, int len, int pages, int feed, int pal, int cont)
{
(void)pages; // unused params
(void)cont; // unused params
ModalPause mp; // this might take a while, so signal a pause
GameArea* panel = wxGetApp().frame->GetPanel();
static uint16_t* accum_prdata;
static int accum_prdata_len = 0, accum_prdata_size = 0;
static uint16_t prdata[162 * 145] = { 0 };
int lines = len / 40;
uint16_t* out = prdata + 162; // 1-pix top border
for (int y = 0; y < lines / 8; y++) {
for (int x = 0; x < 160 / 8; x++) {
for (int k = 0; k < 8; k++) {
int a = *data++;
int b = *data++;
for (int j = 0, mask = 0x80; j < 8; j++, mask >>= 1) {
int c = ((a & mask) | ((b & mask) << 1)) >> (7 - j);
// the guide I read said 0 is on top, but if I do that,
// output seems reversed. So either 11 in pal means
// white, or 00 in c means bits 0-1 in pal.
// the guide I read also said 00 is white and 11 is black
// I'm going with "00 in c means bits 0-1 in pal"
if (pal != 0xe4) // 11 10 01 00
c = (pal & (3 << c * 2)) >> c * 2;
out[j + k * 162] = systemGbPalette[c];
}
}
out += 8;
}
out += 2 + 7 * 162; // 2-pix rhs border
}
// assume no bottom margin means "more coming"
// probably ought to make this time out somehow
// or at the very least dump when the game state changes
uint16_t* to_print = prdata;
if ((gopts.print_auto_page && !(feed & 15)) || accum_prdata_len) {
if (!accum_prdata_len)
accum_prdata_len = 162; // top border
accum_prdata_len += lines * 162;
if (accum_prdata_size < accum_prdata_len) {
if (!accum_prdata_size)
accum_prdata = (uint16_t*)calloc(accum_prdata_len, 2);
else
accum_prdata = (uint16_t*)realloc(accum_prdata, accum_prdata_len * 2);
accum_prdata_size = accum_prdata_len;
}
memcpy(accum_prdata + accum_prdata_len - lines * 162, prdata + 162,
lines * 162 * 2);
if (gopts.print_auto_page && !(feed & 15))
return;
to_print = accum_prdata;
lines = accum_prdata_len / 162 - 1;
accum_prdata_len = 0;
}
if (gopts.print_screen_cap) {
wxFileName fn = wxFileName(wxGetApp().frame->GetGamePath(gopts.scrshot_dir), wxEmptyString);
int num = 1;
do {
wxString bfn;
bfn.Printf(wxT("%s-print%02d"), panel->game_name().c_str(),
num++);
if (captureFormat == 0)
bfn.append(wxT(".png"));
else // if(gopts.cap_format == 1)
bfn.append(wxT(".bmp"));
fn.SetFullName(bfn);
} while (fn.FileExists());
fn.Mkdir(0777, wxPATH_MKDIR_FULL);
int d = systemColorDepth;
int rs = systemRedShift, bs = systemBlueShift, gs = systemGreenShift;
systemColorDepth = 16;
systemRedShift = 10;
systemGreenShift = 5;
systemBlueShift = 0;
wxString of = fn.GetFullPath();
bool ret = captureFormat == 0 ? utilWritePNGFile(of.mb_fn_str(), 160, lines, (uint8_t*)to_print) : utilWriteBMPFile(of.mb_fn_str(), 160, lines, (uint8_t*)to_print);
if (ret) {
wxString msg;
msg.Printf(_("Wrote printer output to %s"), of.c_str());
systemScreenMessage(msg);
}