Major - Architecture Rework - 11.0.0

This commit is contained in:
2025-06-03 16:29:44 +02:00
parent ce51c11b31
commit d364517633
1441 changed files with 1914 additions and 856805 deletions

View File

@@ -69,7 +69,8 @@ class application_class
public:
application_class();
~application_class();
d_3d_class* get_direct_3d();
virtual d_3d_class* get_direct_3d();
void set_direct_3d(d_3d_class* direct_3d) { direct_3d_ = direct_3d; };
render_texture_class* get_scene_texture() const { return scene_texture_; };
render_texture_class* get_render_texture() const { return render_texture_; };
@@ -82,9 +83,9 @@ public:
void create_big_cube(int side_count);
void process_terrain_generation();
bool initialize(int, int, HWND, bool is_vulkan);
virtual bool initialize(int, int, HWND, bool is_vulkan);
void shutdown();
bool frame(input_class*);
virtual bool frame(input_class*);
void physics_thread_function();
int get_physics_tick_rate() const { return physics_tick_rate_; };
void set_physics_tick_rate(int physics_tick_rate) { physics_tick_rate_ = physics_tick_rate; };
@@ -104,9 +105,14 @@ public:
std::vector<object*> get_cubes() const { return cubes_; };
std::vector<object*> get_terrain_cubes() const { return terrain_chunk_; };
std::vector<object*> get_kobjects() const { return object_; };
void set_kobjects(std::vector<object*> kobjects) { object_ = kobjects; };
void add_kobject(std::wstring& filepath);
void set_path(WCHAR* path) { path_ = path; };
void set_w_folder(const std::filesystem::path& w_folder) { w_folder_ = w_folder; };
std::filesystem::path get_w_folder() const { return w_folder_; };
int get_object_id() const { return object_id_; };
void set_object_id(int object_id) { object_id_ = object_id; };
void generate_terrain();
void delete_terrain();
@@ -142,14 +148,13 @@ public:
// --------------- Stats --------------- //
// ------------------------------------- //
int get_current_fps() const;
int get_min_fps() const;
int get_max_fps() const;
float get_frame_time() const;
int get_draw_calls() const;
void reset_fps_stats();
void increment_draw_call_count();
void reset_draw_call_count();
int get_current_fps() const { return fps_ ? fps_->GetFps() : 0; };
int get_min_fps() const { return fps_ ? fps_->GetMinFps() : 0; };
int get_max_fps() const { return fps_ ? fps_->GetMaxFps() : 0; };
float get_frame_time() const { return fps_ ? fps_->GetFrameTime() : 0.0f; };
int get_draw_calls() const { return drawcalls_; };
void increment_draw_call_count() { drawcalls_++; };
void reset_draw_call_count() { drawcalls_ = 0; };
// ----------------------------------- //
// ------------- Culling ------------- //
@@ -168,15 +173,6 @@ public:
void set_can_fixed_update(bool can_fixed_update) { can_fixed_update_ = can_fixed_update; };
ID3D11ShaderResourceView* get_back_buffer_srv() const {return back_buffer_srv_;};
// Save and load scene
void save_scene();
bool load_scene();
void set_scene_path(const std::string& path) { scene_path_ = path; };
std::wstring get_scene_path();
std::string convert_w_string_to_string(const std::wstring& w_str);
private:
bool render(float, float, float, float, float);
@@ -221,8 +217,6 @@ private :
HWND hwnd_;
bool windowed_;
std::string scene_path_;
// ------------------------------------- //
// ------------- RENDERING ------------- //
// ------------------------------------- //

View File

@@ -34,7 +34,7 @@ public:
d_3d_class(const d_3d_class&);
~d_3d_class();
bool initialize(int, int, bool, HWND, bool, float, float);
virtual bool initialize(int, int, bool, HWND, bool, float, float);
void shutdown();
virtual void begin_scene(float, float, float, float);

View File

@@ -14,6 +14,7 @@
#include <functional>
#include "render_texture_class.h"
#include "scene_manager.h"
class application_class;
@@ -74,6 +75,7 @@ private:
std::vector<widget_entry> widgets_;
application_class* app_;
scene_manager* scene_manager_;
bool showObjectWindow;
bool showTerrainWindow;

View File

@@ -28,9 +28,9 @@ public:
input_class(const input_class&);
~input_class();
bool Initialize(HINSTANCE, HWND, int, int);
void Shutdown();
bool Frame();
virtual bool Initialize(HINSTANCE, HWND, int, int);
virtual void Shutdown();
virtual bool Frame();
bool IsEscapePressed() const;
void GetMouseLocation(int&, int&) const;

View File

@@ -102,8 +102,7 @@ public:
std::string ObjectTypeToString(ObjectType objectType);
void LaunchObject();
bool LoadTexturesFromPath(std::vector<std::wstring>& texturePaths, TextureContainer& texturesContainer,
d_3d_class* m_Direct3D);
bool LoadTexturesFromPath(std::vector<std::wstring>& texturePaths, TextureContainer& texturesContainer,d_3d_class* m_Direct3D);
bool SetupInstancing(ID3D11Device* device, const std::vector<XMMATRIX>& instanceTransforms);
void EnableInstancing(bool enabled);
void SetInstanceCount(int count);

View File

@@ -0,0 +1,35 @@
#pragma once
#include <filesystem>
#include <string>
#include <vector>
class d_3d_class;
class object;
class application_class;
class scene_manager
{
public:
scene_manager();
~scene_manager();
bool initialize(application_class* app);
bool shutdown();
bool save_scene_as();
bool save_scene();
bool load_scene();
std::wstring get_scene_path();
std::string convert_w_string_to_string(const std::wstring& w_str);
private:
application_class* app_;
std::string scene_path_;
std::vector<object*> object_vec_;
int object_id_;
std::filesystem::path w_folder_;
d_3d_class* direct_3d_;
};

View File

@@ -32,6 +32,25 @@ public:
void send_path(wchar_t* path, std::filesystem::path w_folder);
application_class* get_application_class() const { return application_; }
void set_application_class(application_class* application) {
if (application_) {
delete application_;
}
application_ = application;
}
input_class* get_input_class() const { return input_; }
void set_input(input_class* input) {
if (input_) {
delete input_;
}
input_ = input;
}
HWND get_hwnd() const { return hwnd_; }
void set_d_3D_mock(d_3d_class* mock) {application_->set_direct_3d(mock);}
protected:
bool frame();
void initialize_windows(int&, int&);

View File

@@ -2160,288 +2160,6 @@ void application_class::physics_thread_function()
}
}
std::string application_class::convert_w_string_to_string(const std::wstring& wstr)
{
if (wstr.empty()) return std::string();
int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);
std::string str(size_needed, 0);
WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &str[0], size_needed, NULL, NULL);
return str;
}
void application_class::save_scene() {
if (scene_path_.empty()) {
Logger::Get().Log("Scene path is empty. Cannot save scene.", __FILE__, __LINE__, Logger::LogLevel::Error);
return;
}
std::ofstream outFile(scene_path_);
if (!outFile.is_open()) {
Logger::Get().Log("Failed to open file for saving scene", __FILE__, __LINE__, Logger::LogLevel::Error);
return;
}
for (const auto& object : object_) {
XMFLOAT3 position, scale, rotation;
XMStoreFloat3(&position, object->GetPosition());
XMStoreFloat3(&scale, object->GetScale());
XMStoreFloat3(&rotation, object->GetRotation());
// <20>crire les donn<6E>es de base de l'objet
outFile << object->GetId() << " "
<< object->GetName() << " "
<< position.x << " " << position.y << " " << position.z << " "
<< rotation.x << " " << rotation.y << " " << rotation.z << " "
<< scale.x << " " << scale.y << " " << scale.z << " "
<< convert_w_string_to_string(object->GetModelPath()) << " "
<< object->ShaderTypeToString(object->GetActiveShader()) << " "
<< object->GetBoundingRadius() << " "
<< object->ObjectTypeToString(object->GetType()) << " "
<< object->GetMass() << " "
<< object->IsPhysicsEnabled();
// Sauvegarder les chemins des textures_
// Format: nombre de textures_ diffuses, puis les chemins
// M<>me chose pour les autres types de textures_
// Textures diffuses
const auto& diffusePaths = object->GetTextureContainer().GetPaths(TextureType::Diffuse);
outFile << " " << diffusePaths.size();
for (const auto& path : diffusePaths) {
outFile << " " << convert_w_string_to_string(path);
}
// Textures normales
const auto& normalPaths = object->GetTextureContainer().GetPaths(TextureType::Normal);
outFile << " " << normalPaths.size();
for (const auto& path : normalPaths) {
outFile << " " << convert_w_string_to_string(path);
}
// Textures sp<73>culaires
const auto& specularPaths = object->GetTextureContainer().GetPaths(TextureType::Specular);
outFile << " " << specularPaths.size();
for (const auto& path : specularPaths) {
outFile << " " << convert_w_string_to_string(path);
}
// Textures alpha
const auto& alphaPaths = object->GetTextureContainer().GetPaths(TextureType::Alpha);
outFile << " " << alphaPaths.size();
for (const auto& path : alphaPaths) {
outFile << " " << convert_w_string_to_string(path);
}
outFile << std::endl;
}
outFile.close();
Logger::Get().Log("Scene saved successfully to " + scene_path_, __FILE__, __LINE__, Logger::LogLevel::Info);
}
bool application_class::load_scene() {
Logger::Get().Log("Loading scene from " , __FILE__, __LINE__, Logger::LogLevel::Info);
std::wstring scenePath = get_scene_path();
if (!scenePath.empty())
{
set_scene_path(convert_w_string_to_string(scenePath));
}
if (scene_path_.empty()) {
Logger::Get().Log("Scene path is empty. Cannot load scene.", __FILE__, __LINE__, Logger::LogLevel::Error);
return false;
}
std::ifstream inFile(scenePath);
if (!inFile.is_open()) {
Logger::Get().Log("Failed to open file for loading scene: ", __FILE__, __LINE__, Logger::LogLevel::Error);
return false;
}
// Nettoyer les objets existants
for (auto& obj : object_) {
if (obj) {
obj->Shutdown();
delete obj;
}
}
object_.clear();
object_id_ = 0;
// Sauvegarder le r<>pertoire de travail actuel
std::wstring currentDirectory = w_folder_;
std::string line;
while (std::getline(inFile, line)) {
std::istringstream iss(line);
int id;
std::string name;
XMFLOAT3 position, rotation, scale;
std::string modelPath;
std::string shaderTypeStr;
float boundingRadius;
std::string objectTypeStr;
float mass;
bool physicsEnabled;
// Lire les donn<6E>es de base de l'objet
iss >> id >> name
>> position.x >> position.y >> position.z
>> rotation.x >> rotation.y >> rotation.z
>> scale.x >> scale.y >> scale.z
>> modelPath >> shaderTypeStr
>> boundingRadius >> objectTypeStr
>> mass >> physicsEnabled;
if (iss.fail()) {
Logger::Get().Log("Failed to parse object data", __FILE__, __LINE__, Logger::LogLevel::Error);
continue;
}
// Cr<43>er un nouvel objet
object* newObject = new object();
// Convertir le chemin du mod<6F>le en wstring
std::wstring wModelPath(modelPath.begin(), modelPath.end());
// V<>rifier si le chemin est relatif (ne commence pas par un disque comme C:)
if (modelPath.length() > 1 && modelPath[1] != ':') {
// C'est un chemin relatif, pr<70>fixer avec le r<>pertoire de travail
if (currentDirectory.back() != L'/' && currentDirectory.back() != L'\\') {
// Ajouter un s<>parateur si n<>cessaire
wModelPath = currentDirectory + L"\\" + wModelPath;
} else {
wModelPath = currentDirectory + wModelPath;
}
}
// Cr<43>er le conteneur de textures_ pour stocker les chemins
TextureContainer objectTextures;
// IMPORTANT: Vider les conteneurs de chemins de textures_
objectTextures.diffusePaths.clear();
objectTextures.normalPaths.clear();
objectTextures.specularPaths.clear();
objectTextures.alphaPaths.clear();
// Lire les chemins des textures_ diffuses
int diffuseTextureCount;
iss >> diffuseTextureCount;
for (int i = 0; i < diffuseTextureCount; i++) {
std::string texturePath;
iss >> texturePath;
std::wstring wTexturePath(texturePath.begin(), texturePath.end());
objectTextures.diffusePaths.push_back(wTexturePath);
}
// Lire les chemins des textures_ normales
int normalTextureCount;
iss >> normalTextureCount;
for (int i = 0; i < normalTextureCount; i++) {
std::string texturePath;
iss >> texturePath;
std::wstring wTexturePath(texturePath.begin(), texturePath.end());
objectTextures.normalPaths.push_back(wTexturePath);
}
// Lire les chemins des textures_ sp<73>culaires
int specularTextureCount;
iss >> specularTextureCount;
for (int i = 0; i < specularTextureCount; i++) {
std::string texturePath;
iss >> texturePath;
std::wstring wTexturePath(texturePath.begin(), texturePath.end());
objectTextures.specularPaths.push_back(wTexturePath);
}
// Lire les chemins des textures_ alpha
int alphaTextureCount;
iss >> alphaTextureCount;
for (int i = 0; i < alphaTextureCount; i++) {
std::string texturePath;
iss >> texturePath;
std::wstring wTexturePath(texturePath.begin(), texturePath.end());
objectTextures.alphaPaths.push_back(wTexturePath);
}
// preload texture
if (!newObject->PreloadTextures(direct_3d_->get_device(), direct_3d_->get_device_context(), objectTextures))
{
// G<>rer l'erreur
return false;
}
// Initialiser l'objet avec le mod<6F>le et les chemins de textures_
char modelFilename[256];
size_t convertedChars = 0;
wcstombs_s(&convertedChars, modelFilename, sizeof(modelFilename), wModelPath.c_str(), _TRUNCATE);
Logger::Get().Log("Loading model: " + std::string(modelFilename), __FILE__, __LINE__, Logger::LogLevel::Info);
// NE PAS charger les textures_ avant initialize
// Laisser la m<>thode initialize s'en charger <20> partir des chemins
if (!newObject->Initialize(direct_3d_->get_device(), direct_3d_->get_device_context(), modelFilename, objectTextures)) {
Logger::Get().Log("Failed to initialize object: " + name, __FILE__, __LINE__, Logger::LogLevel::Error);
delete newObject;
continue;
}
// D<>finir les propri<72>t<EFBFBD>s de l'objet
newObject->SetId(id);
newObject->SetName(name);
newObject->SetPosition(XMLoadFloat3(&position));
newObject->SetRotation(XMLoadFloat3(&rotation));
newObject->SetScale(XMLoadFloat3(&scale));
newObject->SetModelPath(wModelPath);
newObject->SetActiveShader(newObject->StringToShaderType(shaderTypeStr));
newObject->SetBoundingRadius(boundingRadius);
newObject->SetType(newObject->StringToObjectType(objectTypeStr));
newObject->SetMass(mass);
newObject->SetPhysicsEnabled(physicsEnabled);
// Mettre <20> jour l'ID global si n<>cessaire
if (id >= object_id_) {
object_id_ = id + 1;
}
object_.push_back(newObject);
Logger::Get().Log("Loaded object: " + name, __FILE__, __LINE__, Logger::LogLevel::Info);
}
Logger::Get().Log("Scene loaded successfully from ", __FILE__, __LINE__, Logger::LogLevel::Info);
return true;
}
std::wstring application_class::get_scene_path()
{
OPENFILENAME ofn;
wchar_t szFile[260];
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd_;
ofn.lpstrFile = szFile;
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = L"Ker Scene\0*.ker\0";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
if (GetOpenFileName(&ofn) == TRUE)
{
std::filesystem::path filepath = ofn.lpstrFile;
return filepath.wstring();
}
return L"";
}
int application_class::get_total_vertex_count() const
{
int totalVertices = 0;
@@ -2530,46 +2248,6 @@ int application_class::get_visible_triangle_count() const
return visibleTriangles;
}
int application_class::get_current_fps() const
{
return fps_->GetFps();
}
int application_class::get_min_fps() const
{
return fps_->GetMinFps();
}
int application_class::get_max_fps() const
{
return fps_->GetMaxFps();
}
float application_class::get_frame_time() const
{
return fps_->GetFrameTime();
}
int application_class::get_draw_calls() const
{
return drawcalls_;
}
void application_class::reset_fps_stats()
{
fps_->ResetStats();
}
void application_class::increment_draw_call_count()
{
drawcalls_++;
}
void application_class::reset_draw_call_count()
{
drawcalls_ = 0;
}
void application_class::create_big_cube(int side_count)
{
Logger::Get().Log("Lancement de la g<>n<EFBFBD>ration du terrain dans un thread s<>par<61>", __FILE__, __LINE__, Logger::LogLevel::Info);

View File

@@ -160,6 +160,13 @@ bool imguiManager::Initialize(HWND hwnd, ID3D11Device* device, ID3D11DeviceConte
unsigned char* pixels;
int width, height;
io->Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
// initialize the scene manager
scene_manager_ = new scene_manager;
if (!scene_manager_->initialize(app_)) {
Logger::Get().Log("Failed to initialize scene manager", __FILE__, __LINE__, Logger::LogLevel::Error);
return false;
}
Logger::Get().Log("imgui initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
@@ -237,15 +244,18 @@ void imguiManager::SetupDockspace() {
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Scene")) {
if (ImGui::MenuItem("Save Scene")) {
app_->save_scene();
}
if (ImGui::MenuItem("Load Scene")) {
app_->load_scene();
}
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Scene")) {
if (ImGui::MenuItem("Save Scene")) {
scene_manager_->save_scene();
}
if (ImGui::MenuItem("Save Scene As...")) {
scene_manager_->save_scene_as();
}
if (ImGui::MenuItem("Load Scene")) {
scene_manager_->load_scene();
}
ImGui::EndMenu();
}
ImGui::EndMenuBar();
}

View File

@@ -0,0 +1,353 @@
#include "scene_manager.h"
#include "application_class.h"
scene_manager::scene_manager()
{
}
scene_manager::~scene_manager()
{
shutdown();
}
bool scene_manager::initialize(application_class* app)
{
if (!app) {
Logger::Get().Log("Application pointer is null", __FILE__, __LINE__, Logger::LogLevel::Error);
return false;
}
app_ = app;
return true;
}
bool scene_manager::shutdown()
{
app_ = nullptr;
return true;
}
bool scene_manager::save_scene_as() {
Logger::Get().Log("Saving scene as...", __FILE__, __LINE__, Logger::LogLevel::Info);
OPENFILENAME ofn;
wchar_t szFile[260] = { 0 };
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = app_->get_hwnd();
ofn.lpstrFile = szFile;
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = L"Ker Scene\0*.ker\0";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT;
ofn.lpstrDefExt = L"ker";
if (GetSaveFileName(&ofn) == TRUE) {
std::filesystem::path filepath = ofn.lpstrFile;
// Mettre <20> jour le chemin de sc<73>ne
scene_path_ = convert_w_string_to_string(filepath.wstring());
object_vec_ = app_->get_kobjects();
// Sauvegarder la sc<73>ne avec le nouveau chemin
save_scene();
Logger::Get().Log("Scene saved as: " + scene_path_, __FILE__, __LINE__, Logger::LogLevel::Info);
return false;
}
return true;
}
bool scene_manager::load_scene() {
Logger::Get().Log("Loading scene from " , __FILE__, __LINE__, Logger::LogLevel::Info);
object_vec_ = app_->get_kobjects();
object_id_ = app_->get_object_id();
w_folder_ = app_->get_w_folder();
direct_3d_ = app_->get_direct_3d();
std::wstring scenePath = get_scene_path();
if (!scenePath.empty())
{
scene_path_ = convert_w_string_to_string(scenePath);
}
std::ifstream inFile(scene_path_);
if (!inFile.is_open()) {
Logger::Get().Log("Failed to open file for loading scene: ", __FILE__, __LINE__, Logger::LogLevel::Error);
return false;
}
// Nettoyer les objets existants
for (auto& obj : object_vec_) {
if (obj) {
obj->Shutdown();
delete obj;
}
}
object_vec_.clear();
object_id_ = 0;
// Sauvegarder le r<>pertoire de travail actuel
std::wstring currentDirectory = w_folder_;
std::string line;
while (std::getline(inFile, line)) {
std::istringstream iss(line);
int id;
std::string name;
XMFLOAT3 position, rotation, scale;
std::string modelPath;
std::string shaderTypeStr;
float boundingRadius;
std::string objectTypeStr;
float mass;
bool physicsEnabled;
// Lire les donn<6E>es de base de l'objet
iss >> id >> name
>> position.x >> position.y >> position.z
>> rotation.x >> rotation.y >> rotation.z
>> scale.x >> scale.y >> scale.z
>> modelPath >> shaderTypeStr
>> boundingRadius >> objectTypeStr
>> mass >> physicsEnabled;
if (iss.fail()) {
Logger::Get().Log("Failed to parse object data", __FILE__, __LINE__, Logger::LogLevel::Error);
continue;
}
// Cr<43>er un nouvel objet
object* newObject = new object();
// Convertir le chemin du mod<6F>le en wstring
std::wstring wModelPath(modelPath.begin(), modelPath.end());
// V<>rifier si le chemin est relatif (ne commence pas par un disque comme C:)
if (modelPath.length() > 1 && modelPath[1] != ':') {
// C'est un chemin relatif, pr<70>fixer avec le r<>pertoire de travail
if (currentDirectory.back() != L'/' && currentDirectory.back() != L'\\') {
// Ajouter un s<>parateur si n<>cessaire
wModelPath = currentDirectory + L"\\" + wModelPath;
} else {
wModelPath = currentDirectory + wModelPath;
}
}
// Cr<43>er le conteneur de textures_ pour stocker les chemins
TextureContainer objectTextures;
// IMPORTANT: Vider les conteneurs de chemins de textures_
objectTextures.diffusePaths.clear();
objectTextures.normalPaths.clear();
objectTextures.specularPaths.clear();
objectTextures.alphaPaths.clear();
// Lire les chemins des textures_ diffuses
int diffuseTextureCount;
iss >> diffuseTextureCount;
for (int i = 0; i < diffuseTextureCount; i++) {
std::string texturePath;
iss >> texturePath;
std::wstring wTexturePath(texturePath.begin(), texturePath.end());
objectTextures.diffusePaths.push_back(wTexturePath);
}
// Lire les chemins des textures_ normales
int normalTextureCount;
iss >> normalTextureCount;
for (int i = 0; i < normalTextureCount; i++) {
std::string texturePath;
iss >> texturePath;
std::wstring wTexturePath(texturePath.begin(), texturePath.end());
objectTextures.normalPaths.push_back(wTexturePath);
}
// Lire les chemins des textures_ sp<73>culaires
int specularTextureCount;
iss >> specularTextureCount;
for (int i = 0; i < specularTextureCount; i++) {
std::string texturePath;
iss >> texturePath;
std::wstring wTexturePath(texturePath.begin(), texturePath.end());
objectTextures.specularPaths.push_back(wTexturePath);
}
// Lire les chemins des textures_ alpha
int alphaTextureCount;
iss >> alphaTextureCount;
for (int i = 0; i < alphaTextureCount; i++) {
std::string texturePath;
iss >> texturePath;
std::wstring wTexturePath(texturePath.begin(), texturePath.end());
objectTextures.alphaPaths.push_back(wTexturePath);
}
// preload texture
if (!newObject->PreloadTextures(direct_3d_->get_device(), direct_3d_->get_device_context(), objectTextures))
{
// G<>rer l'erreur
return false;
}
// Initialiser l'objet avec le mod<6F>le et les chemins de textures_
char modelFilename[256];
size_t convertedChars = 0;
wcstombs_s(&convertedChars, modelFilename, sizeof(modelFilename), wModelPath.c_str(), _TRUNCATE);
Logger::Get().Log("Loading model: " + std::string(modelFilename), __FILE__, __LINE__, Logger::LogLevel::Info);
// NE PAS charger les textures_ avant initialize
// Laisser la m<>thode initialize s'en charger <20> partir des chemins
if (!newObject->Initialize(direct_3d_->get_device(), direct_3d_->get_device_context(), modelFilename, objectTextures)) {
Logger::Get().Log("Failed to initialize object: " + name, __FILE__, __LINE__, Logger::LogLevel::Error);
delete newObject;
continue;
}
// D<>finir les propri<72>t<EFBFBD>s de l'objet
newObject->SetId(id);
newObject->SetName(name);
newObject->SetPosition(XMLoadFloat3(&position));
newObject->SetRotation(XMLoadFloat3(&rotation));
newObject->SetScale(XMLoadFloat3(&scale));
newObject->SetModelPath(wModelPath);
newObject->SetActiveShader(newObject->StringToShaderType(shaderTypeStr));
newObject->SetBoundingRadius(boundingRadius);
newObject->SetType(newObject->StringToObjectType(objectTypeStr));
newObject->SetMass(mass);
newObject->SetPhysicsEnabled(physicsEnabled);
// Mettre <20> jour l'ID global si n<>cessaire
if (id >= object_id_) {
object_id_ = id + 1;
app_->set_object_id(object_id_);
}
object_vec_.push_back(newObject);
Logger::Get().Log("Loaded object: " + name, __FILE__, __LINE__, Logger::LogLevel::Info);
}
// pass the vec to the application
app_->set_kobjects(object_vec_);
Logger::Get().Log("Scene loaded successfully from ", __FILE__, __LINE__, Logger::LogLevel::Info);
return true;
}
bool scene_manager::save_scene() {
if (scene_path_.empty()) {
Logger::Get().Log("Scene path is empty. Cannot save scene.", __FILE__, __LINE__, Logger::LogLevel::Error);
return false;
}
std::ofstream outFile(scene_path_);
if (!outFile.is_open()) {
Logger::Get().Log("Failed to open file for saving scene", __FILE__, __LINE__, Logger::LogLevel::Error);
return false;
}
for (const auto& object : object_vec_) {
XMFLOAT3 position, scale, rotation;
XMStoreFloat3(&position, object->GetPosition());
XMStoreFloat3(&scale, object->GetScale());
XMStoreFloat3(&rotation, object->GetRotation());
// <20>crire les donn<6E>es de base de l'objet
outFile << object->GetId() << " "
<< object->GetName() << " "
<< position.x << " " << position.y << " " << position.z << " "
<< rotation.x << " " << rotation.y << " " << rotation.z << " "
<< scale.x << " " << scale.y << " " << scale.z << " "
<< convert_w_string_to_string(object->GetModelPath()) << " "
<< object->ShaderTypeToString(object->GetActiveShader()) << " "
<< object->GetBoundingRadius() << " "
<< object->ObjectTypeToString(object->GetType()) << " "
<< object->GetMass() << " "
<< object->IsPhysicsEnabled();
// Sauvegarder les chemins des textures_
// Format: nombre de textures_ diffuses, puis les chemins
// M<>me chose pour les autres types de textures_
// Textures diffuses
const auto& diffusePaths = object->GetTextureContainer().GetPaths(TextureType::Diffuse);
outFile << " " << diffusePaths.size();
for (const auto& path : diffusePaths) {
outFile << " " << convert_w_string_to_string(path);
}
// Textures normales
const auto& normalPaths = object->GetTextureContainer().GetPaths(TextureType::Normal);
outFile << " " << normalPaths.size();
for (const auto& path : normalPaths) {
outFile << " " << convert_w_string_to_string(path);
}
// Textures sp<73>culaires
const auto& specularPaths = object->GetTextureContainer().GetPaths(TextureType::Specular);
outFile << " " << specularPaths.size();
for (const auto& path : specularPaths) {
outFile << " " << convert_w_string_to_string(path);
}
// Textures alpha
const auto& alphaPaths = object->GetTextureContainer().GetPaths(TextureType::Alpha);
outFile << " " << alphaPaths.size();
for (const auto& path : alphaPaths) {
outFile << " " << convert_w_string_to_string(path);
}
outFile << std::endl;
}
outFile.close();
Logger::Get().Log("Scene saved successfully to " + scene_path_, __FILE__, __LINE__, Logger::LogLevel::Info);
return true;
}
std::wstring scene_manager::get_scene_path()
{
OPENFILENAME ofn;
wchar_t szFile[260];
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = app_->get_hwnd();
ofn.lpstrFile = szFile;
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = L"Ker Scene\0*.ker\0";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
if (GetOpenFileName(&ofn) == TRUE)
{
std::filesystem::path filepath = ofn.lpstrFile;
return filepath.wstring();
}
return L"";
}
std::string scene_manager::convert_w_string_to_string(const std::wstring& wstr)
{
if (wstr.empty()) return std::string();
int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);
std::string str(size_needed, 0);
WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &str[0], size_needed, NULL, NULL);
return str;
}

View File

@@ -169,64 +169,6 @@ void system_class::run()
// Loop until there is a quit message from the window or the user.
done = false;
// Ask if the user wants to load a scene or create a new one
int result_scene = MessageBox(NULL, L"Do you want to load a saved scene?", L"Load Scene", MB_YESNO | MB_ICONQUESTION);
if (result_scene == IDYES)
{
application_->load_scene();
}
else
{
OPENFILENAME ofn;
wchar_t szFile[260] = L"";
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd_;
ofn.lpstrFile = szFile;
ofn.nMaxFile = sizeof(szFile);
// Allow multiple extensions
ofn.lpstrFilter = L"Ker Scene (*.ker)\0*.ker\0All Files (*.*)\0*.*\0";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT;
if (GetSaveFileName(&ofn) == TRUE)
{
std::filesystem::path filepath = ofn.lpstrFile;
std::string path = filepath.string();
// Add the selected extension if not already present
switch (ofn.nFilterIndex)
{
case 1:
if (filepath.extension() != L".ker")
path += ".ker";
break;
case 2:
if (filepath.extension() != L".txt")
path += ".txt";
break;
}
application_->set_scene_path(path);
// Create a new scene file
std::ofstream outFile(path);
if (outFile.is_open())
{
// initialize the new scene file with default content if needed
outFile.close();
Logger::Get().Log("New scene file created successfully", __FILE__, __LINE__, Logger::LogLevel::Info);
}
else
{
Logger::Get().Log("Failed to create new scene file", __FILE__, __LINE__, Logger::LogLevel::Error);
}
}
}
while (!done)
{