• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Return-To-The-Roots / s25client / 24903561834

24 Apr 2026 05:43PM UTC coverage: 50.223% (-0.2%) from 50.384%
24903561834

Pull #1890

github

web-flow
Merge 70c42b3ee into 45e8bc010
Pull Request #1890: Add support for borderless Windows

167 of 641 new or added lines in 44 files covered. (26.05%)

25 existing lines in 8 files now uncovered.

23077 of 45949 relevant lines covered (50.22%)

43513.44 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

8.6
/extras/videoDrivers/SDL2/VideoSDL2.cpp
1
// Copyright (C) 2005 - 2026 Settlers Freaks (sf-team at siedler25.org)
2
//
3
// SPDX-License-Identifier: GPL-2.0-or-later
4

5
#include "VideoSDL2.h"
6
#include "RTTR_Assert.h"
7
#include "driver/Interface.h"
8
#include "driver/VideoDriverLoaderInterface.h"
9
#include "driver/VideoInterface.h"
10
#include "enum_cast.hpp"
11
#include "helpers/LSANUtils.h"
12
#include "helpers/containerUtils.h"
13
#include "icon.h"
14
#include "openglCfg.hpp"
15
#include <s25util/utf8.h>
16
#include <boost/nowide/iostream.hpp>
17
#include <SDL.h>
18
#include <SDL_hints.h>
19
#include <SDL_video.h>
20
#include <algorithm>
21
#include <memory>
22

23
#ifdef _WIN32
24
#    include <boost/nowide/convert.hpp>
25
#    ifndef WIN32_LEAN_AND_MEAN
26
#        define WIN32_LEAN_AND_MEAN
27
#    endif
28
#    include <windows.h> // Avoid "Windows headers require the default packing option" due to SDL2
29
#    include <SDL_syswm.h>
30
#endif // _WIN32
31
#if RTTR_OGL_GL4ES
32
#    include <gl4esinit.h>
33
#endif
34

35
namespace {
36

37
/// Check that the (SDL) call returns success or print the error
38
/// Can be used in conditions: if(CHECK_SDL(SDL_Foo()))
39
bool CHECK_SDL(int sdlResult)
1✔
40
{
41
    if(sdlResult >= 0)
1✔
42
        return true;
1✔
NEW
43
    VideoSDL2::PrintError();
×
NEW
44
    return false;
×
45
}
46

47
template<typename T>
48
struct SDLMemoryDeleter
49
{
50
    void operator()(T* p) const { SDL_free(p); }
×
51
};
52

53
template<typename T>
54
using SDL_memory = std::unique_ptr<T, SDLMemoryDeleter<T>>;
55

56
void setSpecialKeys(KeyEvent& ke, const SDL_Keymod mod)
×
57
{
58
    ke.ctrl = (mod & KMOD_CTRL);
×
59
    ke.shift = (mod & KMOD_SHIFT);
×
60
    ke.alt = (mod & KMOD_ALT);
×
61
}
×
62
void setSpecialKeys(KeyEvent& ke)
×
63
{
64
    setSpecialKeys(ke, SDL_GetModState());
×
65
}
×
66
} // namespace
67

68
IVideoDriver* CreateVideoInstance(VideoDriverLoaderInterface* CallBack)
1✔
69
{
70
    return new VideoSDL2(CallBack);
1✔
71
}
72

73
void FreeVideoInstance(IVideoDriver* driver)
1✔
74
{
75
    delete driver;
1✔
76
}
1✔
77

78
const char* GetDriverName()
5✔
79
{
80
#if RTTR_OGL_GL4ES
81
    return "(SDL2) OpenGL-ES gl4es via SDL2-Library";
82
#else
83
    return "(SDL2) OpenGL via SDL2-Library";
5✔
84
#endif
85
}
86

87
VideoSDL2::VideoSDL2(VideoDriverLoaderInterface* CallBack) : VideoDriver(CallBack), window(nullptr), context(nullptr) {}
1✔
88

89
VideoSDL2::~VideoSDL2()
2✔
90
{
91
    CleanUp();
1✔
92
}
2✔
93

94
const char* VideoSDL2::GetName() const
2✔
95
{
96
    return GetDriverName();
2✔
97
}
98

99
bool VideoSDL2::Initialize()
1✔
100
{
101
    initialized = false;
1✔
102
    rttr::ScopedLeakDisabler _;
1✔
103
    // Do not emulate mouse events using touch
104
    SDL_SetHint(SDL_HINT_TOUCH_MOUSE_EVENTS, "0");
1✔
105
    if(CHECK_SDL(SDL_InitSubSystem(SDL_INIT_VIDEO)))
1✔
106
        initialized = true;
1✔
107

108
    return initialized;
1✔
109
}
110

111
void VideoSDL2::CleanUp()
1✔
112
{
113
    if(!initialized)
1✔
114
        return;
×
115

116
    if(context)
1✔
117
        SDL_GL_DeleteContext(context);
×
118
    if(window)
1✔
119
        SDL_DestroyWindow(window);
×
120
    SDL_QuitSubSystem(SDL_INIT_VIDEO);
1✔
121
    SDL_Quit();
1✔
122
    initialized = false;
1✔
123
}
124

125
void VideoSDL2::UpdateCurrentSizes()
×
126
{
127
    int w, h, w2, h2;
128
    SDL_GetWindowSize(window, &w, &h);
×
129
    SDL_GL_GetDrawableSize(window, &w2, &h2);
×
130
    SetNewSize(VideoMode(w, h), Extent(w2, h2));
×
131
}
×
132

NEW
133
static VideoMode getDesktopSize(SDL_Window* window, VideoMode fallback)
×
134
{
NEW
135
    const int display = window ? std::max(0, SDL_GetWindowDisplayIndex(window)) : 0;
×
136
    SDL_DisplayMode dskSize;
NEW
137
    if(CHECK_SDL(SDL_GetDesktopDisplayMode(display, &dskSize)))
×
NEW
138
        return VideoMode(dskSize.w, dskSize.h);
×
NEW
139
    return fallback;
×
140
}
141

NEW
142
bool VideoSDL2::CreateScreen(const std::string& title, const VideoMode size, DisplayMode displayMode)
×
143
{
144
    if(!initialized)
×
145
        return false;
×
146

147
    // GL-Attributes
148
    CHECK_SDL(SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, RTTR_OGL_MAJOR));
×
149
    CHECK_SDL(SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, RTTR_OGL_MINOR));
×
150
    SDL_GLprofile profile;
151
    if(RTTR_OGL_ES || RTTR_OGL_GL4ES)
152
        profile = SDL_GL_CONTEXT_PROFILE_ES;
153
    else if(RTTR_OGL_COMPAT)
154
        profile = SDL_GL_CONTEXT_PROFILE_COMPATIBILITY;
×
155
    else
156
        profile = SDL_GL_CONTEXT_PROFILE_CORE;
157

158
    CHECK_SDL(SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, profile));
×
159

160
    CHECK_SDL(SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8));
×
161
    CHECK_SDL(SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8));
×
162
    CHECK_SDL(SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8));
×
163
    CHECK_SDL(SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1));
×
164

NEW
165
    const int wndPos = SDL_WINDOWPOS_CENTERED;
×
NEW
166
    const unsigned commonFlags = SDL_WINDOW_OPENGL;
×
167
    // TODO: Fix GUI scaling with High DPI support enabled.
168
    // See https://github.com/Return-To-The-Roots/s25client/issues/1621
169
    // commonFlags |= SDL_WINDOW_ALLOW_HIGHDPI;
170

NEW
171
    unsigned windowTypeFlag = 0;
×
NEW
172
    auto requestedSize = size;
×
NEW
173
    if(displayMode == DisplayMode::Fullscreen)
×
174
    {
NEW
175
        windowTypeFlag = SDL_WINDOW_FULLSCREEN;
×
NEW
176
        requestedSize = FindClosestVideoMode(size);
×
NEW
177
    } else if(displayMode == DisplayMode::BorderlessWindow)
×
178
    {
NEW
179
        windowTypeFlag = SDL_WINDOW_BORDERLESS;
×
NEW
180
        requestedSize = getDesktopSize(nullptr, size);
×
NEW
181
    } else if(displayMode.resizeable)
×
NEW
182
        windowTypeFlag = SDL_WINDOW_RESIZABLE;
×
183

NEW
184
    window = SDL_CreateWindow(title.c_str(), wndPos, wndPos, requestedSize.width, requestedSize.height,
×
185
                              commonFlags | windowTypeFlag);
186

187
    if(!window)
×
188
    {
NEW
189
        PrintError();
×
190
        // Fallback to borderless fullscreen if unable to set resolution
NEW
191
        if(displayMode == DisplayMode::Fullscreen)
×
NEW
192
            return CreateScreen(title, size, DisplayMode::BorderlessWindow);
×
193
        // No borderless -> Resizable window as last fallback to at least show something
NEW
194
        if(displayMode == DisplayMode::BorderlessWindow)
×
NEW
195
            return CreateScreen(title, size, DisplayMode::Windowed);
×
NEW
196
        PrintError();
×
UNCOV
197
        return false;
×
198
    }
199

NEW
200
    UpdateCurrentDisplayMode();
×
201
    UpdateCurrentSizes();
×
202

NEW
203
    if(displayMode != DisplayMode::Fullscreen)
×
204
        MoveWindowToCenter();
×
205

206
    SDL_Surface* iconSurf =
207
      SDL_CreateRGBSurfaceFrom(image.data(), 48, 48, 32, 48 * 4, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
×
208
    if(iconSurf)
×
209
    {
210
        SDL_SetWindowIcon(window, iconSurf);
×
211
        SDL_FreeSurface(iconSurf);
×
212
    } else
NEW
213
        PrintError();
×
214

215
    context = SDL_GL_CreateContext(window);
×
216

217
#ifdef _WIN32
218
    SetWindowTextW(GetConsoleWindow(), boost::nowide::widen(title).c_str());
219
#endif
220

221
    std::fill(keyboard.begin(), keyboard.end(), false);
×
222

223
    SDL_ShowCursor(0);
×
224

225
    return true;
×
226
}
227

NEW
228
bool VideoSDL2::ResizeScreen(VideoMode newSize, DisplayMode displayMode)
×
229
{
230
    if(!initialized)
×
231
        return false;
×
232

NEW
233
    bool centerWindow = false;
×
234

NEW
235
    if(displayMode_ != displayMode)
×
236
    {
NEW
237
        if(displayMode_ == DisplayMode::Fullscreen || displayMode == DisplayMode::Fullscreen)
×
238
        {
NEW
239
            if(!CHECK_SDL(
×
NEW
240
                 SDL_SetWindowFullscreen(window, (displayMode == DisplayMode::Fullscreen) ? SDL_WINDOW_FULLSCREEN : 0)))
×
241
            {
NEW
242
                if(displayMode == DisplayMode::Fullscreen)
×
NEW
243
                    displayMode = DisplayMode::BorderlessWindow;
×
244
            }
245
        }
NEW
246
        SDL_SetWindowResizable(window,
×
NEW
247
                               static_cast<SDL_bool>(displayMode == DisplayMode::Windowed && displayMode.resizeable));
×
NEW
248
        SDL_SetWindowBordered(window, static_cast<SDL_bool>(displayMode == DisplayMode::Windowed));
×
249

NEW
250
        UpdateCurrentDisplayMode();
×
NEW
251
        centerWindow = true;
×
252
    }
NEW
253
    if(displayMode_ == DisplayMode::BorderlessWindow)
×
NEW
254
        newSize = getDesktopSize(nullptr, newSize);
×
UNCOV
255
    if(newSize != GetWindowSize())
×
256
    {
NEW
257
        if(displayMode_ == DisplayMode::Fullscreen)
×
258
        {
259
            auto const targetMode = FindClosestVideoMode(newSize);
×
260
            SDL_DisplayMode target;
261
            target.w = targetMode.width;
×
262
            target.h = targetMode.height;
×
NEW
263
            target.format = 0;       // don't care
×
NEW
264
            target.refresh_rate = 0; // don't care
×
NEW
265
            target.driverdata = nullptr;
×
266
            // Explicitly change the window size to avoid a bug with SDL reporting the wrong size until alt+tab
267
            SDL_SetWindowSize(window, target.w, target.h);
×
NEW
268
            if(!CHECK_SDL(SDL_SetWindowDisplayMode(window, &target)))
×
UNCOV
269
                return false;
×
270
        } else
271
        {
272
            SDL_SetWindowSize(window, newSize.width, newSize.height);
×
NEW
273
            centerWindow = true;
×
274
        }
275
        UpdateCurrentSizes();
×
276
    }
NEW
277
    if(centerWindow)
×
NEW
278
        MoveWindowToCenter();
×
279

UNCOV
280
    return true;
×
281
}
282

NEW
283
void VideoSDL2::PrintError()
×
284
{
NEW
285
    PrintError(SDL_GetError());
×
NEW
286
}
×
287

NEW
288
void VideoSDL2::PrintError(const std::string& msg)
×
289
{
290
    boost::nowide::cerr << msg << std::endl;
×
291
}
×
292

293
void VideoSDL2::ShowErrorMessage(const std::string& title, const std::string& message)
×
294
{
295
    // window==nullptr is okay too ("no parent")
296
#ifdef __linux__
297
    // When using window, SDL will try to use a system tool like "zenity" which isn't always installed on every distro
298
    // so rttr will crash. But without a window it will use an x11 backend that should be available on x11 AND wayland
299
    // for compatibility reasons
300
    SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, title.c_str(), message.c_str(), nullptr);
×
301
#else
302
    SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, title.c_str(), message.c_str(), window);
303
#endif
304
}
×
305

306
void VideoSDL2::HandlePaste()
×
307
{
308
    if(!SDL_HasClipboardText())
×
309
        return;
×
310

311
    SDL_memory<char> text(SDL_GetClipboardText());
×
312
    if(!text || *text == '\0') // empty string indicates error
×
313
        PrintError(text ? SDL_GetError() : "Paste failed.");
×
314

315
    for(const char32_t c : s25util::utf8to32(text.get()))
×
316
        CallBack->Msg_KeyDown(KeyEvent(c));
×
317
}
318

319
void VideoSDL2::DestroyScreen()
×
320
{
321
    CleanUp();
×
322
}
×
323

324
bool VideoSDL2::SwapBuffers()
×
325
{
326
    SDL_GL_SwapWindow(window);
×
327
    return true;
×
328
}
329

330
bool VideoSDL2::MessageLoop()
×
331
{
332
    SDL_Event ev;
333
    while(SDL_PollEvent(&ev))
×
334
    {
335
        switch(ev.type)
×
336
        {
337
            default: break;
×
338

339
            case SDL_QUIT: return false;
×
340
            case SDL_WINDOWEVENT:
×
341
            {
342
                switch(ev.window.event)
×
343
                {
344
                    case SDL_WINDOWEVENT_RESIZED:
×
345
                    {
NEW
346
                        UpdateCurrentDisplayMode();
×
347
                        VideoMode newSize(ev.window.data1, ev.window.data2);
×
348
                        if(newSize != GetWindowSize())
×
349
                        {
350
                            UpdateCurrentSizes();
×
351
                            CallBack->WindowResized();
×
352
                        }
353
                    }
354
                    break;
×
355
                }
356
            }
357
            break;
×
358

359
            case SDL_KEYDOWN:
×
360
            {
361
                KeyEvent ke;
×
362

363
                switch(ev.key.keysym.sym)
×
364
                {
365
                    default:
×
366
                    {
367
                        // Die 12 F-Tasten
368
                        if(ev.key.keysym.sym >= SDLK_F1 && ev.key.keysym.sym <= SDLK_F12)
×
369
                            ke.kt = static_cast<KeyType>(rttr::enum_cast(KeyType::F1) + ev.key.keysym.sym - SDLK_F1);
×
370
                    }
371
                    break;
×
372
                    case SDLK_RETURN: ke.kt = KeyType::Return; break;
×
373
                    case SDLK_SPACE: ke.kt = KeyType::Space; break;
×
374
                    case SDLK_LEFT: ke.kt = KeyType::Left; break;
×
375
                    case SDLK_RIGHT: ke.kt = KeyType::Right; break;
×
376
                    case SDLK_UP: ke.kt = KeyType::Up; break;
×
377
                    case SDLK_DOWN: ke.kt = KeyType::Down; break;
×
378
                    case SDLK_BACKSPACE: ke.kt = KeyType::Backspace; break;
×
379
                    case SDLK_DELETE: ke.kt = KeyType::Delete; break;
×
380
                    case SDLK_LSHIFT:
×
381
                    case SDLK_RSHIFT: ke.kt = KeyType::Shift; break;
×
382
                    case SDLK_TAB: ke.kt = KeyType::Tab; break;
×
383
                    case SDLK_HOME: ke.kt = KeyType::Home; break;
×
384
                    case SDLK_END: ke.kt = KeyType::End; break;
×
385
                    case SDLK_ESCAPE: ke.kt = KeyType::Escape; break;
×
386
                    case SDLK_PRINTSCREEN: ke.kt = KeyType::Print; break;
×
387
                    // case SDLK_BACKQUOTE: ev.key.keysym.scancode = '^'; break;
388
                    case SDLK_v:
×
389
                        if(SDL_GetModState() & KMOD_CTRL)
×
390
                        {
391
                            HandlePaste();
×
392
                            continue;
×
393
                        }
394
                        break;
×
395
                }
396

397
                setSpecialKeys(ke, SDL_Keymod(ev.key.keysym.mod));
×
398

399
                if(ke.kt != KeyType::Invalid)
×
400
                    CallBack->Msg_KeyDown(ke);
×
401
                else if(ke.alt || ke.ctrl)
×
402
                {
403
                    // Handle shortcuts (CTRL+x, ALT+y)
404
                    // but not possible combinations (ALT+0054)
405
                    const SDL_Keycode keycode = ev.key.keysym.sym;
×
406
                    if(keycode >= 'a' && keycode <= 'z')
×
407
                    {
408
                        ke.kt = KeyType::Char;
×
409
                        ke.c = static_cast<char32_t>(keycode);
×
410
                        CallBack->Msg_KeyDown(ke);
×
411
                    }
412
                }
413
            }
414
            break;
×
415
            case SDL_TEXTINPUT:
×
416
            {
417
                const std::u32string text = s25util::utf8to32(ev.text.text);
×
418
                KeyEvent ke(0);
×
419
                setSpecialKeys(ke);
×
420
                for(char32_t c : text)
×
421
                {
422
                    ke.c = c;
×
423
                    CallBack->Msg_KeyDown(ke);
×
424
                }
425
                break;
×
426
            }
427
            case SDL_MOUSEBUTTONDOWN:
×
428
                mouse_xy.pos = getGuiScale().screenToView(Position(ev.button.x, ev.button.y));
×
429

430
                switch(ev.button.button)
×
431
                {
432
                    case SDL_BUTTON_LEFT:
×
433
                        mouse_xy.ldown = true;
×
434
                        CallBack->Msg_LeftDown(mouse_xy);
×
435
                        break;
×
436

437
                    case SDL_BUTTON_RIGHT:
×
438
                        mouse_xy.rdown = true;
×
439
                        CallBack->Msg_RightDown(mouse_xy);
×
440
                        break;
×
441

442
                    case SDL_BUTTON_MIDDLE:
×
443
                        mouse_xy.mdown = true;
×
444
                        CallBack->Msg_MiddleDown(mouse_xy);
×
445
                        break;
×
446
                }
447
                break;
×
448
            case SDL_MOUSEBUTTONUP:
×
449
                mouse_xy.pos = getGuiScale().screenToView(Position(ev.button.x, ev.button.y));
×
450

451
                switch(ev.button.button)
×
452
                {
453
                    case SDL_BUTTON_LEFT:
×
454
                        mouse_xy.ldown = false;
×
455
                        CallBack->Msg_LeftUp(mouse_xy);
×
456
                        break;
×
457

458
                    case SDL_BUTTON_RIGHT:
×
459
                        mouse_xy.rdown = false;
×
460
                        CallBack->Msg_RightUp(mouse_xy);
×
461
                        break;
×
462

463
                    case SDL_BUTTON_MIDDLE:
×
464
                        mouse_xy.mdown = false;
×
465
                        CallBack->Msg_MiddleUp(mouse_xy);
×
466
                        break;
×
467
                }
468
                break;
×
469
            case SDL_MOUSEWHEEL:
×
470
            {
471
                int y = ev.wheel.y;
×
472
                if(ev.wheel.direction == SDL_MOUSEWHEEL_FLIPPED)
×
473
                    y = -y;
×
474
                if(y > 0)
×
475
                    CallBack->Msg_WheelUp(mouse_xy);
×
476
                else if(y < 0)
×
477
                    CallBack->Msg_WheelDown(mouse_xy);
×
478
            }
479
            break;
×
480
            case SDL_MOUSEMOTION:
×
481
            {
482
                const auto newPos = getGuiScale().screenToView(Position(ev.motion.x, ev.motion.y));
×
483
                // Avoid duplicate events especially when warping the mouse
484
                if(newPos != mouse_xy.pos)
×
485
                {
486
                    mouse_xy.pos = newPos;
×
487
                    CallBack->Msg_MouseMove(mouse_xy);
×
488
                }
489
            }
490
            break;
×
491
            case SDL_FINGERDOWN:
×
492
            {
493
                VideoMode wnSize = GetWindowSize();
×
494
                mouse_xy.pos = getGuiScale().screenToView(Position(static_cast<int>(ev.tfinger.x * wnSize.width),
×
495
                                                                   static_cast<int>(ev.tfinger.y * wnSize.height)));
×
496
                mouse_xy.ldown = true;
×
497
                mouse_xy.num_tfingers++;
×
498
                CallBack->Msg_LeftDown(mouse_xy);
×
499
                break;
×
500
            }
501
            case SDL_FINGERUP:
×
502
            {
503
                VideoMode wnSize = GetWindowSize();
×
504
                mouse_xy.pos = getGuiScale().screenToView(Position(static_cast<int>(ev.tfinger.x * wnSize.width),
×
505
                                                                   static_cast<int>(ev.tfinger.y * wnSize.height)));
×
506
                mouse_xy.ldown = false;
×
507
                CallBack->Msg_LeftUp(mouse_xy);
×
508
                mouse_xy.num_tfingers--; // Dirty way to count leftUp as touch event without extra isTouch bool
×
509
                break;
×
510
            }
511
            case SDL_FINGERMOTION:
×
512
            {
513
                VideoMode wnSize = GetWindowSize();
×
514
                const auto newPos = getGuiScale().screenToView(Position(
×
515
                  static_cast<int>(ev.tfinger.x * wnSize.width), static_cast<int>(ev.tfinger.y * wnSize.height)));
×
516

517
                if(newPos != mouse_xy.pos)
×
518
                {
519
                    mouse_xy.pos = newPos;
×
520
                    CallBack->Msg_MouseMove(mouse_xy);
×
521
                }
522
                break;
×
523
            }
524
            case SDL_MULTIGESTURE:
×
525
            {
526
                if(std::fabs(ev.mgesture.dDist) > 0.001)
×
527
                {
528
                    if(ev.mgesture.dDist > 0)
×
529
                        CallBack->Msg_WheelUp(mouse_xy);
×
530
                    else
531
                        CallBack->Msg_WheelDown(mouse_xy);
×
532
                }
533
                break;
×
534
            }
535
        }
536
    }
537

538
    return true;
×
539
}
540

541
unsigned long VideoSDL2::GetTickCount() const
×
542
{
543
    return SDL_GetTicks();
×
544
}
545

NEW
546
std::vector<VideoMode> VideoSDL2::ListVideoModes() const
×
547
{
548
    int display = SDL_GetWindowDisplayIndex(window);
×
549
    if(display < 0)
×
550
        display = 0;
×
NEW
551
    std::vector<VideoMode> video_modes;
×
UNCOV
552
    for(int i = SDL_GetNumDisplayModes(display) - 1; i >= 0; --i)
×
553
    {
554
        SDL_DisplayMode mode;
555
        if(SDL_GetDisplayMode(display, i, &mode) != 0)
×
NEW
556
            PrintError();
×
557
        else
558
        {
559
            VideoMode vm(mode.w, mode.h);
×
560
            if(!helpers::contains(video_modes, vm))
×
561
                video_modes.push_back(vm);
×
562
        }
563
    }
NEW
564
    return video_modes;
×
565
}
566

567
OpenGL_Loader_Proc VideoSDL2::GetLoaderFunction() const
×
568
{
569
#if RTTR_OGL_GL4ES
570
    return gl4es_GetProcAddress;
571
#else
572
    return SDL_GL_GetProcAddress;
×
573
#endif
574
}
575

576
void VideoSDL2::SetMousePos(Position pos)
×
577
{
578
    const auto screenPos = getGuiScale().viewToScreen(pos);
×
579
    mouse_xy.pos = pos;
×
580
    SDL_WarpMouseInWindow(window, screenPos.x, screenPos.y);
×
581
}
×
582

583
KeyEvent VideoSDL2::GetModKeyState() const
×
584
{
585
    KeyEvent ke;
×
586
    setSpecialKeys(ke);
×
587
    return ke;
×
588
}
589

590
void* VideoSDL2::GetMapPointer() const
×
591
{
592
#ifdef WIN32
593
    SDL_SysWMinfo wmInfo;
594
    SDL_VERSION(&wmInfo.version);
595
    SDL_GetWindowWMInfo(window, &wmInfo);
596
    // return (void*)wmInfo.info.win.window;
597
    return (void*)wmInfo.info.win.window;
598
#else
599
    return nullptr;
×
600
#endif
601
}
602

603
void VideoSDL2::MoveWindowToCenter()
×
604
{
NEW
605
    SDL_PumpEvents(); // Let window system run update events/initialization
×
NEW
606
    const int display = window ? std::max(0, SDL_GetWindowDisplayIndex(window)) : 0;
×
607

608
    SDL_Rect usableBounds;
NEW
609
    CHECK_SDL(SDL_GetDisplayUsableBounds(display, &usableBounds));
×
610
    int top, left, bottom, right;
NEW
611
    if(!CHECK_SDL(SDL_GetWindowBordersSize(window, &top, &left, &bottom, &right)))
×
612
        top = left = bottom = right = 0;
×
NEW
613
    auto wndOuterSize = GetWindowSize();
×
NEW
614
    wndOuterSize.width += left + right;
×
NEW
615
    wndOuterSize.height += top + bottom;
×
NEW
616
    if(usableBounds.w < wndOuterSize.width || usableBounds.h < wndOuterSize.height)
×
617
    {
NEW
618
        SDL_SetWindowSize(window, usableBounds.w - left - right, usableBounds.h - top - bottom);
×
619
        UpdateCurrentSizes();
×
NEW
620
        wndOuterSize.width = usableBounds.w;
×
NEW
621
        wndOuterSize.height = usableBounds.h;
×
622
    }
NEW
623
    const int x = usableBounds.x + (usableBounds.w - wndOuterSize.width) / 2 + left;
×
NEW
624
    const int y = usableBounds.y + (usableBounds.h - wndOuterSize.height) / 2 + top;
×
NEW
625
    SDL_SetWindowPosition(window, x, y);
×
NEW
626
}
×
627

NEW
628
void VideoSDL2::UpdateCurrentDisplayMode()
×
629
{
NEW
630
    RTTR_Assert(window);
×
NEW
631
    const auto flags = SDL_GetWindowFlags(window);
×
NEW
632
    if(flags & SDL_WINDOW_FULLSCREEN)
×
NEW
633
        displayMode_ = DisplayMode::Fullscreen;
×
NEW
634
    else if((flags & SDL_WINDOW_BORDERLESS) != 0)
×
NEW
635
        displayMode_ = DisplayMode::BorderlessWindow;
×
636
    else
NEW
637
        displayMode_ = DisplayMode::Windowed;
×
NEW
638
    displayMode_.resizeable = (flags & SDL_WINDOW_RESIZABLE) != 0;
×
UNCOV
639
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc