Device.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. #include <cstdlib>
  25. #include "Config.h"
  26. #include "Device.h"
  27. #include "Accelerometer.h"
  28. #include "Args.h"
  29. #include "DiskFile.h"
  30. #include "DiskFilesystem.h"
  31. #include "JSONParser.h"
  32. #include "Keyboard.h"
  33. #include "Log.h"
  34. #include "LuaEnvironment.h"
  35. #include "Memory.h"
  36. #include "Mouse.h"
  37. #include "OS.h"
  38. #include "OsWindow.h"
  39. #include "Renderer.h"
  40. #include "ResourceManager.h"
  41. #include "StringSetting.h"
  42. #include "StringUtils.h"
  43. #include "TextReader.h"
  44. #include "Touch.h"
  45. #include "Types.h"
  46. #include "Bundle.h"
  47. #include "TempAllocator.h"
  48. #include "ResourcePackage.h"
  49. #include "ConsoleServer.h"
  50. #include "World.h"
  51. #include "LuaStack.h"
  52. #include "WorldManager.h"
  53. #include "NetworkFilesystem.h"
  54. #if defined(LINUX) || defined(WINDOWS)
  55. #include "BundleCompiler.h"
  56. #endif
  57. #if defined(ANDROID)
  58. #include "ApkFilesystem.h"
  59. #endif
  60. #define MAX_SUBSYSTEMS_HEAP 8 * 1024 * 1024
  61. namespace crown
  62. {
  63. //-----------------------------------------------------------------------------
  64. Device::Device()
  65. : m_allocator(default_allocator(), MAX_SUBSYSTEMS_HEAP)
  66. , m_argc(0)
  67. , m_argv(NULL)
  68. , m_fileserver(0)
  69. , m_console_port(10001)
  70. , m_is_init(false)
  71. , m_is_running(false)
  72. , m_is_paused(false)
  73. , m_is_really_paused(false)
  74. , m_frame_count(0)
  75. , m_last_time(0)
  76. , m_current_time(0)
  77. , m_last_delta_time(0.0f)
  78. , m_time_since_start(0.0)
  79. , m_filesystem(NULL)
  80. , m_lua_environment(NULL)
  81. , m_renderer(NULL)
  82. , m_bundle_compiler(NULL)
  83. , m_console(NULL)
  84. , m_resource_manager(NULL)
  85. , m_resource_bundle(NULL)
  86. , m_physx(NULL)
  87. , m_world_manager(NULL)
  88. , m_renderer_init_request(false)
  89. {
  90. // Bundle dir is current dir by default.
  91. string::strncpy(m_bundle_dir, os::get_cwd(), MAX_PATH_LENGTH);
  92. string::strncpy(m_source_dir, "", MAX_PATH_LENGTH);
  93. string::strncpy(m_boot_file, "lua/game", MAX_PATH_LENGTH);
  94. }
  95. //-----------------------------------------------------------------------------
  96. Device::~Device()
  97. {
  98. }
  99. //-----------------------------------------------------------------------------
  100. void Device::init()
  101. {
  102. // Initialize
  103. Log::i("Initializing Crown Engine %d.%d.%d...", CROWN_VERSION_MAJOR, CROWN_VERSION_MINOR, CROWN_VERSION_MICRO);
  104. // RPC only in debug or development builds
  105. #if defined(CROWN_DEBUG) || defined(CROWN_DEVELOPMENT)
  106. m_console = CE_NEW(m_allocator, ConsoleServer)(m_console_port);
  107. m_console->init(false);
  108. #endif
  109. Log::d("Creating filesystem...");
  110. // Default bundle filesystem
  111. #if defined (LINUX) || defined(WINDOWS)
  112. if (m_fileserver == 1)
  113. {
  114. m_filesystem = CE_NEW(m_allocator, NetworkFilesystem)(NetAddress(127, 0, 0, 1), 10001);
  115. }
  116. else
  117. {
  118. m_filesystem = CE_NEW(m_allocator, DiskFilesystem)(m_bundle_dir);
  119. }
  120. #elif defined(ANDROID)
  121. if (m_fileserver == 1)
  122. {
  123. m_filesystem = CE_NEW(m_allocator, NetworkFilesystem)(NetAddress(192, 168, 0, 7), 10001);
  124. }
  125. else
  126. {
  127. m_filesystem = CE_NEW(m_allocator, ApkFilesystem)();
  128. }
  129. #endif
  130. m_resource_bundle = Bundle::create(m_allocator, *m_filesystem);
  131. // Create resource manager
  132. Log::d("Creating resource manager...");
  133. m_resource_manager = CE_NEW(m_allocator, ResourceManager)(*m_resource_bundle, 0);
  134. Log::d("Resource seed: %d", m_resource_manager->seed());
  135. // Create world manager
  136. Log::d("Creating world manager...");
  137. m_world_manager = CE_NEW(m_allocator, WorldManager)();
  138. // Create window
  139. Log::d("Creating main window...");
  140. m_window = CE_NEW(m_allocator, OsWindow);
  141. // Create input devices
  142. m_keyboard = CE_NEW(m_allocator, Keyboard);
  143. m_mouse = CE_NEW(m_allocator, Mouse);
  144. m_touch = CE_NEW(m_allocator, Touch);
  145. // Create renderer
  146. Log::d("Creating renderer...");
  147. m_renderer = CE_NEW(m_allocator, Renderer)(m_allocator);
  148. m_renderer->init();
  149. Log::d("Creating lua environment...");
  150. m_lua_environment = CE_NEW(m_allocator, LuaEnvironment)();
  151. m_lua_environment->init();
  152. Log::d("Creating physics...");
  153. m_physx = CE_NEW(m_allocator, Physics)();
  154. Log::d("Creating audio...");
  155. audio_system::init();
  156. Log::d("Crown Engine initialized.");
  157. Log::d("Initializing Game...");
  158. m_is_init = true;
  159. start();
  160. // Execute lua boot file
  161. if (m_lua_environment->load_and_execute(m_boot_file))
  162. {
  163. if (!m_lua_environment->call_global("init", 0))
  164. {
  165. pause();
  166. }
  167. }
  168. else
  169. {
  170. pause();
  171. }
  172. Log::d("Total allocated size: %llu", m_allocator.allocated_size());
  173. }
  174. //-----------------------------------------------------------------------------
  175. void Device::shutdown()
  176. {
  177. CE_ASSERT(is_init(), "Engine is not initialized");
  178. // Shutdowns the game
  179. m_lua_environment->call_global("shutdown", 0);
  180. audio_system::shutdown();
  181. Log::d("Releasing Physics...");
  182. if (m_physx)
  183. {
  184. CE_DELETE(m_allocator, m_physx);
  185. }
  186. Log::d("Releasing LuaEnvironment...");
  187. if (m_lua_environment)
  188. {
  189. m_lua_environment->shutdown();
  190. CE_DELETE(m_allocator, m_lua_environment);
  191. }
  192. Log::d("Releasing Input Devices...");
  193. CE_DELETE(m_allocator, m_touch);
  194. CE_DELETE(m_allocator, m_mouse);
  195. CE_DELETE(m_allocator, m_keyboard);
  196. Log::d("Releasing Renderer...");
  197. if (m_renderer)
  198. {
  199. m_renderer->shutdown();
  200. CE_DELETE(m_allocator, m_renderer);
  201. }
  202. Log::d("Releasing WorldManager...");
  203. CE_DELETE(m_allocator, m_world_manager);
  204. Log::d("Releasing ResourceManager...");
  205. if (m_resource_manager)
  206. {
  207. CE_DELETE(m_allocator, m_resource_manager);
  208. }
  209. if (m_resource_bundle)
  210. {
  211. Bundle::destroy(m_allocator, m_resource_bundle);
  212. }
  213. Log::d("Releasing Filesystem...");
  214. if (m_filesystem)
  215. {
  216. CE_DELETE(m_allocator, m_filesystem);
  217. }
  218. #if defined(CROWN_DEBUG) || defined(CROWN_DEVELOPMENT)
  219. m_console->shutdown();
  220. CE_DELETE(m_allocator, m_console);
  221. m_console = NULL;
  222. #endif
  223. m_allocator.clear();
  224. m_is_init = false;
  225. }
  226. //-----------------------------------------------------------------------------
  227. bool Device::is_init() const
  228. {
  229. return m_is_init;
  230. }
  231. //-----------------------------------------------------------------------------
  232. bool Device::is_paused() const
  233. {
  234. return m_is_paused;
  235. }
  236. //-----------------------------------------------------------------------------
  237. Filesystem* Device::filesystem()
  238. {
  239. return m_filesystem;
  240. }
  241. //-----------------------------------------------------------------------------
  242. ResourceManager* Device::resource_manager()
  243. {
  244. return m_resource_manager;
  245. }
  246. //-----------------------------------------------------------------------------
  247. LuaEnvironment* Device::lua_environment()
  248. {
  249. return m_lua_environment;
  250. }
  251. //-----------------------------------------------------------------------------
  252. OsWindow* Device::window()
  253. {
  254. return m_window;
  255. }
  256. //-----------------------------------------------------------------------------
  257. Renderer* Device::renderer()
  258. {
  259. return m_renderer;
  260. }
  261. //-----------------------------------------------------------------------------
  262. Keyboard* Device::keyboard()
  263. {
  264. return m_keyboard;
  265. }
  266. //-----------------------------------------------------------------------------
  267. Mouse* Device::mouse()
  268. {
  269. return m_mouse;
  270. }
  271. //-----------------------------------------------------------------------------
  272. Touch* Device::touch()
  273. {
  274. return m_touch;
  275. }
  276. //-----------------------------------------------------------------------------
  277. Accelerometer* Device::accelerometer()
  278. {
  279. return NULL;
  280. }
  281. //-----------------------------------------------------------------------------
  282. void Device::start()
  283. {
  284. CE_ASSERT(m_is_init, "Cannot start uninitialized engine.");
  285. m_is_running = true;
  286. m_last_time = os::milliseconds();
  287. }
  288. //-----------------------------------------------------------------------------
  289. void Device::stop()
  290. {
  291. CE_ASSERT(m_is_init, "Cannot stop uninitialized engine.");
  292. m_is_running = false;
  293. }
  294. //-----------------------------------------------------------------------------
  295. void Device::pause()
  296. {
  297. m_is_paused = true;
  298. }
  299. //-----------------------------------------------------------------------------
  300. void Device::unpause()
  301. {
  302. m_is_paused = false;
  303. }
  304. //-----------------------------------------------------------------------------
  305. bool Device::is_running() const
  306. {
  307. return m_is_running;
  308. }
  309. //-----------------------------------------------------------------------------
  310. uint64_t Device::frame_count() const
  311. {
  312. return m_frame_count;
  313. }
  314. //-----------------------------------------------------------------------------
  315. float Device::last_delta_time() const
  316. {
  317. return m_last_delta_time;
  318. }
  319. //-----------------------------------------------------------------------------
  320. double Device::time_since_start() const
  321. {
  322. return m_time_since_start;
  323. }
  324. //-----------------------------------------------------------------------------
  325. void Device::frame()
  326. {
  327. m_current_time = os::microseconds();
  328. m_last_delta_time = (m_current_time - m_last_time) / 1000000.0f;
  329. m_last_time = m_current_time;
  330. m_time_since_start += m_last_delta_time;
  331. m_console->update();
  332. if (!m_is_paused)
  333. {
  334. m_resource_manager->poll_resource_loader();
  335. if (!m_lua_environment->call_global("frame", 1, ARGUMENT_FLOAT, last_delta_time()))
  336. {
  337. pause();
  338. }
  339. m_renderer->frame();
  340. }
  341. clear_lua_temporaries();
  342. m_frame_count++;
  343. }
  344. //-----------------------------------------------------------------------------
  345. void Device::update_world(World* world, float dt)
  346. {
  347. world->update(dt);
  348. }
  349. //-----------------------------------------------------------------------------
  350. void Device::render_world(World* world, Camera* camera)
  351. {
  352. world->render(camera);
  353. }
  354. //-----------------------------------------------------------------------------
  355. WorldId Device::create_world()
  356. {
  357. return m_world_manager->create_world();
  358. }
  359. //-----------------------------------------------------------------------------
  360. void Device::destroy_world(WorldId world)
  361. {
  362. m_world_manager->destroy_world(world);
  363. }
  364. //-----------------------------------------------------------------------------
  365. ResourcePackage* Device::create_resource_package(const char* name)
  366. {
  367. CE_ASSERT_NOT_NULL(name);
  368. ResourceId package_id = m_resource_manager->load("package", name);
  369. m_resource_manager->flush();
  370. PackageResource* package_res = (PackageResource*) m_resource_manager->data(package_id);
  371. ResourcePackage* package = CE_NEW(default_allocator(), ResourcePackage)(*m_resource_manager, package_id, package_res);
  372. return package;
  373. }
  374. //-----------------------------------------------------------------------------
  375. void Device::destroy_resource_package(ResourcePackage* package)
  376. {
  377. CE_ASSERT_NOT_NULL(package);
  378. m_resource_manager->unload(package->resource_id());
  379. CE_DELETE(default_allocator(), package);
  380. }
  381. //-----------------------------------------------------------------------------
  382. void Device::compile(const char* , const char* , const char* )
  383. {
  384. }
  385. //-----------------------------------------------------------------------------
  386. void Device::reload(const char* type, const char* name)
  387. {
  388. #if defined(LINUX) || defined(WINDOWS)
  389. TempAllocator4096 temp;
  390. DynamicString filename(temp);
  391. filename += name;
  392. filename += '.';
  393. filename += type;
  394. if (!m_bundle_compiler->compile(m_bundle_dir, m_source_dir, filename.c_str()))
  395. {
  396. Log::d("Compilation failed.");
  397. return;
  398. }
  399. ResourceId old_res_id = m_resource_manager->resource_id(type, name);
  400. const void* old_res = m_resource_manager->data(old_res_id);
  401. m_resource_manager->unload(old_res_id, true);
  402. ResourceId res_id = m_resource_manager->load(type, name);
  403. m_resource_manager->flush();
  404. const void* new_res = m_resource_manager->data(res_id);
  405. uint32_t type_hash = hash::murmur2_32(type, string::strlen(type), 0);
  406. switch (type_hash)
  407. {
  408. case UNIT_TYPE:
  409. {
  410. Log::d("Reloading unit: %s", name);
  411. /// Reload unit in all worlds
  412. for (uint32_t i = 0; i < m_world_manager->worlds().size(); i++)
  413. {
  414. m_world_manager->worlds()[i]->reload_units((UnitResource*) old_res, (UnitResource*) new_res);
  415. }
  416. break;
  417. }
  418. case SOUND_TYPE:
  419. {
  420. Log::d("Reloading sound: %s", name);
  421. for (uint32_t i = 0; i < m_world_manager->worlds().size(); i++)
  422. {
  423. m_world_manager->worlds()[i]->sound_world()->reload_sounds((SoundResource*) old_res, (SoundResource*) new_res);
  424. }
  425. break;
  426. }
  427. default:
  428. {
  429. CE_ASSERT(false, "Oops, unknown resource type: %s", type);
  430. break;
  431. }
  432. }
  433. #endif
  434. }
  435. static Device* g_device;
  436. void set_device(Device* device)
  437. {
  438. g_device = device;
  439. }
  440. Device* device()
  441. {
  442. return g_device;
  443. }
  444. } // namespace crown