Améliorations de la gestion des chemins et textures

- 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`.
This commit is contained in:
CatChow0 2024-10-01 16:02:30 +02:00
parent b3af9f4ce7
commit 975edf0e62
8 changed files with 74 additions and 14 deletions

View File

@ -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();
}

View File

@ -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);
}

View File

@ -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<string> 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<std::wstring> 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)

View File

@ -72,6 +72,8 @@ public:
std::vector<Object*> GetTerrainCubes() const { return m_terrainChunk; };
std::vector<Object*> 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 ------------ //

View File

@ -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

View File

@ -161,10 +161,12 @@ void imguiManager::WidgetObjectWindow(ApplicationClass* app)
// Texture
// add all texture category names to a vector
std::vector<std::string> textureCategories = { "Diffuse", "Normal", "Specular", "Alpha", "Light", "Change Me" };
std::vector<std::string> 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);

View File

@ -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);

View File

@ -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&);