scene.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #include "WallpaperEngine/fs/utils.h"
  8. namespace WallpaperEngine
  9. {
  10. using json = nlohmann::json;
  11. scene::scene (irr::io::path& file)
  12. {
  13. this->m_content = WallpaperEngine::FileSystem::loadFullFile (file);
  14. this->m_json = json::parse (this->m_content);
  15. // check basic elements
  16. json::const_iterator camera_it = this->m_json.find ("camera");
  17. json::const_iterator general_it = this->m_json.find ("general");
  18. json::const_iterator objects_it = this->m_json.find ("objects");
  19. if (camera_it != this->m_json.end () && objects_it.value ().is_array () == true)
  20. {
  21. this->m_camera = new camera (*camera_it);
  22. }
  23. if (objects_it != this->m_json.end () && objects_it.value ().is_array () == true)
  24. {
  25. json::const_iterator cur = this->m_json ["objects"].begin ();
  26. json::const_iterator end = this->m_json ["objects"].end ();
  27. for (; cur != end; cur ++)
  28. {
  29. this->m_objects.push_back (new object (*cur, this));
  30. }
  31. }
  32. json::const_iterator orthogonalprojection = (*general_it).find ("orthogonalprojection");
  33. if (orthogonalprojection != (*general_it).end () && (*orthogonalprojection).is_object () == true)
  34. {
  35. json::const_iterator width = (*orthogonalprojection).find ("width");
  36. json::const_iterator height = (*orthogonalprojection).find ("height");
  37. if (width != (*orthogonalprojection).end () && (*width).is_number () == true)
  38. {
  39. this->m_width = *width;
  40. }
  41. if (height != (*orthogonalprojection).end () && (*height).is_number () == true)
  42. {
  43. this->m_height= *height;
  44. }
  45. this->m_isOrthogonal = true;
  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. }