20#include "sdl_window.hpp"
21#include "sdl_utils.hpp"
24 Sint32 height, [[maybe_unused]] Uint32 flags)
26 auto props = SDL_CreateProperties();
27 SDL_SetStringProperty(props, SDL_PROP_WINDOW_CREATE_TITLE_STRING, title.c_str());
28 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_X_NUMBER, startupX);
29 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_Y_NUMBER, startupY);
30 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, width);
31 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, height);
33 if (flags & SDL_WINDOW_HIGH_PIXEL_DENSITY)
34 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIGH_PIXEL_DENSITY_BOOLEAN,
true);
36 if (flags & SDL_WINDOW_FULLSCREEN)
37 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_FULLSCREEN_BOOLEAN,
true);
39 if (flags & SDL_WINDOW_BORDERLESS)
40 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_BORDERLESS_BOOLEAN,
true);
42 _window = SDL_CreateWindowWithProperties(props);
43 SDL_DestroyProperties(props);
45 auto scale = SDL_GetWindowPixelDensity(_window);
46 const int iscale =
static_cast<int>(scale * 100.0f);
47 auto w = 100 * width / iscale;
48 auto h = 100 * height / iscale;
49 (void)SDL_SetWindowSize(_window, w, h);
50 SDL_SetHint(SDL_HINT_APP_NAME,
"");
51 (void)SDL_SyncWindow(_window);
55 : _window(other._window), _offset_x(other._offset_x), _offset_y(other._offset_y)
57 other._window =
nullptr;
60SdlWindow::~SdlWindow()
62 SDL_DestroyWindow(_window);
65Uint32 SdlWindow::id()
const
69 return SDL_GetWindowID(_window);
72SDL_DisplayID SdlWindow::displayIndex()
const
76 return SDL_GetDisplayForWindow(_window);
79SDL_Rect SdlWindow::rect()
const
84 SDL_GetWindowPosition(_window, &rect.x, &rect.y);
85 SDL_GetWindowSizeInPixels(_window, &rect.w, &rect.h);
90SDL_Window* SdlWindow::window()
const
95Sint32 SdlWindow::offsetX()
const
100void SdlWindow::setOffsetX(Sint32 x)
105void SdlWindow::setOffsetY(Sint32 y)
110Sint32 SdlWindow::offsetY()
const
119 const auto factor = SDL_GetWindowDisplayScale(_window);
120 const auto dsf =
static_cast<UINT32
>(100 * factor);
121 mon.attributes.desktopScaleFactor = dsf;
122 mon.attributes.deviceScaleFactor = 100;
126 auto prc = SDL_GetWindowSizeInPixels(_window, &pixelWidth, &pixelHeight);
130 mon.width = pixelWidth;
131 mon.height = pixelHeight;
133 mon.attributes.physicalWidth = WINPR_ASSERTING_INT_CAST(uint32_t, pixelWidth);
134 mon.attributes.physicalHeight = WINPR_ASSERTING_INT_CAST(uint32_t, pixelHeight);
138 auto did = SDL_GetDisplayForWindow(_window);
139 auto rc = SDL_GetDisplayBounds(did, &rect);
147 auto orientation = SDL_GetCurrentDisplayOrientation(did);
148 mon.attributes.orientation = sdl::utils::orientaion_to_rdp(orientation);
150 auto primary = SDL_GetPrimaryDisplay();
151 mon.is_primary = SDL_GetWindowID(_window) == primary;
152 mon.orig_screen = did;
156bool SdlWindow::grabKeyboard(
bool enable)
160 SDL_SetWindowKeyboardGrab(_window, enable);
164bool SdlWindow::grabMouse(
bool enable)
168 SDL_SetWindowMouseGrab(_window, enable);
172void SdlWindow::setBordered(
bool bordered)
175 SDL_SetWindowBordered(_window, bordered);
176 (void)SDL_SyncWindow(_window);
179void SdlWindow::raise()
181 SDL_RaiseWindow(_window);
182 (void)SDL_SyncWindow(_window);
185void SdlWindow::resizeable(
bool use)
187 SDL_SetWindowResizable(_window, use);
188 (void)SDL_SyncWindow(_window);
191void SdlWindow::fullscreen(
bool enter)
193 (void)SDL_SetWindowFullscreen(_window, enter);
194 (void)SDL_SyncWindow(_window);
197void SdlWindow::minimize()
199 SDL_MinimizeWindow(_window);
200 (void)SDL_SyncWindow(_window);
203bool SdlWindow::fill(Uint8 r, Uint8 g, Uint8 b, Uint8 a)
205 auto surface = SDL_GetWindowSurface(_window);
208 SDL_Rect rect = { 0, 0, surface->w, surface->h };
209 auto color = SDL_MapSurfaceRGBA(surface, r, g, b, a);
211 SDL_FillSurfaceRect(surface, &rect, color);
215bool SdlWindow::blit(SDL_Surface* surface,
const SDL_Rect& srcRect, SDL_Rect& dstRect)
217 auto screen = SDL_GetWindowSurface(_window);
218 if (!screen || !surface)
220 if (!SDL_SetSurfaceClipRect(surface, &srcRect))
222 if (!SDL_SetSurfaceClipRect(screen, &dstRect))
224 if (!SDL_BlitSurfaceScaled(surface, &srcRect, screen, &dstRect, SDL_SCALEMODE_LINEAR))
226 SDL_LogError(SDL_LOG_CATEGORY_RENDER,
"SDL_BlitScaled: %s", SDL_GetError());
232void SdlWindow::updateSurface()
234 SDL_UpdateWindowSurface(_window);
SdlWindow(const std::string &title, Sint32 startupX, Sint32 startupY, Sint32 width, Sint32 height, Uint32 flags)