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