#include "pch.hpp" #ifdef WINDOW_GLFW #undef WINDOW_GLFW #include "utils/Window/WindowEvents.h" #include namespace Archimedes { class Window; class WindowGLFW { public: 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; }); if(!glfwInit()) { std::cout << "glfwInit failed!\n"; std::abort(); } w = glfwCreateWindow(640, 480, "Archimedes", NULL, NULL); if(!w) { glfwTerminate(); std::abort(); } glfwMakeContextCurrent(w); glfwSwapInterval(1); glfwSetWindowUserPointer(w, &data); glfwSetWindowSizeCallback(w, [](GLFWwindow* window, int w, int h){ WindowData& d = *(WindowData*) glfwGetWindowUserPointer(window); d.sendEvent(new ResizeWindowEvent(w, h)); }); glfwSetWindowCloseCallback(w, [](GLFWwindow* window){ WindowData& d = *(WindowData*) glfwGetWindowUserPointer(window); d.sendEvent(new CloseWindowEvent(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 KeyPressedWindowEvent(key, 0)); break; case GLFW_RELEASE: d.sendEvent(new KeyReleasedWindowEvent(key)); break; case GLFW_REPEAT: d.sendEvent(new KeyPressedWindowEvent(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 MouseButtonPressedWindowEvent(button)); break; case GLFW_RELEASE: d.sendEvent(new MouseButtonReleasedWindowEvent(button)); break; } }); glfwSetScrollCallback(w, [](GLFWwindow* window, double dx, double dy){ WindowData& d = *(WindowData*) glfwGetWindowUserPointer(window); d.sendEvent(new ScrollWindowEvent(dx, dy)); }); glfwSetCursorPosCallback(w, [](GLFWwindow* window, double dx, double dy){ WindowData& d = *(WindowData*) glfwGetWindowUserPointer(window); d.sendEvent(new MouseMovedWindowEvent(dx, dy)); }); } ~WindowGLFW() { glfwTerminate(); } bool shouldClose() { return glfwWindowShouldClose(w); } void doFrame() { restoreContext(); glfwSwapBuffers(w); } void pollEvents() { glfwPollEvents(); } void restoreContext() { glfwMakeContextCurrent(w); } void getSize(int& w, int& h) { glfwGetFramebufferSize(this->w, &w, &h); } GLFWwindow* getWindow() { return w; } WindowData data; private: GLFWwindow* w; }; typedef WindowGLFW WindowImpl; } #endif