Browse Source

Implement application events

master
Rick van Vonderen 5 years ago
parent
commit
66427b0934
  1. 60
      inferno/src/inferno/application.cpp
  2. 23
      inferno/src/inferno/application.h

60
inferno/src/inferno/application.cpp

@ -1,12 +1,14 @@
#include <cstdio> // printf #include <memory> // unique_ptr
#include <glad/glad.h> #include <glad/glad.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include "inferno/application.h" #include "inferno/application.h"
#include "inferno/core.h" #include "inferno/core.h"
#include "inferno/event/applicationevent.h"
#include "inferno/event/event.h" #include "inferno/event/event.h"
#include "inferno/log.h" #include "inferno/log.h"
#include "inferno/window.h"
namespace Inferno { namespace Inferno {
@ -24,47 +26,49 @@ namespace Inferno {
void Application::run() void Application::run()
{ {
NF_CORE_LOG("Startup!"); NF_CORE_LOG("Application startup!");
Event event; m_window = std::make_unique<Window>();
m_window->setEventCallback(NF_BIND_EVENT(Application::onEvent));
glfwInit(); while(!glfwWindowShouldClose(m_window->getWindow())) {
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);
GLFWwindow* window = glfwCreateWindow(1280, 720, "Inferno", NULL, NULL); if(glfwGetKey(m_window->getWindow(), GLFW_KEY_ESCAPE) == GLFW_PRESS) {
if (window == NULL) { glfwSetWindowShouldClose(m_window->getWindow(), GL_TRUE);
NF_CORE_DANGER("Failed to create GLFW window");
glfwTerminate();
return;// -1;
} }
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
NF_CORE_DANGER("Failed to initialize GLAD"); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
return;// -1;
}
glViewport(0, 0, 1280, 720); m_window->update();
}
while(!glfwWindowShouldClose(window)) { NF_CORE_LOG("Application shutdown!");
}
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) { void Application::onEvent(Event &e)
glfwSetWindowShouldClose(window, GL_TRUE); {
EventDispatcher dispatcher(e);
dispatcher.dispatch<WindowCloseEvent>(NF_BIND_EVENT(Application::onWindowClose));
dispatcher.dispatch<WindowResizeEvent>(NF_BIND_EVENT(Application::onWindowResize));
} }
glClearColor(0.2f, 0.3f, 0.3f, 1.0f); bool Application::onWindowClose(WindowCloseEvent &e)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); {
(void)e;
NF_CORE_INFO("WindowCloseEvent triggered");
glfwPollEvents(); glfwSetWindowShouldClose(m_window->getWindow(), GL_TRUE);
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;
} }
} }

23
inferno/src/inferno/application.h

@ -1,8 +1,15 @@
#ifndef APPLICATION_H #ifndef APPLICATION_H
#define APPLICATION_H #define APPLICATION_H
#include <memory> // unique_ptr
namespace Inferno { namespace Inferno {
class Event;
class WindowCloseEvent;
class WindowResizeEvent;
class Window;
class Application class Application
{ {
public: public:
@ -11,9 +18,17 @@ namespace Inferno {
void run(); void run();
void onEvent(Event &e);
bool onWindowClose(WindowCloseEvent &e);
bool onWindowResize(WindowResizeEvent &e);
// -----------------------------------------
static inline Application &get() { return *s_instance; } static inline Application &get() { return *s_instance; }
private: private:
std::unique_ptr<Window> m_window;
static Application* s_instance; static Application* s_instance;
}; };
@ -23,11 +38,3 @@ namespace Inferno {
} }
#endif // APPLICATION_H #endif // APPLICATION_H
// @Todo
// v Application -> Singleton
// v Add assert
// - Event class
// - Event Dispatcher
// - template
// - Implement event in Application::OnEvent(Event& e);

Loading…
Cancel
Save