/
surfacesdl-graphics.cpp
2469 lines (2025 loc) · 68.9 KB
/
surfacesdl-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"
#if defined(SDL_BACKEND)
#include "backends/graphics/surfacesdl/surfacesdl-graphics.h"
#include "backends/events/sdl/sdl-events.h"
#include "backends/platform/sdl/sdl.h"
#include "common/config-manager.h"
#include "common/mutex.h"
#include "common/textconsole.h"
#include "common/translation.h"
#include "common/util.h"
#include "common/frac.h"
#ifdef USE_RGB_COLOR
#include "common/list.h"
#endif
#include "graphics/font.h"
#include "graphics/fontman.h"
#include "graphics/scaler.h"
#include "graphics/scaler/aspect.h"
#include "graphics/surface.h"
#include "gui/EventRecorder.h"
static const OSystem::GraphicsMode s_supportedGraphicsModes[] = {
{"1x", _s("Normal (no scaling)"), GFX_NORMAL},
#ifdef USE_SCALERS
{"2x", "2x", GFX_DOUBLESIZE},
{"3x", "3x", GFX_TRIPLESIZE},
{"2xsai", "2xSAI", GFX_2XSAI},
{"super2xsai", "Super2xSAI", GFX_SUPER2XSAI},
{"supereagle", "SuperEagle", GFX_SUPEREAGLE},
{"advmame2x", "AdvMAME2x", GFX_ADVMAME2X},
{"advmame3x", "AdvMAME3x", GFX_ADVMAME3X},
#ifdef USE_HQ_SCALERS
{"hq2x", "HQ2x", GFX_HQ2X},
{"hq3x", "HQ3x", GFX_HQ3X},
#endif
{"tv2x", "TV2x", GFX_TV2X},
{"dotmatrix", "DotMatrix", GFX_DOTMATRIX},
#endif
{0, 0, 0}
};
DECLARE_TRANSLATION_ADDITIONAL_CONTEXT("Normal (no scaling)", "lowres")
// Table of the cursor scalers [scaleFactor - 1]
static ScalerProc *scalersMagn[3] = {
#ifdef USE_SCALERS
Normal1x, AdvMame2x, AdvMame3x
#else // remove dependencies on other scalers
Normal1x, Normal1x, Normal1x
#endif
};
static const int s_gfxModeSwitchTable[][4] = {
{ GFX_NORMAL, GFX_DOUBLESIZE, GFX_TRIPLESIZE, -1 },
{ GFX_NORMAL, GFX_ADVMAME2X, GFX_ADVMAME3X, -1 },
{ GFX_NORMAL, GFX_HQ2X, GFX_HQ3X, -1 },
{ GFX_NORMAL, GFX_2XSAI, -1, -1 },
{ GFX_NORMAL, GFX_SUPER2XSAI, -1, -1 },
{ GFX_NORMAL, GFX_SUPEREAGLE, -1, -1 },
{ GFX_NORMAL, GFX_TV2X, -1, -1 },
{ GFX_NORMAL, GFX_DOTMATRIX, -1, -1 }
};
#ifdef USE_SCALERS
static int cursorStretch200To240(uint8 *buf, uint32 pitch, int width, int height, int srcX, int srcY, int origSrcY);
#endif
AspectRatio::AspectRatio(int w, int h) {
// TODO : Validation and so on...
// Currently, we just ensure the program don't instantiate non-supported aspect ratios
_kw = w;
_kh = h;
}
#if !defined(_WIN32_WCE) && !defined(__SYMBIAN32__) && defined(USE_SCALERS)
static AspectRatio getDesiredAspectRatio() {
const size_t AR_COUNT = 4;
const char *desiredAspectRatioAsStrings[AR_COUNT] = { "auto", "4/3", "16/9", "16/10" };
const AspectRatio desiredAspectRatios[AR_COUNT] = { AspectRatio(0, 0), AspectRatio(4,3), AspectRatio(16,9), AspectRatio(16,10) };
//TODO : We could parse an arbitrary string, if we code enough proper validation
Common::String desiredAspectRatio = ConfMan.get("desired_screen_aspect_ratio");
for (size_t i = 0; i < AR_COUNT; i++) {
assert(desiredAspectRatioAsStrings[i] != NULL);
if (!scumm_stricmp(desiredAspectRatio.c_str(), desiredAspectRatioAsStrings[i])) {
return desiredAspectRatios[i];
}
}
// TODO : Report a warning
return AspectRatio(0, 0);
}
#endif
SurfaceSdlGraphicsManager::SurfaceSdlGraphicsManager(SdlEventSource *sdlEventSource, SdlWindow *window)
:
SdlGraphicsManager(sdlEventSource, window),
#ifdef USE_OSD
_osdSurface(0), _osdAlpha(SDL_ALPHA_TRANSPARENT), _osdFadeStartTime(0),
#endif
_hwscreen(0),
#if SDL_VERSION_ATLEAST(2, 0, 0)
_renderer(nullptr), _screenTexture(nullptr),
_viewport(), _windowWidth(1), _windowHeight(1),
#else
_originalBitsPerPixel(0),
#endif
_screen(0), _tmpscreen(0),
#ifdef USE_RGB_COLOR
_screenFormat(Graphics::PixelFormat::createFormatCLUT8()),
_cursorFormat(Graphics::PixelFormat::createFormatCLUT8()),
#endif
_overlayVisible(false),
_overlayscreen(0), _tmpscreen2(0),
_scalerProc(0), _screenChangeCount(0),
_mouseVisible(false), _mouseNeedsRedraw(false), _mouseData(0), _mouseSurface(0),
_mouseOrigSurface(0), _cursorDontScale(false), _cursorPaletteDisabled(true),
_currentShakePos(0), _newShakePos(0),
_paletteDirtyStart(0), _paletteDirtyEnd(0),
_screenIsLocked(false),
_graphicsMutex(0),
_displayDisabled(false),
#ifdef USE_SDL_DEBUG_FOCUSRECT
_enableFocusRectDebugCode(false), _enableFocusRect(false), _focusRect(),
#endif
_transactionMode(kTransactionNone) {
// allocate palette storage
_currentPalette = (SDL_Color *)calloc(sizeof(SDL_Color), 256);
_cursorPalette = (SDL_Color *)calloc(sizeof(SDL_Color), 256);
_mouseBackup.x = _mouseBackup.y = _mouseBackup.w = _mouseBackup.h = 0;
memset(&_mouseCurState, 0, sizeof(_mouseCurState));
_graphicsMutex = g_system->createMutex();
#ifdef USE_SDL_DEBUG_FOCUSRECT
if (ConfMan.hasKey("use_sdl_debug_focusrect"))
_enableFocusRectDebugCode = ConfMan.getBool("use_sdl_debug_focusrect");
#endif
memset(&_oldVideoMode, 0, sizeof(_oldVideoMode));
memset(&_videoMode, 0, sizeof(_videoMode));
memset(&_transactionDetails, 0, sizeof(_transactionDetails));
#if !defined(_WIN32_WCE) && !defined(__SYMBIAN32__) && defined(USE_SCALERS)
_videoMode.mode = GFX_DOUBLESIZE;
_videoMode.scaleFactor = 2;
_videoMode.aspectRatioCorrection = ConfMan.getBool("aspect_ratio");
_videoMode.desiredAspectRatio = getDesiredAspectRatio();
_scalerProc = Normal2x;
#else // for small screen platforms
_videoMode.mode = GFX_NORMAL;
_videoMode.scaleFactor = 1;
_videoMode.aspectRatioCorrection = false;
_scalerProc = Normal1x;
#endif
_scalerType = 0;
#if !defined(_WIN32_WCE) && !defined(__SYMBIAN32__)
_videoMode.fullscreen = ConfMan.getBool("fullscreen");
#else
_videoMode.fullscreen = true;
#endif
}
SurfaceSdlGraphicsManager::~SurfaceSdlGraphicsManager() {
unloadGFXMode();
if (_mouseSurface)
SDL_FreeSurface(_mouseSurface);
_mouseSurface = 0;
if (_mouseOrigSurface)
SDL_FreeSurface(_mouseOrigSurface);
_mouseOrigSurface = 0;
g_system->deleteMutex(_graphicsMutex);
free(_currentPalette);
free(_cursorPalette);
free(_mouseData);
}
void SurfaceSdlGraphicsManager::activateManager() {
SdlGraphicsManager::activateManager();
// Register the graphics manager as a event observer
g_system->getEventManager()->getEventDispatcher()->registerObserver(this, 10, false);
}
void SurfaceSdlGraphicsManager::deactivateManager() {
// Unregister the event observer
if (g_system->getEventManager()->getEventDispatcher()) {
g_system->getEventManager()->getEventDispatcher()->unregisterObserver(this);
}
SdlGraphicsManager::deactivateManager();
}
bool SurfaceSdlGraphicsManager::hasFeature(OSystem::Feature f) {
return
(f == OSystem::kFeatureFullscreenMode) ||
(f == OSystem::kFeatureAspectRatioCorrection) ||
(f == OSystem::kFeatureCursorPalette) ||
(f == OSystem::kFeatureIconifyWindow);
}
void SurfaceSdlGraphicsManager::setFeatureState(OSystem::Feature f, bool enable) {
switch (f) {
case OSystem::kFeatureFullscreenMode:
setFullscreenMode(enable);
break;
case OSystem::kFeatureAspectRatioCorrection:
setAspectRatioCorrection(enable);
break;
case OSystem::kFeatureCursorPalette:
_cursorPaletteDisabled = !enable;
blitCursor();
break;
case OSystem::kFeatureIconifyWindow:
if (enable)
_window->iconifyWindow();
break;
default:
break;
}
}
bool SurfaceSdlGraphicsManager::getFeatureState(OSystem::Feature f) {
// We need to allow this to be called from within a transaction, since we
// currently use it to retreive the graphics state, when switching from
// SDL->OpenGL mode for example.
//assert(_transactionMode == kTransactionNone);
switch (f) {
case OSystem::kFeatureFullscreenMode:
return _videoMode.fullscreen;
case OSystem::kFeatureAspectRatioCorrection:
return _videoMode.aspectRatioCorrection;
case OSystem::kFeatureCursorPalette:
return !_cursorPaletteDisabled;
default:
return false;
}
}
const OSystem::GraphicsMode *SurfaceSdlGraphicsManager::getSupportedGraphicsModes() const {
return s_supportedGraphicsModes;
}
int SurfaceSdlGraphicsManager::getDefaultGraphicsMode() const {
#ifdef USE_SCALERS
return GFX_DOUBLESIZE;
#else
return GFX_NORMAL;
#endif
}
void SurfaceSdlGraphicsManager::resetGraphicsScale() {
setGraphicsMode(s_gfxModeSwitchTable[_scalerType][0]);
}
void SurfaceSdlGraphicsManager::beginGFXTransaction() {
assert(_transactionMode == kTransactionNone);
_transactionMode = kTransactionActive;
_transactionDetails.sizeChanged = false;
_transactionDetails.needHotswap = false;
_transactionDetails.needUpdatescreen = false;
_transactionDetails.normal1xScaler = false;
#ifdef USE_RGB_COLOR
_transactionDetails.formatChanged = false;
#endif
_oldVideoMode = _videoMode;
}
OSystem::TransactionError SurfaceSdlGraphicsManager::endGFXTransaction() {
int errors = OSystem::kTransactionSuccess;
assert(_transactionMode != kTransactionNone);
if (_transactionMode == kTransactionRollback) {
if (_videoMode.fullscreen != _oldVideoMode.fullscreen) {
errors |= OSystem::kTransactionFullscreenFailed;
_videoMode.fullscreen = _oldVideoMode.fullscreen;
} else if (_videoMode.aspectRatioCorrection != _oldVideoMode.aspectRatioCorrection) {
errors |= OSystem::kTransactionAspectRatioFailed;
_videoMode.aspectRatioCorrection = _oldVideoMode.aspectRatioCorrection;
} else if (_videoMode.mode != _oldVideoMode.mode) {
errors |= OSystem::kTransactionModeSwitchFailed;
_videoMode.mode = _oldVideoMode.mode;
_videoMode.scaleFactor = _oldVideoMode.scaleFactor;
#ifdef USE_RGB_COLOR
} else if (_videoMode.format != _oldVideoMode.format) {
errors |= OSystem::kTransactionFormatNotSupported;
_videoMode.format = _oldVideoMode.format;
_screenFormat = _videoMode.format;
#endif
} else if (_videoMode.screenWidth != _oldVideoMode.screenWidth || _videoMode.screenHeight != _oldVideoMode.screenHeight) {
errors |= OSystem::kTransactionSizeChangeFailed;
_videoMode.screenWidth = _oldVideoMode.screenWidth;
_videoMode.screenHeight = _oldVideoMode.screenHeight;
_videoMode.overlayWidth = _oldVideoMode.overlayWidth;
_videoMode.overlayHeight = _oldVideoMode.overlayHeight;
}
if (_videoMode.fullscreen == _oldVideoMode.fullscreen &&
_videoMode.aspectRatioCorrection == _oldVideoMode.aspectRatioCorrection &&
_videoMode.mode == _oldVideoMode.mode &&
_videoMode.screenWidth == _oldVideoMode.screenWidth &&
_videoMode.screenHeight == _oldVideoMode.screenHeight) {
// Our new video mode would now be exactly the same as the
// old one. Since we still can not assume SDL_SetVideoMode
// to be working fine, we need to invalidate the old video
// mode, so loadGFXMode would error out properly.
_oldVideoMode.setup = false;
}
}
#ifdef USE_RGB_COLOR
if (_transactionDetails.sizeChanged || _transactionDetails.formatChanged) {
#else
if (_transactionDetails.sizeChanged) {
#endif
unloadGFXMode();
if (!loadGFXMode()) {
if (_oldVideoMode.setup) {
_transactionMode = kTransactionRollback;
errors |= endGFXTransaction();
}
} else {
setGraphicsModeIntern();
clearOverlay();
_videoMode.setup = true;
// OSystem_SDL::pollEvent used to update the screen change count,
// but actually it gives problems when a video mode was changed
// but OSystem_SDL::pollEvent was not called. This for example
// caused a crash under certain circumstances when doing an RTL.
// To fix this issue we update the screen change count right here.
_screenChangeCount++;
}
} else if (_transactionDetails.needHotswap) {
setGraphicsModeIntern();
if (!hotswapGFXMode()) {
if (_oldVideoMode.setup) {
_transactionMode = kTransactionRollback;
errors |= endGFXTransaction();
}
} else {
_videoMode.setup = true;
// OSystem_SDL::pollEvent used to update the screen change count,
// but actually it gives problems when a video mode was changed
// but OSystem_SDL::pollEvent was not called. This for example
// caused a crash under certain circumstances when doing an RTL.
// To fix this issue we update the screen change count right here.
_screenChangeCount++;
if (_transactionDetails.needUpdatescreen)
internUpdateScreen();
}
} else if (_transactionDetails.needUpdatescreen) {
setGraphicsModeIntern();
internUpdateScreen();
}
_transactionMode = kTransactionNone;
return (OSystem::TransactionError)errors;
}
#ifdef USE_RGB_COLOR
Common::List<Graphics::PixelFormat> SurfaceSdlGraphicsManager::getSupportedFormats() const {
assert(!_supportedFormats.empty());
return _supportedFormats;
}
void SurfaceSdlGraphicsManager::detectSupportedFormats() {
// Clear old list
_supportedFormats.clear();
// Some tables with standard formats that we always list
// as "supported". If frontend code tries to use one of
// these, we will perform the necessary format
// conversion in the background. Of course this incurs a
// performance hit, but on desktop ports this should not
// matter. We still push the currently active format to
// the front, so if frontend code just uses the first
// available format, it will get one that is "cheap" to
// use.
const Graphics::PixelFormat RGBList[] = {
#ifdef USE_RGB_COLOR
// RGBA8888, ARGB8888, RGB888
Graphics::PixelFormat(4, 8, 8, 8, 8, 24, 16, 8, 0),
Graphics::PixelFormat(4, 8, 8, 8, 8, 16, 8, 0, 24),
Graphics::PixelFormat(3, 8, 8, 8, 0, 16, 8, 0, 0),
#endif
// RGB565, XRGB1555, RGB555, RGBA4444, ARGB4444
Graphics::PixelFormat(2, 5, 6, 5, 0, 11, 5, 0, 0),
Graphics::PixelFormat(2, 5, 5, 5, 1, 10, 5, 0, 15),
Graphics::PixelFormat(2, 5, 5, 5, 0, 10, 5, 0, 0),
Graphics::PixelFormat(2, 4, 4, 4, 4, 12, 8, 4, 0),
Graphics::PixelFormat(2, 4, 4, 4, 4, 8, 4, 0, 12)
};
const Graphics::PixelFormat BGRList[] = {
#ifdef USE_RGB_COLOR
// ABGR8888, BGRA8888, BGR888
Graphics::PixelFormat(4, 8, 8, 8, 8, 0, 8, 16, 24),
Graphics::PixelFormat(4, 8, 8, 8, 8, 8, 16, 24, 0),
Graphics::PixelFormat(3, 8, 8, 8, 0, 0, 8, 16, 0),
#endif
// BGR565, XBGR1555, BGR555, ABGR4444, BGRA4444
Graphics::PixelFormat(2, 5, 6, 5, 0, 0, 5, 11, 0),
Graphics::PixelFormat(2, 5, 5, 5, 1, 0, 5, 10, 15),
Graphics::PixelFormat(2, 5, 5, 5, 0, 0, 5, 10, 0),
Graphics::PixelFormat(2, 4, 4, 4, 4, 0, 4, 8, 12),
Graphics::PixelFormat(2, 4, 4, 4, 4, 4, 8, 12, 0)
};
Graphics::PixelFormat format = Graphics::PixelFormat::createFormatCLUT8();
if (_hwscreen) {
// Get our currently set hardware format
format = Graphics::PixelFormat(_hwscreen->format->BytesPerPixel,
8 - _hwscreen->format->Rloss, 8 - _hwscreen->format->Gloss,
8 - _hwscreen->format->Bloss, 8 - _hwscreen->format->Aloss,
_hwscreen->format->Rshift, _hwscreen->format->Gshift,
_hwscreen->format->Bshift, _hwscreen->format->Ashift);
// Workaround to SDL not providing an accurate Aloss value on Mac OS X.
if (_hwscreen->format->Amask == 0)
format.aLoss = 8;
// Push it first, as the prefered format.
_supportedFormats.push_back(format);
}
// TODO: prioritize matching alpha masks
int i;
// Push some RGB formats
for (i = 0; i < ARRAYSIZE(RGBList); i++) {
if (_hwscreen && (RGBList[i].bytesPerPixel > format.bytesPerPixel))
continue;
if (RGBList[i] != format)
_supportedFormats.push_back(RGBList[i]);
}
// Push some BGR formats
for (i = 0; i < ARRAYSIZE(BGRList); i++) {
if (_hwscreen && (BGRList[i].bytesPerPixel > format.bytesPerPixel))
continue;
if (BGRList[i] != format)
_supportedFormats.push_back(BGRList[i]);
}
// Finally, we always supposed 8 bit palette graphics
_supportedFormats.push_back(Graphics::PixelFormat::createFormatCLUT8());
}
#endif
bool SurfaceSdlGraphicsManager::setGraphicsMode(int mode) {
Common::StackLock lock(_graphicsMutex);
assert(_transactionMode == kTransactionActive);
if (_oldVideoMode.setup && _oldVideoMode.mode == mode)
return true;
int newScaleFactor = 1;
switch (mode) {
case GFX_NORMAL:
newScaleFactor = 1;
break;
#ifdef USE_SCALERS
case GFX_DOUBLESIZE:
newScaleFactor = 2;
break;
case GFX_TRIPLESIZE:
newScaleFactor = 3;
break;
case GFX_2XSAI:
newScaleFactor = 2;
break;
case GFX_SUPER2XSAI:
newScaleFactor = 2;
break;
case GFX_SUPEREAGLE:
newScaleFactor = 2;
break;
case GFX_ADVMAME2X:
newScaleFactor = 2;
break;
case GFX_ADVMAME3X:
newScaleFactor = 3;
break;
#ifdef USE_HQ_SCALERS
case GFX_HQ2X:
newScaleFactor = 2;
break;
case GFX_HQ3X:
newScaleFactor = 3;
break;
#endif
case GFX_TV2X:
newScaleFactor = 2;
break;
case GFX_DOTMATRIX:
newScaleFactor = 2;
break;
#endif // USE_SCALERS
default:
warning("unknown gfx mode %d", mode);
return false;
}
_transactionDetails.normal1xScaler = (mode == GFX_NORMAL);
if (_oldVideoMode.setup && _oldVideoMode.scaleFactor != newScaleFactor)
_transactionDetails.needHotswap = true;
_transactionDetails.needUpdatescreen = true;
_videoMode.mode = mode;
_videoMode.scaleFactor = newScaleFactor;
return true;
}
void SurfaceSdlGraphicsManager::setGraphicsModeIntern() {
Common::StackLock lock(_graphicsMutex);
ScalerProc *newScalerProc = 0;
switch (_videoMode.mode) {
case GFX_NORMAL:
newScalerProc = Normal1x;
break;
#ifdef USE_SCALERS
case GFX_DOUBLESIZE:
newScalerProc = Normal2x;
break;
case GFX_TRIPLESIZE:
newScalerProc = Normal3x;
break;
case GFX_2XSAI:
newScalerProc = _2xSaI;
break;
case GFX_SUPER2XSAI:
newScalerProc = Super2xSaI;
break;
case GFX_SUPEREAGLE:
newScalerProc = SuperEagle;
break;
case GFX_ADVMAME2X:
newScalerProc = AdvMame2x;
break;
case GFX_ADVMAME3X:
newScalerProc = AdvMame3x;
break;
#ifdef USE_HQ_SCALERS
case GFX_HQ2X:
newScalerProc = HQ2x;
break;
case GFX_HQ3X:
newScalerProc = HQ3x;
break;
#endif
case GFX_TV2X:
newScalerProc = TV2x;
break;
case GFX_DOTMATRIX:
newScalerProc = DotMatrix;
break;
#endif // USE_SCALERS
default:
error("Unknown gfx mode %d", _videoMode.mode);
}
_scalerProc = newScalerProc;
if (_videoMode.mode != GFX_NORMAL) {
for (int i = 0; i < ARRAYSIZE(s_gfxModeSwitchTable); i++) {
if (s_gfxModeSwitchTable[i][1] == _videoMode.mode || s_gfxModeSwitchTable[i][2] == _videoMode.mode) {
_scalerType = i;
break;
}
}
}
if (!_screen || !_hwscreen)
return;
// Blit everything to the screen
_forceFull = true;
// Even if the old and new scale factors are the same, we may have a
// different scaler for the cursor now.
blitCursor();
}
int SurfaceSdlGraphicsManager::getGraphicsMode() const {
assert(_transactionMode == kTransactionNone);
return _videoMode.mode;
}
void SurfaceSdlGraphicsManager::initSize(uint w, uint h, const Graphics::PixelFormat *format) {
assert(_transactionMode == kTransactionActive);
#ifdef USE_RGB_COLOR
//avoid redundant format changes
Graphics::PixelFormat newFormat;
if (!format)
newFormat = Graphics::PixelFormat::createFormatCLUT8();
else
newFormat = *format;
assert(newFormat.bytesPerPixel > 0);
if (newFormat != _videoMode.format) {
_videoMode.format = newFormat;
_transactionDetails.formatChanged = true;
_screenFormat = newFormat;
}
#endif
// Avoid redundant res changes
if ((int)w == _videoMode.screenWidth && (int)h == _videoMode.screenHeight)
return;
_videoMode.screenWidth = w;
_videoMode.screenHeight = h;
_transactionDetails.sizeChanged = true;
}
int SurfaceSdlGraphicsManager::effectiveScreenHeight() const {
return _videoMode.scaleFactor *
(_videoMode.aspectRatioCorrection
? real2Aspect(_videoMode.screenHeight)
: _videoMode.screenHeight);
}
static void fixupResolutionForAspectRatio(AspectRatio desiredAspectRatio, int &width, int &height) {
assert(&width != &height);
if (desiredAspectRatio.isAuto())
return;
int kw = desiredAspectRatio.kw();
int kh = desiredAspectRatio.kh();
const int w = width;
const int h = height;
int bestW = 0, bestH = 0;
uint bestMetric = (uint)-1; // Metric is wasted space
#if SDL_VERSION_ATLEAST(2, 0, 0)
const int numModes = SDL_GetNumDisplayModes(0);
SDL_DisplayMode modeData, *mode = &modeData;
for (int i = 0; i < numModes; ++i) {
if (SDL_GetDisplayMode(0, i, &modeData)) {
continue;
}
#else
SDL_Rect const* const*availableModes = SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_SWSURFACE); //TODO : Maybe specify a pixel format
assert(availableModes);
while (const SDL_Rect *mode = *availableModes++) {
#endif
if (mode->w < w)
continue;
if (mode->h < h)
continue;
if (mode->h * kw != mode->w * kh)
continue;
uint metric = mode->w * mode->h - w * h;
if (metric > bestMetric)
continue;
bestMetric = metric;
bestW = mode->w;
bestH = mode->h;
// Make editors a bit more happy by having the same amount of closing as
// opening curley braces.
#if SDL_VERSION_ATLEAST(2, 0, 0)
}
#else
}
#endif
if (!bestW || !bestH) {
warning("Unable to enforce the desired aspect ratio");
return;
}
width = bestW;
height = bestH;
}
bool SurfaceSdlGraphicsManager::loadGFXMode() {
_forceFull = true;
#if !defined(__MAEMO__) && !defined(DINGUX) && !defined(GPH_DEVICE) && !defined(LINUXMOTO) && !defined(OPENPANDORA)
_videoMode.overlayWidth = _videoMode.screenWidth * _videoMode.scaleFactor;
_videoMode.overlayHeight = _videoMode.screenHeight * _videoMode.scaleFactor;
if (_videoMode.screenHeight != 200 && _videoMode.screenHeight != 400)
_videoMode.aspectRatioCorrection = false;
if (_videoMode.aspectRatioCorrection)
_videoMode.overlayHeight = real2Aspect(_videoMode.overlayHeight);
_videoMode.hardwareWidth = _videoMode.screenWidth * _videoMode.scaleFactor;
_videoMode.hardwareHeight = effectiveScreenHeight();
// On GPH devices ALL the _videoMode.hardware... are setup in GPHGraphicsManager::loadGFXMode()
#elif !defined(GPH_DEVICE)
_videoMode.hardwareWidth = _videoMode.overlayWidth;
_videoMode.hardwareHeight = _videoMode.overlayHeight;
#endif
//
// Create the surface that contains the 8 bit game data
//
#ifdef USE_RGB_COLOR
_screen = SDL_CreateRGBSurface(SDL_SWSURFACE, _videoMode.screenWidth, _videoMode.screenHeight,
_screenFormat.bytesPerPixel << 3,
((1 << _screenFormat.rBits()) - 1) << _screenFormat.rShift ,
((1 << _screenFormat.gBits()) - 1) << _screenFormat.gShift ,
((1 << _screenFormat.bBits()) - 1) << _screenFormat.bShift ,
((1 << _screenFormat.aBits()) - 1) << _screenFormat.aShift );
if (_screen == NULL)
error("allocating _screen failed");
// Avoid having SDL_SRCALPHA set even if we supplied an alpha-channel in the format.
SDL_SetAlpha(_screen, 0, 255);
#else
_screen = SDL_CreateRGBSurface(SDL_SWSURFACE, _videoMode.screenWidth, _videoMode.screenHeight, 8, 0, 0, 0, 0);
if (_screen == NULL)
error("allocating _screen failed");
#endif
// SDL 1.2 palettes default to all black,
// SDL 1.3 palettes default to all white,
// Thus set our own default palette to all black.
// SDL_SetColors does nothing for non indexed surfaces.
SDL_SetColors(_screen, _currentPalette, 0, 256);
//
// Create the surface that contains the scaled graphics in 16 bit mode
//
if (_videoMode.fullscreen) {
fixupResolutionForAspectRatio(_videoMode.desiredAspectRatio, _videoMode.hardwareWidth, _videoMode.hardwareHeight);
}
#ifdef ENABLE_EVENTRECORDER
_displayDisabled = ConfMan.getBool("disable_display");
if (_displayDisabled) {
_hwscreen = g_eventRec.getSurface(_videoMode.hardwareWidth, _videoMode.hardwareHeight);
} else
#endif
{
// Save the original bpp to be able to restore the video mode on unload
#if !SDL_VERSION_ATLEAST(2, 0, 0)
if (_originalBitsPerPixel == 0) {
const SDL_VideoInfo *videoInfo = SDL_GetVideoInfo();
_originalBitsPerPixel = videoInfo->vfmt->BitsPerPixel;
}
#endif
_hwscreen = SDL_SetVideoMode(_videoMode.hardwareWidth, _videoMode.hardwareHeight, 16,
_videoMode.fullscreen ? (SDL_FULLSCREEN|SDL_SWSURFACE) : SDL_SWSURFACE
);
}
#ifdef USE_RGB_COLOR
detectSupportedFormats();
#endif
if (_hwscreen == NULL) {
// DON'T use error(), as this tries to bring up the debug
// console, which WON'T WORK now that _hwscreen is hosed.
if (!_oldVideoMode.setup) {
warning("SDL_SetVideoMode says we can't switch to that mode (%s)", SDL_GetError());
g_system->quit();
} else {
return false;
}
}
//
// Create the surface used for the graphics in 16 bit before scaling, and also the overlay
//
// 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("allocating _tmpscreen failed");
_overlayscreen = SDL_CreateRGBSurface(SDL_SWSURFACE, _videoMode.overlayWidth, _videoMode.overlayHeight,
16,
_hwscreen->format->Rmask,
_hwscreen->format->Gmask,
_hwscreen->format->Bmask,
_hwscreen->format->Amask);
if (_overlayscreen == NULL)
error("allocating _overlayscreen failed");
_overlayFormat.bytesPerPixel = _overlayscreen->format->BytesPerPixel;
_overlayFormat.rLoss = _overlayscreen->format->Rloss;
_overlayFormat.gLoss = _overlayscreen->format->Gloss;
_overlayFormat.bLoss = _overlayscreen->format->Bloss;
_overlayFormat.aLoss = _overlayscreen->format->Aloss;
_overlayFormat.rShift = _overlayscreen->format->Rshift;
_overlayFormat.gShift = _overlayscreen->format->Gshift;
_overlayFormat.bShift = _overlayscreen->format->Bshift;
_overlayFormat.aShift = _overlayscreen->format->Ashift;
_tmpscreen2 = SDL_CreateRGBSurface(SDL_SWSURFACE, _videoMode.overlayWidth + 3, _videoMode.overlayHeight + 3,
16,
_hwscreen->format->Rmask,
_hwscreen->format->Gmask,
_hwscreen->format->Bmask,
_hwscreen->format->Amask);
if (_tmpscreen2 == NULL)
error("allocating _tmpscreen2 failed");
#ifdef USE_OSD
_osdSurface = SDL_CreateRGBSurface(SDL_SWSURFACE | SDL_RLEACCEL | SDL_SRCCOLORKEY | SDL_SRCALPHA,
_hwscreen->w,
_hwscreen->h,
16,
_hwscreen->format->Rmask,
_hwscreen->format->Gmask,
_hwscreen->format->Bmask,
_hwscreen->format->Amask);
if (_osdSurface == NULL)
error("allocating _osdSurface failed");
SDL_SetColorKey(_osdSurface, SDL_RLEACCEL | SDL_SRCCOLORKEY | SDL_SRCALPHA, kOSDColorKey);
#endif
#if !SDL_VERSION_ATLEAST(2, 0, 0)
// For SDL2 the output resolution might differ from the requested
// resolution. We handle resetting the keyboard emulation properly inside
// our SDL_SetVideoMode wrapper for SDL2.
_eventSource->resetKeyboadEmulation(
_videoMode.screenWidth * _videoMode.scaleFactor - 1,
effectiveScreenHeight() - 1);
#endif
// Distinguish 555 and 565 mode
if (_hwscreen->format->Rmask == 0x7C00)
InitScalers(555);
else
InitScalers(565);
return true;
}
void SurfaceSdlGraphicsManager::unloadGFXMode() {
if (_screen) {
SDL_FreeSurface(_screen);
_screen = NULL;
}
#if SDL_VERSION_ATLEAST(2, 0, 0)
deinitializeRenderer();
#endif
if (_hwscreen) {
SDL_FreeSurface(_hwscreen);
_hwscreen = NULL;
}
if (_tmpscreen) {
SDL_FreeSurface(_tmpscreen);
_tmpscreen = NULL;
}
if (_tmpscreen2) {
SDL_FreeSurface(_tmpscreen2);
_tmpscreen2 = NULL;
}
if (_overlayscreen) {
SDL_FreeSurface(_overlayscreen);
_overlayscreen = NULL;
}
#ifdef USE_OSD
if (_osdSurface) {
SDL_FreeSurface(_osdSurface);
_osdSurface = NULL;
}
#endif
DestroyScalers();
#if !SDL_VERSION_ATLEAST(2, 0, 0)
// Reset video mode to original
// This will ensure that any new graphic manager will use the initial BPP when listing available modes
if (_originalBitsPerPixel != 0)
SDL_SetVideoMode(_videoMode.screenWidth, _videoMode.screenHeight, _originalBitsPerPixel, _videoMode.fullscreen ? (SDL_FULLSCREEN | SDL_SWSURFACE) : SDL_SWSURFACE);
#endif
}
bool SurfaceSdlGraphicsManager::hotswapGFXMode() {
if (!_screen)
return false;
// Keep around the old _screen & _overlayscreen so we can restore the screen data
// after the mode switch.
SDL_Surface *old_screen = _screen;
_screen = NULL;
SDL_Surface *old_overlayscreen = _overlayscreen;
_overlayscreen = NULL;
// Release the HW screen surface
SDL_FreeSurface(_hwscreen); _hwscreen = NULL;
SDL_FreeSurface(_tmpscreen); _tmpscreen = NULL;
SDL_FreeSurface(_tmpscreen2); _tmpscreen2 = NULL;
#ifdef USE_OSD
// Release the OSD surface
SDL_FreeSurface(_osdSurface); _osdSurface = NULL;
#endif
// 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_overlayscreen, NULL, _overlayscreen, NULL);
// Free the old surfaces
SDL_FreeSurface(old_screen);
SDL_FreeSurface(old_overlayscreen);