raylib-cpp
C++ object-oriented wrapper library for raylib.
Window.hpp
1#ifndef RAYLIB_CPP_INCLUDE_WINDOW_HPP_
2#define RAYLIB_CPP_INCLUDE_WINDOW_HPP_
3
4#include <string>
5
6#include "./raylib.hpp"
7#include "./RaylibException.hpp"
8
9namespace raylib {
13class Window {
14 public:
20 Window(int width = 800, int height = 450, const std::string& title = "raylib",
21 bool lateInit = false) {
22 if (!lateInit) {
23 if (!Init(width, height, title)) {
24 throw RaylibException("Failed to create Window");
25 }
26 }
27 }
28
33 Close();
34 }
35
41 bool Init(int width = 800, int height = 450, const std::string& title = "raylib") {
42 ::InitWindow(width, height, title.c_str());
43 return IsWindowReady();
44 }
45
49 inline bool ShouldClose() const {
50 return ::WindowShouldClose();
51 }
52
56 inline void Close() {
57 ::CloseWindow();
58 }
59
63 inline bool IsCursorOnScreen() const {
64 return ::IsCursorOnScreen();
65 }
66
70 inline static bool IsReady() {
71 return ::IsWindowReady();
72 }
73
77 inline bool IsFullscreen() const {
78 return ::IsWindowFullscreen();
79 }
80
84 inline bool IsHidden() const {
85 return ::IsWindowHidden();
86 }
87
91 inline bool IsMinimized() const {
92 return ::IsWindowMinimized();
93 }
94
98 inline bool IsMaximized() const {
99 return ::IsWindowMaximized();
100 }
101
105 inline bool IsFocused() const {
106 return ::IsWindowFocused();
107 }
108
112 inline bool IsResized() const {
113 return ::IsWindowResized();
114 }
115
119 inline bool IsState(unsigned int flag) const {
120 return ::IsWindowState(flag);
121 }
122
126 inline Window& SetState(unsigned int flag) {
127 ::SetWindowState(flag);
128 return *this;
129 }
130
134 inline Window& ClearState(unsigned int flag) {
135 ::ClearWindowState(flag);
136 return *this;
137 }
138
142 inline Window& ClearBackground(const ::Color& color = BLACK) {
143 ::ClearBackground(color);
144 return *this;
145 }
146
152 return *this;
153 }
154
158 inline Window& SetFullscreen(bool fullscreen) {
159 if (fullscreen) {
160 if (!IsFullscreen()) {
162 }
163 } else {
164 if (IsFullscreen()) {
166 }
167 }
168
169 return *this;
170 }
171
175 inline Window& Maximize() {
176 ::MaximizeWindow();
177 return *this;
178 }
179
183 inline Window& Minimize() {
184 ::MinimizeWindow();
185 return *this;
186 }
187
191 inline Window& Restore() {
192 ::RestoreWindow();
193 return *this;
194 }
195
199 inline Window& SetIcon(const ::Image& image) {
200 ::SetWindowIcon(image);
201 return *this;
202 }
203
207 inline Window& SetTitle(const std::string& title) {
208 ::SetWindowTitle(title.c_str());
209 return *this;
210 }
211
215 inline Window& SetPosition(int x, int y) {
216 ::SetWindowPosition(x, y);
217 return *this;
218 }
219
223 inline Window& SetPosition(const ::Vector2& position) {
224 return SetPosition(static_cast<int>(position.x), static_cast<int>(position.y));
225 }
226
230 inline Window& SetMonitor(int monitor) {
231 ::SetWindowMonitor(monitor);
232 return *this;
233 }
234
238 inline Window& SetMinSize(int width, int height) {
239 ::SetWindowMinSize(width, height);
240 return *this;
241 }
242
246 inline Window& SetMinSize(const ::Vector2& size) {
247 ::SetWindowMinSize(static_cast<int>(size.x), static_cast<int>(size.y));
248 return *this;
249 }
250
254 inline Window& SetSize(int width, int height) {
255 ::SetWindowSize(width, height);
256 return *this;
257 }
258
262 inline Window& SetSize(const ::Vector2& size) {
263 return SetSize(static_cast<int>(size.x), static_cast<int>(size.y));
264 }
265
269 inline Vector2 GetSize() const {
270 return {static_cast<float>(GetWidth()), static_cast<float>(GetHeight())};
271 }
272
276 inline void* GetHandle() const {
277 return ::GetWindowHandle();
278 }
279
285 return *this;
286 }
287
291 inline Window& EndDrawing() {
292 ::EndDrawing();
293 return *this;
294 }
295
299 inline int GetWidth() const {
300 return ::GetScreenWidth();
301 }
302
306 inline int GetHeight() const {
307 return ::GetScreenHeight();
308 }
309
313 inline ::Vector2 GetPosition() const {
314 return ::GetWindowPosition();
315 }
316
320 inline ::Vector2 GetScaleDPI() const {
321 return ::GetWindowScaleDPI();
322 }
323
327 inline Window& SetTargetFPS(int fps) {
328 ::SetTargetFPS(fps);
329 return *this;
330 }
331
335 inline int GetFPS() const {
336 return ::GetFPS();
337 }
338
342 inline Window& DrawFPS(int posX = 10, int posY = 10) {
343 ::DrawFPS(posX, posY);
344 return *this;
345 }
346
350 inline float GetFrameTime() const {
351 return ::GetFrameTime();
352 }
353
357 inline double GetTime() const {
358 return ::GetTime();
359 }
360};
361} // namespace raylib
362
363#endif // RAYLIB_CPP_INCLUDE_WINDOW_HPP_
Exception used for most raylib-related exceptions.
Vector2 type.
Definition: Vector2.hpp:16
Window and Graphics Device Functions.
Definition: Window.hpp:13
int GetHeight() const
Get current screen height.
Definition: Window.hpp:306
bool Init(int width=800, int height=450, const std::string &title="raylib")
Initializes the window.
Definition: Window.hpp:41
Window & ToggleFullscreen()
Toggle window state: fullscreen/windowed.
Definition: Window.hpp:150
Window & SetPosition(int x, int y)
Set window position on screen.
Definition: Window.hpp:215
int GetWidth() const
Get current screen width.
Definition: Window.hpp:299
Window & ClearState(unsigned int flag)
Clear window configuration state flags.
Definition: Window.hpp:134
Window & Restore()
Set window state: not minimized/maximized (only PLATFORM_DESKTOP)
Definition: Window.hpp:191
Window & SetSize(const ::Vector2 &size)
Set window dimensions.
Definition: Window.hpp:262
inline ::Vector2 GetPosition() const
Get window position XY on monitor.
Definition: Window.hpp:313
Window & SetState(unsigned int flag)
Set window configuration state using flags.
Definition: Window.hpp:126
Window & SetTitle(const std::string &title)
Set title for window.
Definition: Window.hpp:207
Window & SetMonitor(int monitor)
Set monitor for the current window.
Definition: Window.hpp:230
Window & BeginDrawing()
Setup canvas (framebuffer) to start drawing.
Definition: Window.hpp:283
Window & SetMinSize(const ::Vector2 &size)
Set window minimum dimensions.
Definition: Window.hpp:246
Window(int width=800, int height=450, const std::string &title="raylib", bool lateInit=false)
Initialize window and OpenGL context.
Definition: Window.hpp:20
bool IsFullscreen() const
Check if window is currently fullscreen.
Definition: Window.hpp:77
Window & Maximize()
Set window state: maximized, if resizable (only PLATFORM_DESKTOP)
Definition: Window.hpp:175
void Close()
Close window and unload OpenGL context.
Definition: Window.hpp:56
bool IsState(unsigned int flag) const
Check if one specific window flag is enabled.
Definition: Window.hpp:119
bool ShouldClose() const
Check if KEY_ESCAPE pressed or Close icon pressed.
Definition: Window.hpp:49
~Window()
Close window and unload OpenGL context.
Definition: Window.hpp:32
double GetTime() const
Returns elapsed time in seconds since InitWindow()
Definition: Window.hpp:357
Window & ClearBackground(const ::Color &color=BLACK)
Clear window with given color.
Definition: Window.hpp:142
Window & SetTargetFPS(int fps)
Set target FPS (maximum)
Definition: Window.hpp:327
Window & SetSize(int width, int height)
Set window dimensions.
Definition: Window.hpp:254
int GetFPS() const
Returns current FPS.
Definition: Window.hpp:335
Window & SetPosition(const ::Vector2 &position)
Set window position on screen.
Definition: Window.hpp:223
Window & SetFullscreen(bool fullscreen)
Set whether or not the application should be fullscreen.
Definition: Window.hpp:158
static bool IsReady()
Check if window has been initialized successfully.
Definition: Window.hpp:70
float GetFrameTime() const
Returns time in seconds for last frame drawn.
Definition: Window.hpp:350
bool IsCursorOnScreen() const
Check if cursor is on the current screen.
Definition: Window.hpp:63
Vector2 GetSize() const
Get the screen's width and height.
Definition: Window.hpp:269
bool IsHidden() const
Check if window is currently hidden.
Definition: Window.hpp:84
inline ::Vector2 GetScaleDPI() const
Get window scale DPI factor.
Definition: Window.hpp:320
Window & DrawFPS(int posX=10, int posY=10)
Draw current FPS.
Definition: Window.hpp:342
Window & EndDrawing()
End canvas drawing and swap buffers (double buffering)
Definition: Window.hpp:291
bool IsResized() const
Check if window has been resized last frame.
Definition: Window.hpp:112
Window & SetIcon(const ::Image &image)
Set icon for window.
Definition: Window.hpp:199
Window & Minimize()
Set window state: minimized, if resizable (only PLATFORM_DESKTOP)
Definition: Window.hpp:183
bool IsFocused() const
Check if window is currently focused.
Definition: Window.hpp:105
bool IsMaximized() const
Check if window is currently minimized.
Definition: Window.hpp:98
bool IsMinimized() const
Check if window is currently minimized.
Definition: Window.hpp:91
Window & SetMinSize(int width, int height)
Set window minimum dimensions.
Definition: Window.hpp:238
void * GetHandle() const
Get native window handle.
Definition: Window.hpp:276
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:8
static void SetWindowTitle(const std::string &title)
Set title for window.
Definition: Functions.hpp:31
static void InitWindow(int width, int height, const std::string &title="raylib")
Initialize window and OpenGL context.
Definition: Functions.hpp:24