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