World.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 "Assert.h"
  24. #include "World.h"
  25. #include "Allocator.h"
  26. #include "Device.h"
  27. #include "ResourceManager.h"
  28. namespace crown
  29. {
  30. //-----------------------------------------------------------------------------
  31. World::World()
  32. : m_unit_pool(default_allocator(), MAX_UNITS, sizeof(Unit), CE_ALIGNOF(Unit))
  33. , m_camera_pool(default_allocator(), MAX_CAMERAS, sizeof(Camera), CE_ALIGNOF(Camera))
  34. {
  35. m_id.id = INVALID_ID;
  36. m_sound_world = SoundWorld::create(default_allocator());
  37. }
  38. //-----------------------------------------------------------------------------
  39. World::~World()
  40. {
  41. // Destroy all units
  42. for (uint32_t i = 0; i < m_units.size(); i++)
  43. {
  44. CE_DELETE(m_unit_pool, m_units[i]);
  45. }
  46. SoundWorld::destroy(default_allocator(), m_sound_world);
  47. }
  48. //-----------------------------------------------------------------------------
  49. WorldId World::id() const
  50. {
  51. return m_id;
  52. }
  53. //-----------------------------------------------------------------------------
  54. void World::set_id(WorldId id)
  55. {
  56. m_id = id;
  57. }
  58. //-----------------------------------------------------------------------------
  59. UnitId World::spawn_unit(const char* name, const Vector3& pos, const Quaternion& rot)
  60. {
  61. UnitResource* ur = (UnitResource*) device()->resource_manager()->lookup(UNIT_EXTENSION, name);
  62. return spawn_unit(ur, pos, rot);
  63. }
  64. //-----------------------------------------------------------------------------
  65. UnitId World::spawn_unit(UnitResource* ur, const Vector3& pos, const Quaternion& rot)
  66. {
  67. // Allocate memory for unit
  68. Unit* unit = CE_NEW(m_unit_pool, Unit)(*this, ur, Matrix4x4(rot, pos));
  69. // Create Id for the unit
  70. const UnitId unit_id = m_units.create(unit);
  71. unit->set_id(unit_id);
  72. return unit_id;
  73. }
  74. //-----------------------------------------------------------------------------
  75. void World::destroy_unit(UnitId id)
  76. {
  77. CE_ASSERT(m_units.has(id), "Unit does not exist");
  78. CE_DELETE(m_unit_pool, m_units.lookup(id));
  79. m_units.destroy(id);
  80. }
  81. //-----------------------------------------------------------------------------
  82. void World::reload_units(UnitResource* old_ur, UnitResource* new_ur)
  83. {
  84. for (uint32_t i = 0; i < m_units.size(); i++)
  85. {
  86. if (m_units[i]->resource() == old_ur)
  87. {
  88. m_units[i]->reload(new_ur);
  89. }
  90. }
  91. }
  92. //-----------------------------------------------------------------------------
  93. uint32_t World::num_units() const
  94. {
  95. return m_units.size();
  96. }
  97. //-----------------------------------------------------------------------------
  98. void World::link_unit(UnitId child, UnitId parent, int32_t node)
  99. {
  100. CE_ASSERT(m_units.has(child), "Child unit does not exist");
  101. CE_ASSERT(m_units.has(parent), "Parent unit does not exist");
  102. Unit* parent_unit = lookup_unit(parent);
  103. parent_unit->link_node(0, node);
  104. }
  105. //-----------------------------------------------------------------------------
  106. void World::unlink_unit(UnitId child)
  107. {
  108. CE_ASSERT(m_units.has(child), "Child unit does not exist");
  109. }
  110. //-----------------------------------------------------------------------------
  111. Unit* World::lookup_unit(UnitId unit)
  112. {
  113. CE_ASSERT(m_units.has(unit), "Unit does not exist");
  114. return m_units.lookup(unit);
  115. }
  116. //-----------------------------------------------------------------------------
  117. Camera* World::lookup_camera(CameraId camera)
  118. {
  119. CE_ASSERT(m_cameras.has(camera), "Camera does not exist");
  120. return m_cameras.lookup(camera);
  121. }
  122. //-----------------------------------------------------------------------------
  123. void World::update(float dt)
  124. {
  125. // Update scene graphs
  126. m_scenegraph_manager.update();
  127. // Update physics world
  128. m_physics_world.update(dt);
  129. m_scenegraph_manager.update();
  130. m_sound_world->update();
  131. }
  132. //-----------------------------------------------------------------------------
  133. void World::render(Camera* camera)
  134. {
  135. m_render_world.update(camera->world_pose(), camera->m_projection, camera->m_view_x, camera->m_view_y,
  136. camera->m_view_width, camera->m_view_height);
  137. }
  138. //-----------------------------------------------------------------------------
  139. CameraId World::create_camera(SceneGraph& sg, int32_t node)
  140. {
  141. // Allocate memory for camera
  142. Camera* camera = CE_NEW(m_camera_pool, Camera)(sg, node);
  143. // Create Id for the camera
  144. const CameraId camera_id = m_cameras.create(camera);
  145. return camera_id;
  146. }
  147. //-----------------------------------------------------------------------------
  148. void World::destroy_camera(CameraId id)
  149. {
  150. CE_ASSERT(m_cameras.has(id), "Camera does not exist");
  151. CE_DELETE(m_camera_pool, m_cameras.lookup(id));
  152. m_cameras.destroy(id);
  153. }
  154. //-----------------------------------------------------------------------------
  155. SoundInstanceId World::play_sound(const char* name, const bool loop, const float volume, const Vector3& pos, const float range)
  156. {
  157. m_sound_world->play(name, loop, volume);
  158. }
  159. //-----------------------------------------------------------------------------
  160. void World::stop_sound(SoundInstanceId id)
  161. {
  162. m_sound_world->stop(id);
  163. }
  164. //-----------------------------------------------------------------------------
  165. void World::link_sound(SoundInstanceId id, Unit* unit, int32_t node)
  166. {
  167. }
  168. //-----------------------------------------------------------------------------
  169. void World::set_listener_pose(const Matrix4x4& pose)
  170. {
  171. m_sound_world->set_listener_pose(pose);
  172. }
  173. //-----------------------------------------------------------------------------
  174. void World::set_sound_position(SoundInstanceId id, const Vector3& pos)
  175. {
  176. m_sound_world->set_sound_positions(1, &id, &pos);
  177. }
  178. //-----------------------------------------------------------------------------
  179. void World::set_sound_range(SoundInstanceId id, float range)
  180. {
  181. }
  182. //-----------------------------------------------------------------------------
  183. void World::set_sound_volume(SoundInstanceId id, float vol)
  184. {
  185. m_sound_world->set_sound_volumes(1, &id, &vol);
  186. }
  187. //-----------------------------------------------------------------------------
  188. GuiId World::create_window_gui(const char* name)
  189. {
  190. GuiResource* gr = (GuiResource*)device()->resource_manager()->lookup(GUI_EXTENSION, name);
  191. return m_render_world.create_gui(gr);
  192. }
  193. //-----------------------------------------------------------------------------
  194. GuiId World::create_world_gui(const Matrix4x4 pose, const uint32_t width, const uint32_t height)
  195. {
  196. // Must be implemented
  197. }
  198. //-----------------------------------------------------------------------------
  199. void World::destroy_gui(GuiId id)
  200. {
  201. m_render_world.destroy_gui(id);
  202. }
  203. //-----------------------------------------------------------------------------
  204. Gui* World::lookup_gui(GuiId id)
  205. {
  206. return m_render_world.lookup_gui(id);
  207. }
  208. //-----------------------------------------------------------------------------
  209. SceneGraphManager* World::scene_graph_manager()
  210. {
  211. return &m_scenegraph_manager;
  212. }
  213. //-----------------------------------------------------------------------------
  214. RenderWorld* World::render_world()
  215. {
  216. return &m_render_world;
  217. }
  218. //-----------------------------------------------------------------------------
  219. PhysicsWorld* World::physics_world()
  220. {
  221. return &m_physics_world;
  222. }
  223. //-----------------------------------------------------------------------------
  224. SoundWorld* World::sound_world()
  225. {
  226. return m_sound_world;
  227. }
  228. } // namespace crown