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