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:
parent
5235535cc2
commit
fb68938889
|
@ -51,7 +51,8 @@ void Configuration::set_defaults()
|
|||
{"default font path", "BPmono.ttf"},
|
||||
{"default font size", 16},
|
||||
{"use play button", false},
|
||||
{"fullscreen", false}
|
||||
{"fullscreen", false},
|
||||
{"fullscreen enabled", true}
|
||||
};
|
||||
config["audio"] = {
|
||||
{"default-sfx-root", "resource/sfx"},
|
||||
|
|
|
@ -186,38 +186,39 @@ void sb::Display::respond(SDL_Event& event)
|
|||
|
||||
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");
|
||||
SDL_SetWindowFullscreen(const_cast<SDL_Window*>(window()), 0);
|
||||
}
|
||||
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)
|
||||
#if !defined(__EMSCRIPTEN__)
|
||||
if (SDL_GetWindowFlags(const_cast<SDL_Window*>(window())) & SDL_WINDOW_FULLSCREEN_DESKTOP)
|
||||
{
|
||||
display_index = 0;
|
||||
sb::Log::sdl_error("Error getting current display index of window, defaulting to index 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");
|
||||
sb::Log::log("Fullscreen requested");
|
||||
SDL_SetWindowFullscreen(const_cast<SDL_Window*>(window()), 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Set the window's fullscreen display mode to the same as the desktop to use the same resolution */
|
||||
sb::Log::log("Setting window's fullscreen display mode to current desktop display mode");
|
||||
if (SDL_SetWindowDisplayMode(const_cast<SDL_Window*>(window()), &mode) != 0)
|
||||
{
|
||||
sb::Log::sdl_error("Error setting window's fullscreen display mode");
|
||||
}
|
||||
sb::Log::log("Exit fullscreen requested");
|
||||
SDL_SetWindowFullscreen(const_cast<SDL_Window*>(window()), SDL_WINDOW_FULLSCREEN_DESKTOP);
|
||||
}
|
||||
#else
|
||||
EmscriptenFullscreenChangeEvent status;
|
||||
emscripten_get_fullscreen_status(&status);
|
||||
if (!status.isFullscreen)
|
||||
{
|
||||
sb::Log::log("Fullscreen requested");
|
||||
|
||||
sb::Log::log("fullscreen requested");
|
||||
SDL_SetWindowFullscreen(const_cast<SDL_Window*>(window()), SDL_WINDOW_FULLSCREEN);
|
||||
/* Set a string to refer to Module.canvas. See https://emscripten.org/docs/api_reference/html5.h.html#registration-functions */
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,11 +11,19 @@
|
|||
#pragma once
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include "glm/vec2.hpp"
|
||||
|
||||
#include "SDL.h"
|
||||
#include "SDL_image.h"
|
||||
|
||||
#include "sdl2-gfx/SDL2_gfxPrimitives.h"
|
||||
#include "sdl2-gfx/SDL2_rotozoom.h"
|
||||
|
||||
#if defined(__EMSCRIPTEN__)
|
||||
#include "emscripten/html5.h"
|
||||
#endif
|
||||
|
||||
#include "Node.hpp"
|
||||
#include "Box.hpp"
|
||||
#include "Log.hpp"
|
||||
|
@ -52,13 +60,14 @@ namespace sb
|
|||
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
|
||||
* state to the opposite state.
|
||||
* Set window to fullscreen if it is not currently displayed fullscreen, otherwise exit fullscreen.
|
||||
*
|
||||
* Before calling SDL's fullscreen function, this function queries the display index to find out which display (for example,
|
||||
* which monitor in a dual-screen system) is displaying the window. It then sets the window's fullscreen display mode to the
|
||||
* 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.
|
||||
* In builds other than Emscripten, this uses the SDL_WINDOW_FULLSCREEN_DESKTOP flag to create a fullscreen window. SDL creates
|
||||
* the fullscreen window without changing video modes by resizing the window to the size of desktop and removing borders.
|
||||
*
|
||||
* 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;
|
||||
|
||||
|
|
Loading…
Reference in New Issue