World.cpp 11 KB

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