From 7f7123ef4ca5e37da45907eeb7f29ed15e535dce Mon Sep 17 00:00:00 2001 From: Joursoir Date: Fri, 26 Mar 2021 20:30:13 +0000 Subject: add events manager --- src/window/EventsManager.cpp | 127 +++++++++++++++++++++++++++++++++++++++++++ src/window/EventsManager.hpp | 93 +++++++++++++++++++++++++++++++ 2 files changed, 220 insertions(+) create mode 100644 src/window/EventsManager.cpp create mode 100644 src/window/EventsManager.hpp 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 +#include + +#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 */ -- cgit v1.2.3-18-g5258