68 Logger::Get().
Log(
"Loading scene from " , __FILE__, __LINE__, Logger::LogLevel::Info);
72 direct_3d_ = app_->get_direct_3d();
75 if (!scenePath.empty()) {
79 std::ifstream inFile(scene_path_);
80 if (!inFile.is_open()) {
81 Logger::Get().
Log(
"Failed to open file for loading scene: " + scene_path_, __FILE__, __LINE__, Logger::LogLevel::Error);
92 entity_manager->
Clear();
95 std::wstring currentDirectory = w_folder_;
98 while (std::getline(inFile, line)) {
99 std::istringstream iss(line);
102 XMFLOAT3 position, rotation, scale;
103 std::string modelPath;
104 std::string shaderTypeStr;
105 float boundingRadius;
106 std::string objectTypeStr;
112 >> position.x >> position.y >> position.z
113 >> rotation.x >> rotation.y >> rotation.z
114 >> scale.x >> scale.y >> scale.z;
121 if (modelPath.empty() || (modelPath[0] >=
'A' && modelPath[0] <=
'Z')) {
123 iss.seekg(-
static_cast<int>(modelPath.length()), std::ios::cur);
124 modelPath =
"assets/Model/TXT/cube.txt";
128 >> boundingRadius >> objectTypeStr
129 >> mass >> physicsEnabled;
132 Logger::Get().
Log(
"Failed to parse entity data: " + line, __FILE__, __LINE__, Logger::LogLevel::Error);
137 if (
id >= object_id_) {
142 std::wstring wModelPath(modelPath.begin(), modelPath.end());
145 if (modelPath !=
"NoModel" && modelPath.length() > 1 && modelPath[1] !=
':') {
147 if (currentDirectory.back() != L
'/' && currentDirectory.back() != L
'\\') {
149 wModelPath = currentDirectory + L
"\\" + wModelPath;
151 wModelPath = currentDirectory + wModelPath;
159 objectTextures.diffusePaths.clear();
160 objectTextures.normalPaths.clear();
161 objectTextures.specularPaths.clear();
162 objectTextures.alphaPaths.clear();
165 int diffuseTextureCount;
166 iss >> diffuseTextureCount;
167 for (
int i = 0; i < diffuseTextureCount; i++) {
168 std::string texturePath;
170 std::wstring wTexturePath(texturePath.begin(), texturePath.end());
171 objectTextures.diffusePaths.push_back(wTexturePath);
175 int normalTextureCount;
176 iss >> normalTextureCount;
177 for (
int i = 0; i < normalTextureCount; i++) {
178 std::string texturePath;
180 std::wstring wTexturePath(texturePath.begin(), texturePath.end());
181 objectTextures.normalPaths.push_back(wTexturePath);
185 int specularTextureCount;
186 iss >> specularTextureCount;
187 for (
int i = 0; i < specularTextureCount; i++) {
188 std::string texturePath;
190 std::wstring wTexturePath(texturePath.begin(), texturePath.end());
191 objectTextures.specularPaths.push_back(wTexturePath);
195 int alphaTextureCount;
196 iss >> alphaTextureCount;
197 for (
int i = 0; i < alphaTextureCount; i++) {
198 std::string texturePath;
200 std::wstring wTexturePath(texturePath.begin(), texturePath.end());
201 objectTextures.alphaPaths.push_back(wTexturePath);
205 if (modelPath ==
"NoModel") {
206 Logger::Get().
Log(
"Skipping entity without model: " + name, __FILE__, __LINE__, Logger::LogLevel::Warning);
212 std::shared_ptr<model_class> sharedModel;
215 std::string modelKey = modelPath;
216 auto it = modelCache.find(modelKey);
217 if (it != modelCache.end()) {
219 Logger::Get().
Log(
"Using cached model for: " + modelKey, __FILE__, __LINE__, Logger::LogLevel::Info);
220 sharedModel = it->second;
223 char modelFilename[256];
224 size_t convertedChars = 0;
225 wcstombs_s(&convertedChars, modelFilename,
sizeof(modelFilename), wModelPath.c_str(), _TRUNCATE);
227 Logger::Get().
Log(
"Loading model: " + std::string(modelFilename), __FILE__, __LINE__, Logger::LogLevel::Info);
230 auto newModel = std::make_shared<model_class>();
234 Logger::Get().
Log(
"Failed to preload textures for: " + name, __FILE__, __LINE__, Logger::LogLevel::Error);
239 Logger::Get().
Log(
"Failed to initialize model: " + name, __FILE__, __LINE__, Logger::LogLevel::Error);
244 modelCache[modelKey] = newModel;
245 sharedModel = newModel;
249 auto entity = entity_manager->CreateEntity();
253 identityComponent->
SetName(name);
258 transformComponent->
SetPosition(XMLoadFloat3(&position));
259 transformComponent->SetRotation(XMLoadFloat3(&rotation));
260 transformComponent->SetScale(XMLoadFloat3(&scale));
261 transformComponent->UpdateWorldMatrix();
273 modelPathComponent->
SetPath(wModelPath);
278 physicsComponent->SetMass(mass);
279 physicsComponent->SetBoundingRadius(boundingRadius);
280 physicsComponent->SetPhysicsEnabled(physicsEnabled);
282 Logger::Get().
Log(
"Entity loaded: " + name +
" with ID: " + std::to_string(
id), __FILE__, __LINE__, Logger::LogLevel::Info);
291 Logger::Get().
Log(
"Scene loaded successfully from " + scene_path_, __FILE__, __LINE__, Logger::LogLevel::Info);
299 if (scene_path_.empty()) {
302 Logger::Get().
Log(
"Scene save cancelled by user", __FILE__, __LINE__, Logger::LogLevel::Info);
307 std::ofstream outFile(scene_path_);
308 if (!outFile.is_open()) {
309 Logger::Get().
Log(
"Failed to open file for saving scene", __FILE__, __LINE__, Logger::LogLevel::Error);
313 for (
const auto&
object : entity_) {
314 XMFLOAT3 position, scale, rotation;
317 float boundingRadius = 0;
318 std::string name =
"NONE";
319 std::string shaderType =
"NONE";
320 std::string objectType =
"NONE";
321 std::wstring model_path = L
"";
322 bool physics_enabled =
false;
327 XMStoreFloat3(&position, transform->GetPosition());
328 XMStoreFloat3(&rotation, transform->GetRotation());
329 XMStoreFloat3(&scale, transform->GetScale());
336 id = identity->GetId();
337 name = identity->GetName();
338 objectType = identity->ObjectTypeToString(identity->GetType());
343 if (model_path_component) {
345 model_path = model_path_component->GetPath();
351 shaderType = shader->ShaderTypeToString(shader->GetActiveShader());
356 physics_enabled =
physics->IsPhysicsEnabled();
358 boundingRadius =
physics->GetBoundingRadius();
364 << position.x <<
" " << position.y <<
" " << position.z <<
" "
365 << rotation.x <<
" " << rotation.y <<
" " << rotation.z <<
" "
366 << scale.x <<
" " << scale.y <<
" " << scale.z <<
" "
369 << boundingRadius <<
" "
382 const auto& model = render->GetModel();
384 const auto& textureContainer = model->GetTextureContainer();
386 const auto& diffusePaths = textureContainer.GetPaths(TextureType::Diffuse);
387 outFile <<
" " << diffusePaths.size();
388 for (
const auto& path : diffusePaths) {
392 const auto& normalPaths = textureContainer.GetPaths(TextureType::Normal);
393 outFile <<
" " << normalPaths.size();
394 for (
const auto& path : normalPaths) {
397 const auto& specularPaths = textureContainer.GetPaths(TextureType::Specular);
398 outFile <<
" " << specularPaths.size();
399 for (
const auto& path : specularPaths) {
402 const auto& alphaPaths = textureContainer.GetPaths(TextureType::Alpha);
403 outFile <<
" " << alphaPaths.size();
404 for (
const auto& path : alphaPaths) {
410 outFile << std::endl;
414 Logger::Get().
Log(
"Scene saved successfully to " + scene_path_, __FILE__, __LINE__, Logger::LogLevel::Info);