raylib-cpp
C++ object-oriented wrapper library for raylib.
Loading...
Searching...
No Matches
Window.hpp
1#ifndef RAYLIB_CPP_INCLUDE_WINDOW_HPP_
2#define RAYLIB_CPP_INCLUDE_WINDOW_HPP_
3
4#include <span>
5#include <string>
6#include <string_view>
7
8#include "./raylib.hpp"
9#include "./RaylibException.hpp"
10#include "./Vector2.hpp"
11
12namespace raylib {
16class Window {
17 public:
24 // Nothing.
25 }
26
40 Window(int width, int height, const std::string_view title = "raylib", unsigned int flags = 0) {
41 Init(width, height, title, flags);
42 }
43
48 Close();
49 }
50
64 void Init(int width = 800, int height = 450, const std::string_view title = "raylib", unsigned int flags = 0) {
65 if (flags != 0) {
66 ::SetConfigFlags(flags);
67 }
68 ::InitWindow(width, height, title.data());
69 if (!::IsWindowReady()) {
70 throw RaylibException("Failed to create Window");
71 }
72 }
73
77 bool ShouldClose() const {
78 return ::WindowShouldClose();
79 }
80
84 void SetExitKey(int key) {
85 ::SetExitKey(key);
86 }
87
91 void Close() {
92 if (::IsWindowReady()) {
93 ::CloseWindow();
94 }
95 }
96
100 bool IsCursorOnScreen() const {
101 return ::IsCursorOnScreen();
102 }
103
107 bool IsFullscreen() const {
108 return ::IsWindowFullscreen();
109 }
110
114 bool IsHidden() const {
115 return ::IsWindowHidden();
116 }
117
121 bool IsMinimized() const {
122 return ::IsWindowMinimized();
123 }
124
128 bool IsMaximized() const {
129 return ::IsWindowMaximized();
130 }
131
135 bool IsFocused() const {
136 return ::IsWindowFocused();
137 }
138
142 bool IsResized() const {
143 return ::IsWindowResized();
144 }
145
149 bool IsState(unsigned int flag) const {
150 return ::IsWindowState(flag);
151 }
152
156 Window& SetState(unsigned int flag) {
157 ::SetWindowState(flag);
158 return *this;
159 }
160
164 Window& ClearState(unsigned int flag) {
165 ::ClearWindowState(flag);
166 return *this;
167 }
168
172 Window& ClearBackground(const ::Color& color = BLACK) {
173 ::ClearBackground(color);
174 return *this;
175 }
176
182 return *this;
183 }
184
188 Window& SetFullscreen(bool fullscreen) {
189 if (fullscreen) {
190 if (!IsFullscreen()) {
192 }
193 } else {
194 if (IsFullscreen()) {
196 }
197 }
198
199 return *this;
200 }
201
206 ::ToggleBorderlessWindowed();
207 return *this;
208 }
209
214 ::MaximizeWindow();
215 return *this;
216 }
217
222 ::MinimizeWindow();
223 return *this;
224 }
225
230 ::RestoreWindow();
231 return *this;
232 }
233
237 Window& SetIcon(const ::Image& image) {
238 ::SetWindowIcon(image);
239 return *this;
240 }
241
245 Window& SetIcons(Image* images, int count) {
246 ::SetWindowIcons(images, count);
247 return *this;
248 }
249
253 Window& SetIcons(std::span<Image> images) {
254 ::SetWindowIcons(images.data(), static_cast<int>(images.size()));
255 return *this;
256 }
257
261 Window& SetTitle(const std::string_view title) {
262 ::SetWindowTitle(title.data());
263 return *this;
264 }
265
269 Window& SetPosition(int x, int y) {
270 ::SetWindowPosition(x, y);
271 return *this;
272 }
273
277 Window& SetPosition(const ::Vector2& position) {
278 return SetPosition(static_cast<int>(position.x), static_cast<int>(position.y));
279 }
280
284 Window& SetMonitor(int monitor) {
285 ::SetWindowMonitor(monitor);
286 return *this;
287 }
288
292 Window& SetMinSize(int width, int height) {
293 ::SetWindowMinSize(width, height);
294 return *this;
295 }
296
300 Window& SetMinSize(const ::Vector2& size) {
301 ::SetWindowMinSize(static_cast<int>(size.x), static_cast<int>(size.y));
302 return *this;
303 }
304
308 Window& SetSize(int width, int height) {
309 ::SetWindowSize(width, height);
310 return *this;
311 }
312
316 Window& SetOpacity(float opacity) {
317 ::SetWindowOpacity(opacity);
318 return *this;
319 }
320
325 ::SetWindowFocused();
326 return *this;
327 }
328
332 Window& SetSize(const ::Vector2& size) {
333 return SetSize(static_cast<int>(size.x), static_cast<int>(size.y));
334 }
335
339 Vector2 GetSize() const {
340 return {static_cast<float>(GetWidth()), static_cast<float>(GetHeight())};
341 }
342
346 void* GetHandle() const {
347 return ::GetWindowHandle();
348 }
349
355 return *this;
356 }
357
362 ::EndDrawing();
363 return *this;
364 }
365
369 int GetWidth() const {
370 return ::GetScreenWidth();
371 }
372
376 int GetHeight() const {
377 return ::GetScreenHeight();
378 }
379
383 int GetRenderWidth() const {
384 return ::GetRenderWidth();
385 }
386
390 int GetRenderHeight() const {
391 return ::GetRenderHeight();
392 }
393
398 return ::GetWindowPosition();
399 }
400
405 return ::GetWindowScaleDPI();
406 }
407
411 void SetClipboardText(const std::string_view text) {
412 ::SetClipboardText(text.data());
413 }
414
418 const std::string GetClipboardText() {
419 return ::GetClipboardText();
420 }
421
426 ::SetTargetFPS(fps);
427 return *this;
428 }
429
433 int GetFPS() const {
434 return ::GetFPS();
435 }
436
440 void DrawFPS(int posX = 10, int posY = 10) const {
441 ::DrawFPS(posX, posY);
442 }
443
447 float GetFrameTime() const {
448 return ::GetFrameTime();
449 }
450
454 double GetTime() const {
455 return ::GetTime();
456 }
457
461 static bool IsReady() {
462 return ::IsWindowReady();
463 }
464
472 void SetConfigFlags(unsigned int flags) {
473 ::SetConfigFlags(flags);
474 }
475};
476} // namespace raylib
477
478using RWindow = raylib::Window;
479
480#endif // RAYLIB_CPP_INCLUDE_WINDOW_HPP_
Image type, bpp always RGBA (32bit)
Definition: Image.hpp:19
Exception used for most raylib-related exceptions.
Vector2 type.
Definition: Vector2.hpp:19
Window and Graphics Device Functions.
Definition: Window.hpp:16
int GetHeight() const
Get current screen height.
Definition: Window.hpp:376
void SetClipboardText(const std::string_view text)
Set clipboard text content.
Definition: Window.hpp:411
int GetRenderWidth() const
Get current render width (it considers HiDPI)
Definition: Window.hpp:383
Window & ToggleFullscreen()
Toggle window state: fullscreen/windowed.
Definition: Window.hpp:180
int GetRenderHeight() const
Get current render height (it considers HiDPI)
Definition: Window.hpp:390
Window & SetPosition(int x, int y)
Set window position on screen.
Definition: Window.hpp:269
int GetWidth() const
Get current screen width.
Definition: Window.hpp:369
Window & SetFocused()
Set window focused (only PLATFORM_DESKTOP)
Definition: Window.hpp:324
Window & ClearState(unsigned int flag)
Clear window configuration state flags.
Definition: Window.hpp:164
Window & SetTitle(const std::string_view title)
Set title for window.
Definition: Window.hpp:261
Vector2 GetScaleDPI() const
Get window scale DPI factor.
Definition: Window.hpp:404
Window & Restore()
Set window state: not minimized/maximized (only PLATFORM_DESKTOP)
Definition: Window.hpp:229
Window & SetSize(const ::Vector2 &size)
Set window dimensions.
Definition: Window.hpp:332
Vector2 GetPosition() const
Get window position XY on monitor.
Definition: Window.hpp:397
Window & SetState(unsigned int flag)
Set window configuration state using flags.
Definition: Window.hpp:156
Window & SetMonitor(int monitor)
Set monitor for the current window.
Definition: Window.hpp:284
Window & BeginDrawing()
Setup canvas (framebuffer) to start drawing.
Definition: Window.hpp:353
Window & SetMinSize(const ::Vector2 &size)
Set window minimum dimensions.
Definition: Window.hpp:300
bool IsFullscreen() const
Check if window is currently fullscreen.
Definition: Window.hpp:107
void SetConfigFlags(unsigned int flags)
Sets the configuration flags for raylib.
Definition: Window.hpp:472
Window & Maximize()
Set window state: maximized, if resizable (only PLATFORM_DESKTOP)
Definition: Window.hpp:213
void Close()
Close window and unload OpenGL context.
Definition: Window.hpp:91
bool IsState(unsigned int flag) const
Check if one specific window flag is enabled.
Definition: Window.hpp:149
bool ShouldClose() const
Check if KEY_ESCAPE pressed or Close icon pressed.
Definition: Window.hpp:77
~Window()
Close window and unload OpenGL context.
Definition: Window.hpp:47
double GetTime() const
Returns elapsed time in seconds since InitWindow()
Definition: Window.hpp:454
void Init(int width=800, int height=450, const std::string_view title="raylib", unsigned int flags=0)
Initializes the window.
Definition: Window.hpp:64
Window & ClearBackground(const ::Color &color=BLACK)
Clear window with given color.
Definition: Window.hpp:172
Window & SetTargetFPS(int fps)
Set target FPS (maximum)
Definition: Window.hpp:425
Window & SetSize(int width, int height)
Set window dimensions.
Definition: Window.hpp:308
int GetFPS() const
Returns current FPS.
Definition: Window.hpp:433
void SetExitKey(int key)
Set a custom key to exit program (default is ESC)
Definition: Window.hpp:84
void DrawFPS(int posX=10, int posY=10) const
Draw current FPS.
Definition: Window.hpp:440
const std::string GetClipboardText()
Get clipboard text content.
Definition: Window.hpp:418
Window & SetPosition(const ::Vector2 &position)
Set window position on screen.
Definition: Window.hpp:277
Window & SetFullscreen(bool fullscreen)
Set whether or not the application should be fullscreen.
Definition: Window.hpp:188
static bool IsReady()
Check if window has been initialized successfully.
Definition: Window.hpp:461
float GetFrameTime() const
Returns time in seconds for last frame drawn.
Definition: Window.hpp:447
Window & ToggleBorderless()
Toggle window state: borderless/windowed.
Definition: Window.hpp:205
bool IsCursorOnScreen() const
Check if cursor is on the current screen.
Definition: Window.hpp:100
Vector2 GetSize() const
Get the screen's width and height.
Definition: Window.hpp:339
bool IsHidden() const
Check if window is currently hidden.
Definition: Window.hpp:114
Window & SetIcons(Image *images, int count)
Set icon for window (multiple images, RGBA 32bit, only PLATFORM_DESKTOP)
Definition: Window.hpp:245
Window & EndDrawing()
End canvas drawing and swap buffers (double buffering)
Definition: Window.hpp:361
bool IsResized() const
Check if window has been resized last frame.
Definition: Window.hpp:142
Window()
Build a Window object, but defer the initialization.
Definition: Window.hpp:23
Window & SetOpacity(float opacity)
Set window opacity [0.0f..1.0f] (only PLATFORM_DESKTOP)
Definition: Window.hpp:316
Window & SetIcon(const ::Image &image)
Set icon for window.
Definition: Window.hpp:237
Window & Minimize()
Set window state: minimized, if resizable (only PLATFORM_DESKTOP)
Definition: Window.hpp:221
bool IsFocused() const
Check if window is currently focused.
Definition: Window.hpp:135
Window(int width, int height, const std::string_view title="raylib", unsigned int flags=0)
Initialize window and OpenGL context.
Definition: Window.hpp:40
bool IsMaximized() const
Check if window is currently minimized.
Definition: Window.hpp:128
bool IsMinimized() const
Check if window is currently minimized.
Definition: Window.hpp:121
Window & SetMinSize(int width, int height)
Set window minimum dimensions.
Definition: Window.hpp:292
Window & SetIcons(std::span< Image > images)
Set icon for window (multiple images, RGBA 32bit, only PLATFORM_DESKTOP)
Definition: Window.hpp:253
void * GetHandle() const
Get native window handle.
Definition: Window.hpp:346