OpenTTD Source  20241108-master-g80f628063a
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 <http://www.gnu.org/licenses/>.
6  */
7 
10 #ifndef VIDEO_SDL_H
11 #define VIDEO_SDL_H
12 
13 #include <condition_variable>
14 
15 #include "video_driver.hpp"
16 
19 public:
20  VideoDriver_SDL_Base(bool uses_hardware_acceleration = false) : VideoDriver(uses_hardware_acceleration), sdl_window(nullptr), buffer_locked(false) {}
21 
22  std::optional<std::string_view> Start(const StringList &param) override;
23 
24  void Stop() override;
25 
26  void MakeDirty(int left, int top, int width, int height) override;
27 
28  void MainLoop() override;
29 
30  bool ChangeResolution(int w, int h) override;
31 
32  bool ToggleFullscreen(bool fullscreen) override;
33 
34  bool AfterBlitterChange() override;
35 
36  bool ClaimMousePointer() override;
37 
38  void EditBoxGainedFocus() override;
39 
40  void EditBoxLostFocus() override;
41 
42  std::vector<int> GetListOfMonitorRefreshRates() override;
43 
44  std::string_view GetInfoString() const override { return this->driver_info; }
45 
46 protected:
47  struct SDL_Window *sdl_window;
51  std::string driver_info;
52 
53  Dimension GetScreenSize() const override;
54  void InputLoop() override;
55  bool LockVideoBuffer() override;
56  void UnlockVideoBuffer() override;
57  void CheckPaletteAnim() override;
58  bool PollEvent() override;
59 
61  void ClientSizeChanged(int w, int h, bool force);
62 
64  virtual bool AllocateBackingStore(int w, int h, bool force = false) = 0;
66  virtual void *GetVideoPointer() = 0;
68  virtual void ReleaseVideoPointer() = 0;
70  virtual bool CreateMainWindow(uint w, uint h, uint flags = 0);
71 
72 private:
73  void LoopOnce();
74  void MainLoopCleanup();
75  bool CreateMainSurface(uint w, uint h, bool resize);
76  std::optional<std::string_view> Initialize();
77 
78 #ifdef __EMSCRIPTEN__
79  /* Convert a constant pointer back to a non-constant pointer to a member function. */
80  static void EmscriptenLoop(void *self) { ((VideoDriver_SDL_Base *)self)->LoopOnce(); }
81 #endif
82 
87 
88  int startup_display;
89 };
90 
91 #endif /* VIDEO_SDL_H */
The SDL video driver.
Definition: sdl2_v.h:18
std::optional< std::string_view > Start(const StringList &param) override
Start this driver.
Definition: sdl2_v.cpp:543
bool buffer_locked
Video buffer was locked by the main thread.
Definition: sdl2_v.h:49
Dimension GetScreenSize() const override
Get the resolution of the main screen.
Definition: sdl2_v.cpp:718
bool PollEvent() override
Process a single system event.
Definition: sdl2_v.cpp:380
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:222
virtual void ReleaseVideoPointer()=0
Hand video buffer back to the painting backend.
bool AfterBlitterChange() override
Callback invoked after the blitter was changed.
Definition: sdl2_v.cpp:710
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:599
Palette local_palette
Current palette to use for drawing.
Definition: sdl2_v.h:48
void MainLoop() override
Perform the actual drawing.
Definition: sdl2_v.cpp:659
std::string driver_info
Information string about selected driver.
Definition: sdl2_v.h:51
void UnlockVideoBuffer() override
Unlock a previously locked video buffer.
Definition: sdl2_v.cpp:737
void ClientSizeChanged(int w, int h, bool force)
Indicate to the driver the client-side might have changed.
Definition: sdl2_v.cpp:124
void MakeDirty(int left, int top, int width, int height) override
Mark a particular area dirty.
Definition: sdl2_v.cpp:32
virtual bool CreateMainWindow(uint w, uint h, uint flags=0)
Create the main window.
Definition: sdl2_v.cpp:136
std::vector< int > GetListOfMonitorRefreshRates() override
Get a list of refresh rates of each available monitor.
Definition: sdl2_v.cpp:230
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:86
bool ChangeResolution(int w, int h) override
Change the resolution of the window.
Definition: sdl2_v.cpp:675
void Stop() override
Stop this driver.
Definition: sdl2_v.cpp:591
bool LockVideoBuffer() override
Make sure the video buffer is ready for drawing.
Definition: sdl2_v.cpp:726
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:211
Rect dirty_rect
Rectangle encompassing the dirty area of the video buffer.
Definition: sdl2_v.h:50
void CheckPaletteAnim() override
Process any pending palette animation.
Definition: sdl2_v.cpp:38
virtual void * GetVideoPointer()=0
Get a pointer to the video buffer.
struct SDL_Window * sdl_window
Main SDL window.
Definition: sdl2_v.h:47
bool ToggleFullscreen(bool fullscreen) override
Change the full screen setting.
Definition: sdl2_v.cpp:680
The base of all video drivers.
std::vector< std::string > StringList
Type for a list of strings.
Definition: string_type.h:60
Dimensions (a width and height) of a rectangle in 2D.
Information about the currently used palette.
Definition: gfx_type.h:328
Specification of a rectangle with absolute coordinates of all edges.
Base of all video drivers.