raylib-cpp
C++ object-oriented wrapper library for raylib.
Window.hpp
1 /*
2 * LICENSE: zlib/libpng
3 *
4 * raylib-cpp is licensed under an unmodified zlib/libpng license, which is an OSI-certified,
5 * BSD-like license that allows static linking with closed source software:
6 *
7 * Copyright (c) 2020 Rob Loach (@RobLoach)
8 *
9 * This software is provided "as-is", without any express or implied warranty. In no event
10 * will the authors be held liable for any damages arising from the use of this software.
11 *
12 * Permission is granted to anyone to use this software for any purpose, including commercial
13 * applications, and to alter it and redistribute it freely, subject to the following restrictions:
14 *
15 * 1. The origin of this software must not be misrepresented; you must not claim that you
16 * wrote the original software. If you use this software in a product, an acknowledgment
17 * in the product documentation would be appreciated but is not required.
18 *
19 * 2. Altered source versions must be plainly marked as such, and must not be misrepresented
20 * as being the original software.
21 *
22 * 3. This notice may not be removed or altered from any source distribution.
23 */
24 
25 #ifndef RAYLIB_CPP_INCLUDE_WINDOW_HPP_
26 #define RAYLIB_CPP_INCLUDE_WINDOW_HPP_
27 
28 #include <string>
29 
30 #include "./raylib.hpp"
31 
32 namespace raylib {
44 class Window {
45  public:
49  Window(int width = 800, int height = 450, const std::string& title = "raylib",
50  bool lateInit = false) {
51  if (!lateInit) {
52  Init(width, height, title);
53  }
54  }
55 
56  ~Window() {
57  Close();
58  }
59 
60  void Init(int width = 800, int height = 450, const std::string& title = "raylib") {
61  ::InitWindow(width, height, title.c_str());
62  }
63 
67  inline bool ShouldClose() const {
68  return ::WindowShouldClose();
69  }
70 
74  inline void Close() {
75  ::CloseWindow();
76  }
77 
81  inline bool IsCursorOnScreen() const {
82  return ::IsCursorOnScreen();
83  }
84 
88  inline static bool IsReady() {
89  return ::IsWindowReady();
90  }
91 
95  inline bool IsFullscreen() const {
96  return ::IsWindowFullscreen();
97  }
98 
102  inline bool IsHidden() const {
103  return ::IsWindowHidden();
104  }
105 
109  inline bool IsMinimized() const {
110  return ::IsWindowMinimized();
111  }
112 
116  inline bool IsMaximized() const {
117  return ::IsWindowMaximized();
118  }
119 
123  inline bool IsFocused() const {
124  return ::IsWindowFocused();
125  }
126 
130  inline bool IsResized() const {
131  return ::IsWindowResized();
132  }
133 
137  inline bool IsState(unsigned int flag) const {
138  return ::IsWindowState(flag);
139  }
140 
144  inline Window& SetState(unsigned int flag) {
145  ::SetWindowState(flag);
146  return *this;
147  }
148 
152  inline Window& ClearState(unsigned int flag) {
153  ::ClearWindowState(flag);
154  return *this;
155  }
156 
162  return *this;
163  }
164 
168  inline Window& SetFullscreen(bool fullscreen) {
169  if (fullscreen) {
170  if (!IsFullscreen()) {
172  }
173  } else {
174  if (IsFullscreen()) {
176  }
177  }
178 
179  return *this;
180  }
181 
185  inline Window& Maximize() {
186  ::MaximizeWindow();
187  return *this;
188  }
189 
193  inline Window& Minimize() {
194  ::MinimizeWindow();
195  return *this;
196  }
197 
201  inline Window& Restore() {
202  ::RestoreWindow();
203  return *this;
204  }
205 
209  inline Window& SetIcon(const ::Image& image) {
210  ::SetWindowIcon(image);
211  return *this;
212  }
213 
217  inline Window& SetTitle(const std::string& title) {
218  ::SetWindowTitle(title.c_str());
219  return *this;
220  }
221 
225  inline Window& SetPosition(int x, int y) {
226  ::SetWindowPosition(x, y);
227  return *this;
228  }
229 
233  inline Window& SetPosition(const ::Vector2& position) {
234  return SetPosition(static_cast<int>(position.x), static_cast<int>(position.y));
235  }
236 
240  inline Window& SetMonitor(int monitor) {
241  ::SetWindowMonitor(monitor);
242  return *this;
243  }
244 
248  inline Window& SetMinSize(int width, int height) {
249  ::SetWindowMinSize(width, height);
250  return *this;
251  }
252 
256  inline Window& SetSize(int width, int height) {
257  ::SetWindowSize(width, height);
258  return *this;
259  }
260 
264  inline Window& SetSize(const ::Vector2& size) {
265  return SetSize(static_cast<int>(size.x), static_cast<int>(size.y));
266  }
267 
271  inline ::Vector2 GetSize() {
272  return {static_cast<float>(GetWidth()), static_cast<float>(GetHeight())};
273  }
274 
278  inline void* GetHandle() const {
279  return ::GetWindowHandle();
280  }
281 
285  inline Window& BeginDrawing() {
286  ::BeginDrawing();
287  return *this;
288  }
289 
293  inline Window& EndDrawing() {
294  ::EndDrawing();
295  return *this;
296  }
297 
301  inline int GetWidth() const {
302  return ::GetScreenWidth();
303  }
304 
308  inline int GetHeight() const {
309  return ::GetScreenHeight();
310  }
311 
315  inline ::Vector2 GetPosition() const {
316  return ::GetWindowPosition();
317  }
318 
322  inline ::Vector2 GetScaleDPI() const {
323  return ::GetWindowScaleDPI();
324  }
325 
329  inline Window& SetTargetFPS(int fps) {
330  ::SetTargetFPS(fps);
331  return *this;
332  }
333 
337  inline int GetFPS() const {
338  return ::GetFPS();
339  }
340 
344  inline float GetFrameTime() const {
345  return ::GetFrameTime();
346  }
347 
351  inline double GetTime() const {
352  return ::GetTime();
353  }
354 };
355 } // namespace raylib
356 
357 #endif // RAYLIB_CPP_INCLUDE_WINDOW_HPP_
raylib::Window::GetSize
inline ::Vector2 GetSize()
Get the screen's width and height.
Definition: Window.hpp:271
raylib::Window::GetWidth
int GetWidth() const
Get current screen width.
Definition: Window.hpp:301
raylib::Window::GetHandle
void * GetHandle() const
Get native window handle.
Definition: Window.hpp:278
raylib
Provides all the classes associated with raylib-cpp.
Definition: AudioDevice.hpp:31
raylib::Window::SetMonitor
Window & SetMonitor(int monitor)
Set monitor for the current window.
Definition: Window.hpp:240
raylib::Window::GetTime
double GetTime() const
Returns elapsed time in seconds since InitWindow()
Definition: Window.hpp:351
raylib::Window::~Window
~Window()
Definition: Window.hpp:56
raylib::Window::IsHidden
bool IsHidden() const
Check if window is currently hidden.
Definition: Window.hpp:102
raylib::Window::Init
void Init(int width=800, int height=450, const std::string &title="raylib")
Definition: Window.hpp:60
raylib::Window::Close
void Close()
Close window and unload OpenGL context.
Definition: Window.hpp:74
raylib::Window::SetMinSize
Window & SetMinSize(int width, int height)
Set window minimum dimensions.
Definition: Window.hpp:248
raylib::Window::SetState
Window & SetState(unsigned int flag)
Set window configuration state using flags.
Definition: Window.hpp:144
raylib::Window::GetFrameTime
float GetFrameTime() const
Returns time in seconds for last frame drawn.
Definition: Window.hpp:344
raylib::Window::ShouldClose
bool ShouldClose() const
Check if KEY_ESCAPE pressed or Close icon pressed.
Definition: Window.hpp:67
raylib::Window::SetFullscreen
Window & SetFullscreen(bool fullscreen)
Set whether or not the application should be fullscreen.
Definition: Window.hpp:168
raylib::Window::SetPosition
Window & SetPosition(int x, int y)
Set window position on screen.
Definition: Window.hpp:225
raylib::Window::SetSize
Window & SetSize(const ::Vector2 &size)
Set window dimensions.
Definition: Window.hpp:264
raylib::Window::IsMaximized
bool IsMaximized() const
Check if window is currently minimized.
Definition: Window.hpp:116
raylib::Window::GetScaleDPI
inline ::Vector2 GetScaleDPI() const
Get window scale DPI factor.
Definition: Window.hpp:322
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:49
raylib::Window::IsFocused
bool IsFocused() const
Check if window is currently focused.
Definition: Window.hpp:123
raylib::Window::IsResized
bool IsResized() const
Check if window has been resized last frame.
Definition: Window.hpp:130
raylib::Window::GetHeight
int GetHeight() const
Get current screen height.
Definition: Window.hpp:308
raylib::Window::EndDrawing
Window & EndDrawing()
End canvas drawing and swap buffers (double buffering)
Definition: Window.hpp:293
raylib::Window::SetSize
Window & SetSize(int width, int height)
Set window dimensions.
Definition: Window.hpp:256
raylib::Window::GetFPS
int GetFPS() const
Returns current FPS.
Definition: Window.hpp:337
raylib::Window::Minimize
Window & Minimize()
Set window state: minimized, if resizable (only PLATFORM_DESKTOP)
Definition: Window.hpp:193
raylib::Window::ToggleFullscreen
Window & ToggleFullscreen()
Toggle window state: fullscreen/windowed.
Definition: Window.hpp:160
raylib::Window::Maximize
Window & Maximize()
Set window state: maximized, if resizable (only PLATFORM_DESKTOP)
Definition: Window.hpp:185
raylib::Window::SetTitle
Window & SetTitle(const std::string &title)
Set title for window.
Definition: Window.hpp:217
raylib::Window::IsCursorOnScreen
bool IsCursorOnScreen() const
Check if cursor is on the current screen.
Definition: Window.hpp:81
raylib::Window::IsFullscreen
bool IsFullscreen() const
Check if window is currently fullscreen.
Definition: Window.hpp:95
raylib::Window::IsReady
static bool IsReady()
Check if window has been initialized successfully.
Definition: Window.hpp:88
raylib::Window::BeginDrawing
Window & BeginDrawing()
Setup canvas (framebuffer) to start drawing.
Definition: Window.hpp:285
raylib::Window::ClearState
Window & ClearState(unsigned int flag)
Clear window configuration state flags.
Definition: Window.hpp:152
raylib::Window::IsState
bool IsState(unsigned int flag) const
Check if one specific window flag is enabled.
Definition: Window.hpp:137
raylib::Window::SetPosition
Window & SetPosition(const ::Vector2 &position)
Set window position on screen.
Definition: Window.hpp:233
raylib::Window::SetTargetFPS
Window & SetTargetFPS(int fps)
Set target FPS (maximum)
Definition: Window.hpp:329
raylib::Window::GetPosition
inline ::Vector2 GetPosition() const
Get window position XY on monitor.
Definition: Window.hpp:315
raylib::Window::IsMinimized
bool IsMinimized() const
Check if window is currently minimized.
Definition: Window.hpp:109
raylib::Window::Restore
Window & Restore()
Set window state: not minimized/maximized (only PLATFORM_DESKTOP)
Definition: Window.hpp:201
raylib::Window::SetIcon
Window & SetIcon(const ::Image &image)
Set icon for window.
Definition: Window.hpp:209
raylib::Window
Window and Graphics Device Functions.
Definition: Window.hpp:44