Add Scene with systems

- Add Scene class which populates the engine with Systems which handles
  all logic in the game
- Add Systems to ecs
This commit is contained in:
Thraix
2023-05-20 19:45:15 +02:00
parent 05d2c2940b
commit 84b24457a0
20 changed files with 626 additions and 42 deletions
+14
View File
@@ -172,16 +172,20 @@
<ClCompile Include="src\copium\asset\Asset.cpp" />
<ClCompile Include="src\copium\asset\AssetFile.cpp" />
<ClCompile Include="src\copium\asset\AssetManager.cpp" />
<ClCompile Include="src\copium\asset\AssetRef.cpp" />
<ClCompile Include="src\copium\buffer\RendererVertexBuffer.cpp" />
<ClCompile Include="src\copium\core\Application.cpp" />
<ClCompile Include="src\copium\buffer\Buffer.cpp" />
<ClCompile Include="src\copium\core\Device.cpp" />
<ClCompile Include="src\copium\core\Scene.cpp" />
<ClCompile Include="src\copium\core\Vulkan.cpp" />
<ClCompile Include="src\copium\core\Window.cpp" />
<ClCompile Include="src\copium\ecs\ComponentPoolBase.cpp" />
<ClCompile Include="src\copium\ecs\ECSManager.cpp" />
<ClCompile Include="src\copium\ecs\Entity.cpp" />
<ClCompile Include="src\copium\ecs\EntitySet.cpp" />
<ClCompile Include="src\copium\ecs\SystemOrderer.cpp" />
<ClCompile Include="src\copium\ecs\SystemPool.cpp" />
<ClCompile Include="src\copium\event\Event.cpp" />
<ClCompile Include="src\copium\event\EventDispatcher.cpp" />
<ClCompile Include="src\copium\event\KeyPressEvent.cpp" />
@@ -236,8 +240,10 @@
<ClInclude Include="src\copium\asset\AssetFile.h" />
<ClInclude Include="src\copium\asset\AssetManager.h" />
<ClInclude Include="src\copium\asset\AssetMeta.h" />
<ClInclude Include="src\copium\asset\AssetRef.h" />
<ClInclude Include="src\copium\buffer\RendererVertexBuffer.h" />
<ClInclude Include="src\copium\core\Device.h" />
<ClInclude Include="src\copium\core\Scene.h" />
<ClInclude Include="src\copium\core\Vulkan.h" />
<ClInclude Include="src\copium\core\Window.h" />
<ClInclude Include="src\copium\ecs\ComponentPool.h" />
@@ -246,6 +252,10 @@
<ClInclude Include="src\copium\ecs\ECSManager.h" />
<ClInclude Include="src\copium\ecs\Entity.h" />
<ClInclude Include="src\copium\ecs\EntitySet.h" />
<ClInclude Include="src\copium\ecs\System.h" />
<ClInclude Include="src\copium\ecs\SystemBase.h" />
<ClInclude Include="src\copium\ecs\SystemOrderer.h" />
<ClInclude Include="src\copium\ecs\SystemPool.h" />
<ClInclude Include="src\copium\event\Event.h" />
<ClInclude Include="src\copium\event\EventDispatcher.h" />
<ClInclude Include="src\copium\event\EventHandler.h" />
@@ -259,6 +269,10 @@
<ClInclude Include="src\copium\event\MouseScrollEvent.h" />
<ClInclude Include="src\copium\event\WindowFocusEvent.h" />
<ClInclude Include="src\copium\event\WindowResizeEvent.h" />
<ClInclude Include="src\copium\example\Components.h" />
<ClInclude Include="src\copium\example\FrameCountSystem.h" />
<ClInclude Include="src\copium\example\RenderSystem.h" />
<ClInclude Include="src\copium\example\MouseFollowSystem.h" />
<ClInclude Include="src\copium\mesh\Mesh.h" />
<ClInclude Include="src\copium\pipeline\ShaderBinding.h" />
<ClInclude Include="src\copium\renderer\Batch.h" />
+42
View File
@@ -198,6 +198,18 @@
<ClCompile Include="src\copium\ecs\Entity.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\copium\core\Scene.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\copium\asset\AssetRef.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\copium\ecs\SystemPool.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\copium\ecs\SystemOrderer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\copium\sampler\DepthAttachment.h">
@@ -410,5 +422,35 @@
<ClInclude Include="src\copium\ecs\ComponentPoolBase.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\copium\core\Scene.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\copium\asset\AssetRef.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\copium\ecs\System.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\copium\ecs\SystemPool.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\copium\ecs\SystemBase.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\copium\ecs\SystemOrderer.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\copium\example\FrameCountSystem.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\copium\example\Components.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\copium\example\MouseFollowSystem.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\copium\example\RenderSystem.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
@@ -0,0 +1,58 @@
#include "copium/asset/AssetRef.h"
#include "copium/asset/AssetManager.h"
namespace Copium
{
AssetRef::AssetRef(AssetHandle handle)
: handle{handle}, refCounter{new int{1}}
{}
AssetRef::~AssetRef()
{
if (refCounter == nullptr)
return;
(*refCounter)--;
if (*refCounter == 0)
{
AssetManager::UnloadAsset(handle);
delete refCounter;
}
}
AssetRef::AssetRef(const AssetRef& other)
: handle{other.handle}, refCounter{other.refCounter}
{
(*refCounter)++;
}
AssetRef::AssetRef(AssetRef&& other)
: handle{other.handle}, refCounter{other.refCounter}
{
other.refCounter = nullptr;
}
AssetRef& AssetRef::operator=(const AssetRef& rhs)
{
handle = rhs.handle;
refCounter = rhs.refCounter;
(*refCounter)++;
return *this;
}
AssetRef& AssetRef::operator=(AssetRef&& rhs)
{
handle = rhs.handle;
refCounter = rhs.refCounter;
rhs.refCounter = nullptr;
return *this;
}
AssetRef::operator AssetHandle() const
{
return handle;
}
}
+26
View File
@@ -0,0 +1,26 @@
#pragma once
#include "copium/asset/AssetMeta.h"
namespace Copium
{
class AssetRef
{
private:
AssetHandle handle;
int* refCounter;
public:
AssetRef(AssetHandle handle);
~AssetRef();
AssetRef(const AssetRef& other);
AssetRef(AssetRef&& other);
AssetRef& operator=(const AssetRef& rhs);
AssetRef& operator=(AssetRef&& rhs);
operator AssetHandle() const;
};
}
+5 -37
View File
@@ -48,20 +48,18 @@ namespace Copium
{
EventDispatcher::AddEventHandler(this);
InitializeFrameBuffer();
InitializeRenderer();
InitializeGraphicsPipeline();
InitializeTextureSampler();
InitializeDescriptorSets();
InitializeMesh();
InitializeCommandBuffer();
InitializeScene();
}
Application::~Application()
{
vkDeviceWaitIdle(Vulkan::GetDevice());
AssetManager::UnloadAsset(texture2D);
AssetManager::UnloadAsset(texture2D2);
AssetManager::UnloadAsset(font);
AssetManager::UnloadAsset(graphicsPipeline);
AssetManager::UnloadAsset(graphicsPipelinePassthrough);
AssetManager::UnloadAsset(framebuffer);
@@ -92,6 +90,7 @@ namespace Copium
EventResult Application::OnEvent(const Event& event)
{
scene->OnEvent(event);
switch (event.GetType())
{
case EventType::WindowResize:
@@ -150,16 +149,14 @@ namespace Copium
framebuffer = AssetManager::LoadAsset<Framebuffer>("framebuffer.meta");
}
void Application::InitializeRenderer()
void Application::InitializeScene()
{
renderer = std::make_unique<Renderer>();
scene = std::make_unique<Scene>(*commandBuffer, *descriptorPool);
}
void Application::InitializeTextureSampler()
{
texture2D = AssetManager::LoadAsset<Texture2D>("fox.meta");
texture2D2 = AssetManager::LoadAsset<Texture2D>("fox2.meta");
font = AssetManager::LoadAsset<Font>("font.meta");
}
void Application::InitializeDescriptorSets()
@@ -171,8 +168,6 @@ namespace Copium
descriptorSetPassthrough = AssetManager::GetAsset<Pipeline>(graphicsPipelinePassthrough).CreateDescriptorSet(*descriptorPool, 0);
descriptorSetPassthrough->SetSampler(AssetManager::GetAsset<Framebuffer>(framebuffer).GetColorAttachment(), 0);
descriptorSetRenderer = renderer->GetGraphicsPipeline().CreateDescriptorSet(*descriptorPool, 1);
}
void Application::InitializeGraphicsPipeline()
@@ -209,25 +204,7 @@ namespace Copium
mesh->Bind(*commandBuffer);
mesh->Render(*commandBuffer);
renderer->SetDescriptorSet(*descriptorSetRenderer);
renderer->Begin(*commandBuffer);
for (int y = 0; y < 10; y++)
{
for (int x = 0; x < 10; x++)
{
renderer->Quad(glm::vec2{-1 + x * 0.2 + 0.05, -1 + y * 0.2 + 0.05}, glm::vec2{0.1, 0.1}, glm::vec3{x * 0.1, y * 0.1, 1.0});
}
}
float aspect = fb.GetWidth() / (float)fb.GetHeight();
renderer->Quad(glm::vec2{-0.8, -0.4}, glm::vec2{0.8, 0.8}, AssetManager::GetAsset<Texture2D>(texture2D));
renderer->Quad(glm::vec2{ 0.1, -0.4}, glm::vec2{0.8, 0.8}, AssetManager::GetAsset<Font>(font));
renderer->Quad(mousePos - glm::vec2(0.1), glm::vec2{0.2}, AssetManager::GetAsset<Texture2D>(texture2D2));
std::string s = std::to_string(fps) + " fps";
BoundingBox boundingBox = AssetManager::GetAsset<Font>(font).GetTextBoundingBox(s, 0.06);
glm::vec2 pos = glm::vec2{-aspect + 0.01, 0.94};
renderer->Quad(pos + boundingBox.lb, boundingBox.GetSize());
renderer->Text(s, pos, AssetManager::GetAsset<Font>(font), 0.06, glm::vec3{0.0f});
renderer->End();
scene->Update();
fb.Unbind(*commandBuffer);
@@ -252,7 +229,6 @@ namespace Copium
float time = startTimer.Elapsed();
Framebuffer& fb = AssetManager::GetAsset<Framebuffer>(framebuffer);
float aspect = fb.GetWidth() / (float)fb.GetHeight();
time = 0;
{
glm::mat4 projection = glm::perspective(glm::radians(45.0f), aspect, 0.1f, 10.0f);
@@ -265,13 +241,5 @@ namespace Copium
uniformBuffer.Set("lightPos", (glm::vec3)(glm::rotate(glm::mat4{1.0f}, time * glm::radians(45.0f), glm::vec3(0, 1, 0)) * glm::vec4{0.3, 0.1, 0, 1}));
uniformBuffer.Update();
}
{
UniformBuffer& uniformBuffer = descriptorSetRenderer->GetUniformBuffer("ubo");
uniformBuffer.Set("projection", glm::ortho(-aspect, aspect, 1.0f, -1.0f));
// uniformBuffer.Set("view", glm::translate(glm::mat4(1), glm::vec3(0.1 * glm::sin(4 * time), 0.1 * glm::cos(4 * time), 0.0)));
uniformBuffer.Set("view", glm::mat4(1));
uniformBuffer.Update();
}
}
}
+3 -3
View File
@@ -1,6 +1,7 @@
#pragma once
#include "copium/asset/AssetMeta.h"
#include "copium/core/Scene.h"
#include "copium/buffer/Framebuffer.h"
#include "copium/event/EventHandler.h"
#include "copium/mesh/Mesh.h"
@@ -15,7 +16,6 @@ namespace Copium
{
CP_DELETE_COPY_AND_MOVE_CTOR(Application);
private:
std::unique_ptr<Renderer> renderer;
AssetHandle framebuffer;
AssetHandle texture2D;
AssetHandle texture2D2;
@@ -25,7 +25,7 @@ namespace Copium
std::unique_ptr<DescriptorPool> descriptorPool;
std::unique_ptr<DescriptorSet> descriptorSet;
std::unique_ptr<DescriptorSet> descriptorSetPassthrough;
std::unique_ptr<DescriptorSet> descriptorSetRenderer;
std::unique_ptr<Scene> scene;
std::unique_ptr<Mesh> mesh;
std::unique_ptr<Mesh> meshPassthrough;
std::unique_ptr<CommandBuffer> commandBuffer;
@@ -42,7 +42,7 @@ namespace Copium
EventResult OnEvent(const Event& event) override;
private:
void InitializeFrameBuffer();
void InitializeRenderer();
void InitializeScene();
void InitializeTextureSampler();
void InitializeDescriptorSets();
void InitializeGraphicsPipeline();
+73
View File
@@ -0,0 +1,73 @@
#include "copium/core/Scene.h"
#include "copium/asset/AssetManager.h"
#include "copium/asset/AssetMeta.h"
#include "copium/asset/AssetRef.h"
#include "copium/core/Vulkan.h"
#include "copium/ecs/Entity.h"
#include "copium/ecs/System.h"
#include "copium/event/MouseMoveEvent.h"
#include "copium/example/FrameCountSystem.h"
#include "copium/example/MouseFollowSystem.h"
#include "copium/example/RenderSystem.h"
#include "copium/example/Components.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <string>
namespace Copium
{
Scene::Scene(CommandBuffer& commandBuffer, DescriptorPool& descriptorPool)
{
renderer = std::make_unique<Renderer>();
descriptorSetRenderer = renderer->GetGraphicsPipeline().CreateDescriptorSet(descriptorPool, 1);
ecs = std::make_unique<ECSManager>();
ecs->AddSystem<FrameCountSystem>();
ecs->AddSystem<RenderSystem>(renderer.get(), descriptorSetRenderer.get(), &commandBuffer); // better way to store the RenderSystem data?
// TODO: Load from scene file
for (int y = 0; y < 10; y++)
{
for (int x = 0; x < 10; x++)
{
Entity entity = Entity::Create(ecs.get());
entity.AddComponent<TransformC>(glm::vec2{-1 + x * 0.2 + 0.05, -1 + y * 0.2 + 0.05}, glm::vec2{0.1, 0.1});
entity.AddComponent<ColorC>(glm::vec3{x * 0.1f, y * 0.1f, 1.0f});
}
}
float aspect = Vulkan::GetSwapChain().GetExtent().width / (float)Vulkan::GetSwapChain().GetExtent().height;
Entity entityFox = Entity::Create(ecs.get());
entityFox.AddComponent<TransformC>(glm::vec2{-0.9f, -0.4f}, glm::vec2{0.8f, 0.8f});
entityFox.AddComponent<TextureC>(AssetRef{AssetManager::LoadAsset("fox.meta")}, glm::vec2{0.0f, 0.0f}, glm::vec2{1.0f, 1.0f});
Entity entityFontAtlas = Entity::Create(ecs.get());
entityFontAtlas.AddComponent<TransformC>(glm::vec2{0.1f, -0.4f}, glm::vec2{0.8, 0.8});
entityFontAtlas.AddComponent<TextureC>(AssetRef{AssetManager::LoadAsset("font.meta")}, glm::vec2{0.0f, 0.0f}, glm::vec2{1.0f, 1.0f});
Entity entityMouse = Entity::Create(ecs.get());
entityMouse.AddComponent<TransformC>(glm::vec2(0.1), glm::vec2{0.2});
entityMouse.AddComponent<TextureC>(AssetRef{AssetManager::LoadAsset("fox2.meta")}, glm::vec2{0.0f, 0.0f}, glm::vec2{1.0f, 1.0f});
entityMouse.AddComponent<MouseFollowC>();
glm::vec2 pos = glm::vec2{-aspect + 0.01, 0.94};
Entity entityText = Entity::Create(ecs.get());
entityText.AddComponent<TransformC>(glm::vec2{-aspect + 0.01, 0.94}, glm::vec2{1.0});
entityText.AddComponent<TextC>(AssetRef{AssetManager::LoadAsset("font.meta")}, std::to_string(0) + " fps", 0.06f);
entityText.AddComponent<FrameCountC>();
}
void Scene::Update()
{
ecs->UpdateSystems();
}
EventResult Scene::OnEvent(const Event& event)
{
// ecs->UpdateEventSystems(event);
MouseFollowSystem{ecs.get(), event}.Run(); // TODO: Remove when I figure out how to handle events in systems
return EventResult::Continue;
}
}
+25
View File
@@ -0,0 +1,25 @@
#pragma once
#include "copium/renderer/Renderer.h"
#include "copium/ecs/ECSManager.h"
#include "copium/event/Event.h"
#include "copium/event/EventResult.h"
#include <memory>
namespace Copium
{
class Scene
{
private:
std::unique_ptr<Renderer> renderer;
std::unique_ptr<ECSManager> ecs;
std::unique_ptr<DescriptorSet> descriptorSetRenderer;
int frameCounter = 0;
int fps = 0;
public:
Scene(CommandBuffer& commandBuffer, DescriptorPool& descriptorPool);
void Update();
EventResult OnEvent(const Event& event);
};
}
@@ -4,6 +4,10 @@
namespace Copium
{
ECSManager::ECSManager()
: systemPool{std::make_unique<SystemPool>(this)}
{}
ECSManager::~ECSManager()
{
for (auto&& components : componentPool)
@@ -13,6 +17,11 @@ namespace Copium
componentPool.clear();
}
void ECSManager::UpdateSystems()
{
systemPool->Update();
}
size_t ECSManager::GetEntityCount() const
{
return entities.size();
+14 -2
View File
@@ -3,6 +3,7 @@
#include "copium/ecs/ComponentPool.h"
#include "copium/ecs/Config.h"
#include "copium/ecs/SystemPool.h"
#include "copium/util/Common.h"
#include <functional>
@@ -18,10 +19,21 @@ namespace Copium
private:
std::unordered_set<EntityID> entities;
std::map<std::type_index, ComponentPoolBase*> componentPool;
std::unique_ptr<SystemPool> systemPool;
int currentEntityId = 1;
public:
ECSManager();
~ECSManager();
template <typename SystemClass, typename... Args>
SystemOrderer AddSystem(const Args&... args)
{
return systemPool->AddSystem(typeid(SystemClass), new SystemClass{args...});
}
void UpdateSystems();
EntityID CreateEntity();
void DestroyEntity(EntityID entity);
size_t GetEntityCount() const;
@@ -37,7 +49,7 @@ namespace Copium
template <typename Component, typename... Args>
Component& AddComponent(EntityID entity, Args&&... args)
{
return AddComponent(entity, Component(args...));
return AddComponent(entity, Component{args...});
}
template <typename Component>
@@ -76,7 +88,7 @@ namespace Copium
{
auto pool = GetComponentPoolAssure<Component>();
Component* component = pool->FindComponent(entity);
ASSERT(component, "Entity did not contain component (entity=%u, Component=%s)", entity, typeid(Component).name());
CP_ASSERT(component, "Entity did not contain component (entity=%u, Component=%s)", entity, typeid(Component).name());
return *component;
}
+20
View File
@@ -0,0 +1,20 @@
#pragma once
#include "copium/ecs/ECSManager.h"
#include "copium/ecs/SystemBase.h"
#include "copium/ecs/Entity.h"
namespace Copium
{
template <typename... Components>
class System : public SystemBase
{
public:
void Run() override
{
manager->Each<Components...>([&](EntityID entityId, Components&... components) { RunEntity(Entity{manager, entityId}, components...); });
}
virtual void RunEntity(Entity entity, Components&... components) = 0;
};
}
+16
View File
@@ -0,0 +1,16 @@
#pragma once
namespace Copium
{
class ECSManager;
class SystemBase
{
friend class SystemPool;
protected:
ECSManager* manager;
public:
virtual void Run() = 0;
};
}
@@ -0,0 +1,21 @@
#include "copium/ecs/SystemOrderer.h"
#include "copium/ecs/SystemPool.h"
namespace Copium
{
SystemOrderer::SystemOrderer(std::type_index systemId, SystemPool* systemPool)
: systemId{systemId},
systemPool{systemPool}
{}
void SystemOrderer::Before(const std::type_index& otherSystemId)
{
systemPool->MoveSystemBefore(systemId, otherSystemId);
}
void SystemOrderer::After(const std::type_index& otherSystemId)
{
systemPool->MoveSystemAfter(systemId, otherSystemId);
}
}
@@ -0,0 +1,35 @@
#pragma once
#include <vector>
#include <map>
#include <typeindex>
namespace Copium
{
class SystemPool;
class SystemOrderer
{
private:
std::type_index systemId;
SystemPool* systemPool = nullptr;
public:
SystemOrderer(std::type_index systemId, SystemPool* systemPool);
template <typename Other>
void Before()
{
Before(typeid(Other));
}
template <typename Other>
void After()
{
After(typeid(Other));
}
private:
void Before(const std::type_index& otherSystemId);
void After(const std::type_index& otherSystemId);
};
}
@@ -0,0 +1,59 @@
#include "copium/ecs/SystemPool.h"
namespace Copium
{
SystemPool::SystemPool(ECSManager* manager)
: manager{manager}
{}
SystemPool::~SystemPool()
{
for (auto& system : systemOrder)
{
delete system;
}
systemOrder.clear();
systems.clear();
}
SystemOrderer SystemPool::AddSystem(const std::type_index& systemId, SystemBase* system)
{
system->manager = manager;
systems.emplace(systemId, system);
systemOrder.emplace_back(system);
return SystemOrderer{systemId, this};
}
void SystemPool::Update()
{
for (auto& system : systemOrder)
{
system->Run();
}
}
void SystemPool::MoveSystemAfter(const std::type_index& systemId, const std::type_index& afterSystemId)
{
auto it1 = systems.find(systemId);
CP_ASSERT(it1 != systems.end(), "System does not exist in SystemPool");
auto it2 = systems.find(afterSystemId);
CP_ASSERT(it2 != systems.end(), "System does not exist in SystemPool");
auto itSystemId = std::find(systemOrder.rbegin(), systemOrder.rend(), it1->second);
auto itAfterSystemId = std::find(systemOrder.rbegin(), systemOrder.rend(), it2->second);
std::rotate(itSystemId, itSystemId + 1, itAfterSystemId);
}
void SystemPool::MoveSystemBefore(const std::type_index& systemId, const std::type_index& beforeSystemId)
{
auto it1 = systems.find(systemId);
CP_ASSERT(it1 != systems.end(), "System does not exist in SystemPool");
auto it2 = systems.find(beforeSystemId);
CP_ASSERT(it2 != systems.end(), "System does not exist in SystemPool");
auto itSystemId = std::find(systemOrder.rbegin(), systemOrder.rend(), it1->second);
auto itBeforeSystemId = std::find(systemOrder.rbegin(), systemOrder.rend(), it2->second);
std::rotate(itSystemId, itSystemId + 1, itBeforeSystemId + 1);
}
}
+32
View File
@@ -0,0 +1,32 @@
#pragma once
#include "copium/ecs/SystemBase.h"
#include "copium/ecs/SystemOrderer.h"
#include "copium/util/Common.h"
#include <vector>
#include <map>
#include <typeindex>
namespace Copium
{
class ECSManager;
class SystemPool final
{
CP_DELETE_COPY_AND_MOVE_CTOR(SystemPool);
private:
ECSManager* manager;
std::map<std::type_index, SystemBase*> systems;
std::vector<SystemBase*> systemOrder;
public:
SystemPool(ECSManager* manager);
~SystemPool();
SystemOrderer AddSystem(const std::type_index& systemId, SystemBase* system);
void Update();
void MoveSystemAfter(const std::type_index& systemId, const std::type_index& afterSystemId);
void MoveSystemBefore(const std::type_index& systemId, const std::type_index& beforeSystemId);
};
}
@@ -0,0 +1,41 @@
#pragma once
#include "copium/asset/AssetRef.h"
#include <string>
#include <glm/glm.hpp>
namespace Copium
{
struct TransformC
{
glm::vec2 position;
glm::vec2 size;
};
struct ColorC
{
glm::vec3 color;
};
struct TextureC
{
AssetRef asset;
glm::vec2 texCoord1;
glm::vec2 texCoord2;
};
struct TextC
{
AssetRef font;
std::string text;
float fontSize;
};
struct MouseFollowC
{};
struct FrameCountC
{};
}
@@ -0,0 +1,34 @@
#pragma once
#include "copium/ecs/System.h"
#include "copium/example/Components.h"
namespace Copium
{
class FrameCountSystem : public System<FrameCountC, TextC>
{
private:
Timer timer;
int frameCounter = 0;
int fps = 0;
public:
void RunEntity(Entity entity, FrameCountC& frameCount, TextC& text)
{
text.text = std::to_string(fps) + " fps";
}
void Run() override
{
if (timer.Elapsed() >= 1.0)
{
fps = frameCounter;
frameCounter = 0;
timer.Start(); // Not quite accurate since the elapsed time might me 1.1, then we lose 0.1 precision
System::Run();
}
frameCounter++;
}
};
}
@@ -0,0 +1,34 @@
#pragma once
#include "copium/core/Vulkan.h"
#include "copium/ecs/System.h"
#include "copium/event/Event.h"
#include "copium/event/MouseMoveEvent.h"
#include "copium/example/Components.h"
namespace Copium
{
class MouseFollowSystem : public System<MouseFollowC, TransformC>
{
public:
const Event& event;
public:
MouseFollowSystem(ECSManager* manager, const Event& event)
: event{event}
{
System::manager = manager;
}
void RunEntity(Entity entity, MouseFollowC& mouseFollow, TransformC& transform)
{
if (event.GetType() == EventType::MouseMove)
{
const MouseMoveEvent& mouseMoveEvent = static_cast<const MouseMoveEvent&>(event);
float aspect = Vulkan::GetSwapChain().GetExtent().width / (float)Vulkan::GetSwapChain().GetExtent().height;
transform.position = {(mouseMoveEvent.GetPos().x / Vulkan::GetSwapChain().GetExtent().width - 0.5) * 2.0 * aspect,
-(mouseMoveEvent.GetPos().y / Vulkan::GetSwapChain().GetExtent().height - 0.5) * 2.0};
transform.position -= transform.size * glm::vec2{0.5f};
}
}
};
}
@@ -0,0 +1,65 @@
#pragma once
#include "copium/ecs/System.h"
#include "copium/example/Components.h"
#include "copium/renderer/Renderer.h"
#include "copium/asset/AssetManager.h"
#include "copium/core/Vulkan.h"
#include <glm/gtc/matrix_transform.hpp>
namespace Copium
{
class RenderSystem : public System<TransformC>
{
private:
Renderer* renderer;
DescriptorSet* descriptorSet;
CommandBuffer* commandBuffer;
public:
RenderSystem(Renderer* renderer, DescriptorSet* descriptorSet, CommandBuffer* commandBuffer)
: renderer{renderer},
descriptorSet{descriptorSet},
commandBuffer{commandBuffer}
{}
void RunEntity(Entity entity, TransformC& transform)
{
if (entity.HasComponent<TextC>())
{
const TextC& text = entity.GetComponent<TextC>();
renderer->Text(text.text, transform.position, AssetManager::GetAsset<Font>(text.font), text.fontSize);
}
else if (entity.HasComponent<ColorC>())
{
const ColorC& color = entity.GetComponent<ColorC>();
renderer->Quad(transform.position, transform.size, color.color);
}
else if (entity.HasComponent<TextureC>())
{
const TextureC& texture = entity.GetComponent<TextureC>();
renderer->Quad(transform.position, transform.size, AssetManager::GetAsset<Sampler>(texture.asset), texture.texCoord1, texture.texCoord2);
}
}
void Run() override
{
float aspect = Vulkan::GetSwapChain().GetExtent().width / (float)Vulkan::GetSwapChain().GetExtent().height;
UniformBuffer& uniformBuffer = descriptorSet->GetUniformBuffer("ubo");
uniformBuffer.Set("projection", glm::ortho(-aspect, aspect, 1.0f, -1.0f));
// uniformBuffer.Set("view", glm::translate(glm::mat4(1), glm::vec3(0.1 * glm::sin(4 * time), 0.1 * glm::cos(4 * time), 0.0)));
uniformBuffer.Set("view", glm::mat4(1));
uniformBuffer.Update();
// Not sure how to put this in the ECSManager system handler
// Potentially have the system take in the descriptorSetRenderer as constructor parameter
// But not sure how commandBuffer can be added, since it can change each frame
// Maybe introducing Resource concept to ECSManager?
renderer->SetDescriptorSet(*descriptorSet);
renderer->Begin(*commandBuffer);
System::Run();
renderer->End();
}
};
}