OpenTTD Source 20250521-master-g82876c25e0
sdl2_opengl_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/* XXX -- Temporary hack for Windows compile */
11#define WINGDIAPI
12#define APIENTRY
13
14#include "../stdafx.h"
15#include "../openttd.h"
16#include "../gfx_func.h"
17#include "../rev.h"
18#include "../blitter/factory.hpp"
19#include "../network/network.h"
20#include "../thread.h"
21#include "../progress.h"
22#include "../core/random_func.hpp"
23#include "../core/math_func.hpp"
24#include "../core/geometry_func.hpp"
25#include "../fileio_func.h"
26#include "../framerate_type.h"
27#include "../window_func.h"
28#include "sdl2_opengl_v.h"
29#include <SDL.h>
30#include <mutex>
31#include <condition_variable>
32#include <GL/gl.h>
33#include "../3rdparty/opengl/glext.h"
34#include "opengl.h"
35#ifdef __EMSCRIPTEN__
36# include <emscripten.h>
37# include <emscripten/html5.h>
38#endif
39
40#include "../safeguards.h"
41
42static FVideoDriver_SDL_OpenGL iFVideoDriver_SDL_OpenGL;
43
45static OGLProc GetOGLProcAddressCallback(const char *proc)
46{
47 return reinterpret_cast<OGLProc>(SDL_GL_GetProcAddress(proc));
48}
49
50bool VideoDriver_SDL_OpenGL::CreateMainWindow(uint w, uint h, uint flags)
51{
52 return this->VideoDriver_SDL_Base::CreateMainWindow(w, h, flags | SDL_WINDOW_OPENGL);
53}
54
55std::optional<std::string_view> VideoDriver_SDL_OpenGL::Start(const StringList &param)
56{
57 auto error = VideoDriver_SDL_Base::Start(param);
58 if (error) return error;
59
60 error = this->AllocateContext();
61 if (error) {
62 this->Stop();
63 return error;
64 }
65
66 this->driver_info += " (";
67 this->driver_info += OpenGLBackend::Get()->GetDriverName();
68 this->driver_info += ")";
69
70 /* Now we have a OpenGL context, force a client-size-changed event,
71 * so all buffers are allocated correctly. */
72 int w, h;
73 SDL_GetWindowSize(this->sdl_window, &w, &h);
74 this->ClientSizeChanged(w, h, true);
75 /* We should have a valid screen buffer now. If not, something went wrong and we should abort. */
76 if (_screen.dst_ptr == nullptr) {
77 this->Stop();
78 return "Can't get pointer to screen buffer";
79 }
80 /* Main loop expects to start with the buffer unmapped. */
81 this->ReleaseVideoPointer();
82
83 return std::nullopt;
84}
85
87{
88 this->DestroyContext();
90}
91
92void VideoDriver_SDL_OpenGL::DestroyContext()
93{
95
96 if (this->gl_context != nullptr) {
97 SDL_GL_DeleteContext(this->gl_context);
98 this->gl_context = nullptr;
99 }
100}
101
103{
104 SDL_GL_SetSwapInterval(vsync);
105}
106
107std::optional<std::string_view> VideoDriver_SDL_OpenGL::AllocateContext()
108{
109 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
110 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
111 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
112 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
113 SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
114 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
115
116 if (_debug_driver_level >= 8) {
117 SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);
118 }
119
120 this->gl_context = SDL_GL_CreateContext(this->sdl_window);
121 if (this->gl_context == nullptr) return "SDL2: Can't active GL context";
122
124
126}
127
129{
130 OpenGLBackend::Get()->PopulateCursorCache();
131}
132
137
139{
140 if (this->gl_context == nullptr) return false;
141
142 if (_screen.dst_ptr != nullptr) this->ReleaseVideoPointer();
143
144 w = std::max(w, 64);
145 h = std::max(h, 64);
146 this->dirty_rect = {};
147
148 bool res = OpenGLBackend::Get()->Resize(w, h, force);
149 SDL_GL_SwapWindow(this->sdl_window);
150 _screen.dst_ptr = this->GetVideoPointer();
151
152 CopyPalette(this->local_palette, true);
153
154 return res;
155}
156
158{
159 if (BlitterFactory::GetCurrentBlitter()->NeedsAnimationBuffer()) {
161 }
163}
164
166{
167 if (this->anim_buffer != nullptr) OpenGLBackend::Get()->ReleaseAnimBuffer(this->dirty_rect);
169 this->dirty_rect = {};
170 this->anim_buffer = nullptr;
171}
172
174{
176
177 if (this->local_palette.count_dirty != 0) {
179
180 /* Always push a changed palette to OpenGL. */
181 OpenGLBackend::Get()->UpdatePalette(this->local_palette.palette, this->local_palette.first_dirty, this->local_palette.count_dirty);
183 blitter->PaletteAnimate(this->local_palette);
184 }
185
186 this->local_palette.count_dirty = 0;
187 }
188
191
192 SDL_GL_SwapWindow(this->sdl_window);
193}
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 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...
@ Blitter
The blitter takes care of the palette animation.
The factory for SDL' OpenGL video driver.
void Paint()
Render video buffer to the screen.
Definition opengl.cpp:1039
uint8_t * GetAnimBuffer()
Get a pointer to the memory for the separate animation buffer.
Definition opengl.cpp:1170
void * GetVideoBuffer()
Get a pointer to the memory for the video driver to draw to.
Definition opengl.cpp:1148
bool Resize(int w, int h, bool force=false)
Change the size of the drawing window and allocate matching resources.
Definition opengl.cpp:913
static std::optional< std::string_view > Create(GetOGLProcAddressProc get_proc, const Dimension &screen_res)
Create and initialize the singleton back-end class.
Definition opengl.cpp:464
void UpdatePalette(const Colour *pal, uint first, uint length)
Update the stored palette.
Definition opengl.cpp:1025
void ReleaseAnimBuffer(const Rect &update_rect)
Update animation buffer texture after the animation buffer was filled.
Definition opengl.cpp:1231
void ClearCursorCache()
Queue a request for cursor cache clear.
Definition opengl.cpp:1135
static OpenGLBackend * Get()
Get singleton instance of this class.
Definition opengl.h:83
void DrawMouseCursor()
Draw mouse cursor on screen.
Definition opengl.cpp:1071
void ReleaseVideoBuffer(const Rect &update_rect)
Update video buffer texture after the video buffer was filled.
Definition opengl.cpp:1193
static void Destroy()
Free resources and destroy singleton back-end class.
Definition opengl.cpp:477
RAII class for measuring simple elements of performance.
std::optional< std::string_view > Start(const StringList &param) override
Start this driver.
Definition sdl2_v.cpp:558
Dimension GetScreenSize() const override
Get the resolution of the main screen.
Definition sdl2_v.cpp:729
Palette local_palette
Current palette to use for drawing.
Definition sdl2_v.h:48
std::string driver_info
Information string about selected driver.
Definition sdl2_v.h:51
void ClientSizeChanged(int w, int h, bool force)
Indicate to the driver the client-side might have changed.
Definition sdl2_v.cpp:124
virtual bool CreateMainWindow(uint w, uint h, uint flags=0)
Create the main window.
Definition sdl2_v.cpp:136
void Stop() override
Stop this driver.
Definition sdl2_v.cpp:602
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
void Paint() override
Paint the window.
void ReleaseVideoPointer() override
Hand video buffer back to the painting backend.
void * gl_context
OpenGL context.
void ToggleVsync(bool vsync) override
Change the vsync setting.
bool CreateMainWindow(uint w, uint h, uint flags) override
Create the main window.
void * GetVideoPointer() override
Get a pointer to the video buffer.
void Stop() override
Stop this driver.
void ClearSystemSprites() override
Clear all cached sprites.
bool AllocateBackingStore(int w, int h, bool force=false) override
(Re-)create the backing store.
std::optional< std::string_view > Start(const StringList &param) override
Start this driver.
void PopulateSystemSprites() override
Populate all sprites in cache.
uint8_t * anim_buffer
Animation buffer from OpenGL back-end.
@ PFE_VIDEO
Speed of painting drawn video buffer.
OpenGL video driver support.
bool CopyPalette(Palette &local_palette, bool force_copy)
Copy the current palette if the palette was updated.
Definition palette.cpp:225
static OGLProc GetOGLProcAddressCallback(const char *proc)
Platform-specific callback to get an OpenGL function pointer.
OpenGL backend of the SDL2 video driver.
std::vector< std::string > StringList
Type for a list of strings.
Definition string_type.h:60
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
bool _video_vsync
Whether we should use vsync (only if active video driver supports HW acceleration).