OpenTTD Source  12.0-beta2
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 
19 public:
20  VideoDriver_Win32Base() : main_wnd(nullptr), fullscreen(false), buffer_locked(false) {}
21 
22  void Stop() override;
23 
24  void MakeDirty(int left, int top, int width, int height) override;
25 
26  void MainLoop() override;
27 
28  bool ChangeResolution(int w, int h) override;
29 
30  bool ToggleFullscreen(bool fullscreen) override;
31 
32  bool ClaimMousePointer() override;
33 
34  void EditBoxLostFocus() override;
35 
36  std::vector<int> GetListOfMonitorRefreshRates() override;
37 
38 protected:
39  HWND main_wnd;
40  bool fullscreen;
41  bool has_focus = false;
43  int width = 0;
44  int height = 0;
45  int width_org = 0;
46  int height_org = 0;
47 
49 
50  Dimension GetScreenSize() const override;
51  float GetDPIScale() 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 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 
73 private:
74  friend LRESULT CALLBACK WndProcGdi(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
75 };
78 public:
79  VideoDriver_Win32GDI() : dib_sect(nullptr), gdi_palette(nullptr), buffer_bits(nullptr) {}
80 
81  const char *Start(const StringList &param) override;
82 
83  void Stop() override;
84 
85  bool AfterBlitterChange() override;
86 
87  const char *GetName() const override { return "win32"; }
88 
89 protected:
90  HBITMAP dib_sect;
91  HPALETTE gdi_palette;
92  void *buffer_bits;
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
103 public:
104  static int RedrawScreenDebug();
105 #endif
106 };
107 
110 public:
111  FVideoDriver_Win32GDI() : DriverFactoryBase(Driver::DT_VIDEO, 9, "win32", "Win32 GDI Video Driver") {}
112  Driver *CreateInstance() const override { return new VideoDriver_Win32GDI(); }
113 };
114 
115 #ifdef WITH_OPENGL
116 
118 class VideoDriver_Win32OpenGL : public VideoDriver_Win32Base {
119 public:
120  VideoDriver_Win32OpenGL() : dc(nullptr), gl_rc(nullptr), anim_buffer(nullptr) {}
121 
122  const char *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 *GetAnimBuffer() override { return this->anim_buffer; }
140 
141  void ToggleVsync(bool vsync) override;
142 
143  const char *GetName() const override { return "win32-opengl"; }
144 
145 protected:
146  HDC dc;
147  HGLRC gl_rc;
148  uint8 *anim_buffer;
149 
150  uint8 GetFullscreenBpp() override { return 32; } // OpenGL is always 32 bpp.
151 
152  void Paint() override;
153 
154  bool AllocateBackingStore(int w, int h, bool force = false) override;
155  void *GetVideoPointer() override;
156  void ReleaseVideoPointer() override;
157  void PaletteChanged(HWND hWnd) override {}
158 
159  const char *AllocateContext();
160  void DestroyContext();
161 };
162 
164 class FVideoDriver_Win32OpenGL : public DriverFactoryBase {
165 public:
166  FVideoDriver_Win32OpenGL() : DriverFactoryBase(Driver::DT_VIDEO, 10, "win32-opengl", "Win32 OpenGL Video Driver") {}
167  /* virtual */ Driver *CreateInstance() const override { return new VideoDriver_Win32OpenGL(); }
168 
169 protected:
170  bool UsesHardwareAcceleration() const override { return true; }
171 };
172 
173 #endif /* WITH_OPENGL */
174 
175 #endif /* VIDEO_WIN32_H */
VideoDriver_Win32Base::AllocateBackingStore
virtual bool AllocateBackingStore(int w, int h, bool force=false)=0
(Re-)create the backing store.
VideoDriver_Win32Base::MainLoop
void MainLoop() override
Perform the actual drawing.
Definition: win32_v.cpp:860
VideoDriver::HasAnimBuffer
virtual bool HasAnimBuffer()
Does this video driver support a separate animation buffer in addition to the colour buffer?
Definition: video_driver.hpp:136
VideoDriver_Win32Base
Base class for Windows video drivers.
Definition: win32_v.h:18
Driver::GetName
virtual const char * GetName() const =0
Get the name of this driver.
VideoDriver
The base of all video drivers.
Definition: video_driver.hpp:35
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition: geometry_type.hpp:27
VideoDriver_Win32GDI::Paint
void Paint() override
Paint the window.
Definition: win32_v.cpp:1118
VideoDriver_Win32Base::EditBoxLostFocus
void EditBoxLostFocus() override
An edit box lost the input focus.
Definition: win32_v.cpp:904
VideoDriver_Win32Base::width
int width
Width in pixels of our display surface.
Definition: win32_v.h:43
VideoDriver_Win32Base::ReleaseVideoPointer
virtual void ReleaseVideoPointer()
Hand video buffer back to the painting backend.
Definition: win32_v.h:69
DriverFactoryBase::DriverFactoryBase
DriverFactoryBase(Driver::Type type, int priority, const char *name, const char *description)
Construct a new DriverFactory.
Definition: driver.cpp:216
VideoDriver_Win32Base::PollEvent
bool PollEvent() override
Process a single system event.
Definition: win32_v.cpp:847
DriverFactoryBase::CreateInstance
virtual Driver * CreateInstance() const =0
Create an instance of this driver-class.
VideoDriver_Win32Base::main_wnd
HWND main_wnd
Handle to system window.
Definition: win32_v.h:39
VideoDriver_Win32Base::buffer_locked
bool buffer_locked
Video buffer was locked by the main thread.
Definition: win32_v.h:48
VideoDriver_Win32GDI::AllocateBackingStore
bool AllocateBackingStore(int w, int h, bool force=false) override
(Re-)create the backing store.
Definition: win32_v.cpp:1032
VideoDriver_Win32Base::GetScreenSize
Dimension GetScreenSize() const override
Get the resolution of the main screen.
Definition: win32_v.cpp:935
VideoDriver_Win32Base::height_org
int height_org
Original monitor resolution height, before we changed it.
Definition: win32_v.h:46
VideoDriver_Win32Base::height
int height
Height in pixels of our display surface.
Definition: win32_v.h:44
VideoDriver::ClearSystemSprites
virtual void ClearSystemSprites()
Clear all cached sprites.
Definition: video_driver.hpp:108
VideoDriver_Win32GDI::buffer_bits
void * buffer_bits
Internal rendering buffer.
Definition: win32_v.h:92
VideoDriver_Win32GDI::dib_sect
HBITMAP dib_sect
System bitmap object referencing our rendering buffer.
Definition: win32_v.h:90
VideoDriver_Win32Base::Stop
void Stop() override
Stop this driver.
Definition: win32_v.cpp:799
VideoDriver_Win32Base::PaletteChanged
virtual void PaletteChanged(HWND hWnd)=0
Palette of the window has changed.
VideoDriver_Win32Base::GetListOfMonitorRefreshRates
std::vector< int > GetListOfMonitorRefreshRates() override
Get a list of refresh rates of each available monitor.
Definition: win32_v.cpp:928
VideoDriver_Win32GDI::PaletteChanged
void PaletteChanged(HWND hWnd) override
Palette of the window has changed.
Definition: win32_v.cpp:1107
VideoDriver::Paint
virtual void Paint()
Paint the window.
Definition: video_driver.hpp:272
VideoDriver_Win32GDI::GetName
const char * GetName() const override
Get the name of this driver.
Definition: win32_v.h:87
VideoDriver::ToggleVsync
virtual void ToggleVsync(bool vsync)
Change the vsync setting.
Definition: video_driver.hpp:75
Driver::Start
virtual const char * Start(const StringList &parm)=0
Start this driver.
VideoDriver_Win32Base::CheckPaletteAnim
void CheckPaletteAnim() override
Process any pending palette animation.
Definition: win32_v.cpp:812
FVideoDriver_Win32GDI
The factory for Windows' video driver.
Definition: win32_v.h:109
VideoDriver_Win32Base::has_focus
bool has_focus
Does our window have system focus?
Definition: win32_v.h:41
StringList
std::vector< std::string > StringList
Type for a list of strings.
Definition: string_type.h:58
VideoDriver_Win32Base::MakeWindow
bool MakeWindow(bool full_screen, bool resize=true)
Instantiate a new window.
Definition: win32_v.cpp:134
VideoDriver_Win32Base::GetFullscreenBpp
virtual uint8 GetFullscreenBpp()
Get screen depth to use for fullscreen mode.
Definition: win32_v.cpp:122
VideoDriver_Win32GDI::Stop
void Stop() override
Stop this driver.
Definition: win32_v.cpp:1024
VideoDriver_Win32Base::ToggleFullscreen
bool ToggleFullscreen(bool fullscreen) override
Change the full screen setting.
Definition: win32_v.cpp:896
VideoDriver_Win32Base::UnlockVideoBuffer
void UnlockVideoBuffer() override
Unlock a previously locked video buffer.
Definition: win32_v.cpp:992
VideoDriver::GetAnimBuffer
virtual uint8 * GetAnimBuffer()
Get a pointer to the animation buffer of the video back-end.
Definition: video_driver.hpp:145
VideoDriver::UseSystemCursor
virtual bool UseSystemCursor()
Get whether the mouse cursor is drawn by the video driver.
Definition: video_driver.hpp:95
VideoDriver_Win32Base::width_org
int width_org
Original monitor resolution width, before we changed it.
Definition: win32_v.h:45
VideoDriver::PopulateSystemSprites
virtual void PopulateSystemSprites()
Populate all sprites in cache.
Definition: video_driver.hpp:103
VideoDriver_Win32GDI::Start
const char * Start(const StringList &param) override
Start this driver.
Definition: win32_v.cpp:1007
VideoDriver_Win32Base::fullscreen
bool fullscreen
Whether to use (true) fullscreen mode.
Definition: win32_v.h:40
VideoDriver_Win32Base::GetVideoPointer
virtual void * GetVideoPointer()=0
Get a pointer to the video buffer.
video_driver.hpp
Driver::DT_VIDEO
@ DT_VIDEO
A video driver.
Definition: driver.h:44
VideoDriver_Win32Base::MakeDirty
void MakeDirty(int left, int top, int width, int height) override
Mark a particular area dirty.
Definition: win32_v.cpp:806
FVideoDriver_Win32GDI::CreateInstance
Driver * CreateInstance() const override
Create an instance of this driver-class.
Definition: win32_v.h:112
DriverFactoryBase::UsesHardwareAcceleration
virtual bool UsesHardwareAcceleration() const
Does the driver use hardware acceleration (video-drivers only).
Definition: driver.h:114
VideoDriver_Win32Base::LockVideoBuffer
bool LockVideoBuffer() override
Make sure the video buffer is ready for drawing.
Definition: win32_v.cpp:981
VideoDriver_Win32GDI
The GDI video driver for windows.
Definition: win32_v.h:77
VideoDriver_Win32Base::dirty_rect
Rect dirty_rect
Region of the screen that needs redrawing.
Definition: win32_v.h:42
VideoDriver_Win32Base::ChangeResolution
bool ChangeResolution(int w, int h) override
Change the resolution of the window.
Definition: win32_v.cpp:886
VideoDriver_Win32GDI::AfterBlitterChange
bool AfterBlitterChange() override
Callback invoked after the blitter was changed.
Definition: win32_v.cpp:1067
VideoDriver::HasEfficient8Bpp
virtual bool HasEfficient8Bpp() const
Has this video driver an efficient code path for palette animated 8-bpp sprites?
Definition: video_driver.hpp:127
Driver
A driver for communicating with the user.
Definition: driver.h:23
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition: geometry_type.hpp:47
VideoDriver_Win32Base::GetDPIScale
float GetDPIScale() override
Get DPI scaling factor of the screen OTTD is displayed on.
Definition: win32_v.cpp:940
VideoDriver_Win32GDI::GetVideoPointer
void * GetVideoPointer() override
Get a pointer to the video buffer.
Definition: win32_v.h:95
VideoDriver_Win32Base::InputLoop
void InputLoop() override
Handle input logic, is CTRL pressed, should we fast-forward, etc.
Definition: win32_v.cpp:818
DriverFactoryBase
Base for all driver factories.
Definition: driver.h:59
VideoDriver::AfterBlitterChange
virtual bool AfterBlitterChange()
Callback invoked after the blitter was changed.
Definition: video_driver.hpp:81
VideoDriver_Win32GDI::gdi_palette
HPALETTE gdi_palette
Palette object for 8bpp blitter.
Definition: win32_v.h:91