Skip to content

Commit

Permalink
Replace bool fullscreen parameter with bitset enum
Browse files Browse the repository at this point in the history
  • Loading branch information
falbrechtskirchinger committed Jul 26, 2023
1 parent 86fbb04 commit 9c88c90
Show file tree
Hide file tree
Showing 21 changed files with 118 additions and 84 deletions.
38 changes: 22 additions & 16 deletions extras/videoDrivers/SDL2/VideoSDL2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void VideoSDL2::UpdateCurrentSizes()
SetNewSize(VideoMode(w, h), Extent(w2, h2));
}

bool VideoSDL2::CreateScreen(const std::string& title, const VideoMode& size, bool fullscreen)
bool VideoSDL2::CreateScreen(const std::string& title, const VideoMode& size, DisplayMode displayMode)
{
if(!initialized)
return false;
Expand All @@ -125,18 +125,19 @@ bool VideoSDL2::CreateScreen(const std::string& title, const VideoMode& size, bo
CHECK_SDL(SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8));
CHECK_SDL(SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1));

int wndPos = SDL_WINDOWPOS_CENTERED;

const int wndPos = SDL_WINDOWPOS_CENTERED;
const auto fullscreen = bitset::isSet(displayMode, DisplayMode::Fullscreen);
const auto requestedSize = fullscreen ? FindClosestVideoMode(size) : size;

const unsigned commonFlags = SDL_WINDOW_OPENGL;
const unsigned fullscreenFlag = (fullscreen ? SDL_WINDOW_FULLSCREEN : 0);
window = SDL_CreateWindow(title.c_str(), wndPos, wndPos, requestedSize.width, requestedSize.height,
SDL_WINDOW_OPENGL | (fullscreen ? SDL_WINDOW_FULLSCREEN : SDL_WINDOW_RESIZABLE));
commonFlags | fullscreenFlag);

// Fallback to non-fullscreen
if(!window && fullscreen)
{
window = SDL_CreateWindow(title.c_str(), wndPos, wndPos, requestedSize.width, requestedSize.height,
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
window =
SDL_CreateWindow(title.c_str(), wndPos, wndPos, requestedSize.width, requestedSize.height, commonFlags);
}

if(!window)
Expand All @@ -145,10 +146,11 @@ bool VideoSDL2::CreateScreen(const std::string& title, const VideoMode& size, bo
return false;
}

isFullscreen_ = (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN) != 0;
displayMode_ =
bitset::set(displayMode_, DisplayMode::Fullscreen, (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN) != 0);
UpdateCurrentSizes();

if(!isFullscreen_)
if(!bitset::isSet(displayMode_, DisplayMode::Fullscreen))
MoveWindowToCenter();

SDL_Surface* iconSurf =
Expand All @@ -173,16 +175,19 @@ bool VideoSDL2::CreateScreen(const std::string& title, const VideoMode& size, bo
return true;
}

bool VideoSDL2::ResizeScreen(const VideoMode& newSize, bool fullscreen)
bool VideoSDL2::ResizeScreen(const VideoMode& newSize, DisplayMode displayMode)
{
if(!initialized)
return false;

if(isFullscreen_ != fullscreen)
const auto newFullscreen = bitset::isSet(displayMode, DisplayMode::Fullscreen);
auto fullscreen = bitset::isSet(displayMode_, DisplayMode::Fullscreen);
if(fullscreen != newFullscreen)
{
SDL_SetWindowFullscreen(window, fullscreen ? SDL_WINDOW_FULLSCREEN : 0);
isFullscreen_ = (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN) != 0;
if(!isFullscreen_)
SDL_SetWindowFullscreen(window, newFullscreen ? SDL_WINDOW_FULLSCREEN : 0);
fullscreen = (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN) != 0;
displayMode_ = bitset::set(displayMode_, DisplayMode::Fullscreen, fullscreen);
if(!fullscreen)
{
#if SDL_VERSION_ATLEAST(2, 0, 5)
SDL_SetWindowResizable(window, SDL_TRUE);
Expand All @@ -193,7 +198,7 @@ bool VideoSDL2::ResizeScreen(const VideoMode& newSize, bool fullscreen)

if(newSize != GetWindowSize())
{
if(isFullscreen_)
if(fullscreen)
{
auto const targetMode = FindClosestVideoMode(newSize);
SDL_DisplayMode target;
Expand Down Expand Up @@ -267,7 +272,8 @@ bool VideoSDL2::MessageLoop()
{
case SDL_WINDOWEVENT_RESIZED:
{
isFullscreen_ = (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN) != 0;
displayMode_ = bitset::set(displayMode_, DisplayMode::Fullscreen,
(SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN) != 0);
VideoMode newSize(ev.window.data1, ev.window.data2);
if(newSize != GetWindowSize())
{
Expand Down
4 changes: 2 additions & 2 deletions extras/videoDrivers/SDL2/VideoSDL2.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class VideoSDL2 final : public VideoDriver

bool Initialize() override;

bool CreateScreen(const std::string& title, const VideoMode& size, bool fullscreen) override;
bool ResizeScreen(const VideoMode& newSize, bool fullscreen) override;
bool CreateScreen(const std::string& title, const VideoMode& size, DisplayMode displayMode) override;
bool ResizeScreen(const VideoMode& newSize, DisplayMode displayMode) override;

void DestroyScreen() override;

Expand Down
24 changes: 14 additions & 10 deletions extras/videoDrivers/WinAPI/WinAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,18 @@ void VideoWinAPI::CleanUp()
* @bug Hardwarecursor ist bei Fenstermodus sichtbar,
* Cursor deaktivieren ist fehlerhaft
*/
bool VideoWinAPI::CreateScreen(const std::string& title, const VideoMode& newSize, bool fullscreen)
bool VideoWinAPI::CreateScreen(const std::string& title, const VideoMode& newSize, DisplayMode displayMode)
{
if(!initialized)
return false;

const auto fullscreen = bitset::isSet(displayMode, DisplayMode::Fullscreen);
if(!RegisterAndCreateWindow(title, newSize, fullscreen))
return false;

if(fullscreen && !MakeFullscreen(GetWindowSize()))
return false;
isFullscreen_ = fullscreen;
displayMode_ = bitset::set(displayMode_, DisplayMode::Fullscreen);

if(!InitOGL())
return false;
Expand All @@ -174,34 +175,37 @@ bool VideoWinAPI::CreateScreen(const std::string& title, const VideoMode& newSiz
*
* @todo Vollbildmodus ggf. wechseln
*/
bool VideoWinAPI::ResizeScreen(const VideoMode& newSize, bool fullscreen)
bool VideoWinAPI::ResizeScreen(const VideoMode& newSize, DisplayMode displayMode)
{
if(!initialized || !isWindowResizable)
return false;

if(isFullscreen_ == fullscreen && newSize == GetWindowSize())
const auto newFullscreen = bitset::isSet(displayMode, DisplayMode::Fullscreen);
auto fullscreen = bitset::isSet(displayMode_, DisplayMode::Fullscreen);
if(fullscreen == newFullscreen && newSize == GetWindowSize())
return true;

ShowWindow(screen, SW_HIDE);

VideoMode windowSize = fullscreen ? FindClosestVideoMode(newSize) : newSize;
VideoMode windowSize = newFullscreen ? FindClosestVideoMode(newSize) : newSize;
// Try to switch full screen first
if(isFullscreen_ && !fullscreen)
if(fullscreen && !newFullscreen)
{
if(ChangeDisplaySettings(nullptr, 0) != DISP_CHANGE_SUCCESSFUL)
return false;
} else if(isFullscreen_ || fullscreen)
} else if(fullscreen || newFullscreen)
{
if(!MakeFullscreen(windowSize))
return false;
}
displayMode_ = bitset::set(displayMode_, DisplayMode::Fullscreen, newFullscreen);

// Fensterstyle ggf. ändern
std::pair<DWORD, DWORD> style = GetStyleFlags(isFullscreen_);
std::pair<DWORD, DWORD> style = GetStyleFlags(newFullscreen);
SetWindowLongPtr(screen, GWL_STYLE, style.first);
SetWindowLongPtr(screen, GWL_EXSTYLE, style.second);

RECT wRect = CalculateWindowRect(isFullscreen_, windowSize);
RECT wRect = CalculateWindowRect(newFullscreen, windowSize);

// Fenstergröße ändern
UINT flags = SWP_SHOWWINDOW | SWP_DRAWFRAME | SWP_FRAMECHANGED;
Expand Down Expand Up @@ -405,7 +409,7 @@ void VideoWinAPI::DestroyScreen()

UnregisterClassW(windowClassName.c_str(), GetModuleHandle(nullptr));

isFullscreen_ = false;
displayMode_ = bitset::clear(displayMode_, DisplayMode::Fullscreen);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions extras/videoDrivers/WinAPI/WinAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ class VideoWinAPI final : public VideoDriver
bool Initialize() override;

/// Erstellt das Fenster mit entsprechenden Werten.
bool CreateScreen(const std::string& title, const VideoMode& newSize, bool fullscreen) override;
bool CreateScreen(const std::string& title, const VideoMode& newSize, DisplayMode displayMode) override;

/// Erstellt oder verändert das Fenster mit entsprechenden Werten.
bool ResizeScreen(const VideoMode& newSize, bool fullscreen) override;
bool ResizeScreen(const VideoMode& newSize, DisplayMode displayMode) override;

/// Schliesst das Fenster.
void DestroyScreen() override;
Expand Down
4 changes: 2 additions & 2 deletions libs/driver/include/driver/VideoDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class VideoDriver : public IVideoDriver

VideoMode GetWindowSize() const override final { return windowSize_; }
Extent GetRenderSize() const override final { return renderSize_; }
bool IsFullscreen() const override final { return isFullscreen_; }
DisplayMode GetDisplayMode() const override final { return displayMode_; }

/// prüft auf Initialisierung.
bool IsInitialized() const override final { return initialized; }
Expand All @@ -43,7 +43,7 @@ class VideoDriver : public IVideoDriver
bool initialized; /// Initialisierungsstatus.
MouseCoords mouse_xy; /// Status der Maus.
std::array<bool, 512> keyboard; /// Status der Tastatur;
bool isFullscreen_; /// Vollbild an/aus?
DisplayMode displayMode_; /// Fullscreen on/off?
private:
// cached as possibly used often
VideoMode windowSize_;
Expand Down
14 changes: 11 additions & 3 deletions libs/driver/include/driver/VideoInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@
#include "Point.h"
#include "VideoMode.h"
#include "exportImport.h"
#include "s25util/enumUtils.h"
#include <string>
#include <vector>

/// Function type for loading OpenGL methods
using OpenGL_Loader_Proc = void* (*)(const char*);

enum class DisplayMode : unsigned
{
None,
Fullscreen = 1 << 0
};
MAKE_BITSET_STRONG(DisplayMode);

class BOOST_SYMBOL_VISIBLE IVideoDriver
{
public:
Expand All @@ -25,9 +33,9 @@ class BOOST_SYMBOL_VISIBLE IVideoDriver
virtual bool Initialize() = 0;

/// Erstellt das Fenster mit entsprechenden Werten.
virtual bool CreateScreen(const std::string& title, const VideoMode& newSize, bool fullscreen) = 0;
virtual bool CreateScreen(const std::string& title, const VideoMode& newSize, DisplayMode displayMode) = 0;

virtual bool ResizeScreen(const VideoMode& newSize, bool fullscreen) = 0;
virtual bool ResizeScreen(const VideoMode& newSize, DisplayMode displayMode) = 0;

/// Schliesst das Fenster.
virtual void DestroyScreen() = 0;
Expand Down Expand Up @@ -61,7 +69,7 @@ class BOOST_SYMBOL_VISIBLE IVideoDriver
virtual VideoMode GetWindowSize() const = 0;
/// Get the size of the render region in pixels
virtual Extent GetRenderSize() const = 0;
virtual bool IsFullscreen() const = 0;
virtual DisplayMode GetDisplayMode() const = 0;

/// Get state of the modifier keys
virtual KeyEvent GetModKeyState() const = 0;
Expand Down
2 changes: 1 addition & 1 deletion libs/driver/src/VideoDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ IVideoDriver::~IVideoDriver() = default;
* @param[in] CallBack DriverCallback für Rückmeldungen.
*/
VideoDriver::VideoDriver(VideoDriverLoaderInterface* CallBack)
: CallBack(CallBack), initialized(false), isFullscreen_(false), renderSize_(0, 0)
: CallBack(CallBack), initialized(false), displayMode_(DisplayMode::None), renderSize_(0, 0)
{
std::fill(keyboard.begin(), keyboard.end(), false);
}
Expand Down
7 changes: 4 additions & 3 deletions libs/s25main/GameManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ bool GameManager::Start()
}

// Fenster erstellen
const auto screenSize =
settings_.video.fullscreen ? settings_.video.fullscreenSize : settings_.video.windowedSize; //-V807
if(!videoDriver_.CreateScreen(screenSize, settings_.video.fullscreen))
const auto screenSize = bitset::isSet(settings_.video.displayMode, DisplayMode::Fullscreen) ?
settings_.video.fullscreenSize :
settings_.video.windowedSize; //-V807
if(!videoDriver_.CreateScreen(screenSize, settings_.video.displayMode))
return false;
videoDriver_.setTargetFramerate(settings_.video.framerate);
videoDriver_.SetMouseWarping(settings_.global.smartCursor);
Expand Down
9 changes: 5 additions & 4 deletions libs/s25main/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ void Settings::LoadDefaults()
{
video.fullscreenSize = VIDEODRIVER.GetWindowSize();
video.windowedSize = VIDEODRIVER.IsFullscreen() ? VideoMode(800, 600) : video.fullscreenSize;
video.fullscreen = VIDEODRIVER.IsFullscreen();
video.displayMode = VIDEODRIVER.GetDisplayMode();
} else
{
video.windowedSize = video.fullscreenSize = VideoMode(800, 600);
video.fullscreen = false;
video.displayMode = DisplayMode::None;
}
video.framerate = 0; // Special value for HW vsync
video.vbo = true;
Expand Down Expand Up @@ -223,7 +223,8 @@ void Settings::Load()
video.windowedSize.height = iniVideo->getIntValue("windowed_height");
video.fullscreenSize.width = iniVideo->getIntValue("fullscreen_width");
video.fullscreenSize.height = iniVideo->getIntValue("fullscreen_height");
video.fullscreen = iniVideo->getBoolValue("fullscreen");
video.displayMode =
bitset::set(video.displayMode, DisplayMode::Fullscreen, iniVideo->getBoolValue("fullscreen"));
video.framerate = iniVideo->getValue("framerate", 0);
video.vbo = iniVideo->getBoolValue("vbo");
video.shared_textures = iniVideo->getBoolValue("shared_textures");
Expand Down Expand Up @@ -405,7 +406,7 @@ void Settings::Save()
iniVideo->setValue("fullscreen_height", video.fullscreenSize.height);
iniVideo->setValue("windowed_width", video.windowedSize.width);
iniVideo->setValue("windowed_height", video.windowedSize.height);
iniVideo->setValue("fullscreen", video.fullscreen);
iniVideo->setValue("fullscreen", bitset::isSet(video.displayMode, DisplayMode::Fullscreen));
iniVideo->setValue("framerate", video.framerate);
iniVideo->setValue("vbo", video.vbo);
iniVideo->setValue("shared_textures", video.shared_textures);
Expand Down
3 changes: 2 additions & 1 deletion libs/s25main/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#pragma once

#include "DrawPoint.h"
#include "driver/VideoInterface.h"
#include "driver/VideoMode.h"
#include "s25util/ProxySettings.h"
#include "s25util/Singleton.h"
Expand Down Expand Up @@ -59,7 +60,7 @@ class Settings : public Singleton<Settings, SingletonPolicies::WithLongevity>
{
VideoMode fullscreenSize, windowedSize;
signed short framerate; // <0 for unlimited, 0 for HW Vsync
bool fullscreen;
DisplayMode displayMode;
bool vbo;
bool shared_textures;
} video;
Expand Down
11 changes: 6 additions & 5 deletions libs/s25main/WindowManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -600,10 +600,11 @@ void WindowManager::Msg_KeyDown(const KeyEvent& ke)
if(ke.alt && (ke.kt == KeyType::Return))
{
// Switch Fullscreen/Windowed
const auto newScreenSize =
!SETTINGS.video.fullscreen ? SETTINGS.video.fullscreenSize : SETTINGS.video.windowedSize; //-V807
VIDEODRIVER.ResizeScreen(newScreenSize, !SETTINGS.video.fullscreen);
SETTINGS.video.fullscreen = VIDEODRIVER.IsFullscreen();
const auto newScreenSize = !bitset::isSet(SETTINGS.video.displayMode, DisplayMode::Fullscreen) ?
SETTINGS.video.fullscreenSize :
SETTINGS.video.windowedSize; //-V807
VIDEODRIVER.ResizeScreen(newScreenSize, bitset::toggle(SETTINGS.video.displayMode, DisplayMode::Fullscreen));
SETTINGS.video.displayMode = VIDEODRIVER.GetDisplayMode();
} else if(ke.kt == KeyType::Print)
TakeScreenshot();
else
Expand Down Expand Up @@ -632,7 +633,7 @@ void WindowManager::Msg_ScreenResize(const Extent& newSize)
curRenderSize = sr.newSize;

// Don't change fullscreen size (only in menu)
if(!SETTINGS.video.fullscreen)
if(!bitset::isSet(SETTINGS.video.displayMode, DisplayMode::Fullscreen))
SETTINGS.video.windowedSize = VIDEODRIVER.GetWindowSize();

// ist unser Desktop gültig?
Expand Down
2 changes: 1 addition & 1 deletion libs/s25main/desktops/dskBenchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ void dskBenchmark::Msg_PaintAfter()
void dskBenchmark::SetActive(bool activate)
{
if(!IsActive() && activate)
VIDEODRIVER.ResizeScreen(VideoMode(1600, 900), false);
VIDEODRIVER.ResizeScreen(VideoMode(1600, 900), DisplayMode::None);
dskMenuBase::SetActive(activate);
}

Expand Down
18 changes: 10 additions & 8 deletions libs/s25main/desktops/dskOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,8 @@ dskOptions::dskOptions() : Desktop(LOADER.GetImageN("setup013", 0))
}

// "Vollbild" setzen
groupGrafik->GetCtrl<ctrlOptionGroup>(ID_grpFullscreen)->SetSelection(SETTINGS.video.fullscreen); //-V807
groupGrafik->GetCtrl<ctrlOptionGroup>(ID_grpFullscreen)
->SetSelection(bitset::isSet(SETTINGS.video.displayMode, DisplayMode::Fullscreen)); //-V807

// "Limit Framerate" füllen
auto* cbFrameRate = groupGrafik->GetCtrl<ctrlComboBox>(ID_cbFramerate);
Expand Down Expand Up @@ -502,7 +503,9 @@ void dskOptions::Msg_Group_OptionGroupChange(const unsigned /*group_id*/, const
switch(ctrl_id)
{
case ID_grpIpv6: SETTINGS.server.ipv6 = enabled; break;
case ID_grpFullscreen: SETTINGS.video.fullscreen = enabled; break;
case ID_grpFullscreen:
SETTINGS.video.displayMode = bitset::set(SETTINGS.video.displayMode, DisplayMode::Fullscreen, enabled);
break;
case ID_grpVBO: SETTINGS.video.vbo = enabled; break;
case ID_grpOptTextures: SETTINGS.video.shared_textures = enabled; break;
case ID_grpMusic:
Expand Down Expand Up @@ -569,13 +572,12 @@ void dskOptions::Msg_ButtonClick(const unsigned ctrl_id)
return;

SETTINGS.Save();

if((SETTINGS.video.fullscreen && SETTINGS.video.fullscreenSize != VIDEODRIVER.GetWindowSize()) //-V807
|| SETTINGS.video.fullscreen != VIDEODRIVER.IsFullscreen())
const auto fullscreen = bitset::isSet(SETTINGS.video.displayMode, DisplayMode::Fullscreen);
if((fullscreen && SETTINGS.video.fullscreenSize != VIDEODRIVER.GetWindowSize()) //-V807
|| fullscreen != VIDEODRIVER.IsFullscreen())
{
const auto screenSize =
SETTINGS.video.fullscreen ? SETTINGS.video.fullscreenSize : SETTINGS.video.windowedSize;
if(!VIDEODRIVER.ResizeScreen(screenSize, SETTINGS.video.fullscreen))
const auto screenSize = fullscreen ? SETTINGS.video.fullscreenSize : SETTINGS.video.windowedSize;
if(!VIDEODRIVER.ResizeScreen(screenSize, SETTINGS.video.displayMode))
{
WINDOWMANAGER.Show(std::make_unique<iwMsgbox>(
_("Sorry!"), _("You need to restart your game to change the screen resolution!"), this,
Expand Down
Loading

0 comments on commit 9c88c90

Please sign in to comment.