object.cpp 4.4 KB

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