GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/utils/sdlhelper.cpp Lines: 17 70 24.3 %
Date: 2017-11-29 Branches: 5 44 11.4 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2013-2017  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
#ifndef USE_SDL2
22
23
#include "utils/sdlhelper.h"
24
25
#include "logger.h"
26
27
#include "utils/cast.h"
28
#include "utils/env.h"
29
#include "utils/stringutils.h"
30
31
#if defined(USE_X11) && defined(USE_OPENGL)
32
#include "utils/glxhelper.h"
33
#endif  // defined(USE_X11) && defined(USE_OPENGL)
34
35
PRAGMA48(GCC diagnostic push)
36
PRAGMA48(GCC diagnostic ignored "-Wshadow")
37
#include <SDL_events.h>
38
#include <SDL_syswm.h>
39
PRAGMA48(GCC diagnostic pop)
40
41
#include "debug.h"
42
43
4
bool SDL::getAllVideoModes(StringVect &modeList)
44
{
45
    /* Get available fullscreen/hardware modes */
46
    SDL_Rect *const *const modes = SDL_ListModes(nullptr,
47
4
        SDL_FULLSCREEN | SDL_HWSURFACE);
48
49
#ifdef ANDROID
50
    const std::string modeString =
51
        toString(CAST_S32(modes[0]->w)).append("x")
52
       .append(toString(CAST_S32(modes[0]->h)));
53
    logger->log("support mode: " + modeString);
54
    modeList.push_back(modeString);
55
    return true;
56
#else  // ANDROID
57
58
    /* Check which modes are available */
59
4
    if (modes == static_cast<SDL_Rect **>(nullptr))
60
    {
61
        logger->log1("No modes available");
62
        return false;
63
    }
64
4
    else if (modes == reinterpret_cast<SDL_Rect **>(-1))
65
    {
66
4
        logger->log1("All resolutions available");
67
4
        return true;
68
    }
69
    else
70
    {
71
        for (int i = 0; modes[i] != nullptr; ++ i)
72
        {
73
            const std::string modeString =
74
                toString(CAST_S32(modes[i]->w)).append("x")
75
                .append(toString(CAST_S32(modes[i]->h)));
76
            logger->log("support mode: " + modeString);
77
            modeList.push_back(modeString);
78
        }
79
        return true;
80
    }
81
#endif  // ANDROID
82
}
83
84
8
void SDL::SetWindowTitle(const SDL_Surface *const window A_UNUSED,
85
                         const char *const title)
86
{
87
8
    SDL_WM_SetCaption(title, nullptr);
88
8
}
89
90
void SDL::SetWindowIcon(const SDL_Surface *const window A_UNUSED,
91
                        SDL_Surface *const icon)
92
{
93
    SDL_WM_SetIcon(icon, nullptr);
94
}
95
96
void SDL::grabInput(const SDL_Surface *const window A_UNUSED, const bool grab)
97
{
98
    SDL_WM_GrabInput(grab ? SDL_GRAB_ON : SDL_GRAB_OFF);
99
}
100
101
void SDL::setGamma(const SDL_Surface *const window A_UNUSED, const float gamma)
102
{
103
    SDL_SetGamma(gamma, gamma, gamma);
104
}
105
106
void SDL::setVsync(const int val)
107
{
108
    SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, val);
109
}
110
111
bool SDL::getWindowWMInfo(const SDL_Surface *const window A_UNUSED,
112
                          SDL_SysWMinfo *const info)
113
{
114
    return SDL_GetWMInfo(info) != 0;
115
}
116
117
3
SDL_Thread *SDL::createThread(int (SDLCALL *fn)(void *),
118
                              const char *const name A_UNUSED,
119
                              void *const data)
120
{
121
3
    return SDL_CreateThread(fn, data);
122
}
123
124
#if defined(USE_X11) && defined(USE_OPENGL)
125
void *SDL::createGLContext(SDL_Surface *const window A_UNUSED,
126
                           const int major,
127
                           const int minor,
128
                           const int profile)
129
{
130
    SDL_SysWMinfo info;
131
    SDL_VERSION(&info.version);
132
    SDL_GetWMInfo(&info);
133
    void *context = GlxHelper::createContext(info.info.x11.window,
134
        info.info.x11.display, major, minor, profile);
135
    if (!context && (major > 3 || (major == 3 && minor > 3)))
136
    {
137
        logger->log("Try fallback to OpenGL 3.3 core context");
138
        context = GlxHelper::createContext(info.info.x11.window,
139
            info.info.x11.display, 3, 3, profile);
140
        if (!context && profile == 0x01)
141
        {
142
            logger->log("Try fallback to OpenGL 3.3 compatibility context");
143
            context = GlxHelper::createContext(info.info.x11.window,
144
                info.info.x11.display, 3, 3, 0x02);
145
        }
146
    }
147
    if (!context && (major > 3 || (major == 3 && minor > 0)))
148
    {
149
        logger->log("Try fallback to OpenGL 3.0 core context");
150
        context = GlxHelper::createContext(info.info.x11.window,
151
            info.info.x11.display, 3, 0, profile);
152
    }
153
    if (!context && (major > 2 || (major == 2 && minor > 1)))
154
    {
155
        logger->log("Try fallback to OpenGL 2.1 compatibility context");
156
        context = GlxHelper::createContext(info.info.x11.window,
157
            info.info.x11.display, 2, 1, 0x02);
158
    }
159
    return context;
160
}
161
162
void SDL::makeCurrentContext(void *const context)
163
{
164
    SDL_SysWMinfo info;
165
    SDL_VERSION(&info.version);
166
    SDL_GetWMInfo(&info);
167
    GlxHelper::makeCurrent(info.info.x11.window,
168
        info.info.x11.display,
169
        context);
170
}
171
#else  // defined(USE_X11) && defined(USE_OPENGL)
172
173
void *SDL::createGLContext(SDL_Surface *const window A_UNUSED,
174
                           const int major A_UNUSED,
175
                           const int minor A_UNUSED,
176
                           const int profile A_UNUSED)
177
{
178
    return nullptr;
179
}
180
181
void SDL::makeCurrentContext(void *const context A_UNUSED)
182
{
183
}
184
#endif  // defined(USE_X11) && defined(USE_OPENGL)
185
186
2
void SDL::initLogger()
187
{
188
2
}
189
190
void SDL::setLogLevel(const int level A_UNUSED)
191
{
192
}
193
194
8
void SDL::WaitThread(SDL_Thread *const thread)
195
{
196

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