start template refactor
This commit is contained in:
143
include/utils/Window/WindowImpl/WindowGLFW/WindowGLFW.h
Normal file
143
include/utils/Window/WindowImpl/WindowGLFW/WindowGLFW.h
Normal file
@@ -0,0 +1,143 @@
|
||||
#ifndef WINDOW_GLFW
|
||||
#define WINDOW_GLFW
|
||||
|
||||
#include "pch.hpp"
|
||||
|
||||
#include "utils/Window/WindowEvents.h"
|
||||
|
||||
#define GLFW_INCLUDE_NONE
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
|
||||
namespace Archimedes {
|
||||
|
||||
template <class WindowImpl, class RendererImpl>
|
||||
requires allowed_windows<WindowImpl, RendererImpl>
|
||||
&& allowed_renderers<RendererImpl>
|
||||
class Window;
|
||||
|
||||
template <class R>
|
||||
class WindowGLFW {
|
||||
|
||||
public:
|
||||
|
||||
WindowGLFW(Window<WindowGLFW<R>, R>* p, const std::function<void(Event*)>& sendEvent) {
|
||||
|
||||
/*if(glfwPlatformSupported(GLFW_PLATFORM_WAYLAND)) {
|
||||
glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_WAYLAND);
|
||||
}*/
|
||||
|
||||
|
||||
data.window = p;
|
||||
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
|
||||
w = glfwCreateWindow(640, 480, "Archimedes", NULL, NULL);
|
||||
|
||||
if(!w) {
|
||||
std::cout << "glfwCreateWindow failed!\n";
|
||||
glfwTerminate();
|
||||
std::abort();
|
||||
}
|
||||
glfwMakeContextCurrent(w);
|
||||
glfwSwapInterval(1);
|
||||
|
||||
glfwSetWindowUserPointer(w, &data);
|
||||
|
||||
glfwSetWindowSizeCallback(w, [](GLFWwindow* window, int w, int h){
|
||||
WindowData<WindowGLFW<R>, R>& d = *(WindowData<WindowGLFW<R>, R>*) glfwGetWindowUserPointer(window);
|
||||
|
||||
d.sendEvent(new ResizeWindowEvent(w, h));
|
||||
});
|
||||
|
||||
glfwSetWindowCloseCallback(w, [](GLFWwindow* window){
|
||||
WindowData<WindowGLFW<R>, R>& d = *(WindowData<WindowGLFW<R>, R>*) glfwGetWindowUserPointer(window);
|
||||
|
||||
d.sendEvent(new CloseWindowEvent(d.window));
|
||||
});
|
||||
|
||||
glfwSetKeyCallback(w, [](GLFWwindow* window, int key, int scancode, int action, int mods){
|
||||
WindowData<WindowGLFW<R>, R>& d = *(WindowData<WindowGLFW<R>, R>*) 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<WindowGLFW<R>, R>& d = *(WindowData<WindowGLFW<R>, R>*) 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<WindowGLFW<R>, R>& d = *(WindowData<WindowGLFW<R>, R>*) glfwGetWindowUserPointer(window);
|
||||
|
||||
d.sendEvent(new ScrollWindowEvent(dx, dy));
|
||||
});
|
||||
|
||||
glfwSetCursorPosCallback(w, [](GLFWwindow* window, double dx, double dy){
|
||||
WindowData<WindowGLFW<R>, R>& d = *(WindowData<WindowGLFW<R>, R>*) 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<WindowGLFW<R>, R> data;
|
||||
|
||||
private:
|
||||
GLFWwindow* w;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user