World.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 "DebugLine.h"
  29. #include "Actor.h"
  30. #include "LuaEnvironment.h"
  31. namespace crown
  32. {
  33. //-----------------------------------------------------------------------------
  34. World::World()
  35. : m_unit_pool(default_allocator(), CE_MAX_UNITS, sizeof(Unit), CE_ALIGNOF(Unit))
  36. , m_camera_pool(default_allocator(), CE_MAX_CAMERAS, sizeof(Camera), CE_ALIGNOF(Camera))
  37. , m_physics_world(*this)
  38. , m_events(default_allocator())
  39. {
  40. m_id.id = INVALID_ID;
  41. m_sound_world = SoundWorld::create(default_allocator());
  42. }
  43. //-----------------------------------------------------------------------------
  44. World::~World()
  45. {
  46. // Destroy all units
  47. for (uint32_t i = 0; i < m_units.size(); i++)
  48. {
  49. CE_DELETE(m_unit_pool, m_units[i]);
  50. }
  51. SoundWorld::destroy(default_allocator(), m_sound_world);
  52. }
  53. //-----------------------------------------------------------------------------
  54. WorldId World::id() const
  55. {
  56. return m_id;
  57. }
  58. //-----------------------------------------------------------------------------
  59. void World::set_id(WorldId id)
  60. {
  61. m_id = id;
  62. }
  63. //-----------------------------------------------------------------------------
  64. UnitId World::spawn_unit(const char* name, const Vector3& pos, const Quaternion& rot)
  65. {
  66. UnitResource* ur = (UnitResource*) device()->resource_manager()->lookup(UNIT_EXTENSION, name);
  67. ResourceId id = device()->resource_manager()->resource_id(UNIT_EXTENSION, name);
  68. return spawn_unit(id, ur, pos, rot);
  69. }
  70. //-----------------------------------------------------------------------------
  71. UnitId World::spawn_unit(const ResourceId id, UnitResource* ur, const Vector3& pos, const Quaternion& rot)
  72. {
  73. Unit* u = (Unit*) m_unit_pool.allocate(sizeof(Unit), CE_ALIGNOF(Unit));
  74. const UnitId unit_id = m_units.create(u);
  75. new (u) Unit(*this, unit_id, id, ur, Matrix4x4(rot, pos));
  76. // SpawnUnitEvent ev;
  77. // ev.unit = unit_id;
  78. // event_stream::write(m_events, EventType::SPAWN, ev);
  79. return unit_id;
  80. }
  81. //-----------------------------------------------------------------------------
  82. void World::destroy_unit(UnitId id)
  83. {
  84. CE_ASSERT(m_units.has(id), "Unit does not exist");
  85. CE_DELETE(m_unit_pool, m_units.lookup(id));
  86. m_units.destroy(id);
  87. // DestroyUnitEvent ev;
  88. // ev.unit = id;
  89. // event_stream::write(m_events, EventType::DESTROY, ev);
  90. }
  91. //-----------------------------------------------------------------------------
  92. void World::reload_units(UnitResource* old_ur, UnitResource* new_ur)
  93. {
  94. for (uint32_t i = 0; i < m_units.size(); i++)
  95. {
  96. if (m_units[i]->resource() == old_ur)
  97. {
  98. m_units[i]->reload(new_ur);
  99. }
  100. }
  101. }
  102. //-----------------------------------------------------------------------------
  103. uint32_t World::num_units() const
  104. {
  105. return m_units.size();
  106. }
  107. //-----------------------------------------------------------------------------
  108. void World::link_unit(UnitId child, UnitId parent, int32_t node)
  109. {
  110. CE_ASSERT(m_units.has(child), "Child unit does not exist");
  111. CE_ASSERT(m_units.has(parent), "Parent unit does not exist");
  112. Unit* parent_unit = lookup_unit(parent);
  113. parent_unit->link_node(0, node);
  114. }
  115. //-----------------------------------------------------------------------------
  116. void World::unlink_unit(UnitId child)
  117. {
  118. CE_ASSERT(m_units.has(child), "Child unit does not exist");
  119. }
  120. //-----------------------------------------------------------------------------
  121. Unit* World::lookup_unit(UnitId unit)
  122. {
  123. CE_ASSERT(m_units.has(unit), "Unit does not exist");
  124. return m_units.lookup(unit);
  125. }
  126. //-----------------------------------------------------------------------------
  127. Camera* World::lookup_camera(CameraId camera)
  128. {
  129. CE_ASSERT(m_cameras.has(camera), "Camera does not exist");
  130. return m_cameras.lookup(camera);
  131. }
  132. //-----------------------------------------------------------------------------
  133. void World::update(float dt)
  134. {
  135. m_physics_world.update(dt);
  136. m_scenegraph_manager.update();
  137. m_sound_world->update();
  138. process_physics_events();
  139. }
  140. //-----------------------------------------------------------------------------
  141. void World::render(Camera* camera)
  142. {
  143. m_render_world.update(camera->world_pose(), camera->m_projection, camera->m_view_x, camera->m_view_y,
  144. camera->m_view_width, camera->m_view_height, device()->last_delta_time());
  145. }
  146. //-----------------------------------------------------------------------------
  147. CameraId World::create_camera(SceneGraph& sg, int32_t node)
  148. {
  149. // Allocate memory for camera
  150. Camera* camera = CE_NEW(m_camera_pool, Camera)(sg, node);
  151. // Create Id for the camera
  152. const CameraId camera_id = m_cameras.create(camera);
  153. return camera_id;
  154. }
  155. //-----------------------------------------------------------------------------
  156. void World::destroy_camera(CameraId id)
  157. {
  158. CE_ASSERT(m_cameras.has(id), "Camera does not exist");
  159. CE_DELETE(m_camera_pool, m_cameras.lookup(id));
  160. m_cameras.destroy(id);
  161. }
  162. //-----------------------------------------------------------------------------
  163. SoundInstanceId World::play_sound(const char* name, const bool loop, const float volume, const Vector3& pos, const float range)
  164. {
  165. return m_sound_world->play(name, loop, volume, pos);
  166. }
  167. //-----------------------------------------------------------------------------
  168. void World::stop_sound(SoundInstanceId id)
  169. {
  170. m_sound_world->stop(id);
  171. }
  172. //-----------------------------------------------------------------------------
  173. void World::link_sound(SoundInstanceId id, Unit* unit, int32_t node)
  174. {
  175. }
  176. //-----------------------------------------------------------------------------
  177. void World::set_listener_pose(const Matrix4x4& pose)
  178. {
  179. m_sound_world->set_listener_pose(pose);
  180. }
  181. //-----------------------------------------------------------------------------
  182. void World::set_sound_position(SoundInstanceId id, const Vector3& pos)
  183. {
  184. m_sound_world->set_sound_positions(1, &id, &pos);
  185. }
  186. //-----------------------------------------------------------------------------
  187. void World::set_sound_range(SoundInstanceId id, float range)
  188. {
  189. m_sound_world->set_sound_ranges(1, &id, &range);
  190. }
  191. //-----------------------------------------------------------------------------
  192. void World::set_sound_volume(SoundInstanceId id, float vol)
  193. {
  194. m_sound_world->set_sound_volumes(1, &id, &vol);
  195. }
  196. //-----------------------------------------------------------------------------
  197. GuiId World::create_window_gui(const char* name)
  198. {
  199. GuiResource* gr = (GuiResource*)device()->resource_manager()->lookup(GUI_EXTENSION, name);
  200. return m_render_world.create_gui(gr);
  201. }
  202. //-----------------------------------------------------------------------------
  203. GuiId World::create_world_gui(const Matrix4x4 pose, const uint32_t width, const uint32_t height)
  204. {
  205. // Must be implemented
  206. return GuiId();
  207. }
  208. //-----------------------------------------------------------------------------
  209. void World::destroy_gui(GuiId id)
  210. {
  211. m_render_world.destroy_gui(id);
  212. }
  213. //-----------------------------------------------------------------------------
  214. Gui* World::lookup_gui(GuiId id)
  215. {
  216. return m_render_world.lookup_gui(id);
  217. }
  218. //-----------------------------------------------------------------------------
  219. DebugLine* World::create_debug_line(bool depth_test)
  220. {
  221. return CE_NEW(default_allocator(), DebugLine)(depth_test);
  222. }
  223. //-----------------------------------------------------------------------------
  224. void World::destroy_debug_line(DebugLine* line)
  225. {
  226. CE_DELETE(default_allocator(), line);
  227. }
  228. //-----------------------------------------------------------------------------
  229. SceneGraphManager* World::scene_graph_manager()
  230. {
  231. return &m_scenegraph_manager;
  232. }
  233. //-----------------------------------------------------------------------------
  234. RenderWorld* World::render_world()
  235. {
  236. return &m_render_world;
  237. }
  238. //-----------------------------------------------------------------------------
  239. PhysicsWorld* World::physics_world()
  240. {
  241. return &m_physics_world;
  242. }
  243. //-----------------------------------------------------------------------------
  244. SoundWorld* World::sound_world()
  245. {
  246. return m_sound_world;
  247. }
  248. //-----------------------------------------------------------------------------
  249. void World::process_physics_events()
  250. {
  251. EventStream& events = m_physics_world.events();
  252. // Read all events
  253. const char* ee = array::begin(events);
  254. while (ee != array::end(events))
  255. {
  256. event_stream::Header h = *(event_stream::Header*) ee;
  257. // Log::d("=== PHYSICS EVENT ===");
  258. // Log::d("type = %d", h.type);
  259. // Log::d("size = %d", h.size);
  260. const char* event = ee + sizeof(event_stream::Header);
  261. switch (h.type)
  262. {
  263. case physics_world::EventType::COLLISION:
  264. {
  265. physics_world::CollisionEvent coll_ev = *(physics_world::CollisionEvent*) event;
  266. // Log::d("type = %s", coll_ev.type == physics_world::CollisionEvent::BEGIN_TOUCH ? "begin" : "end");
  267. // Log::d("actor_0 = (%p)", coll_ev.actors[0]);
  268. // Log::d("actor_1 = (%p)", coll_ev.actors[1]);
  269. // Log::d("unit_0 = (%p)", coll_ev.actors[0]->unit());
  270. // Log::d("unit_1 = (%p)", coll_ev.actors[1]->unit());
  271. // Log::d("where = (%f %f %f)", coll_ev.where.x, coll_ev.where.y, coll_ev.where.z);
  272. // Log::d("normal = (%f %f %f)", coll_ev.normal.x, coll_ev.normal.y, coll_ev.normal.z);
  273. device()->lua_environment()->call_physics_callback(
  274. coll_ev.actors[0],
  275. coll_ev.actors[1],
  276. coll_ev.actors[0]->unit(),
  277. coll_ev.actors[1]->unit(),
  278. coll_ev.where,
  279. coll_ev.normal,
  280. (coll_ev.type == physics_world::CollisionEvent::BEGIN_TOUCH) ? "begin" : "end");
  281. break;
  282. }
  283. case physics_world::EventType::TRIGGER:
  284. {
  285. physics_world::TriggerEvent trigg_ev = *(physics_world::TriggerEvent*) event;
  286. // Log::d("type = %s", trigg_ev.type == physics_world::TriggerEvent::BEGIN_TOUCH ? "begin" : "end");
  287. // Log::d("trigger = (%p)", trigg_ev.trigger);
  288. // Log::d("other = (%p)", trigg_ev.other);
  289. break;
  290. }
  291. default:
  292. {
  293. CE_FATAL("Unknown Physics event");
  294. break;
  295. }
  296. }
  297. // Log::d("=====================");
  298. // Next event
  299. ee += sizeof(event_stream::Header) + h.size;
  300. }
  301. array::clear(events);
  302. }
  303. } // namespace crown