OpenTTD Source: src/video/sdl2_opengl_v.cpp Source File

OpenTTD Source 13.1

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
43 static FVideoDriver_SDL_OpenGL iFVideoDriver_SDL_OpenGL;
44
46 static OGLProc GetOGLProcAddressCallback(const char *proc)
47 {
48 return reinterpret_cast<OGLProc>(SDL_GL_GetProcAddress(proc));
49 }
50
51 bool VideoDriver_SDL_OpenGL::CreateMainWindow(uint w, uint h, uint flags)
52 {
53 return this->VideoDriver_SDL_Base::CreateMainWindow(w, h, SDL_WINDOW_OPENGL);
54 }
55
56 const char *VideoDriver_SDL_OpenGL::Start(const StringList &param)
57 {
58 const char *error = VideoDriver_SDL_Base::Start(param);
59 if (error != nullptr) return error;
60
61 error = this->AllocateContext();
62 if (error != nullptr) {
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 nullptr;
85 }
86
87 void VideoDriver_SDL_OpenGL::Stop()
88 {
89 this->DestroyContext();
90 this->VideoDriver_SDL_Base::Stop();
91 }
92
93 void VideoDriver_SDL_OpenGL::DestroyContext()
94 {
95 OpenGLBackend::Destroy();
96
97 if (this->gl_context != nullptr) {
98 SDL_GL_DeleteContext(this->gl_context);
99 this->gl_context = nullptr;
100 }
101 }
102
103 void VideoDriver_SDL_OpenGL::ToggleVsync(bool vsync)
104 {
105 SDL_GL_SetSwapInterval(vsync);
106 }
107
108 const char *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
124 ToggleVsync(_video_vsync);
125
126 return OpenGLBackend::Create(&GetOGLProcAddressCallback, this->GetScreenSize());
127 }
128
129 void VideoDriver_SDL_OpenGL::PopulateSystemSprites()
130 {
131 OpenGLBackend::Get()->PopulateCursorCache();
132 }
133
134 void VideoDriver_SDL_OpenGL::ClearSystemSprites()
135 {
136 OpenGLBackend::Get()->ClearCursorCache();
137 }
138
139 bool VideoDriver_SDL_OpenGL::AllocateBackingStore(int w, int h, bool force)
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
158 void *VideoDriver_SDL_OpenGL::GetVideoPointer()
159 {
160 if (BlitterFactory::GetCurrentBlitter()->NeedsAnimationBuffer()) {
161 this->anim_buffer = OpenGLBackend::Get()->GetAnimBuffer();
162 }
163 return OpenGLBackend::Get()->GetVideoBuffer();
164 }
165
166 void VideoDriver_SDL_OpenGL::ReleaseVideoPointer()
167 {
168 if (this->anim_buffer != nullptr) OpenGLBackend::Get()->ReleaseAnimBuffer(this->dirty_rect);
169 OpenGLBackend::Get()->ReleaseVideoBuffer(this->dirty_rect);
170 MemSetT(&this->dirty_rect, 0);
171 this->anim_buffer = nullptr;
172 }
173
174 void VideoDriver_SDL_OpenGL::Paint()
175 {
176 PerformanceMeasurer framerate(PFE_VIDEO);
177
178 if (this->local_palette.count_dirty != 0) {
179 Blitter *blitter = BlitterFactory::GetCurrentBlitter();
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);
183 if (blitter->UsePaletteAnimation() == Blitter::PALETTE_ANIMATION_BLITTER) {
184 blitter->PaletteAnimate(this->local_palette);
185 }
186
187 this->local_palette.count_dirty = 0;
188 }
189
190 OpenGLBackend::Get()->Paint();
191 OpenGLBackend::Get()->DrawMouseCursor();
192
193 SDL_GL_SwapWindow(this->sdl_window);
194 }
VideoDriver_SDL_OpenGL::AllocateBackingStore
bool AllocateBackingStore(int w, int h, bool force=false) override
(Re-)create the backing store.
Definition: sdl2_opengl_v.cpp:139
PFE_VIDEO
@ PFE_VIDEO
Speed of painting drawn video buffer.
Definition: framerate_type.h:59
VideoDriver_SDL_Base::driver_info
std::string driver_info
Information string about selected driver.
Definition: sdl2_v.h:53
Blitter::UsePaletteAnimation
virtual Blitter::PaletteAnimation UsePaletteAnimation()=0
Check if the blitter uses palette animation at all.
Blitter
How all blitters should look like.
Definition: base.hpp:28
VideoDriver_SDL_OpenGL::Stop
void Stop() override
Stop this driver.
Definition: sdl2_opengl_v.cpp:87
VideoDriver_SDL_OpenGL::CreateMainWindow
bool CreateMainWindow(uint w, uint h, uint flags) override
Create the main window.
Definition: sdl2_opengl_v.cpp:51
PerformanceMeasurer
RAII class for measuring simple elements of performance.
Definition: framerate_type.h:92
VideoDriver_SDL_OpenGL::PopulateSystemSprites
void PopulateSystemSprites() override
Populate all sprites in cache.
Definition: sdl2_opengl_v.cpp:129
OpenGLBackend::Get
static OpenGLBackend * Get()
Get singleton instance of this class.
Definition: opengl.h:84
OpenGLBackend::DrawMouseCursor
void DrawMouseCursor()
Draw mouse cursor on screen.
Definition: opengl.cpp:1076
VideoDriver_SDL_Base::Stop
void Stop() override
Stop this driver.
Definition: sdl2_v.cpp:562
VideoDriver_SDL_OpenGL::Paint
void Paint() override
Paint the window.
Definition: sdl2_opengl_v.cpp:174
Palette::palette
Colour palette[256]
Current palette. Entry 0 has to be always fully transparent!
Definition: gfx_type.h:321
OpenGLBackend::ReleaseVideoBuffer
void ReleaseVideoBuffer(const Rect &update_rect)
Update video buffer texture after the video buffer was filled.
Definition: opengl.cpp:1204
VideoDriver_SDL_Base::local_palette
Palette local_palette
Current palette to use for drawing.
Definition: sdl2_v.h:50
BlitterFactory::GetCurrentBlitter
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition: factory.hpp:141
OpenGLBackend::GetVideoBuffer
void * GetVideoBuffer()
Get a pointer to the memory for the video driver to draw to.
Definition: opengl.cpp:1159
StringList
std::vector< std::string > StringList
Type for a list of strings.
Definition: string_type.h:59
OpenGLBackend::Create
static const char * Create(GetOGLProcAddressProc get_proc, const Dimension &screen_res)
Create and initialize the singleton back-end class.
Definition: opengl.cpp:469
OpenGLBackend::Destroy
static void Destroy()
Free resources and destroy singleton back-end class.
Definition: opengl.cpp:482
VideoDriver_SDL_Base::CreateMainWindow
virtual bool CreateMainWindow(uint w, uint h, uint flags=0)
Create the main window.
Definition: sdl2_v.cpp:135
Palette::count_dirty
int count_dirty
The number of dirty elements.
Definition: gfx_type.h:323
VideoDriver_SDL_OpenGL::ToggleVsync
void ToggleVsync(bool vsync) override
Change the vsync setting.
Definition: sdl2_opengl_v.cpp:103
VideoDriver_SDL_OpenGL::Start
const char * Start(const StringList &param) override
Start this driver.
Definition: sdl2_opengl_v.cpp:56
OpenGLBackend::ClearCursorCache
void ClearCursorCache()
Queue a request for cursor cache clear.
Definition: opengl.cpp:1146
VideoDriver_SDL_OpenGL::ReleaseVideoPointer
void ReleaseVideoPointer() override
Hand video buffer back to the painting backend.
Definition: sdl2_opengl_v.cpp:166
VideoDriver_SDL_Base::ClientSizeChanged
void ClientSizeChanged(int w, int h, bool force)
Indicate to the driver the client-side might have changed.
Definition: sdl2_v.cpp:123
VideoDriver_SDL_OpenGL::GetVideoPointer
void * GetVideoPointer() override
Get a pointer to the video buffer.
Definition: sdl2_opengl_v.cpp:158
GetOGLProcAddressCallback
static OGLProc GetOGLProcAddressCallback(const char *proc)
Platform-specific callback to get an OpenGL funtion pointer.
Definition: sdl2_opengl_v.cpp:46
CopyPalette
bool CopyPalette(Palette &local_palette, bool force_copy)
Copy the current palette if the palette was updated.
Definition: gfx.cpp:1294
VideoDriver_SDL_Base::dirty_rect
Rect dirty_rect
Rectangle encompassing the dirty area of the video buffer.
Definition: sdl2_v.h:52
opengl.h
OpenGLBackend::Paint
void Paint()
Render video buffer to the screen.
Definition: opengl.cpp:1044
FVideoDriver_SDL_OpenGL
The factory for SDL' OpenGL video driver.
Definition: sdl2_opengl_v.h:52
error
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:134
_video_vsync
bool _video_vsync
Whether we should use vsync (only if _video_hw_accel is enabled).
Definition: video_driver.cpp:24
OpenGLBackend::UpdatePalette
void UpdatePalette(const Colour *pal, uint first, uint length)
Update the stored palette.
Definition: opengl.cpp:1030
Blitter::PaletteAnimate
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...
sdl2_opengl_v.h
Blitter::PALETTE_ANIMATION_BLITTER
@ PALETTE_ANIMATION_BLITTER
The blitter takes care of the palette animation.
Definition: base.hpp:52
MemSetT
static void MemSetT(T *ptr, byte value, size_t num=1)
Type-safe version of memset().
Definition: mem_func.hpp:49
VideoDriver_SDL_OpenGL::ClearSystemSprites
void ClearSystemSprites() override
Clear all cached sprites.
Definition: sdl2_opengl_v.cpp:134
OpenGLBackend::ReleaseAnimBuffer
void ReleaseAnimBuffer(const Rect &update_rect)
Update animation buffer texture after the animation buffer was filled.
Definition: opengl.cpp:1246
VideoDriver_SDL_Base::GetScreenSize
Dimension GetScreenSize() const override
Get the resolution of the main screen.
Definition: sdl2_v.cpp:687
VideoDriver_SDL_OpenGL::gl_context
void * gl_context
OpenGL context.
Definition: sdl2_opengl_v.h:44
OpenGLBackend::Resize
bool Resize(int w, int h, bool force=false)
Change the size of the drawing window and allocate matching resources.
Definition: opengl.cpp:915
OpenGLBackend::GetAnimBuffer
uint8 * GetAnimBuffer()
Get a pointer to the memory for the separate animation buffer.
Definition: opengl.cpp:1181
VideoDriver_SDL_OpenGL::anim_buffer
uint8 * anim_buffer
Animation buffer from OpenGL back-end.
Definition: sdl2_opengl_v.h:45
VideoDriver_SDL_Base::sdl_window
struct SDL_Window * sdl_window
Main SDL window.
Definition: sdl2_v.h:49
VideoDriver_SDL_Base::Start
const char * Start(const StringList &param) override
Start this driver.
Definition: sdl2_v.cpp:527