-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathVideoSDL2.cpp
More file actions
489 lines (430 loc) · 14.6 KB
/
Copy pathVideoSDL2.cpp
File metadata and controls
489 lines (430 loc) · 14.6 KB
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
// Copyright (C) 2005 - 2021 Settlers Freaks (sf-team at siedler25.org)
//
// SPDX-License-Identifier: GPL-2.0-or-later
#include "VideoSDL2.h"
#include "driver/Interface.h"
#include "driver/VideoDriverLoaderInterface.h"
#include "driver/VideoInterface.h"
#include "enum_cast.hpp"
#include "helpers/LSANUtils.h"
#include "helpers/containerUtils.h"
#include "icon.h"
#ifndef __EMSCRIPTEN__
#include "openglCfg.hpp"
#endif
#include <s25util/utf8.h>
#include <boost/nowide/iostream.hpp>
#include <SDL.h>
#include <algorithm>
#include <memory>
#ifdef _WIN32
# include <boost/nowide/convert.hpp>
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif
# include <windows.h> // Avoid "Windows headers require the default packing option" due to SDL2
# include <SDL_syswm.h>
#endif // _WIN32
#define CHECK_SDL(call) \
do \
{ \
if((call) == -1) \
PrintError(SDL_GetError()); \
} while(false)
namespace {
template<typename T>
struct SDLMemoryDeleter
{
void operator()(T* p) const { SDL_free(p); }
};
template<typename T>
using SDL_memory = std::unique_ptr<T, SDLMemoryDeleter<T>>;
} // namespace
IVideoDriver* CreateVideoInstance(VideoDriverLoaderInterface* CallBack)
{
return new VideoSDL2(CallBack);
}
void FreeVideoInstance(IVideoDriver* driver)
{
delete driver;
}
const char* GetVideoDriverName()
{
return "(SDL2) OpenGL via SDL2-Library";
}
VideoSDL2::VideoSDL2(VideoDriverLoaderInterface* CallBack) : VideoDriver(CallBack), window(nullptr), context(nullptr) {}
VideoSDL2::~VideoSDL2()
{
CleanUp();
}
const char* VideoSDL2::GetName() const
{
return GetVideoDriverName();
}
bool VideoSDL2::Initialize()
{
initialized = false;
rttr::ScopedLeakDisabler _;
if(SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
{
PrintError(SDL_GetError());
return false;
}
initialized = true;
return initialized;
}
void VideoSDL2::CleanUp()
{
if(!initialized)
return;
if(context)
SDL_GL_DeleteContext(context);
if(window)
SDL_DestroyWindow(window);
SDL_QuitSubSystem(SDL_INIT_VIDEO);
SDL_Quit();
initialized = false;
}
void VideoSDL2::UpdateCurrentSizes()
{
int w, h, w2, h2;
SDL_GetWindowSize(window, &w, &h);
SDL_GL_GetDrawableSize(window, &w2, &h2);
SetNewSize(VideoMode(w, h), Extent(w2, h2));
}
bool VideoSDL2::CreateScreen(const std::string& title, const VideoMode& size, bool fullscreen)
{
if(!initialized)
return false;
#ifndef __EMSCRIPTEN__
// GL-Attributes
CHECK_SDL(SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, RTTR_OGL_MAJOR));
CHECK_SDL(SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, RTTR_OGL_MINOR));
SDL_GLprofile profile;
if((RTTR_OGL_ES))
profile = SDL_GL_CONTEXT_PROFILE_ES;
else if((RTTR_OGL_COMPAT))
profile = SDL_GL_CONTEXT_PROFILE_COMPATIBILITY;
else
profile = SDL_GL_CONTEXT_PROFILE_CORE;
CHECK_SDL(SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, profile));
CHECK_SDL(SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8));
CHECK_SDL(SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8));
CHECK_SDL(SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8));
CHECK_SDL(SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1));
#endif
int wndPos = SDL_WINDOWPOS_CENTERED;
const auto requestedSize = fullscreen ? FindClosestVideoMode(size) : size;
unsigned commonFlags = SDL_WINDOW_OPENGL;
// TODO: Fix GUI scaling with High DPI support enabled.
// See https://github.com/Return-To-The-Roots/s25client/issues/1621
// commonFlags |= SDL_WINDOW_ALLOW_HIGHDPI;
window = SDL_CreateWindow(title.c_str(), wndPos, wndPos, requestedSize.width, requestedSize.height,
commonFlags | (fullscreen ? SDL_WINDOW_FULLSCREEN : SDL_WINDOW_RESIZABLE));
// Fallback to non-fullscreen
if(!window && fullscreen)
{
window = SDL_CreateWindow(title.c_str(), wndPos, wndPos, requestedSize.width, requestedSize.height,
commonFlags | SDL_WINDOW_RESIZABLE);
}
if(!window)
{
PrintError(SDL_GetError());
return false;
}
isFullscreen_ = (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN) != 0;
UpdateCurrentSizes();
if(!isFullscreen_)
MoveWindowToCenter();
SDL_Surface* iconSurf =
SDL_CreateRGBSurfaceFrom(image.data(), 48, 48, 32, 48 * 4, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
if(iconSurf)
{
SDL_SetWindowIcon(window, iconSurf);
SDL_FreeSurface(iconSurf);
} else
PrintError(SDL_GetError());
context = SDL_GL_CreateContext(window);
if(!context)
{
PrintError(SDL_GetError());
return false;
}
#ifdef _WIN32
SetWindowTextW(GetConsoleWindow(), boost::nowide::widen(title).c_str());
#endif
std::fill(keyboard.begin(), keyboard.end(), false);
SDL_ShowCursor(0);
return true;
}
bool VideoSDL2::ResizeScreen(const VideoMode& newSize, bool fullscreen)
{
if(!initialized)
return false;
if(isFullscreen_ != fullscreen)
{
SDL_SetWindowFullscreen(window, fullscreen ? SDL_WINDOW_FULLSCREEN : 0);
isFullscreen_ = (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN) != 0;
if(!isFullscreen_)
{
SDL_SetWindowResizable(window, SDL_TRUE);
MoveWindowToCenter();
}
}
if(newSize != GetWindowSize())
{
if(isFullscreen_)
{
auto const targetMode = FindClosestVideoMode(newSize);
SDL_DisplayMode target;
target.w = targetMode.width;
target.h = targetMode.height;
target.format = 0; // don't care
target.refresh_rate = 0; // don't care
target.driverdata = nullptr; // initialize to 0
// Explicitly change the window size to avoid a bug with SDL reporting the wrong size until alt+tab
SDL_SetWindowSize(window, target.w, target.h);
if(SDL_SetWindowDisplayMode(window, &target) < 0)
{
PrintError(SDL_GetError());
return false;
}
} else
{
SDL_SetWindowSize(window, newSize.width, newSize.height);
}
UpdateCurrentSizes();
}
return true;
}
void VideoSDL2::PrintError(const std::string& msg) const
{
boost::nowide::cerr << msg << std::endl;
}
void VideoSDL2::ShowErrorMessage(const std::string& title, const std::string& message)
{
// window==nullptr is okay too ("no parent")
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, title.c_str(), message.c_str(), window);
}
void VideoSDL2::HandlePaste()
{
if(!SDL_HasClipboardText())
return;
SDL_memory<char> text(SDL_GetClipboardText());
if(!text || *text == '\0') // empty string indicates error
PrintError(text ? SDL_GetError() : "Paste failed.");
KeyEvent ke = {KeyType::Char, 0, false, false, false};
for(const char32_t c : s25util::utf8to32(text.get()))
{
ke.c = static_cast<unsigned>(c);
CallBack->Msg_KeyDown(ke);
}
}
void VideoSDL2::DestroyScreen()
{
CleanUp();
}
bool VideoSDL2::SwapBuffers()
{
SDL_GL_SwapWindow(window);
return true;
}
bool VideoSDL2::MessageLoop()
{
SDL_Event ev;
while(SDL_PollEvent(&ev))
{
switch(ev.type)
{
default: break;
case SDL_QUIT: return false;
case SDL_WINDOWEVENT:
{
switch(ev.window.event)
{
case SDL_WINDOWEVENT_RESIZED:
{
isFullscreen_ = (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN) != 0;
VideoMode newSize(ev.window.data1, ev.window.data2);
if(newSize != GetWindowSize())
{
UpdateCurrentSizes();
CallBack->WindowResized();
}
}
break;
}
}
break;
case SDL_KEYDOWN:
{
KeyEvent ke = {KeyType::Invalid, 0, false, false, false};
switch(ev.key.keysym.sym)
{
default:
{
// Die 12 F-Tasten
if(ev.key.keysym.sym >= SDLK_F1 && ev.key.keysym.sym <= SDLK_F12)
ke.kt = static_cast<KeyType>(rttr::enum_cast(KeyType::F1) + ev.key.keysym.sym - SDLK_F1);
}
break;
case SDLK_RETURN: ke.kt = KeyType::Return; break;
case SDLK_SPACE: ke.kt = KeyType::Space; break;
case SDLK_LEFT: ke.kt = KeyType::Left; break;
case SDLK_RIGHT: ke.kt = KeyType::Right; break;
case SDLK_UP: ke.kt = KeyType::Up; break;
case SDLK_DOWN: ke.kt = KeyType::Down; break;
case SDLK_BACKSPACE: ke.kt = KeyType::Backspace; break;
case SDLK_DELETE: ke.kt = KeyType::Delete; break;
case SDLK_LSHIFT:
case SDLK_RSHIFT: ke.kt = KeyType::Shift; break;
case SDLK_TAB: ke.kt = KeyType::Tab; break;
case SDLK_HOME: ke.kt = KeyType::Home; break;
case SDLK_END: ke.kt = KeyType::End; break;
case SDLK_ESCAPE: ke.kt = KeyType::Escape; break;
case SDLK_PRINTSCREEN: ke.kt = KeyType::Print; break;
// case SDLK_BACKQUOTE: ev.key.keysym.scancode = '^'; break;
case SDLK_v:
if(SDL_GetModState() & KMOD_CTRL)
{
HandlePaste();
continue;
}
break;
}
if(ke.kt == KeyType::Invalid)
break;
/// Strg, Alt, usw gedrückt?
if(ev.key.keysym.mod & KMOD_CTRL)
ke.ctrl = true;
if(ev.key.keysym.mod & KMOD_SHIFT)
ke.shift = true;
if(ev.key.keysym.mod & KMOD_ALT)
ke.alt = true;
CallBack->Msg_KeyDown(ke);
}
break;
case SDL_TEXTINPUT:
{
const std::u32string text = s25util::utf8to32(ev.text.text);
SDL_Keymod mod = SDL_GetModState();
KeyEvent ke = {KeyType::Char, 0, (mod & KMOD_CTRL) != 0, (mod & KMOD_SHIFT) != 0,
(mod & KMOD_ALT) != 0};
for(char32_t c : text)
{
ke.c = static_cast<unsigned>(c);
CallBack->Msg_KeyDown(ke);
}
break;
}
case SDL_MOUSEBUTTONDOWN:
mouse_xy.pos = getGuiScale().screenToView(Position(ev.button.x, ev.button.y));
if(/*!mouse_xy.ldown && */ ev.button.button == SDL_BUTTON_LEFT)
{
mouse_xy.ldown = true;
CallBack->Msg_LeftDown(mouse_xy);
}
if(/*!mouse_xy.rdown &&*/ ev.button.button == SDL_BUTTON_RIGHT)
{
mouse_xy.rdown = true;
CallBack->Msg_RightDown(mouse_xy);
}
break;
case SDL_MOUSEBUTTONUP:
mouse_xy.pos = getGuiScale().screenToView(Position(ev.button.x, ev.button.y));
if(/*mouse_xy.ldown &&*/ ev.button.button == SDL_BUTTON_LEFT)
{
mouse_xy.ldown = false;
CallBack->Msg_LeftUp(mouse_xy);
}
if(/*mouse_xy.rdown &&*/ ev.button.button == SDL_BUTTON_RIGHT)
{
mouse_xy.rdown = false;
CallBack->Msg_RightUp(mouse_xy);
}
break;
case SDL_MOUSEWHEEL:
{
int y = ev.wheel.y;
if(ev.wheel.direction == SDL_MOUSEWHEEL_FLIPPED)
y = -y;
if(y > 0)
CallBack->Msg_WheelUp(mouse_xy);
else if(y < 0)
CallBack->Msg_WheelDown(mouse_xy);
}
break;
case SDL_MOUSEMOTION:
mouse_xy.pos = getGuiScale().screenToView(Position(ev.motion.x, ev.motion.y));
CallBack->Msg_MouseMove(mouse_xy);
break;
}
}
return true;
}
unsigned long VideoSDL2::GetTickCount() const
{
return SDL_GetTicks();
}
void VideoSDL2::ListVideoModes(std::vector<VideoMode>& video_modes) const
{
int display = SDL_GetWindowDisplayIndex(window);
if(display < 0)
display = 0;
for(int i = SDL_GetNumDisplayModes(display) - 1; i >= 0; --i)
{
SDL_DisplayMode mode;
if(SDL_GetDisplayMode(display, i, &mode) != 0)
PrintError(SDL_GetError());
else
{
VideoMode vm(mode.w, mode.h);
if(!helpers::contains(video_modes, vm))
video_modes.push_back(vm);
}
}
}
OpenGL_Loader_Proc VideoSDL2::GetLoaderFunction() const
{
return SDL_GL_GetProcAddress;
}
void VideoSDL2::SetMousePos(Position pos)
{
const auto screenPos = getGuiScale().viewToScreen(pos);
mouse_xy.pos = pos;
SDL_WarpMouseInWindow(window, screenPos.x, screenPos.y);
}
KeyEvent VideoSDL2::GetModKeyState() const
{
const SDL_Keymod modifiers = SDL_GetModState();
const KeyEvent ke = {KeyType::Invalid, 0, ((modifiers & KMOD_CTRL) != 0), ((modifiers & KMOD_SHIFT) != 0),
((modifiers & KMOD_ALT) != 0)};
return ke;
}
void* VideoSDL2::GetMapPointer() const
{
#ifdef WIN32
SDL_SysWMinfo wmInfo;
SDL_VERSION(&wmInfo.version);
SDL_GetWindowWMInfo(window, &wmInfo);
// return (void*)wmInfo.info.win.window;
return (void*)wmInfo.info.win.window;
#else
return nullptr;
#endif
}
void VideoSDL2::MoveWindowToCenter()
{
SDL_Rect usableBounds;
CHECK_SDL(SDL_GetDisplayUsableBounds(SDL_GetWindowDisplayIndex(window), &usableBounds));
int top, left, bottom, right;
CHECK_SDL(SDL_GetWindowBordersSize(window, &top, &left, &bottom, &right));
usableBounds.w -= left + right;
usableBounds.h -= top + bottom;
if(usableBounds.w < GetWindowSize().width || usableBounds.h < GetWindowSize().height)
{
SDL_SetWindowSize(window, usableBounds.w, usableBounds.h);
UpdateCurrentSizes();
}
SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
}