From 07f66c09df426c5d5e6644f275b7747e22ecf31b Mon Sep 17 00:00:00 2001 From: Rick van Vonderen <0945444@hr.nl> Date: Mon, 16 Dec 2019 17:43:42 +0100 Subject: [PATCH] Add application events, window class --- inferno/src/inferno/event/applicationevent.h | 41 +++++++ inferno/src/inferno/window.cpp | 121 +++++++++++++++++++ inferno/src/inferno/window.h | 47 +++++++ 3 files changed, 209 insertions(+) create mode 100644 inferno/src/inferno/event/applicationevent.h create mode 100644 inferno/src/inferno/window.cpp create mode 100644 inferno/src/inferno/window.h diff --git a/inferno/src/inferno/event/applicationevent.h b/inferno/src/inferno/event/applicationevent.h new file mode 100644 index 0000000..f87410b --- /dev/null +++ b/inferno/src/inferno/event/applicationevent.h @@ -0,0 +1,41 @@ +#ifndef APPLICATIONEVENT_H +#define APPLICATIONEVENT_H + +#include // stringstream + +#include "inferno/event/event.h" + +namespace Inferno { + + class WindowCloseEvent : public Event { + public: + EVENT_CLASS_TYPE(WindowClose) + EVENT_CLASS_CATEGORY(ApplicationEventCategory) + }; + + class WindowResizeEvent : public Event { + public: + WindowResizeEvent(int width, int height) : + m_width(width), m_height(height) {} + + virtual std::string toString() const override { + std::stringstream ss; + ss << "WindowResize: " << m_width << "x" << m_height; + return ss.str(); + } + +// ----------------------------------------- + + inline int getWidth() const { return m_width; } + inline int getHeight() const { return m_height; } + + EVENT_CLASS_TYPE(WindowResize) + EVENT_CLASS_CATEGORY(ApplicationEventCategory) + + private: + int m_width; + int m_height; + }; +} + +#endif // APPLICATIONEVENT_H diff --git a/inferno/src/inferno/window.cpp b/inferno/src/inferno/window.cpp new file mode 100644 index 0000000..be2ec3c --- /dev/null +++ b/inferno/src/inferno/window.cpp @@ -0,0 +1,121 @@ +#include +#include + +#include "inferno/core.h" +#include "inferno/event/applicationevent.h" +#include "inferno/log.h" +#include "inferno/window.h" + +namespace Inferno { + + unsigned char Window::s_windowCount = 0; + + Window::Window(const WindowProperties &properties) : + m_windowProperties(properties) + { + this->initialize(); + } + + Window::~Window() + { + this->destroy(); + } + + void Window::initialize() + { + const char* title = m_windowProperties.title; + unsigned int width = m_windowProperties.width; + unsigned int height = m_windowProperties.height; + + // Only init once + if (s_windowCount == 0) { + glfwInit(); + } + + // Set window properties + 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); + + // Create GLFW window + m_window = glfwCreateWindow(width, height, title, NULL, NULL); + s_windowCount++; + NF_CORE_ASSERT(m_window, "Failed to create GLFW window!"); + + // Initialize glad + glfwMakeContextCurrent(m_window); + int glad = gladLoadGLLoader((GLADloadproc)glfwGetProcAddress); + NF_CORE_ASSERT(glad, "Failed to initialize glad!"); + + // Log OpenGL properties + NF_CORE_INFO("OpenGL Info:"); + NF_CORE_INFO(" Vendor: %s", glGetString(GL_VENDOR)); + NF_CORE_INFO(" Renderer: %s", glGetString(GL_RENDERER)); + NF_CORE_INFO(" Version: %s", glGetString(GL_VERSION)); + + // Capture cursor and hide it + // glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); + + // Associate the wrapper to the window + glfwSetWindowUserPointer(m_window, this); + + // Error callback + glfwSetErrorCallback([](int error, const char* description) { + NF_CORE_LOG("GLFW Error %d: %s", error, description); + }); + + // Window close callback + glfwSetWindowCloseCallback(m_window, [](GLFWwindow* window) { + Window &w = *(Window*)glfwGetWindowUserPointer(window); + + WindowCloseEvent event; + w.m_eventCallback(event); + }); + + // Window resize callback + glfwSetWindowSizeCallback(m_window, [](GLFWwindow* window, int width, int height) { + Window &w = *(Window*)glfwGetWindowUserPointer(window); + + w.m_windowProperties.width = width; + w.m_windowProperties.height = height; + + WindowResizeEvent event(width, height); + w.m_eventCallback(event); + }); + + // Keyboard callback + // glfwSetKeyCallback + + // Mouse position callback + // glfwSetCursorPosCallback + + // Mouse button callback + // glfwSetMouseButtonCallback + + // Mouse scroll callback + // glfwSetScrollCallback + + glViewport(0, 0, width, height); + + // Enable z-buffer / depth buffer + glEnable(GL_DEPTH_TEST); + } + + void Window::update() + { + glfwPollEvents(); + glfwSwapBuffers(m_window); + } + + void Window::destroy() + { + glfwDestroyWindow(m_window); + s_windowCount--; + + if (s_windowCount == 0) { + glfwTerminate(); + } + } + +} diff --git a/inferno/src/inferno/window.h b/inferno/src/inferno/window.h new file mode 100644 index 0000000..54d4aec --- /dev/null +++ b/inferno/src/inferno/window.h @@ -0,0 +1,47 @@ +#ifndef WINDOW_H +#define WINDOW_H + +#include // function + +class GLFWwindow; + +namespace Inferno { + + class Event; + + struct WindowProperties { + const char* title = "Inferno"; + unsigned int width = 1280; + unsigned int height = 720; + }; + + class Window { + public: + Window(const WindowProperties &properties = WindowProperties()); + virtual ~Window(); + + void initialize(); + void update(); + // void render(); + void destroy(); + +// ----------------------------------------- + + inline int getWidth() const { return m_windowProperties.width; } + inline int getHeight() const { return m_windowProperties.height; } + inline GLFWwindow* getWindow() const { return m_window; } + + inline void setEventCallback(std::function callback) { m_eventCallback = callback; } + + private: + WindowProperties m_windowProperties; + GLFWwindow* m_window; + + std::function m_eventCallback; + + static unsigned char s_windowCount; + }; + +} + +#endif // WINDOW_H