1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
/*
* The Mana Client
* Copyright (C) 2004-2009 The Mana World Development Team
* Copyright (C) 2009-2012 The Mana Developers
*
* This file is part of The Mana Client.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "video.h"
#include "log.h"
#include "sdlgraphics.h"
#include "utils/stringutils.h"
#ifdef USE_OPENGL
#include "openglgraphics.h"
#endif
#include <algorithm>
int VideoSettings::scale() const
{
if (userScale == 0)
return autoScale();
return std::clamp(userScale, 1, maxScale());
}
int VideoSettings::autoScale() const
{
// Automatic scaling factor based on at least 800x600 logical resolution
return std::max(1, std::min(width / 800, height / 600));
}
int VideoSettings::maxScale() const
{
// Logical resolution needs to stay at least 640x480
return std::max(1, std::min(width / 640, height / 480));
}
Video::~Video()
{
mGraphics.reset(); // reset graphics first
if (mWindow)
SDL_DestroyWindow(mWindow);
}
Graphics *Video::initialize(const VideoSettings &settings)
{
mSettings = settings;
if (!initDisplayModes())
{
logger->log("Failed to initialize display modes: %s", SDL_GetError());
}
SDL_DisplayMode displayMode;
if (mSettings.windowMode == WindowMode::Fullscreen)
{
SDL_DisplayMode requestedMode;
requestedMode.format = 0;
requestedMode.w = mSettings.width;
requestedMode.h = mSettings.height;
requestedMode.refresh_rate = 0;
requestedMode.driverdata = nullptr;
if (SDL_GetClosestDisplayMode(mSettings.display, &requestedMode, &displayMode) == nullptr)
{
logger->log("SDL_GetClosestDisplayMode failed: %s, falling back to borderless mode", SDL_GetError());
mSettings.windowMode = WindowMode::WindowedFullscreen;
}
}
int windowFlags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI;
const char *videoMode = "windowed";
switch (mSettings.windowMode)
{
case WindowMode::Windowed:
break;
case WindowMode::Fullscreen:
windowFlags |= SDL_WINDOW_FULLSCREEN;
videoMode = "fullscreen";
break;
case WindowMode::WindowedFullscreen:
windowFlags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
videoMode = "windowed fullscreen";
break;
}
if (mSettings.openGL)
windowFlags |= SDL_WINDOW_OPENGL;
logger->log("Setting video mode %dx%d %s",
mSettings.width,
mSettings.height,
videoMode);
mWindow = SDL_CreateWindow("Mana",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
mSettings.width,
mSettings.height,
windowFlags);
if (!mWindow)
{
logger->error(strprintf("Failed to create window: %s",
SDL_GetError()));
return nullptr;
}
SDL_SetWindowMinimumSize(mWindow, 640, 480);
if (mSettings.windowMode == WindowMode::Fullscreen)
{
if (SDL_SetWindowDisplayMode(mWindow, &displayMode) != 0)
{
logger->log("SDL_SetWindowDisplayMode failed: %s", SDL_GetError());
}
}
// Make sure the resolution is reflected in current settings
SDL_GetWindowSize(mWindow, &mSettings.width, &mSettings.height);
#ifdef USE_OPENGL
if (mSettings.openGL)
{
mGraphics = OpenGLGraphics::create(mWindow, mSettings);
if (!mGraphics)
{
logger->log("Failed to create OpenGL context, falling back to SDL renderer: %s",
SDL_GetError());
mSettings.openGL = false;
}
}
#endif
if (!mGraphics)
mGraphics = SDLGraphics::create(mWindow, mSettings);
mGraphics->updateSize(mSettings.width, mSettings.height, mSettings.scale());
return mGraphics.get();
}
bool Video::apply(const VideoSettings &settings)
{
if (mSettings == settings)
return true;
// When changing to fullscreen mode, we set the display mode first
if (settings.windowMode == WindowMode::Fullscreen)
{
SDL_DisplayMode displayMode;
if (SDL_GetWindowDisplayMode(mWindow, &displayMode) != 0)
{
logger->error(strprintf("SDL_GetCurrentDisplayMode failed: %s", SDL_GetError()));
return false;
}
if (displayMode.w != settings.width || displayMode.h != settings.height)
{
#ifdef __APPLE__
// Workaround SDL2 issue when switching display modes while already
// fullscreen on macOS (tested as of SDL 2.30.0).
if (SDL_GetWindowFlags(mWindow) & SDL_WINDOW_FULLSCREEN)
SDL_SetWindowFullscreen(mWindow, 0);
#endif
displayMode.w = settings.width;
displayMode.h = settings.height;
if (SDL_SetWindowDisplayMode(mWindow, &displayMode) != 0)
{
logger->error(strprintf("SDL_SetWindowDisplayMode failed: %s", SDL_GetError()));
return false;
}
}
}
int windowFlags = 0;
switch (settings.windowMode)
{
case WindowMode::Windowed:
break;
case WindowMode::WindowedFullscreen:
windowFlags = SDL_WINDOW_FULLSCREEN_DESKTOP;
break;
case WindowMode::Fullscreen:
windowFlags = SDL_WINDOW_FULLSCREEN;
break;
}
if (SDL_SetWindowFullscreen(mWindow, windowFlags) != 0)
{
logger->error(strprintf("SDL_SetWindowFullscreen failed: %s", SDL_GetError()));
return false;
}
if (settings.windowMode == WindowMode::Windowed &&
(settings.width != mSettings.width ||
settings.height != mSettings.height)) {
#ifdef __APPLE__
// Workaround SDL2 issue when setting the window size on a window
// which the user has put in fullscreen. Unfortunately, this mode can't
// be distinguished from a maximized window (tested as of SDL 2.30.0).
if (!(SDL_GetWindowFlags(mWindow) & SDL_WINDOW_MAXIMIZED))
#endif
SDL_SetWindowSize(mWindow, settings.width, settings.height);
}
mSettings = settings;
SDL_GetWindowSize(mWindow, &mSettings.width, &mSettings.height);
mGraphics->setVSync(mSettings.vsync);
mGraphics->updateSize(mSettings.width, mSettings.height, mSettings.scale());
return true;
}
void Video::windowSizeChanged(int width, int height)
{
mSettings.width = width;
mSettings.height = height;
mGraphics->updateSize(width, height, mSettings.scale());
}
bool Video::initDisplayModes()
{
const int displayIndex = mSettings.display;
SDL_DisplayMode mode;
if (SDL_GetDesktopDisplayMode(displayIndex, &mode) != 0)
return false;
mDesktopDisplayMode.width = mode.w;
mDesktopDisplayMode.height = mode.h;
// Get available fullscreen/hardware modes
const int numModes = SDL_GetNumDisplayModes(displayIndex);
for (int i = 0; i < numModes; i++)
{
if (SDL_GetDisplayMode(displayIndex, i, &mode) != 0)
return false;
// Skip the unreasonably small modes
if (mode.w < 640 || mode.h < 480)
continue;
// Only list each resolution once
// (we currently don't support selecting the refresh rate)
if (std::find_if(mDisplayModes.cbegin(),
mDisplayModes.cend(),
[&mode](const DisplayMode &other) {
return mode.w == other.width && mode.h == other.height;
}) != mDisplayModes.cend())
continue;
mDisplayModes.push_back(DisplayMode { mode.w, mode.h });
}
return true;
}
|