Browse Source

Add render system

master
Riyyi 4 years ago
parent
commit
a4845d1bda
  1. 15
      inferno/src/inferno/application.cpp
  2. 7
      inferno/src/inferno/scene/components.h
  3. 10
      inferno/src/inferno/scene/scene.cpp
  4. 37
      inferno/src/inferno/systems/render.cpp
  5. 30
      inferno/src/inferno/systems/render.h

15
inferno/src/inferno/application.cpp

@ -80,22 +80,18 @@ namespace Inferno {
{
dbg() << "Application startup";
glm::mat4 colors = {
1.0f, 0.0f, 1.0f, 1.0f, // Lower left corner: purple
1.0f, 1.0f, 0.0f, 1.0f,
0.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f,
};
Entity quad = m_scene->createEntity("Quad");
quad.add<SpriteComponent>(glm::vec4 { 1.0f, 1.0f, 1.0f, 1.0f }, m_texture);
Entity quad2 = m_scene->createEntity("Quad 2");
auto& quad2Transform = quad2.get<TransformComponent>();
quad2Transform.translate.x = 1.1f;
quad2.add<SpriteComponent>(glm::vec4 { 0.5f, 0.6f, 0.8f, 1.0f }, m_texture);
Entity quad3 = m_scene->createEntity("Quad 3");
auto& quad3Transform = quad3.get<TransformComponent>();
quad3Transform.translate.x = 2.2f;
quad3.add<SpriteComponent>(glm::vec4 { 1.0f, 1.0f, 1.0f, 1.0f }, m_texture2);
std::array<CharacterVertex, Renderer::vertexPerQuad> character;
@ -171,10 +167,7 @@ namespace Inferno {
Renderer2D::the().beginScene(m_scene->cameraProjectionView()); // camera, lights, environment
RendererCharacter::the().beginScene();
Renderer2D::the().drawQuad(quad.get<TransformComponent>(), colors);
Renderer2D::the().drawQuad(quad2.get<TransformComponent>(), { 0.5f, 0.6f, 0.8f, 1.0f }, m_texture);
Renderer2D::the().drawQuad(quad3.get<TransformComponent>(), { 1.0f, 1.0f, 1.0f, 1.0f }, m_texture2);
m_scene->render();
RendererCharacter::the().drawCharacter(character, f->texture());
Renderer2D::the().endScene();

7
inferno/src/inferno/scene/components.h

@ -1,12 +1,14 @@
#ifndef COMPONENTS_H
#define COMPONENTS_H
#include <memory> // std::shared_ptr
#include <string> // std::string
#include "glm/ext/matrix_float4x4.hpp" // glm::mat4
#include "glm/ext/vector_float3.hpp" // glm::vec3
#include "inferno/log.h"
#include "inferno/render/texture.h"
namespace Inferno {
@ -41,6 +43,11 @@ namespace Inferno {
glm::mat4 projection { 1.0f }; // Identity matrix
};
struct SpriteComponent {
glm::vec4 color;
std::shared_ptr<Texture> texture;
};
// ----------------------------------------
const LogStream& operator<<(const LogStream& stream, const TransformComponent& value);

10
inferno/src/inferno/scene/scene.cpp

@ -3,6 +3,7 @@
#include "inferno/scene/entity.h"
#include "inferno/scene/scene.h"
#include "inferno/systems/camera.h"
#include "inferno/systems/render.h"
#include "inferno/systems/transform.h"
namespace Inferno {
@ -34,20 +35,29 @@ namespace Inferno {
auto& cameraTransform = camera.get<TransformComponent>();
cameraTransform.translate.z = 1.0f;
cameraTransform.rotate.z = -1.0f;
RenderSystem* renderSystem = new RenderSystem();
renderSystem->initialize();
RenderSystem::the().setRegistry(m_registry);
}
void Scene::update(float deltaTime)
{
(void)deltaTime;
TransformSystem::the().update();
CameraSystem::the().update();
}
void Scene::render()
{
RenderSystem::the().render();
}
void Scene::destroy()
{
RenderSystem::the().destroy();
CameraSystem::the().destroy();
TransformSystem::the().destroy();
}

37
inferno/src/inferno/systems/render.cpp

@ -0,0 +1,37 @@
#include <glm/ext/matrix_transform.hpp> // glm::translate, glm::rotate, glm::scale, glm::radians
#include "inferno/assertions.h"
#include "inferno/log.h"
#include "inferno/render/renderer.h"
#include "inferno/scene/components.h"
#include "inferno/scene/entity.h"
#include "inferno/systems/render.h"
namespace Inferno {
RenderSystem* RenderSystem::s_instance = nullptr;
void RenderSystem::initialize()
{
ASSERT(!s_instance, "RenderSystem already exists!");
s_instance = this;
dbg(Log::Info) << "RenderSystem initialized";
}
void RenderSystem::render()
{
auto group = m_registry->group<TransformComponent, SpriteComponent>();
for(auto&& [entity, transform, sprite] : group.each()) {
Renderer2D::the().drawQuad(transform, sprite.color, sprite.texture);
}
}
void RenderSystem::destroy()
{
delete s_instance;
s_instance = nullptr;
}
}

30
inferno/src/inferno/systems/render.h

@ -0,0 +1,30 @@
#ifndef RENDER_SYSTEM_H
#define RENDER_SYSTEM_H
#include <memory> //std::shared_ptr
#include "entt/entity/registry.hpp" // entt::entity, entt::registry
#include "inferno/render/renderer.h"
namespace Inferno {
class RenderSystem {
public:
void initialize();
void render();
void destroy();
void setRegistry(const std::shared_ptr<entt::registry>& registry) { m_registry = registry; };
static inline RenderSystem& the() { return *s_instance; }
private:
std::shared_ptr<entt::registry> m_registry;
static RenderSystem* s_instance;
};
}
#endif // RENDER_SYSTEM_H
Loading…
Cancel
Save