use SDL's desktop fullscreen flag for simpler fullscreen behavior, and use Emscripten's fullscreen functionality for web builds; add a flag for disabling fullscreen through the configuration

This commit is contained in:
ohsqueezy 2023-12-05 17:56:56 -05:00
parent 5235535cc2
commit fb68938889
3 changed files with 44 additions and 33 deletions

View File

@ -51,7 +51,8 @@ void Configuration::set_defaults()
{"default font path", "BPmono.ttf"}, {"default font path", "BPmono.ttf"},
{"default font size", 16}, {"default font size", 16},
{"use play button", false}, {"use play button", false},
{"fullscreen", false} {"fullscreen", false},
{"fullscreen enabled", true}
}; };
config["audio"] = { config["audio"] = {
{"default-sfx-root", "resource/sfx"}, {"default-sfx-root", "resource/sfx"},

View File

@ -186,38 +186,39 @@ void sb::Display::respond(SDL_Event& event)
void sb::Display::toggle_fullscreen() const void sb::Display::toggle_fullscreen() const
{ {
if (SDL_GetWindowFlags(const_cast<SDL_Window*>(window())) & SDL_WINDOW_FULLSCREEN) if (configuration()("display", "fullscreen enabled"))
{ {
sb::Log::log("exit fullscreen requested"); #if !defined(__EMSCRIPTEN__)
SDL_SetWindowFullscreen(const_cast<SDL_Window*>(window()), 0); if (SDL_GetWindowFlags(const_cast<SDL_Window*>(window())) & SDL_WINDOW_FULLSCREEN_DESKTOP)
}
else
{
/* Get the index of the display currently displaying the game window */
int display_index;
if ((display_index = SDL_GetWindowDisplayIndex(const_cast<SDL_Window*>(window()))) < 0)
{ {
display_index = 0; sb::Log::log("Fullscreen requested");
sb::Log::sdl_error("Error getting current display index of window, defaulting to index 0."); SDL_SetWindowFullscreen(const_cast<SDL_Window*>(window()), 0);
}
/* Get the display mode (including the resolution) of the display currently displaying the window */
SDL_DisplayMode mode;
if (SDL_GetDesktopDisplayMode(display_index, &mode) != 0)
{
sb::Log::sdl_error("Error getting display mode");
} }
else else
{ {
/* Set the window's fullscreen display mode to the same as the desktop to use the same resolution */ sb::Log::log("Exit fullscreen requested");
sb::Log::log("Setting window's fullscreen display mode to current desktop display mode"); SDL_SetWindowFullscreen(const_cast<SDL_Window*>(window()), SDL_WINDOW_FULLSCREEN_DESKTOP);
if (SDL_SetWindowDisplayMode(const_cast<SDL_Window*>(window()), &mode) != 0)
{
sb::Log::sdl_error("Error setting window's fullscreen display mode");
}
} }
#else
EmscriptenFullscreenChangeEvent status;
emscripten_get_fullscreen_status(&status);
if (!status.isFullscreen)
{
sb::Log::log("Fullscreen requested");
sb::Log::log("fullscreen requested"); /* Set a string to refer to Module.canvas. See https://emscripten.org/docs/api_reference/html5.h.html#registration-functions */
SDL_SetWindowFullscreen(const_cast<SDL_Window*>(window()), SDL_WINDOW_FULLSCREEN); EM_ASM(specialHTMLTargets["!canvas"] = Module.canvas;);
emscripten_request_fullscreen("!canvas", true);
}
else
{
sb::Log::log("Exit fullscreen requested");
emscripten_exit_fullscreen();
}
#endif
}
else
{
sb::Log::log("Fullscreen requested, but it is currently disabled by the configuration.", sb::Log::WARN);
} }
} }

View File

@ -11,11 +11,19 @@
#pragma once #pragma once
#include <sstream> #include <sstream>
#include "glm/vec2.hpp" #include "glm/vec2.hpp"
#include "SDL.h" #include "SDL.h"
#include "SDL_image.h" #include "SDL_image.h"
#include "sdl2-gfx/SDL2_gfxPrimitives.h" #include "sdl2-gfx/SDL2_gfxPrimitives.h"
#include "sdl2-gfx/SDL2_rotozoom.h" #include "sdl2-gfx/SDL2_rotozoom.h"
#if defined(__EMSCRIPTEN__)
#include "emscripten/html5.h"
#endif
#include "Node.hpp" #include "Node.hpp"
#include "Box.hpp" #include "Box.hpp"
#include "Log.hpp" #include "Log.hpp"
@ -52,13 +60,14 @@ namespace sb
void respond(SDL_Event& event); void respond(SDL_Event& event);
/*! /*!
* Query SDL window flags to determine if fullscreen is on or off and use SDL's fullscreen function to toggle the fullscreen * Set window to fullscreen if it is not currently displayed fullscreen, otherwise exit fullscreen.
* state to the opposite state.
* *
* Before calling SDL's fullscreen function, this function queries the display index to find out which display (for example, * In builds other than Emscripten, this uses the SDL_WINDOW_FULLSCREEN_DESKTOP flag to create a fullscreen window. SDL creates
* which monitor in a dual-screen system) is displaying the window. It then sets the window's fullscreen display mode to the * the fullscreen window without changing video modes by resizing the window to the size of desktop and removing borders.
* current desktop display mode of that display. This ensures that the fullscreen will succeed smoothly, without a resolution *
* change, but it also requires that the game is adaptable to dynamic window sizing. * Emscripten builds use `emscripten_request_fullscreen()` instead, which handles fullscreen from web browsers more smoothly.
*
* If "display" > "fullscreen enabled" is false in the configuration, this function does nothing.
*/ */
void toggle_fullscreen() const; void toggle_fullscreen() const;