From 975edf0e62a7b20e38d2ded17fa959a76162bc65 Mon Sep 17 00:00:00 2001 From: CatChow0 Date: Tue, 1 Oct 2024 16:02:30 +0200 Subject: [PATCH] =?UTF-8?q?Am=C3=A9liorations=20de=20la=20gestion=20des=20?= =?UTF-8?q?chemins=20et=20textures?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Ajout de la récupération du chemin d'exécution du module dans `WinMain` et transmission à `SystemClass`. - Ajout de la méthode `SendPath` dans `SystemClass` pour transmettre le chemin et le dossier de travail à `ApplicationClass`. - Remplacement de la variable `result` par `Hresult` pour les résultats des appels DirectX dans `ApplicationClass::Initialize`. - Définition du chemin de travail courant avant de charger les textures dans `ApplicationClass::AddKobject`. - Vérification que l'objet a bien reçu les textures après l'initialisation dans `ApplicationClass::AddKobject`. - Ajout des méthodes `SetPath` et `SetWFolder` dans `ApplicationClass` pour définir le chemin et le dossier de travail. - Réduction du nombre de catégories de textures affichées dans `imguiManager::WidgetObjectWindow`. --- enginecustom/Main.cpp | 11 ++++++++++ enginecustom/Systemclass.cpp | 6 ++++++ enginecustom/applicationclass.cpp | 32 ++++++++++++++++++++--------- enginecustom/applicationclass.h | 4 ++++ enginecustom/assets/Model/imgui.ini | 24 ++++++++++++++++++++++ enginecustom/imguiManager.cpp | 8 +++++--- enginecustom/modelclass.cpp | 1 - enginecustom/systemclass.h | 2 ++ 8 files changed, 74 insertions(+), 14 deletions(-) create mode 100644 enginecustom/assets/Model/imgui.ini diff --git a/enginecustom/Main.cpp b/enginecustom/Main.cpp index f470c5a..f641411 100644 --- a/enginecustom/Main.cpp +++ b/enginecustom/Main.cpp @@ -6,6 +6,16 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pScmdline, SystemClass* System; bool result; + wchar_t path[MAX_PATH]; + HMODULE hmodule = GetModuleHandle(NULL); + if (hmodule != NULL) + { + GetModuleFileName(hmodule, path, (sizeof(path) / sizeof(wchar_t))); + } + + std::filesystem::path exePath(path); + std::filesystem::path WFolder = exePath.parent_path(); + // Create the system object. System = new SystemClass; @@ -14,6 +24,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pScmdline, if (result) { Logger::Get().Log("System initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize); + System->SendPath(path,WFolder); System->Run(); } diff --git a/enginecustom/Systemclass.cpp b/enginecustom/Systemclass.cpp index 95a6451..b36806b 100644 --- a/enginecustom/Systemclass.cpp +++ b/enginecustom/Systemclass.cpp @@ -462,4 +462,10 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT umessage, WPARAM wparam, LPARAM lparam) return ApplicationHandle->MessageHandler(hwnd, umessage, wparam, lparam); } } +} + +void SystemClass::SendPath(wchar_t* path, std::filesystem::path WFolder) +{ + m_Application->SetPath(path); + m_Application->SetWFolder(WFolder); } \ No newline at end of file diff --git a/enginecustom/applicationclass.cpp b/enginecustom/applicationclass.cpp index 7fe8233..4cb3a87 100644 --- a/enginecustom/applicationclass.cpp +++ b/enginecustom/applicationclass.cpp @@ -51,6 +51,7 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd) char spriteFilename[128]; char fpsString[32]; bool result; + HRESULT Hresult; m_screenWidth = screenWidth; m_screenHeight = screenHeight; @@ -182,8 +183,8 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd) for (const auto& textureFilename : textureFilenames) { ID3D11ShaderResourceView* texture = nullptr; - result = DirectX::CreateWICTextureFromFile(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), textureFilename.c_str(), nullptr, &texture); - if (FAILED(result)) + Hresult = DirectX::CreateWICTextureFromFile(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), textureFilename.c_str(), nullptr, &texture); + if (FAILED(Hresult)) { Logger::Get().Log("Failed to load texture: " + std::string(textureFilename.begin(), textureFilename.end()), __FILE__, __LINE__, Logger::LogLevel::Error); return false; @@ -283,8 +284,8 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd) for (const auto& textureFilename : bathTextures) { ID3D11ShaderResourceView* texture = nullptr; - result = DirectX::CreateWICTextureFromFile(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), textureFilename.c_str(), nullptr, &texture); - if (FAILED(result)) + Hresult = DirectX::CreateWICTextureFromFile(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), textureFilename.c_str(), nullptr, &texture); + if (FAILED(Hresult)) { Logger::Get().Log("Failed to load texture: " + std::string(textureFilename.begin(), textureFilename.end()), __FILE__, __LINE__, Logger::LogLevel::Error); return false; @@ -316,8 +317,8 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd) for (const auto& textureFilename : waterTextures) { ID3D11ShaderResourceView* texture = nullptr; - result = DirectX::CreateWICTextureFromFile(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), textureFilename.c_str(), nullptr, &texture); - if (FAILED(result)) + Hresult = DirectX::CreateWICTextureFromFile(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), textureFilename.c_str(), nullptr, &texture); + if (FAILED(Hresult)) { Logger::Get().Log("Failed to load texture: " + std::string(textureFilename.begin(), textureFilename.end()), __FILE__, __LINE__, Logger::LogLevel::Error); return false; @@ -1661,7 +1662,7 @@ void ApplicationClass::AddKobject(WCHAR* filepath) char modelFilename[128]; vector Filename; - bool result; + HRESULT result; filesystem::path p(filepath); string filename = p.stem().string(); @@ -1669,10 +1670,11 @@ void ApplicationClass::AddKobject(WCHAR* filepath) size_t convertedChars = 0; wcstombs_s(&convertedChars, modelFilename, sizeof(modelFilename), filepath, _TRUNCATE); + filesystem::current_path(m_WFolder); // Liste des fichiers de texture std::vector kobjTexture = { - L"assets/Texture/marble01.png" + L"assets/Texture/Bricks2K.png" }; @@ -1691,6 +1693,10 @@ void ApplicationClass::AddKobject(WCHAR* filepath) std::wstring ws(errMsg); std::string str(ws.begin(), ws.end()); + // Log the current working directory + std::filesystem::path cwd = std::filesystem::current_path(); + Logger::Get().Log("Current working directory: " + cwd.string(), __FILE__, __LINE__, Logger::LogLevel::Error); + Logger::Get().Log("Failed to load texture: " + std::string(textureFilename.begin(), textureFilename.end()) + "\nError: " + std::to_string(result) + "\nDescription: " + str, @@ -1700,7 +1706,6 @@ void ApplicationClass::AddKobject(WCHAR* filepath) textures.push_back(texture); } - Object* newObject = new Object(); newObject->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), modelFilename, textures); newObject->SetMass(1.0f); @@ -1711,6 +1716,12 @@ void ApplicationClass::AddKobject(WCHAR* filepath) m_ObjectId++; m_object.push_back(newObject); + + // Vérifiez que l'objet a bien reçu les textures + if (newObject->GetTexture(0) == nullptr) + { + Logger::Get().Log("Object texture is null after initialization", __FILE__, __LINE__, Logger::LogLevel::Error); + } } void ApplicationClass::AddCube() @@ -1719,7 +1730,7 @@ void ApplicationClass::AddCube() Logger::Get().Log("Adding cube", __FILE__, __LINE__); char modelFilename[128]; - bool result; + HRESULT result; // Set the file name of the model. strcpy_s(modelFilename, "assets/Model/TXT/cube.txt"); @@ -1751,6 +1762,7 @@ void ApplicationClass::AddCube() newCube->SetTranslateMatrix(XMMatrixTranslation(position, 0.0f, 0.0f)); m_cubes.push_back(newCube); + } void ApplicationClass::DeleteKobject(int index) diff --git a/enginecustom/applicationclass.h b/enginecustom/applicationclass.h index dd854f0..28e46ea 100644 --- a/enginecustom/applicationclass.h +++ b/enginecustom/applicationclass.h @@ -72,6 +72,8 @@ public: std::vector GetTerrainCubes() const { return m_terrainChunk; }; std::vector GetKobjects() const { return m_object; }; void AddKobject(WCHAR* filepath); + void SetPath(WCHAR* path) { m_path = path; }; + void SetWFolder(std::filesystem::path WFolder) { m_WFolder = WFolder; }; void GenerateTerrain(); void DeleteTerrain(); @@ -160,6 +162,8 @@ private : // ----------------------------------- // float m_waterHeight, m_waterTranslation; + wchar_t* m_path; + std::filesystem::path m_WFolder; // ------------------------------------------------- // // ------------- FPS AND INFO ON SCREEN ------------ // diff --git a/enginecustom/assets/Model/imgui.ini b/enginecustom/assets/Model/imgui.ini new file mode 100644 index 0000000..672e85e --- /dev/null +++ b/enginecustom/assets/Model/imgui.ini @@ -0,0 +1,24 @@ +[Window][Debug##Default] +Pos=60,60 +Size=400,400 + +[Window][Khaotic Engine] +Pos=1120,51 +Size=392,273 + +[Window][Objects] +Pos=930,39 +Size=457,294 + +[Window][Terrain] +Pos=60,60 +Size=342,82 + +[Window][Light] +Pos=1551,17 +Size=358,535 + +[Window][Shader Manager] +Pos=30,255 +Size=172,284 + diff --git a/enginecustom/imguiManager.cpp b/enginecustom/imguiManager.cpp index 5cda2f1..dcae250 100644 --- a/enginecustom/imguiManager.cpp +++ b/enginecustom/imguiManager.cpp @@ -161,10 +161,12 @@ void imguiManager::WidgetObjectWindow(ApplicationClass* app) // Texture // add all texture category names to a vector - std::vector textureCategories = { "Diffuse", "Normal", "Specular", "Alpha", "Light", "Change Me" }; + std::vector textureCategories = { + "Diffuse" + }; - for (int count = 0; count < 6; count++) + for (int count = 0; count < 1; count++) { std::string textureLabel = "Texture##" + std::to_string(index); ID3D11ShaderResourceView* texture = object->GetTexture(count); @@ -185,7 +187,7 @@ void imguiManager::WidgetObjectWindow(ApplicationClass* app) } // Display all images - for (int count = 0; count < 6; count++) + for (int count = 0; count < 1; count++) { std::string textureLabel = "Texture##" + std::to_string(index); ID3D11ShaderResourceView* texture = object->GetTexture(count); diff --git a/enginecustom/modelclass.cpp b/enginecustom/modelclass.cpp index 78107d2..7b53b56 100644 --- a/enginecustom/modelclass.cpp +++ b/enginecustom/modelclass.cpp @@ -84,7 +84,6 @@ ID3D11ShaderResourceView* ModelClass::GetTexture(int index) const { return nullptr; // Retourne nullptr si l'index est hors limites } - bool ModelClass::InitializeBuffers(ID3D11Device* device) { Logger::Get().Log("Initializing buffers", __FILE__, __LINE__, Logger::LogLevel::Initialize); diff --git a/enginecustom/systemclass.h b/enginecustom/systemclass.h index 8039b58..24de3c2 100644 --- a/enginecustom/systemclass.h +++ b/enginecustom/systemclass.h @@ -24,6 +24,8 @@ public: LRESULT CALLBACK MessageHandler(HWND, UINT, WPARAM, LPARAM); + void SendPath(wchar_t* path, std::filesystem::path WFolder); + private: bool Frame(); void InitializeWindows(int&, int&);