Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
VideoMode.h
Go to the documentation of this file.
1/* Copyright (C) 2023 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"
23
24#include <memory>
25
26typedef struct SDL_Window SDL_Window;
27
28namespace Renderer
29{
30namespace Backend
31{
32class IDevice;
33}
34}
35
37{
38public:
39 CVideoMode();
41
42 /**
43 * Initialise the video mode, for use in an SDL-using application.
44 */
45 bool InitSDL();
46
47 /**
48 * Initialise parts of the video mode, for use in Atlas (which uses
49 * wxWidgets instead of SDL for GL).
50 */
51 bool InitNonSDL();
52
53 /**
54 * Shut down after InitSDL/InitNonSDL, so that they can be used again.
55 */
56 void Shutdown();
57
58 /**
59 * Creates a backend device. Also we use wxWidgets in Atlas so we don't need
60 * to create one for that case.
61 */
62 bool CreateBackendDevice(const bool createSDLContext);
63
64 /**
65 * Resize the SDL window and associated graphics stuff to the new size.
66 */
67 bool ResizeWindow(int w, int h);
68
69 /**
70 * Set scale and tell dependent compoenent to recompute sizes.
71 */
72 void Rescale(float scale);
73
74 /**
75 * Switch to fullscreen or windowed mode.
76 */
77 bool SetFullscreen(bool fullscreen);
78
79 /**
80 * Returns true if window runs in fullscreen mode.
81 */
82 bool IsInFullscreen() const;
83
84 /**
85 * Switch between fullscreen and windowed mode.
86 */
87 bool ToggleFullscreen();
88
89 /**
90 * Update window position, to restore later if necessary (SDL2 only).
91 */
92 void UpdatePosition(int x, int y);
93
94 /**
95 * Update the graphics code to start drawing to the new size.
96 * This should be called after the GL context has been resized.
97 * This can also be used when the GL context is managed externally, not via SDL.
98 */
99 static void UpdateRenderer(int w, int h);
100
101 int GetXRes() const;
102 int GetYRes() const;
103 int GetBPP() const;
104
105 bool IsVSyncEnabled() const;
106
107 int GetDesktopXRes() const;
108 int GetDesktopYRes() const;
109 int GetDesktopBPP() const;
110 int GetDesktopFreq() const;
111
112 float GetScale() const;
113
115
116 void SetWindowIcon();
117
118 void SetCursor(const CStrW& name);
119 void ResetCursor();
120
122
123private:
124 void ReadConfig();
125 int GetBestBPP();
126 bool SetVideoMode(int w, int h, int bpp, bool fullscreen);
127
130
131 /**
132 * Remember whether Init has been called. (This isn't used for anything
133 * important, just for verifying that the callers call our methods in
134 * the right order.)
135 */
136 bool m_IsInitialised = false;
137
139
140 // Initial desktop settings.
141 // Frequency is in Hz, and BPP means bits per pixels (not bytes per pixels).
146
147 float m_Scale = 1.0f;
148
149 // Config file settings (0 if unspecified)
150 int m_ConfigW = 0;
151 int m_ConfigH = 0;
152 int m_ConfigBPP = 0;
155 bool m_ConfigVSync = false;
156
157 // (m_ConfigFullscreen defaults to false, so users don't get stuck if
158 // e.g. half the filesystem is missing and the config files aren't loaded).
159 bool m_ConfigFullscreen = false;
160
161 // If we're fullscreen, size/position of window when we were last windowed (or the default window
162 // size/position if we started fullscreen), to support switching back to the old window size/position
167
168 // Whether we're currently being displayed fullscreen
169 bool m_IsFullscreen = false;
170
171 // The last mode selected
175
176 class CCursor;
177 std::unique_ptr<CCursor> m_Cursor;
178
180 std::unique_ptr<Renderer::Backend::IDevice> m_BackendDevice;
181};
182
184
185#endif // INCLUDED_VIDEOMODE
struct SDL_Window SDL_Window
Definition: VideoMode.h:26
CVideoMode g_VideoMode
Definition: VideoMode.cpp:118
Definition: VideoMode.cpp:121
Definition: VideoMode.h:37
void DowngradeBackendSettingAfterCreationFailure()
Definition: VideoMode.cpp:688
CVideoMode()
Definition: VideoMode.cpp:269
int m_WindowedY
Definition: VideoMode.h:166
bool IsInFullscreen() const
Returns true if window runs in fullscreen mode.
Definition: VideoMode.cpp:799
bool m_ConfigEnableHiDPI
Definition: VideoMode.h:154
bool InitSDL()
Initialise the video mode, for use in an SDL-using application.
Definition: VideoMode.cpp:538
int GetDesktopFreq() const
Definition: VideoMode.cpp:890
bool m_IsFullscreen
Definition: VideoMode.h:169
void Rescale(float scale)
Set scale and tell dependent compoenent to recompute sizes.
Definition: VideoMode.cpp:721
std::unique_ptr< Renderer::Backend::IDevice > m_BackendDevice
Definition: VideoMode.h:180
int m_PreferredW
Definition: VideoMode.h:142
bool InitNonSDL()
Initialise parts of the video mode, for use in Atlas (which uses wxWidgets instead of SDL for GL).
Definition: VideoMode.cpp:614
SDL_Window * GetWindow()
Definition: VideoMode.cpp:896
static void UpdateRenderer(int w, int h)
Update the graphics code to start drawing to the new size.
Definition: VideoMode.cpp:813
void Shutdown()
Shut down after InitSDL/InitNonSDL, so that they can be used again.
Definition: VideoMode.cpp:625
int m_ConfigBPP
Definition: VideoMode.h:152
bool CreateBackendDevice(const bool createSDLContext)
Creates a backend device.
Definition: VideoMode.cpp:641
void SetCursor(const CStrW &name)
Definition: VideoMode.cpp:938
int GetDesktopBPP() const
Definition: VideoMode.cpp:884
int GetBestBPP()
Definition: VideoMode.cpp:839
int m_ConfigDisplay
Definition: VideoMode.h:153
Renderer::Backend::Backend m_Backend
Definition: VideoMode.h:179
int m_ConfigH
Definition: VideoMode.h:151
bool SetFullscreen(bool fullscreen)
Switch to fullscreen or windowed mode.
Definition: VideoMode.cpp:733
void UpdatePosition(int x, int y)
Update window position, to restore later if necessary (SDL2 only).
Definition: VideoMode.cpp:804
int GetXRes() const
Definition: VideoMode.cpp:848
int m_CurrentH
Definition: VideoMode.h:173
int m_WindowedH
Definition: VideoMode.h:164
int m_PreferredBPP
Definition: VideoMode.h:144
bool IsVSyncEnabled() const
Definition: VideoMode.cpp:866
int GetBPP() const
Definition: VideoMode.cpp:860
Renderer::Backend::IDevice * GetBackendDevice()
Definition: VideoMode.h:121
int GetYRes() const
Definition: VideoMode.cpp:854
int m_WindowedW
Definition: VideoMode.h:163
int GetDesktopXRes() const
Definition: VideoMode.cpp:872
SDL_Window * m_Window
Definition: VideoMode.h:138
bool ToggleFullscreen()
Switch between fullscreen and windowed mode.
Definition: VideoMode.cpp:794
float GetScale() const
Definition: VideoMode.cpp:728
bool m_ConfigFullscreen
Definition: VideoMode.h:159
void ReadConfig()
Definition: VideoMode.cpp:276
int GetDesktopYRes() const
Definition: VideoMode.cpp:878
int m_WindowedX
Definition: VideoMode.h:165
int m_CurrentBPP
Definition: VideoMode.h:174
void SetWindowIcon()
Definition: VideoMode.cpp:902
float m_Scale
Definition: VideoMode.h:147
int m_PreferredFreq
Definition: VideoMode.h:145
void ResetCursor()
Definition: VideoMode.cpp:944
std::unique_ptr< CCursor > m_Cursor
Definition: VideoMode.h:177
int m_ConfigW
Definition: VideoMode.h:150
int m_CurrentW
Definition: VideoMode.h:172
bool TryCreateBackendDevice(SDL_Window *window)
Definition: VideoMode.cpp:655
bool m_IsInitialised
Remember whether Init has been called.
Definition: VideoMode.h:136
bool m_ConfigVSync
Definition: VideoMode.h:155
bool ResizeWindow(int w, int h)
Resize the SDL window and associated graphics stuff to the new size.
Definition: VideoMode.cpp:696
int m_PreferredH
Definition: VideoMode.h:143
bool SetVideoMode(int w, int h, int bpp, bool fullscreen)
Definition: VideoMode.cpp:308
Definition: IDevice.h:48
Backend
Definition: Backend.h:28
Definition: VideoMode.h:29