Add application events, window class
This commit is contained in:
@@ -0,0 +1,41 @@
|
|||||||
|
#ifndef APPLICATIONEVENT_H
|
||||||
|
#define APPLICATIONEVENT_H
|
||||||
|
|
||||||
|
#include <sstream> // 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
|
||||||
@@ -0,0 +1,121 @@
|
|||||||
|
#include <glad/glad.h>
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
|
#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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
#ifndef WINDOW_H
|
||||||
|
#define WINDOW_H
|
||||||
|
|
||||||
|
#include <functional> // 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<void(Event&)> callback) { m_eventCallback = callback; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
WindowProperties m_windowProperties;
|
||||||
|
GLFWwindow* m_window;
|
||||||
|
|
||||||
|
std::function<void(Event&)> m_eventCallback;
|
||||||
|
|
||||||
|
static unsigned char s_windowCount;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // WINDOW_H
|
||||||
Reference in New Issue
Block a user