20 #include "sdl_window.hpp"
21 #include "sdl_utils.hpp"
24 Sint32 height, Uint32 flags)
25 : _window(SDL_CreateWindow(title.c_str(), startupX, startupY, width, height, flags))
30 : _window(other._window), _offset_x(other._offset_x), _offset_y(other._offset_y)
32 other._window =
nullptr;
35 SdlWindow::~SdlWindow()
37 SDL_DestroyWindow(_window);
40 Uint32 SdlWindow::id()
const
44 return SDL_GetWindowID(_window);
47 int SdlWindow::displayIndex()
const
51 return SDL_GetWindowDisplayIndex(_window);
54 SDL_Rect SdlWindow::rect()
const
59 SDL_GetWindowPosition(_window, &rect.x, &rect.y);
60 SDL_GetWindowSize(_window, &rect.w, &rect.h);
65 SDL_Window* SdlWindow::window()
const
70 Sint32 SdlWindow::offsetX()
const
75 void SdlWindow::setOffsetX(Sint32 x)
80 void SdlWindow::setOffsetY(Sint32 y)
85 Sint32 SdlWindow::offsetY()
const
90 bool SdlWindow::grabKeyboard(
bool enable)
94 #if SDL_VERSION_ATLEAST(2, 0, 16)
95 SDL_SetWindowKeyboardGrab(_window, enable ? SDL_TRUE : SDL_FALSE);
98 SDL_LogError(SDL_LOG_CATEGORY_INPUT,
"Keyboard grabbing not supported by SDL2 < 2.0.16");
103 bool SdlWindow::grabMouse(
bool enable)
107 #if SDL_VERSION_ATLEAST(2, 0, 16)
108 SDL_SetWindowMouseGrab(_window, enable ? SDL_TRUE : SDL_FALSE);
110 SDL_SetWindowGrab(_window, enable ? SDL_TRUE : SDL_FALSE);
115 void SdlWindow::setBordered(
bool bordered)
118 SDL_SetWindowBordered(_window, bordered ? SDL_TRUE : SDL_FALSE);
121 void SdlWindow::raise()
123 SDL_RaiseWindow(_window);
126 void SdlWindow::resizeable(
bool use)
128 SDL_SetWindowResizable(_window, use ? SDL_TRUE : SDL_FALSE);
131 void SdlWindow::fullscreen(
bool enter)
133 auto curFlags = SDL_GetWindowFlags(_window);
137 if (!(curFlags & SDL_WINDOW_BORDERLESS))
139 auto idx = SDL_GetWindowDisplayIndex(_window);
140 SDL_DisplayMode mode = {};
141 SDL_GetCurrentDisplayMode(idx, &mode);
143 SDL_RestoreWindow(_window);
145 SDL_SetWindowBordered(_window, SDL_FALSE);
146 SDL_SetWindowPosition(_window, 0, 0);
147 #if SDL_VERSION_ATLEAST(2, 0, 16)
148 SDL_SetWindowAlwaysOnTop(_window, SDL_TRUE);
150 SDL_RaiseWindow(_window);
151 SDL_SetWindowSize(_window, mode.w, mode.h);
156 if (curFlags & SDL_WINDOW_BORDERLESS)
159 SDL_SetWindowBordered(_window, SDL_TRUE);
160 #if SDL_VERSION_ATLEAST(2, 0, 16)
161 SDL_SetWindowAlwaysOnTop(_window, SDL_FALSE);
163 SDL_RaiseWindow(_window);
164 SDL_MinimizeWindow(_window);
165 SDL_MaximizeWindow(_window);
170 void SdlWindow::minimize()
172 SDL_MinimizeWindow(_window);
175 bool SdlWindow::fill(Uint8 r, Uint8 g, Uint8 b, Uint8 a)
177 auto surface = SDL_GetWindowSurface(_window);
180 SDL_Rect rect = { 0, 0, surface->w, surface->h };
181 auto color = SDL_MapRGBA(surface->format, r, g, b, a);
183 SDL_FillRect(surface, &rect, color);
187 bool SdlWindow::blit(SDL_Surface* surface, SDL_Rect srcRect, SDL_Rect& dstRect)
189 auto screen = SDL_GetWindowSurface(_window);
190 if (!screen || !surface)
192 if (!SDL_SetClipRect(surface, &srcRect))
194 if (!SDL_SetClipRect(screen, &dstRect))
196 auto rc = SDL_BlitScaled(surface, &srcRect, screen, &dstRect);
199 SDL_LogError(SDL_LOG_CATEGORY_RENDER,
"SDL_BlitScaled: %s [%d]", sdl_error_string(rc), rc);
204 void SdlWindow::updateSurface()
206 SDL_UpdateWindowSurface(_window);
SdlWindow(const std::string &title, Sint32 startupX, Sint32 startupY, Sint32 width, Sint32 height, Uint32 flags)