Asset explorer showing files

After loading a project the asset explorer now show all the project files.
This commit is contained in:
Nigel Barink 2022-11-11 13:10:05 +01:00
parent b5db500d48
commit 628225af45
5 changed files with 200 additions and 137 deletions

View File

@ -1,9 +1,7 @@
#pragma once #pragma once
#include <filesystem>
#include <string> #include <string>
#include "../../YoggieEngine/src/Graphics/Primitives/Mesh.h" #include "../../YoggieEngine/src/YoggieEngine.h"
#include "../../YoggieEngine/src/Graphics/Primitives/Texture.h"
#include "../../../YoggieEngine/src/Scene/UUID.h"
typedef uint64_t ASSET_UUID ;
enum class ASSET_TYPE { enum class ASSET_TYPE {
Unknown = -1, Unknown = -1,
@ -14,11 +12,12 @@ enum class ASSET_TYPE {
class Asset { class Asset {
public: public:
Asset(const char* name): ID(UUID::Generate()), name(std::string(name)) {} Asset(const char* name): name(name) {}
virtual ASSET_TYPE GetType() = 0; virtual ASSET_TYPE GetType() { return ASSET_TYPE::Unknown; }
const char* GetName() const { return name.c_str(); }
bool isFolder = false ;
protected: protected:
std::string& name; std::string name;
ASSET_UUID ID;
}; };

View File

@ -0,0 +1,149 @@
#include "AssetManager.h"
#include "../../YoggieEngine/src/AssetManager/ModelImporter.h"
#include <iostream>
#include <fstream>
#include <spdlog/spdlog.h>
std::vector<Asset> AssetManager::assets;
std::filesystem::path AssetManager::currentPath;
void AssetManager::Init()
{
assets = std::vector<Asset>();
currentPath = std::filesystem::path(".");
}
void AssetManager::BuildAssetView()
{
if (currentPath.empty()) {
return;
}
for (auto& dir_entry : std::filesystem::directory_iterator(currentPath))
{
auto asset = Asset(dir_entry.path().filename().string().c_str());
if (dir_entry.is_directory()) {
asset.isFolder = true;
}
assets.push_back(asset);
}
}
void AssetManager::setAssetPath(std::filesystem::path path)
{
currentPath = path;
}
YoggieEngine::Mesh* AssetManager::LoadFromAssetFile(const std::filesystem::path assetPath)
{
YoggieEngine::Mesh imported;
std::ifstream AssetFile;
AssetFile.open(assetPath, std::ios::binary);
if (AssetFile.is_open()) {
char* Header = (char*)malloc(8);
unsigned long long Vsize = 0;
uint32_t Vnum = 0;
uint32_t Enum = 0;
// Read header
AssetFile.read(Header, 8);
AssetFile.read((char*)&Vsize, sizeof(unsigned long long));
AssetFile.read((char*)&Vnum, sizeof(uint32_t));
AssetFile.read((char*)&Enum, sizeof(uint32_t));
// print Header info
std::cout << "File has header: " << Header << std::endl;
std::cout << "Vertex size: " << Vsize << std::endl;
std::cout << "Number of Vertices: " << Vnum << std::endl;
std::cout << "Number of Elements: " << Enum << std::endl;
free(Header);
// Load Vertices (Vertex + UV )
imported.vertices = std::vector < YoggieEngine::Vertex>();
for (int i = 0; i < Vnum; i++)
{
YoggieEngine::Vertex data = YoggieEngine::Vertex();
AssetFile.read((char*)&data, Vsize);
imported.vertices.push_back(data);
}
// skip x bytes
AssetFile.ignore(sizeof(char) * 3);
// Load Elements
imported.elements = std::vector<unsigned int>();
for (int i = 0; i < Enum; i++) {
unsigned int data = 0;
AssetFile.read((char*)&data, sizeof(unsigned int));
imported.elements.push_back(data);
}
}
else {
std::cout << "Failed ot open mesh " << std::endl;
}
return nullptr;
}
YoggieEngine::Renderable* AssetManager::LoadFromSource(const std::filesystem::path srcPath, const std::filesystem::path assetFolder)
{
auto model = (YoggieEngine::ModelImporter()).Import(srcPath.string());
YoggieEngine::Mesh* exportMesh = model->renderable->mesh;
std::filesystem::path MeshFileName = assetFolder / srcPath.filename().replace_extension(".mesh");
std::cout << "Save path: " << MeshFileName << std::endl;
std::ofstream meshAsset;
meshAsset.open(MeshFileName.c_str(), std::ios::binary);
if (meshAsset.is_open()) {
// write a header
static const char* HEADER = "MESH";
meshAsset.write(HEADER, sizeof(HEADER));
auto Vsize = sizeof(YoggieEngine::Vertex);
std::cout << "size of vertex: " << Vsize << std::endl;
std::cout << "Addr of vSize: " << &Vsize << std::endl;
auto Vnum = exportMesh->vertices.size();
auto Enum = exportMesh->elements.size();
meshAsset.write((char*)&Vsize, sizeof(unsigned long long));
meshAsset.write((char*)&Vnum, sizeof(uint32_t));
meshAsset.write((char*)&Enum, sizeof(uint32_t));
// write all vertices
for (auto vertice : exportMesh->vertices)
{
meshAsset.write((char*)&vertice, sizeof(vertice));
}
// write 3 x 0 byte
meshAsset.write((const char*)"\0\0\0", sizeof(char) * 3);
// write all indices
for (auto index : exportMesh->elements) {
meshAsset.write((char*)&index, sizeof(index));
}
meshAsset.close();
}
else {
spdlog::error("Failed to create/open mesh file.");
}
return model->renderable;
}

View File

@ -1,118 +1,20 @@
#pragma once #pragma once
#include <filesystem> #include <vector>
#include <fstream> #include "Asset.h"
#include "../../../YoggieEngine/src/Graphics/Renderable.h"
#include "../../../YoggieEngine/src/AssetManager/ModelImporter.h"
#include <spdlog/spdlog.h>
#include <iostream>
class AssetManager { class AssetManager {
public: public:
static YoggieEngine::Mesh* LoadFromAssetFile(const std::filesystem::path assetPath ) static void Init();
{ static void BuildAssetView();
YoggieEngine::Mesh imported; static void setAssetPath(std::filesystem::path path);
std::ifstream AssetFile; static YoggieEngine::Mesh* LoadFromAssetFile(const std::filesystem::path assetPath);
AssetFile.open(assetPath, std::ios::binary); static YoggieEngine::Renderable* LoadFromSource(const std::filesystem::path srcPath, const std::filesystem::path assetFolder);
if (AssetFile.is_open()) {
char* Header = (char*) malloc(8); static std::vector<Asset> assets ;
unsigned long long Vsize = 0;
uint32_t Vnum = 0;
uint32_t Enum = 0;
// Read header private:
AssetFile.read(Header, 8); static std::filesystem::path currentPath;
AssetFile.read((char*)&Vsize, sizeof(unsigned long long ));
AssetFile.read((char*)&Vnum, sizeof(uint32_t));
AssetFile.read((char*)&Enum, sizeof(uint32_t));
// print Header info
std::cout << "File has header: " << Header << std::endl;
std::cout << "Vertex size: " << Vsize << std::endl;
std::cout << "Number of Vertices: " << Vnum << std::endl;
std::cout << "Number of Elements: " << Enum << std::endl;
free(Header);
// Load Vertices (Vertex + UV )
imported.vertices = std::vector < YoggieEngine::Vertex>();
for (int i = 0; i < Vnum; i++)
{
YoggieEngine::Vertex data = YoggieEngine::Vertex();
AssetFile.read((char*) & data, Vsize);
imported.vertices.push_back(data);
}
// skip x bytes
AssetFile.ignore(sizeof(char) * 3);
// Load Elements
imported.elements = std::vector<unsigned int>();
for (int i = 0; i < Enum; i++) {
unsigned int data = 0;
AssetFile.read((char*)&data, sizeof(unsigned int));
imported.elements.push_back(data);
}
}
else {
std::cout << "Failed ot open mesh " << std::endl;
}
return nullptr;
}
static YoggieEngine::Renderable* LoadFromSource(const std::filesystem::path srcPath , const std::filesystem::path assetFolder ) {
auto model = (YoggieEngine::ModelImporter()).Import(srcPath.string());
YoggieEngine::Mesh* exportMesh = model->renderable->mesh;
std::filesystem::path MeshFileName = assetFolder / srcPath.filename().replace_extension( ".mesh");
std::cout << "Save path: " << MeshFileName << std::endl;
std::ofstream meshAsset;
meshAsset.open(MeshFileName, std::ios::binary );
if (meshAsset.is_open()) {
// write a header
static const char* HEADER = "MESH";
meshAsset.write(HEADER, sizeof(HEADER));
auto Vsize = sizeof(YoggieEngine::Vertex);
std::cout << "size of vertex: " << Vsize << std::endl;
std::cout << "Addr of vSize: " << &Vsize << std::endl;
auto Vnum = exportMesh->vertices.size();
auto Enum = exportMesh->elements.size();
meshAsset.write((char*) & Vsize, sizeof(unsigned long long ) );
meshAsset.write((char*)&Vnum, sizeof(uint32_t));
meshAsset.write((char*)&Enum, sizeof(uint32_t));
// write all vertices
for (auto vertice : exportMesh->vertices)
{
meshAsset.write((char*)&vertice, sizeof(vertice));
}
// write 3 x 0 byte
meshAsset.write((const char*)"\0\0\0", sizeof(char) * 3);
// write all indices
for (auto index : exportMesh->elements) {
meshAsset.write((char*)&index, sizeof(index));
}
meshAsset.close();
}
else {
spdlog::error("Failed to create/open mesh file.");
}
return model->renderable;
}
}; };

View File

@ -3,7 +3,7 @@
#include <iostream> #include <iostream>
#include "../../YoggieEngine/src/Scene/Components.h" #include "../../YoggieEngine/src/Scene/Components.h"
#include "../../YoggieEngine/src/Scene/Entity.h" #include "../../YoggieEngine/src/Scene/Entity.h"
#include "../AssetManagement/AssetManager.h"
class Editor; class Editor;
void ComponentView(const std::string& componentName, voidFunction func) void ComponentView(const std::string& componentName, voidFunction func)
@ -184,27 +184,37 @@ void AssetsFinder() {
ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0.f, 0.f, 0.f, 0.f)); ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0.f, 0.f, 0.f, 0.f));
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(1.f, 1.f, 1.f, 0.2f)); ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(1.f, 1.f, 1.f, 0.2f));
for (int row = 0; row < 4; row++) {
ImGui::TableNextRow();
for (int column = 0; column < 3; column++) {
ImGui::TableSetColumnIndex(column);
if (column % 2) { int row = 0;
ImGui::ImageButton( int column = 0 ;
(ImTextureID)folderIcon.GetID(), for (auto& asset : AssetManager::assets) {
ImVec2{ (float)iconSize,(float)iconSize }); if (column % 3 == 0) {
ImGui::Text("Folder %d", row); ImGui::TableNextRow();
} column = 0;
else { row++;
ImGui::ImageButton( }
(ImTextureID)AssetIcon.GetID(),
ImVec2{ (float)iconSize, (float)iconSize });
ImGui::Text("Asset %d", row);
}
ImGui::TableSetColumnIndex(column);
if (asset.isFolder) {
ImGui::ImageButton(
(ImTextureID)folderIcon.GetID(),
ImVec2{ (float)iconSize,(float)iconSize });
ImGui::Text(asset.GetName(), row);
} }
} else {
ImGui::ImageButton(
(ImTextureID)AssetIcon.GetID(),
ImVec2{ (float)iconSize, (float)iconSize });
ImGui::Text(asset.GetName(), row);
}
column++;
}
ImGui::PopStyleColor(3); ImGui::PopStyleColor(3);
ImGui::EndTable(); ImGui::EndTable();

View File

@ -36,6 +36,7 @@ public:
double previous = glfwGetTime(); double previous = glfwGetTime();
double lag = 0.0; double lag = 0.0;
AssetManager::Init();
renderer->Prepare(activeRuntime.MainScene); renderer->Prepare(activeRuntime.MainScene);
while (!mainWindow.WindowShouldClose()) while (!mainWindow.WindowShouldClose())
@ -139,6 +140,8 @@ public:
switch (result) { switch (result) {
case(NFD_OKAY): case(NFD_OKAY):
Project::LoadProject(path, activeRuntime.CurrentProject); Project::LoadProject(path, activeRuntime.CurrentProject);
AssetManager::setAssetPath(activeRuntime.CurrentProject.get()->GetProjectDirectory());
AssetManager::BuildAssetView();
break; break;
case(NFD_CANCEL): case(NFD_CANCEL):
break; break;
@ -225,7 +228,7 @@ public:
switch (result) { switch (result) {
case(NFD_OKAY): case(NFD_OKAY):
// Import Model // Import Model
AssetManager::LoadFromSource(modelImportPath, "."); AssetManager::LoadFromSource(modelImportPath, activeRuntime.CurrentProject.get()->GetProjectDirectory() / "Assets");
break; break;
case(NFD_CANCEL): case(NFD_CANCEL):
break; break;