Fireworks Engine  v2.0
Lightweight Sandbox Game Engine using OpenGL for additional Customisation and Quick Prototyping
Window.h
Go to the documentation of this file.
1 #pragma once
2 
3 // STD. includes
4 #include <iostream>
5 #define GL_SILENCE_DEPRECATION
6 // GLEW
7 #include <GL/glew.h>
8 // GLFW
9 #include <GLFW/glfw3.h>
10 #include <glm/glm.hpp>
11 
12 #include <src/utils/glassert.h>
13 
14 namespace fireworks { namespace graphics {
15 
16 #define MAX_KEYS 1024
17 #define MAX_BUTTONS 32
18 
20  enum Keys
21  {
22  UNKNOWN = GLFW_KEY_UNKNOWN,
23  SPACE = GLFW_KEY_SPACE,
24  APOSTROPHE = GLFW_KEY_APOSTROPHE,
25  COMMA = GLFW_KEY_COMMA,
26  MINUS = GLFW_KEY_MINUS,
27  PERIOD = GLFW_KEY_PERIOD,
28  SLASH = GLFW_KEY_SLASH,
29  NUM0 = GLFW_KEY_0,
30  NUM1 = GLFW_KEY_1,
31  NUM2 = GLFW_KEY_2,
32  NUM3 = GLFW_KEY_3,
33  NUM4 = GLFW_KEY_4,
34  NUM5 = GLFW_KEY_5,
35  NUM6 = GLFW_KEY_6,
36  NUM7 = GLFW_KEY_7,
37  NUM8 = GLFW_KEY_8,
38  NUM9 = GLFW_KEY_9,
39  SEMICOLON = GLFW_KEY_SEMICOLON,
40  EQUAL = GLFW_KEY_EQUAL,
41  A = GLFW_KEY_A,
42  B = GLFW_KEY_B,
43  C = GLFW_KEY_C,
44  D = GLFW_KEY_D,
45  E = GLFW_KEY_E,
46  F = GLFW_KEY_F,
47  G = GLFW_KEY_G,
48  H = GLFW_KEY_H,
49  I = GLFW_KEY_I,
50  J = GLFW_KEY_J,
51  K = GLFW_KEY_K,
52  L = GLFW_KEY_L,
53  M = GLFW_KEY_M,
54  N = GLFW_KEY_N,
55  O = GLFW_KEY_O,
56  P = GLFW_KEY_P,
57  Q = GLFW_KEY_Q,
58  R = GLFW_KEY_R,
59  S = GLFW_KEY_S,
60  T = GLFW_KEY_T,
61  U = GLFW_KEY_U,
62  V = GLFW_KEY_V,
63  W = GLFW_KEY_W,
64  X = GLFW_KEY_X,
65  Y = GLFW_KEY_Y,
66  Z = GLFW_KEY_Z,
67  LEFT_BRACKET = GLFW_KEY_LEFT_BRACKET,
68  BACKSLASH = GLFW_KEY_BACKSLASH,
69  RIGHT_BRACKET = GLFW_KEY_RIGHT_BRACKET,
70  GRAVE_ACCENT = GLFW_KEY_GRAVE_ACCENT,
71  WORLD_1 = GLFW_KEY_WORLD_1,
72  WORLD_2 = GLFW_KEY_WORLD_2,
73  ESCAPE = GLFW_KEY_ESCAPE,
74  ENTER = GLFW_KEY_ENTER,
75  TAB = GLFW_KEY_TAB,
76  BACKSPACE = GLFW_KEY_BACKSPACE,
77  INSERT = GLFW_KEY_INSERT,
78  DELETE = GLFW_KEY_DELETE,
79  RIGHT = GLFW_KEY_RIGHT,
80  LEFT = GLFW_KEY_LEFT,
81  DOWN = GLFW_KEY_DOWN,
82  UP = GLFW_KEY_UP,
83  PAGE_UP = GLFW_KEY_PAGE_UP,
84  PAGE_DOWN = GLFW_KEY_PAGE_DOWN,
85  HOME = GLFW_KEY_HOME,
86  END = GLFW_KEY_END,
87  CAPS_LOCK = GLFW_KEY_CAPS_LOCK,
88  SCROLL_LOCK = GLFW_KEY_SCROLL_LOCK,
89  NUM_LOCK = GLFW_KEY_NUM_LOCK,
90  PRINT_SCREEN = GLFW_KEY_PRINT_SCREEN,
91  PAUSE = GLFW_KEY_PAUSE,
92  F1 = GLFW_KEY_F1,
93  F2 = GLFW_KEY_F2,
94  F3 = GLFW_KEY_F3,
95  F4 = GLFW_KEY_F4,
96  F5 = GLFW_KEY_F5,
97  F6 = GLFW_KEY_F6,
98  F7 = GLFW_KEY_F7,
99  F8 = GLFW_KEY_F8,
100  F9 = GLFW_KEY_F9,
101  F10 = GLFW_KEY_F10,
102  F11 = GLFW_KEY_F11,
103  F12 = GLFW_KEY_F12,
104  F13 = GLFW_KEY_F13,
105  F14 = GLFW_KEY_F14,
106  F15 = GLFW_KEY_F15,
107  F16 = GLFW_KEY_F16,
108  F17 = GLFW_KEY_F17,
109  F18 = GLFW_KEY_F18,
110  F19 = GLFW_KEY_F19,
111  F20 = GLFW_KEY_F20,
112  F21 = GLFW_KEY_F21,
113  F22 = GLFW_KEY_F22,
114  F23 = GLFW_KEY_F23,
115  F24 = GLFW_KEY_F24,
116  F25 = GLFW_KEY_F25,
117  KP_0 = GLFW_KEY_KP_0,
118  KP_1 = GLFW_KEY_KP_1,
119  KP_2 = GLFW_KEY_KP_2,
120  KP_3 = GLFW_KEY_KP_3,
121  KP_4 = GLFW_KEY_KP_4,
122  KP_5 = GLFW_KEY_KP_5,
123  KP_6 = GLFW_KEY_KP_6,
124  KP_7 = GLFW_KEY_KP_7,
125  KP_8 = GLFW_KEY_KP_8,
126  KP_9 = GLFW_KEY_KP_9,
127  KP_DECIMAL = GLFW_KEY_KP_DECIMAL,
128  KP_DIVIDE = GLFW_KEY_KP_DIVIDE,
129  KP_MULTIPLY = GLFW_KEY_KP_MULTIPLY,
130  KP_SUBTRACT = GLFW_KEY_KP_SUBTRACT,
131  KP_ADD = GLFW_KEY_KP_ADD,
132  KP_ENTER = GLFW_KEY_KP_ENTER,
133  KP_EQUAL = GLFW_KEY_KP_EQUAL,
134  LEFT_SHIFT = GLFW_KEY_LEFT_SHIFT,
135  LEFT_CONTROL = GLFW_KEY_LEFT_CONTROL,
136  LEFT_ALT = GLFW_KEY_LEFT_ALT,
137  LEFT_SUPER = GLFW_KEY_LEFT_SUPER,
138  RIGHT_SHIFT = GLFW_KEY_RIGHT_SHIFT,
139  RIGHT_CONTROL = GLFW_KEY_RIGHT_CONTROL,
140  RIGHT_ALT = GLFW_KEY_RIGHT_ALT,
141  RIGHT_SUPER = GLFW_KEY_RIGHT_SUPER,
142  MENU = GLFW_KEY_MENU,
143  MOUSE_BUTTON_1 = GLFW_MOUSE_BUTTON_1,
144  MOUSE_BUTTON_2 = GLFW_MOUSE_BUTTON_2,
145  MOUSE_BUTTON_3 = GLFW_MOUSE_BUTTON_3,
146  MOUSE_BUTTON_4 = GLFW_MOUSE_BUTTON_4,
147  MOUSE_BUTTON_5 = GLFW_MOUSE_BUTTON_5,
148  MOUSE_BUTTON_6 = GLFW_MOUSE_BUTTON_6,
149  MOUSE_BUTTON_7 = GLFW_MOUSE_BUTTON_7,
150  MOUSE_BUTTON_8 = GLFW_MOUSE_BUTTON_8,
151  MOUSE_BUTTON_LAST = GLFW_MOUSE_BUTTON_LAST,
152  MOUSE_BUTTON_LEFT = GLFW_MOUSE_BUTTON_LEFT,
153  MOUSE_BUTTON_RIGHT = GLFW_MOUSE_BUTTON_RIGHT,
154  MOUSE_BUTTON_MIDDLE = GLFW_MOUSE_BUTTON_MIDDLE
155  };
156 
157  // Forward decelerations of friend functions
159  static void glfw_initialisation_error(int error, const char* description);
160  static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
161  static void window_resize_callback(GLFWwindow* window, int width, int height);
162  static void mouse_button_callback(GLFWwindow* window, int button, int action, int mods);
163  static void mouse_position_callback(GLFWwindow* window, double xpos, double ypos);
164 
168  class Window
169  {
170  public:
174  glm::vec4 backgroundColor;
175  float deltaMouseX;
176  float deltaMouseY;
177  private:
178  // FIXME: probably use a custom string class later instead of std::string or char*(character pointer)'s
179  const char* m_Title;
180  int m_Width, m_Height;
181  GLFWwindow* m_Window;
182  bool m_Closed;
183  bool m_HeldKeys[MAX_KEYS];
184  bool m_PressedKeys[MAX_KEYS];
185  bool m_ReleasedKeys[MAX_KEYS];
186  bool m_HeldMouseButtons[MAX_BUTTONS];
187  bool m_PressedMouseButtons[MAX_BUTTONS];
188  bool m_ReleasedMouseButtons[MAX_BUTTONS];
189  double m_MouseX = 0;
190  double m_MouseY = 0;
191  bool firstMouse;
192  float lastMouseX;
193  float lastMouseY;
194  public:
199  Window(const char *title, int width, int height);
200 
201  ~Window();
202 
206  void clear() const;
207 
211  void update() const;
212 
215  bool closed() const;
216 
219  inline int getWidth() const { return m_Width; }
220 
223  inline int getHeight() const { return m_Height; }
224 
227  inline GLFWwindow* getGLFWwindow() const { return m_Window; }
228 
233  bool isKeyPressed(unsigned int keycode);
234 
239  bool isKeyReleased(unsigned int keycode);
240 
246  bool isKeyHeld(unsigned int keycode) const;
247 
252  bool isMouseButtonPressed(unsigned int button);
253 
254  // TODO: To be implemented
255  bool isMouseButtonReleased(unsigned int button);
256 
262  bool isMouseButtonHeld(unsigned int button) const;
263 
264  // TODO: Make the function return a Vec2 once the engine specific vector math API is added to the namespace
269  void getMousePosition(double& x, double& y) const;
270  private :
271  bool init();
272  // TODO: Add reference to the GLFW keycode Enums list for the key parameter and also for the mods param(add the reference to the bit fields)
273 
282  friend void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
283 
290  friend void window_resize_callback(GLFWwindow* window, int width, int height);
291 
292  // TODO: Add reference to the GLFW mouse_button Enums list for the button parameter and also for the mods param(add the reference to the bit fields)
300  friend void mouse_button_callback(GLFWwindow* window, int button, int action, int mods);
301 
307  friend void mouse_position_callback(GLFWwindow* window, double xpos, double ypos);
308  };
309 
310 } }
~Window()
Definition: Window.cpp:29
Definition: Window.h:108
Definition: Window.h:53
void update() const
Updates the window.
Definition: Window.cpp:148
void getMousePosition(double &x, double &y) const
Gets the current position of the mouse in Screen Space Coordinates.
Definition: Window.cpp:136
Definition: Window.h:44
void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
Definition: Window.cpp:170
Definition: Window.h:137
Definition: Window.h:72
Definition: Window.h:125
Definition: Window.h:32
Definition: Window.h:77
Definition: Window.h:71
friend void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
A callback function called when the windows registers a key press from the Keyboard.
Definition: Window.cpp:170
Definition: Window.h:85
Definition: Window.h:79
Definition: Window.h:57
Definition: Window.h:69
Definition: Window.h:41
Definition: Window.h:26
Definition: Window.h:24
Definition: Window.h:61
void mouse_button_callback(GLFWwindow *window, int button, int action, int mods)
Definition: Window.cpp:195
Definition: Window.h:126
Definition: Window.h:82
bool isMouseButtonHeld(unsigned int button) const
Tells if a particular key on the Mouse button is being held or not.
Definition: Window.cpp:127
Definition: Window.h:122
Definition: Window.h:111
Definition: Window.h:119
Definition: Window.h:140
Definition: Window.h:50
Definition: Window.h:98
Definition: Window.h:58
Definition: Window.h:103
Definition: Window.h:110
Definition: Window.h:28
void mouse_position_callback(GLFWwindow *window, double xpos, double ypos)
Definition: Window.cpp:204
Definition: Window.h:84
int getWidth() const
Gets the current width of the window.
Definition: Window.h:219
Definition: Window.h:70
Definition: Window.h:118
Definition: Window.h:136
Definition: Window.h:120
Definition: Window.h:40
Definition: Window.h:52
Definition: Window.h:109
Definition: Window.h:138
Definition: Window.h:144
Definition: Window.h:106
Definition: Window.h:80
Definition: Window.h:65
Definition: Window.h:87
int getHeight() const
Gets the current height of the window.
Definition: Window.h:223
Definition: Window.h:63
Definition: Window.h:116
Definition: Window.h:150
Definition: Window.h:74
Definition: Window.h:39
friend void mouse_position_callback(GLFWwindow *window, double xpos, double ypos)
A callback function called when the windows registers a change in the mouse position.
Definition: Window.cpp:204
Definition: Window.h:127
Definition: Window.h:141
Definition: Window.h:47
Definition: Window.h:55
Keys
All Input Buttons mapped to a more understandable mapping from GLFW.
Definition: Window.h:20
bool closed() const
Indicates the current state of the Window.
Definition: Window.cpp:160
Definition: Window.h:27
friend void mouse_button_callback(GLFWwindow *window, int button, int action, int mods)
A callback function called when the windows registers a button press from the Mouse.
Definition: Window.cpp:195
Definition: Window.h:22
Definition: Window.h:56
Definition: Window.h:88
Definition: Window.h:36
Definition: Window.h:30
Definition: audioclip.cpp:3
Definition: Window.h:139
float deltaMouseX
Definition: Window.h:175
Definition: Window.h:66
Definition: Window.h:145
Definition: Window.h:89
Definition: Window.h:64
Definition: Window.h:92
Definition: Window.h:91
friend void window_resize_callback(GLFWwindow *window, int width, int height)
A callback function called when the windows resizes.
Definition: Window.cpp:187
Definition: Window.h:83
Definition: Window.h:123
Definition: Window.h:133
Definition: Window.h:99
Definition: Window.h:81
The class responsible for Window Creation.
Definition: Window.h:168
glm::vec4 backgroundColor
The background color of the Window.
Definition: Window.h:174
Definition: Window.h:96
Definition: Window.h:31
#define MAX_BUTTONS
Definition: Window.h:17
Definition: Window.h:51
Definition: Window.h:129
Definition: Window.h:54
Definition: Window.h:143
Definition: Window.h:93
Definition: Window.h:95
Definition: Window.h:101
Definition: Window.h:38
Definition: Window.h:60
Definition: Window.h:121
bool isMouseButtonPressed(unsigned int button)
Tells if a particular key on the Mouse button was pressed or not.
Definition: Window.cpp:105
Definition: Window.h:131
Definition: Window.h:75
Definition: Window.h:112
Definition: Window.h:148
Definition: Window.h:23
Definition: Window.h:42
void window_resize_callback(GLFWwindow *window, int width, int height)
Definition: Window.cpp:187
Definition: Window.h:100
Definition: Window.h:113
Definition: Window.h:104
Definition: Window.h:107
Definition: Window.h:94
Definition: Window.h:97
Definition: Window.h:45
Definition: Window.h:105
Definition: Window.h:68
Definition: Window.h:62
Definition: Window.h:73
Definition: Window.h:117
void glfw_initialisation_error(int error, const char *description)
Input related GLFW callback functions.
Definition: Window.cpp:165
Definition: Window.h:76
Window(const char *title, int width, int height)
Creates a Window by initializing GLFW.
Definition: Window.cpp:5
Definition: Window.h:90
Definition: Window.h:78
Definition: Window.h:33
Definition: Window.h:134
float deltaMouseY
Definition: Window.h:176
Definition: Window.h:34
Definition: Window.h:35
Definition: Window.h:142
Definition: Window.h:128
Definition: Window.h:48
Definition: Window.h:67
Definition: Window.h:86
GLFWwindow * getGLFWwindow() const
Gets the current window&#39;s pointer to it&#39;s native object.
Definition: Window.h:227
Definition: Window.h:59
Definition: Window.h:43
Definition: Window.h:149
#define MAX_KEYS
Definition: Window.h:16
Definition: Window.h:124
Definition: Window.h:46
void clear() const
Clears the window screen blank.
Definition: Window.cpp:142
Definition: Window.h:102
Definition: Window.h:114
Definition: Window.h:132
bool isMouseButtonReleased(unsigned int button)
Definition: Window.cpp:116
Definition: Window.h:25
bool isKeyPressed(unsigned int keycode)
Tells if a particular key on the keyboard was pressed or not.
Definition: Window.cpp:74
Definition: Window.h:29
Definition: Window.h:147
bool isKeyHeld(unsigned int keycode) const
Tells if a particular key on the keyboard is being held or not.
Definition: Window.cpp:96
Definition: Window.h:130
Definition: Window.h:146
Definition: Window.h:135
Definition: Window.h:37
bool isKeyReleased(unsigned int keycode)
Tells if a particular key on the keyboard was released or not.
Definition: Window.cpp:85
Definition: Window.h:115
Definition: Window.h:49