#include "application.h" #include "rendering/renderer.h" #include "rendering/vertex_array.h" #include "rendering/vertex_buffer.h" #include "rendering/index_buffer.h" #include "rendering/shader.h" #include #include #include namespace Donut { Application* Application::s_instance = nullptr; Application::Application(const std::string& name, int width, int height) : m_running(true), m_minimized(false) { s_instance = this; m_window = std::make_unique(name, width, height); m_window->set_event_callback([this](Event& event) { on_event(event); }); on_init(); } Application::~Application() { on_shutdown(); s_instance = nullptr; } void Application::run() { while (m_running) { if (!m_minimized) { on_update(); on_render(); } m_window->on_update(); } } void Application::close() { m_running = false; } void Application::on_event(Event& event) { EventDispatcher dispatcher(event); dispatcher.dispatch([this, &event](WindowCloseEvent& e) { m_running = false; event.handled = true; return true; }); dispatcher.dispatch([this, &event](WindowResizeEvent& e) { if (e.get_width() == 0 || e.get_height() == 0) m_minimized = true; else m_minimized = false; event.handled = true; return true; }); dispatcher.dispatch([this, &event](KeyPressedEvent& e) { if (e.get_key_code() == 256) close(); return true; }); } void Application::on_init() { Renderer::init(); float vertices[3 * 7] = { -0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f }; uint32_t indices[3] = { 0, 1, 2 }; m_vertex_array = std::shared_ptr(VertexArray::create()); m_vertex_buffer = std::shared_ptr(VertexBuffer::create(vertices, sizeof(vertices))); VertexBufferLayout layout; layout.push(3); layout.push(4); m_vertex_buffer->set_layout(layout); m_vertex_array->add_vertex_buffer(m_vertex_buffer); m_index_buffer = std::shared_ptr(IndexBuffer::create(indices, 3)); m_vertex_array->set_index_buffer(m_index_buffer); m_shader = std::shared_ptr(Shader::create("assets/basic.glsl")); } void Application::on_shutdown() { Renderer::shutdown(); } void Application::on_update() { } void Application::on_render() { Renderer::set_clear_color({ 0.1f, 0.1f, 0.1f, 1.0f }); Renderer::clear(); Renderer::begin_scene(); glm::mat4 view_projection = glm::ortho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f); Renderer::submit(m_shader, m_vertex_array, glm::mat4(1.0f)); Renderer::end_scene(); } }