Skip to content

Commit

Permalink
WIP working on AssetManager / asset loading/creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Y0L042 committed Jun 13, 2024
1 parent f388cb4 commit b34b284
Show file tree
Hide file tree
Showing 36 changed files with 870 additions and 316 deletions.
23 changes: 19 additions & 4 deletions Duin/Duin.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,14 @@ IF EXIST ..\bin\Dist-windows-x86_64\Duin\Duin.dll\ (xcopy /Q /E /Y /I ..\bin\Dis
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="src\Duin.h" />
<ClInclude Include="src\Duin\Assets\AnimatedSprite2D.h" />
<ClInclude Include="src\Duin\Assets\Asset.h" />
<ClInclude Include="src\Duin\Assets\AssetManager.h" />
<ClInclude Include="src\Duin\Assets\AssetStructs.h" />
<ClInclude Include="src\Duin\Assets\GUID.h" />
<ClInclude Include="src\Duin\Assets\Image.h" />
<ClInclude Include="src\Duin\Assets\Sprite2D.h" />
<ClInclude Include="src\Duin\Assets\Texture.h" />
<ClInclude Include="src\Duin\Core\Application.h" />
<ClInclude Include="src\Duin\Core\Core.h" />
<ClInclude Include="src\Duin\Core\Debug\DebugDraw.h" />
Expand All @@ -180,6 +187,8 @@ IF EXIST ..\bin\Dist-windows-x86_64\Duin\Duin.dll\ (xcopy /Q /E /Y /I ..\bin\Dis
<ClInclude Include="src\Duin\Core\Maths\src\Transform2D.h" />
<ClInclude Include="src\Duin\Core\Maths\src\Vector2.h" />
<ClInclude Include="src\Duin\Core\Maths\src\Vector3.h" />
<ClInclude Include="src\Duin\Core\Scene\Object.h" />
<ClInclude Include="src\Duin\Core\Scene\SharedRef.h" />
<ClInclude Include="src\Duin\Core\Scene\SceneManager.h" />
<ClInclude Include="src\Duin\Core\Signal.h" />
<ClInclude Include="src\Duin\Core\Structures\Delaunay.h" />
Expand All @@ -193,19 +202,27 @@ IF EXIST ..\bin\Dist-windows-x86_64\Duin\Duin.dll\ (xcopy /Q /E /Y /I ..\bin\Dis
<ClInclude Include="src\Duin\Events\InputEvent.h" />
<ClInclude Include="src\Duin\Events\InputMap.h" />
<ClInclude Include="src\Duin\Assets\Particle2D.h" />
<ClInclude Include="src\Duin\Assets\TextureResource.h" />
<ClInclude Include="src\Duin\Object\Blackboard.h" />
<ClInclude Include="src\Duin\Object\Node.h" />
<ClInclude Include="src\Duin\Object\Object.h" />
<ClInclude Include="src\dnpch.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\Duin\Assets\AnimatedSprite2D.cpp" />
<ClCompile Include="src\Duin\Assets\Asset.cpp" />
<ClCompile Include="src\Duin\Assets\AssetManager.cpp" />
<ClCompile Include="src\Duin\Assets\AssetStructs.cpp" />
<ClCompile Include="src\Duin\Assets\GUID.cpp" />
<ClCompile Include="src\Duin\Assets\Image.cpp" />
<ClCompile Include="src\Duin\Assets\Particle2D.cpp" />
<ClCompile Include="src\Duin\Assets\Sprite2D.cpp" />
<ClCompile Include="src\Duin\Assets\Texture.cpp" />
<ClCompile Include="src\Duin\Core\Application.cpp" />
<ClCompile Include="src\Duin\Core\Debug\DebugDraw.cpp" />
<ClCompile Include="src\Duin\Core\Debug\Log.cpp" />
<ClCompile Include="src\Duin\Core\Debug\Profiler.cpp" />
<ClCompile Include="src\Duin\Core\Maths\DuinMaths.cpp" />
<ClCompile Include="src\Duin\Core\Scene\Object.cpp" />
<ClCompile Include="src\Duin\Core\Scene\SharedRef.cpp" />
<ClCompile Include="src\Duin\Core\Scene\SceneManager.cpp" />
<ClCompile Include="src\Duin\Core\Structures\Delaunay.cpp" />
<ClCompile Include="src\Duin\Core\Structures\QuadTree.cpp" />
Expand All @@ -214,10 +231,8 @@ IF EXIST ..\bin\Dist-windows-x86_64\Duin\Duin.dll\ (xcopy /Q /E /Y /I ..\bin\Dis
<ClCompile Include="src\Duin\Entity\Entity.cpp" />
<ClCompile Include="src\Duin\Entity\Registry.cpp" />
<ClCompile Include="src\Duin\Events\InputEvent.cpp" />
<ClCompile Include="src\Duin\Assets\TextureResource.cpp" />
<ClCompile Include="src\Duin\Object\Blackboard.cpp" />
<ClCompile Include="src\Duin\Object\Node.cpp" />
<ClCompile Include="src\Duin\Object\Object.cpp" />
<ClCompile Include="src\dnpch.cpp">
<PrecompiledHeader>Create</PrecompiledHeader>
</ClCompile>
Expand Down
31 changes: 19 additions & 12 deletions Duin/Duin.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -136,21 +136,24 @@
<ClInclude Include="src\Duin\Events\InputMap.h">
<Filter>Duin\Events</Filter>
</ClInclude>
<ClInclude Include="src\Duin\Assets\TextureResource.h">
<Filter>Duin\Graphics</Filter>
</ClInclude>
<ClInclude Include="src\Duin\Object\Blackboard.h">
<Filter>Duin\Object</Filter>
</ClInclude>
<ClInclude Include="src\Duin\Object\Node.h">
<Filter>Duin\Object\Node</Filter>
</ClInclude>
<ClInclude Include="src\Duin\Object\Object.h">
<Filter>Duin\Object</Filter>
</ClInclude>
<ClInclude Include="src\dnpch.h" />
<ClInclude Include="src\Duin\Assets\Particle2D.h" />
<ClInclude Include="src\Duin\Assets\AssetManager.h" />
<ClInclude Include="src\Duin\Assets\AssetStructs.h" />
<ClInclude Include="src\Duin\Assets\GUID.h" />
<ClInclude Include="src\Duin\Assets\Image.h" />
<ClInclude Include="src\Duin\Core\Scene\Object.h" />
<ClInclude Include="src\Duin\Core\Scene\SharedRef.h" />
<ClInclude Include="src\Duin\Assets\AnimatedSprite2D.h" />
<ClInclude Include="src\Duin\Assets\Sprite2D.h" />
<ClInclude Include="src\Duin\Assets\Texture.h" />
<ClInclude Include="src\Duin\Assets\Asset.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\Duin\Core\Application.cpp">
Expand Down Expand Up @@ -192,19 +195,23 @@
<ClCompile Include="src\Duin\Events\InputEvent.cpp">
<Filter>Duin\Events</Filter>
</ClCompile>
<ClCompile Include="src\Duin\Assets\TextureResource.cpp">
<Filter>Duin\Graphics</Filter>
</ClCompile>
<ClCompile Include="src\Duin\Object\Blackboard.cpp">
<Filter>Duin\Object</Filter>
</ClCompile>
<ClCompile Include="src\Duin\Object\Node.cpp">
<Filter>Duin\Object\Node</Filter>
</ClCompile>
<ClCompile Include="src\Duin\Object\Object.cpp">
<Filter>Duin\Object</Filter>
</ClCompile>
<ClCompile Include="src\dnpch.cpp" />
<ClCompile Include="src\Duin\Assets\AssetManager.cpp" />
<ClCompile Include="src\Duin\Assets\AssetStructs.cpp" />
<ClCompile Include="src\Duin\Assets\GUID.cpp" />
<ClCompile Include="src\Duin\Assets\Image.cpp" />
<ClCompile Include="src\Duin\Core\Scene\Object.cpp" />
<ClCompile Include="src\Duin\Core\Scene\SharedRef.cpp" />
<ClCompile Include="src\Duin\Assets\AnimatedSprite2D.cpp" />
<ClCompile Include="src\Duin\Assets\Sprite2D.cpp" />
<ClCompile Include="src\Duin\Assets\Particle2D.cpp" />
<ClCompile Include="src\Duin\Assets\Texture.cpp" />
<ClCompile Include="src\Duin\Assets\Asset.cpp" />
</ItemGroup>
</Project>
5 changes: 3 additions & 2 deletions Duin/src/Duin.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@
#include "Duin/Core/Structures/RenderStructs.h"
#include "Duin/Core/Structures/QuadTree.h"
#include "Duin/Core/Structures/Delaunay.h"
#include "Duin/Core/Scene/Object.h"
#include "Duin/Core/Scene/SceneManager.h"

// ---------- Events -----------
#include "Duin/Events/InputEvent.h"
#include "Duin/Events/InputMap.h"

// ---------- Graphics -----------
// ---------- Assets -----------
#include "Duin/Assets/TextureResource.h"
#include "Duin/Assets/AssetManager.h"

// ---------- Entities -----------
#include "Duin/Entity/Entity.h"
Expand All @@ -38,7 +40,6 @@

// ---------- [DEPRECIATED] -----------
// ---------- Objects -----------
#include "Duin/Object/Object.h"
#include "Duin/Object/Node.h"
#include "Duin/Object/Blackboard.h"

Expand Down
8 changes: 8 additions & 0 deletions Duin/src/Duin/Assets/AnimatedSprite2D.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "dnpch.h"

#include "AnimatedSprite2D.h"

namespace Duin
{

}
15 changes: 15 additions & 0 deletions Duin/src/Duin/Assets/AnimatedSprite2D.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include "Duin/Core/Core.h"
#include "Duin/Core/Scene/Object.h"

namespace Duin
{
class DUIN_API AnimatedSprite2D : public Object
{
public:

private:

};
}
30 changes: 30 additions & 0 deletions Duin/src/Duin/Assets/Asset.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "dnpch.h"

#include "Asset.h"
#include "Duin/Assets/AssetManager.h"

namespace Duin
{
Asset::Asset()
{
}

Asset::Asset(AssetManager* assetManager)
: assetManager(assetManager)
{
}

Asset::~Asset()
{
}

void Asset::SetAssetManager(AssetManager* assetManager)
{
this->assetManager = assetManager;
}

AssetManager* Asset::GetAssetManager()
{
return assetManager;
}
}
28 changes: 28 additions & 0 deletions Duin/src/Duin/Assets/Asset.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include "Duin/Core/Core.h"
#include "Duin/Core/UUID.h"

namespace Duin
{
class AssetManager;

/*
* A wrapper for the _Asset class.
* The client will interact with this class, and children.
*/
class DUIN_API Asset
{
public:
Asset();
Asset(AssetManager* assetManager);
~Asset();

void SetAssetManager(AssetManager* assetManager);
AssetManager* GetAssetManager();

private:
UUID uuid;
AssetManager* assetManager = nullptr;
};
}
8 changes: 7 additions & 1 deletion Duin/src/Duin/Assets/AssetManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ namespace Duin
{
}

void AssetManager::LoadTexture(const char* texturePath)
void AssetManager::AddToAssetRegistry(std::shared_ptr<_Asset> asset)
{
assetRegistry[asset->guid] = asset;
}

void AssetManager::RemoveFromAssetRegistry(GUID guid)
{
assetRegistry.erase(guid);
}
}
32 changes: 30 additions & 2 deletions Duin/src/Duin/Assets/AssetManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
#include "Duin/Core/Core.h"
#include "Duin/Core/UUID.h"
#include "Duin/Core/Structures/RenderStructs.h"
#include "Duin/Assets/GUID.h"
#include "Duin/Assets/AssetStructs.h"

#include "RLImGuiComponent.h"

#include <unordered_map>
#include <string>

namespace Duin
{
Expand All @@ -16,9 +19,34 @@ namespace Duin
AssetManager();
~AssetManager();

void LoadTexture(const char* texturePath);
template<typename ASSET, typename FACTORY>
std::shared_ptr<ASSET> LoadAsset(const char* path);

void AddToAssetRegistry(std::shared_ptr<_Asset> asset);
void RemoveFromAssetRegistry(GUID guid);

private:
std::unordered_map<UUID, std::shared_ptr<Texture>> textureMap;
std::unordered_map<GUID, std::shared_ptr<_Asset>> assetRegistry;
};

template<typename ASSET, typename FACTORY>
inline std::shared_ptr<ASSET> AssetManager::LoadAsset(const char* path)
{
static_assert(std::is_base_of<_Asset, ASSET>::value, "ASSET must be a _Asset derived struct!");
static_assert(std::is_base_of<_AssetFactory, FACTORY>::value, "FACTORY must be a _AssetFactory derived class!");

// Check for existing Asset
GUID guid(path);
auto it = assetRegistry.find(guid);
if (it != assetRegistry.end())
{
return std::dynamic_pointer_cast<ASSET>(it->second);
}

// Else load/create Asset
FACTORY* factory = new FACTORY();
std::shared_ptr<_Asset> asset = factory->CreateAsset(this, path);
AddToAssetRegistry(asset);
return std::dynamic_pointer_cast<ASSET>(asset);
}
}
25 changes: 25 additions & 0 deletions Duin/src/Duin/Assets/AssetStructs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "dnpch.h"

#include "AssetStructs.h"
#include "AssetManager.h"

namespace Duin
{
std::shared_ptr<_Asset> _TextureAssetFactory::CreateAsset(AssetManager* assetManager, const char* path)
{
// TODO
::Texture rlTexture = ::LoadTexture(path);
std::shared_ptr<_TextureAsset> textureAsset = std::make_shared<_TextureAsset>(rlTexture);

return textureAsset;
}

std::shared_ptr<_Asset> _ImageAssetFactory::CreateAsset(AssetManager* assetManager, const char* path)
{
// TODO
::Image rlImage = ::LoadImage(path);
std::shared_ptr<_ImageAsset> imageAsset = std::make_shared<_ImageAsset>(rlImage);

return imageAsset;
}
}
Loading

0 comments on commit b34b284

Please sign in to comment.