Skip to content

Commit

Permalink
resource WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
mtuncbilek95 committed Nov 26, 2024
1 parent c806675 commit 6d7e6f0
Show file tree
Hide file tree
Showing 17 changed files with 369 additions and 216 deletions.
13 changes: 3 additions & 10 deletions Example/03-ResourceManagement/main.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
#include <Engine/Configuration/ConfigCreator.h>
#include <Engine/RenderContext/RenderContext.h>

#include <Engine/Resource/Helpers/ResourceHelper.h>
#include <Engine/Resource/Factory/ResourceFactory.h>

using namespace MAGE;

int main(int argC, char** argV)
{
Config::InitializeConfig(argC, argV);

RenderContext::Get().GenerateContext();

while (!Context::GetMainWindow()->IsClosed())
{
RenderContext::Get().PrepareFrame();
RenderContext::Get().SubmitFrame();
}

Context::GetMainDevice()->WaitForIdle();
Guid guid = Guid::GenerateID();
Resource* test = ResourceFactory::Get().GetResourceObject(guid);
}
4 changes: 4 additions & 0 deletions Source/Engine/Data/DayTime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ namespace MAGE
{
const array<string, 12> monthsInString = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };

DayTime::DayTime(const string& val)
{
}

string DayTime::GetMonthName() const
{
return monthsInString[m_month - 1];
Expand Down
1 change: 1 addition & 0 deletions Source/Engine/Data/DayTime.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace MAGE
{
public:
DayTime() = default;
DayTime(const string& val);
~DayTime() = default;

DayTime operator=(const DayTime& other)
Expand Down
19 changes: 1 addition & 18 deletions Source/Engine/Resource/Core/Resource.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,8 @@
#include "Resource.h"

#include "Engine/Resource/Helpers/ResourceHelper.h"
#include <nlohmann/json.hpp>
#include <magic_enum.hpp>

namespace MAGE
{
Resource::Resource(const ResourceProps& desc) : m_resProps(desc)
{
}

void Serialize(const path& absPath)
Resource::Resource(const json& desc) : m_props(desc)
{
/*nlohmann::json json = {};
json["ResourceProperties"]["Guid"] = m_resProps.guid.ToString().c_str();
json["ResourceProperties"]["SizeInBytes"] = m_resProps.sizeInBytes;
json["ResourceProperties"]["MetaPath"] = m_resProps.relativePath;
json["ResourceProperties"]["ResourceName"] = m_resProps.resourceName;
json["ResourceProperties"]["ResourceType"] = m_resProps.type;
json["ResourceProperties"]["LastModified"] = m_resProps.lastModified.ToString().c_str();*/

// Write data from m_props.data into the binary file
}
}
43 changes: 15 additions & 28 deletions Source/Engine/Resource/Core/Resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,31 @@
#pragma once

#include "Engine/Core/Core.h"
#include "Engine/Data/Guid.h"
#include "Engine/Data/DayTime.h"
#include "Engine/Objects/IObject.h"
#include "Engine/Memory/OwnedBuffer.h"

#include <nlohmann/json.hpp>
using json = nlohmann::json;

namespace MAGE
{
enum class ResourceType
{
Unknown,
Texture,
Mesh,
Shader,
Font,
Audio,
Script
};

struct ResourceProps
{
Guid guid;
string resourceName;
string relativePath;
usize sizeInBytes;
ResourceType type;
DayTime lastModified;
OwnedBuffer data;
};

class Resource : public IObject
{
public:
Resource(const ResourceProps& desc);
virtual ~Resource();
Resource(const json& desc);
virtual ~Resource() = default;

string GetResourceName() const { return m_props["Properties"]["Name"].get<string>(); }
string GetGuid() const { return m_props["Properties"]["ID"].get<string>(); }
string GetLastModified() const { return m_props["Properties"]["LastModified"].get<string>(); }
string GetPath() const { return m_props["Properties"]["Path"].get<string>(); }
string GetType() const { return m_props["Properties"]["Type"].get<string>(); }

virtual void Serialize() = 0;
virtual void Deserialize() = 0;

virtual void Destroy() override = 0;

protected:
ResourceProps m_resProps;
json m_props;
};
}
Empty file.
52 changes: 52 additions & 0 deletions Source/Engine/Resource/Factory/ResourceFactory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright (c) 2024 Metehan Tuncbilek
*/

#pragma once

#include "Engine/Core/Core.h"
#include "Engine/Objects/Singleton.h"
#include "Engine/Data/Guid.h"
#include "Engine/Resource/Core/Resource.h"

namespace MAGE
{
class ResourceFactory final : public Singleton<ResourceFactory>
{
friend class ResourceHandler;

public:
ResourceFactory() = default;
~ResourceFactory() = default;

template<typename T = Resource, typename Key = Guid, typename = std::enable_if<std::is_base_of_v<Resource, T> && (std::is_same_v<Guid, Key> || std::is_same_v<string, Key>)>>
auto GetResourceObject(const Key& key) -> typename std::conditional<std::is_void<T>::value, Resource*, T*>::type
{
if constexpr (std::is_same_v<string, Key>)
{
auto it = m_auxillary.find(key);
if (it != m_auxillary.end())
return dynamic_cast<T*>(&*m_resources[it->second]);
}
else
return dynamic_cast<T*>(&*m_resources[key]);

return nullptr;
}

protected:
void RegisterResource(const Shared<Resource>& obj)
{
m_resources[obj->GetGuid()] = obj;
m_auxillary.emplace(obj->GetPath() + obj->GetResourceName() + "." + obj->GetType(), obj->GetGuid());
}

private:
hashmap<Guid, Shared<Resource>, GuidHash> m_resources;
map<string, Guid> m_auxillary;
};
}
80 changes: 0 additions & 80 deletions Source/Engine/Resource/Helpers/ResourceHelper.cpp

This file was deleted.

26 changes: 0 additions & 26 deletions Source/Engine/Resource/Helpers/ResourceHelper.h

This file was deleted.

24 changes: 24 additions & 0 deletions Source/Engine/Resource/Mesh/Mesh.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "Mesh.h"

namespace MAGE
{
Mesh::Mesh(const json& desc) : Resource(desc)
{
}

Mesh::~Mesh()
{
}

void Mesh::Serialize()
{
}

void Mesh::Deserialize()
{
}

void Mesh::Destroy()
{
}
}
31 changes: 31 additions & 0 deletions Source/Engine/Resource/Mesh/Mesh.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright (c) 2024 Metehan Tuncbilek
*/

#pragma once

#include "Engine/Core/Core.h"
#include "Engine/Resource/Core/Resource.h"

#include "Engine/Vulkan/Buffer/VBuffer.h"

namespace MAGE
{
class Mesh : public Resource
{
public:
Mesh(const json& desc);
virtual ~Mesh() override;

void Serialize() override final;
void Deserialize() override final;
void Destroy() override final;

protected:
Owned<VBuffer> m_geoBuffer;
};
}
Loading

0 comments on commit 6d7e6f0

Please sign in to comment.