Unit.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "Unit.h"
  24. #include "World.h"
  25. #include "Allocator.h"
  26. #include "Log.h"
  27. #include "UnitResource.h"
  28. namespace crown
  29. {
  30. typedef Id CameraId;
  31. typedef Id SpriteId;
  32. Unit::Unit()
  33. : m_world(NULL)
  34. , m_num_cameras(0)
  35. , m_num_meshes(0)
  36. {
  37. }
  38. //-----------------------------------------------------------------------------
  39. void Unit::create(World& world, UnitResource* ur, UnitId id, const Vector3& pos, const Quaternion& rot)
  40. {
  41. m_root_node = m_scene_graph.create_node(-1, pos, rot);
  42. // Create renderables
  43. for (uint32_t i = 0; i < ur->num_renderables(); i++)
  44. {
  45. int32_t node = m_scene_graph.create_node(m_root_node, Vector3::ZERO, Quaternion::IDENTITY);
  46. UnitRenderable renderable = ur->get_renderable(i);
  47. if (renderable.type == UnitRenderable::MESH)
  48. {
  49. MeshId mesh = world.create_mesh(renderable.resource, node, Vector3::ZERO, Quaternion::IDENTITY);
  50. add_mesh(renderable.name, mesh);
  51. }
  52. else if (renderable.type == UnitRenderable::SPRITE)
  53. {
  54. SpriteId sprite = world.create_sprite(renderable.resource, node, Vector3::ZERO, Quaternion::IDENTITY);
  55. world.link_sprite(sprite, id, m_root_node);
  56. add_sprite(renderable.name, sprite);
  57. }
  58. else
  59. {
  60. CE_FATAL("Oops, bad renderable type");
  61. }
  62. }
  63. // Create cameras
  64. for (uint32_t i = 0; i < ur->num_cameras(); i++)
  65. {
  66. int32_t cam_node = m_scene_graph.create_node(m_root_node, Vector3::ZERO, Quaternion::IDENTITY);
  67. UnitCamera camera = ur->get_camera(i);
  68. CameraId cam = world.create_camera(cam_node, Vector3::ZERO, Quaternion::IDENTITY);
  69. world.link_camera(cam, id, m_root_node);
  70. add_camera(camera.name, cam);
  71. }
  72. m_world = &world;
  73. m_resource = ur;
  74. m_id = id;
  75. }
  76. //-----------------------------------------------------------------------------
  77. void Unit::destroy()
  78. {
  79. }
  80. //-----------------------------------------------------------------------------
  81. Vector3 Unit::local_position(int32_t node) const
  82. {
  83. return m_scene_graph.local_position(node);
  84. }
  85. //-----------------------------------------------------------------------------
  86. Quaternion Unit::local_rotation(int32_t node) const
  87. {
  88. return m_scene_graph.local_rotation(node);
  89. }
  90. //-----------------------------------------------------------------------------
  91. Matrix4x4 Unit::local_pose(int32_t node) const
  92. {
  93. return m_scene_graph.local_pose(node);
  94. }
  95. //-----------------------------------------------------------------------------
  96. Vector3 Unit::world_position(int32_t node) const
  97. {
  98. return m_scene_graph.world_position(node);
  99. }
  100. //-----------------------------------------------------------------------------
  101. Quaternion Unit::world_rotation(int32_t node) const
  102. {
  103. return m_scene_graph.world_rotation(node);
  104. }
  105. //-----------------------------------------------------------------------------
  106. Matrix4x4 Unit::world_pose(int32_t node) const
  107. {
  108. return m_scene_graph.world_pose(node);
  109. }
  110. //-----------------------------------------------------------------------------
  111. void Unit::set_local_position(const Vector3& pos, int32_t node)
  112. {
  113. m_scene_graph.set_local_position(node, pos);
  114. }
  115. //-----------------------------------------------------------------------------
  116. void Unit::set_local_rotation(const Quaternion& rot, int32_t node)
  117. {
  118. m_scene_graph.set_local_rotation(node, rot);
  119. }
  120. //-----------------------------------------------------------------------------
  121. void Unit::set_local_pose(const Matrix4x4& pose, int32_t node)
  122. {
  123. m_scene_graph.set_local_pose(node, pose);
  124. }
  125. //-----------------------------------------------------------------------------
  126. void Unit::link_node(int32_t child, int32_t parent)
  127. {
  128. m_scene_graph.link(child, parent);
  129. }
  130. //-----------------------------------------------------------------------------
  131. void Unit::unlink_node(int32_t child)
  132. {
  133. m_scene_graph.unlink(child);
  134. }
  135. //-----------------------------------------------------------------------------
  136. void Unit::add_component(StringId32 name, Id component, uint32_t& size, Component* array)
  137. {
  138. Component comp;
  139. comp.name = name;
  140. comp.component = component;
  141. array[size] = comp;
  142. size++;
  143. }
  144. //-----------------------------------------------------------------------------
  145. Id Unit::find_component(const char* name, uint32_t size, Component* array)
  146. {
  147. uint32_t name_hash = hash::murmur2_32(name, string::strlen(name), 0);
  148. Id comp;
  149. comp.id = INVALID_ID;
  150. for (uint32_t i = 0; i < size; i++)
  151. {
  152. if (name_hash == array[i].name)
  153. {
  154. comp = array[i].component;
  155. }
  156. }
  157. return comp;
  158. }
  159. //-----------------------------------------------------------------------------
  160. Id Unit::find_component(uint32_t index, uint32_t size, Component* array)
  161. {
  162. Id comp;
  163. comp.id = INVALID_ID;
  164. if (index < size)
  165. {
  166. comp = array[index].component;
  167. }
  168. return comp;
  169. }
  170. //-----------------------------------------------------------------------------
  171. void Unit::add_camera(StringId32 name, CameraId camera)
  172. {
  173. CE_ASSERT(m_num_cameras < MAX_CAMERA_COMPONENTS, "Max camera number reached");
  174. add_component(name, camera, m_num_cameras, m_cameras);
  175. }
  176. //-----------------------------------------------------------------------------
  177. void Unit::add_mesh(StringId32 name, MeshId mesh)
  178. {
  179. CE_ASSERT(m_num_meshes < MAX_MESH_COMPONENTS, "Max mesh number reached");
  180. add_component(name, mesh, m_num_meshes, m_meshes);
  181. }
  182. //-----------------------------------------------------------------------------
  183. void Unit::add_sprite(StringId32 name, SpriteId sprite)
  184. {
  185. CE_ASSERT(m_num_sprites < MAX_SPRITE_COMPONENTS, "Max sprite number reached");
  186. add_component(name, sprite, m_num_sprites, m_sprites);
  187. }
  188. //-----------------------------------------------------------------------------
  189. Camera* Unit::camera(const char* name)
  190. {
  191. CameraId cam = find_component(name, m_num_cameras, m_cameras);
  192. CE_ASSERT(cam.id != INVALID_ID, "Unit does not have camera with name '%s'", name);
  193. return m_world->lookup_camera(cam);
  194. }
  195. //-----------------------------------------------------------------------------
  196. Camera* Unit::camera(uint32_t i)
  197. {
  198. CameraId cam = find_component(i, m_num_cameras, m_cameras);
  199. CE_ASSERT(cam.id != INVALID_ID, "Unit does not have camera with index '%d'", i);
  200. return m_world->lookup_camera(cam);
  201. }
  202. //-----------------------------------------------------------------------------
  203. Mesh* Unit::mesh(const char* name)
  204. {
  205. MeshId mesh = find_component(name, m_num_meshes, m_meshes);
  206. CE_ASSERT(mesh.id != INVALID_ID, "Unit does not have mesh with name '%s'", name);
  207. return m_world->lookup_mesh(mesh);
  208. }
  209. //-----------------------------------------------------------------------------
  210. Mesh* Unit::mesh(uint32_t i)
  211. {
  212. MeshId mesh = find_component(i, m_num_meshes, m_meshes);
  213. CE_ASSERT(mesh.id != INVALID_ID, "Unit does not have mesh with index '%d'", i);
  214. return m_world->lookup_mesh(mesh);
  215. }
  216. //-----------------------------------------------------------------------------
  217. Sprite* Unit::sprite(const char* name)
  218. {
  219. SpriteId sprite = find_component(name, m_num_sprites, m_sprites);
  220. CE_ASSERT(sprite.id != INVALID_ID, "Unit does not have sprite with name '%s'", name);
  221. return m_world->lookup_sprite(sprite);
  222. }
  223. //-----------------------------------------------------------------------------
  224. Sprite* Unit::sprite(uint32_t i)
  225. {
  226. SpriteId sprite = find_component(i, m_num_sprites, m_sprites);
  227. CE_ASSERT(sprite.id != INVALID_ID, "Unit does not have sprite with index '%d'", i);
  228. return m_world->lookup_sprite(sprite);
  229. }
  230. } // namespace crown