diff --git a/include/utils/Events/Event.h b/include/utils/Events/Event.h index e1a0bc1..cfe29e3 100644 --- a/include/utils/Events/Event.h +++ b/include/utils/Events/Event.h @@ -9,7 +9,9 @@ namespace Archimedes { public: - virtual ~Event() {} + virtual ~Event() { + std::cout << (std::string)*this; + } virtual operator std::string() const = 0; diff --git a/include/utils/Window/Window.h b/include/utils/Window/Window.h index 32dd04c..b93a235 100644 --- a/include/utils/Window/Window.h +++ b/include/utils/Window/Window.h @@ -1,6 +1,7 @@ #ifndef WINDOW_H #define WINDOW_H +#include "WindowEvents.h" #include "utils/Renderer/Renderer.h" #include "WindowGLFW/WindowGLFW.h" @@ -11,7 +12,7 @@ namespace Archimedes { public: - Window() {} + Window(const std::function& sendEventFn) : window(this, sendEventFn) {} ~Window() {} @@ -19,7 +20,7 @@ namespace Archimedes { void doFrame() { - window.pollEvents(); + //window.pollEvents(); window.getSize(renderer->w, renderer->h); diff --git a/include/utils/Window/WindowEvents.h b/include/utils/Window/WindowEvents.h new file mode 100644 index 0000000..5abf794 --- /dev/null +++ b/include/utils/Window/WindowEvents.h @@ -0,0 +1,209 @@ +#ifndef WINDOWEVENTS_H +#define WINDOWEVENTS_H + +#include "utils/Events/Event.h" + +//implimentation independent events + +namespace Archimedes { + + class Window; + + struct WindowData { + Window* window; + std::list eventList; + std::function sendEvent; + }; + + class WindowResizeEvent : public Event { + + public: + + WindowResizeEvent() : width(0), height(0) {} + + WindowResizeEvent(int w, int h) : width(w), height(h) {} + + ~WindowResizeEvent() { + + } + + operator std::string() const { return "Archimedes::WindowResizeEvent"; } + + int width, height; + + }; + + class WindowCloseEvent : public Event { + + public: + + WindowCloseEvent() : window(nullptr) {} + + WindowCloseEvent(const Window* w) : window(w) {} + + ~WindowCloseEvent() { + + } + + operator std::string() const { return "Archimedes::WindowCloseEvent"; } + + const Window* window; + }; + + class WindowKeyPressedEvent : public Event { + + public: + + WindowKeyPressedEvent() : key(0), repeat(0) {} + + WindowKeyPressedEvent(unsigned int k, unsigned int r) : key(k), repeat(r) {} + + ~WindowKeyPressedEvent() { + + } + + operator std::string() const { return "Archimedes::WindowKeyPressedEvent"; } + + unsigned int key; + unsigned int repeat; + }; + + class WindowKeyReleasedEvent : public Event { + + public: + + WindowKeyReleasedEvent() : key(0) {} + + WindowKeyReleasedEvent(unsigned int k) : key(k) {} + + ~WindowKeyReleasedEvent() { + + } + + operator std::string() const { return "Archimedes::WindowKeyReleasedEvent"; } + + unsigned int key; + }; + + class WindowMouseButtonPressedEvent : public Event { + + public: + + WindowMouseButtonPressedEvent() : button(0) {} + + WindowMouseButtonPressedEvent(unsigned int b) : button(b) {} + + ~WindowMouseButtonPressedEvent() { + + } + + operator std::string() const { return "Archimedes::WindowMouseButtonPressedEvent"; } + + unsigned int button; + }; + + class WindowMouseButtonReleasedEvent : public Event { + + public: + + WindowMouseButtonReleasedEvent() : button(0) {} + + WindowMouseButtonReleasedEvent(unsigned int b) : button(b) {} + + ~WindowMouseButtonReleasedEvent() { + + } + + operator std::string() const { return "Archimedes::WindowMouseButtonReleasedEvent"; } + + unsigned int button; + }; + + class WindowScrollEvent : public Event { + + public: + + WindowScrollEvent() : dx(0), dy(0) {} + + WindowScrollEvent(double x, double y) : dx(x), dy(y) {} + + ~WindowScrollEvent() { + + } + + operator std::string() const { return "Archimedes::WindowScrollEvent"; } + + double dx, dy; + }; + + class WindowMouseMovedEvent : public Event { + + public: + + WindowMouseMovedEvent() : x(0), y(0) {} + + WindowMouseMovedEvent(int x, int y) : x(x), y(y) {} + + ~WindowMouseMovedEvent() { + + } + + operator std::string() const { return "Archimedes::WindowMouseMovedEvent"; } + + int x, y; + }; + + class WindowFocusedEvent : public Event { + + public: + + WindowFocusedEvent() : window(nullptr) {} + + WindowFocusedEvent(const Window* w) : window(w) {} + + ~WindowFocusedEvent() { + + } + + operator std::string() const { return "Archimedes::WindowFocusedEvent"; } + + const Window* window; + }; + + class WindowFocusLostEvent : public Event { + + public: + + WindowFocusLostEvent() : window(nullptr) {} + + WindowFocusLostEvent(const Window* w) : window(w) {} + + ~WindowFocusLostEvent() { + + } + + operator std::string() const { return "Archimedes::WindowFocusLostEvent"; } + + const Window* window; + }; + + class WindowMovedEvent : public Event { + + public: + + WindowMovedEvent() : x(0), y(0) {} + + WindowMovedEvent(int x, int y) : x(x), y(y) {} + + ~WindowMovedEvent() { + + } + + operator std::string() const { return "Archimedes::WindowMovedEvent"; } + + int x, y; + }; + +} + +#endif diff --git a/include/utils/Window/WindowGLFW/WindowGLFW.h b/include/utils/Window/WindowGLFW/WindowGLFW.h index f8b92fc..de7d9b4 100644 --- a/include/utils/Window/WindowGLFW/WindowGLFW.h +++ b/include/utils/Window/WindowGLFW/WindowGLFW.h @@ -3,16 +3,23 @@ #ifdef WINDOW_GLFW #undef WINDOW_GLFW +#include "utils/Window/WindowEvents.h" + #include namespace Archimedes { + class Window; + class WindowGLFW { public: - WindowGLFW() { + WindowGLFW(Window* p, const std::function& sendEvent) { + + data.window = p; + data.sendEvent = sendEvent; glfwSetErrorCallback([](int e, const char* m){ std::cout << "GLFW Error: " << m << std::endl; @@ -31,6 +38,60 @@ namespace Archimedes { glfwMakeContextCurrent(w); glfwSwapInterval(1); + glfwSetWindowUserPointer(w, &data); + + glfwSetWindowSizeCallback(w, [](GLFWwindow* window, int w, int h){ + WindowData& d = *(WindowData*) glfwGetWindowUserPointer(window); + + d.sendEvent(new WindowResizeEvent(w, h)); + }); + + glfwSetWindowCloseCallback(w, [](GLFWwindow* window){ + WindowData& d = *(WindowData*) glfwGetWindowUserPointer(window); + + d.sendEvent(new WindowCloseEvent(d.window)); + }); + + glfwSetKeyCallback(w, [](GLFWwindow* window, int key, int scancode, int action, int mods){ + WindowData& d = *(WindowData*) glfwGetWindowUserPointer(window); + + switch(action) { + case GLFW_PRESS: + d.sendEvent(new WindowKeyPressedEvent(key, 0)); + break; + case GLFW_RELEASE: + d.sendEvent(new WindowKeyReleasedEvent(key)); + break; + case GLFW_REPEAT: + d.sendEvent(new WindowKeyPressedEvent(key, 1)); + break; + } + }); + + glfwSetMouseButtonCallback(w, [](GLFWwindow* window, int button, int action, int mods){ + WindowData& d = *(WindowData*) glfwGetWindowUserPointer(window); + + switch(action) { + case GLFW_PRESS: + d.sendEvent(new WindowMouseButtonPressedEvent(button)); + break; + case GLFW_RELEASE: + d.sendEvent(new WindowMouseButtonReleasedEvent(button)); + break; + } + }); + + glfwSetScrollCallback(w, [](GLFWwindow* window, double dx, double dy){ + WindowData& d = *(WindowData*) glfwGetWindowUserPointer(window); + + d.sendEvent(new WindowScrollEvent(dx, dy)); + }); + + glfwSetCursorPosCallback(w, [](GLFWwindow* window, double dx, double dy){ + WindowData& d = *(WindowData*) glfwGetWindowUserPointer(window); + + d.sendEvent(new WindowMouseMovedEvent(dx, dy)); + }); } ~WindowGLFW() { @@ -54,8 +115,11 @@ namespace Archimedes { GLFWwindow* getWindow() { return w; } + WindowData data; + private: GLFWwindow* w; + }; typedef WindowGLFW WindowImpl; diff --git a/modules/WindowModule/src/WindowModule.cpp b/modules/WindowModule/src/WindowModule.cpp index 9d5e8c5..ad26fba 100644 --- a/modules/WindowModule/src/WindowModule.cpp +++ b/modules/WindowModule/src/WindowModule.cpp @@ -14,7 +14,9 @@ WindowModule::~WindowModule() { void WindowModule::onLoad() { - window = new Archimedes::Window(); + window = new Archimedes::Window([this](Archimedes::Event* e){ + app->emitEvent(e); + }); renderer = new Archimedes::Renderer(); @@ -34,6 +36,12 @@ void WindowModule::run() { app->end(); } + for(auto e : window->getWindowImpl().data.eventList) { + app->emitEvent(e); + } + + window->getWindowImpl().data.eventList.clear(); + window->doFrame(); }