OpenTTD Source  20240919-master-gdf0233f4c2
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 #include <windows.h>
17 
20 public:
21  VideoDriver_Win32Base(bool uses_hardware_acceleration = false) : VideoDriver(uses_hardware_acceleration), main_wnd(nullptr), fullscreen(false), buffer_locked(false) {}
22 
23  void Stop() override;
24 
25  void MakeDirty(int left, int top, int width, int height) override;
26 
27  void MainLoop() override;
28 
29  bool ChangeResolution(int w, int h) override;
30 
31  bool ToggleFullscreen(bool fullscreen) override;
32 
33  bool ClaimMousePointer() override;
34 
35  void EditBoxLostFocus() override;
36 
37  std::vector<int> GetListOfMonitorRefreshRates() override;
38 
39 protected:
40  HWND main_wnd;
41  bool fullscreen;
42  bool has_focus = false;
44  int width = 0;
45  int height = 0;
46  int width_org = 0;
47  int height_org = 0;
48 
50 
51  Dimension GetScreenSize() const override;
52  float GetDPIScale() override;
53  void InputLoop() override;
54  bool LockVideoBuffer() override;
55  void UnlockVideoBuffer() override;
56  void CheckPaletteAnim() override;
57  bool PollEvent() override;
58 
59  void Initialize();
60  bool MakeWindow(bool full_screen, bool resize = true);
61  void ClientSizeChanged(int w, int h, bool force = false);
62 
64  virtual uint8_t GetFullscreenBpp();
66  virtual bool AllocateBackingStore(int w, int h, bool force = false) = 0;
68  virtual void *GetVideoPointer() = 0;
70  virtual void ReleaseVideoPointer() {}
72  virtual void PaletteChanged(HWND hWnd) = 0;
73 
74 private:
75  friend LRESULT CALLBACK WndProcGdi(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
76 };
79 public:
80  VideoDriver_Win32GDI() : dib_sect(nullptr), gdi_palette(nullptr), buffer_bits(nullptr) {}
81 
82  std::optional<std::string_view> Start(const StringList &param) override;
83 
84  void Stop() override;
85 
86  bool AfterBlitterChange() override;
87 
88  std::string_view GetName() const override { return "win32"; }
89 
90 protected:
91  HBITMAP dib_sect;
92  HPALETTE gdi_palette;
93  void *buffer_bits;
94 
95  void Paint() override;
96  void *GetVideoPointer() override { return this->buffer_bits; }
97 
98  bool AllocateBackingStore(int w, int h, bool force = false) override;
99  void PaletteChanged(HWND hWnd) override;
100  void MakePalette();
101  void UpdatePalette(HDC dc, uint start, uint count);
102 
103 #ifdef _DEBUG
104 public:
105  static int RedrawScreenDebug();
106 #endif
107 };
108 
111 public:
112  FVideoDriver_Win32GDI() : DriverFactoryBase(Driver::DT_VIDEO, 9, "win32", "Win32 GDI Video Driver") {}
113  Driver *CreateInstance() const override { return new VideoDriver_Win32GDI(); }
114 };
115 
116 #ifdef WITH_OPENGL
117 
119 class VideoDriver_Win32OpenGL : public VideoDriver_Win32Base {
120 public:
121  VideoDriver_Win32OpenGL() : VideoDriver_Win32Base(true), dc(nullptr), gl_rc(nullptr), anim_buffer(nullptr), driver_info(this->GetName()) {}
122 
123  std::optional<std::string_view> Start(const StringList &param) override;
124 
125  void Stop() override;
126 
127  bool ToggleFullscreen(bool fullscreen) override;
128 
129  bool AfterBlitterChange() override;
130 
131  bool HasEfficient8Bpp() const override { return true; }
132 
133  bool UseSystemCursor() override { return true; }
134 
135  void PopulateSystemSprites() override;
136 
137  void ClearSystemSprites() override;
138 
139  bool HasAnimBuffer() override { return true; }
140  uint8_t *GetAnimBuffer() override { return this->anim_buffer; }
141 
142  void ToggleVsync(bool vsync) override;
143 
144  std::string_view GetName() const override { return "win32-opengl"; }
145 
146  std::string_view GetInfoString() const override { return this->driver_info; }
147 
148 protected:
149  HDC dc;
150  HGLRC gl_rc;
151  uint8_t *anim_buffer;
152  std::string driver_info;
153 
154  uint8_t GetFullscreenBpp() override { return 32; } // OpenGL is always 32 bpp.
155 
156  void Paint() override;
157 
158  bool AllocateBackingStore(int w, int h, bool force = false) override;
159  void *GetVideoPointer() override;
160  void ReleaseVideoPointer() override;
161  void PaletteChanged(HWND) override {}
162 
163  std::optional<std::string_view> AllocateContext();
164  void DestroyContext();
165 };
166 
168 class FVideoDriver_Win32OpenGL : public DriverFactoryBase {
169 public:
170  FVideoDriver_Win32OpenGL() : DriverFactoryBase(Driver::DT_VIDEO, 10, "win32-opengl", "Win32 OpenGL Video Driver") {}
171  /* virtual */ Driver *CreateInstance() const override { return new VideoDriver_Win32OpenGL(); }
172 
173 protected:
174  bool UsesHardwareAcceleration() const override { return true; }
175 };
176 
177 #endif /* WITH_OPENGL */
178 
179 #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:886
VideoDriver::HasAnimBuffer
virtual bool HasAnimBuffer()
Does this video driver support a separate animation buffer in addition to the colour buffer?
Definition: video_driver.hpp:135
VideoDriver_Win32Base
Base class for Windows video drivers.
Definition: win32_v.h:19
VideoDriver
The base of all video drivers.
Definition: video_driver.hpp:34
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition: geometry_type.hpp:30
VideoDriver_Win32GDI::Paint
void Paint() override
Paint the window.
Definition: win32_v.cpp:1148
VideoDriver_Win32Base::EditBoxLostFocus
void EditBoxLostFocus() override
An edit box lost the input focus.
Definition: win32_v.cpp:930
VideoDriver_Win32Base::width
int width
Width in pixels of our display surface.
Definition: win32_v.h:44
VideoDriver_Win32Base::ReleaseVideoPointer
virtual void ReleaseVideoPointer()
Hand video buffer back to the painting backend.
Definition: win32_v.h:70
DriverFactoryBase::DriverFactoryBase
DriverFactoryBase(Driver::Type type, int priority, const char *name, const char *description)
Construct a new DriverFactory.
Definition: driver.cpp:244
VideoDriver_Win32Base::PollEvent
bool PollEvent() override
Process a single system event.
Definition: win32_v.cpp:873
VideoDriver::ToggleVsync
virtual void ToggleVsync([[maybe_unused]] bool vsync)
Change the vsync setting.
Definition: video_driver.hpp:74
DriverFactoryBase::CreateInstance
virtual Driver * CreateInstance() const =0
Create an instance of this driver-class.
VideoDriver_Win32Base::main_wnd
HWND main_wnd
Handle to system window.
Definition: win32_v.h:40
VideoDriver_Win32Base::buffer_locked
bool buffer_locked
Video buffer was locked by the main thread.
Definition: win32_v.h:49
VideoDriver_Win32GDI::AllocateBackingStore
bool AllocateBackingStore(int w, int h, bool force=false) override
(Re-)create the backing store.
Definition: win32_v.cpp:1058
VideoDriver_Win32Base::GetScreenSize
Dimension GetScreenSize() const override
Get the resolution of the main screen.
Definition: win32_v.cpp:961
VideoDriver_Win32Base::height_org
int height_org
Original monitor resolution height, before we changed it.
Definition: win32_v.h:47
VideoDriver_Win32Base::height
int height
Height in pixels of our display surface.
Definition: win32_v.h:45
VideoDriver::ClearSystemSprites
virtual void ClearSystemSprites()
Clear all cached sprites.
Definition: video_driver.hpp:107
VideoDriver_Win32GDI::buffer_bits
void * buffer_bits
Internal rendering buffer.
Definition: win32_v.h:93
VideoDriver_Win32GDI::dib_sect
HBITMAP dib_sect
System bitmap object referencing our rendering buffer.
Definition: win32_v.h:91
VideoDriver_Win32Base::Stop
void Stop() override
Stop this driver.
Definition: win32_v.cpp:829
VideoDriver_Win32Base::PaletteChanged
virtual void PaletteChanged(HWND hWnd)=0
Palette of the window has changed.
VideoDriver_Win32Base::GetListOfMonitorRefreshRates
std::vector< int > GetListOfMonitorRefreshRates() override
Get a list of refresh rates of each available monitor.
Definition: win32_v.cpp:954
VideoDriver_Win32GDI::PaletteChanged
void PaletteChanged(HWND hWnd) override
Palette of the window has changed.
Definition: win32_v.cpp:1137
VideoDriver::Paint
virtual void Paint()
Paint the window.
Definition: video_driver.hpp:278
VideoDriver_Win32GDI::GetName
std::string_view GetName() const override
Get the name of this driver.
Definition: win32_v.h:88
VideoDriver_Win32Base::CheckPaletteAnim
void CheckPaletteAnim() override
Process any pending palette animation.
Definition: win32_v.cpp:842
VideoDriver_Win32Base::GetFullscreenBpp
virtual uint8_t GetFullscreenBpp()
Get screen depth to use for fullscreen mode.
Definition: win32_v.cpp:126
FVideoDriver_Win32GDI
The factory for Windows' video driver.
Definition: win32_v.h:110
VideoDriver::GetAnimBuffer
virtual uint8_t * GetAnimBuffer()
Get a pointer to the animation buffer of the video back-end.
Definition: video_driver.hpp:144
VideoDriver_Win32Base::has_focus
bool has_focus
Does our window have system focus?
Definition: win32_v.h:42
StringList
std::vector< std::string > StringList
Type for a list of strings.
Definition: string_type.h:60
VideoDriver_Win32Base::MakeWindow
bool MakeWindow(bool full_screen, bool resize=true)
Instantiate a new window.
Definition: win32_v.cpp:138
VideoDriver_Win32GDI::Start
std::optional< std::string_view > Start(const StringList &param) override
Start this driver.
Definition: win32_v.cpp:1033
VideoDriver_Win32GDI::Stop
void Stop() override
Stop this driver.
Definition: win32_v.cpp:1050
VideoDriver_Win32Base::ToggleFullscreen
bool ToggleFullscreen(bool fullscreen) override
Change the full screen setting.
Definition: win32_v.cpp:922
VideoDriver_Win32Base::UnlockVideoBuffer
void UnlockVideoBuffer() override
Unlock a previously locked video buffer.
Definition: win32_v.cpp:1018
VideoDriver::UseSystemCursor
virtual bool UseSystemCursor()
Get whether the mouse cursor is drawn by the video driver.
Definition: video_driver.hpp:94
VideoDriver_Win32Base::width_org
int width_org
Original monitor resolution width, before we changed it.
Definition: win32_v.h:46
VideoDriver::PopulateSystemSprites
virtual void PopulateSystemSprites()
Populate all sprites in cache.
Definition: video_driver.hpp:102
Driver::Start
virtual std::optional< std::string_view > Start(const StringList &parm)=0
Start this driver.
Driver::GetName
virtual std::string_view GetName() const =0
Get the name of this driver.
VideoDriver_Win32Base::fullscreen
bool fullscreen
Whether to use (true) fullscreen mode.
Definition: win32_v.h:41
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:42
VideoDriver_Win32Base::MakeDirty
void MakeDirty(int left, int top, int width, int height) override
Mark a particular area dirty.
Definition: win32_v.cpp:836
FVideoDriver_Win32GDI::CreateInstance
Driver * CreateInstance() const override
Create an instance of this driver-class.
Definition: win32_v.h:113
DriverFactoryBase::UsesHardwareAcceleration
virtual bool UsesHardwareAcceleration() const
Does the driver use hardware acceleration (video-drivers only).
Definition: driver.h:114
VideoDriver_Win32Base::LockVideoBuffer
bool LockVideoBuffer() override
Make sure the video buffer is ready for drawing.
Definition: win32_v.cpp:1007
VideoDriver_Win32GDI
The GDI video driver for windows.
Definition: win32_v.h:78
VideoDriver_Win32Base::dirty_rect
Rect dirty_rect
Region of the screen that needs redrawing.
Definition: win32_v.h:43
VideoDriver_Win32Base::ChangeResolution
bool ChangeResolution(int w, int h) override
Change the resolution of the window.
Definition: win32_v.cpp:912
VideoDriver_Win32GDI::AfterBlitterChange
bool AfterBlitterChange() override
Callback invoked after the blitter was changed.
Definition: win32_v.cpp:1096
VideoDriver::HasEfficient8Bpp
virtual bool HasEfficient8Bpp() const
Has this video driver an efficient code path for palette animated 8-bpp sprites?
Definition: video_driver.hpp:126
Driver
A driver for communicating with the user.
Definition: driver.h:21
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition: geometry_type.hpp:75
VideoDriver_Win32Base::GetDPIScale
float GetDPIScale() override
Get DPI scaling factor of the screen OTTD is displayed on.
Definition: win32_v.cpp:966
VideoDriver_Win32GDI::GetVideoPointer
void * GetVideoPointer() override
Get a pointer to the video buffer.
Definition: win32_v.h:96
VideoDriver_Win32Base::InputLoop
void InputLoop() override
Handle input logic, is CTRL pressed, should we fast-forward, etc.
Definition: win32_v.cpp:848
DriverFactoryBase
Base for all driver factories.
Definition: driver.h:57
VideoDriver::AfterBlitterChange
virtual bool AfterBlitterChange()
Callback invoked after the blitter was changed.
Definition: video_driver.hpp:80
VideoDriver_Win32GDI::gdi_palette
HPALETTE gdi_palette
Palette object for 8bpp blitter.
Definition: win32_v.h:92