OpenTTD Source 20260211-master-g04436b7401
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
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
19class VideoDriver_Win32Base : public VideoDriver {
20public:
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 void ClaimMousePointer() override;
34
35 void EditBoxLostFocus() override;
36
37 std::vector<int> GetListOfMonitorRefreshRates() override;
38
39protected:
40 HWND main_wnd;
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 void InputLoop() override;
53 bool LockVideoBuffer() override;
54 void UnlockVideoBuffer() override;
55 void CheckPaletteAnim() override;
56 bool PollEvent() override;
57
58 void Initialize();
59 bool MakeWindow(bool full_screen, bool resize = true);
60 void ClientSizeChanged(int w, int h, bool force = false);
61
62 virtual uint8_t GetFullscreenBpp();
63
71 virtual bool AllocateBackingStore(int w, int h, bool force = false) = 0;
72
77 virtual void *GetVideoPointer() = 0;
78
80 virtual void ReleaseVideoPointer() {}
81
86 virtual void PaletteChanged(HWND hWnd) = 0;
87
88private:
89 friend LRESULT CALLBACK WndProcGdi(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
90};
91
92class VideoDriver_Win32GDI : public VideoDriver_Win32Base {
93public:
94 VideoDriver_Win32GDI() : dib_sect(nullptr), gdi_palette(nullptr), buffer_bits(nullptr) {}
95
96 std::optional<std::string_view> Start(const StringList &param) override;
97
98 void Stop() override;
99
100 bool AfterBlitterChange() override;
101
102 std::string_view GetName() const override { return "win32"; }
103
104protected:
105 HBITMAP dib_sect;
106 HPALETTE gdi_palette;
108
109 void Paint() override;
110 void *GetVideoPointer() override { return this->buffer_bits; }
111
112 bool AllocateBackingStore(int w, int h, bool force = false) override;
113 void PaletteChanged(HWND hWnd) override;
114 void MakePalette();
115 void UpdatePalette(HDC dc, uint start, uint count);
116
117#ifdef _DEBUG
118public:
119 static int RedrawScreenDebug();
120#endif
121};
122
124class FVideoDriver_Win32GDI : public DriverFactoryBase {
125public:
126 FVideoDriver_Win32GDI() : DriverFactoryBase(Driver::DT_VIDEO, 9, "win32", "Win32 GDI Video Driver") {}
127 std::unique_ptr<Driver> CreateInstance() const override { return std::make_unique<VideoDriver_Win32GDI>(); }
128};
129
130#ifdef WITH_OPENGL
131
133class VideoDriver_Win32OpenGL : public VideoDriver_Win32Base {
134public:
135 VideoDriver_Win32OpenGL() : VideoDriver_Win32Base(true), dc(nullptr), gl_rc(nullptr), anim_buffer(nullptr), driver_info(this->GetName()) {}
136
137 std::optional<std::string_view> Start(const StringList &param) override;
138
139 void Stop() override;
140
141 bool ToggleFullscreen(bool fullscreen) override;
142
143 bool AfterBlitterChange() override;
144
145 bool HasEfficient8Bpp() const override { return true; }
146
147 bool UseSystemCursor() override { return true; }
148
149 void PopulateSystemSprites() override;
150
151 void ClearSystemSprites() override;
152
153 bool HasAnimBuffer() override { return true; }
154 uint8_t *GetAnimBuffer() override { return this->anim_buffer; }
155
156 void ToggleVsync(bool vsync) override;
157
158 std::string_view GetName() const override { return "win32-opengl"; }
159
160 std::string_view GetInfoString() const override { return this->driver_info; }
161
162protected:
163 HDC dc;
164 HGLRC gl_rc;
165 uint8_t *anim_buffer;
166 std::string driver_info;
167
168 uint8_t GetFullscreenBpp() override { return 32; } // OpenGL is always 32 bpp.
169
170 void Paint() override;
171
172 bool AllocateBackingStore(int w, int h, bool force = false) override;
173 void *GetVideoPointer() override;
174 void ReleaseVideoPointer() override;
175 void PaletteChanged(HWND) override {}
176
177 std::optional<std::string_view> AllocateContext();
178 void DestroyContext();
179};
180
182class FVideoDriver_Win32OpenGL : public DriverFactoryBase {
183public:
184 FVideoDriver_Win32OpenGL() : DriverFactoryBase(Driver::DT_VIDEO, 10, "win32-opengl", "Win32 OpenGL Video Driver") {}
185 std::unique_ptr<Driver> CreateInstance() const override { return std::make_unique<VideoDriver_Win32OpenGL>(); }
186
187protected:
188 bool UsesHardwareAcceleration() const override { return true; }
189};
190
191#endif /* WITH_OPENGL */
192
193#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
DriverFactoryBase(Driver::Type type, int priority, std::string_view name, std::string_view description)
Construct a new DriverFactory.
Definition driver.cpp:246
virtual std::unique_ptr< Driver > CreateInstance() const =0
Create an instance of this driver-class.
virtual std::string_view GetName() const =0
Get the name of this driver.
@ DT_VIDEO
A video driver.
Definition driver.h:42
virtual std::optional< std::string_view > Start(const StringList &parm)=0
Start this driver.
std::unique_ptr< Driver > CreateInstance() const override
Create an instance of this driver-class.
Definition win32_v.h:127
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:1055
void CheckPaletteAnim() override
Process any pending palette animation.
Definition win32_v.cpp:967
void Stop() override
Stop this driver.
Definition win32_v.cpp:954
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:151
bool buffer_locked
Video buffer was locked by the main thread.
Definition win32_v.h:49
virtual void * GetVideoPointer()=0
Get a pointer to the video buffer.
void MakeDirty(int left, int top, int width, int height) override
Mark a particular area dirty.
Definition win32_v.cpp:961
bool PollEvent() override
Process a single system event.
Definition win32_v.cpp:998
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:1091
std::vector< int > GetListOfMonitorRefreshRates() override
Get a list of refresh rates of each available monitor.
Definition win32_v.cpp:1079
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:973
virtual uint8_t GetFullscreenBpp()
Colour depth to use for fullscreen display modes.
Definition win32_v.cpp:139
Dimension GetScreenSize() const override
Get the resolution of the main screen.
Definition win32_v.cpp:1086
virtual void ReleaseVideoPointer()
Hand video buffer back to the painting backend.
Definition win32_v.h:80
void UnlockVideoBuffer() override
Unlock a previously locked video buffer.
Definition win32_v.cpp:1102
void MainLoop() override
Perform the actual drawing.
Definition win32_v.cpp:1011
void ClaimMousePointer() override
Claim the exclusive rights for the mouse pointer.
Definition win32_v.cpp:63
Rect dirty_rect
Region of the screen that needs redrawing.
Definition win32_v.h:43
bool ToggleFullscreen(bool fullscreen) override
Change the full screen setting.
Definition win32_v.cpp:1047
bool ChangeResolution(int w, int h) override
Change the resolution of the window.
Definition win32_v.cpp:1037
std::string_view GetName() const override
Get the name of this driver.
Definition win32_v.h:102
void * buffer_bits
Internal rendering buffer.
Definition win32_v.h:107
HBITMAP dib_sect
System bitmap object referencing our rendering buffer.
Definition win32_v.h:105
void PaletteChanged(HWND hWnd) override
Palette of the window has changed.
Definition win32_v.cpp:1221
std::optional< std::string_view > Start(const StringList &param) override
Start this driver.
Definition win32_v.cpp:1117
void * GetVideoPointer() override
Get a pointer to the video buffer.
Definition win32_v.h:110
HPALETTE gdi_palette
Palette object for 8bpp blitter.
Definition win32_v.h:106
bool AllocateBackingStore(int w, int h, bool force=false) override
(Re-)create the backing store.
Definition win32_v.cpp:1142
void Paint() override
Paint the window.
Definition win32_v.cpp:1232
bool AfterBlitterChange() override
Callback invoked after the blitter was changed.
Definition win32_v.cpp:1180
void Stop() override
Stop this driver.
Definition win32_v.cpp:1134
virtual void PopulateSystemSprites()
Populate all sprites in cache.
virtual std::string_view GetInfoString() const
Get some information about the selected driver/backend to be shown to the user.
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 void ToggleVsync(bool vsync)
Change the vsync setting.
virtual bool AfterBlitterChange()
Callback invoked after the blitter was changed.
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.
#define Rect
Macro that prevents name conflicts between included headers.
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.
Base of all video drivers.