Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/assetloader #10

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
**.exe
**/tempCodeRunnerFile.cpp

**/.DS_Store
.vscode/
.cache/
build/
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.0.0)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -g")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")

project(DummyEngine)
Expand Down
6 changes: 6 additions & 0 deletions DummyEngine/Core/Application/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ namespace DE {
PushLayer(m_ConsoleLayer);

SetUpCallbacks();

m_Executor = MakeThreadPoolExecutor(THREAD_COUNT);
return Unit();
}
S_TERMINATE() {
Expand All @@ -28,6 +30,7 @@ namespace DE {
delete layer;
}
m_Layers.clear();
m_Executor->WaitShutdown();
return Unit();
}

Expand Down Expand Up @@ -81,6 +84,9 @@ namespace DE {
S_METHOD_IMPL(Window&, GetWindow, (), ()) {
return *m_Window;
}
S_METHOD_IMPL(Ref<Executor>, GetExecutor, (), ()) {
return m_Executor;
}

void Application::SetUpCallbacks() {
m_EventDispatcher.AddEventListener<WindowResizeEvent>([this](WindowResizeEvent& event) { OnWindowResize(event); });
Expand Down
5 changes: 5 additions & 0 deletions DummyEngine/Core/Application/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include "DummyEngine/Core/Application/Input.h"
#include "DummyEngine/Core/Application/Window.h"
#include "DummyEngine/Core/Console/ConsoleLayer.hpp"
#include "DummyEngine/Core/Threading/Executor.hpp"

#define THREAD_COUNT 6

namespace DE {

Expand All @@ -14,6 +17,7 @@ namespace DE {
S_METHOD_DEF(Unit, OnEvent, (Event & event));
S_METHOD_DEF(Unit, Run, ());
S_METHOD_DEF(Window&, GetWindow, ());
S_METHOD_DEF(Ref<Executor>, GetExecutor, ());

private:
friend class ImGuiLayer;
Expand All @@ -29,5 +33,6 @@ namespace DE {
ImGuiLayer* m_ImGuiLayer;
ConsoleLayer* m_ConsoleLayer;
Window* m_Window;
Ref<Executor> m_Executor;
};
} // namespace DE
26 changes: 19 additions & 7 deletions DummyEngine/Core/ECS/ComponentManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,38 +55,46 @@ namespace DE {

template <typename ComponentType> void ComponentManager::SetAddHandler(std::function<void(Entity)> func) {
RegisterComponent<ComponentType>();
std::lock_guard<std::mutex> lock(m_ComponentMutex.get(INDEX(ComponentType)));
m_AddHandlers[INDEX(ComponentType)] = func;
}
template <typename ComponentType> void ComponentManager::SetRemoveHandler(std::function<void(Entity)> func) {
RegisterComponent<ComponentType>();
std::lock_guard<std::mutex> lock(m_ComponentMutex.get(INDEX(ComponentType)));
m_RemoveHandlers[INDEX(ComponentType)] = func;
}

template <typename ComponentType> ComponentType* ComponentManager::AddComponent(U32 entity_id, const ComponentType& component) {
RegisterComponent<ComponentType>();
ValidateSignature(entity_id);
m_ComponentMutex.get(INDEX(ComponentType)).lock();
m_Signatures[entity_id].Set(m_ComponentId[INDEX(ComponentType)], true);
auto* c = reinterpret_cast<ComponentType*>(
m_ComponentArrays[INDEX(ComponentType)]->AddComponent(entity_id, const_cast<ComponentType*>(&component)));
m_AddHandlers[INDEX(ComponentType)](m_Storage->GetEntity(entity_id));
auto& addHandler = m_AddHandlers[INDEX(ComponentType)];
m_ComponentMutex.get(INDEX(ComponentType)).unlock();
addHandler(m_Storage->GetEntity(entity_id));
return c;
}
template <typename ComponentType> ComponentType* ComponentManager::GetComponent(U32 entity_id) {
ValidateSignature(entity_id);
if (!HasComponent<ComponentType>(entity_id)) {
return nullptr;
}
std::lock_guard<std::mutex> lock(m_ComponentMutex.get(INDEX(ComponentType)));
return reinterpret_cast<ComponentType*>(m_ComponentArrays[INDEX(ComponentType)]->GetComponent(entity_id));
}
template <typename ComponentType> void ComponentManager::RemoveComponent(U32 entity_id) {
ValidateSignature(entity_id);
if (HasComponent<ComponentType>(entity_id)) {
std::lock_guard<std::mutex> lock(m_ComponentMutex.get(INDEX(ComponentType)));
m_RemoveHandlers[INDEX(ComponentType)](m_Storage->GetEntity(entity_id));
m_Signatures[entity_id].Set(m_ComponentId[INDEX(ComponentType)], false);
m_ComponentArrays[INDEX(ComponentType)]->RemoveComponent(entity_id);
}
}
template <typename ComponentType> bool ComponentManager::HasComponent(U32 entity_id) const {
template <typename ComponentType> bool ComponentManager::HasComponent(U32 entity_id) {
std::lock_guard<std::mutex> lock(m_ComponentMutex.get(INDEX(ComponentType)));
if (m_Signatures.size() < entity_id + 1 || m_ComponentId.find(INDEX(ComponentType)) == m_ComponentId.end()) {
return false;
}
Expand All @@ -103,27 +111,31 @@ namespace DE {
return GetSignature<Components...>();
}

template <typename... Components> typename std::enable_if<sizeof...(Components) == 0, bool>::type ComponentManager::ValidateComponents() const {
template <typename... Components> typename std::enable_if<sizeof...(Components) == 0, bool>::type ComponentManager::ValidateComponents() {
return true;
}
template <typename T, typename... Components> bool ComponentManager::ValidateComponents() const {
template <typename T, typename... Components> bool ComponentManager::ValidateComponents() {
m_ComponentMutex.get(INDEX(T)).lock();
if (m_ComponentId.find(INDEX(T)) == m_ComponentId.end()) {
return false;
}
m_ComponentMutex.get(INDEX(T)).unlock();
return ValidateComponents<Components...>();
}

template <typename... Components> typename std::enable_if<sizeof...(Components) == 0, Signature>::type ComponentManager::GetSignature() const {
template <typename... Components> typename std::enable_if<sizeof...(Components) == 0, Signature>::type ComponentManager::GetSignature() {
return Signature();
}
template <typename T, typename... Components> Signature ComponentManager::GetSignature() const {
template <typename T, typename... Components> Signature ComponentManager::GetSignature() {
Signature res = GetSignature<Components...>();
std::lock_guard<std::mutex> lock(m_ComponentMutex.get(INDEX(T)));
res.Set(m_ComponentId.at(INDEX(T)), true);
return res;
}

template <typename ComponentType> void ComponentManager::RegisterComponent() {
if (m_ComponentId.find(INDEX(ComponentType)) == m_ComponentId.end()) {
std::lock_guard<std::mutex> lock(m_ComponentMutex.get(INDEX(ComponentType)));
if (!m_ComponentId.contains(INDEX(ComponentType))) {
auto default_handler = [](Entity e) {};
m_ComponentId[INDEX(ComponentType)] = m_ComponentId.size();
m_ComponentArrays[INDEX(ComponentType)] = std::make_shared<ComponentArray<ComponentType>>(ComponentArray<ComponentType>());
Expand Down
14 changes: 8 additions & 6 deletions DummyEngine/Core/ECS/ECS.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "DummyEngine/Core/Threading/MutexMap.hpp"
#include "DummyEngine/Utils/Base.h"

#define INDEX(type) std::type_index(typeid(type))
Expand Down Expand Up @@ -65,7 +66,7 @@ namespace DE {
template <typename ComponentType> ComponentType* AddComponent(U32 entity_id, const ComponentType& component);
template <typename ComponentType> ComponentType* GetComponent(U32 entity_id);
template <typename ComponentType> void RemoveComponent(U32 entity_id);
template <typename ComponentType> bool HasComponent(U32 entity_id) const;
template <typename ComponentType> bool HasComponent(U32 entity_id);
void Destroy(U32 entity_id);

template <typename ComponentType> void SetAddHandler(std::function<void(Entity)> func);
Expand All @@ -75,15 +76,16 @@ namespace DE {
bool Matches(U32 id, const Signature& signature) const;

private:
template <typename... Components> typename std::enable_if<sizeof...(Components) == 0, bool>::type ValidateComponents() const;
template <typename T, typename... Components> bool ValidateComponents() const;
template <typename... Components> typename std::enable_if<sizeof...(Components) == 0, bool>::type ValidateComponents();
template <typename T, typename... Components> bool ValidateComponents();

template <typename... Components> typename std::enable_if<sizeof...(Components) == 0, Signature>::type GetSignature() const;
template <typename T, typename... Components> Signature GetSignature() const;
template <typename... Components> typename std::enable_if<sizeof...(Components) == 0, Signature>::type GetSignature();
template <typename T, typename... Components> Signature GetSignature();

void ValidateSignature(U32 entity_id);
template <typename ComponentType> void RegisterComponent();

MutexMap<std::type_index> m_ComponentMutex;
std::unordered_map<std::type_index, std::shared_ptr<IComponentArray>> m_ComponentArrays;
std::unordered_map<std::type_index, U32> m_ComponentId;
std::unordered_map<std::type_index, std::function<void(Entity)>> m_AddHandlers;
Expand Down Expand Up @@ -212,7 +214,7 @@ namespace DE {

template <typename ComponentType> ComponentType* AddComponent(U32 id, U32 gen, const ComponentType& component);
template <typename ComponentType> ComponentType* GetComponent(U32 id, U32 gen);
template <typename ComponentType> bool HasComponent(U32 id, U32 gen) const;
template <typename ComponentType> bool HasComponent(U32 id, U32 gen);
template <typename ComponentType> void RemoveComponent(U32 id, U32 gen);

EntityManager m_EntityManager;
Expand Down
2 changes: 1 addition & 1 deletion DummyEngine/Core/ECS/Storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ namespace DE {
template <typename ComponentType> ComponentType* Storage::GetComponent(U32 id, U32 gen) {
return (m_EntityManager.Valid(id, gen) ? m_ComponentManager.GetComponent<ComponentType>(id) : nullptr);
}
template <typename ComponentType> bool Storage::HasComponent(U32 id, U32 gen) const {
template <typename ComponentType> bool Storage::HasComponent(U32 id, U32 gen) {
return (m_EntityManager.Valid(id, gen) ? m_ComponentManager.HasComponent<ComponentType>(id) : false);
}
template <typename ComponentType> void Storage::RemoveComponent(U32 id, U32 gen) {
Expand Down
17 changes: 14 additions & 3 deletions DummyEngine/Core/Rendering/Renderer/RenderStructs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ namespace DE {
}

void RenderSubMesh::FillData(const RenderSubMeshData& data) {
FillData(data.vertices, data.indices, CreateRef<Material>()); // TODO: fix, use normal material class
}


void RenderSubMesh::FillData(const std::vector<Vertex3D>& vertices, const std::vector<U32>& indices, const Ref<Material>& mat) {
BufferLayout layout({BufferElementType::Float3,
BufferElementType::Float3,
BufferElementType::Float3,
Expand All @@ -87,19 +92,25 @@ namespace DE {

vertex_array = VertexArray::Create();

Ref<VertexBuffer> vertex_buffer = VertexBuffer::Create(layout, data.vertices.size(), &data.vertices[0]);
Ref<IndexBuffer> index_buffer = IndexBuffer::Create(&data.indices[0], data.indices.size());
material.FillData(data.material);
Ref<VertexBuffer> vertex_buffer = VertexBuffer::Create(layout, vertices.size(), &vertices[0]);
Ref<IndexBuffer> index_buffer = IndexBuffer::Create(&indices[0], indices.size());
material.FillData(material); // TODO: fix, use normal material class

vertex_array->AddVertexBuffer(vertex_buffer);
vertex_array->SetIndexBuffer(index_buffer);
}


RenderSubMesh RenderSubMesh::Copy() const {
RenderSubMesh res;
res.material = material;
res.vertex_array = vertex_array->Copy();
return res;
}
void RenderSubMesh::Remove() {
vertex_array->
}

RenderMesh::RenderMesh(Ref<RenderMeshData> data) {
FillData(data);
}
Expand Down
2 changes: 2 additions & 0 deletions DummyEngine/Core/Rendering/Renderer/RenderStructs.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ namespace DE {

RenderSubMesh Copy() const;
void FillData(const RenderSubMeshData& data);
void FillData(const std::vector<Vertex3D>& vertices, const std::vector<U32>& indices, const Ref<Material>& mat);
void Remove();
};

class RenderMesh {
Expand Down
2 changes: 1 addition & 1 deletion DummyEngine/Core/Rendering/Renderer/Shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "DummyEngine/Core/Rendering/RendererOpenGL/GLShader.h"

namespace DE {
Ref<Shader> Shader::Create(const std::vector<ShaderPart>& initializers) {
Ref<Shader> Shader::Create(const std::vector<Ref<ShaderPart>>& initializers) {
switch (Renderer::CurrentAPI()) {
case API::OpenGL: return CreateRef<GLShader>(initializers);
case API::Vulkan: {
Expand Down
6 changes: 4 additions & 2 deletions DummyEngine/Core/Rendering/Renderer/Shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "DummyEngine/Core/ResourceManaging/RawData.h"
#include "DummyEngine/Utils/Base.h"
#include "ShaderPart.h"

namespace DE {

Expand All @@ -11,6 +12,7 @@ namespace DE {

virtual void Bind() const = 0;
virtual void UnBind() const = 0;
virtual void Unload() const = 0;

virtual void SetFloat(const std::string& uniform_name, float value) const = 0;
virtual void SetFloat2(const std::string& uniform_name, float x, float y) const = 0;
Expand All @@ -24,8 +26,8 @@ namespace DE {
virtual void SetInt3(const std::string& uniform_name, int x, int y, int z) const = 0;
virtual void SetInt4(const std::string& uniform_name, int x, int y, int z, int w) const = 0;
virtual void SetMat4(const std::string& uniform_name, Mat4 value) const = 0;
virtual void SetUnifromBlock(const std::string& uniform_name, U32 id) const = 0;
virtual void SetUniformBlock(const std::string& uniform_name, U32 id) const = 0;

static Ref<Shader> Create(const std::vector<ShaderPart>& initializers);
static Ref<Shader> Create(const std::vector<Ref<ShaderPart>>& initializers);
};
} // namespace DE
26 changes: 26 additions & 0 deletions DummyEngine/Core/Rendering/Renderer/ShaderPart.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// Created by balanda on 10/17/2023.
//

#include "ShaderPart.h"

#include "Renderer.h"

namespace DE {
Ref<ShaderPart> ShaderPart::Create(const ShaderPartResource& data) {
switch (Renderer::CurrentAPI()) {
case API::OpenGL: return CreateRef<GLShaderPart>(initializers);
case API::Vulkan: {
DE_ASSERT(false, "Attempt to create Shader on VulkanAPI which is currently unsupported.");
return nullptr;
break;
}
case API::None: {
DE_ASSERT(false, "Attempt to create Shader without RenderingAPI. Current API: None.");
return nullptr;
break;
}
}
return nullptr;
}
} // namespace DE
15 changes: 15 additions & 0 deletions DummyEngine/Core/Rendering/Renderer/ShaderPart.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include "DummyEngine/Core/ResourceManaging/Resources/ShaderPart.hpp"

namespace DE {

class ShaderPart {
public:
virtual ~ShaderPart() = default;

static Ref<ShaderPart> Create(const ShaderPartResource&data);

};

}
2 changes: 1 addition & 1 deletion DummyEngine/Core/Rendering/Renderer/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ namespace DE {
}
return nullptr;
}
Ref<Texture> Texture::Create(const TextureData& texture_data) {
Ref<Texture> Texture::Create(const TextureResource& texture_data) {
switch (Renderer::CurrentAPI()) {
case API::OpenGL: return CreateRef<GLTexture>(texture_data);
case API::Vulkan: {
Expand Down
4 changes: 3 additions & 1 deletion DummyEngine/Core/Rendering/Renderer/Texture.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "DummyEngine/Core/ResourceManaging/RawData.h"
#include "DummyEngine/Core/ResourceManaging/Resources/Texture.hpp"
#include "DummyEngine/Utils/Base.h"

namespace DE {
Expand Down Expand Up @@ -28,9 +29,10 @@ namespace DE {
virtual void SetChannels(Channels channels) = 0;
virtual void Resize(U32 width, U32 height) = 0;
virtual void Bind(U32 slot) const = 0;
virtual void Remove() = 0;

static Ref<Texture> Create(Channels channels = Channels::RGBA, Format format = Format::U8);
static Ref<Texture> Create(U32 width, U32 height, Channels channels = Channels::RGBA, Format format = Format::U8);
static Ref<Texture> Create(const TextureData& texture_data);
static Ref<Texture> Create(const TextureResource& texture_data);
};
} // namespace DE
1 change: 1 addition & 0 deletions DummyEngine/Core/Rendering/Renderer/VertexArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace DE {
class VertexArray {
public:
virtual ~VertexArray() = default;
virtual void Remove() = 0;

virtual void Bind() const = 0;
virtual void UnBind() const = 0;
Expand Down
Loading