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
|
/* 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 "backends/graphics/openglsdl/openglsdl-graphics.h"
#include "backends/events/sdl/sdl-events.h"
#include "backends/platform/sdl/sdl.h"
#include "common/textconsole.h"
#include "common/config-manager.h"
#ifdef USE_OSD
#include "common/translation.h"
#endif
OpenGLSdlGraphicsManager::OpenGLSdlGraphicsManager(uint desktopWidth, uint desktopHeight, SdlEventSource *eventSource, SdlWindow *window)
: SdlGraphicsManager(eventSource, window), _lastRequestedHeight(0),
#if SDL_VERSION_ATLEAST(2, 0, 0)
_glContext(),
#else
_lastVideoModeLoad(0), _hwScreen(nullptr),
#endif
_graphicsScale(2), _ignoreLoadVideoMode(false), _gotResize(false), _wantsFullScreen(false), _ignoreResizeEvents(0),
_desiredFullscreenWidth(0), _desiredFullscreenHeight(0) {
// Setup OpenGL attributes for SDL
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
// Setup proper SDL OpenGL context creation.
#if SDL_VERSION_ATLEAST(2, 0, 0)
OpenGL::ContextType glContextType;
// Context version 1.4 is choosen arbitrarily based on what most shader
// extensions were written against.
#define DEFAULT_GL_MAJOR 1
#define DEFAULT_GL_MINOR 4
#define DEFAULT_GLES_MAJOR 1
#define DEFAULT_GLES_MINOR 1
#define DEFAULT_GLES2_MAJOR 2
#define DEFAULT_GLES2_MINOR 0
#if USE_FORCED_GL
glContextType = OpenGL::kContextGL;
_glContextProfileMask = 0;
_glContextMajor = DEFAULT_GL_MAJOR;
_glContextMinor = DEFAULT_GL_MINOR;
#elif USE_FORCED_GLES
glContextType = OpenGL::kContextGLES;
_glContextProfileMask = SDL_GL_CONTEXT_PROFILE_ES;
_glContextMajor = DEFAULT_GLES_MAJOR;
_glContextMinor = DEFAULT_GLES_MINOR;
#elif USE_FORCED_GLES2
glContextType = OpenGL::kContextGLES2;
_glContextProfileMask = SDL_GL_CONTEXT_PROFILE_ES;
_glContextMajor = DEFAULT_GLES2_MAJOR;
_glContextMinor = DEFAULT_GLES2_MINOR;
#else
bool noDefaults = false;
// Obtain the default GL(ES) context SDL2 tries to setup.
//
// Please note this might not actually be SDL2's defaults when multiple
// instances of this object have been created. But that is no issue
// because then we already set up what we want to use.
//
// In case no defaults are given we prefer OpenGL over OpenGL ES.
if (SDL_GL_GetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, &_glContextProfileMask) != 0) {
_glContextProfileMask = 0;
noDefaults = true;
}
if (SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &_glContextMajor) != 0) {
noDefaults = true;
}
if (SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &_glContextMinor) != 0) {
noDefaults = true;
}
if (noDefaults) {
if (_glContextProfileMask == SDL_GL_CONTEXT_PROFILE_ES) {
_glContextMajor = DEFAULT_GLES_MAJOR;
_glContextMinor = DEFAULT_GLES_MINOR;
} else {
_glContextProfileMask = 0;
_glContextMajor = DEFAULT_GL_MAJOR;
_glContextMinor = DEFAULT_GL_MINOR;
}
}
if (_glContextProfileMask == SDL_GL_CONTEXT_PROFILE_ES) {
if (_glContextMajor >= 2) {
glContextType = OpenGL::kContextGLES2;
} else {
glContextType = OpenGL::kContextGLES;
}
} else if (_glContextProfileMask == SDL_GL_CONTEXT_PROFILE_CORE) {
glContextType = OpenGL::kContextGL;
// Core profile does not allow legacy functionality, which we use.
// Thus we request a standard OpenGL context.
_glContextProfileMask = 0;
_glContextMajor = DEFAULT_GL_MAJOR;
_glContextMinor = DEFAULT_GL_MINOR;
} else {
glContextType = OpenGL::kContextGL;
}
#undef DEFAULT_GL_MAJOR
#undef DEFAULT_GL_MINOR
#undef DEFAULT_GLES_MAJOR
#undef DEFAULT_GLES_MINOR
#undef DEFAULT_GLES2_MAJOR
#undef DEFAULT_GLES2_MINOR
#endif
setContextType(glContextType);
#else
setContextType(OpenGL::kContextGL);
#endif
// Retrieve a list of working fullscreen modes
#if SDL_VERSION_ATLEAST(2, 0, 0)
const int numModes = SDL_GetNumDisplayModes(0);
for (int i = 0; i < numModes; ++i) {
SDL_DisplayMode mode;
if (SDL_GetDisplayMode(0, i, &mode)) {
continue;
}
_fullscreenVideoModes.push_back(VideoMode(mode.w, mode.h));
}
#else
const SDL_Rect *const *availableModes = SDL_ListModes(NULL, SDL_OPENGL | SDL_FULLSCREEN);
// TODO: NULL means that there are no fullscreen modes supported. We
// should probably use this information and disable any fullscreen support
// in this case.
if (availableModes != NULL && availableModes != (void *)-1) {
for (;*availableModes; ++availableModes) {
const SDL_Rect *mode = *availableModes;
_fullscreenVideoModes.push_back(VideoMode(mode->w, mode->h));
}
}
#endif
// Sort the modes in ascending order.
Common::sort(_fullscreenVideoModes.begin(), _fullscreenVideoModes.end());
// Strip duplicates in video modes.
for (uint i = 0; i + 1 < _fullscreenVideoModes.size();) {
if (_fullscreenVideoModes[i] == _fullscreenVideoModes[i + 1]) {
_fullscreenVideoModes.remove_at(i);
} else {
++i;
}
}
// In case SDL is fine with every mode we will force the desktop mode.
// TODO? We could also try to add some default resolutions here.
if (_fullscreenVideoModes.empty() && desktopWidth && desktopHeight) {
_fullscreenVideoModes.push_back(VideoMode(desktopWidth, desktopHeight));
}
// Get information about display sizes from the previous runs.
if (ConfMan.hasKey("last_fullscreen_mode_width", Common::ConfigManager::kApplicationDomain) && ConfMan.hasKey("last_fullscreen_mode_height", Common::ConfigManager::kApplicationDomain)) {
_desiredFullscreenWidth = ConfMan.getInt("last_fullscreen_mode_width", Common::ConfigManager::kApplicationDomain);
_desiredFullscreenHeight = ConfMan.getInt("last_fullscreen_mode_height", Common::ConfigManager::kApplicationDomain);
} else {
// Use the desktop resolutions when no previous default has been setup.
_desiredFullscreenWidth = desktopWidth;
_desiredFullscreenHeight = desktopHeight;
}
}
OpenGLSdlGraphicsManager::~OpenGLSdlGraphicsManager() {
#if SDL_VERSION_ATLEAST(2, 0, 0)
notifyContextDestroy();
SDL_GL_DeleteContext(_glContext);
#endif
}
void OpenGLSdlGraphicsManager::activateManager() {
SdlGraphicsManager::activateManager();
// Register the graphics manager as a event observer
g_system->getEventManager()->getEventDispatcher()->registerObserver(this, 10, false);
}
void OpenGLSdlGraphicsManager::deactivateManager() {
// Unregister the event observer
if (g_system->getEventManager()->getEventDispatcher()) {
g_system->getEventManager()->getEventDispatcher()->unregisterObserver(this);
}
SdlGraphicsManager::deactivateManager();
}
bool OpenGLSdlGraphicsManager::hasFeature(OSystem::Feature f) {
switch (f) {
case OSystem::kFeatureFullscreenMode:
case OSystem::kFeatureIconifyWindow:
return true;
default:
return OpenGLGraphicsManager::hasFeature(f);
}
}
void OpenGLSdlGraphicsManager::setFeatureState(OSystem::Feature f, bool enable) {
switch (f) {
case OSystem::kFeatureFullscreenMode:
assert(getTransactionMode() != kTransactionNone);
_wantsFullScreen = enable;
break;
case OSystem::kFeatureIconifyWindow:
if (enable) {
_window->iconifyWindow();
}
break;
default:
OpenGLGraphicsManager::setFeatureState(f, enable);
}
}
bool OpenGLSdlGraphicsManager::getFeatureState(OSystem::Feature f) {
switch (f) {
case OSystem::kFeatureFullscreenMode:
#if SDL_VERSION_ATLEAST(2, 0, 0)
if (_window) {
return (SDL_GetWindowFlags(_window->getSDLWindow()) & SDL_WINDOW_FULLSCREEN) != 0;
} else {
return _wantsFullScreen;
}
#else
if (_hwScreen) {
return (_hwScreen->flags & SDL_FULLSCREEN) != 0;
} else {
return _wantsFullScreen;
}
#endif
default:
return OpenGLGraphicsManager::getFeatureState(f);
}
}
bool OpenGLSdlGraphicsManager::setGraphicsMode(int mode) {
// HACK: This is stupid but the SurfaceSDL backend defaults to 2x. This
// assures that the launcher (which requests 320x200) has a reasonable
// size. It also makes small games have a reasonable size (i.e. at least
// 640x400). We follow the same logic here until we have a better way to
// give hints to our backend for that.
_graphicsScale = 2;
return OpenGLGraphicsManager::setGraphicsMode(mode);
}
void OpenGLSdlGraphicsManager::resetGraphicsScale() {
OpenGLGraphicsManager::resetGraphicsScale();
// HACK: See OpenGLSdlGraphicsManager::setGraphicsMode.
_graphicsScale = 1;
}
#ifdef USE_RGB_COLOR
Common::List<Graphics::PixelFormat> OpenGLSdlGraphicsManager::getSupportedFormats() const {
Common::List<Graphics::PixelFormat> formats;
// Our default mode is (memory layout wise) RGBA8888 which is a different
// logical layout depending on the endianness. We chose this mode because
// it is the only 32bit color mode we can safely assume to be present in
// OpenGL and OpenGL ES implementations. Thus, we need to supply different
// logical formats based on endianness.
#ifdef SCUMM_LITTLE_ENDIAN
// ABGR8888
formats.push_back(Graphics::PixelFormat(4, 8, 8, 8, 8, 0, 8, 16, 24));
#else
// RGBA8888
formats.push_back(Graphics::PixelFormat(4, 8, 8, 8, 8, 24, 16, 8, 0));
#endif
// RGB565
formats.push_back(Graphics::PixelFormat(2, 5, 6, 5, 0, 11, 5, 0, 0));
// RGBA5551
formats.push_back(Graphics::PixelFormat(2, 5, 5, 5, 1, 11, 6, 1, 0));
// RGBA4444
formats.push_back(Graphics::PixelFormat(2, 4, 4, 4, 4, 12, 8, 4, 0));
#if !USE_FORCED_GLES && !USE_FORCED_GLES2
#if !USE_FORCED_GL
if (!isGLESContext()) {
#endif
#ifdef SCUMM_LITTLE_ENDIAN
// RGBA8888
formats.push_back(Graphics::PixelFormat(4, 8, 8, 8, 8, 24, 16, 8, 0));
#else
// ABGR8888
formats.push_back(Graphics::PixelFormat(4, 8, 8, 8, 8, 0, 8, 16, 24));
#endif
#if !USE_FORCED_GL
}
#endif
#endif
// RGB555, this is used by SCUMM HE 16 bit games.
// This is not natively supported by OpenGL ES implementations, we convert
// the pixel format internally.
formats.push_back(Graphics::PixelFormat(2, 5, 5, 5, 0, 10, 5, 0, 0));
formats.push_back(Graphics::PixelFormat::createFormatCLUT8());
return formats;
}
#endif
void OpenGLSdlGraphicsManager::updateScreen() {
if (_ignoreResizeEvents) {
--_ignoreResizeEvents;
}
OpenGLGraphicsManager::updateScreen();
}
void OpenGLSdlGraphicsManager::notifyVideoExpose() {
}
void OpenGLSdlGraphicsManager::notifyResize(const uint width, const uint height) {
#if SDL_VERSION_ATLEAST(2, 0, 0)
// We sometime get outdated resize events from SDL2. So check that the size we get
// is the actual current window size. If not ignore the resize.
// The issue for example occurs when switching from fullscreen to windowed mode or
// when switching between different fullscreen resolutions because SDL_DestroyWindow
// for a fullscreen window that doesn't have the SDL_WINDOW_FULLSCREEN_DESKTOP flag
// causes a SDL_WINDOWEVENT_RESIZED event with the old resolution to be sent, and this
// event is processed after recreating the window at the new resolution.
int currentWidth, currentHeight;
getWindowDimensions(¤tWidth, ¤tHeight);
if (width != (uint)currentWidth || height != (uint)currentHeight)
return;
setActualScreenSize(width, height);
_eventSource->resetKeyboardEmulation(width - 1, height - 1);
#else
if (!_ignoreResizeEvents && _hwScreen && !(_hwScreen->flags & SDL_FULLSCREEN)) {
// We save that we handled a resize event here. We need to know this
// so we do not overwrite the users requested window size whenever we
// switch aspect ratio or similar.
_gotResize = true;
if (!setupMode(width, height)) {
warning("OpenGLSdlGraphicsManager::notifyResize: Resize failed ('%s')", SDL_GetError());
g_system->quit();
}
}
#endif
}
void OpenGLSdlGraphicsManager::transformMouseCoordinates(Common::Point &point) {
adjustMousePosition(point.x, point.y);
}
void OpenGLSdlGraphicsManager::notifyMousePos(Common::Point mouse) {
setMousePosition(mouse.x, mouse.y);
}
void OpenGLSdlGraphicsManager::setInternalMousePosition(int x, int y) {
_window->warpMouseInWindow(x, y);
}
bool OpenGLSdlGraphicsManager::loadVideoMode(uint requestedWidth, uint requestedHeight, const Graphics::PixelFormat &format) {
// In some cases we might not want to load the requested video mode. This
// will assure that the window size is not altered.
if (_ignoreLoadVideoMode) {
_ignoreLoadVideoMode = false;
return true;
}
// This function should never be called from notifyResize thus we know
// that the requested size came from somewhere else.
_gotResize = false;
// Save the requested dimensions.
_lastRequestedWidth = requestedWidth;
_lastRequestedHeight = requestedHeight;
// Apply the currently saved scale setting.
requestedWidth *= _graphicsScale;
requestedHeight *= _graphicsScale;
// Set up the mode.
return setupMode(requestedWidth, requestedHeight);
}
void OpenGLSdlGraphicsManager::refreshScreen() {
// Swap OpenGL buffers
#if SDL_VERSION_ATLEAST(2, 0, 0)
SDL_GL_SwapWindow(_window->getSDLWindow());
#else
SDL_GL_SwapBuffers();
#endif
}
void *OpenGLSdlGraphicsManager::getProcAddress(const char *name) const {
return SDL_GL_GetProcAddress(name);
}
bool OpenGLSdlGraphicsManager::setupMode(uint width, uint height) {
// In case we request a fullscreen mode we will use the mode the user
// has chosen last time or the biggest mode available.
if (_wantsFullScreen) {
if (_desiredFullscreenWidth && _desiredFullscreenHeight) {
// In case only a distinct set of modes is available we check
// whether the requested mode is actually available.
if (!_fullscreenVideoModes.empty()) {
VideoModeArray::const_iterator i = Common::find(_fullscreenVideoModes.begin(),
_fullscreenVideoModes.end(),
VideoMode(_desiredFullscreenWidth, _desiredFullscreenHeight));
// It's not available fall back to default.
if (i == _fullscreenVideoModes.end()) {
_desiredFullscreenWidth = 0;
_desiredFullscreenHeight = 0;
}
}
}
// In case no desired mode has been set we default to the biggest mode
// available or the requested mode in case we don't know any
// any fullscreen modes.
if (!_desiredFullscreenWidth || !_desiredFullscreenHeight) {
if (!_fullscreenVideoModes.empty()) {
VideoModeArray::const_iterator i = _fullscreenVideoModes.end();
--i;
_desiredFullscreenWidth = i->width;
_desiredFullscreenHeight = i->height;
} else {
_desiredFullscreenWidth = width;
_desiredFullscreenHeight = height;
}
}
// Remember our choice.
ConfMan.setInt("last_fullscreen_mode_width", _desiredFullscreenWidth, Common::ConfigManager::kApplicationDomain);
ConfMan.setInt("last_fullscreen_mode_height", _desiredFullscreenHeight, Common::ConfigManager::kApplicationDomain);
// Use our choice.
width = _desiredFullscreenWidth;
height = _desiredFullscreenHeight;
}
// This is pretty confusing since RGBA8888 talks about the memory
// layout here. This is a different logical layout depending on
// whether we run on little endian or big endian. However, we can
// only safely assume that RGBA8888 in memory layout is supported.
// Thus, we chose this one.
const Graphics::PixelFormat rgba8888 =
#ifdef SCUMM_LITTLE_ENDIAN
Graphics::PixelFormat(4, 8, 8, 8, 8, 0, 8, 16, 24);
#else
Graphics::PixelFormat(4, 8, 8, 8, 8, 24, 16, 8, 0);
#endif
#if SDL_VERSION_ATLEAST(2, 0, 0)
if (_glContext) {
notifyContextDestroy();
SDL_GL_DeleteContext(_glContext);
_glContext = nullptr;
}
_window->destroyWindow();
uint32 flags = SDL_WINDOW_OPENGL;
if (_wantsFullScreen) {
flags |= SDL_WINDOW_FULLSCREEN;
} else {
flags |= SDL_WINDOW_RESIZABLE;
}
// Request a OpenGL (ES) context we can use.
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, _glContextProfileMask);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, _glContextMajor);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, _glContextMinor);
if (!_window->createWindow(width, height, flags)) {
// We treat fullscreen requests as a "hint" for now. This means in
// case it is not available we simply ignore it.
if (_wantsFullScreen) {
_window->createWindow(width, height, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
}
if (!_window->getSDLWindow()) {
return false;
}
}
_glContext = SDL_GL_CreateContext(_window->getSDLWindow());
if (!_glContext) {
return false;
}
notifyContextCreate(rgba8888, rgba8888);
int actualWidth, actualHeight;
getWindowDimensions(&actualWidth, &actualHeight);
setActualScreenSize(actualWidth, actualHeight);
_eventSource->resetKeyboardEmulation(actualWidth - 1, actualHeight - 1);
return true;
#else
// WORKAROUND: Working around infamous SDL bugs when switching
// resolutions too fast. This might cause the event system to supply
// incorrect mouse position events otherwise.
// Reference: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=665779
const uint32 curTime = SDL_GetTicks();
if (_hwScreen && (curTime < _lastVideoModeLoad || curTime - _lastVideoModeLoad < 100)) {
for (int i = 10; i > 0; --i) {
SDL_PumpEvents();
SDL_Delay(10);
}
}
uint32 flags = SDL_OPENGL;
if (_wantsFullScreen) {
flags |= SDL_FULLSCREEN;
} else {
flags |= SDL_RESIZABLE;
}
if (_hwScreen) {
// When a video mode has been setup already we notify the manager that
// the context is about to be destroyed.
// We do this because on Windows SDL_SetVideoMode can destroy and
// recreate the OpenGL context.
notifyContextDestroy();
}
_hwScreen = SDL_SetVideoMode(width, height, 32, flags);
if (!_hwScreen) {
// We treat fullscreen requests as a "hint" for now. This means in
// case it is not available we simply ignore it.
if (_wantsFullScreen) {
_hwScreen = SDL_SetVideoMode(width, height, 32, SDL_OPENGL | SDL_RESIZABLE);
}
}
// Part of the WORKAROUND mentioned above.
_lastVideoModeLoad = SDL_GetTicks();
if (_hwScreen) {
notifyContextCreate(rgba8888, rgba8888);
setActualScreenSize(_hwScreen->w, _hwScreen->h);
_eventSource->resetKeyboardEmulation(_hwScreen->w - 1, _hwScreen->h - 1);
}
// Ignore resize events (from SDL) for a few frames, if this isn't
// caused by a notification from SDL. This avoids bad resizes to a
// (former) resolution for which we haven't processed an event yet.
if (!_gotResize)
_ignoreResizeEvents = 10;
return _hwScreen != nullptr;
#endif
}
void OpenGLSdlGraphicsManager::getWindowDimensions(int *width, int *height) {
#if SDL_VERSION_ATLEAST(2, 0, 0)
SDL_GetWindowSize(_window->getSDLWindow(), width, height);
#else
if (width) {
*width = _hwScreen->w;
}
if (height) {
*height = _hwScreen->h;
}
#endif
}
bool OpenGLSdlGraphicsManager::notifyEvent(const Common::Event &event) {
switch (event.type) {
case Common::EVENT_KEYUP:
return isHotkey(event);
case Common::EVENT_KEYDOWN:
if (event.kbd.hasFlags(Common::KBD_ALT)) {
if ( event.kbd.keycode == Common::KEYCODE_RETURN
|| event.kbd.keycode == (Common::KeyCode)SDLK_KP_ENTER) {
// Alt-Return and Alt-Enter toggle full screen mode
beginGFXTransaction();
setFeatureState(OSystem::kFeatureFullscreenMode, !getFeatureState(OSystem::kFeatureFullscreenMode));
endGFXTransaction();
#ifdef USE_OSD
if (getFeatureState(OSystem::kFeatureFullscreenMode)) {
displayMessageOnOSD(_("Fullscreen mode"));
} else {
displayMessageOnOSD(_("Windowed mode"));
}
#endif
return true;
}
// Alt-s creates a screenshot
if (event.kbd.keycode == Common::KEYCODE_s) {
Common::String filename;
Common::String screenshotsPath;
OSystem_SDL *sdl_g_system = dynamic_cast<OSystem_SDL*>(g_system);
if (sdl_g_system)
screenshotsPath = sdl_g_system->getScreenshotsPath();
for (int n = 0;; n++) {
SDL_RWops *file;
#ifdef USE_PNG
filename = Common::String::format("scummvm%05d.png", n);
#else
filename = Common::String::format("scummvm%05d.bmp", n);
#endif
file = SDL_RWFromFile((screenshotsPath + filename).c_str(), "r");
if (!file)
break;
SDL_RWclose(file);
}
if (saveScreenshot(screenshotsPath + filename)) {
if (screenshotsPath.empty())
debug("Saved screenshot '%s' in current directory", filename.c_str());
else
debug("Saved screenshot '%s' in directory '%s'", filename.c_str(), screenshotsPath.c_str());
} else {
if (screenshotsPath.empty())
warning("Could not save screenshot in current directory");
else
warning("Could not save screenshot in directory '%s'", screenshotsPath.c_str());
}
return true;
}
} else if (event.kbd.hasFlags(Common::KBD_CTRL | Common::KBD_ALT)) {
if ( event.kbd.keycode == Common::KEYCODE_PLUS || event.kbd.keycode == Common::KEYCODE_MINUS
|| event.kbd.keycode == Common::KEYCODE_KP_PLUS || event.kbd.keycode == Common::KEYCODE_KP_MINUS) {
// Ctrl+Alt+Plus/Minus Increase/decrease the size
const int direction = (event.kbd.keycode == Common::KEYCODE_PLUS || event.kbd.keycode == Common::KEYCODE_KP_PLUS) ? +1 : -1;
if (getFeatureState(OSystem::kFeatureFullscreenMode)) {
// In case we are in fullscreen we will choose the previous
// or next mode.
// In case no modes are available we do nothing.
if (_fullscreenVideoModes.empty()) {
return true;
}
// Look for the current mode.
VideoModeArray::const_iterator i = Common::find(_fullscreenVideoModes.begin(),
_fullscreenVideoModes.end(),
VideoMode(_desiredFullscreenWidth, _desiredFullscreenHeight));
if (i == _fullscreenVideoModes.end()) {
return true;
}
// Cycle through the modes in the specified direction.
if (direction > 0) {
++i;
if (i == _fullscreenVideoModes.end()) {
i = _fullscreenVideoModes.begin();
}
} else {
if (i == _fullscreenVideoModes.begin()) {
i = _fullscreenVideoModes.end();
}
--i;
}
_desiredFullscreenWidth = i->width;
_desiredFullscreenHeight = i->height;
// Try to setup the mode.
if (!setupMode(_lastRequestedWidth, _lastRequestedHeight)) {
warning("OpenGLSdlGraphicsManager::notifyEvent: Fullscreen resize failed ('%s')", SDL_GetError());
g_system->quit();
}
} else {
// Calculate the next scaling setting. We approximate the
// current scale setting in case the user resized the
// window. Then we apply the direction change.
int windowWidth = 0, windowHeight = 0;
getWindowDimensions(&windowWidth, &windowHeight);
_graphicsScale = MAX<int>(windowWidth / _lastRequestedWidth, windowHeight / _lastRequestedHeight);
_graphicsScale = MAX<int>(_graphicsScale + direction, 1);
// Since we overwrite a user resize here we reset its
// flag here. This makes enabling AR smoother because it
// will change the window size like in surface SDL.
_gotResize = false;
// Try to setup the mode.
if (!setupMode(_lastRequestedWidth * _graphicsScale, _lastRequestedHeight * _graphicsScale)) {
warning("OpenGLSdlGraphicsManager::notifyEvent: Window resize failed ('%s')", SDL_GetError());
g_system->quit();
}
}
#ifdef USE_OSD
int windowWidth = 0, windowHeight = 0;
getWindowDimensions(&windowWidth, &windowHeight);
const Common::String osdMsg = Common::String::format(_("Resolution: %dx%d"), windowWidth, windowHeight);
displayMessageOnOSD(osdMsg.c_str());
#endif
return true;
} else if (event.kbd.keycode == Common::KEYCODE_a) {
// In case the user changed the window size manually we will
// not change the window size again here.
_ignoreLoadVideoMode = _gotResize;
// Ctrl+Alt+a toggles the aspect ratio correction state.
beginGFXTransaction();
setFeatureState(OSystem::kFeatureAspectRatioCorrection, !getFeatureState(OSystem::kFeatureAspectRatioCorrection));
endGFXTransaction();
// Make sure we do not ignore the next resize. This
// effectively checks whether loadVideoMode has been called.
assert(!_ignoreLoadVideoMode);
#ifdef USE_OSD
if (getFeatureState(OSystem::kFeatureAspectRatioCorrection))
displayMessageOnOSD(_("Enabled aspect ratio correction"));
else
displayMessageOnOSD(_("Disabled aspect ratio correction"));
#endif
return true;
} else if (event.kbd.keycode == Common::KEYCODE_f) {
// Never ever try to resize the window when we simply want to enable or disable filtering.
// This assures that the window size does not change.
_ignoreLoadVideoMode = true;
// Ctrl+Alt+f toggles filtering on/off
beginGFXTransaction();
setFeatureState(OSystem::kFeatureFilteringMode, !getFeatureState(OSystem::kFeatureFilteringMode));
endGFXTransaction();
// Make sure we do not ignore the next resize. This
// effectively checks whether loadVideoMode has been called.
assert(!_ignoreLoadVideoMode);
#ifdef USE_OSD
if (getFeatureState(OSystem::kFeatureFilteringMode)) {
displayMessageOnOSD(_("Filtering enabled"));
} else {
displayMessageOnOSD(_("Filtering disabled"));
}
#endif
return true;
}
}
// Fall through
default:
return false;
}
}
bool OpenGLSdlGraphicsManager::isHotkey(const Common::Event &event) {
if (event.kbd.hasFlags(Common::KBD_ALT)) {
return event.kbd.keycode == Common::KEYCODE_RETURN
|| event.kbd.keycode == (Common::KeyCode)SDLK_KP_ENTER
|| event.kbd.keycode == Common::KEYCODE_s;
} else if (event.kbd.hasFlags(Common::KBD_CTRL | Common::KBD_ALT)) {
return event.kbd.keycode == Common::KEYCODE_PLUS || event.kbd.keycode == Common::KEYCODE_MINUS
|| event.kbd.keycode == Common::KEYCODE_KP_PLUS || event.kbd.keycode == Common::KEYCODE_KP_MINUS
|| event.kbd.keycode == Common::KEYCODE_a
|| event.kbd.keycode == Common::KEYCODE_f;
}
return false;
}
|