OpenTTD Source  1.11.0-beta2
win32_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_WIN32_H
11 #define VIDEO_WIN32_H
12 
13 #include "video_driver.hpp"
14 #include <mutex>
15 #include <condition_variable>
16 
19 public:
20  VideoDriver_Win32Base() : main_wnd(nullptr), fullscreen(false), draw_mutex(nullptr), draw_signal(nullptr) {}
21 
22  void Stop() override;
23 
24  void MakeDirty(int left, int top, int width, int height) override;
25 
26  void MainLoop() override;
27 
28  bool ChangeResolution(int w, int h) override;
29 
30  bool ToggleFullscreen(bool fullscreen) override;
31 
32  void AcquireBlitterLock() override;
33 
34  void ReleaseBlitterLock() override;
35 
36  bool ClaimMousePointer() override;
37 
38  void EditBoxLostFocus() override;
39 
40 protected:
41  HWND main_wnd;
42  bool fullscreen;
43  bool has_focus = false;
45  int width = 0;
46  int height = 0;
47  int width_org = 0;
48  int height_org = 0;
49 
52  volatile bool draw_continue;
53 
54  std::recursive_mutex *draw_mutex;
55  std::condition_variable_any *draw_signal;
56 
57  Dimension GetScreenSize() const override;
58  float GetDPIScale() override;
59  void InputLoop() override;
60  bool LockVideoBuffer() override;
61  void UnlockVideoBuffer() override;
62  void CheckPaletteAnim() override;
63  bool PollEvent() override;
64 
65  void Initialize();
66  bool MakeWindow(bool full_screen);
67  void ClientSizeChanged(int w, int h, bool force = false);
68 
70  virtual uint8 GetFullscreenBpp();
72  virtual bool AllocateBackingStore(int w, int h, bool force = false) = 0;
74  virtual void *GetVideoPointer() = 0;
76  virtual void ReleaseVideoPointer() {}
78  virtual void PaletteChanged(HWND hWnd) = 0;
79 
80 private:
81  std::unique_lock<std::recursive_mutex> draw_lock;
82 
83  static void PaintThreadThunk(VideoDriver_Win32Base *drv);
84 
85  friend LRESULT CALLBACK WndProcGdi(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
86 };
89 public:
90  VideoDriver_Win32GDI() : dib_sect(nullptr), gdi_palette(nullptr), buffer_bits(nullptr) {}
91 
92  const char *Start(const StringList &param) override;
93 
94  void Stop() override;
95 
96  bool AfterBlitterChange() override;
97 
98  const char *GetName() const override { return "win32"; }
99 
100 protected:
101  HBITMAP dib_sect;
102  HPALETTE gdi_palette;
103  void *buffer_bits;
104 
105  void Paint() override;
106  void *GetVideoPointer() override { return this->buffer_bits; }
107  void PaintThread() override;
108 
109  bool AllocateBackingStore(int w, int h, bool force = false) override;
110  void PaletteChanged(HWND hWnd) override;
111  void MakePalette();
112  void UpdatePalette(HDC dc, uint start, uint count);
113 
114 #ifdef _DEBUG
115 public:
116  static int RedrawScreenDebug();
117 #endif
118 };
119 
122 public:
123  FVideoDriver_Win32GDI() : DriverFactoryBase(Driver::DT_VIDEO, 9, "win32", "Win32 GDI Video Driver") {}
124  Driver *CreateInstance() const override { return new VideoDriver_Win32GDI(); }
125 };
126 
127 #ifdef WITH_OPENGL
128 
130 class VideoDriver_Win32OpenGL : public VideoDriver_Win32Base {
131 public:
132  VideoDriver_Win32OpenGL() : dc(nullptr), gl_rc(nullptr), anim_buffer(nullptr) {}
133 
134  const char *Start(const StringList &param) override;
135 
136  void Stop() override;
137 
138  bool ToggleFullscreen(bool fullscreen) override;
139 
140  bool AfterBlitterChange() override;
141 
142  bool HasEfficient8Bpp() const override { return true; }
143 
144  bool UseSystemCursor() override { return true; }
145 
146  void ClearSystemSprites() override;
147 
148  bool HasAnimBuffer() override { return true; }
149  uint8 *GetAnimBuffer() override { return this->anim_buffer; }
150 
151  const char *GetName() const override { return "win32-opengl"; }
152 
153 protected:
154  HDC dc;
155  HGLRC gl_rc;
156  bool vsync;
157  uint8 *anim_buffer;
158 
159  uint8 GetFullscreenBpp() override { return 32; } // OpenGL is always 32 bpp.
160 
161  void Paint() override;
162  void PaintThread() override {}
163 
164  bool AllocateBackingStore(int w, int h, bool force = false) override;
165  void *GetVideoPointer() override;
166  void ReleaseVideoPointer() override;
167  void PaletteChanged(HWND hWnd) override {}
168 
169  const char *AllocateContext();
170  void DestroyContext();
171 };
172 
174 class FVideoDriver_Win32OpenGL : public DriverFactoryBase {
175 public:
176  FVideoDriver_Win32OpenGL() : DriverFactoryBase(Driver::DT_VIDEO, 10, "win32-opengl", "Win32 OpenGL Video Driver") {}
177  /* virtual */ Driver *CreateInstance() const override { return new VideoDriver_Win32OpenGL(); }
178 };
179 
180 #endif /* WITH_OPENGL */
181 
182 #endif /* VIDEO_WIN32_H */
VideoDriver_Win32Base::AllocateBackingStore
virtual bool AllocateBackingStore(int w, int h, bool force=false)=0
(Re-)create the backing store.
VideoDriver_Win32Base::MainLoop
void MainLoop() override
Perform the actual drawing.
Definition: win32_v.cpp:867
VideoDriver::HasAnimBuffer
virtual bool HasAnimBuffer()
Does this video driver support a separate animation buffer in addition to the colour buffer?
Definition: video_driver.hpp:129
VideoDriver_Win32Base
Base class for Windows video drivers.
Definition: win32_v.h:18
Driver::GetName
virtual const char * GetName() const =0
Get the name of this driver.
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_Win32GDI::Paint
void Paint() override
Paint the window.
Definition: win32_v.cpp:1178
VideoDriver_Win32Base::EditBoxLostFocus
void EditBoxLostFocus() override
An edit box lost the input focus.
Definition: win32_v.cpp:981
VideoDriver_Win32Base::width
int width
Width in pixels of our display surface.
Definition: win32_v.h:45
VideoDriver_Win32Base::ReleaseVideoPointer
virtual void ReleaseVideoPointer()
Hand video buffer back to the painting backend.
Definition: win32_v.h:76
DriverFactoryBase::DriverFactoryBase
DriverFactoryBase(Driver::Type type, int priority, const char *name, const char *description)
Construct a new DriverFactory.
Definition: driver.cpp:206
VideoDriver_Win32Base::PollEvent
bool PollEvent() override
Process a single system event.
Definition: win32_v.cpp:854
VideoDriver_Win32Base::draw_threaded
bool draw_threaded
Whether the drawing is/may be done in a separate thread.
Definition: win32_v.h:50
VideoDriver_Win32Base::draw_signal
std::condition_variable_any * draw_signal
Signal to draw the next frame.
Definition: win32_v.h:55
DriverFactoryBase::CreateInstance
virtual Driver * CreateInstance() const =0
Create an instance of this driver-class.
VideoDriver_Win32Base::draw_mutex
std::recursive_mutex * draw_mutex
Mutex to keep the access to the shared memory controlled.
Definition: win32_v.h:54
VideoDriver_Win32Base::main_wnd
HWND main_wnd
Handle to system window.
Definition: win32_v.h:41
VideoDriver_Win32Base::AcquireBlitterLock
void AcquireBlitterLock() override
Acquire any lock(s) required to be held when changing blitters.
Definition: win32_v.cpp:971
VideoDriver_Win32Base::buffer_locked
bool buffer_locked
Video buffer was locked by the main thread.
Definition: win32_v.h:51
VideoDriver_Win32GDI::AllocateBackingStore
bool AllocateBackingStore(int w, int h, bool force=false) override
(Re-)create the backing store.
Definition: win32_v.cpp:1090
VideoDriver_Win32Base::GetScreenSize
Dimension GetScreenSize() const override
Get the resolution of the main screen.
Definition: win32_v.cpp:991
VideoDriver_Win32Base::height_org
int height_org
Original monitor resolution height, before we changed it.
Definition: win32_v.h:48
VideoDriver_Win32Base::height
int height
Height in pixels of our display surface.
Definition: win32_v.h:46
VideoDriver::ClearSystemSprites
virtual void ClearSystemSprites()
Clear all cached sprites.
Definition: video_driver.hpp:101
VideoDriver_Win32GDI::buffer_bits
void * buffer_bits
Internal rendering buffer.
Definition: win32_v.h:103
VideoDriver_Win32GDI::dib_sect
HBITMAP dib_sect
System bitmap object referencing our rendering buffer.
Definition: win32_v.h:101
VideoDriver_Win32Base::Stop
void Stop() override
Stop this driver.
Definition: win32_v.cpp:804
VideoDriver_Win32Base::PaletteChanged
virtual void PaletteChanged(HWND hWnd)=0
Palette of the window has changed.
VideoDriver_Win32GDI::PaletteChanged
void PaletteChanged(HWND hWnd) override
Palette of the window has changed.
Definition: win32_v.cpp:1167
VideoDriver::Paint
virtual void Paint()
Paint the window.
Definition: video_driver.hpp:241
VideoDriver_Win32GDI::GetName
const char * GetName() const override
Get the name of this driver.
Definition: win32_v.h:98
Driver::Start
virtual const char * Start(const StringList &parm)=0
Start this driver.
VideoDriver_Win32Base::CheckPaletteAnim
void CheckPaletteAnim() override
Process any pending palette animation.
Definition: win32_v.cpp:817
FVideoDriver_Win32GDI
The factory for Windows' video driver.
Definition: win32_v.h:121
VideoDriver_Win32Base::has_focus
bool has_focus
Does our window have system focus?
Definition: win32_v.h:43
StringList
std::vector< std::string > StringList
Type for a list of strings.
Definition: string_type.h:58
VideoDriver_Win32Base::GetFullscreenBpp
virtual uint8 GetFullscreenBpp()
Get screen depth to use for fullscreen mode.
Definition: win32_v.cpp:122
VideoDriver_Win32GDI::Stop
void Stop() override
Stop this driver.
Definition: win32_v.cpp:1082
VideoDriver_Win32Base::ToggleFullscreen
bool ToggleFullscreen(bool fullscreen) override
Change the full screen setting.
Definition: win32_v.cpp:963
VideoDriver_Win32Base::UnlockVideoBuffer
void UnlockVideoBuffer() override
Unlock a previously locked video buffer.
Definition: win32_v.cpp:1049
VideoDriver::GetAnimBuffer
virtual uint8 * GetAnimBuffer()
Get a pointer to the animation buffer of the video back-end.
Definition: video_driver.hpp:138
VideoDriver::UseSystemCursor
virtual bool UseSystemCursor()
Get whether the mouse cursor is drawn by the video driver.
Definition: video_driver.hpp:93
VideoDriver::PaintThread
virtual void PaintThread()
Thread function for threaded drawing.
Definition: video_driver.hpp:246
VideoDriver_Win32Base::width_org
int width_org
Original monitor resolution width, before we changed it.
Definition: win32_v.h:47
VideoDriver_Win32Base::ReleaseBlitterLock
void ReleaseBlitterLock() override
Release any lock(s) required to be held when changing blitters.
Definition: win32_v.cpp:976
VideoDriver_Win32GDI::Start
const char * Start(const StringList &param) override
Start this driver.
Definition: win32_v.cpp:1065
VideoDriver_Win32Base::fullscreen
bool fullscreen
Whether to use (true) fullscreen mode.
Definition: win32_v.h:42
VideoDriver_Win32Base::GetVideoPointer
virtual void * GetVideoPointer()=0
Get a pointer to the video buffer.
video_driver.hpp
Driver::DT_VIDEO
@ DT_VIDEO
A video driver.
Definition: driver.h:44
VideoDriver_Win32Base::MakeDirty
void MakeDirty(int left, int top, int width, int height) override
Mark a particular area dirty.
Definition: win32_v.cpp:811
VideoDriver_Win32Base::draw_continue
volatile bool draw_continue
Should we keep continue drawing?
Definition: win32_v.h:52
FVideoDriver_Win32GDI::CreateInstance
Driver * CreateInstance() const override
Create an instance of this driver-class.
Definition: win32_v.h:124
VideoDriver_Win32Base::LockVideoBuffer
bool LockVideoBuffer() override
Make sure the video buffer is ready for drawing.
Definition: win32_v.cpp:1036
VideoDriver_Win32Base::MakeWindow
bool MakeWindow(bool full_screen)
Instantiate a new window.
Definition: win32_v.cpp:133
VideoDriver_Win32GDI
The GDI video driver for windows.
Definition: win32_v.h:88
VideoDriver_Win32Base::dirty_rect
Rect dirty_rect
Region of the screen that needs redrawing.
Definition: win32_v.h:44
VideoDriver_Win32Base::ChangeResolution
bool ChangeResolution(int w, int h) override
Change the resolution of the window.
Definition: win32_v.cpp:950
VideoDriver_Win32GDI::AfterBlitterChange
bool AfterBlitterChange() override
Callback invoked after the blitter was changed.
Definition: win32_v.cpp:1125
VideoDriver::HasEfficient8Bpp
virtual bool HasEfficient8Bpp() const
Has this video driver an efficient code path for palette animated 8-bpp sprites?
Definition: video_driver.hpp:120
VideoDriver_Win32GDI::PaintThread
void PaintThread() override
Thread function for threaded drawing.
Definition: win32_v.cpp:1228
Driver
A driver for communicating with the user.
Definition: driver.h:23
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition: geometry_type.hpp:47
VideoDriver_Win32Base::GetDPIScale
float GetDPIScale() override
Get DPI scaling factor of the screen OTTD is displayed on.
Definition: win32_v.cpp:996
VideoDriver_Win32GDI::GetVideoPointer
void * GetVideoPointer() override
Get a pointer to the video buffer.
Definition: win32_v.h:106
VideoDriver_Win32Base::InputLoop
void InputLoop() override
Handle input logic, is CTRL pressed, should we fast-forward, etc.
Definition: win32_v.cpp:825
DriverFactoryBase
Base for all driver factories.
Definition: driver.h:59
VideoDriver::AfterBlitterChange
virtual bool AfterBlitterChange()
Callback invoked after the blitter was changed.
Definition: video_driver.hpp:67
VideoDriver_Win32GDI::gdi_palette
HPALETTE gdi_palette
Palette object for 8bpp blitter.
Definition: win32_v.h:102