sculpto
Static Public Member Functions | List of all members
scl::scene_serializer Class Reference

#include <scene_serializer.h>

Static Public Member Functions

static void Serialize (const shared< scene > &Scene, const std::filesystem::path &OutFileName)
 
static void Deserialize (shared< scene > &Scene, const std::filesystem::path &InFileName)
 

Detailed Description

Scene serializer class.

Definition at line 78 of file scene_serializer.h.

Member Function Documentation

◆ Deserialize()

void scl::scene_serializer::Deserialize ( shared< scene > &  Scene,
const std::filesystem::path &  InFileName 
)
static
  • Deserialize scene data from specified file function.
Parameters
Scene- scene to put deserialized date in.
InFileName

Definition at line 240 of file scene_serializer.cpp.

241{
242 const std::string root_json_string = assets_manager::LoadFile(InFileName);
243 const json root_json = json::parse(root_json_string);
244
245 const auto &gui_json = root_json.find("gui");
246 const auto &render_json = root_json.find("render");
247 const auto &scene_data_json = root_json.find("scene_data");
248 const auto &objects_json = root_json.find("objects");
249
250 if (gui_json != root_json.end())
251 {
252 const auto &is_gui_enabled = gui_json->find("is_gui_enabled");
253 const auto &is_dockspace = gui_json->find("is_dockspace");
254
255 if (is_gui_enabled != gui_json->end()) application::Get().GuiEnabled = is_gui_enabled->get<bool>();
256 if (is_dockspace != gui_json->end()) gui::IsDockspace = is_dockspace->get<bool>();
257 }
258 if (render_json != root_json.end())
259 {
260 const auto &clear_color = render_json->find("clear_color");
261 const auto &is_wireframe = render_json->find("is_wireframe");
262 const auto &is_vsync = render_json->find("is_vsync");
263
264 if (clear_color != render_json->end()) render_bridge::SetClearColor(clear_color->get<vec4>());
265 if (is_wireframe != render_json->end()) render_bridge::SetWireframeMode(is_wireframe->get<bool>());
266 if (is_vsync != render_json->end()) render_bridge::SetVSync(is_vsync->get<bool>());
267 }
268 if (scene_data_json != root_json.end())
269 {
270 const auto &viewport_id = scene_data_json->find("viewport_id");
271 const auto &enviroment_ambient = scene_data_json->find("enviroment_ambient");
272
273 if (viewport_id != scene_data_json->end()) Scene->SetViewportId(viewport_id->get<int>());
274 if (enviroment_ambient != scene_data_json->end()) Scene->SetEnviromentAmbient(enviroment_ambient->get<vec3>());
275 }
276 if (objects_json != root_json.end())
277 {
278 for (const auto &object_json : *objects_json)
279 {
280 scene_object object;
281 const auto &name_json = object_json.find(typeid(name_component).name());
282 if (name_json == object_json.end())
283 {
284 object = Scene->CreateObject();
285 }
286 else
287 {
288 name_component object_name_component { "" };
289 from_json(*name_json, object_name_component);
290 object = Scene->CreaetOrGetObject(object_name_component.Name);
291 }
292
293 DeserializeObject(object_json, object);
294 }
295 }
296
297}
static bool IsDockspace
Definition: gui.h:20
static void SetVSync(bool VSync)
Definition: render_bridge.h:44
static void SetClearColor(const vec4 &ClearColor)
Definition: render_bridge.h:38
static void SetWireframeMode(bool IsWireframe)
Definition: render_bridge.h:40
std::string LoadFile(const std::filesystem::path &FilePath)
Definition: files_load.cpp:12
nlohmann::json json
bool from_json(const json &Json, name_component &NameComponent)
math::vec3< float > vec3
Definition: base.h:38
math::vec4< float > vec4
Definition: base.h:39

◆ Serialize()

void scl::scene_serializer::Serialize ( const shared< scene > &  Scene,
const std::filesystem::path &  OutFileName 
)
static
  • Serialize scene data to file function.
Parameters
OutFileName- output file name.
Returns
None.

Definition at line 205 of file scene_serializer.cpp.

206{
207 if (!Scene) return;
208
209 json objects_json;
210 Scene->Registry.each([&](auto Entity)
211 {
212 scene_object object = Scene->GetSceneObject(Entity);
213 if (!object) return;
214
215 json j;
216 SerializeObject(j, object);
217 objects_json.push_back(j);
218 });
219
220 json scene_json = {
221 { "gui", {
222 { "is_gui_enabled", application::Get().GuiEnabled },
223 { "is_dockspace", gui::IsDockspace },
224 } },
225 { "render", {
226 { "clear_color", render_bridge::GetClearColor() },
227 { "is_wireframe", render_bridge::GetWireframeMode() },
228 { "is_vsync", render_bridge::GetVSync() },
229 } },
230 { "scene_data", {
231 { "viewport_id", Scene->GetViewportId(), },
232 { "enviroment_ambient", Scene->GetEnviromentAmbient(), }
233 } },
234 { "objects", objects_json }
235 };
236 assets_manager::SaveFile(scene_json.dump(4), OutFileName);
237 SCL_CORE_INFO("Scene saved to file \"{}\".", OutFileName.string());
238}
static const vec4 & GetClearColor()
Definition: render_bridge.h:29
static bool GetVSync()
Definition: render_bridge.h:35
static bool GetWireframeMode()
Definition: render_bridge.h:31
#define SCL_CORE_INFO(...)
Definition: log.h:41
void SaveFile(const std::string &Data, const std::filesystem::path &FilePath)
Definition: files_save.cpp:12

The documentation for this class was generated from the following files: