OpenTTD Source 20250521-master-g82876c25e0
sdl2_default_v.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD 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, version 2.
4 * OpenTTD 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.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10#include "../stdafx.h"
11#include "../openttd.h"
12#include "../error_func.h"
13#include "../gfx_func.h"
14#include "../rev.h"
15#include "../blitter/factory.hpp"
16#include "../network/network.h"
17#include "../thread.h"
18#include "../progress.h"
19#include "../core/random_func.hpp"
20#include "../core/math_func.hpp"
21#include "../core/geometry_func.hpp"
22#include "../fileio_func.h"
23#include "../framerate_type.h"
24#include "../window_func.h"
25#include "sdl2_default_v.h"
26#include <SDL.h>
27#include <mutex>
28#include <condition_variable>
29#ifdef __EMSCRIPTEN__
30# include <emscripten.h>
31# include <emscripten/html5.h>
32#endif
33
34#include "../safeguards.h"
35
36static FVideoDriver_SDL_Default iFVideoDriver_SDL_Default;
37
38static SDL_Surface *_sdl_surface;
39static SDL_Surface *_sdl_rgb_surface;
40static SDL_Surface *_sdl_real_surface;
41static SDL_Palette *_sdl_palette;
42
43void VideoDriver_SDL_Default::UpdatePalette()
44{
45 SDL_Color pal[256];
46
47 for (int i = 0; i != this->local_palette.count_dirty; i++) {
48 pal[i].r = this->local_palette.palette[this->local_palette.first_dirty + i].r;
49 pal[i].g = this->local_palette.palette[this->local_palette.first_dirty + i].g;
50 pal[i].b = this->local_palette.palette[this->local_palette.first_dirty + i].b;
51 pal[i].a = 0;
52 }
53
54 SDL_SetPaletteColors(_sdl_palette, pal, this->local_palette.first_dirty, this->local_palette.count_dirty);
55 SDL_SetSurfacePalette(_sdl_surface, _sdl_palette);
56}
57
58void VideoDriver_SDL_Default::MakePalette()
59{
60 if (_sdl_palette == nullptr) {
61 _sdl_palette = SDL_AllocPalette(256);
62 if (_sdl_palette == nullptr) UserError("SDL2: Couldn't allocate palette: {}", SDL_GetError());
63 }
64
65 CopyPalette(this->local_palette, true);
66 this->UpdatePalette();
67
68 if (_sdl_surface != _sdl_real_surface) {
69 /* When using a shadow surface, also set our palette on the real screen. This lets SDL
70 * allocate as many colors (or approximations) as
71 * possible, instead of using only the default SDL
72 * palette. This allows us to get more colors exactly
73 * right and might allow using better approximations for
74 * other colors.
75 *
76 * Note that colors allocations are tried in-order, so
77 * this favors colors further up into the palette. Also
78 * note that if two colors from the same animation
79 * sequence are approximated using the same color, that
80 * animation will stop working.
81 *
82 * Since changing the system palette causes the colours
83 * to change right away, and allocations might
84 * drastically change, we can't use this for animation,
85 * since that could cause weird coloring between the
86 * palette change and the blitting below, so we only set
87 * the real palette during initialisation.
88 */
89 SDL_SetSurfacePalette(_sdl_real_surface, _sdl_palette);
90 }
91}
92
94{
96
97 if (IsEmptyRect(this->dirty_rect) && this->local_palette.count_dirty == 0) return;
98
99 if (this->local_palette.count_dirty != 0) {
101
102 switch (blitter->UsePaletteAnimation()) {
104 this->UpdatePalette();
105 break;
106
108 blitter->PaletteAnimate(this->local_palette);
109 break;
110 }
111
113 break;
114
115 default:
116 NOT_REACHED();
117 }
118 this->local_palette.count_dirty = 0;
119 }
120
121 SDL_Rect r = { this->dirty_rect.left, this->dirty_rect.top, this->dirty_rect.right - this->dirty_rect.left, this->dirty_rect.bottom - this->dirty_rect.top };
122
123 if (_sdl_surface != _sdl_real_surface) {
124 SDL_BlitSurface(_sdl_surface, &r, _sdl_real_surface, &r);
125 }
126 SDL_UpdateWindowSurfaceRects(this->sdl_window, &r, 1);
127
128 this->dirty_rect = {};
129}
130
132{
134
135 _sdl_real_surface = SDL_GetWindowSurface(this->sdl_window);
136 if (_sdl_real_surface == nullptr) UserError("SDL2: Couldn't get window surface: {}", SDL_GetError());
137
138 if (!force && w == _sdl_real_surface->w && h == _sdl_real_surface->h) return false;
139
140 /* Free any previously allocated rgb surface. */
141 if (_sdl_rgb_surface != nullptr) {
142 SDL_FreeSurface(_sdl_rgb_surface);
143 _sdl_rgb_surface = nullptr;
144 }
145
146 if (bpp == 8) {
147 _sdl_rgb_surface = SDL_CreateRGBSurface(0, w, h, 8, 0, 0, 0, 0);
148 if (_sdl_rgb_surface == nullptr) UserError("SDL2: Couldn't allocate shadow surface: {}", SDL_GetError());
149
150 _sdl_surface = _sdl_rgb_surface;
151 } else {
152 _sdl_surface = _sdl_real_surface;
153 }
154
155 /* X11 doesn't appreciate it if we invalidate areas outside the window
156 * if shared memory is enabled (read: it crashes). So, as we might have
157 * gotten smaller, reset our dirty rects. GameSizeChanged() a bit lower
158 * will mark the whole screen dirty again anyway, but this time with the
159 * new dimensions. */
160 this->dirty_rect = {};
161
162 _screen.width = _sdl_surface->w;
163 _screen.height = _sdl_surface->h;
164 _screen.pitch = _sdl_surface->pitch / (bpp / 8);
165 _screen.dst_ptr = this->GetVideoPointer();
166
167 this->MakePalette();
168
169 return true;
170}
171
173{
174 return _sdl_surface->pixels;
175}
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition factory.hpp:136
How all blitters should look like.
Definition base.hpp:29
virtual uint8_t GetScreenDepth()=0
Get the screen depth this blitter works for.
virtual Blitter::PaletteAnimation UsePaletteAnimation()=0
Check if the blitter uses palette animation at all.
virtual void PaletteAnimate(const Palette &palette)=0
Called when the 8bpp palette is changed; you should redraw all pixels on the screen that are equal to...
@ None
No palette animation.
@ Blitter
The blitter takes care of the palette animation.
@ VideoBackend
Palette animation should be done by video backend (8bpp only!)
Factory for the SDL video driver.
RAII class for measuring simple elements of performance.
Palette local_palette
Current palette to use for drawing.
Definition sdl2_v.h:48
Rect dirty_rect
Rectangle encompassing the dirty area of the video buffer.
Definition sdl2_v.h:50
struct SDL_Window * sdl_window
Main SDL window.
Definition sdl2_v.h:47
bool AllocateBackingStore(int w, int h, bool force=false) override
(Re-)create the backing store.
void * GetVideoPointer() override
Get a pointer to the video buffer.
void Paint() override
Paint the window.
@ PFE_VIDEO
Speed of painting drawn video buffer.
bool IsEmptyRect(const Rect &r)
Check if a rectangle is empty.
bool CopyPalette(Palette &local_palette, bool force_copy)
Copy the current palette if the palette was updated.
Definition palette.cpp:225
Default backend of the SDL2 video driver.
int first_dirty
The first dirty element.
Definition gfx_type.h:370
int count_dirty
The number of dirty elements.
Definition gfx_type.h:371
Colour palette[256]
Current palette. Entry 0 has to be always fully transparent!
Definition gfx_type.h:369