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 
47  void Init(int width = 800, int height = 450, const std::string& title = "raylib") {
48  ::InitWindow(width, height, title.c_str());
49  if (!IsWindowReady()) {
50  throw RaylibException("Failed to create Window");
51  }
52  }
53 
57  inline bool ShouldClose() const {
58  return ::WindowShouldClose();
59  }
60 
64  inline void Close() {
65  ::CloseWindow();
66  }
67 
71  inline bool IsCursorOnScreen() const {
72  return ::IsCursorOnScreen();
73  }
74 
78  inline static bool IsReady() {
79  return ::IsWindowReady();
80  }
81 
85  inline bool IsFullscreen() const {
86  return ::IsWindowFullscreen();
87  }
88 
92  inline bool IsHidden() const {
93  return ::IsWindowHidden();
94  }
95 
99  inline bool IsMinimized() const {
100  return ::IsWindowMinimized();
101  }
102 
106  inline bool IsMaximized() const {
107  return ::IsWindowMaximized();
108  }
109 
113  inline bool IsFocused() const {
114  return ::IsWindowFocused();
115  }
116 
120  inline bool IsResized() const {
121  return ::IsWindowResized();
122  }
123 
127  inline bool IsState(unsigned int flag) const {
128  return ::IsWindowState(flag);
129  }
130 
134  inline Window& SetState(unsigned int flag) {
135  ::SetWindowState(flag);
136  return *this;
137  }
138 
142  inline Window& ClearState(unsigned int flag) {
143  ::ClearWindowState(flag);
144  return *this;
145  }
146 
150  inline Window& ClearBackground(const ::Color& color = BLACK) {
151  ::ClearBackground(color);
152  return *this;
153  }
154 
160  return *this;
161  }
162 
166  inline Window& SetFullscreen(bool fullscreen) {
167  if (fullscreen) {
168  if (!IsFullscreen()) {
170  }
171  } else {
172  if (IsFullscreen()) {
174  }
175  }
176 
177  return *this;
178  }
179 
183  inline Window& Maximize() {
184  ::MaximizeWindow();
185  return *this;
186  }
187 
191  inline Window& Minimize() {
192  ::MinimizeWindow();
193  return *this;
194  }
195 
199  inline Window& Restore() {
200  ::RestoreWindow();
201  return *this;
202  }
203 
207  inline Window& SetIcon(const ::Image& image) {
208  ::SetWindowIcon(image);
209  return *this;
210  }
211 
215  inline Window& SetTitle(const std::string& title) {
216  ::SetWindowTitle(title.c_str());
217  return *this;
218  }
219 
223  inline Window& SetPosition(int x, int y) {
224  ::SetWindowPosition(x, y);
225  return *this;
226  }
227 
231  inline Window& SetPosition(const ::Vector2& position) {
232  return SetPosition(static_cast<int>(position.x), static_cast<int>(position.y));
233  }
234 
238  inline Window& SetMonitor(int monitor) {
239  ::SetWindowMonitor(monitor);
240  return *this;
241  }
242 
246  inline Window& SetMinSize(int width, int height) {
247  ::SetWindowMinSize(width, height);
248  return *this;
249  }
250 
254  inline Window& SetMinSize(const ::Vector2& size) {
255  ::SetWindowMinSize(static_cast<int>(size.x), static_cast<int>(size.y));
256  return *this;
257  }
258 
262  inline Window& SetSize(int width, int height) {
263  ::SetWindowSize(width, height);
264  return *this;
265  }
266 
270  inline Window& SetOpacity(float opacity) {
271  ::SetWindowOpacity(opacity);
272  return *this;
273  }
274 
278  inline Window& SetSize(const ::Vector2& size) {
279  return SetSize(static_cast<int>(size.x), static_cast<int>(size.y));
280  }
281 
285  inline Vector2 GetSize() const {
286  return {static_cast<float>(GetWidth()), static_cast<float>(GetHeight())};
287  }
288 
292  inline void* GetHandle() const {
293  return ::GetWindowHandle();
294  }
295 
299  inline Window& BeginDrawing() {
300  ::BeginDrawing();
301  return *this;
302  }
303 
307  inline Window& EndDrawing() {
308  ::EndDrawing();
309  return *this;
310  }
311 
315  inline int GetWidth() const {
316  return ::GetScreenWidth();
317  }
318 
322  inline int GetHeight() const {
323  return ::GetScreenHeight();
324  }
325 
329  inline int GetRenderWidth() const {
330  return ::GetRenderWidth();
331  }
332 
336  inline int GetRenderHeight() const {
337  return ::GetRenderHeight();
338  }
339 
343  inline Vector2 GetPosition() const {
344  return ::GetWindowPosition();
345  }
346 
350  inline Vector2 GetScaleDPI() const {
351  return ::GetWindowScaleDPI();
352  }
353 
357  inline void SetClipboardText(const std::string& text) {
358  SetClipboardText(text.c_str());
359  }
360 
364  inline const std::string& GetClipboardText() {
365  return GetClipboardText();
366  }
367 
371  inline Window& SetTargetFPS(int fps) {
372  ::SetTargetFPS(fps);
373  return *this;
374  }
375 
379  inline int GetFPS() const {
380  return ::GetFPS();
381  }
382 
386  inline Window& DrawFPS(int posX = 10, int posY = 10) {
387  ::DrawFPS(posX, posY);
388  return *this;
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:322
int GetRenderWidth() const
Get current render width (it considers HiDPI)
Definition: Window.hpp:329
void * GetHandle() const
Get native window handle.
Definition: Window.hpp:292
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:336
Window & Minimize()
Set window state: minimized, if resizable (only PLATFORM_DESKTOP)
Definition: Window.hpp:191
Window & SetTargetFPS(int fps)
Set target FPS (maximum)
Definition: Window.hpp:371
int GetWidth() const
Get current screen width.
Definition: Window.hpp:315
Window & SetTitle(const std::string &title)
Set title for window.
Definition: Window.hpp:215
Window & ClearState(unsigned int flag)
Clear window configuration state flags.
Definition: Window.hpp:142
Vector2 GetScaleDPI() const
Get window scale DPI factor.
Definition: Window.hpp:350
Vector2 GetPosition() const
Get window position XY on monitor.
Definition: Window.hpp:343
Window & DrawFPS(int posX=10, int posY=10)
Draw current FPS.
Definition: Window.hpp:386
Window & EndDrawing()
End canvas drawing and swap buffers (double buffering)
Definition: Window.hpp:307
Window & ToggleFullscreen()
Toggle window state: fullscreen/windowed.
Definition: Window.hpp:158
Window & SetIcon(const ::Image &image)
Set icon for window.
Definition: Window.hpp:207
Window & SetSize(const ::Vector2 &size)
Set window dimensions.
Definition: Window.hpp:278
bool IsFullscreen() const
Check if window is currently fullscreen.
Definition: Window.hpp:85
void Close()
Close window and unload OpenGL context.
Definition: Window.hpp:64
bool IsState(unsigned int flag) const
Check if one specific window flag is enabled.
Definition: Window.hpp:127
bool ShouldClose() const
Check if KEY_ESCAPE pressed or Close icon pressed.
Definition: Window.hpp:57
~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:364
Window & SetPosition(int x, int y)
Set window position on screen.
Definition: Window.hpp:223
Window & SetMonitor(int monitor)
Set monitor for the current window.
Definition: Window.hpp:238
Window & ClearBackground(const ::Color &color=BLACK)
Clear window with given color.
Definition: Window.hpp:150
Window & SetPosition(const ::Vector2 &position)
Set window position on screen.
Definition: Window.hpp:231
int GetFPS() const
Returns current FPS.
Definition: Window.hpp:379
Window & BeginDrawing()
Setup canvas (framebuffer) to start drawing.
Definition: Window.hpp:299
Window & SetState(unsigned int flag)
Set window configuration state using flags.
Definition: Window.hpp:134
void Init(int width=800, int height=450, const std::string &title="raylib")
Initializes the window.
Definition: Window.hpp:47
Window & Restore()
Set window state: not minimized/maximized (only PLATFORM_DESKTOP)
Definition: Window.hpp:199
static bool IsReady()
Check if window has been initialized successfully.
Definition: Window.hpp:78
Window & SetSize(int width, int height)
Set window dimensions.
Definition: Window.hpp:262
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:71
Vector2 GetSize() const
Get the screen's width and height.
Definition: Window.hpp:285
bool IsHidden() const
Check if window is currently hidden.
Definition: Window.hpp:92
bool IsResized() const
Check if window has been resized last frame.
Definition: Window.hpp:120
Window & SetMinSize(int width, int height)
Set window minimum dimensions.
Definition: Window.hpp:246
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:254
Window & SetOpacity(float opacity)
Set window opacity [0.0f..1.0f] (only PLATFORM_DESKTOP)
Definition: Window.hpp:270
bool IsFocused() const
Check if window is currently focused.
Definition: Window.hpp:113
bool IsMaximized() const
Check if window is currently minimized.
Definition: Window.hpp:106
Window & SetFullscreen(bool fullscreen)
Set whether or not the application should be fullscreen.
Definition: Window.hpp:166
Window & Maximize()
Set window state: maximized, if resizable (only PLATFORM_DESKTOP)
Definition: Window.hpp:183
bool IsMinimized() const
Check if window is currently minimized.
Definition: Window.hpp:99
void SetClipboardText(const std::string &text)
Set clipboard text content.
Definition: Window.hpp:357
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