Device.cpp 13 KB

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