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 #include "./Vector2.hpp"
9 
10 namespace raylib {
14 class Window {
15  public:
21  Window() {
22  // Nothing.
23  }
24 
30  Window(int width, int height, const std::string& title = "raylib") {
31  Init(width, height, title);
32  }
33 
37  ~Window() {
38  Close();
39  }
40 
46  inline void Init(int width = 800, int height = 450, const std::string& title = "raylib") {
47  ::InitWindow(width, height, title.c_str());
48  if (!IsWindowReady()) {
49  throw RaylibException("Failed to create Window");
50  }
51  }
52 
56  inline bool ShouldClose() const {
57  return ::WindowShouldClose();
58  }
59 
63  inline void Close() {
64  if (::IsWindowReady()) {
65  ::CloseWindow();
66  }
67  }
68 
72  inline bool IsCursorOnScreen() const {
73  return ::IsCursorOnScreen();
74  }
75 
79  inline static bool IsReady() {
80  return ::IsWindowReady();
81  }
82 
86  inline bool IsFullscreen() const {
87  return ::IsWindowFullscreen();
88  }
89 
93  inline bool IsHidden() const {
94  return ::IsWindowHidden();
95  }
96 
100  inline bool IsMinimized() const {
101  return ::IsWindowMinimized();
102  }
103 
107  inline bool IsMaximized() const {
108  return ::IsWindowMaximized();
109  }
110 
114  inline bool IsFocused() const {
115  return ::IsWindowFocused();
116  }
117 
121  inline bool IsResized() const {
122  return ::IsWindowResized();
123  }
124 
128  inline bool IsState(unsigned int flag) const {
129  return ::IsWindowState(flag);
130  }
131 
135  inline Window& SetState(unsigned int flag) {
136  ::SetWindowState(flag);
137  return *this;
138  }
139 
143  inline Window& ClearState(unsigned int flag) {
144  ::ClearWindowState(flag);
145  return *this;
146  }
147 
151  inline Window& ClearBackground(const ::Color& color = BLACK) {
152  ::ClearBackground(color);
153  return *this;
154  }
155 
161  return *this;
162  }
163 
167  inline Window& SetFullscreen(bool fullscreen) {
168  if (fullscreen) {
169  if (!IsFullscreen()) {
171  }
172  } else {
173  if (IsFullscreen()) {
175  }
176  }
177 
178  return *this;
179  }
180 
184  inline Window& Maximize() {
185  ::MaximizeWindow();
186  return *this;
187  }
188 
192  inline Window& Minimize() {
193  ::MinimizeWindow();
194  return *this;
195  }
196 
200  inline Window& Restore() {
201  ::RestoreWindow();
202  return *this;
203  }
204 
208  inline Window& SetIcon(const ::Image& image) {
209  ::SetWindowIcon(image);
210  return *this;
211  }
212 
216  inline Window& SetTitle(const std::string& title) {
217  ::SetWindowTitle(title.c_str());
218  return *this;
219  }
220 
224  inline Window& SetPosition(int x, int y) {
225  ::SetWindowPosition(x, y);
226  return *this;
227  }
228 
232  inline Window& SetPosition(const ::Vector2& position) {
233  return SetPosition(static_cast<int>(position.x), static_cast<int>(position.y));
234  }
235 
239  inline Window& SetMonitor(int monitor) {
240  ::SetWindowMonitor(monitor);
241  return *this;
242  }
243 
247  inline Window& SetMinSize(int width, int height) {
248  ::SetWindowMinSize(width, height);
249  return *this;
250  }
251 
255  inline Window& SetMinSize(const ::Vector2& size) {
256  ::SetWindowMinSize(static_cast<int>(size.x), static_cast<int>(size.y));
257  return *this;
258  }
259 
263  inline Window& SetSize(int width, int height) {
264  ::SetWindowSize(width, height);
265  return *this;
266  }
267 
271  inline Window& SetOpacity(float opacity) {
272  ::SetWindowOpacity(opacity);
273  return *this;
274  }
275 
279  inline Window& SetSize(const ::Vector2& size) {
280  return SetSize(static_cast<int>(size.x), static_cast<int>(size.y));
281  }
282 
286  inline Vector2 GetSize() const {
287  return {static_cast<float>(GetWidth()), static_cast<float>(GetHeight())};
288  }
289 
293  inline void* GetHandle() const {
294  return ::GetWindowHandle();
295  }
296 
300  inline Window& BeginDrawing() {
301  ::BeginDrawing();
302  return *this;
303  }
304 
308  inline Window& EndDrawing() {
309  ::EndDrawing();
310  return *this;
311  }
312 
316  inline int GetWidth() const {
317  return ::GetScreenWidth();
318  }
319 
323  inline int GetHeight() const {
324  return ::GetScreenHeight();
325  }
326 
330  inline int GetRenderWidth() const {
331  return ::GetRenderWidth();
332  }
333 
337  inline int GetRenderHeight() const {
338  return ::GetRenderHeight();
339  }
340 
344  inline Vector2 GetPosition() const {
345  return ::GetWindowPosition();
346  }
347 
351  inline Vector2 GetScaleDPI() const {
352  return ::GetWindowScaleDPI();
353  }
354 
358  inline void SetClipboardText(const std::string& text) {
359  SetClipboardText(text.c_str());
360  }
361 
365  inline const std::string& GetClipboardText() {
366  return GetClipboardText();
367  }
368 
372  inline Window& SetTargetFPS(int fps) {
373  ::SetTargetFPS(fps);
374  return *this;
375  }
376 
380  inline int GetFPS() const {
381  return ::GetFPS();
382  }
383 
387  inline void DrawFPS(int posX = 10, int posY = 10) const {
388  ::DrawFPS(posX, posY);
389  }
390 
394  inline float GetFrameTime() const {
395  return ::GetFrameTime();
396  }
397 
401  inline double GetTime() const {
402  return ::GetTime();
403  }
404 };
405 } // namespace raylib
406 
407 using RWindow = raylib::Window;
408 
409 #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:14
int GetHeight() const
Get current screen height.
Definition: Window.hpp:323
int GetRenderWidth() const
Get current render width (it considers HiDPI)
Definition: Window.hpp:330
void * GetHandle() const
Get native window handle.
Definition: Window.hpp:293
Window(int width, int height, const std::string &title="raylib")
Initialize window and OpenGL context.
Definition: Window.hpp:30
int GetRenderHeight() const
Get current render height (it considers HiDPI)
Definition: Window.hpp:337
Window & Minimize()
Set window state: minimized, if resizable (only PLATFORM_DESKTOP)
Definition: Window.hpp:192
Window & SetTargetFPS(int fps)
Set target FPS (maximum)
Definition: Window.hpp:372
int GetWidth() const
Get current screen width.
Definition: Window.hpp:316
Window & SetTitle(const std::string &title)
Set title for window.
Definition: Window.hpp:216
Window & ClearState(unsigned int flag)
Clear window configuration state flags.
Definition: Window.hpp:143
Vector2 GetScaleDPI() const
Get window scale DPI factor.
Definition: Window.hpp:351
Vector2 GetPosition() const
Get window position XY on monitor.
Definition: Window.hpp:344
Window & EndDrawing()
End canvas drawing and swap buffers (double buffering)
Definition: Window.hpp:308
Window & ToggleFullscreen()
Toggle window state: fullscreen/windowed.
Definition: Window.hpp:159
Window & SetIcon(const ::Image &image)
Set icon for window.
Definition: Window.hpp:208
Window & SetSize(const ::Vector2 &size)
Set window dimensions.
Definition: Window.hpp:279
bool IsFullscreen() const
Check if window is currently fullscreen.
Definition: Window.hpp:86
void Close()
Close window and unload OpenGL context.
Definition: Window.hpp:63
bool IsState(unsigned int flag) const
Check if one specific window flag is enabled.
Definition: Window.hpp:128
bool ShouldClose() const
Check if KEY_ESCAPE pressed or Close icon pressed.
Definition: Window.hpp:56
~Window()
Close window and unload OpenGL context.
Definition: Window.hpp:37
double GetTime() const
Returns elapsed time in seconds since InitWindow()
Definition: Window.hpp:401
const std::string & GetClipboardText()
Get clipboard text content.
Definition: Window.hpp:365
Window & SetPosition(int x, int y)
Set window position on screen.
Definition: Window.hpp:224
Window & SetMonitor(int monitor)
Set monitor for the current window.
Definition: Window.hpp:239
Window & ClearBackground(const ::Color &color=BLACK)
Clear window with given color.
Definition: Window.hpp:151
Window & SetPosition(const ::Vector2 &position)
Set window position on screen.
Definition: Window.hpp:232
int GetFPS() const
Returns current FPS.
Definition: Window.hpp:380
Window & BeginDrawing()
Setup canvas (framebuffer) to start drawing.
Definition: Window.hpp:300
Window & SetState(unsigned int flag)
Set window configuration state using flags.
Definition: Window.hpp:135
void Init(int width=800, int height=450, const std::string &title="raylib")
Initializes the window.
Definition: Window.hpp:46
void DrawFPS(int posX=10, int posY=10) const
Draw current FPS.
Definition: Window.hpp:387
Window & Restore()
Set window state: not minimized/maximized (only PLATFORM_DESKTOP)
Definition: Window.hpp:200
static bool IsReady()
Check if window has been initialized successfully.
Definition: Window.hpp:79
Window & SetSize(int width, int height)
Set window dimensions.
Definition: Window.hpp:263
float GetFrameTime() const
Returns time in seconds for last frame drawn.
Definition: Window.hpp:394
bool IsCursorOnScreen() const
Check if cursor is on the current screen.
Definition: Window.hpp:72
Vector2 GetSize() const
Get the screen's width and height.
Definition: Window.hpp:286
bool IsHidden() const
Check if window is currently hidden.
Definition: Window.hpp:93
bool IsResized() const
Check if window has been resized last frame.
Definition: Window.hpp:121
Window & SetMinSize(int width, int height)
Set window minimum dimensions.
Definition: Window.hpp:247
Window()
Build a Window object, but defer the initialization.
Definition: Window.hpp:21
Window & SetMinSize(const ::Vector2 &size)
Set window minimum dimensions.
Definition: Window.hpp:255
Window & SetOpacity(float opacity)
Set window opacity [0.0f..1.0f] (only PLATFORM_DESKTOP)
Definition: Window.hpp:271
bool IsFocused() const
Check if window is currently focused.
Definition: Window.hpp:114
bool IsMaximized() const
Check if window is currently minimized.
Definition: Window.hpp:107
Window & SetFullscreen(bool fullscreen)
Set whether or not the application should be fullscreen.
Definition: Window.hpp:167
Window & Maximize()
Set window state: maximized, if resizable (only PLATFORM_DESKTOP)
Definition: Window.hpp:184
bool IsMinimized() const
Check if window is currently minimized.
Definition: Window.hpp:100
void SetClipboardText(const std::string &text)
Set clipboard text content.
Definition: Window.hpp:358
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