Unit.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. #include "SceneGraphManager.h"
  29. #include "Actor.h"
  30. #include "Controller.h"
  31. #include "PhysicsResource.h"
  32. #include "Device.h"
  33. #include "ResourceManager.h"
  34. namespace crown
  35. {
  36. typedef Id CameraId;
  37. typedef Id SpriteId;
  38. static int count = 0;
  39. //-----------------------------------------------------------------------------
  40. Unit::Unit(World& w, SceneGraph& sg, const UnitResource* ur, const Matrix4x4& pose)
  41. : m_world(w)
  42. , m_scene_graph(sg)
  43. , m_resource(ur)
  44. , m_num_cameras(0)
  45. , m_num_meshes(0)
  46. , m_num_sprites(0)
  47. {
  48. // Log debug info
  49. Log::d("Creating unit...");
  50. Log::d("Num scene graph nodes = %d", ur->num_scene_graph_nodes());
  51. Log::d("Num renderables = %d", ur->num_renderables());
  52. Log::d("Num cameras = %d", ur->num_cameras());
  53. Log::d("Num actors = %d", ur->num_actors());
  54. m_controller.component.id = INVALID_ID;
  55. create(pose);
  56. }
  57. //-----------------------------------------------------------------------------
  58. Unit::~Unit()
  59. {
  60. // Destroy cameras
  61. for (uint32_t i = 0; i < m_num_cameras; i++)
  62. {
  63. m_world.destroy_camera(m_cameras[i].component);
  64. }
  65. // Destroy meshes
  66. for (uint32_t i = 0; i < m_num_meshes; i++)
  67. {
  68. m_world.render_world()->destroy_mesh(m_meshes[i].component);
  69. }
  70. // Destroy sprites
  71. for (uint32_t i = 0; i < m_num_sprites; i++)
  72. {
  73. m_world.render_world()->destroy_sprite(m_sprites[i].component);
  74. }
  75. // Destroy actors
  76. for (uint32_t i = 0; i < m_num_actors; i++)
  77. {
  78. m_world.physics_world()->destroy_actor(m_actors[i].component);
  79. }
  80. // Destroy controller
  81. if (m_controller.component.id != INVALID_ID)
  82. {
  83. m_world.physics_world()->destroy_controller(m_controller.component);
  84. }
  85. }
  86. //-----------------------------------------------------------------------------
  87. void Unit::set_id(const UnitId id)
  88. {
  89. m_id = id;
  90. }
  91. //-----------------------------------------------------------------------------
  92. UnitId Unit::id()
  93. {
  94. return m_id;
  95. }
  96. //-----------------------------------------------------------------------------
  97. void Unit::create(const Matrix4x4& pose)
  98. {
  99. // Create the scene graph
  100. m_scene_graph.create(m_resource->num_scene_graph_nodes(), m_resource->scene_graph_names(),
  101. m_resource->scene_graph_poses(), m_resource->scene_graph_parents());
  102. // Set root node's pose
  103. m_scene_graph.set_local_pose(0, pose);
  104. create_camera_objects();
  105. create_renderable_objects();
  106. create_physics_objects();
  107. }
  108. //-----------------------------------------------------------------------------
  109. void Unit::create_camera_objects()
  110. {
  111. for (uint32_t i = 0; i < m_resource->num_cameras(); i++)
  112. {
  113. const UnitCamera camera = m_resource->get_camera(i);
  114. const CameraId cam = m_world.create_camera(m_scene_graph, camera.node);
  115. add_camera(camera.name, cam);
  116. }
  117. }
  118. //-----------------------------------------------------------------------------
  119. void Unit::create_renderable_objects()
  120. {
  121. for (uint32_t i = 0; i < m_resource->num_renderables(); i++)
  122. {
  123. const UnitRenderable renderable = m_resource->get_renderable(i);
  124. switch (renderable.type)
  125. {
  126. case UnitRenderable::MESH: add_mesh(renderable.name, m_world.render_world()->create_mesh(renderable.resource, m_scene_graph, renderable.node)); break;
  127. case UnitRenderable::SPRITE: add_sprite(renderable.name, m_world.render_world()->create_sprite(renderable.resource, m_scene_graph, renderable.node)); break;
  128. default: CE_FATAL("Oops, bad renderable type"); break;
  129. }
  130. }
  131. }
  132. //-----------------------------------------------------------------------------
  133. void Unit::create_physics_objects()
  134. {
  135. const StringId32 name_hash = hash::murmur2_32("actor", string::strlen("actor"), 0);
  136. if (count > 1)
  137. add_actor(name_hash, m_world.physics_world()->create_actor(m_scene_graph, 0, ActorType::DYNAMIC_PHYSICAL));
  138. count++;
  139. if (m_resource->physics_resource().id != 0)
  140. {
  141. const PhysicsResource* pr = (PhysicsResource*) device()->resource_manager()->data(m_resource->physics_resource());
  142. if (pr->has_controller())
  143. {
  144. set_controller(pr->controller().name, m_world.physics_world()->create_controller(pr, m_scene_graph, 0));
  145. }
  146. }
  147. }
  148. //-----------------------------------------------------------------------------
  149. int32_t Unit::node(const char* name) const
  150. {
  151. return m_scene_graph.node(name);
  152. }
  153. //-----------------------------------------------------------------------------
  154. bool Unit::has_node(const char* name) const
  155. {
  156. return m_scene_graph.has_node(name);
  157. }
  158. //-----------------------------------------------------------------------------
  159. uint32_t Unit::num_nodes() const
  160. {
  161. return m_scene_graph.num_nodes();
  162. }
  163. //-----------------------------------------------------------------------------
  164. Vector3 Unit::local_position(int32_t node) const
  165. {
  166. return m_scene_graph.local_position(node);
  167. }
  168. //-----------------------------------------------------------------------------
  169. Quaternion Unit::local_rotation(int32_t node) const
  170. {
  171. return m_scene_graph.local_rotation(node);
  172. }
  173. //-----------------------------------------------------------------------------
  174. Matrix4x4 Unit::local_pose(int32_t node) const
  175. {
  176. return m_scene_graph.local_pose(node);
  177. }
  178. //-----------------------------------------------------------------------------
  179. Vector3 Unit::world_position(int32_t node) const
  180. {
  181. return m_scene_graph.world_position(node);
  182. }
  183. //-----------------------------------------------------------------------------
  184. Quaternion Unit::world_rotation(int32_t node) const
  185. {
  186. return m_scene_graph.world_rotation(node);
  187. }
  188. //-----------------------------------------------------------------------------
  189. Matrix4x4 Unit::world_pose(int32_t node) const
  190. {
  191. return m_scene_graph.world_pose(node);
  192. }
  193. //-----------------------------------------------------------------------------
  194. void Unit::set_local_position(int32_t node, const Vector3& pos)
  195. {
  196. m_scene_graph.set_local_position(node, pos);
  197. }
  198. //-----------------------------------------------------------------------------
  199. void Unit::set_local_rotation(int32_t node, const Quaternion& rot)
  200. {
  201. m_scene_graph.set_local_rotation(node, rot);
  202. }
  203. //-----------------------------------------------------------------------------
  204. void Unit::set_local_pose(int32_t node, const Matrix4x4& pose)
  205. {
  206. m_scene_graph.set_local_pose(node, pose);
  207. }
  208. //-----------------------------------------------------------------------------
  209. void Unit::link_node(int32_t child, int32_t parent)
  210. {
  211. m_scene_graph.link(child, parent);
  212. }
  213. //-----------------------------------------------------------------------------
  214. void Unit::unlink_node(int32_t child)
  215. {
  216. m_scene_graph.unlink(child);
  217. }
  218. //-----------------------------------------------------------------------------
  219. void Unit::update()
  220. {
  221. }
  222. //-----------------------------------------------------------------------------
  223. void Unit::add_component(StringId32 name, Id component, uint32_t& size, Component* array)
  224. {
  225. Component comp;
  226. comp.name = name;
  227. comp.component = component;
  228. array[size] = comp;
  229. size++;
  230. }
  231. //-----------------------------------------------------------------------------
  232. Id Unit::find_component(const char* name, uint32_t size, Component* array)
  233. {
  234. uint32_t name_hash = hash::murmur2_32(name, string::strlen(name), 0);
  235. Id comp;
  236. comp.id = INVALID_ID;
  237. for (uint32_t i = 0; i < size; i++)
  238. {
  239. if (name_hash == array[i].name)
  240. {
  241. comp = array[i].component;
  242. }
  243. }
  244. return comp;
  245. }
  246. //-----------------------------------------------------------------------------
  247. Id Unit::find_component(uint32_t index, uint32_t size, Component* array)
  248. {
  249. Id comp;
  250. comp.id = INVALID_ID;
  251. if (index < size)
  252. {
  253. comp = array[index].component;
  254. }
  255. return comp;
  256. }
  257. //-----------------------------------------------------------------------------
  258. void Unit::add_camera(StringId32 name, CameraId camera)
  259. {
  260. CE_ASSERT(m_num_cameras < MAX_CAMERA_COMPONENTS, "Max camera number reached");
  261. add_component(name, camera, m_num_cameras, m_cameras);
  262. }
  263. //-----------------------------------------------------------------------------
  264. void Unit::add_mesh(StringId32 name, MeshId mesh)
  265. {
  266. CE_ASSERT(m_num_meshes < MAX_MESH_COMPONENTS, "Max mesh number reached");
  267. add_component(name, mesh, m_num_meshes, m_meshes);
  268. }
  269. //-----------------------------------------------------------------------------
  270. void Unit::add_sprite(StringId32 name, SpriteId sprite)
  271. {
  272. CE_ASSERT(m_num_sprites < MAX_SPRITE_COMPONENTS, "Max sprite number reached");
  273. add_component(name, sprite, m_num_sprites, m_sprites);
  274. }
  275. //-----------------------------------------------------------------------------
  276. void Unit::add_actor(StringId32 name, ActorId actor)
  277. {
  278. CE_ASSERT(m_num_actors < MAX_ACTOR_COMPONENTS, "Max actor number reached");
  279. add_component(name, actor, m_num_actors, m_actors);
  280. }
  281. //-----------------------------------------------------------------------------
  282. void Unit::set_controller(StringId32 name, ControllerId controller)
  283. {
  284. m_controller.name = name;
  285. m_controller.component = controller;
  286. }
  287. //-----------------------------------------------------------------------------
  288. Camera* Unit::camera(const char* name)
  289. {
  290. CameraId cam = find_component(name, m_num_cameras, m_cameras);
  291. CE_ASSERT(cam.id != INVALID_ID, "Unit does not have camera with name '%s'", name);
  292. return m_world.lookup_camera(cam);
  293. }
  294. //-----------------------------------------------------------------------------
  295. Camera* Unit::camera(uint32_t i)
  296. {
  297. CameraId cam = find_component(i, m_num_cameras, m_cameras);
  298. CE_ASSERT(cam.id != INVALID_ID, "Unit does not have camera with index '%d'", i);
  299. return m_world.lookup_camera(cam);
  300. }
  301. //-----------------------------------------------------------------------------
  302. Mesh* Unit::mesh(const char* name)
  303. {
  304. MeshId mesh = find_component(name, m_num_meshes, m_meshes);
  305. CE_ASSERT(mesh.id != INVALID_ID, "Unit does not have mesh with name '%s'", name);
  306. return m_world.render_world()->lookup_mesh(mesh);
  307. }
  308. //-----------------------------------------------------------------------------
  309. Mesh* Unit::mesh(uint32_t i)
  310. {
  311. MeshId mesh = find_component(i, m_num_meshes, m_meshes);
  312. CE_ASSERT(mesh.id != INVALID_ID, "Unit does not have mesh with index '%d'", i);
  313. return m_world.render_world()->lookup_mesh(mesh);
  314. }
  315. //-----------------------------------------------------------------------------
  316. Sprite* Unit::sprite(const char* name)
  317. {
  318. SpriteId sprite = find_component(name, m_num_sprites, m_sprites);
  319. CE_ASSERT(sprite.id != INVALID_ID, "Unit does not have sprite with name '%s'", name);
  320. return m_world.render_world()->lookup_sprite(sprite);
  321. }
  322. //-----------------------------------------------------------------------------
  323. Sprite* Unit::sprite(uint32_t i)
  324. {
  325. SpriteId sprite = find_component(i, m_num_sprites, m_sprites);
  326. CE_ASSERT(sprite.id != INVALID_ID, "Unit does not have sprite with index '%d'", i);
  327. return m_world.render_world()->lookup_sprite(sprite);
  328. }
  329. //-----------------------------------------------------------------------------
  330. Actor* Unit::actor(const char* name)
  331. {
  332. ActorId actor = find_component(name, m_num_actors, m_actors);
  333. CE_ASSERT(actor.id != INVALID_ID, "Unit does not have actor with name '%s'", name);
  334. return m_world.physics_world()->lookup_actor(actor);
  335. }
  336. //-----------------------------------------------------------------------------
  337. Actor* Unit::actor(uint32_t i)
  338. {
  339. ActorId actor = find_component(i, m_num_actors, m_actors);
  340. CE_ASSERT(actor.id != INVALID_ID, "Unit does not have actor with name '%d'", i);
  341. return m_world.physics_world()->lookup_actor(actor);
  342. }
  343. //-----------------------------------------------------------------------------
  344. Controller* Unit::controller()
  345. {
  346. if (m_controller.component.id != INVALID_ID)
  347. {
  348. return m_world.physics_world()->lookup_controller(m_controller.component);
  349. }
  350. return NULL;
  351. }
  352. } // namespace crown