OpenTTD Source 20241224-master-gf74b0cf984
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/mem_func.hpp"
25#include "../core/geometry_func.hpp"
26#include "../fileio_func.h"
27#include "../framerate_type.h"
28#include "../window_func.h"
29#include "sdl2_opengl_v.h"
30#include <SDL.h>
31#include <mutex>
32#include <condition_variable>
33#include <GL/gl.h>
34#include "../3rdparty/opengl/glext.h"
35#include "opengl.h"
36#ifdef __EMSCRIPTEN__
37# include <emscripten.h>
38# include <emscripten/html5.h>
39#endif
40
41#include "../safeguards.h"
42
43static FVideoDriver_SDL_OpenGL iFVideoDriver_SDL_OpenGL;
44
46static OGLProc GetOGLProcAddressCallback(const char *proc)
47{
48 return reinterpret_cast<OGLProc>(SDL_GL_GetProcAddress(proc));
49}
50
51bool VideoDriver_SDL_OpenGL::CreateMainWindow(uint w, uint h, uint flags)
52{
53 return this->VideoDriver_SDL_Base::CreateMainWindow(w, h, flags | SDL_WINDOW_OPENGL);
54}
55
56std::optional<std::string_view> VideoDriver_SDL_OpenGL::Start(const StringList &param)
57{
58 auto error = VideoDriver_SDL_Base::Start(param);
59 if (error) return error;
60
61 error = this->AllocateContext();
62 if (error) {
63 this->Stop();
64 return error;
65 }
66
67 this->driver_info += " (";
68 this->driver_info += OpenGLBackend::Get()->GetDriverName();
69 this->driver_info += ")";
70
71 /* Now we have a OpenGL context, force a client-size-changed event,
72 * so all buffers are allocated correctly. */
73 int w, h;
74 SDL_GetWindowSize(this->sdl_window, &w, &h);
75 this->ClientSizeChanged(w, h, true);
76 /* We should have a valid screen buffer now. If not, something went wrong and we should abort. */
77 if (_screen.dst_ptr == nullptr) {
78 this->Stop();
79 return "Can't get pointer to screen buffer";
80 }
81 /* Main loop expects to start with the buffer unmapped. */
82 this->ReleaseVideoPointer();
83
84 return std::nullopt;
85}
86
88{
89 this->DestroyContext();
91}
92
93void VideoDriver_SDL_OpenGL::DestroyContext()
94{
96
97 if (this->gl_context != nullptr) {
98 SDL_GL_DeleteContext(this->gl_context);
99 this->gl_context = nullptr;
100 }
101}
102
104{
105 SDL_GL_SetSwapInterval(vsync);
106}
107
108std::optional<std::string_view> VideoDriver_SDL_OpenGL::AllocateContext()
109{
110 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
111 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
112 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
113 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
114 SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
115 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
116
117 if (_debug_driver_level >= 8) {
118 SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);
119 }
120
121 this->gl_context = SDL_GL_CreateContext(this->sdl_window);
122 if (this->gl_context == nullptr) return "SDL2: Can't active GL context";
123
125
127}
128
130{
131 OpenGLBackend::Get()->PopulateCursorCache();
132}
133
138
140{
141 if (this->gl_context == nullptr) return false;
142
143 if (_screen.dst_ptr != nullptr) this->ReleaseVideoPointer();
144
145 w = std::max(w, 64);
146 h = std::max(h, 64);
147 MemSetT(&this->dirty_rect, 0);
148
149 bool res = OpenGLBackend::Get()->Resize(w, h, force);
150 SDL_GL_SwapWindow(this->sdl_window);
151 _screen.dst_ptr = this->GetVideoPointer();
152
153 CopyPalette(this->local_palette, true);
154
155 return res;
156}
157
159{
160 if (BlitterFactory::GetCurrentBlitter()->NeedsAnimationBuffer()) {
162 }
164}
165
173
175{
177
178 if (this->local_palette.count_dirty != 0) {
180
181 /* Always push a changed palette to OpenGL. */
182 OpenGLBackend::Get()->UpdatePalette(this->local_palette.palette, this->local_palette.first_dirty, this->local_palette.count_dirty);
184 blitter->PaletteAnimate(this->local_palette);
185 }
186
187 this->local_palette.count_dirty = 0;
188 }
189
192
193 SDL_GL_SwapWindow(this->sdl_window);
194}
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition factory.hpp:138
How all blitters should look like.
Definition base.hpp:29
virtual Blitter::PaletteAnimation UsePaletteAnimation()=0
Check if the blitter uses palette animation at all.
@ PALETTE_ANIMATION_BLITTER
The blitter takes care of the palette animation.
Definition base.hpp:53
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...
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:467
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:82
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:480
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:555
Dimension GetScreenSize() const override
Get the resolution of the main screen.
Definition sdl2_v.cpp:730
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:603
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.
void MemSetT(T *ptr, uint8_t value, size_t num=1)
Type-safe version of memset().
Definition mem_func.hpp:49
OpenGL video driver support.
bool CopyPalette(Palette &local_palette, bool force_copy)
Copy the current palette if the palette was updated.
Definition palette.cpp:152
static OGLProc GetOGLProcAddressCallback(const char *proc)
Platform-specific callback to get an OpenGL funtion 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:331
Colour palette[256]
Current palette. Entry 0 has to be always fully transparent!
Definition gfx_type.h:329
bool _video_vsync
Whether we should use vsync (only if active video driver supports HW acceleration).