forked from scummvm/scummvm
/
wincesdl-graphics.cpp
1659 lines (1388 loc) · 47 KB
/
wincesdl-graphics.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
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "common/scummsys.h"
#ifdef _WIN32_WCE
#include "common/system.h"
#include "common/textconsole.h"
#include "common/translation.h"
#include "common/mutex.h"
#include "graphics/scaler/downscaler.h"
#include "graphics/scaler/aspect.h"
#include "backends/graphics/wincesdl/wincesdl-graphics.h"
#include "backends/events/wincesdl/wincesdl-events.h"
#include "backends/platform/wince/wince-sdl.h"
#include "backends/platform/wince/resource.h"
#include "backends/platform/wince/CEActionsPocket.h"
#include "backends/platform/wince/CEActionsSmartphone.h"
#include "backends/platform/wince/CEDevice.h"
#include "backends/platform/wince/CEScaler.h"
#include "backends/platform/wince/CEgui/ItemAction.h"
WINCESdlGraphicsManager::WINCESdlGraphicsManager(SdlEventSource *sdlEventSource)
: SurfaceSdlGraphicsManager(sdlEventSource),
_panelInitialized(false), _noDoubleTapRMB(false), _noDoubleTapPT(false),
_toolbarHighDrawn(false), _newOrientation(0), _orientationLandscape(0),
_panelVisible(true), _saveActiveToolbar(NAME_MAIN_PANEL), _panelStateForced(false),
_canBeAspectScaled(false), _scalersChanged(false), _saveToolbarState(false),
_mouseBackupOld(NULL), _mouseBackupDim(0), _mouseBackupToolbar(NULL),
_usesEmulatedMouse(false), _forceHideMouse(false), _freeLook(false),
_hasfocus(true), _zoomUp(false), _zoomDown(false) {
memset(&_mouseCurState, 0, sizeof(_mouseCurState));
if (_isSmartphone) {
_mouseCurState.x = 20;
_mouseCurState.y = 20;
}
loadDeviceConfigurationElement("repeatTrigger", _keyRepeatTrigger, 200);
loadDeviceConfigurationElement("repeatX", _repeatX, 4);
loadDeviceConfigurationElement("repeatY", _repeatY, 4);
loadDeviceConfigurationElement("stepX1", _stepX1, 2);
loadDeviceConfigurationElement("stepX2", _stepX2, 10);
loadDeviceConfigurationElement("stepX3", _stepX3, 40);
loadDeviceConfigurationElement("stepY1", _stepY1, 2);
loadDeviceConfigurationElement("stepY2", _stepY2, 10);
loadDeviceConfigurationElement("stepY3", _stepY3, 20);
ConfMan.flushToDisk();
_isSmartphone = CEDevice::isSmartphone();
// Query SDL for screen size and init screen dependent stuff
OSystem_WINCE3::initScreenInfos();
create_toolbar();
_hasSmartphoneResolution = CEDevice::hasSmartphoneResolution() || CEDevice::isSmartphone();
if (_hasSmartphoneResolution)
_panelVisible = false; // init correctly in smartphones
_screen = NULL;
}
// Graphics mode consts
// Low end devices 240x320
static const OSystem::GraphicsMode s_supportedGraphicsModesLow[] = {
{"1x", _s("Normal (no scaling)"), GFX_NORMAL},
{0, 0, 0}
};
// High end device 480x640
static const OSystem::GraphicsMode s_supportedGraphicsModesHigh[] = {
{"1x", _s("Normal (no scaling)"), GFX_NORMAL},
{"2x", "2x", GFX_DOUBLESIZE},
#ifndef _MSC_VER // EVC breaks template functions, and I'm tired of fixing them :)
{"2xsai", "2xSAI", GFX_2XSAI},
{"super2xsai", "Super2xSAI", GFX_SUPER2XSAI},
{"supereagle", "SuperEagle", GFX_SUPEREAGLE},
#endif
{"advmame2x", "AdvMAME2x", GFX_ADVMAME2X},
#ifndef _MSC_VER
{"hq2x", "HQ2x", GFX_HQ2X},
{"tv2x", "TV2x", GFX_TV2X},
#endif
{"dotmatrix", "DotMatrix", GFX_DOTMATRIX},
{0, 0, 0}
};
const OSystem::GraphicsMode *WINCESdlGraphicsManager::getSupportedGraphicsModes() const {
if (CEDevice::hasWideResolution())
return s_supportedGraphicsModesHigh;
else
return s_supportedGraphicsModesLow;
}
bool WINCESdlGraphicsManager::hasFeature(OSystem::Feature f) {
return (f == OSystem::kFeatureVirtualKeyboard);
}
void WINCESdlGraphicsManager::setFeatureState(OSystem::Feature f, bool enable) {
switch (f) {
case OSystem::kFeatureFullscreenMode:
return;
case OSystem::kFeatureVirtualKeyboard:
if (_hasSmartphoneResolution)
return;
_toolbarHighDrawn = false;
if (enable) {
_panelStateForced = true;
if (!_toolbarHandler.visible()) swap_panel_visibility();
//_saveToolbarState = _toolbarHandler.visible();
_saveActiveToolbar = _toolbarHandler.activeName();
_toolbarHandler.setActive(NAME_PANEL_KEYBOARD);
_toolbarHandler.setVisible(true);
} else if (_panelStateForced) {
_panelStateForced = false;
_toolbarHandler.setActive(_saveActiveToolbar);
//_toolbarHandler.setVisible(_saveToolbarState);
}
return;
case OSystem::kFeatureDisableKeyFiltering:
if (_hasSmartphoneResolution) {
GUI::Actions::Instance()->beginMapping(enable);
}
return;
default:
SurfaceSdlGraphicsManager::setFeatureState(f, enable);
}
}
bool WINCESdlGraphicsManager::getFeatureState(OSystem::Feature f) {
switch (f) {
case OSystem::kFeatureFullscreenMode:
return false;
case OSystem::kFeatureVirtualKeyboard:
return (_panelStateForced);
default:
return SurfaceSdlGraphicsManager::getFeatureState(f);
}
}
int WINCESdlGraphicsManager::getDefaultGraphicsMode() const {
return GFX_NORMAL;
}
void WINCESdlGraphicsManager::initSize(uint w, uint h, const Graphics::PixelFormat *format) {
if (_hasSmartphoneResolution && h == 240)
h = 200; // mainly for the launcher
if (_isSmartphone && !ConfMan.hasKey("landscape")) {
ConfMan.setInt("landscape", 1);
ConfMan.flushToDisk();
}
_canBeAspectScaled = false;
if (w == 320 && h == 200 && !_hasSmartphoneResolution) {
_canBeAspectScaled = true;
h = 240; // use the extra 40 pixels height for the toolbar
}
if (h == 400) // touche engine fixup
h += 80;
if (!_hasSmartphoneResolution) {
if (h == 240)
_toolbarHandler.setOffset(200);
else
_toolbarHandler.setOffset(400);
} else {
if (h == 240)
_toolbarHandler.setOffset(200);
else // 176x220
_toolbarHandler.setOffset(0);
}
if (w != (uint) _videoMode.screenWidth || h != (uint) _videoMode.screenHeight)
_scalersChanged = false;
_videoMode.overlayWidth = w;
_videoMode.overlayHeight = h;
SurfaceSdlGraphicsManager::initSize(w, h, format);
if (_scalersChanged) {
unloadGFXMode();
loadGFXMode();
_scalersChanged = false;
}
update_game_settings();
}
void WINCESdlGraphicsManager::loadDeviceConfigurationElement(Common::String element, int &value, int defaultValue) {
value = ConfMan.getInt(element, ConfMan.kApplicationDomain);
if (!value) {
value = defaultValue;
ConfMan.setInt(element, value, ConfMan.kApplicationDomain);
}
}
void WINCESdlGraphicsManager::move_cursor_up() {
int x, y;
_usesEmulatedMouse = true;
retrieve_mouse_location(x, y);
if (_keyRepeat > _repeatY)
y -= _stepY3;
else if (_keyRepeat)
y -= _stepY2;
else
y -= _stepY1;
if (y < 0)
y = 0;
EventsBuffer::simulateMouseMove(x, y);
}
void WINCESdlGraphicsManager::move_cursor_down() {
int x, y;
_usesEmulatedMouse = true;
retrieve_mouse_location(x, y);
if (_keyRepeat > _repeatY)
y += _stepY3;
else if (_keyRepeat)
y += _stepY2;
else
y += _stepY1;
if (y > _videoMode.screenHeight * _scaleFactorYm / _scaleFactorYd)
y = _videoMode.screenHeight * _scaleFactorYm / _scaleFactorYd;
EventsBuffer::simulateMouseMove(x, y);
}
void WINCESdlGraphicsManager::move_cursor_left() {
int x, y;
_usesEmulatedMouse = true;
retrieve_mouse_location(x, y);
if (_keyRepeat > _repeatX)
x -= _stepX3;
else if (_keyRepeat)
x -= _stepX2;
else
x -= _stepX1;
if (x < 0)
x = 0;
EventsBuffer::simulateMouseMove(x, y);
}
void WINCESdlGraphicsManager::move_cursor_right() {
int x, y;
_usesEmulatedMouse = true;
retrieve_mouse_location(x, y);
if (_keyRepeat > _repeatX)
x += _stepX3;
else if (_keyRepeat)
x += _stepX2;
else
x += _stepX1;
if (x > _videoMode.screenWidth * _scaleFactorXm / _scaleFactorXd)
x = _videoMode.screenWidth * _scaleFactorXm / _scaleFactorXd;
EventsBuffer::simulateMouseMove(x, y);
}
void WINCESdlGraphicsManager::retrieve_mouse_location(int &x, int &y) {
x = _mouseCurState.x;
y = _mouseCurState.y;
x = x * _scaleFactorXm / _scaleFactorXd;
y = y * _scaleFactorYm / _scaleFactorYd;
if (_zoomDown)
y -= 240;
}
void WINCESdlGraphicsManager::switch_zone() {
int x, y;
int i;
retrieve_mouse_location(x, y);
for (i = 0; i < TOTAL_ZONES; i++)
if (x >= _zones[i].x && y >= _zones[i].y &&
x <= _zones[i].x + _zones[i].width && y <= _zones[i].y + _zones[i].height) {
_mouseXZone[i] = x;
_mouseYZone[i] = y;
break;
}
_currentZone = i + 1;
if (_currentZone >= TOTAL_ZONES)
_currentZone = 0;
EventsBuffer::simulateMouseMove(_mouseXZone[_currentZone], _mouseYZone[_currentZone]);
}
void WINCESdlGraphicsManager::add_right_click(bool pushed) {
int x, y;
retrieve_mouse_location(x, y);
EventsBuffer::simulateMouseRightClick(x, y, pushed);
}
void WINCESdlGraphicsManager::add_left_click(bool pushed) {
int x, y;
retrieve_mouse_location(x, y);
EventsBuffer::simulateMouseLeftClick(x, y, pushed);
}
bool WINCESdlGraphicsManager::update_scalers() {
_videoMode.aspectRatioCorrection = false;
if (CEDevice::hasPocketPCResolution()) {
if (_videoMode.mode != GFX_NORMAL)
return false;
if ((!_orientationLandscape && (_videoMode.screenWidth == 320 || !_videoMode.screenWidth))
|| CEDevice::hasSquareQVGAResolution()) {
if (OSystem_WINCE3::getScreenWidth() != 320) {
_scaleFactorXm = 3;
_scaleFactorXd = 4;
_scaleFactorYm = 1;
_scaleFactorYd = 1;
_scalerProc = DownscaleHorizByThreeQuarters;
} else {
_scaleFactorXm = 1;
_scaleFactorXd = 1;
_scaleFactorYm = 1;
_scaleFactorYd = 1;
_scalerProc = Normal1x;
}
} else if (_orientationLandscape && (_videoMode.screenWidth == 320 || !_videoMode.screenWidth)) {
if (!_panelVisible && !_hasSmartphoneResolution && !_overlayVisible && _canBeAspectScaled) {
_scaleFactorXm = 1;
_scaleFactorXd = 1;
_scaleFactorYm = 6;
_scaleFactorYd = 5;
_scalerProc = Normal1xAspect;
_videoMode.aspectRatioCorrection = true;
} else {
_scaleFactorXm = 1;
_scaleFactorXd = 1;
_scaleFactorYm = 1;
_scaleFactorYd = 1;
_scalerProc = Normal1x;
}
} else if (_videoMode.screenWidth == 640 && !(OSystem_WINCE3::isOzone() && (OSystem_WINCE3::getScreenWidth() >= 640 || OSystem_WINCE3::getScreenHeight() >= 640))) {
_scaleFactorXm = 1;
_scaleFactorXd = 2;
_scaleFactorYm = 1;
_scaleFactorYd = 2;
_scalerProc = DownscaleAllByHalf;
} else if (_videoMode.screenWidth == 640 && (OSystem_WINCE3::isOzone() && (OSystem_WINCE3::getScreenWidth() >= 640 || OSystem_WINCE3::getScreenHeight() >= 640))) {
_scaleFactorXm = 1;
_scaleFactorXd = 1;
_scaleFactorYm = 1;
_scaleFactorYd = 1;
_scalerProc = Normal1x;
}
return true;
} else if (CEDevice::hasWideResolution()) {
#ifdef USE_ARM_SCALER_ASM
if (_videoMode.mode == GFX_DOUBLESIZE && (_videoMode.screenWidth == 320 || !_videoMode.screenWidth)) {
if (!_panelVisible && !_overlayVisible && _canBeAspectScaled) {
_scaleFactorXm = 2;
_scaleFactorXd = 1;
_scaleFactorYm = 12;
_scaleFactorYd = 5;
_scalerProc = Normal2xAspect;
_videoMode.aspectRatioCorrection = true;
} else if ((_panelVisible || _overlayVisible) && _canBeAspectScaled) {
_scaleFactorXm = 2;
_scaleFactorXd = 1;
_scaleFactorYm = 2;
_scaleFactorYd = 1;
_scalerProc = Normal2x;
}
return true;
}
#endif
} else if (CEDevice::hasSmartphoneResolution()) {
if (_videoMode.mode != GFX_NORMAL)
return false;
if (_videoMode.screenWidth > 320)
error("Game resolution not supported on Smartphone");
#ifdef ARM
_scaleFactorXm = 11;
_scaleFactorXd = 16;
#else
_scaleFactorXm = 2;
_scaleFactorXd = 3;
#endif
_scaleFactorYm = 7;
_scaleFactorYd = 8;
_scalerProc = SmartphoneLandscape;
initZones();
return true;
}
return false;
}
void WINCESdlGraphicsManager::update_game_settings() {
Common::String gameid(ConfMan.get("gameid"));
// Finish panel initialization
if (!_panelInitialized && !gameid.empty()) {
CEGUI::Panel *panel;
_panelInitialized = true;
// Add the main panel
panel = new CEGUI::Panel(0, 32);
panel->setBackground(IMAGE_PANEL);
// Save
panel->add(NAME_ITEM_OPTIONS, new CEGUI::ItemAction(ITEM_OPTIONS, POCKET_ACTION_SAVE));
// Skip
panel->add(NAME_ITEM_SKIP, new CEGUI::ItemAction(ITEM_SKIP, POCKET_ACTION_SKIP));
// sound
panel->add(NAME_ITEM_SOUND, new CEGUI::ItemSwitch(ITEM_SOUND_OFF, ITEM_SOUND_ON, &OSystem_WINCE3::_soundMaster));
// bind keys
panel->add(NAME_ITEM_BINDKEYS, new CEGUI::ItemAction(ITEM_BINDKEYS, POCKET_ACTION_BINDKEYS));
// portrait/landscape - screen dependent
// FIXME : will still display the portrait/landscape icon when using a scaler (but will be disabled)
if (ConfMan.hasKey("landscape")) {
if (ConfMan.get("landscape")[0] > 57) {
_newOrientation = _orientationLandscape = ConfMan.getBool("landscape");
//ConfMan.removeKey("landscape", "");
ConfMan.setInt("landscape", _orientationLandscape);
} else
_newOrientation = _orientationLandscape = ConfMan.getInt("landscape");
} else {
_newOrientation = _orientationLandscape = 0;
}
panel->add(NAME_ITEM_ORIENTATION, new CEGUI::ItemSwitch(ITEM_VIEW_LANDSCAPE, ITEM_VIEW_PORTRAIT, &_newOrientation, 2));
_toolbarHandler.add(NAME_MAIN_PANEL, *panel);
_toolbarHandler.setActive(NAME_MAIN_PANEL);
_toolbarHandler.setVisible(true);
if (_videoMode.mode == GFX_NORMAL && ConfMan.hasKey("landscape") && ConfMan.getInt("landscape")) {
setGraphicsMode(GFX_NORMAL);
hotswapGFXMode();
}
if (_hasSmartphoneResolution)
panel->setVisible(false);
_saveToolbarState = true;
}
if (ConfMan.hasKey("no_doubletap_rightclick"))
_noDoubleTapRMB = ConfMan.getBool("no_doubletap_rightclick");
if (ConfMan.hasKey("no_doubletap_paneltoggle"))
_noDoubleTapPT = ConfMan.getBool("no_doubletap_paneltoggle");
}
void WINCESdlGraphicsManager::internUpdateScreen() {
SDL_Surface *srcSurf, *origSurf;
static bool old_overlayVisible = false;
int numRectsOut = 0;
int16 routx, routy, routw, routh, stretch, shakestretch;
assert(_hwscreen != NULL);
// bail if the application is minimized, be nice to OS
if (!_hasfocus) {
Sleep(20);
return;
}
// If the shake position changed, fill the dirty area with blackness
if (_currentShakePos != _newShakePos) {
SDL_Rect blackrect = {0, 0, _videoMode.screenWidth *_scaleFactorXm / _scaleFactorXd, _newShakePos *_scaleFactorYm / _scaleFactorYd};
if (_videoMode.aspectRatioCorrection)
blackrect.h = real2Aspect(blackrect.h - 1) + 1;
SDL_FillRect(_hwscreen, &blackrect, 0);
_currentShakePos = _newShakePos;
_forceFull = true;
}
// Make sure the mouse is drawn, if it should be drawn.
drawMouse();
// Check whether the palette was changed in the meantime and update the
// screen surface accordingly.
if (_paletteDirtyEnd != 0) {
SDL_SetColors(_screen, _currentPalette + _paletteDirtyStart, _paletteDirtyStart, _paletteDirtyEnd - _paletteDirtyStart);
_paletteDirtyEnd = 0;
_forceFull = true;
}
if (!_overlayVisible) {
origSurf = _screen;
srcSurf = _tmpscreen;
} else {
origSurf = _overlayscreen;
srcSurf = _tmpscreen2;
}
if (old_overlayVisible != _overlayVisible) {
old_overlayVisible = _overlayVisible;
update_scalers();
}
// Force a full redraw if requested
if (_forceFull) {
_numDirtyRects = 1;
_dirtyRectList[0].x = 0;
if (!_zoomDown)
_dirtyRectList[0].y = 0;
else
_dirtyRectList[0].y = _videoMode.screenHeight / 2;
_dirtyRectList[0].w = _videoMode.screenWidth;
if (!_zoomUp && !_zoomDown)
_dirtyRectList[0].h = _videoMode.screenHeight;
else
_dirtyRectList[0].h = _videoMode.screenHeight / 2;
_toolbarHandler.forceRedraw();
}
// Only draw anything if necessary
if (_numDirtyRects > 0) {
SDL_Rect *r, *rout;
SDL_Rect dst;
uint32 srcPitch, dstPitch;
SDL_Rect *last_rect = _dirtyRectList + _numDirtyRects;
bool toolbarVisible = _toolbarHandler.visible();
int toolbarOffset = _toolbarHandler.getOffset();
for (r = _dirtyRectList; r != last_rect; ++r) {
dst = *r;
dst.x++; // Shift rect by one since 2xSai needs to access the data around
dst.y++; // any pixel to scale it, and we want to avoid mem access crashes.
// NOTE: This is also known as BLACK MAGIC, copied from the sdl backend
if (SDL_BlitSurface(origSurf, r, srcSurf, &dst) != 0)
error("SDL_BlitSurface failed: %s", SDL_GetError());
}
SDL_LockSurface(srcSurf);
SDL_LockSurface(_hwscreen);
srcPitch = srcSurf->pitch;
dstPitch = _hwscreen->pitch;
for (r = _dirtyRectList, rout = _dirtyRectOut; r != last_rect; ++r) {
// always clamp to enclosing, downsampled-grid-aligned rect in the downscaled image
if (_scaleFactorXd != 1) {
stretch = r->x % _scaleFactorXd;
r->x -= stretch;
r->w += stretch;
r->w = (r->x + r->w + _scaleFactorXd - 1) / _scaleFactorXd * _scaleFactorXd - r->x;
}
if (_scaleFactorYd != 1) {
stretch = r->y % _scaleFactorYd;
r->y -= stretch;
r->h += stretch;
r->h = (r->y + r->h + _scaleFactorYd - 1) / _scaleFactorYd * _scaleFactorYd - r->y;
}
// transform
shakestretch = _currentShakePos * _scaleFactorYm / _scaleFactorYd;
routx = r->x * _scaleFactorXm / _scaleFactorXd; // locate position in scaled screen
routy = r->y * _scaleFactorYm / _scaleFactorYd + shakestretch; // adjust for shake offset
routw = r->w * _scaleFactorXm / _scaleFactorXd;
routh = r->h * _scaleFactorYm / _scaleFactorYd - shakestretch;
// clipping destination rectangle inside device screen (more strict, also more tricky but more stable)
// note that all current scalers do not make dst rect exceed left/right, unless chosen badly (FIXME)
if (_zoomDown) routy -= 240; // adjust for zoom position
if (routy + routh < 0) continue;
if (routy < 0) {
routh += routy;
r->y -= routy * _scaleFactorYd / _scaleFactorYm;
routy = 0;
r->h = routh * _scaleFactorYd / _scaleFactorYm;
}
if (_orientationLandscape) {
if (routy > OSystem_WINCE3::getScreenWidth()) continue;
if (routy + routh > OSystem_WINCE3::getScreenWidth()) {
routh = OSystem_WINCE3::getScreenWidth() - routy;
r->h = routh * _scaleFactorYd / _scaleFactorYm;
}
} else {
if (routy > OSystem_WINCE3::getScreenHeight()) continue;
if (routy + routh > OSystem_WINCE3::getScreenHeight()) {
routh = OSystem_WINCE3::getScreenHeight() - routy;
r->h = routh * _scaleFactorYd / _scaleFactorYm;
}
}
// check if the toolbar is overwritten
if (toolbarVisible && r->y + r->h >= toolbarOffset)
_toolbarHandler.forceRedraw();
// blit it (with added voodoo from the sdl backend, shifting the source rect again)
_scalerProc((byte *)srcSurf->pixels + (r->x * 2 + 2) + (r->y + 1) * srcPitch, srcPitch,
(byte *)_hwscreen->pixels + routx * 2 + routy * dstPitch, dstPitch,
r->w, r->h - _currentShakePos);
// add this rect to output
rout->x = routx;
rout->y = routy - shakestretch;
rout->w = routw;
rout->h = routh + shakestretch;
numRectsOut++;
rout++;
}
SDL_UnlockSurface(srcSurf);
SDL_UnlockSurface(_hwscreen);
}
// Add the toolbar if needed
SDL_Rect toolbar_rect[1];
if (_panelVisible && _toolbarHandler.draw(_toolbarLow, &toolbar_rect[0])) {
// It can be drawn, scale it
uint32 srcPitch, dstPitch;
SDL_Surface *toolbarSurface;
ScalerProc *toolbarScaler;
if (_videoMode.screenHeight > 240) {
if (!_toolbarHighDrawn) {
// Resize the toolbar
SDL_LockSurface(_toolbarLow);
SDL_LockSurface(_toolbarHigh);
Normal2x((byte *)_toolbarLow->pixels, _toolbarLow->pitch, (byte *)_toolbarHigh->pixels, _toolbarHigh->pitch, toolbar_rect[0].w, toolbar_rect[0].h);
SDL_UnlockSurface(_toolbarHigh);
SDL_UnlockSurface(_toolbarLow);
_toolbarHighDrawn = true;
}
toolbar_rect[0].w *= 2;
toolbar_rect[0].h *= 2;
toolbarSurface = _toolbarHigh;
} else
toolbarSurface = _toolbarLow;
drawToolbarMouse(toolbarSurface, true); // draw toolbar mouse if applicable
// Apply the appropriate scaler
SDL_LockSurface(toolbarSurface);
SDL_LockSurface(_hwscreen);
srcPitch = toolbarSurface->pitch;
dstPitch = _hwscreen->pitch;
toolbarScaler = _scalerProc;
if (_videoMode.scaleFactor == 2)
toolbarScaler = Normal2x;
else if (_videoMode.scaleFactor == 3)
toolbarScaler = Normal3x;
toolbarScaler((byte *)toolbarSurface->pixels, srcPitch,
(byte *)_hwscreen->pixels + (_toolbarHandler.getOffset() * _scaleFactorYm / _scaleFactorYd * dstPitch),
dstPitch, toolbar_rect[0].w, toolbar_rect[0].h);
SDL_UnlockSurface(toolbarSurface);
SDL_UnlockSurface(_hwscreen);
// And blit it
toolbar_rect[0].y = _toolbarHandler.getOffset();
toolbar_rect[0].x = toolbar_rect[0].x * _scaleFactorXm / _scaleFactorXd;
toolbar_rect[0].y = toolbar_rect[0].y * _scaleFactorYm / _scaleFactorYd;
toolbar_rect[0].w = toolbar_rect[0].w * _scaleFactorXm / _scaleFactorXd;
toolbar_rect[0].h = toolbar_rect[0].h * _scaleFactorYm / _scaleFactorYd;
SDL_UpdateRects(_hwscreen, 1, toolbar_rect);
drawToolbarMouse(toolbarSurface, false); // undraw toolbar mouse
}
// Finally, blit all our changes to the screen
if (numRectsOut > 0)
SDL_UpdateRects(_hwscreen, numRectsOut, _dirtyRectOut);
_numDirtyRects = 0;
_forceFull = false;
}
bool WINCESdlGraphicsManager::setGraphicsMode(int mode) {
Common::StackLock lock(_graphicsMutex);
int oldScaleFactorXm = _scaleFactorXm;
int oldScaleFactorXd = _scaleFactorXd;
int oldScaleFactorYm = _scaleFactorYm;
int oldScaleFactorYd = _scaleFactorYd;
_scaleFactorXm = -1;
_scaleFactorXd = -1;
_scaleFactorYm = -1;
_scaleFactorYd = -1;
if (ConfMan.hasKey("landscape"))
if (ConfMan.get("landscape")[0] > 57) {
_newOrientation = _orientationLandscape = ConfMan.getBool("landscape");
ConfMan.setInt("landscape", _orientationLandscape);
} else
_newOrientation = _orientationLandscape = ConfMan.getInt("landscape");
else
_newOrientation = _orientationLandscape = 0;
if (OSystem_WINCE3::isOzone() && (OSystem_WINCE3::getScreenWidth() >= 640 || OSystem_WINCE3::getScreenHeight() >= 640) && mode)
_scaleFactorXm = -1;
if (CEDevice::hasPocketPCResolution() && !CEDevice::hasWideResolution() && _orientationLandscape)
_videoMode.mode = GFX_NORMAL;
else
_videoMode.mode = mode;
if (_scaleFactorXm < 0) {
/* Standard scalers, from the SDL backend */
switch (_videoMode.mode) {
case GFX_NORMAL:
_videoMode.scaleFactor = 1;
_scalerProc = Normal1x;
break;
case GFX_DOUBLESIZE:
_videoMode.scaleFactor = 2;
_scalerProc = Normal2x;
break;
case GFX_TRIPLESIZE:
_videoMode.scaleFactor = 3;
_scalerProc = Normal3x;
break;
case GFX_2XSAI:
_videoMode.scaleFactor = 2;
_scalerProc = _2xSaI;
break;
case GFX_SUPER2XSAI:
_videoMode.scaleFactor = 2;
_scalerProc = Super2xSaI;
break;
case GFX_SUPEREAGLE:
_videoMode.scaleFactor = 2;
_scalerProc = SuperEagle;
break;
case GFX_ADVMAME2X:
_videoMode.scaleFactor = 2;
_scalerProc = AdvMame2x;
break;
case GFX_ADVMAME3X:
_videoMode.scaleFactor = 3;
_scalerProc = AdvMame3x;
break;
#ifdef USE_HQ_SCALERS
case GFX_HQ2X:
_videoMode.scaleFactor = 2;
_scalerProc = HQ2x;
break;
case GFX_HQ3X:
_videoMode.scaleFactor = 3;
_scalerProc = HQ3x;
break;
#endif
case GFX_TV2X:
_videoMode.scaleFactor = 2;
_scalerProc = TV2x;
break;
case GFX_DOTMATRIX:
_videoMode.scaleFactor = 2;
_scalerProc = DotMatrix;
break;
default:
error("unknown gfx mode %d", mode);
}
}
// Check if the scaler can be accepted, if not get back to normal scaler
if (_videoMode.scaleFactor && ((_videoMode.scaleFactor * _videoMode.screenWidth > OSystem_WINCE3::getScreenWidth() && _videoMode.scaleFactor * _videoMode.screenWidth > OSystem_WINCE3::getScreenHeight())
|| (_videoMode.scaleFactor * _videoMode.screenHeight > OSystem_WINCE3::getScreenWidth() && _videoMode.scaleFactor * _videoMode.screenHeight > OSystem_WINCE3::getScreenHeight()))) {
_videoMode.scaleFactor = 1;
_scalerProc = Normal1x;
}
// Common scaler system was used
if (_scaleFactorXm < 0) {
_scaleFactorXm = _videoMode.scaleFactor;
_scaleFactorXd = 1;
_scaleFactorYm = _videoMode.scaleFactor;
_scaleFactorYd = 1;
}
_forceFull = true;
if (oldScaleFactorXm != _scaleFactorXm ||
oldScaleFactorXd != _scaleFactorXd ||
oldScaleFactorYm != _scaleFactorYm ||
oldScaleFactorYd != _scaleFactorYd) {
_scalersChanged = true;
} else
_scalersChanged = false;
return true;
}
bool WINCESdlGraphicsManager::loadGFXMode() {
int displayWidth;
int displayHeight;
unsigned int flags = SDL_FULLSCREEN | SDL_SWSURFACE;
_videoMode.fullscreen = true; // forced
_forceFull = true;
_tmpscreen = NULL;
// Recompute scalers if necessary
update_scalers();
// Create the surface that contains the 8 bit game data
_screen = SDL_CreateRGBSurface(SDL_SWSURFACE, _videoMode.screenWidth, _videoMode.screenHeight, 8, 0, 0, 0, 0);
if (_screen == NULL)
error("_screen failed (%s)", SDL_GetError());
// Create the surface that contains the scaled graphics in 16 bit mode
// Always use full screen mode to have a "clean screen"
if (!_videoMode.aspectRatioCorrection) {
displayWidth = _videoMode.screenWidth * _scaleFactorXm / _scaleFactorXd;
displayHeight = _videoMode.screenHeight * _scaleFactorYm / _scaleFactorYd;
} else {
displayWidth = _videoMode.screenWidth * _videoMode.scaleFactor;
displayHeight = _videoMode.screenHeight * _videoMode.scaleFactor;
}
switch (_orientationLandscape) {
case 1:
flags |= SDL_LANDSCVIDEO;
break;
case 2:
flags |= SDL_INVLNDVIDEO;
break;
default:
flags |= SDL_PORTRTVIDEO;
}
_hwscreen = SDL_SetVideoMode(displayWidth, displayHeight, 16, flags);
if (_hwscreen == NULL) {
warning("SDL_SetVideoMode says we can't switch to that mode (%s)", SDL_GetError());
g_system->quit();
}
// see what orientation sdl finally accepted
if (_hwscreen->flags & SDL_PORTRTVIDEO)
_orientationLandscape = _newOrientation = 0;
else if (_hwscreen->flags & SDL_LANDSCVIDEO)
_orientationLandscape = _newOrientation = 1;
else
_orientationLandscape = _newOrientation = 2;
// Create the surface used for the graphics in 16 bit before scaling, and also the overlay
// Distinguish 555 and 565 mode
if (_hwscreen->format->Rmask == 0x7C00)
InitScalers(555);
else
InitScalers(565);
_overlayFormat.bytesPerPixel = _hwscreen->format->BytesPerPixel;
_overlayFormat.rLoss = _hwscreen->format->Rloss;
_overlayFormat.gLoss = _hwscreen->format->Gloss;
_overlayFormat.bLoss = _hwscreen->format->Bloss;
_overlayFormat.aLoss = _hwscreen->format->Aloss;
_overlayFormat.rShift = _hwscreen->format->Rshift;
_overlayFormat.gShift = _hwscreen->format->Gshift;
_overlayFormat.bShift = _hwscreen->format->Bshift;
_overlayFormat.aShift = _hwscreen->format->Ashift;
// Need some extra bytes around when using 2xSaI
_tmpscreen = SDL_CreateRGBSurface(SDL_SWSURFACE, _videoMode.screenWidth + 3, _videoMode.screenHeight + 3, 16, _hwscreen->format->Rmask, _hwscreen->format->Gmask, _hwscreen->format->Bmask, _hwscreen->format->Amask);
if (_tmpscreen == NULL)
error("_tmpscreen creation failed (%s)", SDL_GetError());
// Overlay
if (CEDevice::hasDesktopResolution()) {
_overlayscreen = SDL_CreateRGBSurface(SDL_SWSURFACE, _videoMode.overlayWidth * _scaleFactorXm / _scaleFactorXd, _videoMode.overlayHeight * _scaleFactorYm / _scaleFactorYd, 16, 0, 0, 0, 0);
if (_overlayscreen == NULL)
error("_overlayscreen failed (%s)", SDL_GetError());
_tmpscreen2 = SDL_CreateRGBSurface(SDL_SWSURFACE, _videoMode.overlayWidth * _scaleFactorXm / _scaleFactorXd + 3, _videoMode.overlayHeight * _scaleFactorYm / _scaleFactorYd + 3, 16, 0, 0, 0, 0);
if (_tmpscreen2 == NULL)
error("_tmpscreen2 failed (%s)", SDL_GetError());
} else {
_overlayscreen = SDL_CreateRGBSurface(SDL_SWSURFACE, _videoMode.overlayWidth, _videoMode.overlayHeight, 16, 0, 0, 0, 0);
if (_overlayscreen == NULL)
error("_overlayscreen failed (%s)", SDL_GetError());
_tmpscreen2 = SDL_CreateRGBSurface(SDL_SWSURFACE, _videoMode.overlayWidth + 3, _videoMode.overlayHeight + 3, 16, 0, 0, 0, 0);
if (_tmpscreen2 == NULL)
error("_tmpscreen2 failed (%s)", SDL_GetError());
}
// Toolbar
_toolbarHighDrawn = false;
uint16 *toolbar_screen = (uint16 *)calloc(320 * 40, sizeof(uint16)); // *not* leaking memory here
_toolbarLow = SDL_CreateRGBSurfaceFrom(toolbar_screen, 320, 40, 16, 320 * 2, _hwscreen->format->Rmask, _hwscreen->format->Gmask, _hwscreen->format->Bmask, _hwscreen->format->Amask);
if (_toolbarLow == NULL)
error("_toolbarLow failed (%s)", SDL_GetError());
if (_videoMode.screenHeight > 240) {
uint16 *toolbar_screen_high = (uint16 *)calloc(640 * 80, sizeof(uint16));
_toolbarHigh = SDL_CreateRGBSurfaceFrom(toolbar_screen_high, 640, 80, 16, 640 * 2, _hwscreen->format->Rmask, _hwscreen->format->Gmask, _hwscreen->format->Bmask, _hwscreen->format->Amask);
if (_toolbarHigh == NULL)
error("_toolbarHigh failed (%s)", SDL_GetError());
} else
_toolbarHigh = NULL;
// keyboard cursor control, some other better place for it?
_sdlEventSource->resetKeyboadEmulation(_videoMode.screenWidth * _scaleFactorXm / _scaleFactorXd - 1, _videoMode.screenHeight * _scaleFactorXm / _scaleFactorXd - 1);
return true;
}
void WINCESdlGraphicsManager::unloadGFXMode() {
if (_screen) {
SDL_FreeSurface(_screen);
_screen = NULL;
}
if (_hwscreen) {
SDL_FreeSurface(_hwscreen);
_hwscreen = NULL;
}
if (_tmpscreen) {
SDL_FreeSurface(_tmpscreen);
_tmpscreen = NULL;
}
}
bool WINCESdlGraphicsManager::hotswapGFXMode() {
if (!_screen)
return false;
// Keep around the old _screen & _tmpscreen so we can restore the screen data
// after the mode switch. (also for the overlay)
SDL_Surface *old_screen = _screen;
SDL_Surface *old_tmpscreen = _tmpscreen;
SDL_Surface *old_overlayscreen = _overlayscreen;
SDL_Surface *old_tmpscreen2 = _tmpscreen2;
// Release the HW screen surface
SDL_FreeSurface(_hwscreen);
// Release toolbars
free(_toolbarLow->pixels);
SDL_FreeSurface(_toolbarLow);
if (_toolbarHigh) {
free(_toolbarHigh->pixels);
SDL_FreeSurface(_toolbarHigh);
}
// Setup the new GFX mode
if (!loadGFXMode()) {
unloadGFXMode();
_screen = old_screen;
_overlayscreen = old_overlayscreen;
return false;
}
// reset palette
SDL_SetColors(_screen, _currentPalette, 0, 256);
// Restore old screen content
SDL_BlitSurface(old_screen, NULL, _screen, NULL);
SDL_BlitSurface(old_tmpscreen, NULL, _tmpscreen, NULL);
if (_overlayVisible) {
SDL_BlitSurface(old_overlayscreen, NULL, _overlayscreen, NULL);
SDL_BlitSurface(old_tmpscreen2, NULL, _tmpscreen2, NULL);
}