From 3117b087c2af49859a38652bd40756ff7b5d0bb0 Mon Sep 17 00:00:00 2001 From: Ben Kyd Date: Tue, 25 Oct 2022 18:16:02 +0100 Subject: [PATCH] callback calls the callback that's called by the callback calling callback --- .vscode/settings.json | 3 ++- src/inferno.cpp | 3 ++- src/singleton.hpp | 21 +++++++++++++++++++ src/window.cpp | 49 +++++++++++++++++++++++++++++++++++-------- src/window.hpp | 17 +++++++++++---- 5 files changed, 78 insertions(+), 15 deletions(-) create mode 100644 src/singleton.hpp diff --git a/.vscode/settings.json b/.vscode/settings.json index a0f1242..7e83515 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -68,7 +68,8 @@ "typeindex": "cpp", "typeinfo": "cpp", "valarray": "cpp", - "variant": "cpp" + "variant": "cpp", + "complex": "cpp" }, "cmake.configureOnOpen": false } \ No newline at end of file diff --git a/src/inferno.cpp b/src/inferno.cpp index 6b273be..78cbe9d 100644 --- a/src/inferno.cpp +++ b/src/inferno.cpp @@ -19,7 +19,8 @@ Inferno::Inferno(int argc, char** argv) spdlog::info("INFERNO HART v" INFERNO_VERSION); // Create window - mWin = new Window("Inferno v" INFERNO_VERSION, 1280, 720); + mWin = &Window::GetInstance(); + mWin->init("Inferno v" INFERNO_VERSION, 1280, 720); } Inferno::~Inferno() diff --git a/src/singleton.hpp b/src/singleton.hpp new file mode 100644 index 0000000..4f63e80 --- /dev/null +++ b/src/singleton.hpp @@ -0,0 +1,21 @@ +#pragma once + +namespace inferno::helpers { + +template +class Singleton +{ +public: + static T& GetInstance() + { + static T instance; + return instance; + } + + Singleton( Singleton const& ) = delete; + void operator=( Singleton const& ) = delete; +protected: + Singleton() = default; +}; + +} diff --git a/src/window.cpp b/src/window.cpp index 53e0be8..efba1b3 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -4,7 +4,18 @@ using namespace inferno; -Window::Window(std::string title, int width, int height) +Window::Window() +{ + +} + +Window::~Window() +{ + shutdownImGui(); + shutdownGLFW(); +} + +void Window::init(std::string title, int width, int height) { this->width = width; this->height = height; @@ -16,12 +27,6 @@ Window::Window(std::string title, int width, int height) setupImGui(); } -Window::~Window() -{ - shutdownImGui(); - shutdownGLFW(); -} - void Window::setTitle(std::string title) { glfwSetWindowTitle(window, title.c_str()); @@ -50,6 +55,26 @@ void Window::getPos(int& x, int& y) glfwGetWindowPos(window, &x, &y); } +void Window::setKeyCallback(KeyCallback callback) +{ + mKeyCallback = callback; +} + +void Window::setMouseCallback(MouseCallback callback) +{ + mMouseCallback = callback; +} + +KeyCallback Window::getKeyCallback() +{ + return mKeyCallback; +} + +MouseCallback Window::getMouseCallback() +{ + return mMouseCallback; +} + bool Window::newFrame() { glfwPollEvents(); @@ -153,12 +178,18 @@ void Window::shutdownGLFW() void Window::glfwKeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) { - + if (Window::GetInstance().getKeyCallback() != nullptr) + { + Window::GetInstance().getKeyCallback()(key, scancode, action, mods); + } } void Window::glfwMouseCallback(GLFWwindow* window, double xpos, double ypos) { - + if (Window::GetInstance().getMouseCallback() != nullptr) + { + Window::GetInstance().getMouseCallback()(xpos, ypos); + } } void Window::glfwErrorCallback(int error, const char* description) { diff --git a/src/window.hpp b/src/window.hpp index 1b18760..e9f57f4 100644 --- a/src/window.hpp +++ b/src/window.hpp @@ -2,20 +2,24 @@ #include +#include "singleton.hpp" #include "graphics.hpp" #define WINDOW_FLAGS ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoNavFocus | ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoCollapse namespace inferno { +typedef void (*KeyCallback)(int key, int scan, int action, int mod); +typedef void (*MouseCallback)(double x, double y); - -class Window +class Window : public helpers::Singleton { public: - Window(std::string title, int width, int height); + Window(); ~Window(); + void init(std::string title, int width, int height); + bool newFrame(); void render(); @@ -27,7 +31,10 @@ public: void getPos(int& x, int& y); GLFWwindow* getGLFWWindow() { return window; } - + void setKeyCallback(KeyCallback callback); + void setMouseCallback(MouseCallback callback); + KeyCallback getKeyCallback(); + MouseCallback getMouseCallback(); private: void setupGLFW(std::string title); @@ -37,6 +44,8 @@ private: static void glfwKeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods); static void glfwMouseCallback(GLFWwindow* window, double xpos, double ypos); + KeyCallback mKeyCallback = nullptr; + MouseCallback mMouseCallback = nullptr; static void glfwErrorCallback(int error, const char* description);