More window state logging in SDL2 backend

This commit is contained in:
Nikita Lisitsa 2023-12-03 18:28:54 +03:00
parent a8c89f84bd
commit 8038da6987

View file

@ -1,6 +1,8 @@
#include <psemek/sdl2/window.hpp>
#include <psemek/sdl2/init.hpp>
#include <psemek/gfx/gl.hpp>
#include <psemek/geom/box.hpp>
#include <psemek/log/log.hpp>
namespace psemek::sdl2
{
@ -64,12 +66,14 @@ namespace psemek::sdl2
void window::show_cursor(bool show)
{
log::info() << (show ? "Cursor shown" : "Cursor hidden");
SDL_ShowCursor(show ? SDL_TRUE : SDL_FALSE);
SDL_SetRelativeMouseMode(show ? SDL_FALSE : SDL_TRUE);
}
void window::vsync(bool on)
{
log::info() << "Turning VSync " << (on ? "on" : "off");
if (on)
{
// try adaptive vsync
@ -87,22 +91,36 @@ namespace psemek::sdl2
void window::windowed(bool on)
{
SDL_DisplayMode display_mode;
SDL_GetCurrentDisplayMode(0, &display_mode);
SDL_Rect display_usable_bounds;
SDL_GetDisplayUsableBounds(0, &display_usable_bounds);
log::info() << "Entering " << (on ? "windowed" : "fullscreen") << " mode";
SDL_SetWindowFullscreen(window_, on ? 0 : SDL_WINDOW_FULLSCREEN_DESKTOP);
SDL_SetWindowBordered(window_, on ? SDL_TRUE : SDL_FALSE);
SDL_RestoreWindow(window_);
if (on)
{
SDL_Rect display_usable_bounds;
SDL_GetDisplayUsableBounds(0, &display_usable_bounds);
int top_border, left_border, bottom_border, right_border;
SDL_GetWindowBordersSize(window_, &top_border, &left_border, &bottom_border, &right_border);
SDL_SetWindowSize(window_, display_usable_bounds.w - left_border - right_border, display_usable_bounds.h - top_border - bottom_border);
SDL_SetWindowPosition(window_, left_border, top_border);
geom::box<int, 2> bounds_rect;
bounds_rect[0] = {display_usable_bounds.x, display_usable_bounds.x + display_usable_bounds.w};
bounds_rect[1] = {display_usable_bounds.y, display_usable_bounds.y + display_usable_bounds.h};
log::info() << "Display usable bounds: " << bounds_rect;
log::info() << "Window borders: left " << left_border << ", right " << right_border << ", top " << top_border << ", bottom " << bottom_border;
geom::box<int, 2> window_rect = bounds_rect;
window_rect[0].min += left_border;
window_rect[0].max -= right_border;
window_rect[1].min += top_border;
window_rect[1].max -= bottom_border;
log::info() << "Computed window rect: " << window_rect;
SDL_SetWindowSize(window_, window_rect.dimensions()[0], window_rect.dimensions()[1]);
SDL_SetWindowPosition(window_, window_rect[0].min, window_rect[1].min);
}
}