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/>.
8 /** @file win32_v.h Base of the Windows video driver. */
13 #include "video_driver.hpp"
15 #include <condition_variable>
18 /** Base class for Windows video drivers. */
19 class VideoDriver_Win32Base
: public VideoDriver
{
21 VideoDriver_Win32Base(bool uses_hardware_acceleration
= false) : VideoDriver(uses_hardware_acceleration
), main_wnd(nullptr), fullscreen(false), buffer_locked(false) {}
25 void MakeDirty(int left
, int top
, int width
, int height
) override
;
27 void MainLoop() override
;
29 bool ChangeResolution(int w
, int h
) override
;
31 bool ToggleFullscreen(bool fullscreen
) override
;
33 bool ClaimMousePointer() override
;
35 void EditBoxLostFocus() override
;
37 std::vector
<int> GetListOfMonitorRefreshRates() override
;
40 HWND main_wnd
; ///< Handle to system window.
41 bool fullscreen
; ///< Whether to use (true) fullscreen mode.
42 bool has_focus
= false; ///< Does our window have system focus?
43 Rect dirty_rect
; ///< Region of the screen that needs redrawing.
44 int width
= 0; ///< Width in pixels of our display surface.
45 int height
= 0; ///< Height in pixels of our display surface.
46 int width_org
= 0; ///< Original monitor resolution width, before we changed it.
47 int height_org
= 0; ///< Original monitor resolution height, before we changed it.
49 bool buffer_locked
; ///< Video buffer was locked by the main thread.
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
;
60 bool MakeWindow(bool full_screen
, bool resize
= true);
61 void ClientSizeChanged(int w
, int h
, bool force
= false);
63 /** Get screen depth to use for fullscreen mode. */
64 virtual uint8_t GetFullscreenBpp();
65 /** (Re-)create the backing store. */
66 virtual bool AllocateBackingStore(int w
, int h
, bool force
= false) = 0;
67 /** Get a pointer to the video buffer. */
68 virtual void *GetVideoPointer() = 0;
69 /** Hand video buffer back to the painting backend. */
70 virtual void ReleaseVideoPointer() {}
71 /** Palette of the window has changed. */
72 virtual void PaletteChanged(HWND hWnd
) = 0;
75 friend LRESULT CALLBACK
WndProcGdi(HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
);
77 /** The GDI video driver for windows. */
78 class VideoDriver_Win32GDI
: public VideoDriver_Win32Base
{
80 VideoDriver_Win32GDI() : dib_sect(nullptr), gdi_palette(nullptr), buffer_bits(nullptr) {}
82 std::optional
<std::string_view
> Start(const StringList
¶m
) override
;
86 bool AfterBlitterChange() override
;
88 std::string_view
GetName() const override
{ return "win32"; }
91 HBITMAP dib_sect
; ///< System bitmap object referencing our rendering buffer.
92 HPALETTE gdi_palette
; ///< Palette object for 8bpp blitter.
93 void *buffer_bits
; ///< Internal rendering buffer.
95 void Paint() override
;
96 void *GetVideoPointer() override
{ return this->buffer_bits
; }
98 bool AllocateBackingStore(int w
, int h
, bool force
= false) override
;
99 void PaletteChanged(HWND hWnd
) override
;
101 void UpdatePalette(HDC dc
, uint start
, uint count
);
105 static int RedrawScreenDebug();
109 /** The factory for Windows' video driver. */
110 class FVideoDriver_Win32GDI
: public DriverFactoryBase
{
112 FVideoDriver_Win32GDI() : DriverFactoryBase(Driver::DT_VIDEO
, 9, "win32", "Win32 GDI Video Driver") {}
113 Driver
*CreateInstance() const override
{ return new VideoDriver_Win32GDI(); }
118 /** The OpenGL video driver for windows. */
119 class VideoDriver_Win32OpenGL
: public VideoDriver_Win32Base
{
121 VideoDriver_Win32OpenGL() : VideoDriver_Win32Base(true), dc(nullptr), gl_rc(nullptr), anim_buffer(nullptr), driver_info(this->GetName()) {}
123 std::optional
<std::string_view
> Start(const StringList
¶m
) override
;
125 void Stop() override
;
127 bool ToggleFullscreen(bool fullscreen
) override
;
129 bool AfterBlitterChange() override
;
131 bool HasEfficient8Bpp() const override
{ return true; }
133 bool UseSystemCursor() override
{ return true; }
135 void PopulateSystemSprites() override
;
137 void ClearSystemSprites() override
;
139 bool HasAnimBuffer() override
{ return true; }
140 uint8_t *GetAnimBuffer() override
{ return this->anim_buffer
; }
142 void ToggleVsync(bool vsync
) override
;
144 std::string_view
GetName() const override
{ return "win32-opengl"; }
146 std::string_view
GetInfoString() const override
{ return this->driver_info
; }
149 HDC dc
; ///< Window device context.
150 HGLRC gl_rc
; ///< OpenGL context.
151 uint8_t *anim_buffer
; ///< Animation buffer from OpenGL back-end.
152 std::string driver_info
; ///< Information string about selected driver.
154 uint8_t GetFullscreenBpp() override
{ return 32; } // OpenGL is always 32 bpp.
156 void Paint() override
;
158 bool AllocateBackingStore(int w
, int h
, bool force
= false) override
;
159 void *GetVideoPointer() override
;
160 void ReleaseVideoPointer() override
;
161 void PaletteChanged(HWND
) override
{}
163 std::optional
<std::string_view
> AllocateContext();
164 void DestroyContext();
167 /** The factory for Windows' OpenGL video driver. */
168 class FVideoDriver_Win32OpenGL
: public DriverFactoryBase
{
170 FVideoDriver_Win32OpenGL() : DriverFactoryBase(Driver::DT_VIDEO
, 10, "win32-opengl", "Win32 OpenGL Video Driver") {}
171 /* virtual */ Driver
*CreateInstance() const override
{ return new VideoDriver_Win32OpenGL(); }
174 bool UsesHardwareAcceleration() const override
{ return true; }
177 #endif /* WITH_OPENGL */
179 #endif /* VIDEO_WIN32_H */