ManaPlus
sdlhelper.cpp
Go to the documentation of this file.
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 
45 {
46  /* Get available fullscreen/hardware modes */
47  SDL_Rect *const *const modes = SDL_ListModes(nullptr,
48  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  if (modes == static_cast<SDL_Rect **>(nullptr))
61  {
62  logger->log1("No modes available");
63  return false;
64  }
65  else if (modes == reinterpret_cast<SDL_Rect **>(-1))
66  {
67  logger->log1("All resolutions available");
68  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 void SDL::SetWindowTitle(const SDL_Surface *const window A_UNUSED,
86  const char *const title)
87 {
88  SDL_WM_SetCaption(title, nullptr);
89 }
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 SDL_Thread *SDL::createThread(int (SDLCALL *fn)(void *),
119  const char *const name A_UNUSED,
120  void *const data)
121 {
122  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 
188 {
189 }
190 
191 void SDL::setLogLevel(const int level A_UNUSED)
192 {
193 }
194 
195 void SDL::WaitThread(SDL_Thread *const thread)
196 {
197  if (thread != nullptr && SDL_GetThreadID(thread) != 0U)
198  SDL_WaitThread(thread, nullptr);
199 }
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
#define CAST_S32
Definition: cast.h:30
void log(const char *const log_text,...)
Definition: logger.cpp:269
void log1(const char *const log_text)
Definition: logger.cpp:238
void setEnv(const char *const name, const char *const value)
Definition: env.cpp:60
if(!vert) return
#define PRAGMA48(str)
Definition: localconsts.h:199
#define A_UNUSED
Definition: localconsts.h:160
Logger * logger
Definition: logger.cpp:89
uint32_t data
bool info(InputEvent &event)
Definition: commands.cpp:57
std::string toString(T const &value)
converts any type to a string
Definition: catch.hpp:1774
SDL_Thread * createThread(int(*fn)(void *), const char *const name, void *const data)
Definition: sdlhelper.cpp:118
void SetWindowTitle(const SDL_Surface *const window, const char *const title)
Definition: sdlhelper.cpp:85
void initLogger()
Definition: sdlhelper.cpp:187
void WaitThread(SDL_Thread *const thread)
Definition: sdlhelper.cpp:195
bool PollEvent(SDL_Event *event)
Definition: sdlhelper.cpp:201
bool getWindowWMInfo(const SDL_Surface *const window, SDL_SysWMinfo *const info)
Definition: sdlhelper.cpp:112
void setLogLevel(const int level)
Definition: sdlhelper.cpp:191
void SetWindowIcon(const SDL_Surface *const window, SDL_Surface *const icon)
Definition: sdlhelper.cpp:91
void makeCurrentContext(void *const context)
Definition: sdlhelper.cpp:182
void grabInput(const SDL_Surface *const window, const bool grab)
Definition: sdlhelper.cpp:97
void setGamma(const SDL_Surface *const window, const float gamma)
Definition: sdlhelper.cpp:102
void setVsync(const int val)
Definition: sdlhelper.cpp:107
bool getAllVideoModes(StringVect &modeList)
Definition: sdlhelper.cpp:44
void allowScreenSaver(const bool allow)
Definition: sdlhelper.cpp:210
void * createGLContext(SDL_Surface *const window, const int major, const int minor, const int profile)
Definition: sdlhelper.cpp:174
std::vector< std::string > StringVect
Definition: stringvector.h:29