object.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #include <irrlicht/irrlicht.h>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <nlohmann/json.hpp>
  5. #include <wallpaperengine/object.h>
  6. #include <wallpaperengine/core.h>
  7. #include <wallpaperengine/object3d.h>
  8. #include <wallpaperengine/image.h>
  9. namespace wp
  10. {
  11. using json = nlohmann::json;
  12. object::object (json json_data, wp::scene* scene) : m_object3d (nullptr)
  13. {
  14. this->m_scene = scene;
  15. json::const_iterator size = json_data.find ("size");
  16. json::const_iterator scale = json_data.find ("scale");
  17. json::const_iterator origin = json_data.find ("origin");
  18. if (size != json_data.end () && size.value ().is_string () == true)
  19. {
  20. std::string size_s = *size;
  21. this->m_size = core::ato2vf (size_s.c_str ());
  22. }
  23. if (scale != json_data.end () && scale.value ().is_string () == true)
  24. {
  25. std::string scale_s = *scale;
  26. this->m_scale = core::ato3vf (scale_s.c_str ());
  27. }
  28. if (origin != json_data.end () && origin.value ().is_string () == true)
  29. {
  30. std::string origin_s = *origin;
  31. this->m_origin = core::ato3vf (origin_s.c_str ());
  32. }
  33. json::const_iterator angles = json_data.find ("angles");
  34. json::const_iterator id = json_data.find ("id");
  35. json::const_iterator name = json_data.find ("name");
  36. if (angles != json_data.end () && (*angles).is_string () == true)
  37. {
  38. std::string angles_s = *angles;
  39. this->m_angles = core::ato3vf (angles_s.c_str ());
  40. }
  41. if (id != json_data.end () && (*id).is_null () == false)
  42. {
  43. this->m_id = *id;
  44. }
  45. if (name != json_data.end () && (*name).is_string () == true)
  46. {
  47. this->m_name = *name;
  48. }
  49. json::const_iterator image = json_data.find ("image");
  50. json::const_iterator model = json_data.find ("model");
  51. json::const_iterator particle = json_data.find ("particle");
  52. object3d::Type _type = object3d::Type::Type_None;
  53. if (image != json_data.end () && (*image).is_null () == false)
  54. {
  55. _type = object3d::Type::Type_Material;
  56. }
  57. if (model != json_data.end () && (*model).is_null () == false)
  58. {
  59. _type = object3d::Type::Type_Model;
  60. }
  61. if (particle != json_data.end () && (*particle).is_null () == false)
  62. {
  63. _type = object3d::Type::Type_Particle;
  64. }
  65. // load the effects first so we have access to the textures needed
  66. json::const_iterator effects = json_data.find ("effects");
  67. if (effects != json_data.end () && (*effects).is_array () == true)
  68. {
  69. json::const_iterator cur = (*effects).begin ();
  70. json::const_iterator end = (*effects).end ();
  71. for (; cur != end; cur ++)
  72. {
  73. this->m_effects.push_back (new effect (*cur, this));
  74. }
  75. }
  76. switch (_type)
  77. {
  78. case object3d::Type::Type_Material:
  79. this->m_object3d = new wp::image (json_data, this);
  80. break;
  81. case object3d::Type::Type_Model:
  82. break;
  83. case object3d::Type::Type_Particle:
  84. break;
  85. }
  86. }
  87. object::~object ()
  88. {
  89. if (this->m_object3d != nullptr)
  90. {
  91. delete this->m_object3d;
  92. }
  93. }
  94. void object::render ()
  95. {
  96. if (this->m_object3d != nullptr)
  97. {
  98. this->m_object3d->render ();
  99. }
  100. }
  101. irr::core::vector2df& object::getSize ()
  102. {
  103. return this->m_size;
  104. }
  105. irr::core::vector3df& object::getScale ()
  106. {
  107. return this->m_scale;
  108. }
  109. irr::core::vector3df& object::getOrigin ()
  110. {
  111. return this->m_origin;
  112. }
  113. irr::core::vector3df& object::getAngles ()
  114. {
  115. return this->m_angles;
  116. }
  117. std::vector<effect*>& object::getEffects ()
  118. {
  119. return this->m_effects;
  120. }
  121. wp::scene* object::getScene ()
  122. {
  123. return this->m_scene;
  124. }
  125. }