OpenTTD Source 20261123-master-g1c55c72fd0
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 <windows.h>
15
18public:
26
27 void Stop() override;
28
29 void MakeDirty(int left, int top, int width, int height) override;
30
31 void MainLoop() override;
32
33 bool ChangeResolution(int w, int h) override;
34
35 bool ToggleFullscreen(bool fullscreen) override;
36
37 void ClaimMousePointer() override;
38
39 void EditBoxLostFocus() override;
40
41 std::vector<int> GetListOfMonitorRefreshRates() override;
42
43protected:
44 HWND main_wnd = nullptr;
45 bool fullscreen = false;
46 bool has_focus = false;
48 int width = 0;
49 int height = 0;
50 int width_org = 0;
51 int height_org = 0;
52
53 bool buffer_locked = false;
54 Palette local_palette;
55
56 Dimension GetScreenSize() const override;
57 void InputLoop() override;
58 bool LockVideoBuffer() override;
59 void UnlockVideoBuffer() override;
60 void CheckPaletteAnim() override;
61 bool PollEvent() override;
62
63 std::optional<std::string_view> Initialize();
64 bool MakeWindow(bool full_screen, bool resize = true);
65 void ClientSizeChanged(int w, int h, bool force = false);
66
67 virtual uint8_t GetFullscreenBpp();
68
76 virtual bool AllocateBackingStore(int w, int h, bool force = false) = 0;
77
82 virtual void *GetVideoPointer() = 0;
83
85 virtual void ReleaseVideoPointer() {}
86
91 virtual void PaletteChanged(HWND hWnd) = 0;
92
97 virtual bool MinimiseRestoreWithToggleFullscreen() const { return false; }
98
99private:
100 friend LRESULT CALLBACK WndProcGdi(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
101};
102
104public:
105 std::optional<std::string_view> Start(const StringList &param) override;
106
107 void Stop() override;
108
109 bool AfterBlitterChange() override;
110
111 std::string_view GetName() const override { return "win32"; }
112
113protected:
114 HBITMAP dib_sect = nullptr;
115 HPALETTE gdi_palette = nullptr;
116 void *buffer_bits = nullptr;
117
118 void Paint() override;
119 void *GetVideoPointer() override { return this->buffer_bits; }
120
121 bool AllocateBackingStore(int w, int h, bool force = false) override;
122 void PaletteChanged(HWND hWnd) override;
123 void MakePalette();
124 void UpdatePalette(HDC dc, uint start, uint count);
125
126#ifdef _DEBUG
127public:
128 static int RedrawScreenDebug();
129#endif
130};
131
133class FVideoDriver_Win32GDI : public DriverFactoryBase {
134public:
135 FVideoDriver_Win32GDI() : DriverFactoryBase(Driver::Type::Video, 8, "win32", "Win32 GDI Video Driver") {}
136 std::unique_ptr<Driver> CreateInstance() const override { return std::make_unique<VideoDriver_Win32GDI>(); }
137};
138
139#ifdef WITH_OPENGL
140
142class VideoDriver_Win32OpenGL : public VideoDriver_Win32Base {
143public:
144 VideoDriver_Win32OpenGL() : VideoDriver_Win32Base(true, true), driver_info(this->GetName()) {}
145
146 std::optional<std::string_view> Start(const StringList &param) override;
147
148 void Stop() override;
149
150 bool ToggleFullscreen(bool fullscreen) override;
151
152 bool AfterBlitterChange() override;
153
154 bool HasEfficient8Bpp() const override { return true; }
155
156 bool UseSystemCursor() override { return true; }
157
158 void PopulateSystemSprites() override;
159
160 void ClearSystemSprites() override;
161
162 void ToggleVsync(bool vsync) override;
163
164 std::string_view GetName() const override { return "win32-opengl"; }
165
166 std::string_view GetInfoString() const override { return this->driver_info; }
167
168protected:
169 HDC dc = nullptr;
170 HGLRC gl_rc = nullptr;
171 std::string driver_info;
172
173 uint8_t GetFullscreenBpp() override { return 32; } // OpenGL is always 32 bpp.
174
175 void Paint() override;
176
177 bool AllocateBackingStore(int w, int h, bool force = false) override;
178 void *GetVideoPointer() override;
179 void ReleaseVideoPointer() override;
180 void PaletteChanged(HWND) override {}
181
182 std::optional<std::string_view> AllocateContext();
183 void DestroyContext();
184};
185
187class FVideoDriver_Win32OpenGL : public DriverFactoryBase {
188public:
189 FVideoDriver_Win32OpenGL() : DriverFactoryBase(Driver::Type::Video, 9, "win32-opengl", "Win32 OpenGL Video Driver") {}
190 std::unique_ptr<Driver> CreateInstance() const override { return std::make_unique<VideoDriver_Win32OpenGL>(); }
191
192protected:
193 bool UsesHardwareAcceleration() const override { return true; }
194};
195
196#endif /* WITH_OPENGL */
197
198#endif /* VIDEO_WIN32_H */
Base for all driver factories.
Definition driver.h:55
virtual bool UsesHardwareAcceleration() const
Does the driver use hardware acceleration (video-drivers only).
Definition driver.h:116
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.
@ Video
A video driver.
Definition driver.h:43
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:136
Base class for Windows video drivers.
Definition win32_v.h:17
int height
Height in pixels of our display surface.
Definition win32_v.h:49
bool has_focus
Does our window have system focus?
Definition win32_v.h:46
void EditBoxLostFocus() override
An edit box lost the input focus.
Definition win32_v.cpp:1133
void CheckPaletteAnim() override
Process any pending palette animation.
Definition win32_v.cpp:1040
VideoDriver_Win32Base(bool uses_hardware_acceleration=false, bool supports_animation_buffer=false)
Create the video driver.
Definition win32_v.h:24
void Stop() override
Stop this driver.
Definition win32_v.cpp:1027
int height_org
Original monitor resolution height, before we changed it.
Definition win32_v.h:51
HWND main_wnd
Handle to system window.
Definition win32_v.h:44
bool fullscreen
Whether to use (true) fullscreen mode.
Definition win32_v.h:45
int width_org
Original monitor resolution width, before we changed it.
Definition win32_v.h:50
bool MakeWindow(bool full_screen, bool resize=true)
Instantiate a new window.
Definition win32_v.cpp:150
bool buffer_locked
Video buffer was locked by the main thread.
Definition win32_v.h:53
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:1034
bool PollEvent() override
Process a single system event.
Definition win32_v.cpp:1070
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:1169
std::vector< int > GetListOfMonitorRefreshRates() override
Get a list of refresh rates of each available monitor.
Definition win32_v.cpp:1157
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:48
void InputLoop() override
Handle input logic, is CTRL pressed, should we fast-forward, etc.
Definition win32_v.cpp:1046
virtual uint8_t GetFullscreenBpp()
Colour depth to use for fullscreen display modes.
Definition win32_v.cpp:138
Dimension GetScreenSize() const override
Get the resolution of the main screen.
Definition win32_v.cpp:1164
virtual void ReleaseVideoPointer()
Hand video buffer back to the painting backend.
Definition win32_v.h:85
Palette local_palette
Current palette to use for drawing.
Definition win32_v.h:54
void UnlockVideoBuffer() override
Unlock a previously locked video buffer.
Definition win32_v.cpp:1180
virtual bool MinimiseRestoreWithToggleFullscreen() const
Whether minimising and restoring fullscreen windows uses ToggleFullscreen.
Definition win32_v.h:97
void ClientSizeChanged(int w, int h, bool force=false)
Indicate to the driver the client-size might have changed.
Definition win32_v.cpp:1103
void MainLoop() override
Perform the actual drawing.
Definition win32_v.cpp:1083
void ClaimMousePointer() override
Claim the exclusive rights for the mouse pointer.
Definition win32_v.cpp:62
Rect dirty_rect
Region of the screen that needs redrawing.
Definition win32_v.h:47
bool ToggleFullscreen(bool fullscreen) override
Change the full screen setting.
Definition win32_v.cpp:1125
bool ChangeResolution(int w, int h) override
Change the resolution of the window.
Definition win32_v.cpp:1115
The GDI video driver for windows.
Definition win32_v.h:103
std::string_view GetName() const override
Get the name of this driver.
Definition win32_v.h:111
void * buffer_bits
Internal rendering buffer.
Definition win32_v.h:116
HBITMAP dib_sect
System bitmap object referencing our rendering buffer.
Definition win32_v.h:114
void PaletteChanged(HWND hWnd) override
Palette of the window has changed.
Definition win32_v.cpp:1300
std::optional< std::string_view > Start(const StringList &param) override
Start this driver.
Definition win32_v.cpp:1195
void * GetVideoPointer() override
Get a pointer to the video buffer.
Definition win32_v.h:119
HPALETTE gdi_palette
Palette object for 8bpp blitter.
Definition win32_v.h:115
bool AllocateBackingStore(int w, int h, bool force=false) override
(Re-)create the backing store.
Definition win32_v.cpp:1221
void Paint() override
Paint the window.
Definition win32_v.cpp:1311
bool AfterBlitterChange() override
Callback invoked after the blitter was changed.
Definition win32_v.cpp:1259
void Stop() override
Stop this driver.
Definition win32_v.cpp:1213
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.
const bool supports_animation_buffer
Whether this driver supports hardware accelerated animation.
virtual void ToggleVsync(bool vsync)
Change the vsync setting.
virtual bool AfterBlitterChange()
Callback invoked after the blitter was changed.
VideoDriver(bool uses_hardware_acceleration=false, bool supports_animation_buffer=false)
Create the video driver.
virtual bool HasEfficient8Bpp() const
Has this video driver an efficient code path for palette animated 8-bpp sprites?
virtual void Paint()
Paint the window.
const bool uses_hardware_acceleration
Whether hardware acceleration is used or not.
@ Driver
Driver message facility.
Definition debug_type.h:29
@ Video
Speed of painting drawn video buffer.
#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:61
Dimensions (a width and height) of a rectangle in 2D.
Base of all video drivers.