GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/utils/sdl2helper.cpp Lines: 38 119 31.9 %
Date: 2019-08-19 Branches: 20 72 27.8 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2013-2019  The ManaPlus Developers
4
 *
5
 *  This file is part of The ManaPlus Client.
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#ifdef USE_SDL2
22
23
#include "utils/sdl2helper.h"
24
25
#include "logger.h"
26
27
#include "utils/foreach.h"
28
#include "utils/sdl2logger.h"
29
#include "utils/stringutils.h"
30
31
#include <algorithm>
32
33
PRAGMA48(GCC diagnostic push)
34
PRAGMA48(GCC diagnostic ignored "-Wshadow")
35
#include <SDL_events.h>
36
#include <SDL_hints.h>
37
#include <SDL_render.h>
38
#include <SDL_syswm.h>
39
PRAGMA48(GCC diagnostic pop)
40
41
#include "debug.h"
42
43
2
bool SDL::getAllVideoModes(StringVect &modeList)
44
{
45
4
    std::set<std::string> modes;
46
2
    const int numDisplays = SDL_GetNumVideoDisplays();
47
6
    for (int display = 0; display < numDisplays; display ++)
48
    {
49
2
        const int numModes = SDL_GetNumDisplayModes(display);
50
2
        if (numModes > 0)
51
        {
52
6
            for (int f = 0; f < numModes; f ++)
53
            {
54
                SDL_DisplayMode mode;
55
2
                SDL_GetDisplayMode(display, f, &mode);
56
2
                const int w = mode.w;
57
2
                const int h = mode.h;
58
2
                logger->log("%dx%dx%d", w, h, mode.refresh_rate);
59
6
                modes.insert(strprintf("%dx%d", w, h));
60
            }
61
        }
62
    }
63
4
    FOR_EACH (std::set<std::string>::const_iterator, it, modes)
64
2
        modeList.push_back(*it);
65
4
    return true;
66
}
67
68
4
void SDL::SetWindowTitle(SDL_Window *const window, const char *const title)
69
{
70
4
    SDL_SetWindowTitle(window, title);
71
4
}
72
73
void SDL::SetWindowIcon(SDL_Window *const window, SDL_Surface *const icon)
74
{
75
    SDL_SetWindowIcon(window, icon);
76
}
77
78
void SDL::grabInput(SDL_Window *const window, const bool grab)
79
{
80
    SDL_SetWindowGrab(window, grab ? SDL_TRUE : SDL_FALSE);
81
}
82
83
void SDL::setGamma(SDL_Window *const window, const float gamma)
84
{
85
    SDL_SetWindowBrightness(window, gamma);
86
}
87
88
void SDL::setVsync(const int val)
89
{
90
    SDL_GL_SetSwapInterval(val);
91
}
92
93
bool SDL::getWindowWMInfo(SDL_Window *const window, SDL_SysWMinfo *const info)
94
{
95
    return SDL_GetWindowWMInfo(window, info);
96
}
97
98
2
SDL_Thread *SDL::createThread(SDL_ThreadFunction fn,
99
                              const char *restrict const name,
100
                              void *restrict const data)
101
{
102
2
    return SDL_CreateThread(fn, name, data);
103
}
104
105
void *SDL::createGLContext(SDL_Window *const window,
106
                           const int major,
107
                           const int minor,
108
                           const int profile)
109
{
110
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, major);
111
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, minor);
112
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, profile);
113
//    SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);
114
    SDL_ClearError();
115
    void *context = SDL_GL_CreateContext(window);
116
    if (context == nullptr)
117
    {
118
        logger->log("Error to switch to context %d.%d: %s",
119
            major,
120
            minor,
121
            SDL_GetError());
122
    }
123
    if (context == nullptr && (major > 3 || (major == 3 && minor > 3)))
124
    {
125
        logger->log("Try fallback to OpenGL 3.3 context");
126
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
127
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
128
        SDL_ClearError();
129
        context = SDL_GL_CreateContext(window);
130
        if (context == nullptr)
131
        {
132
            logger->log("Error to switch to context 3.3: %s",
133
                SDL_GetError());
134
        }
135
        if (context == nullptr && profile == 0x01)
136
        {
137
            logger->log("Try fallback to OpenGL 3.3 compatibility context");
138
            SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
139
            SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
140
            SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, 0x02);
141
            SDL_ClearError();
142
            context = SDL_GL_CreateContext(window);
143
            if (context == nullptr)
144
            {
145
                logger->log("Error to switch to compatibility context 3.3: %s",
146
                    SDL_GetError());
147
            }
148
        }
149
    }
150
    if (context == nullptr && (major > 3 || (major == 3 && minor > 0)))
151
    {
152
        logger->log("Error to switch to core context %d.%d: %s",
153
            major,
154
            minor,
155
            SDL_GetError());
156
        logger->log("Try fallback to OpenGL 3.0 core context");
157
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
158
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
159
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, profile);
160
        context = SDL_GL_CreateContext(window);
161
        if (context == nullptr)
162
        {
163
            logger->log("Error to switch to core context 3.0: %s",
164
                SDL_GetError());
165
        }
166
    }
167
    if (context == nullptr && (major > 2 || (major == 2 && minor > 1)))
168
    {
169
        logger->log("Try fallback to OpenGL 2.1 compatibility context");
170
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
171
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
172
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, 0x02);
173
        context = SDL_GL_CreateContext(window);
174
        if (context == nullptr)
175
        {
176
            logger->log("Error to switch to compatibility context 2.1: %s",
177
                SDL_GetError());
178
        }
179
    }
180
    if (context == nullptr)
181
    {
182
        logger->log("Cant find working context.");
183
    }
184
    return context;
185
}
186
187
void SDL::makeCurrentContext(void *const context A_UNUSED)
188
{
189
}
190
191
1
void SDL::initLogger()
192
{
193
1
    SDL2Logger::init();
194
1
}
195
196
void SDL::setLogLevel(const int level)
197
{
198
    SDL2Logger::setLogLevel(level);
199
}
200
201
5
void SDL::WaitThread(SDL_Thread *const thread)
202
{
203
5
    if (thread != nullptr)
204
2
        SDL_WaitThread(thread, nullptr);
205
5
}
206
207
bool SDL::PollEvent(SDL_Event *event)
208
{
209
    SDL_PumpEvents();
210
    return SDL_PeepEvents(event,
211
        1,
212
        SDL_GETEVENT,
213
        SDL_FIRSTEVENT,
214
        SDL_LASTEVENT) > 0;
215
}
216
217
void SDL::allowScreenSaver(const bool allow)
218
{
219
    if (allow)
220
    {
221
#if SDL_VERSION_ATLEAST(2, 0, 2)
222
        SDL_SetHintWithPriority(SDL_HINT_VIDEO_ALLOW_SCREENSAVER,
223
            "1",
224
            SDL_HINT_NORMAL);
225
#endif  // SDL_VERSION_ATLEAST(2, 0, 2)
226
        SDL_EnableScreenSaver();
227
    }
228
    else
229
    {
230
#if SDL_VERSION_ATLEAST(2, 0, 2)
231
        SDL_SetHintWithPriority(SDL_HINT_VIDEO_ALLOW_SCREENSAVER,
232
            "0",
233
            SDL_HINT_NORMAL);
234
#endif  // SDL_VERSION_ATLEAST(2, 0, 2)
235
        SDL_DisableScreenSaver();
236
    }
237
}
238
239
2
void SDL::getRenderers(StringVect &list,
240
                       const std::string &currentRenderer)
241
{
242
    SDL_RendererInfo info;
243
2
    const int num = SDL_GetNumRenderDrivers();
244
14
    for (int f = 0; f < num; f ++)
245
    {
246
6
        if (!SDL_GetRenderDriverInfo(f, &info))
247
30
            list.push_back(info.name);
248
    }
249
2
    if (!currentRenderer.empty())
250
    {
251
        bool found(false);
252
        FOR_EACH (StringVectCIter, it, list)
253
        {
254
            if (*it == currentRenderer)
255
            {
256
                found = true;
257
                break;
258
            }
259
        }
260
        if (!found)
261
            list.push_back(currentRenderer);
262
    }
263
4
    std::sort(list.begin(), list.end());
264
2
}
265
266
311
void SDL::setRendererHint(const std::string &driver)
267
{
268
311
    if (!driver.empty())
269
    {
270
        SDL_SetHintWithPriority(SDL_HINT_RENDER_DRIVER,
271
            driver.c_str(),
272
            SDL_HINT_NORMAL);
273
    }
274
311
}
275
276
#endif  // USE_SDL2