Line data Source code
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" 23 : #include "renderer/backend/Backend.h" 24 : 25 : #include <memory> 26 : 27 : class CStrW; 28 : namespace Renderer::Backend { class IDevice; } 29 : namespace Renderer::Backend { class ISwapChain; } 30 : 31 : typedef struct SDL_Window SDL_Window; 32 : 33 : class CVideoMode 34 : { 35 : public: 36 : CVideoMode(); 37 : ~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 : 107 : SDL_Window* GetWindow(); 108 : 109 : void SetWindowIcon(); 110 : 111 : void SetCursor(const CStrW& name); 112 : void ResetCursor(); 113 : 114 10 : Renderer::Backend::IDevice* GetBackendDevice() { return m_BackendDevice.get(); } 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 : */ 121 : Renderer::Backend::ISwapChain* GetOrCreateSwapChain(); 122 : 123 : private: 124 : void ReadConfig(); 125 : int GetBestBPP(); 126 : bool SetVideoMode(int w, int h, int bpp, bool fullscreen); 127 : 128 : bool TryCreateBackendDevice(SDL_Window* window); 129 : void DowngradeBackendSettingAfterCreationFailure(); 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 : 144 : SDL_Window* m_Window = nullptr; 145 : public: 146 : Input::Manager m_InputManager; 147 : 148 : private: 149 : // Initial desktop settings. 150 : // Frequency is in Hz, and BPP means bits per pixels (not bytes per pixels). 151 : int m_PreferredW = 0; 152 : int m_PreferredH = 0; 153 : int m_PreferredBPP = 0; 154 : int m_PreferredFreq = 0; 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; 162 : int m_ConfigDisplay = 0; 163 : bool m_ConfigEnableHiDPI = false; 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 172 : int m_WindowedW; 173 : int m_WindowedH; 174 : int m_WindowedX; 175 : int m_WindowedY; 176 : 177 : // The last video mode selected. This is the size the renderer is currently 178 : // configured for, not a query of the live window. On Wayland size changes 179 : // are asynchronous, so it can lag the actual window size until the pending 180 : // resize event is applied by OnceAFrameWork. 181 : int m_CurrentW; 182 : int m_CurrentH; 183 : int m_CurrentBPP; 184 : 185 : class CCursor; 186 : std::unique_ptr<CCursor> m_Cursor; 187 : 188 : Renderer::Backend::Backend m_Backend = Renderer::Backend::Backend::GL; 189 : std::unique_ptr<Renderer::Backend::IDevice> m_BackendDevice; 190 : // SwapChain for the corresponding device. 191 : std::unique_ptr<Renderer::Backend::ISwapChain> m_SwapChain; 192 : 193 : // To avoid redundant and/or recursive resizing, we save the new size after VIDEORESIZE messages and 194 : // only update the video mode once per frame. 195 : // These values are the values of the latest resize message. 196 : int m_ResizedW{0}; 197 : int m_ResizedH{0}; 198 : 199 : struct InputHandler 200 : { 201 : CVideoMode& videoMode; 202 : Input::Reaction operator()(const SDL_Event& ev); 203 : }; 204 : Input::Handler<InputHandler> m_InputHandler{m_InputManager, Input::Slot::WINDOW, {*this}}; 205 : }; 206 : 207 : extern CVideoMode g_VideoMode; 208 : 209 : #endif // INCLUDED_VIDEOMODE