Gamedev Framework (gf) 1.2.0
A C++17 framework for 2D games
Window.h
1/*
2 * Gamedev Framework (gf)
3 * Copyright (C) 2016-2022 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
38struct SDL_Window;
39
40namespace gf {
41#ifndef DOXYGEN_SHOULD_SKIP_THIS
42inline 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
62 enum class EventFilter {
63 TouchAsMouse = 0x0001,
64 AnyWindow = 0x0002,
65 };
66
67
68#ifndef DOXYGEN_SHOULD_SKIP_THIS
69}
70
71template<>
72struct EnableBitmaskOperators<WindowHints> {
73 static constexpr bool value = true;
74};
75
76template<>
77struct EnableBitmaskOperators<EventFilter> {
78 static constexpr bool value = true;
79};
80
81inline namespace v1 {
82#endif
83
97 class GF_GRAPHICS_API Window {
98 public:
99
112 Window(const std::string& title, Vector2i size, Flags<WindowHints> hints = All);
113
120
124 Window(const Window&) = delete;
125
129 Window& operator=(const Window&) = delete;
130
142 bool isOpen();
143
152 void close();
153
166 void setTitle(const std::string& title);
167
175
182 void setPosition(Vector2i position);
183
191
198 void setSize(Vector2i size);
199
208
214 void setFullscreen(bool full = true);
215
220
226 bool isFullscreen() const {
227 return m_isFullscreen;
228 }
229
242 bool isMinimized() const;
243
247 void minimize();
248
254 void restore();
255
261 bool isMaximized() const;
262
266 void maximize();
267
273 bool isVisible() const;
274
279 void show();
280
285 void hide();
286
295 void setVisible(bool visible = true);
296
302 bool isDecorated() const;
303
310 void setDecorated(bool decorated = true);
311
317 bool isFocused() const;
318
324 bool isResizable() const;
325
334 void setResizable(bool resizable = true);
335
336
347 uint32_t getWindowId() const {
348 return m_windowId;
349 }
350
372 bool pollEvent(Event& event, Flags<EventFilter> filters = None);
373
398 bool waitEvent(Event& event, Flags<EventFilter> filters = None);
399
418 void setVerticalSyncEnabled(bool enabled);
419
427
437 void setFramerateLimit(unsigned int limit);
438
448 void display();
449
465 void setMouseCursorVisible(bool visible);
466
479 void setMouseCursorGrabbed(bool grabbed);
480
481
491 void setMouseCursor(const Cursor& cursor);
492
496 private:
497 friend class SharedGraphics;
498 friend class RenderWindow;
499
500 void makeMainContextCurrent();
501 void makeSharedContextCurrent();
502 void makeNoContextCurrent();
503
504 private:
505 static std::vector<Event> g_pendingEvents;
506
507 bool pickEventForWindow(uint32_t windowId, Event& event);
508
509 private:
510 Library m_lib; // to automatically initialize SDL
511
512 private:
513 SDL_Window *m_window;
514 uint32_t m_windowId;
515 void *m_mainContext;
516 void *m_sharedContext;
517 bool m_shouldClose;
518 bool m_isFullscreen;
519
520 // framerate limit handling
521 Clock m_clock;
522 Time m_duration;
523
524 unsigned m_vao;
525 };
526
527#ifndef DOXYGEN_SHOULD_SKIP_THIS
528}
529#endif
530
531}
532
533#endif // GL_WINDOW_H
Utility class that measures the elapsed time.
Definition: Clock.h:61
A mouse cursor.
Definition: Cursor.h:63
Bitfield relying on an enumeration.
Definition: Flags.h:48
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
A shared OpenGL context with the main thread.
Definition: SharedGraphics.h:42
Represents a time value.
Definition: Time.h:65
An OS window.
Definition: Window.h:97
void maximize()
Maximize the window.
void setFramerateLimit(unsigned int limit)
Limit the framerate to a maximum fixed frequency.
bool isVisible() const
Check if the window is visible.
void setVisible(bool visible=true)
Show or hide the window.
void setTitle(const std::string &title)
Change the title of the window.
void setDecorated(bool decorated=true)
Show or hide the decoration of the window.
bool isFocused() const
Check if the window is focused.
Window(const Window &)=delete
Deleted copy constructor.
bool pollEvent(Event &event, Flags< EventFilter > filters=None)
Pop the event on top of the event queue, if any, and return it.
void hide()
Hide a window.
void setFullscreen(bool full=true)
Change the window state to fullscreen or not.
void show()
Show a window.
void close()
Request for closing.
void setResizable(bool resizable=true)
Set the window resizable or not.
void setSize(Vector2i size)
Change the size of the rendering region of the window.
void setMouseCursorVisible(bool visible)
Show or hide the mouse cursor.
Vector2i getFramebufferSize() const
Get the size of the underlying framebuffer.
void setMouseCursor(const Cursor &cursor)
Set the displayed cursor to a native system cursor.
Vector2i getSize() const
Get the size of the rendering region of the window.
void minimize()
Minimize the window.
bool isResizable() const
Check if the window is resizable.
bool isDecorated() const
Check if the window is decorated.
~Window()
Destructor.
Window & operator=(const Window &)=delete
Deleted copy assignment.
void setMouseCursorGrabbed(bool grabbed)
Grab or release the mouse cursor.
Vector2i getPosition() const
Get the position of the window.
Window(const std::string &title, Vector2i size, Flags< WindowHints > hints=All)
Create a new window.
bool isMaximized() const
Check if the window is maximized.
void setPosition(Vector2i position)
Change the position of the window on screen.
void toggleFullscreen()
Toggle the fullscreen state.
bool isMinimized() const
Check if the window is minimized.
bool isFullscreen() const
Check if the window is fullscreen or not.
Definition: Window.h:226
bool isVerticalSyncEnabled() const
Check if the vertical synchronization is enabled.
bool isOpen()
Tell whether or not closing has been requested.
void restore()
Restore the window.
bool waitEvent(Event &event, Flags< EventFilter > filters=None)
Wait for an event and return it.
void setVerticalSyncEnabled(bool enabled)
Enable or disable vertical synchronization.
void display()
Display on screen what has been rendered to the window so far.
uint32_t getWindowId() const
Get the window id.
Definition: Window.h:347
@ Visible
The cell is visible (computed by FoV)
constexpr AllType All
Constant to represent "all".
Definition: Types.h:61
constexpr NoneType None
Constant to represent "none".
Definition: Types.h:45
WindowHints
Hints for window creation.
Definition: Window.h:52
EventFilter
Filter for events.
Definition: Window.h:62
@ Decorated
Is the window decorated?
@ Resizable
Is the window resizable?
@ TouchAsMouse
Treat touch events as mouse events.
@ AnyWindow
Grab events from any window, not just the created window.
@ Event
An event is pending on the sockets.
ValueActivity value(float origin, float target, float &value, Time duration, Easing easing=Ease::linear)
Create a gf::ValueActivity.
Definition: Activities.h:526
The namespace for gf classes.
Defines a system event and its parameters.
Definition: Event.h:224