Device.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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 "ConsoleServer.h"
  30. #include "DebugRenderer.h"
  31. #include "DiskFile.h"
  32. #include "DiskFilesystem.h"
  33. #include "JSONParser.h"
  34. #include "Keyboard.h"
  35. #include "Log.h"
  36. #include "LuaEnvironment.h"
  37. #include "Memory.h"
  38. #include "Mouse.h"
  39. #include "OS.h"
  40. #include "OsWindow.h"
  41. #include "Renderer.h"
  42. #include "ResourceManager.h"
  43. #include "StringSetting.h"
  44. #include "StringUtils.h"
  45. #include "TextReader.h"
  46. #include "Touch.h"
  47. #include "Types.h"
  48. #include "Bundle.h"
  49. #include "TempAllocator.h"
  50. #include "ResourcePackage.h"
  51. #if defined(LINUX) || defined(WINDOWS)
  52. #include "BundleCompiler.h"
  53. #endif
  54. #if defined(ANDROID)
  55. #include "ApkFilesystem.h"
  56. #endif
  57. #define MAX_SUBSYSTEMS_HEAP 16 * 1024 * 1024
  58. namespace crown
  59. {
  60. //-----------------------------------------------------------------------------
  61. Device::Device() :
  62. m_allocator(default_allocator(), MAX_SUBSYSTEMS_HEAP),
  63. m_preferred_window_width(1000),
  64. m_preferred_window_height(625),
  65. m_preferred_window_fullscreen(0),
  66. m_parent_window_handle(0),
  67. m_compile(0),
  68. m_continue(0),
  69. m_quit_after_init(0),
  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_debug_renderer(NULL),
  83. m_bundle_compiler(NULL),
  84. m_resource_manager(NULL),
  85. m_resource_bundle(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. bool Device::init(int argc, char** argv)
  99. {
  100. CE_ASSERT(!is_init(), "Engine already initialized");
  101. parse_command_line(argc, argv);
  102. check_preferred_settings();
  103. // Resource compilation only in debug or development mode and only on linux or windows builds
  104. #if (defined(LINUX) || defined(WINDOWS)) && (defined(CROWN_DEBUG) || defined(CROWN_DEVELOPMENT))
  105. if (m_compile == 1)
  106. {
  107. m_bundle_compiler = CE_NEW(m_allocator, BundleCompiler);
  108. if (!m_bundle_compiler->compile(m_bundle_dir, m_source_dir))
  109. {
  110. CE_DELETE(m_allocator, m_bundle_compiler);
  111. m_allocator.clear();
  112. Log::e("Exiting.");
  113. exit(EXIT_FAILURE);
  114. }
  115. if (!m_continue)
  116. {
  117. CE_DELETE(m_allocator, m_bundle_compiler);
  118. m_allocator.clear();
  119. exit(EXIT_SUCCESS);
  120. }
  121. }
  122. #endif
  123. init();
  124. return true;
  125. }
  126. //-----------------------------------------------------------------------------
  127. void Device::init()
  128. {
  129. // Initialize
  130. Log::i("Initializing Crown Engine %d.%d.%d...", CROWN_VERSION_MAJOR, CROWN_VERSION_MINOR, CROWN_VERSION_MICRO);
  131. // Default bundle filesystem
  132. #if defined (LINUX) || defined(WINDOWS)
  133. m_filesystem = CE_NEW(m_allocator, DiskFilesystem)(m_bundle_dir);
  134. #elif defined(ANDROID)
  135. m_filesystem = CE_NEW(m_allocator, ApkFilesystem)();
  136. #endif
  137. Log::d("Filesystem created.");
  138. // Read settings from crown.config
  139. read_engine_settings();
  140. m_resource_bundle = Bundle::create(m_allocator, *m_filesystem);
  141. // Create resource manager
  142. m_resource_manager = CE_NEW(m_allocator, ResourceManager)(*m_resource_bundle, 0);
  143. Log::d("Resource manager created.");
  144. Log::d("Resource seed: %d", m_resource_manager->seed());
  145. // Create input devices
  146. m_keyboard = CE_NEW(m_allocator, Keyboard);
  147. m_mouse = CE_NEW(m_allocator, Mouse);
  148. m_touch = CE_NEW(m_allocator, Touch);
  149. // Create renderer
  150. m_renderer = CE_NEW(m_allocator, Renderer)(m_allocator);
  151. m_renderer->init();
  152. Log::d("Renderer created.");
  153. // Create debug renderer
  154. m_debug_renderer = CE_NEW(m_allocator, DebugRenderer)(*m_renderer);
  155. Log::d("Debug renderer created.");
  156. m_lua_environment = CE_NEW(m_allocator, LuaEnvironment)();
  157. m_lua_environment->init();
  158. Log::d("Lua environment created.");
  159. Log::i("Crown Engine initialized.");
  160. Log::i("Initializing Game...");
  161. m_is_init = true;
  162. start();
  163. // Execute lua boot file
  164. if (m_lua_environment->load_and_execute(m_boot_file))
  165. {
  166. if (!m_lua_environment->call_global("init", 0))
  167. {
  168. pause();
  169. }
  170. }
  171. else
  172. {
  173. pause();
  174. }
  175. Log::d("Total allocated size: %llu", m_allocator.allocated_size());
  176. if (m_quit_after_init == 1)
  177. {
  178. stop();
  179. shutdown();
  180. }
  181. }
  182. //-----------------------------------------------------------------------------
  183. void Device::shutdown()
  184. {
  185. CE_ASSERT(is_init(), "Engine is not initialized");
  186. // Shutdowns the game
  187. m_lua_environment->call_global("shutdown", 0);
  188. Log::i("Releasing LuaEnvironment...");
  189. if (m_lua_environment)
  190. {
  191. m_lua_environment->shutdown();
  192. CE_DELETE(m_allocator, m_lua_environment);
  193. }
  194. Log::i("Releasing Input Devices...");
  195. CE_DELETE(m_allocator, m_touch);
  196. CE_DELETE(m_allocator, m_mouse);
  197. CE_DELETE(m_allocator, m_keyboard);
  198. Log::i("Releasing DebugRenderer...");
  199. if (m_debug_renderer)
  200. {
  201. CE_DELETE(m_allocator, m_debug_renderer);
  202. }
  203. Log::i("Releasing Renderer...");
  204. if (m_renderer)
  205. {
  206. m_renderer->shutdown();
  207. CE_DELETE(m_allocator, m_renderer);
  208. }
  209. Log::i("Releasing ResourceManager...");
  210. if (m_resource_manager)
  211. {
  212. CE_DELETE(m_allocator, m_resource_manager);
  213. }
  214. if (m_resource_bundle)
  215. {
  216. Bundle::destroy(m_allocator, m_resource_bundle);
  217. }
  218. Log::i("Releasing Filesystem...");
  219. if (m_filesystem)
  220. {
  221. CE_DELETE(m_allocator, m_filesystem);
  222. }
  223. #if (defined(LINUX) || defined(WINDOWS)) && (defined(CROWN_DEBUG) || defined(CROWN_DEVELOPMENT))
  224. Log::i("Releasing BundleCompiler...");
  225. if (m_bundle_compiler)
  226. {
  227. CE_DELETE(m_allocator, m_bundle_compiler);
  228. }
  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. Renderer* Device::renderer()
  260. {
  261. return m_renderer;
  262. }
  263. //-----------------------------------------------------------------------------
  264. DebugRenderer* Device::debug_renderer()
  265. {
  266. return m_debug_renderer;
  267. }
  268. //-----------------------------------------------------------------------------
  269. Keyboard* Device::keyboard()
  270. {
  271. return m_keyboard;
  272. }
  273. //-----------------------------------------------------------------------------
  274. Mouse* Device::mouse()
  275. {
  276. return m_mouse;
  277. }
  278. //-----------------------------------------------------------------------------
  279. Touch* Device::touch()
  280. {
  281. return m_touch;
  282. }
  283. //-----------------------------------------------------------------------------
  284. Accelerometer* Device::accelerometer()
  285. {
  286. return NULL;
  287. }
  288. //-----------------------------------------------------------------------------
  289. void Device::start()
  290. {
  291. CE_ASSERT(m_is_init, "Cannot start uninitialized engine.");
  292. m_is_running = true;
  293. m_last_time = os::milliseconds();
  294. }
  295. //-----------------------------------------------------------------------------
  296. void Device::stop()
  297. {
  298. CE_ASSERT(m_is_init, "Cannot stop uninitialized engine.");
  299. m_is_running = false;
  300. }
  301. //-----------------------------------------------------------------------------
  302. void Device::pause()
  303. {
  304. m_is_paused = true;
  305. }
  306. //-----------------------------------------------------------------------------
  307. void Device::unpause()
  308. {
  309. m_is_paused = false;
  310. }
  311. //-----------------------------------------------------------------------------
  312. bool Device::is_running() const
  313. {
  314. return m_is_running;
  315. }
  316. //-----------------------------------------------------------------------------
  317. uint64_t Device::frame_count() const
  318. {
  319. return m_frame_count;
  320. }
  321. //-----------------------------------------------------------------------------
  322. float Device::last_delta_time() const
  323. {
  324. return m_last_delta_time;
  325. }
  326. //-----------------------------------------------------------------------------
  327. double Device::time_since_start() const
  328. {
  329. return m_time_since_start;
  330. }
  331. //-----------------------------------------------------------------------------
  332. void Device::frame()
  333. {
  334. m_current_time = os::microseconds();
  335. m_last_delta_time = (m_current_time - m_last_time) / 1000000.0f;
  336. m_last_time = m_current_time;
  337. m_time_since_start += m_last_delta_time;
  338. if (!m_is_paused)
  339. {
  340. m_resource_manager->poll_resource_loader();
  341. m_renderer->set_layer_clear(0, CLEAR_COLOR | CLEAR_DEPTH, Color4::LIGHTBLUE, 1.0f);
  342. m_renderer->commit(0);
  343. if (!m_lua_environment->call_global("frame", 1, ARGUMENT_FLOAT, last_delta_time()))
  344. {
  345. pause();
  346. }
  347. m_renderer->frame();
  348. m_keyboard->update();
  349. m_mouse->update();
  350. }
  351. m_frame_count++;
  352. }
  353. //-----------------------------------------------------------------------------
  354. ResourcePackage* Device::create_resource_package(const char* name)
  355. {
  356. CE_ASSERT_NOT_NULL(name);
  357. ResourceId package_id = m_resource_manager->load("package", name);
  358. m_resource_manager->flush();
  359. PackageResource* package_res = (PackageResource*) m_resource_manager->data(package_id);
  360. ResourcePackage* package = CE_NEW(default_allocator(), ResourcePackage)(*m_resource_manager, package_id, package_res);
  361. return package;
  362. }
  363. //-----------------------------------------------------------------------------
  364. void Device::destroy_resource_package(ResourcePackage* package)
  365. {
  366. CE_ASSERT_NOT_NULL(package);
  367. m_resource_manager->unload(package->resource_id());
  368. CE_DELETE(default_allocator(), package);
  369. }
  370. //-----------------------------------------------------------------------------
  371. void Device::compile(const char* , const char* , const char* )
  372. {
  373. }
  374. //-----------------------------------------------------------------------------
  375. void Device::reload(ResourceId name)
  376. {
  377. (void)name;
  378. }
  379. //-----------------------------------------------------------------------------
  380. void Device::parse_command_line(int argc, char** argv)
  381. {
  382. static ArgsOption options[] =
  383. {
  384. { "help", AOA_NO_ARGUMENT, NULL, 'i' },
  385. { "source-dir", AOA_REQUIRED_ARGUMENT, NULL, 's' },
  386. { "bundle-dir", AOA_REQUIRED_ARGUMENT, NULL, 'b' },
  387. { "compile", AOA_NO_ARGUMENT, &m_compile, 1 },
  388. { "continue", AOA_NO_ARGUMENT, &m_continue, 1 },
  389. { "width", AOA_REQUIRED_ARGUMENT, NULL, 'w' },
  390. { "height", AOA_REQUIRED_ARGUMENT, NULL, 'h' },
  391. { "fullscreen", AOA_NO_ARGUMENT, &m_preferred_window_fullscreen, 1 },
  392. { "parent-window", AOA_REQUIRED_ARGUMENT, NULL, 'p' },
  393. { "quit-after-init", AOA_NO_ARGUMENT, &m_quit_after_init, 1 },
  394. { NULL, 0, NULL, 0 }
  395. };
  396. Args args(argc, argv, "", options);
  397. int32_t opt;
  398. while ((opt = args.getopt()) != -1)
  399. {
  400. switch (opt)
  401. {
  402. case 0:
  403. {
  404. break;
  405. }
  406. // Source directory
  407. case 's':
  408. {
  409. string::strncpy(m_source_dir, args.optarg(), MAX_PATH_LENGTH);
  410. break;
  411. }
  412. // Bundle directory
  413. case 'b':
  414. {
  415. string::strncpy(m_bundle_dir, args.optarg(), MAX_PATH_LENGTH);
  416. break;
  417. }
  418. // Window width
  419. case 'w':
  420. {
  421. m_preferred_window_width = atoi(args.optarg());
  422. break;
  423. }
  424. // Window height
  425. case 'h':
  426. {
  427. m_preferred_window_height = atoi(args.optarg());
  428. break;
  429. }
  430. // Parent window
  431. case 'p':
  432. {
  433. m_parent_window_handle = string::parse_uint(args.optarg());
  434. break;
  435. }
  436. case 'i':
  437. case '?':
  438. default:
  439. {
  440. print_help_message();
  441. exit(EXIT_FAILURE);
  442. }
  443. }
  444. }
  445. }
  446. //-----------------------------------------------------------------------------
  447. void Device::check_preferred_settings()
  448. {
  449. if (m_compile == 1)
  450. {
  451. if (string::strcmp(m_source_dir, "") == 0)
  452. {
  453. Log::e("You have to specify the source directory when running in compile mode.");
  454. exit(EXIT_FAILURE);
  455. }
  456. if (!os::is_absolute_path(m_source_dir))
  457. {
  458. Log::e("The source directory must be absolute.");
  459. exit(EXIT_FAILURE);
  460. }
  461. }
  462. if (!os::is_absolute_path(m_bundle_dir))
  463. {
  464. Log::e("The bundle directory must be absolute.");
  465. exit(EXIT_FAILURE);
  466. }
  467. if (m_preferred_window_width == 0 || m_preferred_window_height == 0)
  468. {
  469. Log::e("Window width and height must be greater than zero.");
  470. exit(EXIT_FAILURE);
  471. }
  472. }
  473. //-----------------------------------------------------------------------------
  474. void Device::read_engine_settings()
  475. {
  476. // Check crown.config existance
  477. CE_ASSERT(m_filesystem->is_file("crown.config"), "Unable to open crown.config");
  478. // Copy crown config in a buffer
  479. TempAllocator4096 allocator;
  480. File* config_file = m_filesystem->open("crown.config", FOM_READ);
  481. char* json_string = (char*)allocator.allocate(config_file->size());
  482. config_file->read(json_string, config_file->size());
  483. m_filesystem->close(config_file);
  484. // Parse crown.config
  485. JSONParser parser(json_string);
  486. JSONElement root = parser.root();
  487. // Boot
  488. if (root.has_key("boot"))
  489. {
  490. const char* boot = root.key("boot").string_value();
  491. const size_t boot_length = string::strlen(boot) + 1;
  492. string::strncpy(m_boot_file, boot, boot_length);
  493. }
  494. // Window width
  495. if (root.has_key("window_width"))
  496. {
  497. m_preferred_window_width = root.key("window_width").int_value();
  498. }
  499. // Window height
  500. if (root.has_key("window_height"))
  501. {
  502. m_preferred_window_height = root.key("window_height").int_value();
  503. }
  504. allocator.deallocate(json_string);
  505. Log::i("Configuration set");
  506. }
  507. //-----------------------------------------------------------------------------
  508. void Device::print_help_message()
  509. {
  510. os::printf(
  511. "Usage: crown [options]\n"
  512. "Options:\n\n"
  513. "All of the following options take precedence over\n"
  514. "environment variables and configuration files.\n\n"
  515. " --help Show this help.\n"
  516. " --bundle-dir <path> Use <path> as the source directory for compiled resources.\n"
  517. " --width <width> Set the <width> of the main window.\n"
  518. " --height <width> Set the <height> of the main window.\n"
  519. " --fullscreen Start in fullscreen.\n"
  520. " --parent-window <handle> Set the parent window <handle> of the main window.\n"
  521. " Used only by tools.\n"
  522. "\nAvailable only in debug and development builds:\n\n"
  523. " --source-dir <path> Use <path> as the source directory for resource compilation.\n"
  524. " --compile Run the engine as resource compiler.\n"
  525. " --continue Do a full compile of the resources and continue the execution.\n"
  526. " --quit-after-init Quit the engine immediately after the initialization.\n");
  527. }
  528. static Device* g_device;
  529. void set_device(Device* device)
  530. {
  531. g_device = device;
  532. }
  533. Device* device()
  534. {
  535. return g_device;
  536. }
  537. } // namespace crown