scene.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include <irrlicht/irrlicht.h>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <nlohmann/json.hpp>
  5. #include "scene.h"
  6. #include "camera.h"
  7. namespace wp
  8. {
  9. using json = nlohmann::json;
  10. scene::scene (irr::io::path file, irr::io::path basepath)
  11. {
  12. std::ifstream _in (file.c_str ());
  13. this->m_content = "";
  14. this->m_content.append (std::istreambuf_iterator<char> (_in), std::istreambuf_iterator<char> ());
  15. this->m_json = json::parse (this->m_content);
  16. // check basic elements
  17. json::const_iterator camera_it = this->m_json.find ("camera");
  18. json::const_iterator general_it = this->m_json.find ("general");
  19. json::const_iterator objects_it = this->m_json.find ("objects");
  20. if (camera_it != this->m_json.end () && objects_it.value ().is_array () == true)
  21. {
  22. this->m_camera = new camera (*camera_it);
  23. }
  24. if (objects_it != this->m_json.end () && objects_it.value ().is_array () == true)
  25. {
  26. json::const_iterator cur = this->m_json ["objects"].begin ();
  27. json::const_iterator end = this->m_json ["objects"].end ();
  28. for (; cur != end; cur ++)
  29. {
  30. this->m_objects.push_back (new object (*cur, basepath));
  31. }
  32. }
  33. }
  34. scene::~scene ()
  35. {
  36. // free memory used by the objects
  37. std::vector<object*>::const_iterator cur = this->m_objects.begin ();
  38. std::vector<object*>::const_iterator end = this->m_objects.end ();
  39. for (; cur != end; cur ++)
  40. {
  41. delete *cur;
  42. }
  43. // remove elements from the vector
  44. this->m_objects.erase (this->m_objects.begin (), this->m_objects.end ());
  45. // free camera
  46. delete this->m_camera;
  47. }
  48. }