Skip to content

Commit

Permalink
Allow for exclusive fullscreen as well. As some screen recordingsoftw…
Browse files Browse the repository at this point in the history
…are works better with that.
  • Loading branch information
daid committed Feb 22, 2022
1 parent d2aa22a commit 0a00f82
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 45 deletions.
100 changes: 70 additions & 30 deletions src/windowManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
PVector<Window> 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<int32_t>(time(nullptr)));

Expand Down Expand Up @@ -71,12 +71,25 @@ void Window::render()
SDL_GL_SwapWindow(static_cast<SDL_Window*>(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<SDL_Window*>(window), fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
mode = new_mode;
auto size = calculateWindowSize();
SDL_SetWindowSize(static_cast<SDL_Window*>(window), size.x, size.y);
switch(mode)
{
case Mode::Window:
SDL_SetWindowFullscreen(static_cast<SDL_Window*>(window), 0);
break;
case Mode::Fullscreen:
SDL_SetWindowFullscreen(static_cast<SDL_Window*>(window), SDL_WINDOW_FULLSCREEN_DESKTOP);
break;
case Mode::ExclusiveFullscreen:
SDL_SetWindowFullscreen(static_cast<SDL_Window*>(window), SDL_WINDOW_FULLSCREEN);
break;
}
setupView();
}

Expand Down Expand Up @@ -124,27 +137,7 @@ void Window::create()
}

// Create the window of the application
auto windowWidth = static_cast<int>(minimal_virtual_size.x);
auto windowHeight = static_cast<int>(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<int>(std::floor(windowWidth * 0.9f));
windowHeight = static_cast<int>(std::floor(windowHeight * 0.9f));
}
auto size = calculateWindowSize();

#if defined(ANDROID)
constexpr auto context_profile_mask = SDL_GL_CONTEXT_PROFILE_ES;
Expand All @@ -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<SDL_Window*>(window));
if (SDL_GL_SetSwapInterval(-1))
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<int>(minimal_virtual_size.x);
auto windowHeight = static_cast<int>(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<int>(std::floor(windowWidth * 0.9f));
windowHeight = static_cast<int>(std::floor(windowHeight * 0.9f));
}
return {windowWidth, windowHeight};
}
35 changes: 20 additions & 15 deletions src/windowManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,20 @@
union SDL_Event;
class Window : public virtual PObject
{
private:
static PVector<Window> 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);

Expand All @@ -39,9 +32,21 @@ class Window : public virtual PObject
friend class InputHandler;
friend class Engine;
private:
static PVector<Window> 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

0 comments on commit 0a00f82

Please sign in to comment.