Function to load a scene from a file. This function will prompt the user to choose a file to load.
67 {
68 Logger::Get().
Log(
"Loading scene from " , __FILE__, __LINE__, Logger::LogLevel::Info);
69
72 direct_3d_ = app_->get_direct_3d();
74
75 if (!scenePath.empty()) {
77 }
78
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);
82 return false;
83 }
84
85
86 object_id_ = 0;
87
88
90
91
92 entity_manager->
Clear();
93
94
95 std::wstring currentDirectory = w_folder_;
96
97 std::string line;
98 while (std::getline(inFile, line)) {
99 std::istringstream iss(line);
100 int id;
101 std::string name;
102 XMFLOAT3 position, rotation, scale;
103 std::string modelPath;
104 std::string shaderTypeStr;
105 float boundingRadius;
106 std::string objectTypeStr;
107 float mass;
108 bool physicsEnabled;
109
110
111 iss >> id >> name
112 >> position.x >> position.y >> position.z
113 >> rotation.x >> rotation.y >> rotation.z
114 >> scale.x >> scale.y >> scale.z;
115
116
117 iss >> modelPath;
118
119
120
121 if (modelPath.empty() || (modelPath[0] >= 'A' && modelPath[0] <= 'Z')) {
122
123 iss.seekg(-static_cast<int>(modelPath.length()), std::ios::cur);
124 modelPath = "assets/Model/TXT/cube.txt";
125 }
126
127 iss >> shaderTypeStr
128 >> boundingRadius >> objectTypeStr
129 >> mass >> physicsEnabled;
130
131 if (iss.fail()) {
132 Logger::Get().
Log(
"Failed to parse entity data: " + line, __FILE__, __LINE__, Logger::LogLevel::Error);
133 continue;
134 }
135
136
137 if (id >= object_id_) {
138 object_id_ = id + 1;
139 }
140
141
142 std::wstring wModelPath(modelPath.begin(), modelPath.end());
143
144
145 if (modelPath != "NoModel" && modelPath.length() > 1 && modelPath[1] != ':') {
146
147 if (currentDirectory.back() != L'/' && currentDirectory.back() != L'\\') {
148
149 wModelPath = currentDirectory + L"\\" + wModelPath;
150 } else {
151 wModelPath = currentDirectory + wModelPath;
152 }
153 }
154
155
157
158
159 objectTextures.diffusePaths.clear();
160 objectTextures.normalPaths.clear();
161 objectTextures.specularPaths.clear();
162 objectTextures.alphaPaths.clear();
163
164
165 int diffuseTextureCount;
166 iss >> diffuseTextureCount;
167 for (int i = 0; i < diffuseTextureCount; i++) {
168 std::string texturePath;
169 iss >> texturePath;
170 std::wstring wTexturePath(texturePath.begin(), texturePath.end());
171 objectTextures.diffusePaths.push_back(wTexturePath);
172 }
173
174
175 int normalTextureCount;
176 iss >> normalTextureCount;
177 for (int i = 0; i < normalTextureCount; i++) {
178 std::string texturePath;
179 iss >> texturePath;
180 std::wstring wTexturePath(texturePath.begin(), texturePath.end());
181 objectTextures.normalPaths.push_back(wTexturePath);
182 }
183
184
185 int specularTextureCount;
186 iss >> specularTextureCount;
187 for (int i = 0; i < specularTextureCount; i++) {
188 std::string texturePath;
189 iss >> texturePath;
190 std::wstring wTexturePath(texturePath.begin(), texturePath.end());
191 objectTextures.specularPaths.push_back(wTexturePath);
192 }
193
194
195 int alphaTextureCount;
196 iss >> alphaTextureCount;
197 for (int i = 0; i < alphaTextureCount; i++) {
198 std::string texturePath;
199 iss >> texturePath;
200 std::wstring wTexturePath(texturePath.begin(), texturePath.end());
201 objectTextures.alphaPaths.push_back(wTexturePath);
202 }
203
204
205 if (modelPath == "NoModel") {
206 Logger::Get().
Log(
"Skipping entity without model: " + name, __FILE__, __LINE__, Logger::LogLevel::Warning);
207 continue;
208 }
209
210
212 std::shared_ptr<model_class> sharedModel;
213
214
215 std::string modelKey = modelPath;
216 auto it = modelCache.find(modelKey);
217 if (it != modelCache.end()) {
218
219 Logger::Get().
Log(
"Using cached model for: " + modelKey, __FILE__, __LINE__, Logger::LogLevel::Info);
220 sharedModel = it->second;
221 } else {
222
223 char modelFilename[256];
224 size_t convertedChars = 0;
225 wcstombs_s(&convertedChars, modelFilename, sizeof(modelFilename), wModelPath.c_str(), _TRUNCATE);
226
227 Logger::Get().
Log(
"Loading model: " + std::string(modelFilename), __FILE__, __LINE__, Logger::LogLevel::Info);
228
229
230 auto newModel = std::make_shared<model_class>();
231
232
234 Logger::Get().
Log(
"Failed to preload textures for: " + name, __FILE__, __LINE__, Logger::LogLevel::Error);
235 continue;
236 }
237
239 Logger::Get().
Log(
"Failed to initialize model: " + name, __FILE__, __LINE__, Logger::LogLevel::Error);
240 continue;
241 }
242
243
244 modelCache[modelKey] = newModel;
245 sharedModel = newModel;
246 }
247
248
249 auto entity = entity_manager->CreateEntity();
250
251
253 identityComponent->
SetName(name);
255
256
258 transformComponent->
SetPosition(XMLoadFloat3(&position));
259 transformComponent->SetRotation(XMLoadFloat3(&rotation));
260 transformComponent->SetScale(XMLoadFloat3(&scale));
261 transformComponent->UpdateWorldMatrix();
262
263
266
267
270
271
273 modelPathComponent->
SetPath(wModelPath);
274
275
278 physicsComponent->SetMass(mass);
279 physicsComponent->SetBoundingRadius(boundingRadius);
280 physicsComponent->SetPhysicsEnabled(physicsEnabled);
281
282 Logger::Get().
Log(
"Entity loaded: " + name +
" with ID: " + std::to_string(
id), __FILE__, __LINE__, Logger::LogLevel::Info);
283 }
284
285
287
288
290
291 Logger::Get().
Log(
"Scene loaded successfully from " + scene_path_, __FILE__, __LINE__, Logger::LogLevel::Info);
292 return true;
293}
std::filesystem::path get_w_folder() const
int get_object_id() const
std::map< std::string, std::shared_ptr< model_class > > & get_model_cache()
void update_stats_after_modification()
ecs::EntityManager * get_entity_manager() const
void set_object_id(int object_id)
ID3D11Device * get_device()
Gets the Direct3D device.
ID3D11DeviceContext * get_device_context()
Gets the Direct3D device context.
static ObjectType StringToObjectType(const std::string &str)
void SetName(const std::string &name)
void SetPath(const std::wstring &path)
void Initialize() override
bool InitializeWithModel(std::shared_ptr< model_class > model)
void SetActiveShader(ShaderType shader)
static ShaderType StringToShaderType(const std::string &str)
std::string convert_w_string_to_string(const std::wstring &w_str)
std::wstring get_scene_path()