OpenTTD Source  1.11.0-beta2
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() : sdl_window(nullptr) {}
21 
22  const char *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  void AcquireBlitterLock() override;
37 
38  void ReleaseBlitterLock() override;
39 
40  bool ClaimMousePointer() override;
41 
42  void EditBoxGainedFocus() override;
43 
44  void EditBoxLostFocus() override;
45 
46  const char *GetName() const override { return "sdl"; }
47 
48 protected:
49  struct SDL_Window *sdl_window;
52  std::recursive_mutex *draw_mutex = nullptr;
53  std::condition_variable_any *draw_signal = nullptr;
54  volatile bool draw_continue;
57 
58  Dimension GetScreenSize() const override;
59  void InputLoop() override;
60  bool LockVideoBuffer() override;
61  void UnlockVideoBuffer() override;
62  void CheckPaletteAnim() override;
63  bool PollEvent() override;
64 
66  void ClientSizeChanged(int w, int h, bool force);
67 
69  virtual bool AllocateBackingStore(int w, int h, bool force = false) = 0;
71  virtual void *GetVideoPointer() = 0;
73  virtual void ReleaseVideoPointer() = 0;
75  virtual bool CreateMainWindow(uint w, uint h, uint flags = 0);
76 
77 private:
78  void LoopOnce();
79  void MainLoopCleanup();
80  bool CreateMainSurface(uint w, uint h, bool resize);
81  const char *Initialize();
82 
83 #ifdef __EMSCRIPTEN__
84  /* Convert a constant pointer back to a non-constant pointer to a member function. */
85  static void EmscriptenLoop(void *self) { ((VideoDriver_SDL_Base *)self)->LoopOnce(); }
86 #endif
87 
92 
93  int startup_display;
94  std::thread draw_thread;
95  std::unique_lock<std::recursive_mutex> draw_lock;
96 
97  static void PaintThreadThunk(VideoDriver_SDL_Base *drv);
98 };
99 
100 #endif /* VIDEO_SDL_H */
VideoDriver_SDL_Base::UnlockVideoBuffer
void UnlockVideoBuffer() override
Unlock a previously locked video buffer.
Definition: sdl2_v.cpp:806
VideoDriver_SDL_Base::ReleaseBlitterLock
void ReleaseBlitterLock() override
Release any lock(s) required to be held when changing blitters.
Definition: sdl2_v.cpp:780
VideoDriver_SDL_Base::draw_threaded
bool draw_threaded
Whether the drawing is/may be done in a separate thread.
Definition: sdl2_v.h:51
VideoDriver
The base of all video drivers.
Definition: video_driver.hpp:28
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition: geometry_type.hpp:27
VideoDriver_SDL_Base::ReleaseVideoPointer
virtual void ReleaseVideoPointer()=0
Hand video buffer back to the painting backend.
VideoDriver_SDL_Base::MakeDirty
void MakeDirty(int left, int top, int width, int height) override
Mark a particular area dirty.
Definition: sdl2_v.cpp:39
VideoDriver_SDL_Base::LockVideoBuffer
bool LockVideoBuffer() override
Make sure the video buffer is ready for drawing.
Definition: sdl2_v.cpp:793
VideoDriver_SDL_Base::EditBoxGainedFocus
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:227
VideoDriver_SDL_Base::AllocateBackingStore
virtual bool AllocateBackingStore(int w, int h, bool force=false)=0
(Re-)create the backing store.
VideoDriver_SDL_Base::MainLoop
void MainLoop() override
Perform the actual drawing.
Definition: sdl2_v.cpp:654
VideoDriver_SDL_Base::GetName
const char * GetName() const override
Get the name of this driver.
Definition: sdl2_v.h:46
VideoDriver_SDL_Base::Stop
void Stop() override
Stop this driver.
Definition: sdl2_v.cpp:587
VideoDriver_SDL_Base::InputLoop
void InputLoop() override
Handle input logic, is CTRL pressed, should we fast-forward, etc.
Definition: sdl2_v.cpp:595
VideoDriver_SDL_Base::draw_signal
std::condition_variable_any * draw_signal
Signal to draw the next frame.
Definition: sdl2_v.h:53
VideoDriver_SDL_Base::AfterBlitterChange
bool AfterBlitterChange() override
Callback invoked after the blitter was changed.
Definition: sdl2_v.cpp:767
VideoDriver_SDL_Base::local_palette
Palette local_palette
Copy of _cur_palette.
Definition: sdl2_v.h:50
VideoDriver_SDL_Base::PollEvent
bool PollEvent() override
Process a single system event.
Definition: sdl2_v.cpp:372
StringList
std::vector< std::string > StringList
Type for a list of strings.
Definition: string_type.h:58
VideoDriver_SDL_Base::ToggleFullscreen
bool ToggleFullscreen(bool fullscreen) override
Change the full screen setting.
Definition: sdl2_v.cpp:734
VideoDriver_SDL_Base::CheckPaletteAnim
void CheckPaletteAnim() override
Process any pending palette animation.
Definition: sdl2_v.cpp:45
VideoDriver_SDL_Base::CreateMainWindow
virtual bool CreateMainWindow(uint w, uint h, uint flags=0)
Create the main window.
Definition: sdl2_v.cpp:151
VideoDriver_SDL_Base::draw_mutex
std::recursive_mutex * draw_mutex
Mutex to keep the access to the shared memory controlled.
Definition: sdl2_v.h:52
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:136
VideoDriver_SDL_Base::EditBoxLostFocus
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:238
video_driver.hpp
VideoDriver_SDL_Base::dirty_rect
Rect dirty_rect
Rectangle encompassing the dirty area of the video buffer.
Definition: sdl2_v.h:56
VideoDriver_SDL_Base
The SDL video driver.
Definition: sdl2_v.h:18
VideoDriver_SDL_Base::edit_box_focused
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:91
VideoDriver_SDL_Base::draw_continue
volatile bool draw_continue
Should we keep continue drawing?
Definition: sdl2_v.h:54
VideoDriver_SDL_Base::buffer_locked
bool buffer_locked
Video buffer was locked by the main thread.
Definition: sdl2_v.h:55
VideoDriver_SDL_Base::AcquireBlitterLock
void AcquireBlitterLock() override
Acquire any lock(s) required to be held when changing blitters.
Definition: sdl2_v.cpp:775
VideoDriver_SDL_Base::ChangeResolution
bool ChangeResolution(int w, int h) override
Change the resolution of the window.
Definition: sdl2_v.cpp:726
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition: geometry_type.hpp:47
Palette
Information about the currently used palette.
Definition: gfx_type.h:313
VideoDriver_SDL_Base::GetScreenSize
Dimension GetScreenSize() const override
Get the resolution of the main screen.
Definition: sdl2_v.cpp:785
VideoDriver_SDL_Base::sdl_window
struct SDL_Window * sdl_window
Main SDL window.
Definition: sdl2_v.h:49
VideoDriver_SDL_Base::GetVideoPointer
virtual void * GetVideoPointer()=0
Get a pointer to the video buffer.
VideoDriver_SDL_Base::Start
const char * Start(const StringList &param) override
Start this driver.
Definition: sdl2_v.cpp:550