Scene System
- Added Scene - Added Entity - Added Components * TransformComponent * SpriteRendererComponent - Minor fixes
This commit is contained in:
parent
b54afb6046
commit
8287b89fad
11 changed files with 277 additions and 101 deletions
|
@ -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;
|
||||||
|
|
36
Engine/src/Engine/Scene/Components.h
Normal file
36
Engine/src/Engine/Scene/Components.h
Normal 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; }
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
17
Engine/src/Engine/Scene/Entity.cpp
Normal file
17
Engine/src/Engine/Scene/Entity.cpp
Normal 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()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
30
Engine/src/Engine/Scene/Entity.h
Normal file
30
Engine/src/Engine/Scene/Entity.h
Normal 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)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
40
Engine/src/Engine/Scene/Scene.cpp
Normal file
40
Engine/src/Engine/Scene/Scene.cpp
Normal 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;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
28
Engine/src/Engine/Scene/Scene.h
Normal file
28
Engine/src/Engine/Scene/Scene.h
Normal 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);
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
|
@ -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"
|
||||||
|
|
|
@ -34,6 +34,7 @@ project "Mirror"
|
||||||
(dependenciesdir .. "imgui/"),
|
(dependenciesdir .. "imgui/"),
|
||||||
(dependenciesdir .. "imgui/backends"),
|
(dependenciesdir .. "imgui/backends"),
|
||||||
(dependenciesdir .. "glm/"),
|
(dependenciesdir .. "glm/"),
|
||||||
|
(dependenciesdir .. "entt/"),
|
||||||
}
|
}
|
||||||
|
|
||||||
links
|
links
|
||||||
|
|
|
@ -4,9 +4,11 @@
|
||||||
|
|
||||||
#include "MirrorLayer.h"
|
#include "MirrorLayer.h"
|
||||||
|
|
||||||
class Mirror : public Light::Application
|
namespace Light {
|
||||||
{
|
|
||||||
public:
|
class Mirror : public Light::Application
|
||||||
|
{
|
||||||
|
public:
|
||||||
Mirror()
|
Mirror()
|
||||||
{
|
{
|
||||||
LT_CLIENT_TRACE("Mirror::Mirror");
|
LT_CLIENT_TRACE("Mirror::Mirror");
|
||||||
|
@ -20,16 +22,18 @@ 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()
|
||||||
{
|
{
|
||||||
LT_CLIENT_TRACE("Mirror::~Mirror");
|
LT_CLIENT_TRACE("Mirror::~Mirror");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Light::Application* Light::CreateApplication()
|
::Light::Application* ::Light::CreateApplication()
|
||||||
{
|
{
|
||||||
return new Mirror();
|
return new Mirror();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,40 +1,48 @@
|
||||||
#include <LightEngine.h>
|
#include <LightEngine.h>
|
||||||
|
|
||||||
class MirrorLayer : public Light::Layer
|
namespace Light {
|
||||||
{
|
|
||||||
private:
|
class MirrorLayer : public Layer
|
||||||
std::shared_ptr<Light::Texture> m_AwesomefaceTexture;
|
{
|
||||||
|
private:
|
||||||
|
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;
|
||||||
|
@ -95,4 +102,6 @@ public:
|
||||||
m_Camera->Move(m_Direction * m_Speed * deltaTime);
|
m_Camera->Move(m_Direction * m_Speed * deltaTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
}
|
|
@ -34,6 +34,7 @@ project "Sandbox"
|
||||||
(dependenciesdir .. "imgui/"),
|
(dependenciesdir .. "imgui/"),
|
||||||
(dependenciesdir .. "imgui/backends"),
|
(dependenciesdir .. "imgui/backends"),
|
||||||
(dependenciesdir .. "glm/"),
|
(dependenciesdir .. "glm/"),
|
||||||
|
(dependenciesdir .. "entt/"),
|
||||||
}
|
}
|
||||||
|
|
||||||
links
|
links
|
||||||
|
|
Loading…
Add table
Reference in a new issue