GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/utils/sdlhelper.cpp Lines: 17 71 23.9 %
Date: 2021-03-17 Branches: 7 46 15.2 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2013-2019  The ManaPlus Developers
4
 *  Copyright (C) 2019-2021  Andrei Karas
5
 *
6
 *  This file is part of The ManaPlus Client.
7
 *
8
 *  This program is free software; you can redistribute it and/or modify
9
 *  it under the terms of the GNU General Public License as published by
10
 *  the Free Software Foundation; either version 2 of the License, or
11
 *  any later version.
12
 *
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
#ifndef USE_SDL2
23
24
#include "utils/sdlhelper.h"
25
26
#include "logger.h"
27
28
#include "utils/cast.h"
29
#include "utils/env.h"
30
#include "utils/stringutils.h"
31
32
#if defined(USE_X11) && defined(USE_OPENGL)
33
#include "utils/glxhelper.h"
34
#endif  // defined(USE_X11) && defined(USE_OPENGL)
35
36
PRAGMA48(GCC diagnostic push)
37
PRAGMA48(GCC diagnostic ignored "-Wshadow")
38
#include <SDL_events.h>
39
#include <SDL_syswm.h>
40
PRAGMA48(GCC diagnostic pop)
41
42
#include "debug.h"
43
44
2
bool SDL::getAllVideoModes(StringVect &modeList)
45
{
46
    /* Get available fullscreen/hardware modes */
47
    SDL_Rect *const *const modes = SDL_ListModes(nullptr,
48
2
        SDL_FULLSCREEN | SDL_HWSURFACE);
49
50
#ifdef ANDROID
51
    const std::string modeString =
52
        toString(CAST_S32(modes[0]->w)).append("x")
53
       .append(toString(CAST_S32(modes[0]->h)));
54
    logger->log("support mode: " + modeString);
55
    modeList.push_back(modeString);
56
    return true;
57
#else  // ANDROID
58
59
    /* Check which modes are available */
60
2
    if (modes == static_cast<SDL_Rect **>(nullptr))
61
    {
62
        logger->log1("No modes available");
63
        return false;
64
    }
65
2
    else if (modes == reinterpret_cast<SDL_Rect **>(-1))
66
    {
67
2
        logger->log1("All resolutions available");
68
2
        return true;
69
    }
70
    else
71
    {
72
        for (int i = 0; modes[i] != nullptr; ++ i)
73
        {
74
            const std::string modeString =
75
                toString(CAST_S32(modes[i]->w)).append("x")
76
                .append(toString(CAST_S32(modes[i]->h)));
77
            logger->log("support mode: " + modeString);
78
            modeList.push_back(modeString);
79
        }
80
        return true;
81
    }
82
#endif  // ANDROID
83
}
84
85
4
void SDL::SetWindowTitle(const SDL_Surface *const window A_UNUSED,
86
                         const char *const title)
87
{
88
4
    SDL_WM_SetCaption(title, nullptr);
89
4
}
90
91
void SDL::SetWindowIcon(const SDL_Surface *const window A_UNUSED,
92
                        SDL_Surface *const icon)
93
{
94
    SDL_WM_SetIcon(icon, nullptr);
95
}
96
97
void SDL::grabInput(const SDL_Surface *const window A_UNUSED, const bool grab)
98
{
99
    SDL_WM_GrabInput(grab ? SDL_GRAB_ON : SDL_GRAB_OFF);
100
}
101
102
void SDL::setGamma(const SDL_Surface *const window A_UNUSED, const float gamma)
103
{
104
    SDL_SetGamma(gamma, gamma, gamma);
105
}
106
107
void SDL::setVsync(const int val)
108
{
109
    SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, val);
110
}
111
112
bool SDL::getWindowWMInfo(const SDL_Surface *const window A_UNUSED,
113
                          SDL_SysWMinfo *const info)
114
{
115
    return SDL_GetWMInfo(info) != 0;
116
}
117
118
2
SDL_Thread *SDL::createThread(int (SDLCALL *fn)(void *),
119
                              const char *const name A_UNUSED,
120
                              void *const data)
121
{
122
2
    return SDL_CreateThread(fn, data);
123
}
124
125
#if defined(USE_X11) && defined(USE_OPENGL)
126
void *SDL::createGLContext(SDL_Surface *const window A_UNUSED,
127
                           const int major,
128
                           const int minor,
129
                           const int profile)
130
{
131
    SDL_SysWMinfo info;
132
    SDL_VERSION(&info.version)
133
    SDL_GetWMInfo(&info);
134
    void *context = GlxHelper::createContext(info.info.x11.window,
135
        info.info.x11.display, major, minor, profile);
136
    if (!context && (major > 3 || (major == 3 && minor > 3)))
137
    {
138
        logger->log("Try fallback to OpenGL 3.3 core context");
139
        context = GlxHelper::createContext(info.info.x11.window,
140
            info.info.x11.display, 3, 3, profile);
141
        if (!context && profile == 0x01)
142
        {
143
            logger->log("Try fallback to OpenGL 3.3 compatibility context");
144
            context = GlxHelper::createContext(info.info.x11.window,
145
                info.info.x11.display, 3, 3, 0x02);
146
        }
147
    }
148
    if (!context && (major > 3 || (major == 3 && minor > 0)))
149
    {
150
        logger->log("Try fallback to OpenGL 3.0 core context");
151
        context = GlxHelper::createContext(info.info.x11.window,
152
            info.info.x11.display, 3, 0, profile);
153
    }
154
    if (!context && (major > 2 || (major == 2 && minor > 1)))
155
    {
156
        logger->log("Try fallback to OpenGL 2.1 compatibility context");
157
        context = GlxHelper::createContext(info.info.x11.window,
158
            info.info.x11.display, 2, 1, 0x02);
159
    }
160
    return context;
161
}
162
163
void SDL::makeCurrentContext(void *const context)
164
{
165
    SDL_SysWMinfo info;
166
    SDL_VERSION(&info.version)
167
    SDL_GetWMInfo(&info);
168
    GlxHelper::makeCurrent(info.info.x11.window,
169
        info.info.x11.display,
170
        context);
171
}
172
#else  // defined(USE_X11) && defined(USE_OPENGL)
173
174
void *SDL::createGLContext(SDL_Surface *const window A_UNUSED,
175
                           const int major A_UNUSED,
176
                           const int minor A_UNUSED,
177
                           const int profile A_UNUSED)
178
{
179
    return nullptr;
180
}
181
182
void SDL::makeCurrentContext(void *const context A_UNUSED)
183
{
184
}
185
#endif  // defined(USE_X11) && defined(USE_OPENGL)
186
187
1
void SDL::initLogger()
188
{
189
1
}
190
191
void SDL::setLogLevel(const int level A_UNUSED)
192
{
193
}
194
195
5
void SDL::WaitThread(SDL_Thread *const thread)
196
{
197

5
    if (thread != nullptr && SDL_GetThreadID(thread) != 0U)
198
2
        SDL_WaitThread(thread, nullptr);
199
5
}
200
201
bool SDL::PollEvent(SDL_Event *event)
202
{
203
    SDL_PumpEvents();
204
    return SDL_PeepEvents(event,
205
        1,
206
        SDL_GETEVENT,
207
        SDL_ALLEVENTS) > 0;
208
}
209
210
void SDL::allowScreenSaver(const bool allow)
211
{
212
    if (allow)
213
        setEnv("SDL_VIDEO_ALLOW_SCREENSAVER", "1");
214
    else
215
        setEnv("SDL_VIDEO_ALLOW_SCREENSAVER", "0");
216
}
217
218
#endif  // USE_SDL2