OpenTTD Source 20260208-master-g43af8e94d0
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
63 virtual uint8_t GetFullscreenBpp();
65 virtual bool AllocateBackingStore(int w, int h, bool force = false) = 0;
67 virtual void *GetVideoPointer() = 0;
69 virtual void ReleaseVideoPointer() {}
71 virtual void PaletteChanged(HWND hWnd) = 0;
72
73private:
74 friend LRESULT CALLBACK WndProcGdi(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
75};
76
77class VideoDriver_Win32GDI : public VideoDriver_Win32Base {
78public:
79 VideoDriver_Win32GDI() : dib_sect(nullptr), gdi_palette(nullptr), buffer_bits(nullptr) {}
80
81 std::optional<std::string_view> Start(const StringList &param) override;
82
83 void Stop() override;
84
85 bool AfterBlitterChange() override;
86
87 std::string_view GetName() const override { return "win32"; }
88
89protected:
90 HBITMAP dib_sect;
91 HPALETTE gdi_palette;
93
94 void Paint() override;
95 void *GetVideoPointer() override { return this->buffer_bits; }
96
97 bool AllocateBackingStore(int w, int h, bool force = false) override;
98 void PaletteChanged(HWND hWnd) override;
99 void MakePalette();
100 void UpdatePalette(HDC dc, uint start, uint count);
101
102#ifdef _DEBUG
103public:
104 static int RedrawScreenDebug();
105#endif
106};
107
109class FVideoDriver_Win32GDI : public DriverFactoryBase {
110public:
111 FVideoDriver_Win32GDI() : DriverFactoryBase(Driver::DT_VIDEO, 9, "win32", "Win32 GDI Video Driver") {}
112 std::unique_ptr<Driver> CreateInstance() const override { return std::make_unique<VideoDriver_Win32GDI>(); }
113};
114
115#ifdef WITH_OPENGL
116
118class VideoDriver_Win32OpenGL : public VideoDriver_Win32Base {
119public:
120 VideoDriver_Win32OpenGL() : VideoDriver_Win32Base(true), dc(nullptr), gl_rc(nullptr), anim_buffer(nullptr), driver_info(this->GetName()) {}
121
122 std::optional<std::string_view> Start(const StringList &param) override;
123
124 void Stop() override;
125
126 bool ToggleFullscreen(bool fullscreen) override;
127
128 bool AfterBlitterChange() override;
129
130 bool HasEfficient8Bpp() const override { return true; }
131
132 bool UseSystemCursor() override { return true; }
133
134 void PopulateSystemSprites() override;
135
136 void ClearSystemSprites() override;
137
138 bool HasAnimBuffer() override { return true; }
139 uint8_t *GetAnimBuffer() override { return this->anim_buffer; }
140
141 void ToggleVsync(bool vsync) override;
142
143 std::string_view GetName() const override { return "win32-opengl"; }
144
145 std::string_view GetInfoString() const override { return this->driver_info; }
146
147protected:
148 HDC dc;
149 HGLRC gl_rc;
150 uint8_t *anim_buffer;
151 std::string driver_info;
152
153 uint8_t GetFullscreenBpp() override { return 32; } // OpenGL is always 32 bpp.
154
155 void Paint() override;
156
157 bool AllocateBackingStore(int w, int h, bool force = false) override;
158 void *GetVideoPointer() override;
159 void ReleaseVideoPointer() override;
160 void PaletteChanged(HWND) override {}
161
162 std::optional<std::string_view> AllocateContext();
163 void DestroyContext();
164};
165
167class FVideoDriver_Win32OpenGL : public DriverFactoryBase {
168public:
169 FVideoDriver_Win32OpenGL() : DriverFactoryBase(Driver::DT_VIDEO, 10, "win32-opengl", "Win32 OpenGL Video Driver") {}
170 std::unique_ptr<Driver> CreateInstance() const override { return std::make_unique<VideoDriver_Win32OpenGL>(); }
171
172protected:
173 bool UsesHardwareAcceleration() const override { return true; }
174};
175
176#endif /* WITH_OPENGL */
177
178#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:112
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:1052
void CheckPaletteAnim() override
Process any pending palette animation.
Definition win32_v.cpp:964
void Stop() override
Stop this driver.
Definition win32_v.cpp:951
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:148
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:958
bool PollEvent() override
Process a single system event.
Definition win32_v.cpp:995
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:1088
std::vector< int > GetListOfMonitorRefreshRates() override
Get a list of refresh rates of each available monitor.
Definition win32_v.cpp:1076
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:970
virtual uint8_t GetFullscreenBpp()
Get screen depth to use for fullscreen mode.
Definition win32_v.cpp:136
Dimension GetScreenSize() const override
Get the resolution of the main screen.
Definition win32_v.cpp:1083
virtual void ReleaseVideoPointer()
Hand video buffer back to the painting backend.
Definition win32_v.h:69
void UnlockVideoBuffer() override
Unlock a previously locked video buffer.
Definition win32_v.cpp:1099
void MainLoop() override
Perform the actual drawing.
Definition win32_v.cpp:1008
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:1044
bool ChangeResolution(int w, int h) override
Change the resolution of the window.
Definition win32_v.cpp:1034
std::string_view GetName() const override
Get the name of this driver.
Definition win32_v.h:87
void * buffer_bits
Internal rendering buffer.
Definition win32_v.h:92
HBITMAP dib_sect
System bitmap object referencing our rendering buffer.
Definition win32_v.h:90
void PaletteChanged(HWND hWnd) override
Palette of the window has changed.
Definition win32_v.cpp:1218
std::optional< std::string_view > Start(const StringList &param) override
Start this driver.
Definition win32_v.cpp:1114
void * GetVideoPointer() override
Get a pointer to the video buffer.
Definition win32_v.h:95
HPALETTE gdi_palette
Palette object for 8bpp blitter.
Definition win32_v.h:91
bool AllocateBackingStore(int w, int h, bool force=false) override
(Re-)create the backing store.
Definition win32_v.cpp:1139
void Paint() override
Paint the window.
Definition win32_v.cpp:1229
bool AfterBlitterChange() override
Callback invoked after the blitter was changed.
Definition win32_v.cpp:1177
void Stop() override
Stop this driver.
Definition win32_v.cpp:1131
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.