All Classes Files Functions Variables Typedefs Pages
Window.hpp
1 #pragma once
2 
3 #include "RenderContext.hpp"
4 #include "Window.fpp"
5 #include "../config/BaseProxy.hpp"
6 #include "../input/InputEvent.hpp"
7 #include "../input/WindowEvent.hpp"
8 #include "../util/VectorCore.hpp"
9 
10 #include <cstdint>
11 #include <functional>
12 #include <map>
13 #include <memory>
14 #include <stdexcept>
15 #include <string>
16 #include <utility>
17 #include <vector>
18 
19 struct GLFWwindow;
20 
21 namespace lumina {
22 
23 enum class CursorMode : uint8_t {
24  Normal, Hidden, Free
25 };
26 
36 class Window : public config::CommonBase {
37 public:
42  Window(std::string title = "Lumina Application",
43  Vec2i size = Vec2i(600, 400));
44  ~Window();
45 
47  using EventCallback = std::function<EventResult(InputEvent)>;
48 
50  using EventCallbackIndex = std::vector<EventCallback>::size_type;
51 
53  using WindowCallback = std::function<EventResult(WindowEvent)>;
54 
56  using WindowCallbackIndex = std::vector<WindowCallback>::size_type;
57 
65  void setVersionHint(int major, int minor = 0);
66 
72  void setVSync(bool enable);
73 
75  void setTitle(std::string title);
76 
78  void resize(Vec2i size);
79 
81  void setCursorMode(CursorMode mode);
82 
84  Vec2i getSize();
85 
88 
93  void open();
94 
96  void close();
97 
99  void pollEvents();
100 
105  void update();
106 
113  bool isValid();
114 
119 
125 
130 
136 
137 private:
138  GLFWwindow* m_window;
139  Vec2i m_size;
140  std::pair<std::int16_t, std::int16_t> m_version;
141  std::string m_title; // TODO: check if we need this
142  std::unique_ptr<RenderContext> m_renderContext;
143  std::vector<InputEvent> m_eventQueue;
144  std::vector<EventCallback> m_eventCallbacks;
145  std::vector<WindowEvent> m_windowEventQueue;
146  std::vector<WindowCallback> m_windowCallbacks;
147  float m_lastMouseX, m_lastMouseY;
148  bool m_resetLastPos;
149  CursorMode m_cursorMode;
150 
151 
152  void postEvent(InputEvent e);
153  void postWindowEvent(WindowEvent e);
154 
155  static std::map<GLFWwindow*, Window*> s_eventReceiver;
156 
157  static void resizeCallback(GLFWwindow* win, int width, int height);
158  static void keyCallback(GLFWwindow* win,
159  int key,
160  int scancode,
161  int action,
162  int mods);
163  static void charCallback(GLFWwindow* win, unsigned int key);
164  static void mouseButtonCallback(GLFWwindow* win,
165  int button,
166  int action,
167  int mods);
168  static void mousePosCallback(GLFWwindow* w, double xpos, double ypos);
169  static void mouseScrollCallback(GLFWwindow* w, double x, double y);
170  static void windowCloseCallback(GLFWwindow* w);
171 };
172 
173 }
174 
175 #include "Window.tpp"
void open()
Opens the window.
Definition: Window.cpp:32
void close()
Closes window and free resources.
Definition: Window.cpp:108
Definition: InputEvent.hpp:56
WindowCallbackIndex addWindowCallback(WindowCallback &&callback)
Add a callback to process WindowEvents.
Definition: Window.tpp:28
void update()
check for events and window status.
Definition: Window.cpp:146
void pollEvents()
Check for new events.
Definition: Window.cpp:162
EventCallbackIndex addEventCallback(EventCallback &&callback)
Add a callback to process InputEvents.
Definition: Window.tpp:17
void setTitle(std::string title)
Changes the title of the window.
Definition: Window.cpp:126
Vec2i getSize()
Returns the current size of the window.
Definition: Window.cpp:140
Definition: SingleBase.hpp:19
void setVersionHint(int major, int minor=0)
Sets a hint which OpenGL version to use to create the context.
Definition: Window.tpp:13
void setVSync(bool enable)
Disables or enables VSync If VSync is enabled RenderContext::swapBuffers will be synchronized with th...
Definition: Window.cpp:91
bool isValid()
Returns if the window is valid.
Definition: Window.cpp:196
std::vector< WindowCallback >::size_type WindowCallbackIndex
Type for accessing a specific WindowCallback.
Definition: Window.hpp:56
Definition: RenderContext.hpp:19
Window(std::string title="Lumina Application", Vec2i size=Vec2i(600, 400))
Creates a new Window but does not open it.
Definition: Window.tpp:3
void removeWindowCallback(WindowCallbackIndex index)
Removes an WindowEvent callback with the index previously returned by addWindowCallback.
Definition: Window.tpp:34
void setCursorMode(CursorMode mode)
Sets the mode of the mouse cursor.
Definition: Window.cpp:95
void removeEventCallback(EventCallbackIndex index)
Removes an InputEvent callback with the index previously returned by addEventCallback.
Definition: Window.tpp:23
void resize(Vec2i size)
Resizes the window.
Definition: Window.cpp:133
A Window from which a RenderContext can be obtained.
Definition: Window.hpp:36
Definition: WindowEvent.hpp:16
std::vector< EventCallback >::size_type EventCallbackIndex
Type for accessing a specific EventCallback.
Definition: Window.hpp:50
RenderContext & getRenderContext()
Returns the OpenGL context for this window.
Definition: Window.cpp:25
std::function< EventResult(WindowEvent)> WindowCallback
Functor type for handling WindowEvents.
Definition: Window.hpp:53
std::function< EventResult(InputEvent)> EventCallback
Functor type for handling InputEvents.
Definition: Window.hpp:47