World.cpp 8.2 KB

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