Skip to content

Commit

Permalink
Added Basic Database, generating ids, and creating meta files
Browse files Browse the repository at this point in the history
  • Loading branch information
tomheeleynz committed Aug 15, 2022
1 parent b92147d commit ac13c70
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -397,4 +397,5 @@ _deps
# External projects
*-prefix/

*.arcmeta
# End of https://www.toptal.com/developers/gitignore/api/cmake
24 changes: 24 additions & 0 deletions Arcane/src/Arcane/Assets/Asset.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

namespace Arcane
{
enum class AssetType
{
MESH
};

class Asset
{
public:
Asset() {}

int GetID() { return m_ID; }
void SetID(int id) { m_ID = id; };

AssetType GetAssetType() { return m_Type; };
void SetAssetType(AssetType type) { m_Type = type; };
private:
int m_ID = -1;
AssetType m_Type;
};
}
92 changes: 92 additions & 0 deletions Arcane/src/Arcane/Assets/AssetDatabase.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include <iostream>
#include <fstream>
#include <nlohmann/json.hpp>
#include <time.h>
#include <cstdlib>

#include "MeshAsset.h"
#include "AssetDatabase.h"


namespace Arcane
{
AssetDatabase::AssetDatabase(std::filesystem::path assetDir)
{
m_AssetDirPath = assetDir;
srand(time(0));
}

Asset* AssetDatabase::GetAsset(int id)
{
return m_Assets[id];
}

bool AssetDatabase::GenerateDatabase()
{
for (auto& dirEntry : std::filesystem::recursive_directory_iterator(m_AssetDirPath)) {
if (dirEntry.is_regular_file()) {
std::filesystem::path file = dirEntry.path();

bool isAssetGenerated = GenerateAsset(file);
if (!isAssetGenerated) {
return false;
}
}
}
return true;
}

bool AssetDatabase::GenerateAsset(std::filesystem::path currentAssetPath)
{
std::string metaFileName = currentAssetPath.stem().string() + ".arcmeta";
std::filesystem::path metaPath = currentAssetPath.parent_path() / metaFileName;

// Either Create asset id or get from the meta file
int assetID = 0;
if (!CheckMetaInfo(metaPath)) {
assetID = rand();
GenerateMetaFile(metaPath, assetID);
}
else {
assetID = GetMetaID(metaPath);
}

// Get File extension
if (currentAssetPath.extension() == ".obj") {
MeshAsset* meshAsset = new MeshAsset(currentAssetPath);
meshAsset->SetID(assetID);
m_Assets[assetID] = meshAsset;
}
else if (currentAssetPath.extension() == ".fbx") {
// Generate Mesh Asset
}
return true;
}

bool AssetDatabase::CheckMetaInfo(std::filesystem::path currentMetaPath)
{
return std::filesystem::exists(currentMetaPath);
}

void AssetDatabase::GenerateMetaFile(std::filesystem::path metaPath, int newID)
{
// Generate ID
std::ofstream o(metaPath.string().c_str());

// Create json layout
nlohmann::json meta_file;
meta_file["id"] = newID;

// save under .arcmeta file
o << meta_file << std::endl;
}

int AssetDatabase::GetMetaID(std::filesystem::path currentMetaPath)
{
nlohmann::json metaJson;
std::ifstream i(currentMetaPath.string().c_str());
i >> metaJson;

return metaJson["id"];
}
}
28 changes: 28 additions & 0 deletions Arcane/src/Arcane/Assets/AssetDatabase.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include <unordered_map>
#include <filesystem>

#include "Asset.h"

namespace Arcane
{
class AssetDatabase
{
public:
AssetDatabase(std::filesystem::path assetDir);

Asset* GetAsset(int id);

bool GenerateDatabase();
bool GenerateAsset(std::filesystem::path currentAssetPath);

bool CheckMetaInfo(std::filesystem::path currentMetaPath);
void GenerateMetaFile(std::filesystem::path metaPath, int newID);

int GetMetaID(std::filesystem::path metaPath);
private:
std::unordered_map<int, Asset*> m_Assets;
std::filesystem::path m_AssetDirPath;
};
}
10 changes: 10 additions & 0 deletions Arcane/src/Arcane/Assets/MeshAsset.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "MeshAsset.h"

namespace Arcane
{
MeshAsset::MeshAsset(std::filesystem::path meshPath)
{
m_Mesh = new Mesh(meshPath.string());
SetAssetType(AssetType::MESH);
}
}
18 changes: 18 additions & 0 deletions Arcane/src/Arcane/Assets/MeshAsset.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once
#include <filesystem>
#include "Asset.h"
#include "Arcane/Renderer/Mesh.h"

namespace Arcane
{
class MeshAsset : public Asset
{
public:
MeshAsset(std::filesystem::path meshPath);


Mesh* GetMesh() { return m_Mesh; }
private:
Mesh* m_Mesh;
};
}
8 changes: 8 additions & 0 deletions Arcane/src/Arcane/Core/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ namespace Arcane {
}

m_Clock = new CClock();

// Generate Asset Database on application startup
m_AssetDatabase = new AssetDatabase("./src/Assets");
bool assetDatabaseGenerated = m_AssetDatabase->GenerateDatabase();

if (!assetDatabaseGenerated) {
std::cout << "Asset Database Not Created\n";
}
}

void Application::RenderImGui()
Expand Down
8 changes: 6 additions & 2 deletions Arcane/src/Arcane/Core/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
#include "Clock.h"
#include "Window.h"
#include "Arcane/ImGui/ImGuiLayer.h"
#include "Arcane/Assets/AssetDatabase.h"

namespace Arcane {

namespace Arcane
{
struct ApplicationSpecifications
{
std::string Name;
Expand Down Expand Up @@ -42,9 +43,12 @@ namespace Arcane {
Window* m_Window;
static Application* s_Instance;
std::vector<Layer*> m_LayerStack;

bool m_bIsRunning = true;
ImGuiLayer* m_ImGuiLayer;
CClock* m_Clock;
bool m_ImguiEnabled = false;

AssetDatabase* m_AssetDatabase;
};
}

0 comments on commit ac13c70

Please sign in to comment.