147 lines
4.7 KiB
C++
147 lines
4.7 KiB
C++
#ifdef WINDOW_GLFW
|
|
#ifndef WINDOW_GLFW_H
|
|
#define WINDOW_GLFW_H
|
|
|
|
#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();
|
|
}
|
|
#ifdef RENDERER_OPENGL
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|
|
|
glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, 1);
|
|
#endif
|
|
|
|
window = glfwCreateWindow(640, 480, "Archimedes", NULL, NULL);
|
|
|
|
if(!window) {
|
|
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 setSize(const int& w, const int& h) override {
|
|
this->w = w;
|
|
this->h = h;
|
|
|
|
glfwSetWindowSize(window, w, h);
|
|
}
|
|
|
|
void restoreContext() override { 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
|