diff --git a/inferno/src/inferno/application.cpp b/inferno/src/inferno/application.cpp index 78f7544..03c0fad 100644 --- a/inferno/src/inferno/application.cpp +++ b/inferno/src/inferno/application.cpp @@ -1,12 +1,16 @@ -#include #include #include "inferno/application.h" #include "inferno/core.h" #include "inferno/event/applicationevent.h" #include "inferno/event/event.h" +#include "inferno/input.h" #include "inferno/log.h" #include "inferno/settings.h" +#include "inferno/render/buffer.h" +#include "inferno/render/context.h" +#include "inferno/render/renderer.h" +#include "inferno/render/shader.h" #include "inferno/window.h" namespace Inferno { @@ -17,6 +21,39 @@ namespace Inferno { { NF_CORE_ASSERT(!s_instance, "Application already exists!"); s_instance = this; + + // Initialize Settings + new Settings(); + + m_window = std::make_unique(); + m_window->setEventCallback(NF_BIND_EVENT(Application::onEvent)); + + float vertices[] = { + -0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, + 0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, + 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, + -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, + }; + + uint32_t indices[] = { + 0, 1, 2, 2, 3, 0 + }; + + m_vertexArray = std::make_shared(); + + std::shared_ptr vertexBuffer = std::make_shared(vertices, sizeof(vertices)); + vertexBuffer->setLayout({ + { BufferElementType::Vec3, "a_position" }, + { BufferElementType::Vec4, "a_color" }, + }); + + m_vertexArray->addVertexBuffer(vertexBuffer); + + std::shared_ptr indexBuffer = std::make_shared(indices, sizeof(indices)); + + m_vertexArray->setIndexBuffer(indexBuffer); + + m_shader = std::make_unique("assets/glsl/simple.vert", "assets/glsl/simple.frag"); } Application::~Application() @@ -27,20 +64,19 @@ namespace Inferno { { NF_CORE_LOG("Application startup"); - // Initialize Settings - new Settings(); - - m_window = std::make_unique(); - m_window->setEventCallback(NF_BIND_EVENT(Application::onEvent)); - while(!glfwWindowShouldClose(m_window->getWindow())) { if(glfwGetKey(m_window->getWindow(), GLFW_KEY_ESCAPE) == GLFW_PRESS) { glfwSetWindowShouldClose(m_window->getWindow(), GL_TRUE); } - glClearColor(0.2f, 0.3f, 0.3f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + Command::clearColor({ 0.2f, 0.3f, 0.3f, 1.0f }); + Command::clear(); + + Renderer::beginScene(); // camera, lights, environment + m_shader->bind(); + Renderer::submit(m_vertexArray); + Renderer::endScene(); m_window->update(); } @@ -57,7 +93,9 @@ namespace Inferno { bool Application::onWindowClose(WindowCloseEvent &e) { + // Suppress unused warning (void)e; + NF_CORE_INFO("WindowCloseEvent triggered"); glfwSetWindowShouldClose(m_window->getWindow(), GL_TRUE); @@ -67,9 +105,13 @@ namespace Inferno { bool Application::onWindowResize(WindowResizeEvent &e) { + // Suppress unused warning (void)e; + NF_CORE_INFO("WindowResizeEvent %dx%d triggered", e.getWidth(), e.getHeight()); + m_window->getContext()->setViewport(0, 0, e.getWidth(), e.getHeight()); + return true; } diff --git a/inferno/src/inferno/application.h b/inferno/src/inferno/application.h index 38f2231..d2866d3 100644 --- a/inferno/src/inferno/application.h +++ b/inferno/src/inferno/application.h @@ -10,6 +10,9 @@ namespace Inferno { class WindowResizeEvent; class Window; + class VertexArray; + class Shader; + class Application { public: Application(); @@ -23,11 +26,18 @@ namespace Inferno { // ----------------------------------------- + inline Window &getWindow() { return *m_window; } + static inline Application &get() { return *s_instance; } private: std::unique_ptr m_window; + // + std::shared_ptr m_vertexArray; + std::unique_ptr m_shader; + // + static Application* s_instance; }; @@ -37,3 +47,30 @@ namespace Inferno { } #endif // APPLICATION_H + +// Gameplan +// v Entrypoint +// v Logging +// v Events +// v Window +// v Settings loader (json) +// v Input polling +// v OpenGL context +// - GPUDriver (?) +// v Renderer +// v Buffers +// ~ Shader +// - Schene (camera, lights, environment) +// - Texture loading +// - Model loading +// - Entity Component System +// - Serialization +// - Level format +// - Tools (Tiled?) +// - Scripting (Lua) + +// - Global object access can be done in 3 ways: +// - Singleton, static, extern + +// @Todo +// - Settings should contain all file paths (ex: shaders) diff --git a/inferno/src/inferno/render/renderer.cpp b/inferno/src/inferno/render/renderer.cpp new file mode 100644 index 0000000..5328ef4 --- /dev/null +++ b/inferno/src/inferno/render/renderer.cpp @@ -0,0 +1,41 @@ +#include + +#include "inferno/render/buffer.h" +#include "inferno/render/renderer.h" + +namespace Inferno { + + void Command::clear() + { + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + } + + void Command::clearColor(const glm::vec4 &color) + { + glClearColor(color.r, color.g, color.b, color.a); + } + + void Command::drawIndexed(const std::shared_ptr &vertexArray) + { + glDrawElements(GL_TRIANGLES, vertexArray->getIndexBuffer()->getCount(), GL_UNSIGNED_INT, nullptr); + } + +// ----------------------------------------- + + void Renderer::beginScene() + { + + } + + void Renderer::endScene() + { + + } + + void Renderer::submit(const std::shared_ptr &vertexArray) + { + vertexArray->bind(); + Command::drawIndexed(vertexArray); + } + +} diff --git a/inferno/src/inferno/render/renderer.h b/inferno/src/inferno/render/renderer.h new file mode 100644 index 0000000..1c7e42c --- /dev/null +++ b/inferno/src/inferno/render/renderer.h @@ -0,0 +1,32 @@ +#ifndef RENDERER_H +#define RENDERER_H + +#include // std::shared_ptr + +#include + +namespace Inferno { + + class VertexArray; + + class Command { + public: + static void clear(); + static void clearColor(const glm::vec4 &color); + + static void drawIndexed(const std::shared_ptr &vertexArray); + }; + +// ----------------------------------------- + + class Renderer { + public: + static void beginScene(); + static void endScene(); + + static void submit(const std::shared_ptr &vertexArray); + }; + +} + +#endif // RENDERER_H