OpenTTD Source 20260704-master-gbf70c61fb8
sdl2_v.h
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#ifndef VIDEO_SDL_H
11#define VIDEO_SDL_H
12
13#include <condition_variable>
14
15#include "video_driver.hpp"
16
18class VideoDriver_SDL_Base : public VideoDriver {
19public:
24 VideoDriver_SDL_Base(bool uses_hardware_acceleration = false) : VideoDriver(uses_hardware_acceleration) {}
25
26 std::optional<std::string_view> Start(const StringList &param) override;
27
28 void Stop() override;
29
30 void MakeDirty(int left, int top, int width, int height) override;
31
32 void MainLoop() override;
33
34 bool ChangeResolution(int w, int h) override;
35
36 bool ToggleFullscreen(bool fullscreen) override;
37
38 bool AfterBlitterChange() override;
39
40 void ClaimMousePointer() override;
41
42 void EditBoxGainedFocus() override;
43
44 void EditBoxLostFocus() override;
45
46 std::vector<int> GetListOfMonitorRefreshRates() override;
47
48 std::string_view GetInfoString() const override { return this->driver_info; }
49
50 void SetScreensaverInhibited(bool inhibited) override;
51
52protected:
53 struct SDL_Window *sdl_window = nullptr;
54 Palette local_palette{};
55 bool buffer_locked = false;
57 std::string driver_info{};
58
59 Dimension GetScreenSize() const override;
60 void InputLoop() override;
61 bool LockVideoBuffer() override;
62 void UnlockVideoBuffer() override;
63 void CheckPaletteAnim() override;
64 bool PollEvent() override;
65
66 void ClientSizeChanged(int w, int h, bool force);
67
75 virtual bool AllocateBackingStore(int w, int h, bool force = false) = 0;
76
81 virtual void *GetVideoPointer() = 0;
82
84 virtual void ReleaseVideoPointer() = 0;
85
93 virtual bool CreateMainWindow(uint w, uint h, uint flags = 0);
94
95private:
96 void LoopOnce();
97 bool CreateMainSurface(uint w, uint h, bool resize);
98 std::optional<std::string_view> Initialize();
99
100#ifdef __EMSCRIPTEN__
101 /* Convert a constant pointer back to a non-constant pointer to a member function. */
102 static void EmscriptenLoop(void *self) { ((VideoDriver_SDL_Base *)self)->LoopOnce(); }
103#endif
104
108 bool edit_box_focused = false;
109
111};
112
113#endif /* VIDEO_SDL_H */
std::optional< std::string_view > Start(const StringList &param) override
Start this driver.
Definition sdl2_v.cpp:603
bool buffer_locked
Video buffer was locked by the main thread.
Definition sdl2_v.h:55
Dimension GetScreenSize() const override
Get the resolution of the main screen.
Definition sdl2_v.cpp:774
bool PollEvent() override
Process a single system event.
Definition sdl2_v.cpp:417
void EditBoxLostFocus() override
This is called to indicate that an edit box has lost focus, text input mode should be disabled.
Definition sdl2_v.cpp:245
std::string_view GetInfoString() const override
Get some information about the selected driver/backend to be shown to the user.
Definition sdl2_v.h:48
virtual void ReleaseVideoPointer()=0
Hand video buffer back to the painting backend.
void ClaimMousePointer() override
Claim the exclusive rights for the mouse pointer.
Definition sdl2_v.cpp:223
virtual void * GetVideoPointer()=0
Get a pointer to the video buffer.
void LoopOnce()
Run a single tick of the game/main loop.
Definition sdl2_v.cpp:679
bool AfterBlitterChange() override
Callback invoked after the blitter was changed.
Definition sdl2_v.cpp:766
virtual bool AllocateBackingStore(int w, int h, bool force=false)=0
(Re-)create the backing store.
void InputLoop() override
Handle input logic, is CTRL pressed, should we fast-forward, etc.
Definition sdl2_v.cpp:655
Palette local_palette
Current palette to use for drawing.
Definition sdl2_v.h:54
void MainLoop() override
Perform the actual drawing.
Definition sdl2_v.cpp:715
VideoDriver_SDL_Base(bool uses_hardware_acceleration=false)
Create the video driver.
Definition sdl2_v.h:24
std::optional< std::string_view > Initialize()
Initialize the driver.
Definition sdl2_v.cpp:590
std::string driver_info
Information string about selected driver.
Definition sdl2_v.h:57
void UnlockVideoBuffer() override
Unlock a previously locked video buffer.
Definition sdl2_v.cpp:793
void ClientSizeChanged(int w, int h, bool force)
Indicate to the driver the client-size might have changed.
Definition sdl2_v.cpp:141
int startup_display
The display to show OpenTTD on when starting.
Definition sdl2_v.h:110
void MakeDirty(int left, int top, int width, int height) override
Mark a particular area dirty.
Definition sdl2_v.cpp:31
virtual bool CreateMainWindow(uint w, uint h, uint flags=0)
Create the main window.
Definition sdl2_v.cpp:153
std::vector< int > GetListOfMonitorRefreshRates() override
Get a list of refresh rates of each available monitor.
Definition sdl2_v.cpp:253
void SetScreensaverInhibited(bool inhibited) override
Prevents the system from going to sleep.
Definition sdl2_v.cpp:804
bool edit_box_focused
This is true to indicate that keyboard input is in text input mode, and SDL_TEXTINPUT events are enab...
Definition sdl2_v.h:108
bool ChangeResolution(int w, int h) override
Change the resolution of the window.
Definition sdl2_v.cpp:731
void Stop() override
Stop this driver.
Definition sdl2_v.cpp:647
bool CreateMainSurface(uint w, uint h, bool resize)
Create/update the surfaces to draw OpenTTD on.
Definition sdl2_v.cpp:206
bool LockVideoBuffer() override
Make sure the video buffer is ready for drawing.
Definition sdl2_v.cpp:782
void EditBoxGainedFocus() override
This is called to indicate that an edit box has gained focus, text input mode should be enabled.
Definition sdl2_v.cpp:234
Rect dirty_rect
Rectangle encompassing the dirty area of the video buffer.
Definition sdl2_v.h:56
void CheckPaletteAnim() override
Process any pending palette animation.
Definition sdl2_v.cpp:37
struct SDL_Window * sdl_window
Main SDL window.
Definition sdl2_v.h:53
bool ToggleFullscreen(bool fullscreen) override
Change the full screen setting.
Definition sdl2_v.cpp:736
#define Rect
Macro that prevents name conflicts between included headers.
std::vector< std::string > StringList
Type for a list of strings.
Definition string_type.h:61
Dimensions (a width and height) of a rectangle in 2D.
Base of all video drivers.