scene.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include <irrlicht/irrlicht.h>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <nlohmann/json.hpp>
  5. #include <wallpaperengine/scene.h>
  6. #include <wallpaperengine/camera.h>
  7. namespace wp
  8. {
  9. using json = nlohmann::json;
  10. scene::scene (irr::io::path file)
  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, this));
  31. }
  32. }
  33. json::const_iterator orthogonalprojection = (*general_it).find ("orthogonalprojection");
  34. if (orthogonalprojection != (*general_it).end () && (*orthogonalprojection).is_object () == true)
  35. {
  36. json::const_iterator width = (*orthogonalprojection).find ("width");
  37. json::const_iterator height = (*orthogonalprojection).find ("height");
  38. if (width != (*orthogonalprojection).end () && (*width).is_number () == true)
  39. {
  40. this->m_width = *width;
  41. }
  42. if (height != (*orthogonalprojection).end () && (*height).is_number () == true)
  43. {
  44. this->m_height= *height;
  45. }
  46. }
  47. }
  48. scene::~scene ()
  49. {
  50. // free memory used by the objects
  51. std::vector<object*>::const_iterator cur = this->m_objects.begin ();
  52. std::vector<object*>::const_iterator end = this->m_objects.end ();
  53. for (; cur != end; cur ++)
  54. {
  55. delete *cur;
  56. }
  57. // remove elements from the vector
  58. this->m_objects.erase (this->m_objects.begin (), this->m_objects.end ());
  59. // free camera
  60. delete this->m_camera;
  61. }
  62. camera* scene::getCamera ()
  63. {
  64. return this->m_camera;
  65. }
  66. bool scene::isOrthogonal ()
  67. {
  68. return this->m_isOrthogonal;
  69. }
  70. float scene::getProjectionWidth ()
  71. {
  72. return this->m_width;
  73. }
  74. float scene::getProjectionHeight ()
  75. {
  76. return this->m_height;
  77. }
  78. void scene::render ()
  79. {
  80. std::vector<object*>::const_iterator cur = this->m_objects.begin ();
  81. std::vector<object*>::const_iterator end = this->m_objects.end ();
  82. for (; cur != end; cur ++)
  83. {
  84. (*cur)->render ();
  85. }
  86. }
  87. }