Device.cpp 13 KB

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