LCOV - code coverage report
Current view: top level - source/ps - VideoMode.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 3 3 100.0 %
Date: 2022-06-14 00:41:00 Functions: 1 2 50.0 %

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

Generated by: LCOV version 1.13