Gamedev Framework (gf) 0.22.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
58#ifndef DOXYGEN_SHOULD_SKIP_THIS
59}
60
61template<>
62struct EnableBitmaskOperators<WindowHints> {
63 static constexpr bool value = true;
64};
65
66inline 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
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
160
167 void setPosition(Vector2i position);
168
176
183 void setSize(Vector2i size);
184
193
199 void setFullscreen(bool full = true);
200
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
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
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:46
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:82
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.
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.
bool pollEvent(Event &event)
Pop the event on top of the event queue, if any, and return it.
void setMouseCursorGrabbed(bool grabbed)
Grab or release the mouse cursor.
Vector2i getPosition() const
Get the position of the window.
bool waitEvent(Event &event)
Wait for an event and return it.
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:211
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.
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:332
@ Visible
The cell is visible (computed by FoV)
constexpr AllType All
Constant to represent "all".
Definition: Types.h:61
WindowHints
Hints for window creation.
Definition: Window.h:52
@ Decorated
Is the window decorated?
@ Resizable
Is the window resizable?
@ 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