From 66427b09342a9660b61d790779a5c6377ecf8c3d Mon Sep 17 00:00:00 2001 From: Rick van Vonderen <0945444@hr.nl> Date: Mon, 16 Dec 2019 17:47:22 +0100 Subject: [PATCH] Implement application events --- inferno/src/inferno/application.cpp | 64 +++++++++++++++-------------- inferno/src/inferno/application.h | 23 +++++++---- 2 files changed, 49 insertions(+), 38 deletions(-) diff --git a/inferno/src/inferno/application.cpp b/inferno/src/inferno/application.cpp index 555c611..4a1be78 100644 --- a/inferno/src/inferno/application.cpp +++ b/inferno/src/inferno/application.cpp @@ -1,12 +1,14 @@ -#include // printf +#include // unique_ptr #include #include #include "inferno/application.h" #include "inferno/core.h" +#include "inferno/event/applicationevent.h" #include "inferno/event/event.h" #include "inferno/log.h" +#include "inferno/window.h" namespace Inferno { @@ -24,47 +26,49 @@ namespace Inferno { void Application::run() { - NF_CORE_LOG("Startup!"); + NF_CORE_LOG("Application startup!"); - Event event; + m_window = std::make_unique(); + m_window->setEventCallback(NF_BIND_EVENT(Application::onEvent)); - glfwInit(); - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); - glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); + while(!glfwWindowShouldClose(m_window->getWindow())) { - GLFWwindow* window = glfwCreateWindow(1280, 720, "Inferno", NULL, NULL); - if (window == NULL) { - NF_CORE_DANGER("Failed to create GLFW window"); - glfwTerminate(); - return;// -1; - } - glfwMakeContextCurrent(window); + if(glfwGetKey(m_window->getWindow(), GLFW_KEY_ESCAPE) == GLFW_PRESS) { + glfwSetWindowShouldClose(m_window->getWindow(), GL_TRUE); + } + + glClearColor(0.2f, 0.3f, 0.3f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { - NF_CORE_DANGER("Failed to initialize GLAD"); - return;// -1; + m_window->update(); } - glViewport(0, 0, 1280, 720); + NF_CORE_LOG("Application shutdown!"); + } - while(!glfwWindowShouldClose(window)) { + void Application::onEvent(Event &e) + { + EventDispatcher dispatcher(e); + dispatcher.dispatch(NF_BIND_EVENT(Application::onWindowClose)); + dispatcher.dispatch(NF_BIND_EVENT(Application::onWindowResize)); + } - if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) { - glfwSetWindowShouldClose(window, GL_TRUE); - } + bool Application::onWindowClose(WindowCloseEvent &e) + { + (void)e; + NF_CORE_INFO("WindowCloseEvent triggered"); - glClearColor(0.2f, 0.3f, 0.3f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glfwSetWindowShouldClose(m_window->getWindow(), GL_TRUE); - glfwPollEvents(); - glfwSwapBuffers(window); - } + return true; + } - glfwTerminate(); + bool Application::onWindowResize(WindowResizeEvent &e) + { + (void)e; + NF_CORE_INFO("WindowResizeEvent %dx%d triggered", e.getWidth(), e.getHeight()); - NF_CORE_LOG("Shutdown!"); + return true; } } diff --git a/inferno/src/inferno/application.h b/inferno/src/inferno/application.h index ff690b4..3093fe2 100644 --- a/inferno/src/inferno/application.h +++ b/inferno/src/inferno/application.h @@ -1,8 +1,15 @@ #ifndef APPLICATION_H #define APPLICATION_H +#include // unique_ptr + namespace Inferno { + class Event; + class WindowCloseEvent; + class WindowResizeEvent; + class Window; + class Application { public: @@ -11,9 +18,17 @@ namespace Inferno { void run(); + void onEvent(Event &e); + bool onWindowClose(WindowCloseEvent &e); + bool onWindowResize(WindowResizeEvent &e); + +// ----------------------------------------- + static inline Application &get() { return *s_instance; } private: + std::unique_ptr m_window; + static Application* s_instance; }; @@ -23,11 +38,3 @@ namespace Inferno { } #endif // APPLICATION_H - -// @Todo -// v Application -> Singleton -// v Add assert -// - Event class -// - Event Dispatcher -// - template -// - Implement event in Application::OnEvent(Event& e);