diff --git a/include/extratools.h b/include/extratools.h index 20db890..c4bb088 100644 --- a/include/extratools.h +++ b/include/extratools.h @@ -1,7 +1,7 @@ #ifndef EXTRATOOLS_H #define EXTRATOOLS_H -#include +#include #include #include diff --git a/include/utils/Window/Window.h b/include/utils/Window/Window.h index 0651e66..e9bb303 100644 --- a/include/utils/Window/Window.h +++ b/include/utils/Window/Window.h @@ -4,8 +4,6 @@ #include "WindowEvents.h" #include "utils/Renderer/Renderer.h" -#include "WindowGLFW/WindowGLFW.h" -#include "WindowSDL3/WindowSDL3.h" namespace Archimedes { @@ -13,34 +11,44 @@ namespace Archimedes { public: - Window(const std::function& sendEventFn) : window(this, sendEventFn) {} + Window(const std::function& sendEventFn) : eventFn(sendEventFn) {} - ~Window() {} + virtual ~Window() = 0; - bool shouldClose() { return window.shouldClose(); } - - void getSize(int& w, int& h) { - window.getSize(w, h); - } void doFrame() { renderer->render(); - window.doFrame(); + swapBuffers(); + + pollEvents(); - window.pollEvents(); + } + + virtual bool shouldClose() = 0; + + virtual void swapBuffers() = 0; + + virtual void pollEvents() = 0; + + virtual void getSize(int& w, int& h) { + w = this->w; + h = this->h; } Renderer* getRenderer() { return renderer; } void setRenderer(Renderer* r) { renderer = r; } - WindowImpl& getWindowImpl() { return window; } + virtual Window* getWindowImpl() = 0; + + protected: + + int w, h; - private: Renderer* renderer; - WindowImpl window; + std::function eventFn; }; } diff --git a/include/utils/Window/WindowImpl/WindowGLFW/WindowGLFW.h b/include/utils/Window/WindowImpl/WindowGLFW/WindowGLFW.h index a3fc9ae..926c1d9 100644 --- a/include/utils/Window/WindowImpl/WindowGLFW/WindowGLFW.h +++ b/include/utils/Window/WindowImpl/WindowGLFW/WindowGLFW.h @@ -5,7 +5,7 @@ #include "pch.hpp" -#include "utils/Window/WindowEvents.h" +#include "utils/Window/Window.h" #define GLFW_INCLUDE_NONE #include @@ -13,20 +13,13 @@ namespace Archimedes { - class Window; - - class WindowGLFW { + class WindowGLFW : public Window { public: - WindowGLFW(Window* p, const std::function& sendEvent) { + WindowGLFW(const std::function& 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 diff --git a/include/utils/Window/WindowImpl/WindowSDL3/WindowSDL3.h b/include/utils/Window/WindowImpl/WindowSDL3/WindowSDL3.h index 8978cd0..d5d0561 100644 --- a/include/utils/Window/WindowImpl/WindowSDL3/WindowSDL3.h +++ b/include/utils/Window/WindowImpl/WindowSDL3/WindowSDL3.h @@ -5,7 +5,7 @@ #include "pch.hpp" -#include "utils/Window/WindowEvents.h" +#include "utils/Window/Window.h" #include @@ -17,8 +17,6 @@ namespace Archimedes { - class Window; - static bool EventCallback(void* ptr, SDL_Event* e) { WindowData& data = *(WindowData*) ptr; @@ -66,13 +64,13 @@ namespace Archimedes { return true; } - class WindowSDL3 { + class WindowSDL3 : public Window { public: - WindowSDL3(Window* p, const std::function& sendEvent) { + WindowSDL3(const std::function& sendEvent) : Window(sendEvent) { - data.window = p; + data.window = this; data.sendEvent = sendEvent; if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD)) { @@ -90,16 +88,16 @@ namespace Archimedes { 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) + window = SDL_CreateWindow("Archimedes", 1280, 720, window_flags); + if (window == nullptr) { std::cerr << "Error: SDL_CreateWindow(): " << SDL_GetError() << std::endl; std::abort(); } - SDL_SetWindowPosition(w, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED); + SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED); #if RENDERER == 1 - gl_context = SDL_GL_CreateContext(w); + gl_context = SDL_GL_CreateContext(window); if (gl_context == nullptr) { @@ -107,12 +105,12 @@ namespace Archimedes { std::abort(); } - SDL_GL_MakeCurrent(w, gl_context); + SDL_GL_MakeCurrent(window, gl_context); SDL_GL_SetSwapInterval(1); // Enable vsync #endif SDL_AddEventWatch(EventCallback, &data); - SDL_ShowWindow(w); + SDL_ShowWindow(window); } ~WindowSDL3() { @@ -120,52 +118,52 @@ namespace Archimedes { SDL_GL_DestroyContext(gl_context); #endif SDL_RemoveEventWatch(EventCallback, &data); - SDL_DestroyWindow(w); + SDL_DestroyWindow(window); SDL_Quit(); } - bool shouldClose() { return false; } + bool shouldClose() override { return false; } - void doFrame() { + void swapBuffers() override { #if RENDERER == 1 restoreContext(); - SDL_GL_SwapWindow(w); + SDL_GL_SwapWindow(window); #endif } - void pollEvents() { + void pollEvents() override { static SDL_Event e; - while(SDL_PollEvent(&e)) {} + while(SDL_PollEvent(&e)); } + + WindowSDL3* getWindowImpl() { return this; }; + #if RENDERER == 1 - void restoreContext() { SDL_GL_MakeCurrent(w, gl_context); } + void restoreContext() { SDL_GL_MakeCurrent(window, gl_context); } #endif - void getSize(int& w, int& h) { - w = this->width; - h = this->height; + void getSize(int& w, int& h) override { + w = this->w; + h = this->h; } void setSize(int& w, int& h) { - this->width = w; - this->height = h; + this->w = w; + this->h = h; } - SDL_Window* getWindow() { return w; } + SDL_Window* getWindow() { return window; } #if RENDERER == 1 SDL_GLContext getContext() { return gl_context; } #endif WindowData data; private: - SDL_Window* w; - int width = 0, height = 0; + SDL_Window* window; #if RENDERER == 1 SDL_GLContext gl_context; #endif }; - - typedef WindowSDL3 WindowImpl; } #endif