Files
Archimedes/include/utils/Window/WindowImpl/WindowGLFW/WindowGLFW.h
2026-02-08 11:57:18 -06:00

139 lines
4.4 KiB
C++

#if WINDOW == 1
#ifndef WINDOW_GLFW
#define WINDOW_GLFW
#include "pch.hpp"
#include "utils/Window/Window.h"
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
namespace Archimedes {
class WindowGLFW : public Window {
public:
WindowGLFW(const std::function<void(Event*)>& sendEvent) : Window(sendEvent) {
data.window = this;
data.sendEvent = sendEvent;
glfwSetErrorCallback([](int e, const char* m){
std::cout << "GLFW Error " << e << ": " << m << std::endl;
});
if(!glfwInit()) {
std::cout << "glfwInit failed!\n";
std::abort();
}
#if RENDRER == 1
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#endif
window = glfwCreateWindow(640, 480, "Archimedes", NULL, NULL);
if(!w) {
std::cout << "glfwCreateWindow failed!\n";
glfwTerminate();
std::abort();
}
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
glfwSetWindowUserPointer(window, &data);
glfwSetWindowSizeCallback(window, [](GLFWwindow* window, int w, int h){
WindowData& d = *(WindowData*) glfwGetWindowUserPointer(window);
d.sendEvent(new ResizeWindowEvent(w, h));
});
glfwSetWindowCloseCallback(window, [](GLFWwindow* window){
WindowData& d = *(WindowData*) glfwGetWindowUserPointer(window);
d.sendEvent(new CloseWindowEvent(d.window));
});
glfwSetKeyCallback(window, [](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(window, [](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(window, [](GLFWwindow* window, double dx, double dy){
WindowData& d = *(WindowData*) glfwGetWindowUserPointer(window);
d.sendEvent(new ScrollWindowEvent(dx, dy));
});
glfwSetCursorPosCallback(window, [](GLFWwindow* window, double dx, double dy){
WindowData& d = *(WindowData*) glfwGetWindowUserPointer(window);
d.sendEvent(new MouseMovedWindowEvent(dx, dy));
});
}
~WindowGLFW() {
glfwTerminate();
}
void swapBuffers() override { restoreContext(); glfwSwapBuffers(window); };
void pollEvents() override { glfwPollEvents(); }
bool shouldClose() override {
return glfwWindowShouldClose(window);
}
void restoreContext() { glfwMakeContextCurrent(window); }
void getSize(int& w, int& h) override {
glfwGetFramebufferSize(window, &w, &h);
}
GLFWwindow* getWindow() { return window; }
WindowData data;
virtual WindowGLFW* getWindowImpl() override { return this; }
private:
GLFWwindow* window;
};
}
#endif
#endif