World.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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))
  35. , m_units(default_allocator())
  36. , m_camera(default_allocator())
  37. , m_sounds(default_allocator())
  38. , m_unit_to_camera(default_allocator())
  39. , m_unit_to_sound_instance(default_allocator())
  40. {
  41. }
  42. //-----------------------------------------------------------------------------
  43. UnitId World::spawn_unit(const char* name, const Vector3& pos, const Quaternion& rot)
  44. {
  45. // Allocate memory for unit
  46. Unit* unit = CE_NEW(m_unit_pool, Unit)();
  47. // Fetch resource
  48. UnitResource* ur = (UnitResource*) device()->resource_manager()->lookup(UNIT_EXTENSION, name);
  49. // Create Id for the unit
  50. const UnitId unit_id = m_units.create(unit);
  51. unit->create(*this, ur, unit_id, pos, rot);
  52. return unit_id;
  53. }
  54. //-----------------------------------------------------------------------------
  55. void World::destroy_unit(UnitId unit)
  56. {
  57. CE_ASSERT(m_units.has(unit), "Unit does not exist");
  58. }
  59. //-----------------------------------------------------------------------------
  60. void World::destroy_unit(Unit* unit)
  61. {
  62. CE_ASSERT_NOT_NULL(unit);
  63. CE_DELETE(m_unit_pool, unit);
  64. }
  65. //-----------------------------------------------------------------------------
  66. void World::link_unit(UnitId child, UnitId parent, int32_t node)
  67. {
  68. CE_ASSERT(m_units.has(child), "Child unit does not exist");
  69. CE_ASSERT(m_units.has(parent), "Parent unit does not exist");
  70. Unit* child_unit = lookup_unit(child);
  71. Unit* parent_unit = lookup_unit(parent);
  72. parent_unit->link_node(child_unit->m_root_node, node);
  73. }
  74. //-----------------------------------------------------------------------------
  75. void World::unlink_unit(UnitId child)
  76. {
  77. CE_ASSERT(m_units.has(child), "Child unit does not exist");
  78. }
  79. //-----------------------------------------------------------------------------
  80. void World::link_camera(CameraId camera, UnitId unit, int32_t node)
  81. {
  82. UnitToCamera* utc = NULL;
  83. for (uint32_t i = 0; i < m_unit_to_camera.size(); i++)
  84. {
  85. if (utc->camera == camera && utc->unit == unit)
  86. {
  87. utc = &m_unit_to_camera[i];
  88. }
  89. }
  90. if (utc != NULL)
  91. {
  92. utc->node = node;
  93. }
  94. else
  95. {
  96. UnitToCamera new_utc;
  97. new_utc.camera = camera;
  98. new_utc.unit = unit;
  99. new_utc.node = node;
  100. m_unit_to_camera.push_back(new_utc);
  101. }
  102. }
  103. //-----------------------------------------------------------------------------
  104. void World::unlink_camera(CameraId camera)
  105. {
  106. (void)camera;
  107. }
  108. //-----------------------------------------------------------------------------
  109. Unit* World::lookup_unit(UnitId unit)
  110. {
  111. CE_ASSERT(m_units.has(unit), "Unit does not exist");
  112. return m_units.lookup(unit);
  113. }
  114. //-----------------------------------------------------------------------------
  115. Camera* World::lookup_camera(CameraId camera)
  116. {
  117. CE_ASSERT(m_camera.has(camera), "Camera does not exist");
  118. return &m_camera.lookup(camera);
  119. }
  120. //-----------------------------------------------------------------------------
  121. Mesh* World::lookup_mesh(MeshId mesh)
  122. {
  123. return m_render_world.lookup_mesh(mesh);
  124. }
  125. //-----------------------------------------------------------------------------
  126. Sprite* World::lookup_sprite(SpriteId sprite)
  127. {
  128. return m_render_world.lookup_sprite(sprite);
  129. }
  130. //-----------------------------------------------------------------------------
  131. void World::update(Camera& camera, float dt)
  132. {
  133. // Update all the units
  134. for (uint32_t uu = 0; uu < m_units.m_objects.size(); uu++)
  135. {
  136. Unit& unit = *m_units.m_objects[uu];
  137. SceneGraph& graph = unit.m_scene_graph;
  138. // Update unit's scene graph
  139. graph.update();
  140. }
  141. // Update camera poses
  142. for (uint32_t i = 0; i < m_unit_to_camera.size(); i++)
  143. {
  144. const UnitToCamera& utc = m_unit_to_camera[i];
  145. Camera& cam = m_camera.lookup(utc.camera);
  146. Unit* unit = m_units.lookup(utc.unit);
  147. cam.m_world_pose = unit->m_scene_graph.world_pose(utc.node);
  148. }
  149. // Updates sound poses
  150. for (uint32_t i = 0; i < m_unit_to_sound_instance.size(); i++)
  151. {
  152. const UnitToSoundInstance& uts = m_unit_to_sound_instance[i];
  153. SoundInstance& sound = m_sounds.lookup(uts.sound);
  154. Unit* unit = m_units.lookup(uts.unit);
  155. sound.world = unit->m_scene_graph.world_pose(uts.node);
  156. }
  157. // Update render world
  158. m_render_world.update(camera.m_world_pose, camera.m_projection, camera.m_view_x, camera.m_view_y,
  159. camera.m_view_width, camera.m_view_height, dt);
  160. // Update sounds
  161. List<SoundInstance>& sounds = m_sounds.m_objects;
  162. for (uint32_t i = 0; i < sounds.size(); i++)
  163. {
  164. SoundInstance& sound = sounds[i];
  165. device()->sound_renderer()->set_sound_loop(sound.sound, sound.loop);
  166. device()->sound_renderer()->set_sound_gain(sound.sound, sound.volume);
  167. device()->sound_renderer()->set_sound_max_distance(sound.sound, sound.range);
  168. device()->sound_renderer()->set_sound_position(sound.sound, sound.world.translation());
  169. if (!sound.playing)
  170. {
  171. device()->sound_renderer()->play_sound(sound.sound);
  172. sound.playing = true;
  173. }
  174. }
  175. }
  176. //-----------------------------------------------------------------------------
  177. RenderWorld& World::render_world()
  178. {
  179. return m_render_world;
  180. }
  181. //-----------------------------------------------------------------------------
  182. CameraId World::create_camera(int32_t node, const Vector3& pos, const Quaternion& rot)
  183. {
  184. Camera camera;
  185. camera.create(node, pos, rot);
  186. return m_camera.create(camera);
  187. }
  188. //-----------------------------------------------------------------------------
  189. void World::destroy_camera(CameraId camera)
  190. {
  191. m_camera.destroy(camera);
  192. }
  193. //-----------------------------------------------------------------------------
  194. MeshId World::create_mesh(ResourceId id, int32_t node, const Vector3& pos, const Quaternion& rot)
  195. {
  196. return m_render_world.create_mesh(id, node, pos, rot);
  197. }
  198. //-----------------------------------------------------------------------------
  199. void World::destroy_mesh(MeshId id)
  200. {
  201. m_render_world.destroy_mesh(id);
  202. }
  203. //-----------------------------------------------------------------------------
  204. SpriteId World::create_sprite(const char* name, int32_t node, const Vector3& pos, const Quaternion& rot)
  205. {
  206. return m_render_world.create_sprite(name, node, pos, rot);
  207. }
  208. //-----------------------------------------------------------------------------
  209. void World::destroy_sprite(SpriteId id)
  210. {
  211. m_render_world.destroy_sprite(id);
  212. }
  213. //-----------------------------------------------------------------------------
  214. SoundInstanceId World::play_sound(const char* name, const bool loop, const float volume, const Vector3& pos, const float range)
  215. {
  216. SoundResource* sound = (SoundResource*)device()->resource_manager()->lookup(SOUND_EXTENSION, name);
  217. SoundInstance s;
  218. s.sound = sound->m_id;
  219. s.world = Matrix4x4(Quaternion::IDENTITY, pos);
  220. s.volume = volume;
  221. s.range = range;
  222. s.loop = loop;
  223. s.playing = false;
  224. SoundInstanceId id = m_sounds.create(s);
  225. return id;
  226. }
  227. //-----------------------------------------------------------------------------
  228. void World::pause_sound(SoundInstanceId id)
  229. {
  230. CE_ASSERT(m_sounds.has(id), "SoundInstance does not exists");
  231. const SoundInstance& sound = m_sounds.lookup(id);
  232. device()->sound_renderer()->pause_sound(sound.sound);
  233. }
  234. //-----------------------------------------------------------------------------
  235. void World::link_sound(SoundInstanceId id, Unit* unit, int32_t node)
  236. {
  237. //CE_ASSERT(m_units.has(unit), "Unit does not exists");
  238. CE_ASSERT(m_sounds.has(id), "SoundInstance does not exists");
  239. UnitToSoundInstance uts;
  240. uts.sound = id;
  241. uts.unit = unit->m_id;
  242. uts.node = node;
  243. m_unit_to_sound_instance.push_back(uts);
  244. }
  245. //-----------------------------------------------------------------------------
  246. void World::set_listener(const Vector3& pos, const Vector3& vel, const Vector3& or_up, const Vector3& or_at)
  247. {
  248. device()->sound_renderer()->set_listener(pos, vel, or_up, or_at);
  249. }
  250. //-----------------------------------------------------------------------------
  251. void World::set_sound_position(SoundInstanceId id, const Vector3& pos)
  252. {
  253. CE_ASSERT(m_sounds.has(id), "SoundInstance does not exists");
  254. SoundInstance& sound = m_sounds.lookup(id);
  255. sound.world = Matrix4x4(Quaternion::IDENTITY, pos);
  256. }
  257. //-----------------------------------------------------------------------------
  258. void World::set_sound_range(SoundInstanceId id, const float range)
  259. {
  260. CE_ASSERT(m_sounds.has(id), "SoundInstance does not exists");
  261. SoundInstance& sound = m_sounds.lookup(id);
  262. sound.range = range;
  263. }
  264. //-----------------------------------------------------------------------------
  265. void World::set_sound_volume(SoundInstanceId id, const float vol)
  266. {
  267. CE_ASSERT(m_sounds.has(id), "SoundInstance does not exists");
  268. SoundInstance& sound = m_sounds.lookup(id);
  269. sound.volume = vol;
  270. }
  271. } // namespace crown