polymorphic windows

This commit is contained in:
2026-02-08 11:57:18 -06:00
parent bad4bd70bc
commit f277dee1f7
4 changed files with 76 additions and 77 deletions

View File

@@ -5,7 +5,7 @@
#include "pch.hpp"
#include "utils/Window/WindowEvents.h"
#include "utils/Window/Window.h"
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
@@ -13,20 +13,13 @@
namespace Archimedes {
class Window;
class WindowGLFW {
class WindowGLFW : public Window {
public:
WindowGLFW(Window* p, const std::function<void(Event*)>& sendEvent) {
WindowGLFW(const std::function<void(Event*)>& sendEvent) : Window(sendEvent) {
/*if(glfwPlatformSupported(GLFW_PLATFORM_WAYLAND)) {
glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_WAYLAND);
}*/
data.window = p;
data.window = this;
data.sendEvent = sendEvent;
glfwSetErrorCallback([](int e, const char* m){
@@ -42,31 +35,31 @@ namespace Archimedes {
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#endif
w = glfwCreateWindow(640, 480, "Archimedes", NULL, NULL);
window = glfwCreateWindow(640, 480, "Archimedes", NULL, NULL);
if(!w) {
std::cout << "glfwCreateWindow failed!\n";
glfwTerminate();
std::abort();
}
glfwMakeContextCurrent(w);
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
glfwSetWindowUserPointer(w, &data);
glfwSetWindowUserPointer(window, &data);
glfwSetWindowSizeCallback(w, [](GLFWwindow* window, int w, int h){
glfwSetWindowSizeCallback(window, [](GLFWwindow* window, int w, int h){
WindowData& d = *(WindowData*) glfwGetWindowUserPointer(window);
d.sendEvent(new ResizeWindowEvent(w, h));
});
glfwSetWindowCloseCallback(w, [](GLFWwindow* window){
glfwSetWindowCloseCallback(window, [](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){
glfwSetKeyCallback(window, [](GLFWwindow* window, int key, int scancode, int action, int mods){
WindowData& d = *(WindowData*) glfwGetWindowUserPointer(window);
switch(action) {
@@ -82,7 +75,7 @@ namespace Archimedes {
}
});
glfwSetMouseButtonCallback(w, [](GLFWwindow* window, int button, int action, int mods){
glfwSetMouseButtonCallback(window, [](GLFWwindow* window, int button, int action, int mods){
WindowData& d = *(WindowData*) glfwGetWindowUserPointer(window);
switch(action) {
@@ -95,13 +88,13 @@ namespace Archimedes {
}
});
glfwSetScrollCallback(w, [](GLFWwindow* window, double dx, double dy){
glfwSetScrollCallback(window, [](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){
glfwSetCursorPosCallback(window, [](GLFWwindow* window, double dx, double dy){
WindowData& d = *(WindowData*) glfwGetWindowUserPointer(window);
d.sendEvent(new MouseMovedWindowEvent(dx, dy));
@@ -113,31 +106,31 @@ namespace Archimedes {
}
bool shouldClose() {
return glfwWindowShouldClose(w);
void swapBuffers() override { restoreContext(); glfwSwapBuffers(window); };
void pollEvents() override { glfwPollEvents(); }
bool shouldClose() override {
return glfwWindowShouldClose(window);
}
void doFrame() { restoreContext(); glfwSwapBuffers(w); }
void restoreContext() { glfwMakeContextCurrent(window); }
void pollEvents() { glfwPollEvents(); }
void restoreContext() { glfwMakeContextCurrent(w); }
void getSize(int& w, int& h) {
glfwGetFramebufferSize(this->w, &w, &h);
void getSize(int& w, int& h) override {
glfwGetFramebufferSize(window, &w, &h);
}
GLFWwindow* getWindow() { return w; }
GLFWwindow* getWindow() { return window; }
WindowData data;
virtual WindowGLFW* getWindowImpl() override { return this; }
private:
GLFWwindow* w;
GLFWwindow* window;
};
typedef WindowGLFW WindowImpl;
}
#endif