Gamedev Framework (gf)  0.4.0
A C++11 framework for 2D games
Window.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2017 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  *
21  * Part of this file comes from SFML, with the same license:
22  * Copyright (C) 2007-2015 Laurent Gomila (laurent@sfml-dev.org)
23  */
24 #ifndef GF_WINDOW_H
25 #define GF_WINDOW_H
26 
27 #include <string>
28 
29 #include "Clock.h"
30 #include "Flags.h"
31 #include "Library.h"
32 #include "Portability.h"
33 #include "Time.h"
34 #include "Vector.h"
35 
36 struct SDL_Window;
37 
38 namespace gf {
39 #ifndef DOXYGEN_SHOULD_SKIP_THIS
40 inline namespace v1 {
41 #endif
42 
43  struct Event;
44 
45  /**
46  * @ingroup window
47  * @brief Hints for window creation
48  * @sa gf::WindowFlags
49  */
50  enum class WindowHints : uint32_t {
51  Resizable = 0x0001, ///< Is the window resizable?
52  Visible = 0x0002, ///< Is the window visible?
53  Decorated = 0x0004, ///< Is the window decorated?
54  };
55 
56  /**
57  * @ingroup window
58  * @brief Flags for window creation
59  * @sa gf::Flags, gf::WindowHints
60  */
61  using WindowFlags = Flags<WindowHints>;
62 #ifndef DOXYGEN_SHOULD_SKIP_THIS
63 }
64 
65 template<>
66 struct EnableBitmaskOperators<WindowHints> {
67  static constexpr bool value = true;
68 };
69 
70 inline namespace v1 {
71 #endif
72 
73  /**
74  * @ingroup window
75  * @brief An OS window
76  *
77  * The gf::Window class provides a simple interface for manipulating
78  * the window: move, resize, show/hide, control mouse cursor, etc.
79  * It also provides event handling through its pollEvent() and waitEvent()
80  * functions.
81  *
82  * @snippet snippets/doc_class_window.cc window
83  *
84  * @sa gf::RenderWindow
85  */
86  class GF_API Window {
87  public:
88 
89  /**
90  * @brief Create a new window
91  *
92  * This constructor creates the window with the size defined in `size`.
93  * Additional parameters can be passed with `hints` (resizable, visible,
94  * decorated).
95  *
96  * @param title The title of the window
97  * @param size The initial size of the window
98  * @param hints Some hints for the creation of the window
99  * @sa gf::WindowHints
100  */
101  Window(const std::string& title, Vector2u size, WindowFlags hints = WindowFlags(All));
102 
103  /**
104  * @brief Destructor
105  *
106  * Actually destroy the window.
107  */
108  ~Window();
109 
110  /**
111  * @brief Deleted copy constructor
112  */
113  Window(const Window&) = delete;
114 
115  /**
116  * @brief Deleted copy assignment
117  */
118  Window& operator=(const Window&) = delete;
119 
120  /**
121  * @name Window's lifecycle
122  * @{
123  */
124 
125  /**
126  * @brief Tell whether or not closing has been requested
127  *
128  * @return True if the window is open, false if closing has been requested
129  * @sa close()
130  */
131  bool isOpen();
132 
133  /**
134  * @brief Request for closing
135  *
136  * This function does not close the window immediately. It only requests
137  * the window to close. Actual closing is done when the object is destroyed.
138  *
139  * @sa isOpen()
140  */
141  void close();
142 
143  /** @} */
144 
145  /**
146  * @name Window's positon and size
147  * @{
148  */
149 
150  /**
151  * @brief Change the title of the window
152  *
153  * @param title New title
154  */
155  void setTitle(const std::string& title);
156 
157  /**
158  * @brief Get the position of the window
159  *
160  * @return The position of the window, in pixels
161  * @sa setPosition()
162  */
163  Vector2i getPosition() const;
164 
165  /**
166  * @brief Change the position of the window on screen
167  *
168  * @param position New position, in pixels
169  * @sa getPosition()
170  */
171  void setPosition(Vector2i position);
172 
173  /**
174  * @brief Get the size of the rendering region of the window
175  *
176  * @return The size in pixels
177  * @sa setSize(), getFramebufferSize()
178  */
179  Vector2u getSize() const;
180 
181  /**
182  * @brief Change the size of the rendering region of the window
183  *
184  * @param size New size, in pixels
185  * @sa getSize(), getFramebufferSize()
186  */
187  void setSize(Vector2u size);
188 
189  /**
190  * @brief Get the size of the underlying framebuffer
191  *
192  * This size can differ from the size returned by getSize() for high-DPI screens.
193  *
194  * @return getSize(), setSize()
195  */
196  Vector2u getFramebufferSize() const;
197 
198  /**
199  * @brief Change the window state to fullscreen or not
200  *
201  * @param full True if the window must be in fullscreen
202  */
203  void setFullscreen(bool full = true);
204 
205  /**
206  * @brief Toggle the fullscreen state
207  */
208  void toggleFullscreen();
209 
210  /**
211  * @brief Check if the window is fullscreen or not
212  *
213  * @returns True if the window is fullscreen
214  */
215  bool isFullscreen() const {
216  return m_isFullscreen;
217  }
218 
219  /** @} */
220 
221  /**
222  * @name Window's state
223  * @{
224  */
225 
226  /**
227  * @brief Minimize the window
228  */
229  void minimize();
230 
231  /**
232  * @brief Restore the window
233  *
234  * Restore the size and position of a minimized or maximized window.
235  */
236  void restore();
237 
238  /**
239  * @brief Maximize the window
240  */
241  void maximize();
242 
243  /**
244  * @brief Show a window
245  * @sa hide()
246  */
247  void show();
248 
249  /**
250  * @brief Hide a window
251  * @sa show()
252  */
253  void hide();
254 
255  /**
256  * @brief Show or hide the window
257  *
258  * The window is shown by default.
259  *
260  * @param visible True to show the window, false to hide it
261  * @sa show(), hide(), isVisible()
262  */
263  void setVisible(bool visible = true);
264 
265  /**
266  * @brief Show or hide the decoration of the window
267  *
268  * @param decorated True to show decoration
269  * @sa isDecorated()
270  */
271  void setDecorated(bool decorated = true);
272 
273  /**
274  * @brief Check if the window is focused
275  *
276  * @return True if the window is focused
277  */
278  bool isFocused() const;
279 
280  /**
281  * @brief Check if the window is minimized
282  *
283  * @return True if the window is minimized
284  */
285  bool isMinimized() const;
286 
287  /**
288  * @brief Check if the window is resizable
289  *
290  * @return True if the window is resizable
291  */
292  bool isResizable() const;
293 
294  /**
295  * @brief Check if the window is visible
296  *
297  * @return True if the window is visible
298  */
299  bool isVisible() const;
300 
301  /**
302  * @brief Check if the window is decorated
303  *
304  * @return True if the window is decorated
305  */
306  bool isDecorated() const;
307 
308  /** @} */
309 
310  /**
311  * @name Event handling
312  * @{
313  */
314 
315  /**
316  * @brief Pop the event on top of the event queue, if any, and return it
317  *
318  * This function is not blocking: if there's no pending event then
319  * it will return false and leave `event` unmodified.
320  * Note that more than one event may be present in the event queue,
321  * thus you should always call this function in a loop
322  * to make sure that you process every pending event.
323  *
324  * ~~~{.cc}
325  * gf::Event event;
326  *
327  * while (window.pollEvent(event)) {
328  * // process event...
329  * }
330  * ~~~
331  *
332  * @param event Event to be returned
333  * @return True if an event was returned, or false if the event queue was empty
334  * @sa waitEvent()
335  */
336  bool pollEvent(Event& event);
337 
338  /**
339  * @brief Wait for an event and return it
340  *
341  * This function is blocking: if there's no pending event then
342  * it will wait until an event is received.
343  * After this function returns (and no error occurred),
344  * the `event` object is always valid and filled properly.
345  * This function is typically used when you have a thread that
346  * is dedicated to events handling: you want to make this thread
347  * sleep as long as no new event is received.
348  *
349  * ~~~{.cc}
350  * gf::Event event;
351  *
352  * if (window.waitEvent(event))
353  * {
354  * // process event...
355  * }
356  * ~~~
357  *
358  * @param event Event to be returned
359  * @return False if any error occurred
360  * @sa pollEvent()
361  */
362  bool waitEvent(Event& event);
363 
364  /** @} */
365 
366  /**
367  * @name Display
368  * @{
369  */
370 
371  /**
372  * @brief Enable or disable vertical synchronization
373  *
374  * Activating vertical synchronization will limit the number
375  * of frames displayed to the refresh rate of the monitor.
376  * This can avoid some visual artifacts, and limit the framerate
377  * to a good value (but not constant across different computers).
378  *
379  * @param enabled True to enable v-sync, false to deactivate it
380  * @sa isVerticalSyncEnabled()
381  */
382  void setVerticalSyncEnabled(bool enabled);
383 
384  /**
385  * @brief Check if the vertical synchronization is enabled
386  *
387  * @returns True if the v-sync is enabled
388  * @sa setVerticalSyncEnabled()
389  */
390  bool isVerticalSyncEnabled() const;
391 
392  /**
393  * @brief Limit the framerate to a maximum fixed frequency
394  *
395  * If a limit is set, the window will use a small delay after
396  * each call to display() to ensure that the current frame
397  * lasted long enough to match the framerate limit.
398  *
399  * @param limit Framerate limit, in frames per seconds (use 0 to disable limit)
400  */
401  void setFramerateLimit(unsigned int limit);
402 
403  /**
404  * @brief Display on screen what has been rendered to the window so far
405  *
406  * This function is typically called after all OpenGL rendering
407  * has been done for the current frame, in order to show
408  * it on screen.
409  *
410  * @sa RenderWindow::display()
411  */
412  void display();
413 
414  /** @} */
415 
416 
417  /**
418  * @name Input management
419  * @{
420  */
421 
422  /**
423  * @brief Show or hide the mouse cursor
424  *
425  * The mouse cursor is visible by default.
426  *
427  * @param visible True to show the mouse cursor, false to hide it
428  */
429  void setMouseCursorVisible(bool visible);
430 
431  /**
432  * @brief Grab or release the mouse cursor
433  *
434  * If set, grabs the mouse cursor inside this window's client
435  * area so it may no longer be moved outside its bounds.
436  * Note that grabbing is only active while the window has
437  * focus and calling this function for fullscreen windows
438  * won't have any effect (fullscreen windows always grab the
439  * cursor).
440  *
441  * @param grabbed True to enable, false to disable
442  */
443  void setMouseCursorGrabbed(bool grabbed);
444 
445  /** @} */
446 
447 
448  private:
449  Library m_lib; // to automatically initialize SDL
450 
451  private:
452  SDL_Window *m_window;
453  void *m_context;
454  bool m_shouldClose;
455  bool m_isFullscreen;
456 
457  // framerate limit handling
458  Clock m_clock;
459  Time m_duration;
460  };
461 
462 #ifndef DOXYGEN_SHOULD_SKIP_THIS
463 }
464 #endif
465 
466 }
467 
468 #endif // GL_WINDOW_H
Vector2i getPosition() const
Get the position of the window.
void show()
Show a window.
bool pollEvent(Event &event)
Pop the event on top of the event queue, if any, and return it.
void setSize(Vector2u size)
Change the size of the rendering region of the window.
bool isOpen()
Tell whether or not closing has been requested.
Is the window resizable?
void restore()
Restore the window.
void minimize()
Minimize the window.
bool isVisible() const
Check if the window is visible.
Window(const Window &)=delete
Deleted copy constructor.
Bitfield relying on an enumeration.
Definition: Flags.h:68
bool isFullscreen() const
Check if the window is fullscreen or not.
Definition: Window.h:215
void setTitle(const std::string &title)
Change the title of the window.
void setVerticalSyncEnabled(bool enabled)
Enable or disable vertical synchronization.
Window(const std::string &title, Vector2u size, WindowFlags hints=WindowFlags(All))
Create a new window.
void setMouseCursorGrabbed(bool grabbed)
Grab or release the mouse cursor.
constexpr AllType All
Constant to represent "all".
Definition: Types.h:61
bool isVerticalSyncEnabled() const
Check if the vertical synchronization is enabled.
void display()
Display on screen what has been rendered to the window so far.
bool isResizable() const
Check if the window is resizable.
WindowHints
Hints for window creation.
Definition: Window.h:50
Represents a time value.
Definition: Time.h:73
Is the window decorated?
constexpr Flags(AllType)
Constructor with all flags set.
Definition: Flags.h:87
void toggleFullscreen()
Toggle the fullscreen state.
Vector2u getSize() const
Get the size of the rendering region of the window.
Window & operator=(const Window &)=delete
Deleted copy assignment.
~Window()
Destructor.
void setPosition(Vector2i position)
Change the position of the window on screen.
The namespace for gf classes.
Definition: Action.h:34
A class to represent the library.
Definition: Library.h:42
Is the window visible?
An OS window.
Definition: Window.h:86
void setFramerateLimit(unsigned int limit)
Limit the framerate to a maximum fixed frequency.
void maximize()
Maximize the window.
void setVisible(bool visible=true)
Show or hide the window.
void close()
Request for closing.
bool isDecorated() const
Check if the window is decorated.
void setMouseCursorVisible(bool visible)
Show or hide the mouse cursor.
bool isFocused() const
Check if the window is focused.
Defines a system event and its parameters.
Definition: Event.h:118
#define GF_API
Definition: Portability.h:35
Utility class that measures the elapsed time.
Definition: Clock.h:67
bool waitEvent(Event &event)
Wait for an event and return it.
Vector2u getFramebufferSize() const
Get the size of the underlying framebuffer.
void hide()
Hide a window.
void setDecorated(bool decorated=true)
Show or hide the decoration of the window.
void setFullscreen(bool full=true)
Change the window state to fullscreen or not.
bool isMinimized() const
Check if the window is minimized.