Gamedev Framework (gf)  0.19.0
A C++17 framework for 2D games
Window.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2021 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 <cstdint>
28 #include <string>
29 #include <vector>
30 
31 #include "Clock.h"
32 #include "Flags.h"
33 #include "GraphicsApi.h"
34 #include "Library.h"
35 #include "Time.h"
36 #include "Vector.h"
37 
38 struct SDL_Window;
39 
40 namespace gf {
41 #ifndef DOXYGEN_SHOULD_SKIP_THIS
42 inline namespace v1 {
43 #endif
44 
45  struct Event;
46  class Cursor;
47 
52  enum class WindowHints : uint32_t {
53  Resizable = 0x0001,
54  Visible = 0x0002,
55  Decorated = 0x0004,
56  };
57 
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 
82  class GF_GRAPHICS_API Window {
83  public:
84 
97  Window(const std::string& title, Vector2i size, Flags<WindowHints> hints = All);
98 
104  ~Window();
105 
109  Window(const Window&) = delete;
110 
114  Window& operator=(const Window&) = delete;
115 
127  bool isOpen();
128 
137  void close();
138 
151  void setTitle(const std::string& title);
152 
159  Vector2i getPosition() const;
160 
167  void setPosition(Vector2i position);
168 
175  Vector2i getSize() const;
176 
183  void setSize(Vector2i size);
184 
192  Vector2i getFramebufferSize() const;
193 
199  void setFullscreen(bool full = true);
200 
204  void toggleFullscreen();
205 
211  bool isFullscreen() const {
212  return m_isFullscreen;
213  }
214 
227  bool isMinimized() const;
228 
232  void minimize();
233 
239  void restore();
240 
246  bool isMaximized() const;
247 
251  void maximize();
252 
258  bool isVisible() const;
259 
264  void show();
265 
270  void hide();
271 
280  void setVisible(bool visible = true);
281 
287  bool isDecorated() const;
288 
295  void setDecorated(bool decorated = true);
296 
302  bool isFocused() const;
303 
309  bool isResizable() const;
310 
319  void setResizable(bool resizable = true);
320 
321 
332  uint32_t getWindowId() const {
333  return m_windowId;
334  }
335 
357  bool pollEvent(Event& event);
358 
383  bool waitEvent(Event& event);
384 
403  void setVerticalSyncEnabled(bool enabled);
404 
411  bool isVerticalSyncEnabled() const;
412 
422  void setFramerateLimit(unsigned int limit);
423 
433  void display();
434 
450  void setMouseCursorVisible(bool visible);
451 
464  void setMouseCursorGrabbed(bool grabbed);
465 
466 
476  void setMouseCursor(const Cursor& cursor);
477 
481  private:
482  friend class SharedGraphics;
483  friend class RenderWindow;
484 
485  void makeMainContextCurrent();
486  void makeSharedContextCurrent();
487  void makeNoContextCurrent();
488 
489  private:
490  static std::vector<Event> g_pendingEvents;
491 
492  bool pickEventForWindow(uint32_t windowId, Event& event);
493 
494  private:
495  Library m_lib; // to automatically initialize SDL
496 
497  private:
498  SDL_Window *m_window;
499  uint32_t m_windowId;
500  void *m_mainContext;
501  void *m_sharedContext;
502  bool m_shouldClose;
503  bool m_isFullscreen;
504 
505  // framerate limit handling
506  Clock m_clock;
507  Time m_duration;
508 
509  unsigned m_vao;
510  };
511 
512 #ifndef DOXYGEN_SHOULD_SKIP_THIS
513 }
514 #endif
515 
516 }
517 
518 #endif // GL_WINDOW_H
An event is pending on the sockets.
uint32_t getWindowId() const
Get the window id.
Definition: Window.h:332
Is the window decorated?
bool isFullscreen() const
Check if the window is fullscreen or not.
Definition: Window.h:211
Bitfield relying on an enumeration.
Definition: Flags.h:46
A shared OpenGL context with the main thread.
Definition: SharedGraphics.h:42
Is the window resizable?
Represents a time value.
Definition: Time.h:65
The namespace for gf classes.
Definition: Action.h:35
A class to represent the library.
Definition: Library.h:44
A window that can serve as a target for 2D drawing.
Definition: RenderWindow.h:80
An OS window.
Definition: Window.h:82
If all of the activities ends.
The cell is visible (computed by FoV)
A mouse cursor.
Definition: Cursor.h:63
WindowHints
Hints for window creation.
Definition: Window.h:52
Defines a system event and its parameters.
Definition: Event.h:224
Utility class that measures the elapsed time.
Definition: Clock.h:61