World.cpp 11 KB

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