Browse Source

Add graphics context

master
Rick van Vonderen 5 years ago
parent
commit
2275a6be3e
  1. 2
      inferno/src/inferno/entrypoint.h
  2. 4
      inferno/src/inferno/event/event.h
  3. 46
      inferno/src/inferno/render/context.cpp
  4. 25
      inferno/src/inferno/render/context.h
  5. 33
      inferno/src/inferno/window.cpp
  6. 2
      inferno/src/inferno/window.h

2
inferno/src/inferno/entrypoint.h

@ -12,7 +12,7 @@
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
// Supress unused warning // Suppress unused warning
(void)argc; (void)argc;
(void)argv; (void)argv;

4
inferno/src/inferno/event/event.h

@ -13,6 +13,10 @@ namespace Inferno {
WindowClose, WindowClose,
WindowResize, WindowResize,
KeyPress,
KeyRelease,
KeyRepeat,
MouseButtonPress, MouseButtonPress,
MouseButtonRelease, MouseButtonRelease,
MousePosition, MousePosition,

46
inferno/src/inferno/render/context.cpp

@ -0,0 +1,46 @@
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#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()
{
}
}

25
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

33
inferno/src/inferno/window.cpp

@ -1,4 +1,3 @@
#include <glad/glad.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include "inferno/core.h" #include "inferno/core.h"
@ -6,6 +5,7 @@
#include "inferno/event/keyevent.h" #include "inferno/event/keyevent.h"
#include "inferno/event/mouseevent.h" #include "inferno/event/mouseevent.h"
#include "inferno/log.h" #include "inferno/log.h"
#include "inferno/render/context.h"
#include "inferno/settings.h" #include "inferno/settings.h"
#include "inferno/window.h" #include "inferno/window.h"
@ -51,23 +51,16 @@ namespace Inferno {
s_windowCount++; s_windowCount++;
NF_CORE_ASSERT(m_window, "Failed to create GLFW window!"); NF_CORE_ASSERT(m_window, "Failed to create GLFW window!");
// Initialize glad // Associate the wrapper to the window
glfwMakeContextCurrent(m_window); glfwSetWindowUserPointer(m_window, this);
int glad = gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
NF_CORE_ASSERT(glad, "Failed to initialize glad!");
// Log OpenGL properties // Create graphics context
NF_CORE_INFO("OpenGL Info:"); m_context = new Context(m_window);
NF_CORE_INFO(" Vendor: %s", glGetString(GL_VENDOR)); m_context->initialize();
NF_CORE_INFO(" Renderer: %s", glGetString(GL_RENDERER));
NF_CORE_INFO(" Version: %s", glGetString(GL_VERSION));
// Capture cursor and hide it // Capture cursor and hide it
// glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); // glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
// Associate the wrapper to the window
glfwSetWindowUserPointer(m_window, this);
// Error callback // Error callback
glfwSetErrorCallback([](int error, const char* description) { glfwSetErrorCallback([](int error, const char* description) {
NF_CORE_LOG("GLFW Error %d: %s", error, description); NF_CORE_LOG("GLFW Error %d: %s", error, description);
@ -94,6 +87,10 @@ namespace Inferno {
// Keyboard callback // Keyboard callback
glfwSetKeyCallback(m_window, [](GLFWwindow* window, int key, int scanCode, int action, int mods) { 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); Window &w = *(Window*)glfwGetWindowUserPointer(window);
switch (action) { switch (action) {
@ -117,6 +114,9 @@ namespace Inferno {
// Mouse button callback // Mouse button callback
glfwSetMouseButtonCallback(m_window, [](GLFWwindow* window, int button, int action, int mods) { glfwSetMouseButtonCallback(m_window, [](GLFWwindow* window, int button, int action, int mods) {
// Suppress unused warning
(void)mods;
Window &w = *(Window*)glfwGetWindowUserPointer(window); Window &w = *(Window*)glfwGetWindowUserPointer(window);
switch (action) { switch (action) {
@ -148,17 +148,12 @@ namespace Inferno {
MouseScrollEvent event(xOffset, yOffset); MouseScrollEvent event(xOffset, yOffset);
w.m_eventCallback(event); w.m_eventCallback(event);
}); });
glViewport(0, 0, width, height);
// Enable z-buffer / depth buffer
glEnable(GL_DEPTH_TEST);
} }
void Window::update() void Window::update()
{ {
glfwPollEvents(); glfwPollEvents();
glfwSwapBuffers(m_window); m_context->update();
} }
void Window::destroy() void Window::destroy()

2
inferno/src/inferno/window.h

@ -7,6 +7,7 @@ struct GLFWwindow;
namespace Inferno { namespace Inferno {
class Context;
class Event; class Event;
struct WindowProperties { struct WindowProperties {
@ -36,6 +37,7 @@ namespace Inferno {
private: private:
WindowProperties m_windowProperties; WindowProperties m_windowProperties;
GLFWwindow* m_window; GLFWwindow* m_window;
Context* m_context;
std::function<void(Event&)> m_eventCallback; std::function<void(Event&)> m_eventCallback;

Loading…
Cancel
Save