This commit is contained in:
2026-02-08 00:32:46 -06:00
parent b97957ec53
commit bad4bd70bc
9 changed files with 42 additions and 445 deletions

View File

@@ -1,145 +0,0 @@
#if WINDOW == 1
#ifndef WINDOW_GLFW
#define WINDOW_GLFW
#include "pch.hpp"
#include "utils/Window/WindowEvents.h"
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
namespace Archimedes {
class Window;
class WindowGLFW {
public:
WindowGLFW(Window* 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& 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
#endif

View File

@@ -1,3 +1,5 @@
#if WINDOW == 1
#ifndef WINDOW_GLFW
#define WINDOW_GLFW
@@ -11,17 +13,13 @@
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) {
WindowGLFW(Window* p, const std::function<void(Event*)>& sendEvent) {
/*if(glfwPlatformSupported(GLFW_PLATFORM_WAYLAND)) {
glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_WAYLAND);
@@ -57,19 +55,19 @@ namespace Archimedes {
glfwSetWindowUserPointer(w, &data);
glfwSetWindowSizeCallback(w, [](GLFWwindow* window, int w, int h){
WindowData<WindowGLFW<R>, R>& d = *(WindowData<WindowGLFW<R>, R>*) glfwGetWindowUserPointer(window);
WindowData& d = *(WindowData*) glfwGetWindowUserPointer(window);
d.sendEvent(new ResizeWindowEvent(w, h));
});
glfwSetWindowCloseCallback(w, [](GLFWwindow* window){
WindowData<WindowGLFW<R>, R>& d = *(WindowData<WindowGLFW<R>, R>*) glfwGetWindowUserPointer(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<WindowGLFW<R>, R>& d = *(WindowData<WindowGLFW<R>, R>*) glfwGetWindowUserPointer(window);
WindowData& d = *(WindowData*) glfwGetWindowUserPointer(window);
switch(action) {
case GLFW_PRESS:
@@ -85,7 +83,7 @@ namespace Archimedes {
});
glfwSetMouseButtonCallback(w, [](GLFWwindow* window, int button, int action, int mods){
WindowData<WindowGLFW<R>, R>& d = *(WindowData<WindowGLFW<R>, R>*) glfwGetWindowUserPointer(window);
WindowData& d = *(WindowData*) glfwGetWindowUserPointer(window);
switch(action) {
case GLFW_PRESS:
@@ -98,13 +96,13 @@ namespace Archimedes {
});
glfwSetScrollCallback(w, [](GLFWwindow* window, double dx, double dy){
WindowData<WindowGLFW<R>, R>& d = *(WindowData<WindowGLFW<R>, R>*) glfwGetWindowUserPointer(window);
WindowData& d = *(WindowData*) 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);
WindowData& d = *(WindowData*) glfwGetWindowUserPointer(window);
d.sendEvent(new MouseMovedWindowEvent(dx, dy));
});
@@ -131,13 +129,17 @@ namespace Archimedes {
GLFWwindow* getWindow() { return w; }
WindowData<WindowGLFW<R>, R> data;
WindowData data;
private:
GLFWwindow* w;
};
typedef WindowGLFW WindowImpl;
}
#endif
#endif

View File

@@ -1,3 +1,5 @@
#if WINDOW == 2
#ifndef WINDOW_SDL3
#define WINDOW_SDL3
@@ -15,15 +17,11 @@
namespace Archimedes {
template <class WindowImpl, class RendererImpl>
requires allowed_windows<WindowImpl, RendererImpl>
&& allowed_renderers<RendererImpl>
class Window;
template <class W, class R>
static bool EventCallback(void* ptr, SDL_Event* e) {
WindowData<W, R>& data = *(WindowData<W, R>*) ptr;
WindowData& data = *(WindowData*) ptr;
switch(e->type) {
@@ -68,12 +66,11 @@ namespace Archimedes {
return true;
}
template <class R>
class WindowSDL3 {
public:
WindowSDL3(Window<WindowSDL3<R>, R>* p, const std::function<void(Event*)>& sendEvent) {
WindowSDL3(Window* p, const std::function<void(Event*)>& sendEvent) {
data.window = p;
data.sendEvent = sendEvent;
@@ -157,7 +154,7 @@ namespace Archimedes {
#if RENDERER == 1
SDL_GLContext getContext() { return gl_context; }
#endif
WindowData<WindowSDL3<R>, R> data;
WindowData data;
private:
SDL_Window* w;
@@ -168,6 +165,9 @@ namespace Archimedes {
};
typedef WindowSDL3 WindowImpl;
}
#endif
#endif

View File

@@ -1,173 +0,0 @@
#if WINDOW == 2
#ifndef WINDOW_SDL3
#define WINDOW_SDL3
#include "pch.hpp"
#include "utils/Window/WindowEvents.h"
#include <SDL3/SDL.h>
#if RENDERER == 2
#include <SDL3/SDL_opengl.h>
#endif
namespace Archimedes {
class Window;
static bool EventCallback(void* ptr, SDL_Event* e) {
WindowData& data = *(WindowData*) ptr;
switch(e->type) {
case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
case SDL_EVENT_QUIT:
data.sendEvent(new CloseWindowEvent(data.window, e));
break;
case SDL_EVENT_WINDOW_RESIZED:
data.sendEvent(new ResizeWindowEvent(e->window.data1, e->window.data2, e));
break;
case SDL_EVENT_WINDOW_MOUSE_ENTER:
case SDL_EVENT_WINDOW_FOCUS_GAINED:
data.sendEvent(new FocusedWindowEvent(data.window, e));
break;
case SDL_EVENT_WINDOW_MOUSE_LEAVE:
case SDL_EVENT_WINDOW_FOCUS_LOST:
data.sendEvent(new FocusLostWindowEvent(data.window, e));
break;
case SDL_EVENT_KEY_DOWN:
data.sendEvent(new KeyPressedWindowEvent(e->key.key, e->key.repeat ? 1 : 0, e));
break;
case SDL_EVENT_KEY_UP:
data.sendEvent(new KeyReleasedWindowEvent(e->key.key, e));
break;
case SDL_EVENT_MOUSE_MOTION:
data.sendEvent(new MouseMovedWindowEvent(e->motion.x, e->motion.y, e));
break;
case SDL_EVENT_MOUSE_BUTTON_DOWN:
data.sendEvent(new MouseButtonPressedWindowEvent(e->button.button, e));
break;
case SDL_EVENT_MOUSE_BUTTON_UP:
data.sendEvent(new MouseButtonReleasedWindowEvent(e->button.button, e));
break;
case SDL_EVENT_MOUSE_WHEEL:
data.sendEvent(new ScrollWindowEvent(e->wheel.x, e->wheel.y, e));
break;
default:
data.sendEvent(new AnonymousEvent(e));
break;
}
return true;
}
class WindowSDL3 {
public:
WindowSDL3(Window* p, const std::function<void(Event*)>& sendEvent) {
data.window = p;
data.sendEvent = sendEvent;
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD)) {
std::cerr << "Error: SDL_Init(): " << SDL_GetError() << std::flush;
std::abort();
}
#if RENDERER == 1
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
SDL_WindowFlags window_flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN;
#elif RENDERER == 2
SDL_WindowFlags window_flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN | SDL_WINDOW_HIGH_PIXEL_DENSITY;
#endif
w = SDL_CreateWindow("Archimedes", 1280, 720, window_flags);
if (w == nullptr)
{
std::cerr << "Error: SDL_CreateWindow(): " << SDL_GetError() << std::endl;
std::abort();
}
SDL_SetWindowPosition(w, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
#if RENDERER == 1
gl_context = SDL_GL_CreateContext(w);
if (gl_context == nullptr)
{
std::cerr << "Error: SDL_GL_CreateContext(): " << SDL_GetError() << std::endl;
std::abort();
}
SDL_GL_MakeCurrent(w, gl_context);
SDL_GL_SetSwapInterval(1); // Enable vsync
#endif
SDL_AddEventWatch(EventCallback, &data);
SDL_ShowWindow(w);
}
~WindowSDL3() {
#if RENDERER == 1
SDL_GL_DestroyContext(gl_context);
#endif
SDL_RemoveEventWatch(EventCallback, &data);
SDL_DestroyWindow(w);
SDL_Quit();
}
bool shouldClose() { return false; }
void doFrame() {
#if RENDERER == 1
restoreContext();
SDL_GL_SwapWindow(w);
#endif
}
void pollEvents() {
static SDL_Event e;
while(SDL_PollEvent(&e)) {}
}
#if RENDERER == 1
void restoreContext() { SDL_GL_MakeCurrent(w, gl_context); }
#endif
void getSize(int& w, int& h) {
w = this->width;
h = this->height;
}
void setSize(int& w, int& h) {
this->width = w;
this->height = h;
}
SDL_Window* getWindow() { return w; }
#if RENDERER == 1
SDL_GLContext getContext() { return gl_context; }
#endif
WindowData data;
private:
SDL_Window* w;
int width = 0, height = 0;
#if RENDERER == 1
SDL_GLContext gl_context;
#endif
};
typedef WindowSDL3 WindowImpl;
}
#endif
#endif