Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
VideoMode.h
Go to the documentation of this file.
1/* Copyright (C) 2026 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef INCLUDED_VIDEOMODE
19#define INCLUDED_VIDEOMODE
20
21#include "ps/CStrForward.h"
22#include "ps/Input.h"
24
25#include <memory>
26
27class CStrW;
28namespace Renderer::Backend { class IDevice; }
29namespace Renderer::Backend { class ISwapChain; }
30
31typedef struct SDL_Window SDL_Window;
32
34{
35public:
36 CVideoMode();
38
39 /**
40 * Initialise the video mode, for use in an SDL-using application.
41 */
42 bool InitSDL();
43
44 /**
45 * Initialise parts of the video mode, for use in Atlas (which uses
46 * wxWidgets instead of SDL for GL).
47 */
48 bool InitNonSDL();
49
50 /**
51 * Shut down after InitSDL/InitNonSDL, so that they can be used again.
52 */
53 void Shutdown();
54
55 /**
56 * Creates a backend device. Also we use wxWidgets in Atlas so we don't need
57 * to create one for that case.
58 */
59 bool CreateBackendDevice(const bool createSDLContext);
60
61 /**
62 * Resize the SDL window and associated graphics stuff to the new size.
63 */
64 bool OnceAFrameWork();
65
66 /**
67 * Set scale and tell dependent compoenent to recompute sizes.
68 */
69 void Rescale(float scale);
70
71 /**
72 * Switch to fullscreen or windowed mode.
73 */
74 bool SetFullscreen(bool fullscreen);
75
76 /**
77 * Returns true if window runs in fullscreen mode.
78 */
79 bool IsInFullscreen() const;
80
81 /**
82 * Switch between fullscreen and windowed mode.
83 */
84 bool ToggleFullscreen();
85
86 /**
87 * Update the graphics code to start drawing to the new size.
88 * This should be called after the GL context has been resized.
89 * This can also be used when the GL context is managed externally, not via SDL.
90 */
91 void UpdateRenderer(int windowWidth, int windowHeight);
92
93 int GetWindowWidth() const;
94 int GetWindowHeight() const;
95 int GetBPP() const;
96
97 bool IsVSyncEnabled() const;
98 bool IsInitialized() const;
99
100 int GetDesktopXRes() const;
101 int GetDesktopYRes() const;
102 int GetDesktopBPP() const;
103 int GetDesktopFreq() const;
104
105 float GetScale() const;
106
108
109 void SetWindowIcon();
110
111 void SetCursor(const CStrW& name);
112 void ResetCursor();
113
115
116 /**
117 * @return A swapchain (may be invalid) if available. It's not allowed to
118 * call the function during a frame rendering when an old swapchain is
119 * already in use.
120 */
122
123private:
124 void ReadConfig();
125 int GetBestBPP();
126 bool SetVideoMode(int w, int h, int bpp, bool fullscreen);
127
130
131 /**
132 * Immediately recreates a swapchain. It's a caller's responsibility to
133 * wait till the swapchain isn't in use anymore.
134 */
135 void RecreateSwapChain();
136
137 /**
138 * Remember whether Init has been called. (This isn't used for anything
139 * important, just for verifying that the callers call our methods in
140 * the right order.)
141 */
142 bool m_IsInitialised = false;
143
145public:
147
148private:
149 // Initial desktop settings.
150 // Frequency is in Hz, and BPP means bits per pixels (not bytes per pixels).
155
156 float m_Scale = 1.0f;
157
158 // Config file settings (0 if unspecified)
159 int m_ConfigW = 0;
160 int m_ConfigH = 0;
161 int m_ConfigBPP = 0;
164 bool m_ConfigVSync = false;
165
166 // (m_ConfigFullscreen defaults to false, so users don't get stuck if
167 // e.g. half the filesystem is missing and the config files aren't loaded).
168 bool m_ConfigFullscreen = false;
169
170 // If we're fullscreen, size/position of window when we were last windowed (or the default window
171 // size/position if we started fullscreen), to support switching back to the old window size/position
176
177 // The last mode selected
181
182 class CCursor;
183 std::unique_ptr<CCursor> m_Cursor;
184
186 std::unique_ptr<Renderer::Backend::IDevice> m_BackendDevice;
187 // SwapChain for the corresponding device.
188 std::unique_ptr<Renderer::Backend::ISwapChain> m_SwapChain;
189
190 // To avoid redundant and/or recursive resizing, we save the new size after VIDEORESIZE messages and
191 // only update the video mode once per frame.
192 // These values are the values of the latest resize message.
195
197 {
199 Input::Reaction operator()(const SDL_Event& ev);
200 };
202};
203
205
206#endif // INCLUDED_VIDEOMODE
struct SDL_Window SDL_Window
Definition: VideoMode.h:31
CVideoMode g_VideoMode
Definition: VideoMode.cpp:134
Definition: VideoMode.cpp:137
Definition: VideoMode.h:34
void DowngradeBackendSettingAfterCreationFailure()
Definition: VideoMode.cpp:720
CVideoMode()
Definition: VideoMode.cpp:313
int m_WindowedY
Definition: VideoMode.h:175
bool IsInFullscreen() const
Returns true if window runs in fullscreen mode.
Definition: VideoMode.cpp:866
bool m_ConfigEnableHiDPI
Definition: VideoMode.h:163
bool InitSDL()
Initialise the video mode, for use in an SDL-using application.
Definition: VideoMode.cpp:557
int GetDesktopFreq() const
Definition: VideoMode.cpp:959
bool OnceAFrameWork()
Resize the SDL window and associated graphics stuff to the new size.
Definition: VideoMode.cpp:767
void Rescale(float scale)
Set scale and tell dependent compoenent to recompute sizes.
Definition: VideoMode.cpp:788
std::unique_ptr< Renderer::Backend::IDevice > m_BackendDevice
Definition: VideoMode.h:186
int m_PreferredW
Definition: VideoMode.h:151
bool InitNonSDL()
Initialise parts of the video mode, for use in Atlas (which uses wxWidgets instead of SDL for GL).
Definition: VideoMode.cpp:634
void RecreateSwapChain()
Immediately recreates a swapchain.
Definition: VideoMode.cpp:728
int GetWindowWidth() const
Definition: VideoMode.cpp:917
SDL_Window * GetWindow()
Definition: VideoMode.cpp:965
void Shutdown()
Shut down after InitSDL/InitNonSDL, so that they can be used again.
Definition: VideoMode.cpp:645
int m_ConfigBPP
Definition: VideoMode.h:161
bool CreateBackendDevice(const bool createSDLContext)
Creates a backend device.
Definition: VideoMode.cpp:668
std::unique_ptr< Renderer::Backend::ISwapChain > m_SwapChain
Definition: VideoMode.h:188
void SetCursor(const CStrW &name)
Definition: VideoMode.cpp:1012
int GetDesktopBPP() const
Definition: VideoMode.cpp:953
int GetBestBPP()
Definition: VideoMode.cpp:908
int m_ResizedW
Definition: VideoMode.h:193
int m_ConfigDisplay
Definition: VideoMode.h:162
Renderer::Backend::Backend m_Backend
Definition: VideoMode.h:185
int m_ConfigH
Definition: VideoMode.h:160
bool SetFullscreen(bool fullscreen)
Switch to fullscreen or windowed mode.
Definition: VideoMode.cpp:800
int m_CurrentH
Definition: VideoMode.h:179
int m_WindowedH
Definition: VideoMode.h:173
int GetWindowHeight() const
Definition: VideoMode.cpp:923
int m_PreferredBPP
Definition: VideoMode.h:153
bool IsVSyncEnabled() const
Definition: VideoMode.cpp:935
int GetBPP() const
Definition: VideoMode.cpp:929
Input::Handler< InputHandler > m_InputHandler
Definition: VideoMode.h:201
Renderer::Backend::IDevice * GetBackendDevice()
Definition: VideoMode.h:114
int m_WindowedW
Definition: VideoMode.h:172
int m_ResizedH
Definition: VideoMode.h:194
int GetDesktopXRes() const
Definition: VideoMode.cpp:941
SDL_Window * m_Window
Definition: VideoMode.h:144
bool ToggleFullscreen()
Switch between fullscreen and windowed mode.
Definition: VideoMode.cpp:861
Input::Manager m_InputManager
Definition: VideoMode.h:146
Renderer::Backend::ISwapChain * GetOrCreateSwapChain()
Definition: VideoMode.cpp:739
bool IsInitialized() const
Definition: VideoMode.cpp:971
float GetScale() const
Definition: VideoMode.cpp:795
bool m_ConfigFullscreen
Definition: VideoMode.h:168
void ReadConfig()
Definition: VideoMode.cpp:320
int GetDesktopYRes() const
Definition: VideoMode.cpp:947
int m_WindowedX
Definition: VideoMode.h:174
int m_CurrentBPP
Definition: VideoMode.h:180
void SetWindowIcon()
Definition: VideoMode.cpp:976
float m_Scale
Definition: VideoMode.h:156
int m_PreferredFreq
Definition: VideoMode.h:154
void ResetCursor()
Definition: VideoMode.cpp:1018
void UpdateRenderer(int windowWidth, int windowHeight)
Update the graphics code to start drawing to the new size.
Definition: VideoMode.cpp:871
std::unique_ptr< CCursor > m_Cursor
Definition: VideoMode.h:183
int m_ConfigW
Definition: VideoMode.h:159
int m_CurrentW
Definition: VideoMode.h:178
bool TryCreateBackendDevice(SDL_Window *window)
Definition: VideoMode.cpp:682
bool m_IsInitialised
Remember whether Init has been called.
Definition: VideoMode.h:142
bool m_ConfigVSync
Definition: VideoMode.h:164
int m_PreferredH
Definition: VideoMode.h:152
bool SetVideoMode(int w, int h, int bpp, bool fullscreen)
Definition: VideoMode.cpp:347
Usable type to register a handler to the associated Manager.
Definition: Input.h:180
Holds a pointer to all registered HandlerBases.
Definition: Input.h:83
Definition: IDevice.h:64
Definition: ISwapChain.h:33
constexpr std::integral_constant< std::size_t, 1 > WINDOW
Definition: Input.h:52
Reaction
Definition: Input.h:39
Definition: Canvas2D.h:30
Backend
Definition: Backend.h:28
Definition: VideoMode.h:197
Input::Reaction operator()(const SDL_Event &ev)
Definition: VideoMode.cpp:283
CVideoMode & videoMode
Definition: VideoMode.h:198