From bb8d0780b216c45cb7cf3fcfa269b8f6d984c57c Mon Sep 17 00:00:00 2001 From: Jerboa-app Date: Fri, 12 Jan 2024 10:45:42 +0000 Subject: [PATCH] DesktopDisplay and Display (for android) --- examples/Particles/main.cpp | 4 +- examples/Particles/main.h | 2 +- examples/Shape/main.cpp | 4 +- examples/Shape/main.h | 8 +- examples/Sprite/main.cpp | 4 +- examples/Sprite/main.h | 2 +- include/jGL/Display/desktopDisplay.h | 176 ++++++++++++++++++ include/jGL/Display/display.h | 174 ++--------------- include/jGL/OpenGL/openGLInstance.h | 15 +- include/jGL/Vulkan/vulkanInstance.h | 6 +- include/jGL/jGL.h | 8 +- .../{display.cpp => desktopDisplay.cpp} | 23 +-- src/jGL/OpenGL/openGLInstance.cpp | 2 +- src/jGL/Vulkan/vulkanInstance.cpp | 10 +- tests/glTests/test.cpp | 4 +- tests/vulkanTests/test.cpp | 4 +- 16 files changed, 227 insertions(+), 219 deletions(-) create mode 100644 include/jGL/Display/desktopDisplay.h rename src/jGL/Display/{display.cpp => desktopDisplay.cpp} (85%) diff --git a/examples/Particles/main.cpp b/examples/Particles/main.cpp index 3f224b5f..25b5116c 100644 --- a/examples/Particles/main.cpp +++ b/examples/Particles/main.cpp @@ -12,7 +12,7 @@ int main(int argv, char ** argc) } } - jGL::Display display(resX, resY, "Particles", vulkan); + jGL::DesktopDisplay display(glm::ivec2(resX, resY), "Particles", vulkan); glewInit(); @@ -22,7 +22,7 @@ int main(int argv, char ** argc) } else { - jGLInstance = std::move(std::make_unique(display)); + jGLInstance = std::move(std::make_unique(display.getRes())); } jGL::OrthoCam camera(resX, resY, glm::vec2(0.0,0.0)); diff --git a/examples/Particles/main.h b/examples/Particles/main.h index 85beb397..21f6f0c6 100644 --- a/examples/Particles/main.h +++ b/examples/Particles/main.h @@ -6,7 +6,7 @@ #include #include -#include +#include #include #include diff --git a/examples/Shape/main.cpp b/examples/Shape/main.cpp index b73c11d8..38795382 100644 --- a/examples/Shape/main.cpp +++ b/examples/Shape/main.cpp @@ -3,11 +3,11 @@ int main(int argv, char ** argc) { - jGL::Display display(resX, resY, "Shape"); + jGL::DesktopDisplay display(glm::ivec2(resX, resY), "Shape"); glewInit(); - jGLInstance = std::move(std::make_unique(display)); + jGLInstance = std::move(std::make_unique(display.getRes())); jGL::OrthoCam camera(resX, resY, glm::vec2(0.0,0.0)); diff --git a/examples/Shape/main.h b/examples/Shape/main.h index b38c152d..8d19a182 100644 --- a/examples/Shape/main.h +++ b/examples/Shape/main.h @@ -1,5 +1,5 @@ -#ifndef MAIN -#define MAIN +#ifndef MAIN_H +#define MAIN_H #include #include @@ -7,7 +7,7 @@ #include #include -#include +#include #include #include @@ -58,4 +58,4 @@ const char * fragmentShader = "fragment = colour;\n" "}"; -#endif /* MAIN */ +#endif /* MAIN_H */ diff --git a/examples/Sprite/main.cpp b/examples/Sprite/main.cpp index 99117c29..0acadc47 100644 --- a/examples/Sprite/main.cpp +++ b/examples/Sprite/main.cpp @@ -3,11 +3,11 @@ int main(int argv, char ** argc) { - jGL::Display display(resX, resY, "Sprite"); + jGL::DesktopDisplay display(glm::ivec2(resX, resY), "Sprite"); glewInit(); - jGLInstance = std::move(std::make_unique(display)); + jGLInstance = std::move(std::make_unique(display.getRes())); jGL::OrthoCam camera(resX, resY, glm::vec2(0.0,0.0)); diff --git a/examples/Sprite/main.h b/examples/Sprite/main.h index 158e4518..b8854b5c 100644 --- a/examples/Sprite/main.h +++ b/examples/Sprite/main.h @@ -5,7 +5,7 @@ #include #include -#include +#include #include #include diff --git a/include/jGL/Display/desktopDisplay.h b/include/jGL/Display/desktopDisplay.h new file mode 100644 index 00000000..fe5632e7 --- /dev/null +++ b/include/jGL/Display/desktopDisplay.h @@ -0,0 +1,176 @@ +#ifndef DesktopDisplay_H +#define DesktopDisplay_H + +#include +#include +#include + +#include +#define GLFW_INCLUDE_NONE +#include + +#include +#include + +namespace jGL +{ + + void defaultKeyEventCallback + ( + GLFWwindow * window, + int key, + int scancode, + int action, + int mods + ); + + class DesktopDisplay : public Display + { + public: + + DesktopDisplay + ( + glm::ivec2 res, + const char * title, + GLFWkeyfun keyCallback, + GLFWmousebuttonfun mouseButtonCallback, + GLFWscrollfun mouseScrollCallback, + bool vulkan = false + ); + + DesktopDisplay + ( + glm::ivec2 res, + const char * title, + bool vulkan = false + ); + + ~DesktopDisplay(){ glfwTerminate(); free(logo); } + + GLFWwindow * getWindow() const { return glfwWindow; } + + bool isOpen(){ if (glfwWindow != NULL) { return !glfwWindow ? false : true; } return false; } + bool closing(){ return glfwWindowShouldClose(glfwWindow); } + + void open(){ + if (glfwWindow == NULL) + { + // required for MacOS + // https://www.glfw.org/faq.html#41__how_do_i_create_an_opengl_30_context + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, true); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); + glfwWindow = glfwCreateWindow(getResX(), getResY(),title,NULL,NULL); glfwSwapInterval(1); + } + } + + void close(){ if (glfwWindow != NULL) { glfwDestroyWindow(glfwWindow); glfwWindow = NULL; } } + + void setAsFocus(){ if (glfwWindow != NULL) { glfwMakeContextCurrent(glfwWindow); } } + + void keyCallback + ( + GLFWwindow * window, + int key, + int scancode, + int action, + int mods + ); + + void mousePosition(double & x, double & y){ if (glfwWindow != NULL){ glfwGetCursorPos(glfwWindow,&x,&y); } } + + void setMousePosition(double x, double y) + { + if (glfwWindow != NULL) + { + glfwSetCursorPos(glfwWindow, x, y); + } + } + + int getKeyLastState(int key) { return glfwGetKey(glfwWindow, key); } + + void loop() + { + data.clear(); + swap(); + handleEvents(); + if (glfwWindowShouldClose(glfwWindow)){ close(); } + } + + std::vector getEvents(int code) + { + if (data.events.find(code) == data.events.cend()) + { + return {Event()}; + } + else + { + return data.events[code]; + } + } + + std::vector getEventTypes(int code) + { + std::vector e; + if (data.events.find(code) == data.events.cend()) + { + return {EventType::NONE}; + } + else + { + for (auto evt : data.events[code]) + { + e.push_back(evt.type); + } + return e; + } + } + + Event getEvent(int code) + { + if (data.events.find(code) == data.events.cend()) + { + return Event(); + } + else + { + return data.events[code][0]; + } + } + + struct WindowData + { + std::map> events; + double scrollX = 0.0; + double scrollY = 0.0; + bool scrolled = false; + + void clear() + { + scrollX = 0.0; + scrollY = 0.0; + scrolled = false; + events.clear(); + } + }; + + private: + + const char * title; + + GLFWimage * logo; + + GLFWwindow * glfwWindow; + + WindowData data; + + void swap(){ if (glfwWindow != NULL) { glfwSwapBuffers(glfwWindow); } } + + void handleEvents(){ if (glfwWindow != NULL){ glfwPollEvents(); } } + + }; +} + +#endif /* DesktopDisplay_H */ \ No newline at end of file diff --git a/include/jGL/Display/display.h b/include/jGL/Display/display.h index 2b2f2045..7708a184 100644 --- a/include/jGL/Display/display.h +++ b/include/jGL/Display/display.h @@ -1,180 +1,28 @@ #ifndef DISPLAY_H #define DISPLAY_H -#include -#include -#include - -#include -#define GLFW_INCLUDE_NONE -#include - -#include +#include namespace jGL { - - void defaultKeyEventCallback - ( - GLFWwindow * window, - int key, - int scancode, - int action, - int mods - ); - class Display { + public: - Display - ( - unsigned x, - unsigned y, - const char * title, - GLFWkeyfun keyCallback, - GLFWmousebuttonfun mouseButtonCallback, - GLFWscrollfun mouseScrollCallback, - bool vulkan = false - ); - - Display - ( - unsigned x, - unsigned y, - const char * title, - bool vulkan = false - ); - - ~Display(){ glfwTerminate(); free(logo); } - - unsigned getResX() const { return x; } - unsigned getResY() const { return y; } - GLFWwindow * getWindow() const { return glfwWindow; } + Display(glm::ivec2 res) + : resolution(res) + {} - bool isOpen(){ if (glfwWindow != NULL) { return !glfwWindow ? false : true; } return false; } - bool closing(){ return glfwWindowShouldClose(glfwWindow); } + unsigned getResX() const { return resolution.x; } + unsigned getResY() const { return resolution.y; } + glm::ivec2 getRes() const { return resolution; } - void open(){ - if (glfwWindow == NULL) - { - // required for MacOS - // https://www.glfw.org/faq.html#41__how_do_i_create_an_opengl_30_context - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); - glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, true); - glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); - glfwWindow = glfwCreateWindow(x,y,title,NULL,NULL); glfwSwapInterval(1); - } - } - - void close(){ if (glfwWindow != NULL) { glfwDestroyWindow(glfwWindow); glfwWindow = NULL; } } - - void setAsFocus(){ if (glfwWindow != NULL) { glfwMakeContextCurrent(glfwWindow); } } - - void keyCallback - ( - GLFWwindow * window, - int key, - int scancode, - int action, - int mods - ); - - void mousePosition(double & x, double & y){ if (glfwWindow != NULL){ glfwGetCursorPos(glfwWindow,&x,&y); } } - - void setMousePosition(double x, double y) - { - if (glfwWindow != NULL) - { - glfwSetCursorPos(glfwWindow, x, y); - } - } - - int getKeyLastState(int key) { return glfwGetKey(glfwWindow, key); } - - void loop() - { - data.clear(); - swap(); - handleEvents(); - if (glfwWindowShouldClose(glfwWindow)){ close(); } - } - - std::vector getEvents(int code) - { - if (data.events.find(code) == data.events.cend()) - { - return {Event()}; - } - else - { - return data.events[code]; - } - } - - std::vector getEventTypes(int code) - { - std::vector e; - if (data.events.find(code) == data.events.cend()) - { - return {EventType::NONE}; - } - else - { - for (auto evt : data.events[code]) - { - e.push_back(evt.type); - } - return e; - } - } - - Event getEvent(int code) - { - if (data.events.find(code) == data.events.cend()) - { - return Event(); - } - else - { - return data.events[code][0]; - } - } - - struct WindowData - { - std::map> events; - double scrollX = 0.0; - double scrollY = 0.0; - bool scrolled = false; - - void clear() - { - scrollX = 0.0; - scrollY = 0.0; - scrolled = false; - events.clear(); - } - }; - - private: - - unsigned x, y; - const char * title; - - GLFWimage * logo; - - GLFWwindow * glfwWindow; - - WindowData data; - - void swap(){ if (glfwWindow != NULL) { glfwSwapBuffers(glfwWindow); } } + protected: - void handleEvents(){ if (glfwWindow != NULL){ glfwPollEvents(); } } + glm::ivec2 resolution; }; } -#endif /* DISPLAY_H */ \ No newline at end of file +#endif /* DISPLAY_H */ diff --git a/include/jGL/OpenGL/openGLInstance.h b/include/jGL/OpenGL/openGLInstance.h index 1636183e..1c1a48a5 100644 --- a/include/jGL/OpenGL/openGLInstance.h +++ b/include/jGL/OpenGL/openGLInstance.h @@ -19,19 +19,14 @@ namespace jGL::GL public: - OpenGLInstance(const Display & display) - : jGLInstance(display), - framebuffer(glDrawFramebuffer()), - res(glm::vec2(display.getResX(), display.getResY())), + OpenGLInstance(glm::ivec2 res) + : jGLInstance(res), + framebuffer(glDrawFramebuffer()), textRenderer(res) { framebuffer.setResolution ( - glm::vec2 - ( - display.getResX(), - display.getResY() - ) + res ); framebuffer.setMSAA(1); } @@ -119,8 +114,6 @@ namespace jGL::GL glFont defaultFont = glFont(48); - glm::vec2 res; - TextRenderer textRenderer; glm::vec4 clearColour = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f); diff --git a/include/jGL/Vulkan/vulkanInstance.h b/include/jGL/Vulkan/vulkanInstance.h index ec988e1a..0493755f 100644 --- a/include/jGL/Vulkan/vulkanInstance.h +++ b/include/jGL/Vulkan/vulkanInstance.h @@ -9,7 +9,6 @@ #include #include -#include #include #include @@ -36,7 +35,7 @@ namespace jGL::Vulkan public: - VulkanInstance(const Display & display); + VulkanInstance(const DesktopDisplay & display); ~VulkanInstance(); @@ -136,9 +135,6 @@ namespace jGL::Vulkan bool midFrame = false; - uint32_t resX = 0; - uint32_t resY = 0; - glm::vec4 clearColour = glm::vec4(1.0,1.0,1.0,1.0); std::vector imageAvailableSemaphores, renderFinsihedSemaphores; diff --git a/include/jGL/jGL.h b/include/jGL/jGL.h index 79025413..5a20ea50 100644 --- a/include/jGL/jGL.h +++ b/include/jGL/jGL.h @@ -3,7 +3,7 @@ #include -#include +#include #include #include @@ -21,8 +21,8 @@ namespace jGL public: - jGLInstance(const Display & display) - : width(display.getResX()), height(display.getResY()), closing(false) + jGLInstance(glm::ivec2 res) + : resolution(res), closing(false) {} virtual void text @@ -54,7 +54,7 @@ namespace jGL protected: - uint32_t width, height; + glm::ivec2 resolution; uint8_t msaaSamples = 0; bool closing = false; diff --git a/src/jGL/Display/display.cpp b/src/jGL/Display/desktopDisplay.cpp similarity index 85% rename from src/jGL/Display/display.cpp rename to src/jGL/Display/desktopDisplay.cpp index 3d142b5d..b9fd0996 100644 --- a/src/jGL/Display/display.cpp +++ b/src/jGL/Display/desktopDisplay.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include @@ -15,7 +15,7 @@ namespace jGL int action ) { - auto data = reinterpret_cast(glfwGetWindowUserPointer(window)); + auto data = reinterpret_cast(glfwGetWindowUserPointer(window)); double x, y; glfwGetCursorPos(window,&x,&y); @@ -68,23 +68,22 @@ namespace jGL double y ) { - auto e = reinterpret_cast(glfwGetWindowUserPointer(window)); + auto e = reinterpret_cast(glfwGetWindowUserPointer(window)); e->scrolled = true; e->scrollX = x; e->scrollY = y; } - Display::Display + DesktopDisplay::DesktopDisplay ( - unsigned x, - unsigned y, + glm::ivec2 res, const char * title, GLFWkeyfun keyCallback, GLFWmousebuttonfun mouseButtonCallback, GLFWscrollfun mouseScrollCallback, bool vulkan ) - : x(x), y(y), title(title) + : Display(res), title(title) { if ( !glfwInit() ) { exit(EXIT_FAILURE); } @@ -121,17 +120,15 @@ namespace jGL glfwSetWindowUserPointer(glfwWindow, &this->data); } - Display::Display + DesktopDisplay::DesktopDisplay ( - unsigned x, - unsigned y, + glm::ivec2 res, const char * title, bool vulkan ) - : Display + : DesktopDisplay ( - x, - y, + res, title, defaultKeyEventCallback, defaultMouseButtonCallback, diff --git a/src/jGL/OpenGL/openGLInstance.cpp b/src/jGL/OpenGL/openGLInstance.cpp index 633d6555..a68e46d3 100644 --- a/src/jGL/OpenGL/openGLInstance.cpp +++ b/src/jGL/OpenGL/openGLInstance.cpp @@ -17,7 +17,7 @@ namespace jGL::GL position, scale, colour, - res + resolution ); } diff --git a/src/jGL/Vulkan/vulkanInstance.cpp b/src/jGL/Vulkan/vulkanInstance.cpp index c6f6d115..b2f3ccce 100644 --- a/src/jGL/Vulkan/vulkanInstance.cpp +++ b/src/jGL/Vulkan/vulkanInstance.cpp @@ -3,11 +3,9 @@ namespace jGL::Vulkan { - VulkanInstance::VulkanInstance(const Display & display) - : jGLInstance(display) + VulkanInstance::VulkanInstance(const DesktopDisplay & display) + : jGLInstance(display.getRes()) { - resX = width; - resY = height; VkApplicationInfo appInfo{}; appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; appInfo.pApplicationName = "jGL::Vulkan"; @@ -293,7 +291,7 @@ namespace jGL::Vulkan if (result == VK_ERROR_OUT_OF_DATE_KHR) { - swapchain.recreateSwapchain(resX, resY, device, surface.getVkSurfaceKHR()); + swapchain.recreateSwapchain(resolution.x, resolution.y, device, surface.getVkSurfaceKHR()); } else if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR) { @@ -384,7 +382,7 @@ namespace jGL::Vulkan if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR) { - swapchain.recreateSwapchain(resX, resY, device, surface.getVkSurfaceKHR()); + swapchain.recreateSwapchain(resolution.x, resolution.y, device, surface.getVkSurfaceKHR()); } else if (result != VK_SUCCESS) { diff --git a/tests/glTests/test.cpp b/tests/glTests/test.cpp index 1ba3a438..a18d07dc 100644 --- a/tests/glTests/test.cpp +++ b/tests/glTests/test.cpp @@ -5,7 +5,7 @@ const double tol = 1e-6; #include #include -#include +#include std::default_random_engine e; std::uniform_int_distribution U(0,-1); @@ -17,7 +17,7 @@ using jGL::Sampler2D; using jGL::GL::glShader; using jGL::jGLUniform; using jGL::AbstractjGLUniform; -using jGL::Display; +using jGL::DesktopDisplay; static const char * TESTVS01 = "#version 300 es\n" diff --git a/tests/vulkanTests/test.cpp b/tests/vulkanTests/test.cpp index 92e4a525..99683afc 100644 --- a/tests/vulkanTests/test.cpp +++ b/tests/vulkanTests/test.cpp @@ -4,7 +4,7 @@ const double tol = 1e-6; #include -#include +#include #include #include #include @@ -42,7 +42,7 @@ SCENARIO("Vulkan instance", "[vulkan]") { WHEN("A VulkanInstance is created") { - jGL::Display display(1, 1, "Vulkan Test", true); + jGL::DesktopDisplay display(glm::ivec2(1, 1), "Vulkan Test", true); std::unique_ptr vkInstance; bool instantCreated = false; try