#include "ifnpch.h" #include "Application.h"
#include "Events/ApplicationEvent.h" #include "Log.h"
#include <glad/glad.h>
#include "Input.h"
namespace Infinite {
#define BIND_EVENT_FN(x) std::bind(&Application::x, this, std::placeholders::_1)
Application* Application::s_Instance = nullptr;
Application::Application() { IFN_CORE_ASSERT(!s_Instance, "Application already exists!"); s_Instance = this;
m_Window = std::unique_ptr<Window>(Window::Create()); m_Window->SetEventCallback(BIND_EVENT_FN(OnEvent));
m_ImGuiLayer = new ImGuiLayer(); PushOverlay(m_ImGuiLayer);
// Vertex Array + glGenVertexArrays(1, &m_VertexArray); + glBindVertexArray(m_VertexArray);
// Vertex Buffer + glGenBuffers(1, &m_VertexBuffer); + glBindBuffer(GL_ARRAY_BUFFER, m_VertexBuffer);
+ float vertices[3 * 3] = { + -0.5f, -0.5f, 0.0f, + 0.5f, -0.5f, 0.0f, + 0.0f, 0.5f, 0.0f + };
+ glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
+ glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), nullptr);
+ glGenBuffers(1, &m_IndexBuffer); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_IndexBuffer);
+ unsigned int indices[3] = { 0, 1, 2 }; + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
}
Application::~Application() {}
void Application::PushLayer(Layer* layer) { m_LayerStack.PushLayer(layer); }
void Application::PushOverlay(Layer* layer) { m_LayerStack.PushOverlay(layer); }
void Application::OnEvent(Event& e) { EventDispatcher dispatcher(e); dispatcher.Dispatch<WindowCloseEvent>(BIND_EVENT_FN(OnWindowClose));
for (auto it = m_LayerStack.end(); it != m_LayerStack.begin();) { (*--it)->OnEvent(e); if (e.Handled) break; } }
void Application::Run() { while (m_Running) { glClearColor(0.1f, 0.1f, 0.1f, 1); glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(m_VertexArray); glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, nullptr);
for (Layer* layer : m_LayerStack) layer->OnUpdate();
// auto [x, y] = Input::GetMousePosition(); // IFN_CORE_TRACE("{0}, {1}", x, y);
m_ImGuiLayer->Begin(); for (Layer* layer : m_LayerStack) layer->OnImGuiRender(); m_ImGuiLayer->End();
m_Window->OnUpdate(); } }
bool Application::OnWindowClose(WindowCloseEvent& e) { m_Running = false; return true; }
}
|