From 0a00f82e708d80414c7ce73322962cf0b47cec9a Mon Sep 17 00:00:00 2001 From: Daid Date: Tue, 22 Feb 2022 18:52:32 +0100 Subject: [PATCH] Allow for exclusive fullscreen as well. As some screen recordingsoftware works better with that. --- src/windowManager.cpp | 100 +++++++++++++++++++++++++++++------------- src/windowManager.h | 35 ++++++++------- 2 files changed, 90 insertions(+), 45 deletions(-) diff --git a/src/windowManager.cpp b/src/windowManager.cpp index 9780bd7b..d129e397 100644 --- a/src/windowManager.cpp +++ b/src/windowManager.cpp @@ -20,8 +20,8 @@ PVector Window::all_windows; void* Window::gl_context = nullptr; -Window::Window(glm::vec2 virtual_size, bool fullscreen, RenderChain* render_chain, int fsaa) -: minimal_virtual_size(virtual_size), current_virtual_size(virtual_size), render_chain(render_chain), fullscreen(fullscreen), fsaa(fsaa) +Window::Window(glm::vec2 virtual_size, Mode mode, RenderChain* render_chain, int fsaa) +: minimal_virtual_size(virtual_size), current_virtual_size(virtual_size), render_chain(render_chain), mode(mode), fsaa(fsaa) { srand(static_cast(time(nullptr))); @@ -71,12 +71,25 @@ void Window::render() SDL_GL_SwapWindow(static_cast(window)); } -void Window::setFullscreen(bool new_fullscreen) +void Window::setMode(Mode new_mode) { - if (fullscreen == new_fullscreen) + if (mode == new_mode) return; - fullscreen = new_fullscreen; - SDL_SetWindowFullscreen(static_cast(window), fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0); + mode = new_mode; + auto size = calculateWindowSize(); + SDL_SetWindowSize(static_cast(window), size.x, size.y); + switch(mode) + { + case Mode::Window: + SDL_SetWindowFullscreen(static_cast(window), 0); + break; + case Mode::Fullscreen: + SDL_SetWindowFullscreen(static_cast(window), SDL_WINDOW_FULLSCREEN_DESKTOP); + break; + case Mode::ExclusiveFullscreen: + SDL_SetWindowFullscreen(static_cast(window), SDL_WINDOW_FULLSCREEN); + break; + } setupView(); } @@ -124,27 +137,7 @@ void Window::create() } // Create the window of the application - auto windowWidth = static_cast(minimal_virtual_size.x); - auto windowHeight = static_cast(minimal_virtual_size.y); - - SDL_Rect rect; - if (SDL_GetDisplayBounds(display_nr, &rect)) - { - display_nr = 0; - SDL_GetDisplayBounds(display_nr, &rect); - } - - int scale = 2; - while(windowWidth * scale < int(rect.w) && windowHeight * scale < int(rect.h)) - scale += 1; - windowWidth *= scale - 1; - windowHeight *= scale - 1; - - while(windowWidth >= int(rect.w) || windowHeight >= int(rect.h) - 100) - { - windowWidth = static_cast(std::floor(windowWidth * 0.9f)); - windowHeight = static_cast(std::floor(windowHeight * 0.9f)); - } + auto size = calculateWindowSize(); #if defined(ANDROID) constexpr auto context_profile_mask = SDL_GL_CONTEXT_PROFILE_ES; @@ -166,10 +159,18 @@ void Window::create() SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI; - if (fullscreen) { + switch(mode) + { + case Mode::Window: + break; + case Mode::Fullscreen: flags |= SDL_WINDOW_FULLSCREEN_DESKTOP; + break; + case Mode::ExclusiveFullscreen: + flags |= SDL_WINDOW_FULLSCREEN; + break; } - window = SDL_CreateWindow("", SDL_WINDOWPOS_CENTERED_DISPLAY(display_nr), SDL_WINDOWPOS_CENTERED_DISPLAY(display_nr), windowWidth, windowHeight, flags); + window = SDL_CreateWindow("", SDL_WINDOWPOS_CENTERED_DISPLAY(display_nr), SDL_WINDOWPOS_CENTERED_DISPLAY(display_nr), size.x, size.y, flags); if (!gl_context) gl_context = SDL_GL_CreateContext(static_cast(window)); if (SDL_GL_SetSwapInterval(-1)) @@ -313,7 +314,7 @@ void Window::handleEvent(const SDL_Event& event) case SDLK_KP_ENTER: case SDLK_RETURN: if (event.key.keysym.mod & KMOD_ALT) - setFullscreen(!isFullscreen()); + setMode(getMode() == Mode::Window ? Mode::Fullscreen : Mode::Window); else render_chain->onTextInput(sp::TextInputEvent::Return); break; @@ -382,3 +383,42 @@ void Window::setupView() current_virtual_size.y = current_virtual_size.x / window_size.x * window_size.y; } } + +glm::ivec2 Window::calculateWindowSize() const +{ + int display_nr = 0; + for(auto w : all_windows) + { + if (w == this) + break; + display_nr ++; + } + + // Create the window of the application + auto windowWidth = static_cast(minimal_virtual_size.x); + auto windowHeight = static_cast(minimal_virtual_size.y); + + SDL_Rect rect{0,0,0,0}; + if (SDL_GetDisplayBounds(display_nr, &rect)) + { + display_nr = 0; + SDL_GetDisplayBounds(display_nr, &rect); + } + if (mode != Mode::Window && rect.w && rect.h) + { + return {rect.w, rect.h}; + } + + int scale = 2; + while(windowWidth * scale < int(rect.w) && windowHeight * scale < int(rect.h)) + scale += 1; + windowWidth *= scale - 1; + windowHeight *= scale - 1; + + while(windowWidth >= int(rect.w) || windowHeight >= int(rect.h) - 100) + { + windowWidth = static_cast(std::floor(windowWidth * 0.9f)); + windowHeight = static_cast(std::floor(windowHeight * 0.9f)); + } + return {windowWidth, windowHeight}; +} diff --git a/src/windowManager.h b/src/windowManager.h index e7f9e2f1..e0c69dfa 100644 --- a/src/windowManager.h +++ b/src/windowManager.h @@ -7,27 +7,20 @@ union SDL_Event; class Window : public virtual PObject { -private: - static PVector all_windows; - static void* gl_context; - - glm::vec2 minimal_virtual_size; - glm::vec2 current_virtual_size; - void* window = nullptr; - RenderChain* render_chain; - int mouse_button_down_mask = 0; - bool fullscreen; - int fsaa; - public: - Window(glm::vec2 virtual_size, bool fullscreen, RenderChain* chain, int fsaa = 0); + enum class Mode { + Window, + Fullscreen, + ExclusiveFullscreen + }; + Window(glm::vec2 virtual_size, Mode mode, RenderChain* chain, int fsaa = 0); virtual ~Window(); glm::vec2 getVirtualSize() const { return current_virtual_size; } void render(); - bool isFullscreen() { return fullscreen; } - void setFullscreen(bool fullscreen); + Mode getMode() { return mode; } + void setMode(Mode mode); int getFSAA() { return fsaa; } void setFSAA(int fsaa); @@ -39,9 +32,21 @@ class Window : public virtual PObject friend class InputHandler; friend class Engine; private: + static PVector all_windows; + static void* gl_context; + + glm::vec2 minimal_virtual_size; + glm::vec2 current_virtual_size; + void* window = nullptr; + RenderChain* render_chain; + int mouse_button_down_mask = 0; + Mode mode; + int fsaa; + void handleEvent(const SDL_Event& event); void create(); void setupView(); + glm::ivec2 calculateWindowSize() const; }; #endif//WINDOW_MANAGER_H