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:
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user