Cytopia  0.3
A city building simulation game
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
WindowManager Class Reference

#include <WindowManager.hxx>

+ Inheritance diagram for WindowManager:
+ Collaboration diagram for WindowManager:

Public Member Functions

 WindowManager (WindowManager const &)=delete
 
WindowManageroperator= (WindowManager const &)=delete
 
void toggleFullScreen () const
 
void setWindowTitle (const std::string &title)
 sets title of the window More...
 
SDL_Renderer * getRenderer () const
 get SDL Renderer More...
 
SDL_Window * getWindow () const
 get SDL Window More...
 
const std::vector< std::unique_ptr< SDL_DisplayMode > > & getSupportedScreenResolutions () const
 
void setScreenResolution (int mode)
 
void setFullScreenMode (FULLSCREEN_MODE mode) const
 sets the game's window mode More...
 
void newImGuiFrame ()
 creates and renders a new ImGui frame More...
 
void renderScreen ()
 

Public Attributes

friend Singleton< WindowManager >
 

Private Member Functions

 WindowManager ()
 Construct a new WindowManager object. More...
 
 ~WindowManager ()
 Destroy the WindowManager object. More...
 
void initializeScreenResolutions ()
 
void initializeImguiRenderer ()
 Sets up platform/renderer backends and theme. More...
 
void destroyImGuiRenderer ()
 shuts down the renderer for ImGui and the ImGui context. More...
 

Private Attributes

std::string m_title = "Cytopia"
 title of the window More...
 
const std::string m_windowIcon = "resources/images/app_icons/cytopia_icon.png"
 the window's icon More...
 
SDL_Window * m_window = nullptr
 pointer to the SDL_Window More...
 
SDL_Renderer * m_renderer = nullptr
 pointer to the SDL_Renderer More...
 
std::vector< std::unique_ptr< SDL_DisplayMode > > m_resolutions
 
int m_numOfDisplays = 0
 
int m_activeDisplay = 0
 

Additional Inherited Members

- Static Public Member Functions inherited from Singleton< WindowManager >
static WindowManagerinstance (void)
 Get an instance of the singleton. More...
 
- Protected Member Functions inherited from Singleton< WindowManager >
 Singleton () noexcept=default
 
 ~Singleton () noexcept=default
 

Detailed Description

Definition at line 19 of file WindowManager.hxx.

Constructor & Destructor Documentation

◆ WindowManager() [1/2]

WindowManager::WindowManager ( WindowManager const &  )
delete

◆ WindowManager() [2/2]

WindowManager::WindowManager ( )
private

Construct a new WindowManager object.

Exceptions
UIErrorif window or renderer creation fails, or if the window icon cannot be loaded
ConfigurationErrorif the path to the window icon doesn't exist

Definition at line 14 of file WindowManager.cxx.

15 {
16  Uint32 windowFlags = 0;
17  Uint32 rendererFlags = 0;
18 
19 #ifdef __ANDROID__
20  // Android is always fullscreen.. We also need to set screenWidth / screenHeight to the max. resolution in Fullscreen
21  windowFlags = SDL_WINDOW_FULLSCREEN;
22  SDL_DisplayMode mode;
23  SDL_GetDesktopDisplayMode(0, &mode);
26 #endif
27 
28 #ifdef TESTING_ENABLED
29  windowFlags = SDL_WINDOW_HIDDEN;
30  m_window = SDL_CreateWindow(m_title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, windowFlags);
31 #else
32  m_window = SDL_CreateWindow(m_title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, Settings::instance().screenWidth,
33  Settings::instance().screenHeight, windowFlags);
34 #endif
35 
36  if (!m_window)
37  throw UIError(TRACE_INFO "Failed to create window: " + string{SDL_GetError()});
38 
39  rendererFlags = SDL_RENDERER_ACCELERATED | (Settings::instance().vSync ? SDL_RENDERER_PRESENTVSYNC : 0);
40 
41 #if defined(TESTING_ENABLED) && defined(__linux)
42  // Set the index to 2 for running tests
43  m_renderer = SDL_CreateRenderer(m_window, 2, rendererFlags);
44 #else
45  m_renderer = SDL_CreateRenderer(m_window, -1, rendererFlags);
46 #endif
47 
48  SDL_RendererInfo info;
49  SDL_GetRendererInfo(m_renderer, &info);
50  LOG(LOG_DEBUG) << "Current SDL_Renderer: " << info.name;
51 
52  if (!m_renderer)
53  throw UIError(TRACE_INFO "Failed to create Renderer: " + string{SDL_GetError()});
54 
55  string iconFName = fs::getBasePath() + m_windowIcon;
56 
57  if (!fs::fileExists(iconFName))
58  throw ConfigurationError(TRACE_INFO "File " + iconFName + " doesn't exist");
59 
60  SDL_Surface *icon = IMG_Load(iconFName.c_str());
61 
62  if (!icon)
63  throw UIError(TRACE_INFO "Could not load icon " + iconFName + ": " + IMG_GetError());
64 
65  SDL_SetWindowIcon(m_window, icon);
66  SDL_FreeSurface(icon);
67  m_numOfDisplays = SDL_GetNumVideoDisplays();
69  setFullScreenMode(static_cast<FULLSCREEN_MODE>(Settings::instance().fullScreenMode));
70 
72 }
+ Here is the call graph for this function:

◆ ~WindowManager()

WindowManager::~WindowManager ( )
private

Destroy the WindowManager object.

Definition at line 74 of file WindowManager.cxx.

75 {
77 
78  SDL_DestroyRenderer(m_renderer);
79  SDL_DestroyWindow(m_window);
80 }
+ Here is the call graph for this function:

Member Function Documentation

◆ destroyImGuiRenderer()

void WindowManager::destroyImGuiRenderer ( )
private

shuts down the renderer for ImGui and the ImGui context.

Definition at line 181 of file WindowManager.cxx.

182 {
183  ImGui_ImplSDLRenderer_Shutdown();
184  ImGui_ImplSDL2_Shutdown();
185  ImGui::DestroyContext();
186 }
+ Here is the caller graph for this function:

◆ getRenderer()

SDL_Renderer* WindowManager::getRenderer ( ) const
inline

get SDL Renderer

Returns
Pointer to the SDL_Renderer

Definition at line 38 of file WindowManager.hxx.

38 { return m_renderer; };

◆ getSupportedScreenResolutions()

const std::vector<std::unique_ptr<SDL_DisplayMode> >& WindowManager::getSupportedScreenResolutions ( ) const
inline

Definition at line 45 of file WindowManager.hxx.

45 { return m_resolutions; };

◆ getWindow()

SDL_Window* WindowManager::getWindow ( ) const
inline

get SDL Window

Returns
Pointer to the SDL_Window

Definition at line 43 of file WindowManager.hxx.

43 { return m_window; };

◆ initializeImguiRenderer()

void WindowManager::initializeImguiRenderer ( )
private

Sets up platform/renderer backends and theme.

Definition at line 169 of file WindowManager.cxx.

170 {
171  // create context and default theme
172  IMGUI_CHECKVERSION();
173  ImGui::CreateContext();
174  ImGui::StyleColorsDark();
175 
176  // Setup Platform/Renderer backends
177  ImGui_ImplSDL2_InitForSDLRenderer(m_window, m_renderer);
178  ImGui_ImplSDLRenderer_Init(m_renderer);
179 }
+ Here is the caller graph for this function:

◆ initializeScreenResolutions()

void WindowManager::initializeScreenResolutions ( )
private

Definition at line 141 of file WindowManager.cxx.

142 {
144  throw UIError(TRACE_INFO "There is no display with number " + std::to_string(m_activeDisplay) + ". Resetting to display 0");
145 
146  // get the number of different screen modes
147  for (int modeIndex = 0; modeIndex <= SDL_GetNumDisplayModes(m_activeDisplay); modeIndex++)
148  {
149  std::unique_ptr<SDL_DisplayMode> displayMode =
150  std::make_unique<SDL_DisplayMode>((SDL_DisplayMode{SDL_PIXELFORMAT_UNKNOWN, 0, 0, 0, nullptr}));
151 
152  auto isResolitionExist = [&](int w, int h)
153  {
154  auto it = std::find_if(m_resolutions.begin(), m_resolutions.end(),
155  [w, h](auto &mode) { return (mode->w == w) && (mode->h == h); });
156  return it != m_resolutions.end();
157  };
158 
159  if (SDL_GetDisplayMode(m_activeDisplay, modeIndex, displayMode.get()) == 0)
160  {
161  if (isResolitionExist(displayMode->w, displayMode->h))
162  continue;
163 
164  m_resolutions.push_back(std::move(displayMode));
165  }
166  }
167 }
+ Here is the caller graph for this function:

◆ newImGuiFrame()

void WindowManager::newImGuiFrame ( )

creates and renders a new ImGui frame

Definition at line 224 of file WindowManager.cxx.

225 {
226  ImGui_ImplSDLRenderer_NewFrame();
227  ImGui_ImplSDL2_NewFrame();
228  ImGui::NewFrame();
229 }
+ Here is the caller graph for this function:

◆ operator=()

WindowManager& WindowManager::operator= ( WindowManager const &  )
delete

◆ renderScreen()

void WindowManager::renderScreen ( )

Definition at line 231 of file WindowManager.cxx.

232 {
233  // reset renderer color back to black
234  SDL_SetRenderDrawColor(m_renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
235 
236  ImGui::Render();
237  ImGui_ImplSDLRenderer_RenderDrawData(ImGui::GetDrawData());
238 
239  // Render the Frame
240  SDL_RenderPresent(m_renderer);
241 }
+ Here is the caller graph for this function:

◆ setFullScreenMode()

void WindowManager::setFullScreenMode ( FULLSCREEN_MODE  mode) const

sets the game's window mode

Changes the game's window mode to fullscreen, borderless, or windowed

Parameters
modethe new window mode, WINDOWED, BORDERLESS, or FULLSCREEN

Definition at line 96 of file WindowManager.cxx.

97 {
98  Settings::instance().fullScreenMode = static_cast<int>(mode);
99 
100  // reset the actual resolution back to the desired resolution
103  switch (mode)
104  {
106  SDL_SetWindowFullscreen(m_window, 0);
107  break;
109  SDL_SetWindowFullscreen(m_window, SDL_WINDOW_FULLSCREEN);
110  break;
112  SDL_DisplayMode desktopScreenMode;
113  if (SDL_GetDesktopDisplayMode(0, &desktopScreenMode) != 0)
114  {
115  LOG(LOG_INFO) << "SDL_GetDesktopDisplayMode failed: " << SDL_GetError();
116  }
117  // set the actual resolution to the desktop resolution for Borderless
118  Settings::instance().currentScreenHeight = desktopScreenMode.h;
119  Settings::instance().currentScreenWidth = desktopScreenMode.w;
120 
121  // As a workaround, need to switch back into windowed mode before changing the display mode, then back to full screen mode.
122  // Minimize / Restore is another workaround to get the change from fullscreen to Borderless working
123  SDL_SetWindowFullscreen(m_window, 0);
124  SDL_SetWindowFullscreen(m_window, SDL_WINDOW_FULLSCREEN_DESKTOP);
125  SDL_MinimizeWindow(m_window);
126  SDL_RestoreWindow(m_window);
127 
128  break;
129  default:
130  // do nothing or log message on the off chance this is reached
131  break;
132  }
133 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setScreenResolution()

void WindowManager::setScreenResolution ( int  mode)

Definition at line 188 of file WindowManager.cxx.

189 {
190  // check if the desired mode exists first
191  if (mode >= 0 && mode < static_cast<int>(m_resolutions.size()) && m_resolutions[mode])
192  {
193  // m_lastSelectedResolutionIdx = mode;
196 
197  // update the actual resolution
200 
201  switch (static_cast<FULLSCREEN_MODE>(Settings::instance().fullScreenMode))
202  {
204  SDL_SetWindowDisplayMode(m_window, m_resolutions[mode].get());
205  // workaround. After setting Display Resolution in fullscreen, it won't work until disabling / enabling Fullscreen again.
206  SDL_SetWindowFullscreen(m_window, 0);
207  SDL_SetWindowFullscreen(m_window, SDL_WINDOW_FULLSCREEN);
208  break;
210  SDL_SetWindowSize(m_window, m_resolutions[mode]->w, m_resolutions[mode]->h);
211  SDL_SetWindowPosition(m_window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
212  break;
213  default: //AKA FULLSCREEN_MODE::BORDERLESS
214  // do nothing for Borderless fullscreen, it's always the screenSize
215  break;
216  }
217  }
218  else
219  {
220  throw UIError(TRACE_INFO "Cannot set screen mode " + std::to_string(mode));
221  }
222 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setWindowTitle()

void WindowManager::setWindowTitle ( const std::string title)

sets title of the window

Parameters
titlethe new title of the window

Definition at line 135 of file WindowManager.cxx.

136 {
137  m_title = title;
138  SDL_SetWindowTitle(m_window, m_title.c_str());
139 }
+ Here is the caller graph for this function:

◆ toggleFullScreen()

void WindowManager::toggleFullScreen ( ) const

Definition at line 82 of file WindowManager.cxx.

83 {
85 
86  if (Settings::instance().fullScreen)
87  {
88  SDL_SetWindowFullscreen(m_window, SDL_WINDOW_FULLSCREEN);
89  }
90  else
91  {
92  SDL_SetWindowFullscreen(m_window, 0);
93  }
94 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ m_activeDisplay

int WindowManager::m_activeDisplay = 0
private

Definition at line 82 of file WindowManager.hxx.

◆ m_numOfDisplays

int WindowManager::m_numOfDisplays = 0
private

Definition at line 81 of file WindowManager.hxx.

◆ m_renderer

SDL_Renderer* WindowManager::m_renderer = nullptr
private

pointer to the SDL_Renderer

Definition at line 77 of file WindowManager.hxx.

◆ m_resolutions

std::vector<std::unique_ptr<SDL_DisplayMode> > WindowManager::m_resolutions
private

Definition at line 79 of file WindowManager.hxx.

◆ m_title

std::string WindowManager::m_title = "Cytopia"
private

title of the window

Definition at line 73 of file WindowManager.hxx.

◆ m_window

SDL_Window* WindowManager::m_window = nullptr
private

pointer to the SDL_Window

Definition at line 76 of file WindowManager.hxx.

◆ m_windowIcon

const std::string WindowManager::m_windowIcon = "resources/images/app_icons/cytopia_icon.png"
private

the window's icon

Definition at line 74 of file WindowManager.hxx.

◆ Singleton< WindowManager >

Definition at line 22 of file WindowManager.hxx.


The documentation for this class was generated from the following files:
TRACE_INFO
#define TRACE_INFO
Definition: Exception.hxx:12
ConfigurationError
A configuration error.
Definition: Exception.hxx:36
WindowManager::destroyImGuiRenderer
void destroyImGuiRenderer()
shuts down the renderer for ImGui and the ImGui context.
Definition: WindowManager.cxx:181
UIError
A UI-related error occured.
Definition: Exception.hxx:76
SettingsData::vSync
bool vSync
if vSync is enabled or not
Definition: Settings.hxx:70
LOG
Definition: LOG.hxx:32
fileExists
bool fileExists(const std::string &filePath)
Check if a file (or folder) exists.
Definition: Filesystem.cxx:62
FULLSCREEN_MODE::FULLSCREEN
@ FULLSCREEN
WindowManager::m_windowIcon
const std::string m_windowIcon
the window's icon
Definition: WindowManager.hxx:74
LOG_INFO
@ LOG_INFO
Definition: LOG.hxx:25
WindowManager::m_title
std::string m_title
title of the window
Definition: WindowManager.hxx:73
SettingsData::fullScreen
bool fullScreen
Definition: Settings.hxx:76
WindowManager::m_resolutions
std::vector< std::unique_ptr< SDL_DisplayMode > > m_resolutions
Definition: WindowManager.hxx:79
SettingsData::screenHeight
ScreenDimension screenHeight
the screen height
Definition: Settings.hxx:46
FULLSCREEN_MODE
FULLSCREEN_MODE
Definition: WindowManager.hxx:12
LOG_DEBUG
@ LOG_DEBUG
Definition: LOG.hxx:26
FULLSCREEN_MODE::BORDERLESS
@ BORDERLESS
SettingsData::screenWidth
ScreenDimension screenWidth
the screen width
Definition: Settings.hxx:40
getBasePath
std::string getBasePath()
Get base path (where Cytopia is being run)
Definition: Filesystem.cxx:77
SettingsData::currentScreenWidth
ScreenDimension currentScreenWidth
the actual screen width (can differ from the one that's set in borderless fullscreen)
Definition: Settings.hxx:49
FULLSCREEN_MODE::WINDOWED
@ WINDOWED
WindowManager::m_renderer
SDL_Renderer * m_renderer
pointer to the SDL_Renderer
Definition: WindowManager.hxx:77
WindowManager::setFullScreenMode
void setFullScreenMode(FULLSCREEN_MODE mode) const
sets the game's window mode
Definition: WindowManager.cxx:96
SettingsData::fullScreenMode
int fullScreenMode
Definition: Settings.hxx:82
WindowManager::m_numOfDisplays
int m_numOfDisplays
Definition: WindowManager.hxx:81
WindowManager::m_window
SDL_Window * m_window
pointer to the SDL_Window
Definition: WindowManager.hxx:76
SettingsData::currentScreenHeight
ScreenDimension currentScreenHeight
the actual screen height (can differ from the one that's set in borderless fullscreen)
Definition: Settings.hxx:52
Singleton< Settings >::instance
static Settings & instance(void)
Get an instance of the singleton.
Definition: Singleton.hxx:15
WindowManager::initializeScreenResolutions
void initializeScreenResolutions()
Definition: WindowManager.cxx:141
WindowManager::m_activeDisplay
int m_activeDisplay
Definition: WindowManager.hxx:82
WindowManager::initializeImguiRenderer
void initializeImguiRenderer()
Sets up platform/renderer backends and theme.
Definition: WindowManager.cxx:169