From 2275a6be3e16505854d5b4acba5cfd5c32da0906 Mon Sep 17 00:00:00 2001 From: Rick van Vonderen <0945444@hr.nl> Date: Wed, 18 Dec 2019 20:06:16 +0100 Subject: [PATCH] Add graphics context --- inferno/src/inferno/entrypoint.h | 2 +- inferno/src/inferno/event/event.h | 4 +++ inferno/src/inferno/render/context.cpp | 46 ++++++++++++++++++++++++++ inferno/src/inferno/render/context.h | 25 ++++++++++++++ inferno/src/inferno/window.cpp | 33 ++++++++---------- inferno/src/inferno/window.h | 2 ++ 6 files changed, 92 insertions(+), 20 deletions(-) create mode 100644 inferno/src/inferno/render/context.cpp create mode 100644 inferno/src/inferno/render/context.h diff --git a/inferno/src/inferno/entrypoint.h b/inferno/src/inferno/entrypoint.h index 203cc6e..16cb3c4 100644 --- a/inferno/src/inferno/entrypoint.h +++ b/inferno/src/inferno/entrypoint.h @@ -12,7 +12,7 @@ int main(int argc, char* argv[]) { - // Supress unused warning + // Suppress unused warning (void)argc; (void)argv; diff --git a/inferno/src/inferno/event/event.h b/inferno/src/inferno/event/event.h index d4303f2..a674d6b 100644 --- a/inferno/src/inferno/event/event.h +++ b/inferno/src/inferno/event/event.h @@ -13,6 +13,10 @@ namespace Inferno { WindowClose, WindowResize, + KeyPress, + KeyRelease, + KeyRepeat, + MouseButtonPress, MouseButtonRelease, MousePosition, diff --git a/inferno/src/inferno/render/context.cpp b/inferno/src/inferno/render/context.cpp new file mode 100644 index 0000000..5fac876 --- /dev/null +++ b/inferno/src/inferno/render/context.cpp @@ -0,0 +1,46 @@ +#include +#include + +#include "inferno/core.h" +#include "inferno/log.h" +#include "inferno/render/context.h" +#include "inferno/window.h" + +namespace Inferno { + + Context::Context(GLFWwindow* window) : + m_window(window) + { + } + + void Context::initialize() + { + // 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)); + + // Set viewport + Window &w = *(Window*)glfwGetWindowUserPointer(m_window); + glViewport(0, 0, w.getWidth(), w.getHeight()); + + // Enable z-buffer / depth buffer + glEnable(GL_DEPTH_TEST); + } + + void Context::update() + { + glfwSwapBuffers(m_window); + } + + void Context::destroy() + { + } + +} diff --git a/inferno/src/inferno/render/context.h b/inferno/src/inferno/render/context.h new file mode 100644 index 0000000..6296ff5 --- /dev/null +++ b/inferno/src/inferno/render/context.h @@ -0,0 +1,25 @@ +#ifndef CONTEXT_H +#define CONTEXT_H + +struct GLFWwindow; + +namespace Inferno { + + class Context { + public: + Context(GLFWwindow* window); + // virtual ~Context(); + + void initialize(); + void update(); + // void render(); + void destroy(); + + private: + GLFWwindow* m_window; + }; + +} + + +#endif // CONTEXT_H diff --git a/inferno/src/inferno/window.cpp b/inferno/src/inferno/window.cpp index fdd8bef..5450b80 100644 --- a/inferno/src/inferno/window.cpp +++ b/inferno/src/inferno/window.cpp @@ -1,4 +1,3 @@ -#include #include #include "inferno/core.h" @@ -6,6 +5,7 @@ #include "inferno/event/keyevent.h" #include "inferno/event/mouseevent.h" #include "inferno/log.h" +#include "inferno/render/context.h" #include "inferno/settings.h" #include "inferno/window.h" @@ -51,23 +51,16 @@ namespace Inferno { 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!"); + // Associate the wrapper to the window + glfwSetWindowUserPointer(m_window, this); - // 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)); + // Create graphics context + m_context = new Context(m_window); + m_context->initialize(); // 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); @@ -94,6 +87,10 @@ namespace Inferno { // Keyboard callback glfwSetKeyCallback(m_window, [](GLFWwindow* window, int key, int scanCode, int action, int mods) { + // Suppress unused warning + (void)scanCode; + (void)mods; + Window &w = *(Window*)glfwGetWindowUserPointer(window); switch (action) { @@ -117,6 +114,9 @@ namespace Inferno { // Mouse button callback glfwSetMouseButtonCallback(m_window, [](GLFWwindow* window, int button, int action, int mods) { + // Suppress unused warning + (void)mods; + Window &w = *(Window*)glfwGetWindowUserPointer(window); switch (action) { @@ -148,17 +148,12 @@ namespace Inferno { MouseScrollEvent event(xOffset, yOffset); w.m_eventCallback(event); }); - - glViewport(0, 0, width, height); - - // Enable z-buffer / depth buffer - glEnable(GL_DEPTH_TEST); } void Window::update() { glfwPollEvents(); - glfwSwapBuffers(m_window); + m_context->update(); } void Window::destroy() diff --git a/inferno/src/inferno/window.h b/inferno/src/inferno/window.h index 8ff5cde..456f204 100644 --- a/inferno/src/inferno/window.h +++ b/inferno/src/inferno/window.h @@ -7,6 +7,7 @@ struct GLFWwindow; namespace Inferno { + class Context; class Event; struct WindowProperties { @@ -36,6 +37,7 @@ namespace Inferno { private: WindowProperties m_windowProperties; GLFWwindow* m_window; + Context* m_context; std::function m_eventCallback;