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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
|
/*
* Copyright (C) 2002-2025 by the Widelands Development Team
*
* 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 (at your option) 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 <https://www.gnu.org/licenses/>.
*
*/
#include "graphic/graphic.h"
#include <memory>
#include <SDL_messagebox.h>
#include <SDL_video.h>
#include "base/i18n.h"
#include "base/log.h"
#include "base/wexception.h"
#include "build_info.h"
#include "graphic/animation/animation_manager.h"
#include "graphic/build_texture_atlas.h"
#include "graphic/default_resolution.h"
#include "graphic/gl/initialize.h"
#include "graphic/gl/system_headers.h"
#include "graphic/image.h"
#include "graphic/image_cache.h"
#include "graphic/image_io.h"
#include "graphic/note_graphic_resolution_changed.h"
#include "graphic/render_queue.h"
#include "graphic/rendertarget.h"
#include "graphic/screen.h"
#include "graphic/texture.h"
#include "io/filesystem/layered_filesystem.h"
#include "io/streamwrite.h"
#include "notifications/notifications.h"
#ifndef RESIZABLE_WINDOW
#define SDL_SetWindowResizable(window, resizable)
#endif
Graphic* g_gr;
namespace {
// Sets the icon for the application.
void set_icon(SDL_Window* sdl_window) {
#ifndef _WIN32
const std::string icon_name = "images/logos/wl-ico-128.png";
#else
const std::string icon_name = "images/logos/wl-ico-32.png";
#endif
SDL_Surface* s = load_image_as_sdl_surface(icon_name, g_fs);
SDL_SetWindowIcon(sdl_window, s);
SDL_FreeSurface(s);
}
} // namespace
Graphic::Graphic() {
g_image_cache = new ImageCache();
g_animation_manager = new AnimationManager();
}
/**
* Initialize the SDL video mode.
*/
void Graphic::initialize(const TraceGl& trace_gl,
int display,
int window_mode_w,
int window_mode_h,
bool init_fullscreen,
bool init_maximized) {
window_mode_width_ = window_mode_w;
window_mode_height_ = window_mode_h;
if (SDL_GL_LoadLibrary(nullptr) == -1) {
throw wexception("SDL_GL_LoadLibrary failed: %s", SDL_GetError());
}
log_info("Graphics: Try to set Videomode %dx%d\n", window_mode_width_, window_mode_height_);
if (display < 0 || display >= SDL_GetNumVideoDisplays()) {
// Find the display, the mouse pointer is on.
int mouse_x;
int mouse_y;
SDL_GetGlobalMouseState(&mouse_x, &mouse_y);
display = get_display_at(mouse_x, mouse_y);
}
int window_x = SDL_WINDOWPOS_UNDEFINED;
int window_y = SDL_WINDOWPOS_UNDEFINED;
if (display >= 0 && display < SDL_GetNumVideoDisplays()) {
window_x = window_y = SDL_WINDOWPOS_CENTERED_DISPLAY(display);
}
uint32_t window_flags = SDL_WINDOW_OPENGL;
#ifdef RESIZABLE_WINDOW
window_flags |= SDL_WINDOW_RESIZABLE;
#endif
sdl_window_ = SDL_CreateWindow("Widelands Window", window_x, window_y, window_mode_width_,
window_mode_height_, window_flags);
SDL_SetWindowMinimumSize(sdl_window_, kMinimumResolutionW, kMinimumResolutionH);
GLint max;
// LeakSanitizer reports a memory leak which is triggered somewhere in this function call,
// probably coming from the gaphics drivers
gl_context_ = Gl::initialize(
trace_gl == TraceGl::kYes ? Gl::Trace::kYes : Gl::Trace::kNo, sdl_window_, &max);
max_texture_size_ = static_cast<int>(max);
set_fullscreen(init_fullscreen);
if (init_maximized) {
set_maximized(true);
}
resolution_changed();
SDL_SetWindowTitle(sdl_window_, ("Widelands " + build_ver_details()).c_str());
set_icon(sdl_window_);
SDL_GL_SwapWindow(sdl_window_);
/* Information about the video capabilities. */
const char* drv = SDL_GetCurrentVideoDriver();
log_info("**** GRAPHICS REPORT ****\n");
#ifdef WL_USE_GLVND
log_info("VIDEO DRIVER GLVND %s\n", drv ? drv : "NONE");
#else
log_info("VIDEO DRIVER %s\n", drv ? drv : "NONE");
#endif
SDL_DisplayMode disp_mode;
for (int i = 0; i < SDL_GetNumVideoDisplays(); ++i) {
if (SDL_GetCurrentDisplayMode(i, &disp_mode) == 0) {
log_info("Display #%d: %dx%d @ %dhz %s\n", i, disp_mode.w, disp_mode.h,
disp_mode.refresh_rate, SDL_GetPixelFormatName(disp_mode.format));
} else {
log_warn("Couldn't get display mode for display #%d: %s\n", i, SDL_GetError());
}
}
log_info("**** END GRAPHICS REPORT ****\n");
}
void Graphic::rebuild_texture_atlas() const {
verb_log_info("Rebuilding texture atlas");
std::map<std::string, std::unique_ptr<Texture>> textures_in_atlas;
auto texture_atlases = build_texture_atlas(max_texture_size_, &textures_in_atlas);
g_image_cache->fill_with_texture_atlases(
std::move(texture_atlases), std::move(textures_in_atlas));
verb_log_info("Texture atlas rebuilt");
}
Graphic::~Graphic() {
delete g_animation_manager;
g_animation_manager = nullptr;
delete g_image_cache;
g_image_cache = nullptr;
if (sdl_window_ != nullptr) {
SDL_DestroyWindow(sdl_window_);
sdl_window_ = nullptr;
}
if (gl_context_ != nullptr) {
SDL_GL_DeleteContext(gl_context_);
gl_context_ = nullptr;
}
}
int Graphic::get_display_at(int x, int y) const {
for (int i = 0; i < SDL_GetNumVideoDisplays(); ++i) {
SDL_Rect r;
if (SDL_GetDisplayBounds(i, &r) == 0 && r.x <= x && x < r.x + r.w && r.y <= y &&
y < r.y + r.h) {
return i;
}
}
return -1;
}
void Graphic::move_to_display(int display) {
if (display < 0 || display >= SDL_GetNumVideoDisplays()) {
return;
}
SDL_SetWindowPosition(sdl_window_, SDL_WINDOWPOS_CENTERED_DISPLAY(display),
SDL_WINDOWPOS_CENTERED_DISPLAY(display));
}
/**
* Return the screen x resolution
*/
int Graphic::get_xres() const {
return screen_->width();
}
/**
* Return the screen x resolution
*/
int Graphic::get_yres() const {
return screen_->height();
}
int Graphic::get_window_mode_xres() const {
return window_mode_width_;
}
int Graphic::get_window_mode_yres() const {
return window_mode_height_;
}
void Graphic::change_resolution(int w, int h, bool resize_window) {
window_mode_width_ = w;
window_mode_height_ = h;
if (!fullscreen() && resize_window) {
set_window_size(w, h);
}
resolution_changed();
}
void Graphic::set_window_size(int w, int h) {
// Calling SDL_SetWindowSize() when the window is resizable or maximized can sometimes cause
// SDL (and consequently us) to lose track of the real window size, causing nasty glitches.
// To work around this we temporarily set the window as not resizable. It's set back to
// resizable later in refresh().
SDL_SetWindowResizable(sdl_window_, SDL_FALSE);
if (maximized()) {
SDL_RestoreWindow(sdl_window_);
}
SDL_SetWindowSize(sdl_window_, w, h);
}
void Graphic::resolution_changed() {
int old_w = screen_ ? screen_->width() : 0;
int old_h = screen_ ? screen_->height() : 0;
int new_w;
int new_h;
SDL_GetWindowSize(sdl_window_, &new_w, &new_h);
if (old_w == new_w && old_h == new_h) {
return;
}
screen_.reset(new Screen(new_w, new_h));
render_target_.reset(new RenderTarget(screen_.get()));
Notifications::publish(GraphicResolutionChanged{old_w, old_h, new_w, new_h});
}
/**
* Return a pointer to the RenderTarget representing the screen
*/
RenderTarget* Graphic::get_render_target() {
render_target_->reset();
return render_target_.get();
}
int Graphic::max_texture_size_for_font_rendering() const {
// Test with minimum supported size in debug builds.
#ifndef NDEBUG
return kMinimumSizeForTextures;
#else
return max_texture_size_;
#endif
}
int Graphic::get_display() const {
int x;
int y;
int w;
int h;
SDL_GetWindowPosition(sdl_window_, &x, &y);
SDL_GetWindowSize(sdl_window_, &w, &h);
return get_display_at(x + w / 2, y + h / 2);
}
bool Graphic::maximized() const {
uint32_t flags = SDL_GetWindowFlags(sdl_window_);
return (flags & SDL_WINDOW_MAXIMIZED) != 0u;
}
void Graphic::set_maximized(const bool to_maximize, int to_display) {
window_mode_maximized_ = to_maximize;
int display = get_display();
if (to_display < 0) {
to_display = display;
}
if (fullscreen() || (maximized() == to_maximize && display == to_display)) {
return;
}
if (to_maximize) {
// Maximizing only works if the window is resizable.
SDL_SetWindowResizable(sdl_window_, SDL_TRUE);
if (display != to_display) {
SDL_RestoreWindow(sdl_window_);
move_to_display(to_display);
}
SDL_MaximizeWindow(sdl_window_);
} else {
// Avoid glitches. See the comment in set_window_size().
SDL_SetWindowResizable(sdl_window_, SDL_FALSE);
SDL_RestoreWindow(sdl_window_);
}
}
bool Graphic::fullscreen() const {
uint32_t flags = SDL_GetWindowFlags(sdl_window_);
return ((flags & SDL_WINDOW_FULLSCREEN) != 0u) ||
((flags & SDL_WINDOW_FULLSCREEN_DESKTOP) != 0u);
}
void Graphic::set_fullscreen(const bool value, int to_display) {
int display = get_display();
if (to_display < 0) {
to_display = display;
}
if (value == fullscreen() && display == to_display) {
return;
}
// Avoid glitches. See the comment in set_window_size().
SDL_SetWindowResizable(sdl_window_, SDL_FALSE);
// Use the desktop resolution in fullscreen mode.
// SDL will resize the window for us.
if (value) {
window_mode_maximized_ = maximized();
if (display != to_display) {
SDL_SetWindowFullscreen(sdl_window_, 0);
SDL_SetWindowResizable(sdl_window_, SDL_TRUE);
SDL_RestoreWindow(sdl_window_);
move_to_display(to_display);
}
SDL_SetWindowFullscreen(sdl_window_, SDL_WINDOW_FULLSCREEN_DESKTOP);
} else {
SDL_SetWindowFullscreen(sdl_window_, 0);
}
resolution_changed();
}
/**
* Bring the screen uptodate.
*/
void Graphic::refresh() {
RenderQueue::instance().draw(screen_->width(), screen_->height());
if (!fullscreen()) {
// Set the window to our preferred size if it goes out of sync.
// Not sure if this is still needed, leaving it just in case.
int true_width;
int true_height;
SDL_GetWindowSize(sdl_window_, &true_width, &true_height);
if (true_width != window_mode_width_ || true_height != window_mode_height_) {
set_window_size(window_mode_width_, window_mode_height_);
set_maximized(window_mode_maximized_);
resolution_changed();
}
// See the comment in set_window_size().
SDL_SetWindowResizable(sdl_window_, SDL_TRUE);
}
// The backbuffer now contains the current frame. If we want a screenshot,
// we should better take it now, before this is swapped out to the
// frontbuffer and becomes inaccessible to us.
if (!screenshot_filename_.empty()) {
log_info("Save screenshot to %s\n", screenshot_filename_.c_str());
std::unique_ptr<StreamWrite> sw(g_fs->open_stream_write(screenshot_filename_));
save_to_png(screen_->to_texture().get(), sw.get(), ColorType::RGB);
screenshot_filename_.clear();
}
SDL_GL_SwapWindow(sdl_window_);
}
/**
* Save a screenshot to the given file.
*/
void Graphic::screenshot(const std::string& fname) {
screenshot_filename_ = fname;
}
|