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 (void)SDL_SyncWindow(_window);
54 : _window(other._window), _offset_x(other._offset_x), _offset_y(other._offset_y)
56 other._window =
nullptr;
59SdlWindow::~SdlWindow()
61 SDL_DestroyWindow(_window);
64Uint32 SdlWindow::id()
const
68 return SDL_GetWindowID(_window);
71SDL_DisplayID SdlWindow::displayIndex()
const
75 return SDL_GetDisplayForWindow(_window);
78SDL_Rect SdlWindow::rect()
const
83 SDL_GetWindowPosition(_window, &rect.x, &rect.y);
84 SDL_GetWindowSizeInPixels(_window, &rect.w, &rect.h);
89SDL_Window* SdlWindow::window()
const
94Sint32 SdlWindow::offsetX()
const
99void SdlWindow::setOffsetX(Sint32 x)
104void SdlWindow::setOffsetY(Sint32 y)
109Sint32 SdlWindow::offsetY()
const
118 const auto factor = SDL_GetWindowDisplayScale(_window);
119 const auto dsf =
static_cast<UINT32
>(100 * factor);
120 mon.attributes.desktopScaleFactor = dsf;
121 mon.attributes.deviceScaleFactor = 100;
125 auto prc = SDL_GetWindowSizeInPixels(_window, &pixelWidth, &pixelHeight);
129 mon.width = pixelWidth;
130 mon.height = pixelHeight;
132 mon.attributes.physicalWidth = WINPR_ASSERTING_INT_CAST(uint32_t, pixelWidth);
133 mon.attributes.physicalHeight = WINPR_ASSERTING_INT_CAST(uint32_t, pixelHeight);
137 auto did = SDL_GetDisplayForWindow(_window);
138 auto rc = SDL_GetDisplayBounds(did, &rect);
146 auto orientation = SDL_GetCurrentDisplayOrientation(did);
147 mon.attributes.orientation = sdl::utils::orientaion_to_rdp(orientation);
149 auto primary = SDL_GetPrimaryDisplay();
150 mon.is_primary = SDL_GetWindowID(_window) == primary;
151 mon.orig_screen = did;
155bool SdlWindow::grabKeyboard(
bool enable)
159 SDL_SetWindowKeyboardGrab(_window, enable);
163bool SdlWindow::grabMouse(
bool enable)
167 SDL_SetWindowMouseGrab(_window, enable);
171void SdlWindow::setBordered(
bool bordered)
174 SDL_SetWindowBordered(_window, bordered);
175 (void)SDL_SyncWindow(_window);
178void SdlWindow::raise()
180 SDL_RaiseWindow(_window);
181 (void)SDL_SyncWindow(_window);
184void SdlWindow::resizeable(
bool use)
186 SDL_SetWindowResizable(_window, use);
187 (void)SDL_SyncWindow(_window);
190void SdlWindow::fullscreen(
bool enter)
192 (void)SDL_SetWindowFullscreen(_window, enter);
193 (void)SDL_SyncWindow(_window);
196void SdlWindow::minimize()
198 SDL_MinimizeWindow(_window);
199 (void)SDL_SyncWindow(_window);
202bool SdlWindow::fill(Uint8 r, Uint8 g, Uint8 b, Uint8 a)
204 auto surface = SDL_GetWindowSurface(_window);
207 SDL_Rect rect = { 0, 0, surface->w, surface->h };
208 auto color = SDL_MapSurfaceRGBA(surface, r, g, b, a);
210 SDL_FillSurfaceRect(surface, &rect, color);
214bool SdlWindow::blit(SDL_Surface* surface,
const SDL_Rect& srcRect, SDL_Rect& dstRect)
216 auto screen = SDL_GetWindowSurface(_window);
217 if (!screen || !surface)
219 if (!SDL_SetSurfaceClipRect(surface, &srcRect))
221 if (!SDL_SetSurfaceClipRect(screen, &dstRect))
223 if (!SDL_BlitSurfaceScaled(surface, &srcRect, screen, &dstRect, SDL_SCALEMODE_LINEAR))
225 SDL_LogError(SDL_LOG_CATEGORY_RENDER,
"SDL_BlitScaled: %s", SDL_GetError());
231void SdlWindow::updateSurface()
233 SDL_UpdateWindowSurface(_window);
SdlWindow(const std::string &title, Sint32 startupX, Sint32 startupY, Sint32 width, Sint32 height, Uint32 flags)