aboutsummaryrefslogtreecommitdiffstats
path: root/src/window
diff options
context:
space:
mode:
authorJoursoir <chat@joursoir.net>2021-03-21 12:55:31 +0000
committerJoursoir <chat@joursoir.net>2021-03-21 12:55:31 +0000
commit6eec70ec2ad54168442eca4f15030e7e6294cfb7 (patch)
tree0604d7c5d139643e7ba61b3d98e286c0f3188715 /src/window
downloadspace-simulator-6eec70ec2ad54168442eca4f15030e7e6294cfb7.tar.gz
space-simulator-6eec70ec2ad54168442eca4f15030e7e6294cfb7.tar.bz2
space-simulator-6eec70ec2ad54168442eca4f15030e7e6294cfb7.zip
add events subsystem
Diffstat (limited to 'src/window')
-rw-r--r--src/window/Events.cpp85
-rw-r--r--src/window/Events.hpp33
2 files changed, 118 insertions, 0 deletions
diff --git a/src/window/Events.cpp b/src/window/Events.cpp
new file mode 100644
index 0000000..09f6acd
--- /dev/null
+++ b/src/window/Events.cpp
@@ -0,0 +1,85 @@
+#include <string.h>
+#include <GL/glew.h>
+#include <GLFW/glfw3.h>
+
+#include "Events.hpp"
+
+Events::Events() : cur_frame(0), delta_x(0.0), delta_y(0.0),
+ x(0.0), y(0.0), cursor_locked(false)
+{
+ keys = new bool[MAX_KEYS];
+ memset(keys, false, MAX_KEYS * sizeof(bool));
+ frames = new unsigned int[MAX_KEYS];
+ memset(frames, 0, MAX_KEYS * sizeof(unsigned int));
+}
+
+Events::~Events()
+{
+ delete[] keys;
+ delete[] frames;
+}
+
+bool Events::Pressed(int keycode)
+{
+ if(keycode < 0 || keycode >= MOUSE_BUTTONS)
+ return false;
+ return keys[keycode];
+}
+
+bool Events::Jpressed(int keycode)
+{
+ if(keycode < 0 || keycode >= MOUSE_BUTTONS)
+ return false;
+ return keys[keycode] && (frames[keycode] == cur_frame);
+}
+
+bool Events::Clicked(int button)
+{
+ return keys[MOUSE_BUTTONS + button];
+}
+
+bool Events::Jclicked(int button)
+{
+ int idx = MOUSE_BUTTONS + button;
+ return keys[idx] && (frames[idx] == cur_frame);
+}
+
+void Events::NextFrame()
+{
+ cur_frame++;
+}
+
+//////////////////////////////////////////////////////////////////////////////
+
+void Events::KeyHandle(int key, int scancode, int action, int mode)
+{
+ if(action == GLFW_PRESS) {
+ keys[key] = true;
+ frames[key] = cur_frame;
+ }
+ else if(action == GLFW_RELEASE) {
+ keys[key] = false;
+ frames[key] = cur_frame;
+ }
+}
+
+void Events::CursorPosHandle(double xpos, double ypos)
+{
+ delta_x = xpos - x;
+ delta_y = ypos - y;
+
+ x = xpos;
+ y = ypos;
+}
+
+void Events::MouseButtonHandle(int button, int action, int mode)
+{
+ if(action == GLFW_PRESS) {
+ keys[MOUSE_BUTTONS + button] = true;
+ frames[MOUSE_BUTTONS + button] = cur_frame;
+ }
+ else if (action == GLFW_RELEASE){
+ keys[MOUSE_BUTTONS + button] = false;
+ frames[MOUSE_BUTTONS + button] = cur_frame;
+ }
+}
diff --git a/src/window/Events.hpp b/src/window/Events.hpp
new file mode 100644
index 0000000..6914dca
--- /dev/null
+++ b/src/window/Events.hpp
@@ -0,0 +1,33 @@
+#ifndef ENGINE_EVENTS_H
+#define ENGINE_EVENTS_H
+
+#define MAX_KEYS GLFW_KEY_LAST + 1 + GLFW_MOUSE_BUTTON_LAST + 1
+#define MOUSE_BUTTONS GLFW_KEY_LAST + 1 /* when start mouse buttons */
+
+class Events {
+ bool *keys;
+ unsigned int *frames;
+ unsigned int cur_frame;
+ bool update_frame;
+ float delta_x;
+ float delta_y;
+ float x;
+ float y;
+ bool cursor_locked;
+
+public:
+ Events();
+ ~Events();
+
+ bool Pressed(int keycode);
+ bool Jpressed(int keycode);
+ bool Clicked(int button);
+ bool Jclicked(int button);
+
+ void NextFrame();
+ void KeyHandle(int key, int scancode, int action, int mode);
+ void CursorPosHandle(double xpos, double ypos);
+ void MouseButtonHandle(int button, int action, int mode);
+};
+
+#endif /* ENGINE_EVENTS_H */