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 "./RaylibException.hpp"
9#include "./Vector2.hpp"
10#include "./raylib.hpp"
11
12namespace raylib {
16class Window {
17public:
23 Window() = default;
24
39 Window(int width, int height, const std::string_view title = "raylib", unsigned int flags = 0) {
40 Init(width, height, title, flags);
41 }
42
46 ~Window() { Close(); }
47
61 void Init(int width = 800, int height = 450, const std::string_view title = "raylib", unsigned int flags = 0) {
62 if (flags != 0) {
63 ::SetConfigFlags(flags);
64 }
65 ::InitWindow(width, height, title.data());
66 if (!::IsWindowReady()) {
67 throw RaylibException("Failed to create Window");
68 }
69 }
70
74 static bool ShouldClose() { return ::WindowShouldClose(); }
75
79 static void SetExitKey(int key) { ::SetExitKey(key); }
80
84 static void Close() {
85 if (::IsWindowReady()) {
86 ::CloseWindow();
87 }
88 }
89
93 static bool IsCursorOnScreen() { return ::IsCursorOnScreen(); }
94
98 static bool IsCursorHidden() { return ::IsCursorHidden(); }
99
103 static void HideCursor() { ::HideCursor(); }
104
108 static void ShowCursor() { ::ShowCursor(); }
109
113 static bool IsFullscreen() { return ::IsWindowFullscreen(); }
114
118 static bool IsHidden() { return ::IsWindowHidden(); }
119
123 static bool IsMinimized() { return ::IsWindowMinimized(); }
124
128 static bool IsMaximized() { return ::IsWindowMaximized(); }
129
133 static bool IsFocused() { return ::IsWindowFocused(); }
134
138 static bool IsResized() { return ::IsWindowResized(); }
139
143 static bool IsState(unsigned int flag) { return ::IsWindowState(flag); }
144
148 Window& SetState(unsigned int flag) {
149 ::SetWindowState(flag);
150 return *this;
151 }
152
156 Window& ClearState(unsigned int flag) {
157 ::ClearWindowState(flag);
158 return *this;
159 }
160
164 Window& ClearBackground(const ::Color& color = BLACK) {
165 ::ClearBackground(color);
166 return *this;
167 }
168
174 return *this;
175 }
176
180 Window& SetFullscreen(bool fullscreen) {
181 if (fullscreen) {
182 if (!IsFullscreen()) {
184 }
185 }
186 else {
187 if (IsFullscreen()) {
189 }
190 }
191
192 return *this;
193 }
194
199 ::ToggleBorderlessWindowed();
200 return *this;
201 }
202
207 ::MaximizeWindow();
208 return *this;
209 }
210
215 ::MinimizeWindow();
216 return *this;
217 }
218
223 ::RestoreWindow();
224 return *this;
225 }
226
230 Window& SetIcon(const ::Image& image) {
231 ::SetWindowIcon(image);
232 return *this;
233 }
234
238 Window& SetIcons(Image* images, int count) {
239 ::SetWindowIcons(images, count);
240 return *this;
241 }
242
246 Window& SetIcons(std::span<Image> images) {
247 ::SetWindowIcons(images.data(), static_cast<int>(images.size()));
248 return *this;
249 }
250
254 Window& SetTitle(const std::string_view title) {
255 ::SetWindowTitle(title.data());
256 return *this;
257 }
258
262 Window& SetPosition(int x, int y) {
263 ::SetWindowPosition(x, y);
264 return *this;
265 }
266
270 Window& SetPosition(const ::Vector2& position) {
271 return SetPosition(static_cast<int>(position.x), static_cast<int>(position.y));
272 }
273
277 Window& SetMonitor(int monitor) {
278 ::SetWindowMonitor(monitor);
279 return *this;
280 }
281
285 Window& SetMinSize(int width, int height) {
286 ::SetWindowMinSize(width, height);
287 return *this;
288 }
289
293 Window& SetMinSize(const ::Vector2& size) {
294 ::SetWindowMinSize(static_cast<int>(size.x), static_cast<int>(size.y));
295 return *this;
296 }
297
301 Window& SetSize(int width, int height) {
302 ::SetWindowSize(width, height);
303 return *this;
304 }
305
309 Window& SetOpacity(float opacity) {
310 ::SetWindowOpacity(opacity);
311 return *this;
312 }
313
318 ::SetWindowFocused();
319 return *this;
320 }
321
325 Window& SetSize(const ::Vector2& size) { return SetSize(static_cast<int>(size.x), static_cast<int>(size.y)); }
326
330 static Vector2 GetSize() { return {static_cast<float>(GetWidth()), static_cast<float>(GetHeight())}; }
331
335 static void* GetHandle() { return ::GetWindowHandle(); }
336
342 return *this;
343 }
344
349 ::EndDrawing();
350 return *this;
351 }
352
356 static int GetWidth() { return ::GetScreenWidth(); }
357
361 static int GetHeight() { return ::GetScreenHeight(); }
362
366 static int GetRenderWidth() { return ::GetRenderWidth(); }
367
371 static int GetRenderHeight() { return ::GetRenderHeight(); }
372
376 static Vector2 GetPosition() { return ::GetWindowPosition(); }
377
378 /*
379 * Get current window monitor
380 */
381 static int GetMonitor() { return ::GetCurrentMonitor(); }
382
386 static Vector2 GetScaleDPI() { return ::GetWindowScaleDPI(); }
387
391 void SetClipboardText(const std::string_view text) {
392 ::SetClipboardText(text.data());
393 }
394
398 static std::string GetClipboardText() { return ::GetClipboardText(); }
399
404 ::SetTargetFPS(fps);
405 return *this;
406 }
407
411 static int GetFPS() { return ::GetFPS(); }
412
416 static void DrawFPS(int posX = 10, int posY = 10) { ::DrawFPS(posX, posY); }
417
421 static float GetFrameTime() { return ::GetFrameTime(); }
422
426 static double GetTime() { return ::GetTime(); }
427
431 static bool IsValid() { return ::IsWindowReady(); }
432
440 static void SetConfigFlags(unsigned int flags) { ::SetConfigFlags(flags); }
441
453 bool Drawing() {
454 if (m_drawing) {
455 EndDrawing();
456 m_drawing = false;
457 }
458 else {
459 BeginDrawing();
460 m_drawing = true;
461 }
462
463 return m_drawing;
464 }
465
466 protected:
472 bool m_drawing = false;
473};
474} // namespace raylib
475
476using RWindow = raylib::Window;
477
478#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:20
Window and Graphics Device Functions.
Definition: Window.hpp:16
void SetClipboardText(const std::string_view text)
Set clipboard text content.
Definition: Window.hpp:391
Window & ToggleFullscreen()
Toggle window state: fullscreen/windowed.
Definition: Window.hpp:172
static bool IsMaximized()
Check if window is currently minimized.
Definition: Window.hpp:128
static int GetRenderWidth()
Get current render width (it considers HiDPI)
Definition: Window.hpp:366
static bool IsCursorOnScreen()
Check if cursor is on the current screen.
Definition: Window.hpp:93
static int GetHeight()
Get current screen height.
Definition: Window.hpp:361
static bool IsHidden()
Check if window is currently hidden.
Definition: Window.hpp:118
static int GetFPS()
Returns current FPS.
Definition: Window.hpp:411
Window & SetPosition(int x, int y)
Set window position on screen.
Definition: Window.hpp:262
static Vector2 GetPosition()
Get window position XY on monitor.
Definition: Window.hpp:376
bool m_drawing
Handles the internal drawing state for calling either BeginDrawing() or EndDrawing() from the Drawing...
Definition: Window.hpp:472
Window & SetFocused()
Set window focused (only PLATFORM_DESKTOP)
Definition: Window.hpp:317
Window & ClearState(unsigned int flag)
Clear window configuration state flags.
Definition: Window.hpp:156
Window & SetTitle(const std::string_view title)
Set title for window.
Definition: Window.hpp:254
static void * GetHandle()
Get native window handle.
Definition: Window.hpp:335
Window & Restore()
Set window state: not minimized/maximized (only PLATFORM_DESKTOP)
Definition: Window.hpp:222
static Vector2 GetScaleDPI()
Get window scale DPI factor.
Definition: Window.hpp:386
Window & SetSize(const ::Vector2 &size)
Set window dimensions.
Definition: Window.hpp:325
static bool IsValid()
Check if window has been initialized successfully.
Definition: Window.hpp:431
Window & SetState(unsigned int flag)
Set window configuration state using flags.
Definition: Window.hpp:148
Window & SetMonitor(int monitor)
Set monitor for the current window.
Definition: Window.hpp:277
static bool IsResized()
Check if window has been resized last frame.
Definition: Window.hpp:138
Window & BeginDrawing()
Setup canvas (framebuffer) to start drawing.
Definition: Window.hpp:340
Window & SetMinSize(const ::Vector2 &size)
Set window minimum dimensions.
Definition: Window.hpp:293
static void DrawFPS(int posX=10, int posY=10)
Draw current FPS.
Definition: Window.hpp:416
static bool IsState(unsigned int flag)
Check if one specific window flag is enabled.
Definition: Window.hpp:143
Window & Maximize()
Set window state: maximized, if resizable (only PLATFORM_DESKTOP)
Definition: Window.hpp:206
~Window()
Close window and unload OpenGL context.
Definition: Window.hpp:46
static bool ShouldClose()
Check if KEY_ESCAPE pressed or Close icon pressed.
Definition: Window.hpp:74
static std::string GetClipboardText()
Get clipboard text content.
Definition: Window.hpp:398
static void HideCursor()
Hides cursor.
Definition: Window.hpp:103
static bool IsMinimized()
Check if window is currently minimized.
Definition: Window.hpp:123
void Init(int width=800, int height=450, const std::string_view title="raylib", unsigned int flags=0)
Initializes the window.
Definition: Window.hpp:61
Window & ClearBackground(const ::Color &color=BLACK)
Clear window with given color.
Definition: Window.hpp:164
static bool IsCursorHidden()
Check if cursor is not visible.
Definition: Window.hpp:98
static Vector2 GetSize()
Get the screen's width and height.
Definition: Window.hpp:330
Window & SetTargetFPS(int fps)
Set target FPS (maximum)
Definition: Window.hpp:403
Window & SetSize(int width, int height)
Set window dimensions.
Definition: Window.hpp:301
static int GetWidth()
Get current screen width.
Definition: Window.hpp:356
static int GetRenderHeight()
Get current render height (it considers HiDPI)
Definition: Window.hpp:371
Window & SetPosition(const ::Vector2 &position)
Set window position on screen.
Definition: Window.hpp:270
Window & SetFullscreen(bool fullscreen)
Set whether or not the application should be fullscreen.
Definition: Window.hpp:180
static bool IsFullscreen()
Check if window is currently fullscreen.
Definition: Window.hpp:113
Window & ToggleBorderless()
Toggle window state: borderless/windowed.
Definition: Window.hpp:198
static void Close()
Close window and unload OpenGL context.
Definition: Window.hpp:84
static double GetTime()
Returns elapsed time in seconds since InitWindow()
Definition: Window.hpp:426
Window & SetIcons(Image *images, int count)
Set icon for window (multiple images, RGBA 32bit, only PLATFORM_DESKTOP)
Definition: Window.hpp:238
bool Drawing()
Alternates between calling BeginDrawing() and EndDrawing().
Definition: Window.hpp:453
Window & EndDrawing()
End canvas drawing and swap buffers (double buffering)
Definition: Window.hpp:348
static void SetExitKey(int key)
Set a custom key to exit program (default is ESC)
Definition: Window.hpp:79
Window & SetOpacity(float opacity)
Set window opacity [0.0f..1.0f] (only PLATFORM_DESKTOP)
Definition: Window.hpp:309
Window()=default
Build a Window object, but defer the initialization.
static bool IsFocused()
Check if window is currently focused.
Definition: Window.hpp:133
Window & SetIcon(const ::Image &image)
Set icon for window.
Definition: Window.hpp:230
Window & Minimize()
Set window state: minimized, if resizable (only PLATFORM_DESKTOP)
Definition: Window.hpp:214
static float GetFrameTime()
Returns time in seconds for last frame drawn.
Definition: Window.hpp:421
static void SetConfigFlags(unsigned int flags)
Sets the configuration flags for raylib.
Definition: Window.hpp:440
Window(int width, int height, const std::string_view title="raylib", unsigned int flags=0)
Initialize window and OpenGL context.
Definition: Window.hpp:39
static void ShowCursor()
Shows cursor.
Definition: Window.hpp:108
Window & SetMinSize(int width, int height)
Set window minimum dimensions.
Definition: Window.hpp:285
Window & SetIcons(std::span< Image > images)
Set icon for window (multiple images, RGBA 32bit, only PLATFORM_DESKTOP)
Definition: Window.hpp:246
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:8
static void SetWindowTitle(const std::string_view title)
Set title for window.
Definition: Functions.hpp:34
static void InitWindow(int width, int height, const std::string_view title="raylib")
Initialize window and OpenGL context.
Definition: Functions.hpp:27