World.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. #include "SoundRenderer.h"
  29. #include "SoundResource.h"
  30. namespace crown
  31. {
  32. //-----------------------------------------------------------------------------
  33. World::World()
  34. : m_unit_pool(default_allocator(), MAX_UNITS, sizeof(Unit), CE_ALIGNOF(Unit))
  35. , m_camera_pool(default_allocator(), MAX_CAMERAS, sizeof(Camera), CE_ALIGNOF(Camera))
  36. , m_unit_to_sound(default_allocator())
  37. {
  38. }
  39. //-----------------------------------------------------------------------------
  40. UnitId World::spawn_unit(const char* name, const Vector3& pos, const Quaternion& rot)
  41. {
  42. // Fetch resource
  43. UnitResource* ur = (UnitResource*) device()->resource_manager()->lookup(UNIT_EXTENSION, name);
  44. // Create a new scene graph
  45. SceneGraph* sg = m_scenegraph_manager.create_scene_graph();
  46. // Allocate memory for unit
  47. Unit* unit = CE_NEW(m_unit_pool, Unit)(*this, *sg, ur, Matrix4x4(rot, pos));
  48. // Create Id for the unit
  49. const UnitId unit_id = m_units.create(unit);
  50. unit->set_id(unit_id);
  51. return unit_id;
  52. }
  53. //-----------------------------------------------------------------------------
  54. void World::destroy_unit(UnitId id)
  55. {
  56. CE_ASSERT(m_units.has(id), "Unit does not exist");
  57. Unit* unit = m_units.lookup(id);
  58. // Destry unit's scene graph
  59. m_scenegraph_manager.destroy_scene_graph(&unit->m_scene_graph);
  60. CE_DELETE(m_unit_pool, unit);
  61. m_units.destroy(id);
  62. }
  63. //-----------------------------------------------------------------------------
  64. uint32_t World::num_units() const
  65. {
  66. return m_units.size();
  67. }
  68. //-----------------------------------------------------------------------------
  69. void World::link_unit(UnitId child, UnitId parent, int32_t node)
  70. {
  71. CE_ASSERT(m_units.has(child), "Child unit does not exist");
  72. CE_ASSERT(m_units.has(parent), "Parent unit does not exist");
  73. Unit* parent_unit = lookup_unit(parent);
  74. parent_unit->link_node(0, node);
  75. }
  76. //-----------------------------------------------------------------------------
  77. void World::unlink_unit(UnitId child)
  78. {
  79. CE_ASSERT(m_units.has(child), "Child unit does not exist");
  80. }
  81. //-----------------------------------------------------------------------------
  82. Unit* World::lookup_unit(UnitId unit)
  83. {
  84. CE_ASSERT(m_units.has(unit), "Unit does not exist");
  85. return m_units.lookup(unit);
  86. }
  87. //-----------------------------------------------------------------------------
  88. Camera* World::lookup_camera(CameraId camera)
  89. {
  90. CE_ASSERT(m_cameras.has(camera), "Camera does not exist");
  91. return m_cameras.lookup(camera);
  92. }
  93. //-----------------------------------------------------------------------------
  94. Mesh* World::lookup_mesh(MeshId mesh)
  95. {
  96. return m_render_world.lookup_mesh(mesh);
  97. }
  98. //-----------------------------------------------------------------------------
  99. Sprite* World::lookup_sprite(SpriteId sprite)
  100. {
  101. return m_render_world.lookup_sprite(sprite);
  102. }
  103. //-----------------------------------------------------------------------------
  104. Actor* World::lookup_actor(ActorId actor)
  105. {
  106. return m_physics_world.lookup_actor(actor);
  107. }
  108. //-----------------------------------------------------------------------------
  109. void World::update(float dt)
  110. {
  111. // Update units
  112. for (Unit** uu = m_units.begin(); uu != m_units.end(); uu++)
  113. {
  114. (*uu)->update();
  115. }
  116. // Update physics world
  117. m_physics_world.update();
  118. // Update scene graphs
  119. m_scenegraph_manager.update();
  120. }
  121. //-----------------------------------------------------------------------------
  122. void World::render(Camera* camera)
  123. {
  124. m_render_world.update(camera->world_pose(), camera->m_projection, camera->m_view_x, camera->m_view_y,
  125. camera->m_view_width, camera->m_view_height);
  126. }
  127. //-----------------------------------------------------------------------------
  128. CameraId World::create_camera(SceneGraph& sg, int32_t node)
  129. {
  130. // Allocate memory for camera
  131. Camera* camera = CE_NEW(m_camera_pool, Camera)(sg, node);
  132. // Create Id for the camera
  133. const CameraId camera_id = m_cameras.create(camera);
  134. return camera_id;
  135. }
  136. //-----------------------------------------------------------------------------
  137. void World::destroy_camera(CameraId id)
  138. {
  139. CE_ASSERT(m_cameras.has(id), "Camera does not exist");
  140. Camera* camera = m_cameras.lookup(id);
  141. CE_DELETE(m_camera_pool, camera);
  142. }
  143. //-----------------------------------------------------------------------------
  144. MeshId World::create_mesh(ResourceId id, SceneGraph& sg, int32_t node)
  145. {
  146. return m_render_world.create_mesh(id, sg, node);
  147. }
  148. //-----------------------------------------------------------------------------
  149. void World::destroy_mesh(MeshId id)
  150. {
  151. m_render_world.destroy_mesh(id);
  152. }
  153. //-----------------------------------------------------------------------------
  154. SpriteId World::create_sprite(ResourceId id, SceneGraph& sg, int32_t node)
  155. {
  156. return m_render_world.create_sprite(id, sg, node);
  157. }
  158. //-----------------------------------------------------------------------------
  159. void World::destroy_sprite(SpriteId id)
  160. {
  161. m_render_world.destroy_sprite(id);
  162. }
  163. //-----------------------------------------------------------------------------
  164. ActorId World::create_actor(ActorType::Enum type)
  165. {
  166. return m_physics_world.create_actor(type);
  167. }
  168. //-----------------------------------------------------------------------------
  169. void World::destroy_actor(ActorId id)
  170. {
  171. m_physics_world.destroy_actor(id);
  172. }
  173. //-----------------------------------------------------------------------------
  174. SoundId World::play_sound(const char* name, const bool loop, const float volume, const Vector3& pos, const float range)
  175. {
  176. SoundResource* sound = (SoundResource*)device()->resource_manager()->lookup(SOUND_EXTENSION, name);
  177. SoundRenderer* sr = device()->sound_renderer();
  178. const SoundSourceId source = sr->create_sound_source();
  179. Sound s;
  180. s.buffer = sound->sound_buffer();
  181. s.source = source;
  182. s.world = Matrix4x4(Quaternion::IDENTITY, pos);
  183. s.volume = volume;
  184. s.range = range;
  185. s.loop = loop;
  186. s.playing = false;
  187. SoundId id = m_sounds.create(s);
  188. sr->bind_buffer(s.buffer, s.source);
  189. sr->set_sound_loop(s.source, s.loop);
  190. sr->set_sound_gain(s.source, s.volume);
  191. sr->set_sound_max_distance(s.source, s.range);
  192. sr->set_sound_position(s.source, s.world.translation());
  193. sr->play_sound(s.source);
  194. return id;
  195. }
  196. //-----------------------------------------------------------------------------
  197. void World::stop_sound(SoundId id)
  198. {
  199. CE_ASSERT(m_sounds.has(id), "Sound does not exists");
  200. const Sound& s = m_sounds.lookup(id);
  201. SoundRenderer* sr = device()->sound_renderer();
  202. sr->pause_sound(s.source);
  203. sr->unbind_buffer(s.source);
  204. sr->destroy_sound_source(s.source);
  205. m_sounds.destroy(id);
  206. }
  207. //-----------------------------------------------------------------------------
  208. void World::link_sound(SoundId id, Unit* unit, int32_t node)
  209. {
  210. //CE_ASSERT(m_units.has(unit), "Unit does not exists");
  211. CE_ASSERT(m_sounds.has(id), "Sound does not exists");
  212. UnitToSound uts;
  213. uts.sound = id;
  214. uts.unit = unit->m_id;
  215. uts.node = node;
  216. m_unit_to_sound.push_back(uts);
  217. }
  218. //-----------------------------------------------------------------------------
  219. void World::set_listener(const Vector3& pos, const Vector3& vel, const Vector3& or_up, const Vector3& or_at)
  220. {
  221. device()->sound_renderer()->set_listener(pos, vel, or_up, or_at);
  222. }
  223. //-----------------------------------------------------------------------------
  224. void World::set_sound_position(SoundId id, const Vector3& pos)
  225. {
  226. CE_ASSERT(m_sounds.has(id), "Sound does not exists");
  227. Sound& sound = m_sounds.lookup(id);
  228. sound.world = Matrix4x4(Quaternion::IDENTITY, pos);
  229. }
  230. //-----------------------------------------------------------------------------
  231. void World::set_sound_range(SoundId id, const float range)
  232. {
  233. CE_ASSERT(m_sounds.has(id), "Sound does not exists");
  234. Sound& sound = m_sounds.lookup(id);
  235. sound.range = range;
  236. }
  237. //-----------------------------------------------------------------------------
  238. void World::set_sound_volume(SoundId id, const float vol)
  239. {
  240. CE_ASSERT(m_sounds.has(id), "Sound does not exists");
  241. Sound& sound = m_sounds.lookup(id);
  242. sound.volume = vol;
  243. }
  244. //-----------------------------------------------------------------------------
  245. RenderWorld& World::render_world()
  246. {
  247. return m_render_world;
  248. }
  249. //-----------------------------------------------------------------------------
  250. PhysicsWorld* World::physics_world()
  251. {
  252. return &m_physics_world;
  253. }
  254. } // namespace crown