Skip to content

Commit d40a26b

Browse files
Enable non-resizable windows (SDL2 only)
Add a hidden setting to control whether the window is resizable. Fixes #1512
1 parent 60499d9 commit d40a26b

File tree

20 files changed

+88
-38
lines changed

20 files changed

+88
-38
lines changed

extras/videoDrivers/SDL2/VideoSDL2.cpp

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ void VideoSDL2::UpdateCurrentSizes()
9191
SetNewSize(VideoMode(w, h), Extent(w2, h2));
9292
}
9393

94-
bool VideoSDL2::CreateScreen(const std::string& title, const VideoMode& size, bool fullscreen)
94+
bool VideoSDL2::CreateScreen(const std::string& title, const VideoMode& size, WindowMode windowMode, bool fullscreen)
9595
{
9696
if(!initialized)
9797
return false;
@@ -116,15 +116,18 @@ bool VideoSDL2::CreateScreen(const std::string& title, const VideoMode& size, bo
116116
int wndPos = SDL_WINDOWPOS_CENTERED;
117117

118118
const auto requestedSize = fullscreen ? FindClosestVideoMode(size) : size;
119+
unsigned commonFlags = SDL_WINDOW_OPENGL;
120+
unsigned fullscreenFlags = (fullscreen ? SDL_WINDOW_FULLSCREEN : 0);
121+
unsigned windowFlags = (windowMode == WindowMode::Resizable ? SDL_WINDOW_RESIZABLE : 0);
119122

120123
window = SDL_CreateWindow(title.c_str(), wndPos, wndPos, requestedSize.width, requestedSize.height,
121-
SDL_WINDOW_OPENGL | (fullscreen ? SDL_WINDOW_FULLSCREEN : SDL_WINDOW_RESIZABLE));
124+
commonFlags | fullscreenFlags | windowFlags);
122125

123126
// Fallback to non-fullscreen
124127
if(!window && fullscreen)
125128
{
126129
window = SDL_CreateWindow(title.c_str(), wndPos, wndPos, requestedSize.width, requestedSize.height,
127-
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
130+
commonFlags | windowFlags);
128131
}
129132

130133
if(!window)
@@ -133,7 +136,9 @@ bool VideoSDL2::CreateScreen(const std::string& title, const VideoMode& size, bo
133136
return false;
134137
}
135138

136-
isFullscreen_ = (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN) != 0;
139+
const auto flags = SDL_GetWindowFlags(window);
140+
isFullscreen_ = (flags & SDL_WINDOW_FULLSCREEN) != 0;
141+
windowMode_ = ((flags & SDL_WINDOW_RESIZABLE) != 0 ? WindowMode::Resizable : WindowMode::NonResizable);
137142
UpdateCurrentSizes();
138143

139144
if(!isFullscreen_)
@@ -161,7 +166,7 @@ bool VideoSDL2::CreateScreen(const std::string& title, const VideoMode& size, bo
161166
return true;
162167
}
163168

164-
bool VideoSDL2::ResizeScreen(const VideoMode& newSize, bool fullscreen)
169+
bool VideoSDL2::ResizeScreen(const VideoMode& newSize, WindowMode windowMode, bool fullscreen)
165170
{
166171
if(!initialized)
167172
return false;
@@ -171,12 +176,17 @@ bool VideoSDL2::ResizeScreen(const VideoMode& newSize, bool fullscreen)
171176
SDL_SetWindowFullscreen(window, fullscreen ? SDL_WINDOW_FULLSCREEN : 0);
172177
isFullscreen_ = (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN) != 0;
173178
if(!isFullscreen_)
174-
{
179+
MoveWindowToCenter();
180+
}
181+
182+
if(!isFullscreen_ || windowMode != windowMode_)
183+
{
184+
if(!isFullscreen_)
175185
#if SDL_VERSION_ATLEAST(2, 0, 5)
176-
SDL_SetWindowResizable(window, SDL_TRUE);
186+
SDL_SetWindowResizable(window, static_cast<SDL_bool>(windowMode == WindowMode::Resizable));
177187
#endif
178-
MoveWindowToCenter();
179-
}
188+
windowMode_ =
189+
((SDL_GetWindowFlags(window) & SDL_WINDOW_RESIZABLE) != 0 ? WindowMode::Resizable : WindowMode::NonResizable);
180190
}
181191

182192
if(newSize != GetWindowSize())
@@ -203,6 +213,17 @@ bool VideoSDL2::ResizeScreen(const VideoMode& newSize, bool fullscreen)
203213
}
204214
UpdateCurrentSizes();
205215
}
216+
217+
if(isFullscreen_ != fullscreen || windowMode != windowMode_)
218+
{
219+
if(!isFullscreen_)
220+
#if SDL_VERSION_ATLEAST(2, 0, 5)
221+
SDL_SetWindowResizable(window, static_cast<SDL_bool>(windowMode == WindowMode::Resizable));
222+
#endif
223+
windowMode_ =
224+
((SDL_GetWindowFlags(window) & SDL_WINDOW_RESIZABLE) != 0 ? WindowMode::Resizable : WindowMode::NonResizable);
225+
}
226+
206227
return true;
207228
}
208229

extras/videoDrivers/SDL2/VideoSDL2.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ class VideoSDL2 final : public VideoDriver
2424

2525
bool Initialize() override;
2626

27-
bool CreateScreen(const std::string& title, const VideoMode& size, bool fullscreen) override;
28-
bool ResizeScreen(const VideoMode& newSize, bool fullscreen) override;
27+
bool CreateScreen(const std::string& title, const VideoMode& size, WindowMode windowMode, bool fullscreen) override;
28+
bool ResizeScreen(const VideoMode& newSize, WindowMode windowMode, bool fullscreen) override;
2929

3030
void DestroyScreen() override;
3131

extras/videoDrivers/WinAPI/WinAPI.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ void VideoWinAPI::CleanUp()
138138
* @bug Hardwarecursor ist bei Fenstermodus sichtbar,
139139
* Cursor deaktivieren ist fehlerhaft
140140
*/
141-
bool VideoWinAPI::CreateScreen(const std::string& title, const VideoMode& newSize, bool fullscreen)
141+
bool VideoWinAPI::CreateScreen(const std::string& title, const VideoMode& newSize, WindowMode /* windowMode */,
142+
bool fullscreen)
142143
{
143144
if(!initialized)
144145
return false;
@@ -174,7 +175,7 @@ bool VideoWinAPI::CreateScreen(const std::string& title, const VideoMode& newSiz
174175
*
175176
* @todo Vollbildmodus ggf. wechseln
176177
*/
177-
bool VideoWinAPI::ResizeScreen(const VideoMode& newSize, bool fullscreen)
178+
bool VideoWinAPI::ResizeScreen(const VideoMode& newSize, WindowMode /* windowMode */, bool fullscreen)
178179
{
179180
if(!initialized || !isWindowResizable)
180181
return false;
@@ -253,7 +254,7 @@ RECT VideoWinAPI::CalculateWindowRect(bool fullscreen, VideoMode& size) const
253254
return wRect;
254255
}
255256

256-
bool VideoWinAPI::RegisterAndCreateWindow(const std::string& title, const VideoMode& wndSize, bool fullscreen)
257+
bool VideoWinAPI::RegisterAndCreateWindow(const std::string& title, const VideoMode& wndSize, WindowMode windowMode)
257258
{
258259
std::wstring wTitle = boost::nowide::widen(title);
259260
windowClassName = wTitle.substr(0, wTitle.find(' '));

extras/videoDrivers/WinAPI/WinAPI.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ class VideoWinAPI final : public VideoDriver
3030
bool Initialize() override;
3131

3232
/// Erstellt das Fenster mit entsprechenden Werten.
33-
bool CreateScreen(const std::string& title, const VideoMode& newSize, bool fullscreen) override;
33+
bool CreateScreen(const std::string& title, const VideoMode& newSize, WindowMode windowMode,
34+
bool fullscreen) override;
3435

3536
/// Erstellt oder verändert das Fenster mit entsprechenden Werten.
36-
bool ResizeScreen(const VideoMode& newSize, bool fullscreen) override;
37+
bool ResizeScreen(const VideoMode& newSize, WindowMode windowMode, bool fullscreen) override;
3738

3839
/// Schliesst das Fenster.
3940
void DestroyScreen() override;

libs/driver/include/driver/VideoDriver.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class VideoDriver : public IVideoDriver
3030
VideoMode GetWindowSize() const override final { return windowSize_; }
3131
Extent GetRenderSize() const override final { return renderSize_; }
3232
bool IsFullscreen() const override final { return isFullscreen_; }
33+
WindowMode GetWindowMode() const override final { return windowMode_; }
3334

3435
/// prüft auf Initialisierung.
3536
bool IsInitialized() const override final { return initialized; }
@@ -44,6 +45,7 @@ class VideoDriver : public IVideoDriver
4445
MouseCoords mouse_xy; /// Status der Maus.
4546
std::array<bool, 512> keyboard; /// Status der Tastatur;
4647
bool isFullscreen_; /// Vollbild an/aus?
48+
WindowMode windowMode_; /// Resizable/non-resizable window?
4749
private:
4850
// cached as possibly used often
4951
VideoMode windowSize_;

libs/driver/include/driver/VideoInterface.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
/// Function type for loading OpenGL methods
1515
using OpenGL_Loader_Proc = void* (*)(const char*);
1616

17+
enum class WindowMode
18+
{
19+
Resizable,
20+
NonResizable,
21+
};
22+
1723
class BOOST_SYMBOL_VISIBLE IVideoDriver
1824
{
1925
public:
@@ -25,9 +31,10 @@ class BOOST_SYMBOL_VISIBLE IVideoDriver
2531
virtual bool Initialize() = 0;
2632

2733
/// Erstellt das Fenster mit entsprechenden Werten.
28-
virtual bool CreateScreen(const std::string& title, const VideoMode& newSize, bool fullscreen) = 0;
34+
virtual bool CreateScreen(const std::string& title, const VideoMode& newSize, WindowMode windowMode,
35+
bool fullscreen) = 0;
2936

30-
virtual bool ResizeScreen(const VideoMode& newSize, bool fullscreen) = 0;
37+
virtual bool ResizeScreen(const VideoMode& newSize, WindowMode windowMode, bool fullscreen) = 0;
3138

3239
/// Schliesst das Fenster.
3340
virtual void DestroyScreen() = 0;
@@ -62,6 +69,7 @@ class BOOST_SYMBOL_VISIBLE IVideoDriver
6269
/// Get the size of the render region in pixels
6370
virtual Extent GetRenderSize() const = 0;
6471
virtual bool IsFullscreen() const = 0;
72+
virtual WindowMode GetWindowMode() const = 0;
6573

6674
/// Get state of the modifier keys
6775
virtual KeyEvent GetModKeyState() const = 0;

libs/s25main/GameManager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ bool GameManager::Start()
5050
// Fenster erstellen
5151
const auto screenSize =
5252
settings_.video.fullscreen ? settings_.video.fullscreenSize : settings_.video.windowedSize; //-V807
53-
if(!videoDriver_.CreateScreen(screenSize, settings_.video.fullscreen))
53+
if(!videoDriver_.CreateScreen(screenSize, settings_.video.windowMode, settings_.video.fullscreen))
5454
return false;
5555
videoDriver_.setTargetFramerate(settings_.video.framerate);
5656
videoDriver_.SetMouseWarping(settings_.global.smartCursor);

libs/s25main/Settings.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,12 @@ void Settings::LoadDefaults()
8585
video.fullscreenSize = VIDEODRIVER.GetWindowSize();
8686
video.windowedSize = VIDEODRIVER.IsFullscreen() ? VideoMode(800, 600) : video.fullscreenSize;
8787
video.fullscreen = VIDEODRIVER.IsFullscreen();
88+
video.windowMode = VIDEODRIVER.GetWindowMode();
8889
} else
8990
{
9091
video.windowedSize = video.fullscreenSize = VideoMode(800, 600);
9192
video.fullscreen = false;
93+
video.windowMode = WindowMode::Resizable;
9294
}
9395
video.framerate = 0; // Special value for HW vsync
9496
video.vbo = true;
@@ -224,6 +226,8 @@ void Settings::Load()
224226
video.fullscreenSize.width = iniVideo->getIntValue("fullscreen_width");
225227
video.fullscreenSize.height = iniVideo->getIntValue("fullscreen_height");
226228
video.fullscreen = iniVideo->getBoolValue("fullscreen");
229+
const auto resizable = iniVideo->getValue("resizable_window", true);
230+
video.windowMode = (resizable ? WindowMode::Resizable : WindowMode::NonResizable);
227231
video.framerate = iniVideo->getValue("framerate", 0);
228232
video.vbo = iniVideo->getBoolValue("vbo");
229233
video.shared_textures = iniVideo->getBoolValue("shared_textures");
@@ -402,6 +406,7 @@ void Settings::Save()
402406
iniVideo->setValue("windowed_width", video.windowedSize.width);
403407
iniVideo->setValue("windowed_height", video.windowedSize.height);
404408
iniVideo->setValue("fullscreen", video.fullscreen);
409+
iniVideo->setValue("resizable_window", video.windowMode == WindowMode::Resizable);
405410
iniVideo->setValue("framerate", video.framerate);
406411
iniVideo->setValue("vbo", video.vbo);
407412
iniVideo->setValue("shared_textures", video.shared_textures);

libs/s25main/Settings.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#pragma once
66

77
#include "DrawPoint.h"
8+
#include "driver/VideoInterface.h"
89
#include "driver/VideoMode.h"
910
#include "s25util/ProxySettings.h"
1011
#include "s25util/Singleton.h"
@@ -61,6 +62,7 @@ class Settings : public Singleton<Settings, SingletonPolicies::WithLongevity>
6162
VideoMode fullscreenSize, windowedSize;
6263
signed short framerate; // <0 for unlimited, 0 for HW Vsync
6364
bool fullscreen;
65+
WindowMode windowMode;
6466
bool vbo;
6567
bool shared_textures;
6668
} video;

libs/s25main/WindowManager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ void WindowManager::Msg_KeyDown(const KeyEvent& ke)
602602
// Switch Fullscreen/Windowed
603603
const auto newScreenSize =
604604
!SETTINGS.video.fullscreen ? SETTINGS.video.fullscreenSize : SETTINGS.video.windowedSize; //-V807
605-
VIDEODRIVER.ResizeScreen(newScreenSize, !SETTINGS.video.fullscreen);
605+
VIDEODRIVER.ResizeScreen(newScreenSize, SETTINGS.video.windowMode, !SETTINGS.video.fullscreen);
606606
SETTINGS.video.fullscreen = VIDEODRIVER.IsFullscreen();
607607
} else if(ke.kt == KeyType::Print)
608608
TakeScreenshot();

0 commit comments

Comments
 (0)