OpenTTD Source 20261112-master-gbcbec5e63f
sdl3_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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "../stdafx.h"
11#include "../error_func.h"
12#include "../gfx_func.h"
15#include "../framerate_type.h"
16#include "sdl3_default_v.h"
17#include <SDL3/SDL_video.h>
18#ifdef __EMSCRIPTEN__
19# include <emscripten.h>
20# include <emscripten/html5.h>
21#endif
22
23#include "../safeguards.h"
24
26
27static SDL_Surface *_sdl_surface;
28static SDL_Surface *_sdl_rgb_surface;
29static SDL_Surface *_sdl_real_surface;
30static SDL_Palette *_sdl_palette;
31
36{
37 SDL_Color pal[256];
38
39 for (int i = 0; i != this->local_palette.count_dirty; i++) {
40 pal[i].r = this->local_palette.palette[this->local_palette.first_dirty + i].r;
41 pal[i].g = this->local_palette.palette[this->local_palette.first_dirty + i].g;
42 pal[i].b = this->local_palette.palette[this->local_palette.first_dirty + i].b;
43 pal[i].a = 0;
44 }
45
46 SDL_SetPaletteColors(_sdl_palette, pal, this->local_palette.first_dirty, this->local_palette.count_dirty);
47 SDL_SetSurfacePalette(_sdl_surface, _sdl_palette);
48}
49
54{
55 if (_sdl_palette == nullptr) {
56 _sdl_palette = SDL_CreatePalette(256);
57 if (_sdl_palette == nullptr) UserError("SDL3: Couldn't allocate palette: {}", SDL_GetError());
58 }
59
60 CopyPalette(this->local_palette, true);
61 this->UpdatePalette();
62
64 /* When using a shadow surface, also set our palette on the real screen. This lets SDL
65 * allocate as many colours (or approximations) as
66 * possible, instead of using only the default SDL
67 * palette. This allows us to get more colours exactly
68 * right and might allow using better approximations for
69 * other colours.
70 *
71 * Note that colours allocations are tried in-order, so
72 * this favors colours further up into the palette. Also
73 * note that if two colours from the same animation
74 * sequence are approximated using the same colour, that
75 * animation will stop working.
76 *
77 * Since changing the system palette causes the colours
78 * to change right away, and allocations might
79 * drastically change, we can't use this for animation,
80 * since that could cause weird colouring between the
81 * palette change and the blitting below, so we only set
82 * the real palette during initialisation.
83 */
84 SDL_SetSurfacePalette(_sdl_real_surface, _sdl_palette);
85 }
86}
87
89{
91
92 if (IsEmptyRect(this->dirty_rect) && this->local_palette.count_dirty == 0) return;
93
94 if (this->local_palette.count_dirty != 0) {
96
97 switch (blitter->UsePaletteAnimation()) {
99 this->UpdatePalette();
100 break;
101
103 blitter->PaletteAnimate(this->local_palette);
104 break;
105 }
106
108 break;
109
110 default:
111 NOT_REACHED();
112 }
113 this->local_palette.count_dirty = 0;
114 }
115
116 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 };
117
119 SDL_BlitSurface(_sdl_surface, &r, _sdl_real_surface, &r);
120 }
121 SDL_UpdateWindowSurfaceRects(this->sdl_window, &r, 1);
122
123 this->dirty_rect = {};
124}
125
127{
129
130 _sdl_real_surface = SDL_GetWindowSurface(this->sdl_window);
131 if (_sdl_real_surface == nullptr) UserError("SDL3: Couldn't get window surface: {}", SDL_GetError());
132
133 if (!force && w == _sdl_real_surface->w && h == _sdl_real_surface->h) return false;
134
135 /* Free any previously allocated rgb surface. */
136 if (_sdl_rgb_surface != nullptr) {
137 SDL_DestroySurface(_sdl_rgb_surface);
138 _sdl_rgb_surface = nullptr;
139 }
140
141 if (bpp == 8) {
142 _sdl_rgb_surface = SDL_CreateSurface(w, h, SDL_PIXELFORMAT_INDEX8);
143 if (_sdl_rgb_surface == nullptr) UserError("SDL3: Couldn't allocate shadow surface: {}", SDL_GetError());
144
146 } else {
148 }
149
150 /* X11 doesn't appreciate it if we invalidate areas outside the window
151 * if shared memory is enabled (read: it crashes). So, as we might have
152 * gotten smaller, reset our dirty rects. GameSizeChanged() a bit lower
153 * will mark the whole screen dirty again anyway, but this time with the
154 * new dimensions. */
155 this->dirty_rect = {};
156
157 _screen.width = _sdl_surface->w;
158 _screen.height = _sdl_surface->h;
159 _screen.pitch = _sdl_surface->pitch / (bpp / 8);
160 _screen.dst_ptr = this->GetVideoPointer();
161
162 this->MakePalette();
163
164 return true;
165}
166
168{
169 return _sdl_surface->pixels;
170}
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition factory.hpp:139
How all blitters should look like.
Definition base.hpp:28
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.
Definition base.hpp:50
@ Blitter
The blitter takes care of the palette animation.
Definition base.hpp:52
@ VideoBackend
Palette animation should be done by video backend (8bpp only!).
Definition base.hpp:51
Factory for the SDL video driver.
RAII class for measuring simple elements of performance.
Palette local_palette
Current palette to use for drawing.
Definition sdl3_v.h:53
Rect dirty_rect
Rectangle encompassing the dirty area of the video buffer.
Definition sdl3_v.h:55
struct SDL_Window * sdl_window
Main SDL window.
Definition sdl3_v.h:52
void UpdatePalette()
Update the dirty palette colours.
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.
void MakePalette()
Allocate and configure the palette.
Error reporting related functions.
Factory to 'query' all available blitters.
Types for recording game performance data.
@ Video
Speed of painting drawn video buffer.
Geometry functions.
bool IsEmptyRect(const Rect &r)
Check if a rectangle is empty.
Functions related to the gfx engine.
bool CopyPalette(Palette &local_palette, bool force_copy)
Copy the current palette if the palette was updated.
Definition palette.cpp:230
A number of safeguards to prevent using unsafe methods.
static SDL_Palette * _sdl_palette
The palette mapping.
static FVideoDriver_SDL_Default iFVideoDriver_SDL_Default
The default, non-GL, SDL video driver.
static SDL_Surface * _sdl_real_surface
The actual surface of the screen.
static SDL_Surface * _sdl_rgb_surface
Optional surface to use for blitting all changes at once.
static SDL_Surface * _sdl_surface
The surface to draw on/to.
Default backend of the SDL3 video driver.
Definition of base types and functions in a cross-platform compatible way.