20 #include "sdl_window.hpp"
21 #include "sdl_utils.hpp"
24 Sint32 height, 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 _window = SDL_CreateWindowWithProperties(props);
34 SDL_DestroyProperties(props);
38 : _window(other._window), _offset_x(other._offset_x), _offset_y(other._offset_y)
40 other._window =
nullptr;
43 SdlWindow::~SdlWindow()
45 SDL_DestroyWindow(_window);
48 Uint32 SdlWindow::id()
const
52 return SDL_GetWindowID(_window);
55 int SdlWindow::displayIndex()
const
59 return SDL_GetDisplayForWindow(_window);
62 SDL_Rect SdlWindow::rect()
const
67 SDL_GetWindowPosition(_window, &rect.x, &rect.y);
68 SDL_GetWindowSize(_window, &rect.w, &rect.h);
73 SDL_Window* SdlWindow::window()
const
78 Sint32 SdlWindow::offsetX()
const
83 void SdlWindow::setOffsetX(Sint32 x)
88 void SdlWindow::setOffsetY(Sint32 y)
93 Sint32 SdlWindow::offsetY()
const
98 bool SdlWindow::grabKeyboard(
bool enable)
102 SDL_SetWindowKeyboardGrab(_window, enable);
106 bool SdlWindow::grabMouse(
bool enable)
110 SDL_SetWindowMouseGrab(_window, enable);
114 void SdlWindow::setBordered(
bool bordered)
117 SDL_SetWindowBordered(_window, bordered);
120 void SdlWindow::raise()
122 SDL_RaiseWindow(_window);
125 void SdlWindow::resizeable(
bool use)
127 SDL_SetWindowResizable(_window, use);
130 void SdlWindow::fullscreen(
bool enter)
132 auto curFlags = SDL_GetWindowFlags(_window);
136 if (!(curFlags & SDL_WINDOW_BORDERLESS))
138 auto idx = SDL_GetDisplayForWindow(_window);
139 auto mode = SDL_GetCurrentDisplayMode(idx);
141 SDL_RestoreWindow(_window);
143 SDL_SetWindowBordered(_window,
false);
144 SDL_SetWindowPosition(_window, 0, 0);
145 SDL_SetWindowAlwaysOnTop(_window,
true);
146 SDL_RaiseWindow(_window);
148 SDL_SetWindowSize(_window, mode->w, mode->h);
153 if (curFlags & SDL_WINDOW_BORDERLESS)
156 SDL_SetWindowBordered(_window,
true);
157 SDL_SetWindowAlwaysOnTop(_window,
false);
158 SDL_RaiseWindow(_window);
159 SDL_MinimizeWindow(_window);
160 SDL_MaximizeWindow(_window);
165 void SdlWindow::minimize()
167 SDL_MinimizeWindow(_window);
170 bool SdlWindow::fill(Uint8 r, Uint8 g, Uint8 b, Uint8 a)
172 auto surface = SDL_GetWindowSurface(_window);
175 SDL_Rect rect = { 0, 0, surface->w, surface->h };
176 auto color = SDL_MapSurfaceRGBA(surface, r, g, b, a);
178 SDL_FillSurfaceRect(surface, &rect, color);
182 bool SdlWindow::blit(SDL_Surface* surface,
const SDL_Rect& srcRect, SDL_Rect& dstRect)
184 auto screen = SDL_GetWindowSurface(_window);
185 if (!screen || !surface)
187 if (!SDL_SetSurfaceClipRect(surface, &srcRect))
189 if (!SDL_SetSurfaceClipRect(screen, &dstRect))
191 if (!SDL_BlitSurfaceScaled(surface, &srcRect, screen, &dstRect, SDL_SCALEMODE_LINEAR))
193 SDL_LogError(SDL_LOG_CATEGORY_RENDER,
"SDL_BlitScaled: %s", SDL_GetError());
199 void SdlWindow::updateSurface()
201 SDL_UpdateWindowSurface(_window);
SdlWindow(const std::string &title, Sint32 startupX, Sint32 startupY, Sint32 width, Sint32 height, Uint32 flags)