aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoursoir <chat@joursoir.net>2021-03-26 20:30:13 +0000
committerJoursoir <chat@joursoir.net>2021-03-26 20:30:13 +0000
commit7f7123ef4ca5e37da45907eeb7f29ed15e535dce (patch)
treec1ac4e1ac3af95aba2e0b53ec3b37748ea932f84
parentaee5bf7802f112111961faa824deb6f97ef80203 (diff)
downloadspace-simulator-7f7123ef4ca5e37da45907eeb7f29ed15e535dce.tar.gz
space-simulator-7f7123ef4ca5e37da45907eeb7f29ed15e535dce.tar.bz2
space-simulator-7f7123ef4ca5e37da45907eeb7f29ed15e535dce.zip
add events manager
-rw-r--r--src/window/EventsManager.cpp127
-rw-r--r--src/window/EventsManager.hpp93
2 files changed, 220 insertions, 0 deletions
diff --git a/src/window/EventsManager.cpp b/src/window/EventsManager.cpp
new file mode 100644
index 0000000..ddf2901
--- /dev/null
+++ b/src/window/EventsManager.cpp
@@ -0,0 +1,127 @@
+#include <stdio.h>
+#include <GLFW/glfw3.h>
+
+#include "Window.hpp"
+#include "EventsManager.hpp"
+#include "Events.hpp"
+
+struct lStorage EventsManager::listeners(2);
+
+void EventsManager::PollEvents()
+{
+ int max = listeners.max_n, i;
+ for(i = 0; i < max; i++) {
+ if(!listeners.list[i])
+ continue;
+
+ listeners.list[i]->NextFrame();
+ }
+ glfwPollEvents();
+}
+
+void EventsManager::AddListener(Events *e, int listen)
+{
+ if(listen <= 0)
+ return;
+
+ GLFWwindow *win = e->GetWindow()->GetWin();
+ if(listen & EM_LISTEN_KEY)
+ glfwSetKeyCallback(win, key_callback);
+ if(listen & EM_LISTEN_CURSOR)
+ glfwSetCursorPosCallback(win, cursor_pos_callback);
+ if(listen & EM_LISTEN_MBUTTON)
+ glfwSetMouseButtonCallback(win, mouse_button_callback);
+ if(listen & EM_LISTEN_WINDOWSIZE)
+ glfwSetWindowSizeCallback(win, window_size_callback);
+ if(listen & EM_LISTEN_MSCROLL)
+ glfwSetScrollCallback(win, scroll_callback);
+
+ listeners.Add(e, win, listen);
+}
+
+//////////////////////////////////////////////////////////////////////////////
+
+void EventsManager::key_callback(GLFWwindow *window, int key, int scancode,
+ int action, int mode)
+{
+ int max = listeners.max_n, i;
+ for(i = 0; i < max; i++) {
+ if(!listeners.list[i])
+ continue;
+ if(!(listeners.listen_events[i] & EM_LISTEN_KEY))
+ continue;
+ if(listeners.win[i] != window)
+ continue;
+
+ //fprintf(stderr, "key_callback\n");
+ listeners.list[i]->KeyHandle(key, scancode, action, mode);
+ }
+}
+
+void EventsManager::cursor_pos_callback(GLFWwindow *window, double xpos,
+ double ypos)
+{
+ int max = listeners.max_n, i;
+ for(i = 0; i < max; i++) {
+ if(!listeners.list[i])
+ continue;
+ if(!(listeners.listen_events[i] & EM_LISTEN_CURSOR))
+ continue;
+ if(listeners.win[i] != window)
+ continue;
+
+ //fprintf(stderr, "cursor_pos_callback\n");
+ listeners.list[i]->CursorPosHandle(xpos, ypos);
+ }
+}
+
+void EventsManager::mouse_button_callback(GLFWwindow *window, int button,
+ int action, int mode)
+{
+ int max = listeners.max_n, i;
+ for(i = 0; i < max; i++) {
+ if(!listeners.list[i])
+ continue;
+ if(!(listeners.listen_events[i] & EM_LISTEN_MBUTTON))
+ continue;
+ if(listeners.win[i] != window)
+ continue;
+
+ //fprintf(stderr, "mouse_button_callback\n");
+ listeners.list[i]->MouseButtonHandle(button, action, mode);
+ }
+}
+
+void EventsManager::window_size_callback(GLFWwindow* window,
+ int width, int height)
+{
+ int max = listeners.max_n, i;
+ for(i = 0; i < max; i++) {
+ if(!listeners.list[i])
+ continue;
+ if(!(listeners.listen_events[i] & EM_LISTEN_WINDOWSIZE))
+ continue;
+ if(listeners.win[i] != window)
+ continue;
+
+ //fprintf(stderr, "window_size_callback\n");
+ listeners.list[i]->WindowResizeHandle(width, height);
+ }
+}
+
+void EventsManager::scroll_callback(GLFWwindow* window,
+ double xoffset, double yoffset)
+{
+ int max = listeners.max_n, i;
+ for(i = 0; i < max; i++) {
+ if(!listeners.list[i])
+ continue;
+ if(!(listeners.listen_events[i] & EM_LISTEN_MSCROLL))
+ continue;
+ if(listeners.win[i] != window)
+ continue;
+
+ //fprintf(stderr, "scroll_callback\n");
+ listeners.list[i]->MouseScrollHandle(xoffset, yoffset);
+ }
+}
diff --git a/src/window/EventsManager.hpp b/src/window/EventsManager.hpp
new file mode 100644
index 0000000..29a4524
--- /dev/null
+++ b/src/window/EventsManager.hpp
@@ -0,0 +1,93 @@
+#ifndef ENGINE_EVENTSMANAGER_H
+#define ENGINE_EVENTSMANAGER_H
+
+class Events;
+
+enum em_listen {
+ EM_LISTEN_KEY = 1,
+ EM_LISTEN_CURSOR = 2,
+ EM_LISTEN_MBUTTON = 4,
+ EM_LISTEN_WINDOWSIZE = 8,
+ EM_LISTEN_MSCROLL = 16
+};
+
+struct lStorage {
+ Events **list;
+ GLFWwindow **win;
+ int *listen_events;
+ int max_n;
+
+ lStorage(int n) : max_n(n)
+ {
+ int i;
+ list = new Events*[n];
+ win = new GLFWwindow*[n];
+ listen_events = new int[n];
+ for(i = 0; i < n; i++) {
+ list[i] = 0;
+ win[i] = 0;
+ listen_events[i] = 0;
+ }
+ }
+
+ ~lStorage()
+ {
+ delete[] list;
+ delete[] win;
+ delete[] listen_events;
+ }
+
+ void Add(Events *e, GLFWwindow *w, int listen)
+ {
+ int i;
+ for(i = 0; i < max_n; i++) {
+ if(list[i] == 0) {
+ list[i] = e;
+ win[i] = w;
+ listen_events[i] = listen;
+ break;
+ }
+ }
+
+ // realloc memory:
+ Events **tmp_list = list;
+ GLFWwindow **tmp_win = win;
+ int *tmp_levents = listen_events;
+
+ list = new Events*[max_n + 1];
+ win = new GLFWwindow*[max_n + 1];
+ listen_events = new int[max_n + 1];
+
+ for(i = 0; i < max_n; i++) {
+ list[i] = tmp_list[i];
+ win[i] = tmp_win[i];
+ listen_events[i] = tmp_levents[i];
+ }
+ list[max_n] = e;
+ win[max_n] = w;
+ tmp_levents[max_n] = listen;
+
+ delete[] tmp_list;
+ delete[] tmp_win;
+ delete[] tmp_levents;
+ }
+};
+
+class EventsManager {
+ static struct lStorage listeners;
+
+public:
+ static void PollEvents();
+ static void AddListener(Events *e, int listen);
+
+private:
+ EventsManager() { }
+
+ static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mode);
+ static void cursor_pos_callback(GLFWwindow *window, double xpos, double ypos);
+ static void mouse_button_callback(GLFWwindow *window, int button, int action, int mode);
+ static void window_size_callback(GLFWwindow *window, int width, int height);
+ static void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
+};
+
+#endif /* ENGINE_EVENTSMANAGER_H */