FreeRDP
Loading...
Searching...
No Matches
SDL3/sdl_window.cpp
1
20#include "sdl_window.hpp"
21#include "sdl_utils.hpp"
22
23SdlWindow::SdlWindow(const std::string& title, Sint32 startupX, Sint32 startupY, Sint32 width,
24 Sint32 height, [[maybe_unused]] Uint32 flags)
25{
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);
32 // SDL_SetProperty(props, SDL_PROP_WINDOW_CREATE_FL);
33 _window = SDL_CreateWindowWithProperties(props);
34 SDL_DestroyProperties(props);
35}
36
37SdlWindow::SdlWindow(SdlWindow&& other) noexcept
38 : _window(other._window), _offset_x(other._offset_x), _offset_y(other._offset_y)
39{
40 other._window = nullptr;
41}
42
43SdlWindow::~SdlWindow()
44{
45 SDL_DestroyWindow(_window);
46}
47
48Uint32 SdlWindow::id() const
49{
50 if (!_window)
51 return 0;
52 return SDL_GetWindowID(_window);
53}
54
55int SdlWindow::displayIndex() const
56{
57 if (!_window)
58 return 0;
59 return WINPR_ASSERTING_INT_CAST(int, SDL_GetDisplayForWindow(_window));
60}
61
62SDL_Rect SdlWindow::rect() const
63{
64 SDL_Rect rect = {};
65 if (_window)
66 {
67 SDL_GetWindowPosition(_window, &rect.x, &rect.y);
68 SDL_GetWindowSize(_window, &rect.w, &rect.h);
69 }
70 return rect;
71}
72
73SDL_Window* SdlWindow::window() const
74{
75 return _window;
76}
77
78Sint32 SdlWindow::offsetX() const
79{
80 return _offset_x;
81}
82
83void SdlWindow::setOffsetX(Sint32 x)
84{
85 _offset_x = x;
86}
87
88void SdlWindow::setOffsetY(Sint32 y)
89{
90 _offset_y = y;
91}
92
93Sint32 SdlWindow::offsetY() const
94{
95 return _offset_y;
96}
97
98bool SdlWindow::grabKeyboard(bool enable)
99{
100 if (!_window)
101 return false;
102 SDL_SetWindowKeyboardGrab(_window, enable);
103 return true;
104}
105
106bool SdlWindow::grabMouse(bool enable)
107{
108 if (!_window)
109 return false;
110 SDL_SetWindowMouseGrab(_window, enable);
111 return true;
112}
113
114void SdlWindow::setBordered(bool bordered)
115{
116 if (_window)
117 SDL_SetWindowBordered(_window, bordered);
118}
119
120void SdlWindow::raise()
121{
122 SDL_RaiseWindow(_window);
123}
124
125void SdlWindow::resizeable(bool use)
126{
127 SDL_SetWindowResizable(_window, use);
128}
129
130void SdlWindow::fullscreen(bool enter)
131{
132 auto curFlags = SDL_GetWindowFlags(_window);
133
134 if (enter)
135 {
136 if (!(curFlags & SDL_WINDOW_BORDERLESS))
137 {
138 auto idx = SDL_GetDisplayForWindow(_window);
139 auto mode = SDL_GetCurrentDisplayMode(idx);
140
141 SDL_RestoreWindow(_window); // Maximize so we can see the caption and
142 // bits
143 SDL_SetWindowBordered(_window, false);
144 SDL_SetWindowPosition(_window, 0, 0);
145 SDL_SetWindowAlwaysOnTop(_window, true);
146 SDL_RaiseWindow(_window);
147 if (mode)
148 SDL_SetWindowSize(_window, mode->w, mode->h);
149 }
150 }
151 else
152 {
153 if (curFlags & SDL_WINDOW_BORDERLESS)
154 {
155
156 SDL_SetWindowBordered(_window, true);
157 SDL_SetWindowAlwaysOnTop(_window, false);
158 SDL_RaiseWindow(_window);
159 SDL_MinimizeWindow(_window); // Maximize so we can see the caption and bits
160 SDL_MaximizeWindow(_window); // Maximize so we can see the caption and bits
161 }
162 }
163}
164
165void SdlWindow::minimize()
166{
167 SDL_MinimizeWindow(_window);
168}
169
170bool SdlWindow::fill(Uint8 r, Uint8 g, Uint8 b, Uint8 a)
171{
172 auto surface = SDL_GetWindowSurface(_window);
173 if (!surface)
174 return false;
175 SDL_Rect rect = { 0, 0, surface->w, surface->h };
176 auto color = SDL_MapSurfaceRGBA(surface, r, g, b, a);
177
178 SDL_FillSurfaceRect(surface, &rect, color);
179 return true;
180}
181
182bool SdlWindow::blit(SDL_Surface* surface, const SDL_Rect& srcRect, SDL_Rect& dstRect)
183{
184 auto screen = SDL_GetWindowSurface(_window);
185 if (!screen || !surface)
186 return false;
187 if (!SDL_SetSurfaceClipRect(surface, &srcRect))
188 return true;
189 if (!SDL_SetSurfaceClipRect(screen, &dstRect))
190 return true;
191 if (!SDL_BlitSurfaceScaled(surface, &srcRect, screen, &dstRect, SDL_SCALEMODE_LINEAR))
192 {
193 SDL_LogError(SDL_LOG_CATEGORY_RENDER, "SDL_BlitScaled: %s", SDL_GetError());
194 return false;
195 }
196 return true;
197}
198
199void SdlWindow::updateSurface()
200{
201 SDL_UpdateWindowSurface(_window);
202}
SdlWindow(const std::string &title, Sint32 startupX, Sint32 startupY, Sint32 width, Sint32 height, Uint32 flags)