OpenTTD Source  20241121-master-g67a0fccfad
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 */
Base for all driver factories.
Definition: driver.h:57
virtual bool UsesHardwareAcceleration() const
Does the driver use hardware acceleration (video-drivers only).
Definition: driver.h:114
virtual Driver * CreateInstance() const =0
Create an instance of this driver-class.
DriverFactoryBase(Driver::Type type, int priority, const char *name, const char *description)
Construct a new DriverFactory.
Definition: driver.cpp:244
A driver for communicating with the user.
Definition: driver.h:21
virtual std::string_view GetName() const =0
Get the name of this driver.
virtual std::optional< std::string_view > Start(const StringList &parm)=0
Start this driver.
@ DT_VIDEO
A video driver.
Definition: driver.h:42
The factory for Windows' video driver.
Definition: win32_v.h:110
Driver * CreateInstance() const override
Create an instance of this driver-class.
Definition: win32_v.h:113
Base class for Windows video drivers.
Definition: win32_v.h:19
int height
Height in pixels of our display surface.
Definition: win32_v.h:45
bool has_focus
Does our window have system focus?
Definition: win32_v.h:42
void EditBoxLostFocus() override
An edit box lost the input focus.
Definition: win32_v.cpp:930
void CheckPaletteAnim() override
Process any pending palette animation.
Definition: win32_v.cpp:842
void Stop() override
Stop this driver.
Definition: win32_v.cpp:829
int height_org
Original monitor resolution height, before we changed it.
Definition: win32_v.h:47
HWND main_wnd
Handle to system window.
Definition: win32_v.h:40
bool fullscreen
Whether to use (true) fullscreen mode.
Definition: win32_v.h:41
int width_org
Original monitor resolution width, before we changed it.
Definition: win32_v.h:46
bool MakeWindow(bool full_screen, bool resize=true)
Instantiate a new window.
Definition: win32_v.cpp:138
bool buffer_locked
Video buffer was locked by the main thread.
Definition: win32_v.h:49
void MakeDirty(int left, int top, int width, int height) override
Mark a particular area dirty.
Definition: win32_v.cpp:836
bool PollEvent() override
Process a single system event.
Definition: win32_v.cpp:873
virtual void PaletteChanged(HWND hWnd)=0
Palette of the window has changed.
bool LockVideoBuffer() override
Make sure the video buffer is ready for drawing.
Definition: win32_v.cpp:1007
virtual void * GetVideoPointer()=0
Get a pointer to the video buffer.
std::vector< int > GetListOfMonitorRefreshRates() override
Get a list of refresh rates of each available monitor.
Definition: win32_v.cpp:954
virtual bool AllocateBackingStore(int w, int h, bool force=false)=0
(Re-)create the backing store.
int width
Width in pixels of our display surface.
Definition: win32_v.h:44
void InputLoop() override
Handle input logic, is CTRL pressed, should we fast-forward, etc.
Definition: win32_v.cpp:848
virtual uint8_t GetFullscreenBpp()
Get screen depth to use for fullscreen mode.
Definition: win32_v.cpp:126
Dimension GetScreenSize() const override
Get the resolution of the main screen.
Definition: win32_v.cpp:961
virtual void ReleaseVideoPointer()
Hand video buffer back to the painting backend.
Definition: win32_v.h:70
void UnlockVideoBuffer() override
Unlock a previously locked video buffer.
Definition: win32_v.cpp:1018
void MainLoop() override
Perform the actual drawing.
Definition: win32_v.cpp:886
Rect dirty_rect
Region of the screen that needs redrawing.
Definition: win32_v.h:43
float GetDPIScale() override
Get DPI scaling factor of the screen OTTD is displayed on.
Definition: win32_v.cpp:966
bool ToggleFullscreen(bool fullscreen) override
Change the full screen setting.
Definition: win32_v.cpp:922
bool ChangeResolution(int w, int h) override
Change the resolution of the window.
Definition: win32_v.cpp:912
The GDI video driver for windows.
Definition: win32_v.h:78
std::string_view GetName() const override
Get the name of this driver.
Definition: win32_v.h:88
void * buffer_bits
Internal rendering buffer.
Definition: win32_v.h:93
HBITMAP dib_sect
System bitmap object referencing our rendering buffer.
Definition: win32_v.h:91
void PaletteChanged(HWND hWnd) override
Palette of the window has changed.
Definition: win32_v.cpp:1137
std::optional< std::string_view > Start(const StringList &param) override
Start this driver.
Definition: win32_v.cpp:1033
void * GetVideoPointer() override
Get a pointer to the video buffer.
Definition: win32_v.h:96
HPALETTE gdi_palette
Palette object for 8bpp blitter.
Definition: win32_v.h:92
bool AllocateBackingStore(int w, int h, bool force=false) override
(Re-)create the backing store.
Definition: win32_v.cpp:1058
void Paint() override
Paint the window.
Definition: win32_v.cpp:1148
bool AfterBlitterChange() override
Callback invoked after the blitter was changed.
Definition: win32_v.cpp:1096
void Stop() override
Stop this driver.
Definition: win32_v.cpp:1050
The base of all video drivers.
virtual void PopulateSystemSprites()
Populate all sprites in cache.
virtual void ClearSystemSprites()
Clear all cached sprites.
virtual bool UseSystemCursor()
Get whether the mouse cursor is drawn by the video driver.
virtual bool HasAnimBuffer()
Does this video driver support a separate animation buffer in addition to the colour buffer?
virtual bool AfterBlitterChange()
Callback invoked after the blitter was changed.
virtual void ToggleVsync([[maybe_unused]] bool vsync)
Change the vsync setting.
virtual bool HasEfficient8Bpp() const
Has this video driver an efficient code path for palette animated 8-bpp sprites?
virtual void Paint()
Paint the window.
virtual uint8_t * GetAnimBuffer()
Get a pointer to the animation buffer of the video back-end.
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.
Specification of a rectangle with absolute coordinates of all edges.
Base of all video drivers.