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 
9 namespace raylib {
13 class 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 
32  ~Window() {
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 
283  inline Window& BeginDrawing() {
284  ::BeginDrawing();
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_
raylib::Window::GetWidth
int GetWidth() const
Get current screen width.
Definition: Window.hpp:299
raylib::Window::GetHandle
void * GetHandle() const
Get native window handle.
Definition: Window.hpp:276
raylib
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:8
raylib::Window::SetMonitor
Window & SetMonitor(int monitor)
Set monitor for the current window.
Definition: Window.hpp:230
raylib::Window::GetTime
double GetTime() const
Returns elapsed time in seconds since InitWindow()
Definition: Window.hpp:357
raylib::Window::~Window
~Window()
Close window and unload OpenGL context.
Definition: Window.hpp:32
raylib::Window::IsHidden
bool IsHidden() const
Check if window is currently hidden.
Definition: Window.hpp:84
raylib::SetWindowTitle
static void SetWindowTitle(const std::string &title)
Set title for window.
Definition: Functions.hpp:31
raylib::Window::Close
void Close()
Close window and unload OpenGL context.
Definition: Window.hpp:56
raylib::Window::ClearBackground
Window & ClearBackground(const ::Color &color=BLACK)
Clear window with given color.
Definition: Window.hpp:142
raylib::Window::SetMinSize
Window & SetMinSize(int width, int height)
Set window minimum dimensions.
Definition: Window.hpp:238
raylib::Window::SetState
Window & SetState(unsigned int flag)
Set window configuration state using flags.
Definition: Window.hpp:126
raylib::Window::GetFrameTime
float GetFrameTime() const
Returns time in seconds for last frame drawn.
Definition: Window.hpp:350
raylib::Window::ShouldClose
bool ShouldClose() const
Check if KEY_ESCAPE pressed or Close icon pressed.
Definition: Window.hpp:49
raylib::InitWindow
static void InitWindow(int width, int height, const std::string &title="raylib")
Initialize window and OpenGL context.
Definition: Functions.hpp:24
raylib::Window::SetFullscreen
Window & SetFullscreen(bool fullscreen)
Set whether or not the application should be fullscreen.
Definition: Window.hpp:158
raylib::Window::SetPosition
Window & SetPosition(int x, int y)
Set window position on screen.
Definition: Window.hpp:215
raylib::Window::SetSize
Window & SetSize(const ::Vector2 &size)
Set window dimensions.
Definition: Window.hpp:262
raylib::Window::IsMaximized
bool IsMaximized() const
Check if window is currently minimized.
Definition: Window.hpp:98
raylib::Window::GetScaleDPI
inline ::Vector2 GetScaleDPI() const
Get window scale DPI factor.
Definition: Window.hpp:320
raylib::Window::Window
Window(int width=800, int height=450, const std::string &title="raylib", bool lateInit=false)
Initialize window and OpenGL context.
Definition: Window.hpp:20
raylib::Window::IsFocused
bool IsFocused() const
Check if window is currently focused.
Definition: Window.hpp:105
raylib::Window::IsResized
bool IsResized() const
Check if window has been resized last frame.
Definition: Window.hpp:112
raylib::Window::GetHeight
int GetHeight() const
Get current screen height.
Definition: Window.hpp:306
raylib::Window::Init
bool Init(int width=800, int height=450, const std::string &title="raylib")
Initializes the window.
Definition: Window.hpp:41
raylib::Window::EndDrawing
Window & EndDrawing()
End canvas drawing and swap buffers (double buffering)
Definition: Window.hpp:291
raylib::Window::SetSize
Window & SetSize(int width, int height)
Set window dimensions.
Definition: Window.hpp:254
raylib::Vector2
Vector2 type.
Definition: Vector2.hpp:16
raylib::Window::GetFPS
int GetFPS() const
Returns current FPS.
Definition: Window.hpp:335
raylib::Window::Minimize
Window & Minimize()
Set window state: minimized, if resizable (only PLATFORM_DESKTOP)
Definition: Window.hpp:183
raylib::Window::ToggleFullscreen
Window & ToggleFullscreen()
Toggle window state: fullscreen/windowed.
Definition: Window.hpp:150
raylib::Window::Maximize
Window & Maximize()
Set window state: maximized, if resizable (only PLATFORM_DESKTOP)
Definition: Window.hpp:175
raylib::Window::SetMinSize
Window & SetMinSize(const ::Vector2 &size)
Set window minimum dimensions.
Definition: Window.hpp:246
raylib::Window::SetTitle
Window & SetTitle(const std::string &title)
Set title for window.
Definition: Window.hpp:207
raylib::Window::IsCursorOnScreen
bool IsCursorOnScreen() const
Check if cursor is on the current screen.
Definition: Window.hpp:63
raylib::Window::IsFullscreen
bool IsFullscreen() const
Check if window is currently fullscreen.
Definition: Window.hpp:77
raylib::Window::IsReady
static bool IsReady()
Check if window has been initialized successfully.
Definition: Window.hpp:70
raylib::Window::BeginDrawing
Window & BeginDrawing()
Setup canvas (framebuffer) to start drawing.
Definition: Window.hpp:283
raylib::Window::ClearState
Window & ClearState(unsigned int flag)
Clear window configuration state flags.
Definition: Window.hpp:134
raylib::Window::DrawFPS
Window & DrawFPS(int posX=10, int posY=10)
Draw current FPS.
Definition: Window.hpp:342
raylib::Window::GetSize
Vector2 GetSize() const
Get the screen's width and height.
Definition: Window.hpp:269
raylib::Window::IsState
bool IsState(unsigned int flag) const
Check if one specific window flag is enabled.
Definition: Window.hpp:119
raylib::Window::SetPosition
Window & SetPosition(const ::Vector2 &position)
Set window position on screen.
Definition: Window.hpp:223
raylib::RaylibException
Exception used for most raylib-related exceptions.
Definition: RaylibException.hpp:13
raylib::Window::SetTargetFPS
Window & SetTargetFPS(int fps)
Set target FPS (maximum)
Definition: Window.hpp:327
raylib::Window::GetPosition
inline ::Vector2 GetPosition() const
Get window position XY on monitor.
Definition: Window.hpp:313
raylib::Window::IsMinimized
bool IsMinimized() const
Check if window is currently minimized.
Definition: Window.hpp:91
raylib::Window::Restore
Window & Restore()
Set window state: not minimized/maximized (only PLATFORM_DESKTOP)
Definition: Window.hpp:191
raylib::Window::SetIcon
Window & SetIcon(const ::Image &image)
Set icon for window.
Definition: Window.hpp:199
raylib::Window
Window and Graphics Device Functions.
Definition: Window.hpp:13