- Added UUID
- Fixed SceneSerializer texture de-serialization with a temporary solution
- Added GetEntityByTag to Scene
- Added 'editorlayer.yaml' as a Scene to the Mirror project
- EditorLayer no longer generates entities, rather it loads them from 'editorlayer.yaml'
This commit is contained in:
Light 2021-10-07 01:13:18 +03:30
parent 74a6035c32
commit 8447e5dab2
13 changed files with 2663 additions and 50 deletions

View file

@ -0,0 +1,13 @@
#include "ltpch.h"
#include "UUID.h"
namespace Light {
std::mt19937_64 UUID::s_Engine = std::mt19937_64(std::random_device()());
std::uniform_int_distribution<uint64_t> UUID::s_UniformDistribution;
UUID::UUID(uint64_t uuid /* = -1 */)
: m_UUID(uuid == -1 ? s_UniformDistribution(s_Engine) : uuid)
{
}
}

View file

@ -0,0 +1,35 @@
#pragma once
#include <random>
namespace Light {
class UUID
{
private:
static std::mt19937_64 s_Engine;
static std::uniform_int_distribution<uint64_t> s_UniformDistribution;
private:
uint64_t m_UUID;
public:
UUID(uint64_t uuid = -1);
operator uint64_t() const { return m_UUID; }
};
}
namespace std {
template <>
struct hash<Light::UUID>
{
std::size_t operator()(const Light::UUID& uuid) const
{
return hash<uint64_t>()(static_cast<uint64_t>(uuid));
}
};
}

View file

@ -0,0 +1,17 @@
#pragma once
#include "Base/Base.h"
#include "Core/UUID.h"
namespace Light {
struct UUIDComponent
{
UUID uuid;
UUIDComponent(UUID _uuid): uuid(_uuid) {}
UUIDComponent(const UUIDComponent&) = default;
};
}

View file

@ -4,6 +4,8 @@
#include "Scene.h"
#include "Components/UUIDComponent.h"
#include <entt/entt.hpp>
namespace Light {
@ -42,6 +44,8 @@ namespace Light {
m_Scene->m_Registry.remove<T>(m_Handle);
}
inline uint64_t GetUUID() { return GetComponent<UUIDComponent>().uuid; }
inline bool IsValid() const { return m_Handle != entt::null && m_Scene != nullptr; }
operator uint32_t() { return (uint32_t)m_Handle; }

View file

@ -84,10 +84,39 @@ namespace Light {
}
Entity Scene::CreateEntity(const std::string& name, const TransformComponent& transform)
{
return CreateEntityWithUUID(name, UUID(), transform);
}
Entity Scene::GetEntityByTag(const std::string& tag)
{
// TagComponent tagComp(tag);
// entt::entity entity = entt::to_entity(m_Registry, tagComp);
Entity entity;
m_Registry.view<TagComponent>().
each([&](TagComponent& tagComp)
{
if (tagComp.tag == tag)
entity = Entity(entt::to_entity(m_Registry, tagComp), this);
});
if (entity.IsValid())
return entity;
else {
LT_ENGINE_ERROR("Scene::GetEntityByTag: failed to find entity by tag: {}", tag);
return Entity();
}
}
Entity Scene::CreateEntityWithUUID(const std::string& name, UUID uuid, const TransformComponent& transform)
{
Entity entity { m_Registry.create(), this };
entity.AddComponent<TagComponent>(name);
entity.AddComponent<TransformComponent>(transform);
entity.AddComponent<UUIDComponent>(uuid);
return entity;
}

View file

@ -2,6 +2,8 @@
#include "Base/Base.h"
#include "Core/UUID.h"
#include "Components/TransformComponent.h"
#include <entt/entt.hpp>
@ -34,6 +36,11 @@ namespace Light {
void OnRender(const Ref<Framebuffer>& targetFrameBuffer = nullptr);
Entity CreateEntity(const std::string& name, const TransformComponent& transform = TransformComponent());
Entity GetEntityByTag(const std::string& tag);
private:
Entity CreateEntityWithUUID(const std::string& name, UUID uuid, const TransformComponent& transform = TransformComponent());
};
}

View file

@ -16,14 +16,14 @@ namespace Light {
return MakeScope(new ResourceManager());
}
ResourceManager::ResourceManager():
m_Shaders{},
m_Textures{}
ResourceManager::ResourceManager() :
m_Shaders{},
m_Textures{}
{
LT_ENGINE_ASSERT(!s_Context, "ResourceManager::ResourceManager: repeated singleton construction");
s_Context = this;
}
void ResourceManager::LoadShaderImpl(const std::string& name, const std::string& vertexPath, const std::string& pixelPath)
{
// check
@ -55,7 +55,7 @@ namespace Light {
ImageFileHandle imgFile = FileManager::ReadImageFile(path, desiredComponents);
// create texture
m_Textures[name] = Ref<Texture>(Texture::Create(imgFile.GetWidth(), imgFile.GetHeight(), imgFile.GetComponents(), imgFile.GetData(), GraphicsContext::GetSharedContext(), path) );
m_Textures[name] = Ref<Texture>(Texture::Create(imgFile.GetWidth(), imgFile.GetHeight(), imgFile.GetComponents(), imgFile.GetData(), GraphicsContext::GetSharedContext(), path));
// free file
imgFile.Release();
@ -71,4 +71,5 @@ namespace Light {
m_Textures[name] = nullptr;
}
}

View file

@ -15,7 +15,6 @@ namespace Light {
static ResourceManager* s_Context;
private:
std::unordered_map<std::string, Ref<Shader>> m_Shaders;
std::unordered_map<std::string, Ref<Texture>> m_Textures;

View file

@ -5,6 +5,8 @@
#include "Graphics/Texture.h"
#include "Utility/ResourceManager.h"
namespace YAML {
template<>
@ -46,7 +48,7 @@ namespace YAML {
static bool decode(const Node& node, glm::vec4& rhs)
{
if (!node.IsSequence() || node.size() != 3)
if (!node.IsSequence() || node.size() != 4)
return false;
rhs.x = node[0].as<float>();
@ -119,6 +121,11 @@ namespace Light {
auto entities = data["Entities"];
if (entities)
{
/* #TEMPORARY SOLUTION# */
std::unordered_set<std::string> texturePaths;
/* #TEMPORARY SOLUTION# */
for (auto entity : entities)
{
uint64_t uuid = entity["Entity"].as<uint64_t>(); // #todo
@ -130,8 +137,10 @@ namespace Light {
LT_ENGINE_TRACE("SceneSerializer::Deserialize: Deserialized entity '{}' : '{}'", uuid, name);
Entity deserializedEntity = m_Scene->CreateEntity(name);
Entity deserializedEntity = m_Scene->CreateEntityWithUUID(name, uuid);
TagComponent gg = deserializedEntity.GetComponent<TagComponent>();
LT_ENGINE_TRACE(gg.tag);
auto transformComponent = entity["TransformComponent"];
if (transformComponent)
{
@ -142,17 +151,36 @@ namespace Light {
entityTransforomComponent.scale = transformComponent["Scale"].as<glm::vec3>();
}
/* #TEMPORARY SOLUTION# */
auto spriteRendererComponent = entity["SpriteRendererComponent"];
if (spriteRendererComponent)
{
auto& entitySpriteRendererComponent = deserializedEntity.AddComponent<SpriteRendererComponent>();
entitySpriteRendererComponent.tint = spriteRendererComponent["Tint"].as<glm::vec4>();
std::string texturePath = spriteRendererComponent["Texture"].as<std::string>();
if (!texturePaths.contains(texturePath))
{
ResourceManager::LoadTexture(texturePath, texturePath);
texturePaths.insert(texturePath);
}
entitySpriteRendererComponent.texture = ResourceManager::GetTexture(texturePath);
}
/* #TEMPORARY SOLUTION# */
auto cameraComponent = entity["CameraComponent"];
if(cameraComponent)
{
auto& entityCameraComponent = deserializedEntity.AddComponent<CameraComponent>();
auto& cameraSpecifications = cameraComponent["Camera"];
const auto& cameraSpecifications = cameraComponent["Camera"];
entityCameraComponent.camera.SetProjectionType((SceneCamera::ProjectionType)cameraSpecifications["ProjectionType"].as<int>());
entityCameraComponent.camera.SetOrthographicSize(cameraSpecifications["OrthographicsSize"].as<float>());
entityCameraComponent.camera.SetOrthographicNearPlane(cameraSpecifications["OrthographicsNearPlane"].as<float>());
entityCameraComponent.camera.SetOrthographicFarPlane(cameraSpecifications["OrthographicsFarPlane"].as<float>());
entityCameraComponent.camera.SetOrthographicSize(cameraSpecifications["OrthographicSize"].as<float>());
entityCameraComponent.camera.SetOrthographicNearPlane(cameraSpecifications["OrthographicNearPlane"].as<float>());
entityCameraComponent.camera.SetOrthographicFarPlane(cameraSpecifications["OrthographicFarPlane"].as<float>());
entityCameraComponent.camera.SetPerspectiveVerticalFOV(cameraSpecifications["PerspectiveVerticalFOV"].as<float>());
entityCameraComponent.camera.SetPerspectiveNearPlane(cameraSpecifications["PerspectiveNearPlane"].as<float>());
@ -162,9 +190,9 @@ namespace Light {
entityCameraComponent.isPrimary = cameraComponent["IsPrimary"].as<bool>();
}
// #todo: figure out texture de-serialization
}
return true;
}
return false;
@ -184,8 +212,7 @@ namespace Light {
void SceneSerializer::SerializeEntity(YAML::Emitter& out, Entity entity)
{
out << YAML::BeginMap; // entity
out << YAML::Key << "Entity" << YAML::Value << "69696969696969"; // dummy uuid
out << YAML::EndMap; // entity
out << YAML::Key << "Entity" << YAML::Value << entity.GetUUID(); // dummy uuid
if (entity.HasComponent<TagComponent>())
{
@ -251,6 +278,7 @@ namespace Light {
out << YAML::EndMap; // camera component
}
out << YAML::EndMap; // entity
}
}

View file

@ -19,7 +19,6 @@ namespace Light {
void SerializeBinary(const std::string& filePath);
bool DeserializeBinary(const std::string& filePath);
private:
void SerializeEntity(YAML::Emitter& out, Entity entity);

File diff suppressed because it is too large Load diff

View file

@ -12,18 +12,12 @@ namespace Light {
m_PropertiesPanel = CreateRef<PropertiesPanel>();
m_SceneHierarchyPanel = CreateRef<SceneHierarchyPanel>(m_Scene, m_PropertiesPanel);
SummonAwesomeness();
m_Framebuffer = Framebuffer::Create({ 1, 1, 1 }, GraphicsContext::GetSharedContext());
m_CameraEntity = m_Scene->CreateEntity("Game Camera", TransformComponent({0.0f, 0.0f, 1000.0f}));
m_CameraEntity.AddComponent<CameraComponent>(SceneCamera(), true);
// for native script components
m_Scene->OnCreate();
SceneSerializer serializer(m_Scene);
serializer.Serialize("assets/scenes/test.yaml");
serializer.Deserialize("../../Mirror/res/Scenes/editorlayer.yaml");
m_CameraEntity = m_Scene->GetEntityByTag("Game Camera");
}
void EditorLayer::OnUpdate(float deltaTime)
@ -79,22 +73,4 @@ namespace Light {
UserInterface::DockspaceEnd();
}
void EditorLayer::SummonAwesomeness()
{
ResourceManager::LoadTexture("awesomeface", "../../Mirror/res/Textures/awesomeface.png");
auto texture = ResourceManager::GetTexture("awesomeface");
for(unsigned int i = 0; i < 250; i++)
{
const glm::vec3 translation = Math::RandVec3(-500, 500);
const glm::vec3 scale = glm::vec3(Math::Rand(125, 200));
const glm::vec3 rotation = glm::vec3(0.0f, 0.0f, glm::radians(Math::Rand(0, 359)));
const glm::vec4 tint = glm::vec4(Math::RandVec3(0, 1, 2), 1.0f);
auto entity = m_Scene->CreateEntity("quad" + std::to_string(i), { translation, scale, rotation });
entity.AddComponent<SpriteRendererComponent>(texture, tint);
}
}
}

View file

@ -16,8 +16,6 @@ namespace Light {
glm::vec2 m_Direction;
float m_Speed = 1000.0f;
std::vector<Entity> m_Entities;
Ref<Scene> m_Scene;
Ref<SceneHierarchyPanel> m_SceneHierarchyPanel;
@ -37,9 +35,6 @@ namespace Light {
void OnRender() override;
void OnUserInterfaceUpdate() override;
private:
void SummonAwesomeness();
};
}