Scene System

- Added Scene
- Added Entity
- Added Components
      * TransformComponent
      * SpriteRendererComponent

- Minor fixes
This commit is contained in:
Light 2021-07-25 17:50:08 +04:30
parent b54afb6046
commit 8287b89fad
11 changed files with 277 additions and 101 deletions

View file

@ -4,10 +4,15 @@ struct VertexOut
float4 Position : SV_Position; float4 Position : SV_Position;
}; };
cbuffer cv_ViewProjection : register(b0)
{
row_major matrix viewProjection;
}
VertexOut main(float3 InPosition : POSITION, float4 InColor : COLOR) VertexOut main(float3 InPosition : POSITION, float4 InColor : COLOR)
{ {
VertexOut vso; VertexOut vso;
vso.Position = float4(InPosition.x, InPosition.y, InPosition.z, 1.0); vso.Position = mul(float4(InPosition.x, InPosition.y, InPosition.z, 1.0), viewProjection);
vso.Color = InColor; vso.Color = InColor;
return vso; return vso;

View file

@ -0,0 +1,36 @@
#pragma once
#include "Base.h"
#include <glm/glm.hpp>
namespace Light {
class Texture;
struct TransformComponent
{
glm::vec2 position, size;
// glm::mat4 transform{ 1.0f };
TransformComponent() = default;
TransformComponent(const TransformComponent&) = default;
TransformComponent(const glm::vec2& _position, const glm::vec2& _size) : position(_position), size(_size) {}
// operator glm::mat4&() { return transform; }
// operator const glm::mat4& () const { return transform; }
};
struct SpriteRendererComponent
{
std::shared_ptr<Texture> texture;
SpriteRendererComponent() = default;
SpriteRendererComponent(const SpriteRendererComponent&) = default;
SpriteRendererComponent(std::shared_ptr<Texture> _texture) : texture(_texture) {}
operator std::shared_ptr<Texture>() { return texture; }
};
}

View file

@ -0,0 +1,17 @@
#include "ltpch.h"
#include "Entity.h"
#include "Scene.h"
namespace Light {
Entity::Entity(entt::entity handle, Scene* scene)
: m_Handle(handle), m_Scene(scene)
{
}
Entity::~Entity()
{
}
}

View file

@ -0,0 +1,30 @@
#pragma once
#include "Base.h"
#include <entt.hpp>
namespace Light {
class Scene;
class Entity
{
private:
entt::entity m_Handle;
Scene* m_Scene;
public:
Entity(){}
Entity(entt::entity handle, Scene* registry);
~Entity();
template<typename T, typename... Args>
T& AddComponent(Args&&... args)
{
return m_Scene->m_Registry.emplace<T>(m_Handle, std::forward<Args>(args)...);
}
};
}

View file

@ -0,0 +1,40 @@
#include "ltpch.h"
#include "Scene.h"
#include "Entity.h"
#include "Components.h"
#include "Graphics/Renderer.h"
#include <glm/glm.hpp>
namespace Light {
Scene::Scene()
{
}
Scene::~Scene()
{
}
void Scene::OnRender()
{
auto group = m_Registry.group(entt::get<TransformComponent, SpriteRendererComponent>);
group.each([](TransformComponent& transform, SpriteRendererComponent& sprite) {
Renderer::DrawQuad(glm::vec3(transform.position, 0.0f), transform.size, sprite.texture);
});
}
Entity Scene::CreateEntity(const std::string& name, const glm::vec2& position, const glm::vec2& size)
{
Entity entity { m_Registry.create(), this } ;
entity.AddComponent<TransformComponent>(position, size);
return entity;
}
}

View file

@ -0,0 +1,28 @@
#pragma once
#include "Base.h"
#include <glm/glm.hpp>
#include <entt.hpp>
namespace Light {
class Entity;
class Scene
{
private:
friend class Entity;
entt::registry m_Registry;
public:
Scene();
~Scene();
void OnRender();
Entity CreateEntity(const std::string& name, const glm::vec2& position, const glm::vec2& size);
};
}

View file

@ -44,6 +44,11 @@
//** THIRD_PARTY **// //** THIRD_PARTY **//
#include <imgui.h> #include <imgui.h>
//** SCENE **//
#include "Scene/Scene.h"
#include "Scene/Entity.h"
#include "Scene/Components.h"
// entry point // entry point
#ifdef LIGHT_ENTRY_POINT #ifdef LIGHT_ENTRY_POINT
#include "EntryPoint.h" #include "EntryPoint.h"

View file

@ -34,6 +34,7 @@ project "Mirror"
(dependenciesdir .. "imgui/"), (dependenciesdir .. "imgui/"),
(dependenciesdir .. "imgui/backends"), (dependenciesdir .. "imgui/backends"),
(dependenciesdir .. "glm/"), (dependenciesdir .. "glm/"),
(dependenciesdir .. "entt/"),
} }
links links

View file

@ -4,6 +4,8 @@
#include "MirrorLayer.h" #include "MirrorLayer.h"
namespace Light {
class Mirror : public Light::Application class Mirror : public Light::Application
{ {
public: public:
@ -20,7 +22,7 @@ public:
m_Window->SetProperties(properties); m_Window->SetProperties(properties);
// Attach the sandbox layer // Attach the sandbox layer
Light::LayerStack::AttachLayer(new MirrorLayer("MirrorLayer")); LayerStack::AttachLayer(new MirrorLayer("MirrorLayer"));
} }
~Mirror() ~Mirror()
@ -29,7 +31,9 @@ public:
} }
}; };
Light::Application* Light::CreateApplication() ::Light::Application* ::Light::CreateApplication()
{ {
return new Mirror(); return new Mirror();
} }
}

View file

@ -1,40 +1,48 @@
#include <LightEngine.h> #include <LightEngine.h>
class MirrorLayer : public Light::Layer namespace Light {
class MirrorLayer : public Layer
{ {
private: private:
std::shared_ptr<Light::Texture> m_AwesomefaceTexture; std::shared_ptr<Texture> m_AwesomefaceTexture;
std::vector<glm::vec3> positions; std::vector<glm::vec3> positions;
std::vector<glm::vec2> sizes; std::vector<glm::vec2> sizes;
glm::vec2 m_Direction; glm::vec2 m_Direction;
float m_Speed = 1.2f; float m_Speed = 1000.0f;
std::shared_ptr<Light::Camera> m_Camera; std::shared_ptr<Camera> m_Camera;
std::shared_ptr<Light::Framebuffer> m_Framebuffer; std::shared_ptr<Framebuffer> m_Framebuffer;
Scene m_Scene;
Entity m_TestEntity;
bool m_GameSceneEvents = false; bool m_GameSceneEvents = false;
public: public:
MirrorLayer(const std::string& name) MirrorLayer(const std::string& name)
: Light::Layer(name), m_Direction(glm::vec2(0.0f, 0.0f)) : Layer(name), m_Direction(glm::vec2(0.0f, 0.0f))
{ {
m_Camera = std::make_shared<Light::Camera>(glm::vec2(0.0f), 800.0f / 600.0f, 1.0f); m_Camera = std::make_shared<Camera>(glm::vec2(500.0f), NULL, 1000.0f);
Light::ResourceManager::LoadTexture("awesomeface", "res/Textures/awesomeface.png"); ResourceManager::LoadTexture("awesomeface", "res/Textures/awesomeface.png");
m_AwesomefaceTexture = Light::ResourceManager::GetTexture("awesomeface"); m_AwesomefaceTexture = ResourceManager::GetTexture("awesomeface");
m_Framebuffer = std::shared_ptr<Light::Framebuffer>(Light::Framebuffer::Create({ 800u, 600u, 1, glm::vec4(1.0f, 0.0f, 0.0f, 1.0f), false }, Light::GraphicsContext::GetSharedContext())); m_Framebuffer = std::shared_ptr<Framebuffer>(Framebuffer::Create({ 800u, 600u, 1, glm::vec4(1.0f, 0.0f, 0.0f, 1.0f), false }, GraphicsContext::GetSharedContext()));
for (int i = 0; i < 100; i++) for (int i = 0; i < 250; i++)
{ {
glm::vec3 position = glm::vec3(-1.0f + (-100.0f / 400.0f) + ((rand() % 1000) / 400.0f), -1.0f + (-100.0f / 300.0f) + ((rand() % 800) / 300.0f), 0.0f); glm::vec3 position = glm::vec3(rand() % 3000 - 1400.0f, rand() % 3000 - 1400.0f, 0.0f);
glm::vec2 size = glm::vec2(100 / 400.0f, 100 / 300.0f); glm::vec2 size = glm::vec2(250.0f, 250.0f);
positions.push_back(position); positions.push_back(position);
sizes.push_back(size); sizes.push_back(size);
m_Scene.CreateEntity("quad", position, size).AddComponent<SpriteRendererComponent>(m_AwesomefaceTexture);
} }
} }
@ -43,19 +51,18 @@ public:
m_Camera->CalculateProjection(); m_Camera->CalculateProjection();
m_Camera->CalculateView(); m_Camera->CalculateView();
Light::Renderer::BeginScene(m_Camera, m_Framebuffer); Renderer::BeginScene(m_Camera, m_Framebuffer);
for (int i = 0; i < 100; i++) m_Scene.OnRender();
Light::Renderer::DrawQuad(positions[i], sizes[i], m_AwesomefaceTexture);
Light::Renderer::EndScene(); Renderer::EndScene();
} }
void OnUserInterfaceUpdate() void OnUserInterfaceUpdate()
{ {
if (ImGui::Begin("GameView")) if (ImGui::Begin("GameView"))
{ {
Light::Input::ReceiveGameEvents(ImGui::IsWindowFocused()); Input::ReceiveGameEvents(ImGui::IsWindowFocused());
static ImVec2 regionAvailPrev = { 0, 0 }; static ImVec2 regionAvailPrev = { 0, 0 };
ImVec2 regionAvail = ImGui::GetContentRegionAvail(); ImVec2 regionAvail = ImGui::GetContentRegionAvail();
@ -67,7 +74,7 @@ public:
regionAvailPrev = regionAvail; regionAvailPrev = regionAvail;
} }
if (Light::GraphicsContext::GetGraphicsAPI() == Light::GraphicsAPI::DirectX) if (GraphicsContext::GetGraphicsAPI() == GraphicsAPI::DirectX)
ImGui::Image(m_Framebuffer->GetColorAttachment(), regionAvail); ImGui::Image(m_Framebuffer->GetColorAttachment(), regionAvail);
else else
ImGui::Image(m_Framebuffer->GetColorAttachment(), regionAvail, ImVec2(0, 1), ImVec2(1, 0)); ImGui::Image(m_Framebuffer->GetColorAttachment(), regionAvail, ImVec2(0, 1), ImVec2(1, 0));
@ -78,16 +85,16 @@ public:
void OnUpdate(float deltaTime) override void OnUpdate(float deltaTime) override
{ {
if (Light::Input::GetKeyboardKey(KEY_A)) if (Input::GetKeyboardKey(KEY_A))
m_Direction.x = -1.0f; m_Direction.x = -1.0f;
else if (Light::Input::GetKeyboardKey(KEY_D)) else if (Input::GetKeyboardKey(KEY_D))
m_Direction.x = 1.0f; m_Direction.x = 1.0f;
else else
m_Direction.x = 0.0f; m_Direction.x = 0.0f;
if (Light::Input::GetKeyboardKey(KEY_W)) if (Input::GetKeyboardKey(KEY_W))
m_Direction.y = 1.0f; m_Direction.y = 1.0f;
else if (Light::Input::GetKeyboardKey(KEY_S)) else if (Input::GetKeyboardKey(KEY_S))
m_Direction.y = -1.0f; m_Direction.y = -1.0f;
else else
m_Direction.y = 0.0f; m_Direction.y = 0.0f;
@ -96,3 +103,5 @@ public:
} }
}; };
}

View file

@ -34,6 +34,7 @@ project "Sandbox"
(dependenciesdir .. "imgui/"), (dependenciesdir .. "imgui/"),
(dependenciesdir .. "imgui/backends"), (dependenciesdir .. "imgui/backends"),
(dependenciesdir .. "glm/"), (dependenciesdir .. "glm/"),
(dependenciesdir .. "entt/"),
} }
links links