World.cpp 11 KB

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