diff --git a/Editor/src/AssetManagement/AssetManager.cpp b/Editor/src/AssetManagement/AssetManager.cpp index 502c494..3aa6879 100644 --- a/Editor/src/AssetManagement/AssetManager.cpp +++ b/Editor/src/AssetManagement/AssetManager.cpp @@ -41,7 +41,7 @@ void AssetManager::setAssetPath(std::filesystem::path path) YoggieEngine::Mesh* AssetManager::LoadFromAssetFile(const std::filesystem::path assetPath) { - YoggieEngine::Mesh imported; + YoggieEngine::Mesh* imported = nullptr; std::ifstream AssetFile; AssetFile.open(assetPath, std::ios::binary); @@ -65,14 +65,16 @@ YoggieEngine::Mesh* AssetManager::LoadFromAssetFile(const std::filesystem::path std::cout << "Number of Elements: " << Enum << std::endl; free(Header); + + imported = new YoggieEngine::Mesh(); // Load Vertices (Vertex + UV ) - imported.vertices = std::vector < YoggieEngine::Vertex>(); + 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); + imported->vertices.push_back(data); } @@ -80,11 +82,11 @@ YoggieEngine::Mesh* AssetManager::LoadFromAssetFile(const std::filesystem::path AssetFile.ignore(sizeof(char) * 3); // Load Elements - imported.elements = std::vector(); + imported->elements = std::vector(); for (int i = 0; i < Enum; i++) { unsigned int data = 0; AssetFile.read((char*)&data, sizeof(unsigned int)); - imported.elements.push_back(data); + imported->elements.push_back(data); } } @@ -92,8 +94,7 @@ YoggieEngine::Mesh* AssetManager::LoadFromAssetFile(const std::filesystem::path std::cout << "Failed ot open mesh " << std::endl; } - - return nullptr; + return imported; } @@ -107,7 +108,7 @@ YoggieEngine::Renderable* AssetManager::LoadFromSource(const std::filesystem::pa std::cout << "Save path: " << MeshFileName << std::endl; std::ofstream meshAsset; - meshAsset.open(MeshFileName.c_str(), std::ios::binary); + meshAsset.open(MeshFileName, std::ios::binary); if (meshAsset.is_open()) { // write a header @@ -123,7 +124,7 @@ YoggieEngine::Renderable* AssetManager::LoadFromSource(const std::filesystem::pa meshAsset.write((char*)&Vnum, sizeof(uint32_t)); meshAsset.write((char*)&Enum, sizeof(uint32_t)); // write all vertices - for (auto vertice : exportMesh->vertices) + for (auto& vertice : exportMesh->vertices) { meshAsset.write((char*)&vertice, sizeof(vertice)); } diff --git a/Editor/src/UI/MainMenuBar.h b/Editor/src/UI/MainMenuBar.h index 1dd26a4..20d0602 100644 --- a/Editor/src/UI/MainMenuBar.h +++ b/Editor/src/UI/MainMenuBar.h @@ -122,13 +122,27 @@ public: if (ImGui::MenuItem("Import MeshAsset (temp)")) { auto result = NFD_OpenDialog("mesh", NULL, &path); + switch (result) { case(NFD_OKAY): - AssetManager::LoadFromAssetFile(path); - break; + { + YoggieEngine::Mesh* importedMesh = AssetManager::LoadFromAssetFile(path); + if (importedMesh != nullptr) + { + auto full_name = std::filesystem::path(path); + auto importedModel = scene.AddEntity(full_name.filename().u8string()); + auto& rendererComponent = importedModel.AddComponent(); + rendererComponent.mesh = *importedMesh; + + } + } + break; + case(NFD_CANCEL): + spdlog::debug("User cancelled action"); break; case(NFD_ERROR): + spdlog::warn("Something went wrong!"); break; }