Device.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  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 <unistd.h>
  26. #include "Config.h"
  27. #include "Device.h"
  28. #include "Accelerometer.h"
  29. #include "Args.h"
  30. #include "ConsoleServer.h"
  31. #include "DebugRenderer.h"
  32. #include "DiskFile.h"
  33. #include "DiskFilesystem.h"
  34. #include "InputManager.h"
  35. #include "JSONParser.h"
  36. #include "Keyboard.h"
  37. #include "Log.h"
  38. #include "LuaEnvironment.h"
  39. #include "Memory.h"
  40. #include "Mouse.h"
  41. #include "OS.h"
  42. #include "OsWindow.h"
  43. #include "Renderer.h"
  44. #include "ResourceManager.h"
  45. #include "StringSetting.h"
  46. #include "StringUtils.h"
  47. #include "TextReader.h"
  48. #include "Touch.h"
  49. #include "Types.h"
  50. #include "Bundle.h"
  51. #include "TempAllocator.h"
  52. #include "ResourcePackage.h"
  53. #include "EventBuffer.h"
  54. #include "SoundRenderer.h"
  55. #include "OggDecoder.h"
  56. #include "SoundResource.h"
  57. #include "SoundSample.h"
  58. #if defined(LINUX) || defined(WINDOWS)
  59. #include "BundleCompiler.h"
  60. #endif
  61. #if defined(ANDROID)
  62. #include "ApkFilesystem.h"
  63. #endif
  64. #define MAX_SUBSYSTEMS_HEAP 16 * 1024 * 1024
  65. namespace crown
  66. {
  67. //-----------------------------------------------------------------------------
  68. Device::Device() :
  69. m_allocator(default_allocator(), MAX_SUBSYSTEMS_HEAP),
  70. m_preferred_window_width(1000),
  71. m_preferred_window_height(625),
  72. m_preferred_window_fullscreen(0),
  73. m_parent_window_handle(0),
  74. m_compile(0),
  75. m_continue(0),
  76. m_quit_after_init(0),
  77. m_is_init(false),
  78. m_is_running(false),
  79. m_is_paused(false),
  80. m_is_really_paused(false),
  81. m_frame_count(0),
  82. m_last_time(0),
  83. m_current_time(0),
  84. m_last_delta_time(0.0f),
  85. m_time_since_start(0.0),
  86. m_filesystem(NULL),
  87. m_input_manager(NULL),
  88. m_lua_environment(NULL),
  89. m_renderer(NULL),
  90. m_debug_renderer(NULL),
  91. m_sound_renderer(NULL),
  92. m_bundle_compiler(NULL),
  93. m_resource_manager(NULL),
  94. m_resource_bundle(NULL),
  95. m_console_server(NULL)
  96. {
  97. // Bundle dir is current dir by default.
  98. string::strncpy(m_bundle_dir, os::get_cwd(), MAX_PATH_LENGTH);
  99. string::strncpy(m_source_dir, "", MAX_PATH_LENGTH);
  100. string::strncpy(m_boot_file, "lua/game", MAX_PATH_LENGTH);
  101. }
  102. //-----------------------------------------------------------------------------
  103. Device::~Device()
  104. {
  105. }
  106. //-----------------------------------------------------------------------------
  107. bool Device::init(int argc, char** argv)
  108. {
  109. CE_ASSERT(!is_init(), "Engine already initialized");
  110. parse_command_line(argc, argv);
  111. check_preferred_settings();
  112. // Resource compilation only in debug or development mode and only on linux or windows builds
  113. #if (defined(LINUX) || defined(WINDOWS)) && (defined(CROWN_DEBUG) || defined(CROWN_DEVELOPMENT))
  114. if (m_compile == 1)
  115. {
  116. m_bundle_compiler = CE_NEW(m_allocator, BundleCompiler);
  117. if (!m_bundle_compiler->compile(m_bundle_dir, m_source_dir))
  118. {
  119. CE_DELETE(m_allocator, m_bundle_compiler);
  120. m_allocator.clear();
  121. Log::e("Exiting.");
  122. exit(EXIT_FAILURE);
  123. }
  124. if (!m_continue)
  125. {
  126. CE_DELETE(m_allocator, m_bundle_compiler);
  127. m_allocator.clear();
  128. exit(EXIT_SUCCESS);
  129. }
  130. }
  131. #endif
  132. init();
  133. return true;
  134. }
  135. //-----------------------------------------------------------------------------
  136. void Device::init()
  137. {
  138. // Initialize
  139. Log::i("Initializing Crown Engine %d.%d.%d...", CROWN_VERSION_MAJOR, CROWN_VERSION_MINOR, CROWN_VERSION_MICRO);
  140. // Default bundle filesystem
  141. #if defined (LINUX) || defined(WINDOWS)
  142. m_filesystem = CE_NEW(m_allocator, DiskFilesystem)(m_bundle_dir);
  143. #elif defined(ANDROID)
  144. m_filesystem = CE_NEW(m_allocator, ApkFilesystem)();
  145. #endif
  146. Log::d("Filesystem created.");
  147. // Read settings from crown.config
  148. read_engine_settings();
  149. m_resource_bundle = Bundle::create(m_allocator, *m_filesystem);
  150. // // Read resource seed
  151. // DiskFile* seed_file = (DiskFile*)filesystem()->open(g_default_mountpoint.value(), "seed.ini", FOM_READ);
  152. // TextReader reader(*seed_file);
  153. // char tmp_buf[32];
  154. // reader.read_string(tmp_buf, 32);
  155. // filesystem()->close(seed_file);
  156. // uint32_t seed = string::parse_uint(tmp_buf);
  157. // Create resource manager
  158. m_resource_manager = CE_NEW(m_allocator, ResourceManager)(*m_resource_bundle, 0);
  159. Log::d("Resource manager created.");
  160. Log::d("Resource seed: %d", m_resource_manager->seed());
  161. // Create input manager
  162. m_input_manager = CE_NEW(m_allocator, InputManager)();
  163. Log::d("Input manager created.");
  164. // default_allocator, maybe it needs fix
  165. m_window = CE_NEW(m_allocator, OsWindow)(m_preferred_window_width, m_preferred_window_height, m_parent_window_handle);
  166. CE_ASSERT(m_window != NULL, "Unable to create the window");
  167. // Create main window
  168. m_window->set_title("Crown Game Engine");
  169. Log::d("Window created.");
  170. // Create renderer
  171. m_renderer = CE_NEW(m_allocator, Renderer)(m_allocator);
  172. m_renderer->init();
  173. m_renderer_init_request = false;
  174. Log::d("Renderer created.");
  175. // Create debug renderer
  176. m_debug_renderer = CE_NEW(m_allocator, DebugRenderer)(*m_renderer);
  177. Log::d("Debug renderer created.");
  178. m_lua_environment = CE_NEW(m_allocator, LuaEnvironment)();
  179. m_lua_environment->init();
  180. Log::d("Lua environment created.");
  181. // FIXME: replace default_allocator with linear m_allocator
  182. m_sound_renderer = CE_NEW(m_allocator, SoundRenderer)(m_allocator);
  183. m_sound_renderer->init();
  184. Log::d("SoundRenderer created.");
  185. // ResourceId rid = m_resource_manager->load("sound", "sounds/birds1");
  186. // m_resource_manager->flush();
  187. // SoundResource* res = (SoundResource*)m_resource_manager->data(rid);
  188. // WaveSample sample;
  189. // sample.create(res->data(), res->size(), res->sample_rate(), res->channels(), res->block_size(), res->bits_ps());
  190. // OggDecoder decoder((char*)stream->data(), stream->size());
  191. // SoundSourceId source = m_sound_renderer->create_source();
  192. // decoder.stream();
  193. // SoundBufferId buffer1 = m_sound_renderer->create_buffer(decoder.data(), decoder.size(), stream->sample_rate(), stream->channels(), stream->bits_per_sample());
  194. // decoder.stream();
  195. // SoundBufferId buffer2 = m_sound_renderer->create_buffer(decoder.data(), decoder.size(), stream->sample_rate(), stream->channels(), stream->bits_per_sample());
  196. // SoundBufferId active = buffer1;
  197. // m_sound_renderer->bind_buffer_to_source(buffer1, source);
  198. // m_sound_renderer->bind_buffer_to_source(buffer2, source);
  199. // m_sound_renderer->play_source(source, false);
  200. // while(decoder.stream())
  201. // {
  202. // while(!m_sound_renderer->update_source(source, active, decoder.data(), decoder.size()));
  203. // active = buffer2;
  204. // buffer2 = buffer1;
  205. // buffer1 = active;
  206. // if (!m_sound_renderer->source_playing(source))
  207. // {
  208. // m_sound_renderer->play_source(source, false);
  209. // }
  210. // }
  211. Log::i("Crown Engine initialized.");
  212. Log::i("Initializing Game...");
  213. m_is_init = true;
  214. start();
  215. // Show main window
  216. m_window->show();
  217. // Execute lua boot file
  218. if (m_lua_environment->load_and_execute(m_boot_file))
  219. {
  220. if (!m_lua_environment->call_global("init", 0))
  221. {
  222. pause();
  223. }
  224. }
  225. else
  226. {
  227. pause();
  228. }
  229. Log::d("Total allocated size: %llu", m_allocator.allocated_size());
  230. if (m_quit_after_init == 1)
  231. {
  232. stop();
  233. shutdown();
  234. }
  235. }
  236. //-----------------------------------------------------------------------------
  237. void Device::shutdown()
  238. {
  239. CE_ASSERT(is_init(), "Engine is not initialized");
  240. // Shutdowns the game
  241. m_lua_environment->call_global("shutdown", 0);
  242. Log::i("Releasing ConsoleServer...");
  243. if (m_console_server)
  244. {
  245. //m_console_server->shutdown();
  246. CE_DELETE(m_allocator, m_console_server);
  247. }
  248. Log::d("Releasing SoundRenderer...");
  249. if (m_sound_renderer)
  250. {
  251. m_sound_renderer->shutdown();
  252. // FIXME: replace default_allocator with linear m_allocator
  253. CE_DELETE(m_allocator, m_sound_renderer);
  254. }
  255. Log::i("Releasing LuaEnvironment...");
  256. if (m_lua_environment)
  257. {
  258. m_lua_environment->shutdown();
  259. CE_DELETE(m_allocator, m_lua_environment);
  260. }
  261. Log::i("Releasing InputManager...");
  262. if (m_input_manager)
  263. {
  264. CE_DELETE(m_allocator, m_input_manager);
  265. }
  266. Log::i("Releasing DebugRenderer...");
  267. if (m_debug_renderer)
  268. {
  269. CE_DELETE(m_allocator, m_debug_renderer);
  270. }
  271. Log::i("Releasing Renderer...");
  272. if (m_renderer)
  273. {
  274. m_renderer->shutdown();
  275. CE_DELETE(m_allocator, m_renderer);
  276. }
  277. Log::i("Releasing Window...");
  278. if (m_window)
  279. {
  280. CE_DELETE(m_allocator, m_window);
  281. }
  282. Log::i("Releasing ResourceManager...");
  283. if (m_resource_manager)
  284. {
  285. CE_DELETE(m_allocator, m_resource_manager);
  286. }
  287. if (m_resource_bundle)
  288. {
  289. Bundle::destroy(m_allocator, m_resource_bundle);
  290. }
  291. Log::i("Releasing Filesystem...");
  292. if (m_filesystem)
  293. {
  294. CE_DELETE(m_allocator, m_filesystem);
  295. }
  296. #if (defined(LINUX) || defined(WINDOWS)) && (defined(CROWN_DEBUG) || defined(CROWN_DEVELOPMENT))
  297. Log::i("Releasing BundleCompiler...");
  298. if (m_bundle_compiler)
  299. {
  300. CE_DELETE(m_allocator, m_bundle_compiler);
  301. }
  302. #endif
  303. m_allocator.clear();
  304. m_is_init = false;
  305. }
  306. //-----------------------------------------------------------------------------
  307. bool Device::is_init() const
  308. {
  309. return m_is_init;
  310. }
  311. //-----------------------------------------------------------------------------
  312. bool Device::is_paused() const
  313. {
  314. return m_is_paused;
  315. }
  316. //-----------------------------------------------------------------------------
  317. Filesystem* Device::filesystem()
  318. {
  319. return m_filesystem;
  320. }
  321. //-----------------------------------------------------------------------------
  322. ResourceManager* Device::resource_manager()
  323. {
  324. return m_resource_manager;
  325. }
  326. //-----------------------------------------------------------------------------
  327. InputManager* Device::input_manager()
  328. {
  329. return m_input_manager;
  330. }
  331. //-----------------------------------------------------------------------------
  332. LuaEnvironment* Device::lua_environment()
  333. {
  334. return m_lua_environment;
  335. }
  336. //-----------------------------------------------------------------------------
  337. OsWindow* Device::window()
  338. {
  339. return m_window;
  340. }
  341. //-----------------------------------------------------------------------------
  342. Renderer* Device::renderer()
  343. {
  344. return m_renderer;
  345. }
  346. //-----------------------------------------------------------------------------
  347. DebugRenderer* Device::debug_renderer()
  348. {
  349. return m_debug_renderer;
  350. }
  351. //-----------------------------------------------------------------------------
  352. SoundRenderer* Device::sound_renderer()
  353. {
  354. return m_sound_renderer;
  355. }
  356. //-----------------------------------------------------------------------------
  357. Keyboard* Device::keyboard()
  358. {
  359. return m_input_manager->keyboard();
  360. }
  361. //-----------------------------------------------------------------------------
  362. Mouse* Device::mouse()
  363. {
  364. return m_input_manager->mouse();
  365. }
  366. //-----------------------------------------------------------------------------
  367. Touch* Device::touch()
  368. {
  369. return m_input_manager->touch();
  370. }
  371. //-----------------------------------------------------------------------------
  372. Accelerometer* Device::accelerometer()
  373. {
  374. return m_input_manager->accelerometer();
  375. }
  376. //-----------------------------------------------------------------------------
  377. ConsoleServer* Device::console_server()
  378. {
  379. return m_console_server;
  380. }
  381. //-----------------------------------------------------------------------------
  382. void Device::start()
  383. {
  384. CE_ASSERT(m_is_init, "Cannot start uninitialized engine.");
  385. m_is_running = true;
  386. m_last_time = os::milliseconds();
  387. }
  388. //-----------------------------------------------------------------------------
  389. void Device::stop()
  390. {
  391. CE_ASSERT(m_is_init, "Cannot stop uninitialized engine.");
  392. m_is_running = false;
  393. }
  394. //-----------------------------------------------------------------------------
  395. void Device::pause()
  396. {
  397. m_is_paused = true;
  398. Log::d("Engine paused");
  399. }
  400. //-----------------------------------------------------------------------------
  401. void Device::unpause()
  402. {
  403. m_is_paused = false;
  404. m_is_really_paused = false;
  405. Log::d("Engine unpaused");
  406. }
  407. //-----------------------------------------------------------------------------
  408. bool Device::is_running() const
  409. {
  410. return m_is_running;
  411. }
  412. //-----------------------------------------------------------------------------
  413. uint64_t Device::frame_count() const
  414. {
  415. return m_frame_count;
  416. }
  417. //-----------------------------------------------------------------------------
  418. float Device::last_delta_time() const
  419. {
  420. return m_last_delta_time;
  421. }
  422. //-----------------------------------------------------------------------------
  423. double Device::time_since_start() const
  424. {
  425. return m_time_since_start;
  426. }
  427. //-----------------------------------------------------------------------------
  428. void Device::frame(cb callback)
  429. {
  430. m_current_time = os::microseconds();
  431. m_last_delta_time = (m_current_time - m_last_time) / 1000000.0f;
  432. m_last_time = m_current_time;
  433. m_time_since_start += m_last_delta_time;
  434. if (!m_is_paused)
  435. {
  436. m_resource_manager->poll_resource_loader();
  437. m_window->frame();
  438. m_input_manager->frame(frame_count());
  439. if (!m_lua_environment->call_global("frame", 1, ARGUMENT_FLOAT, last_delta_time()))
  440. {
  441. pause();
  442. }
  443. callback(m_last_delta_time);
  444. m_renderer->frame();
  445. }
  446. m_frame_count++;
  447. os_event_buffer()->clear();
  448. }
  449. //-----------------------------------------------------------------------------
  450. ResourcePackage* Device::create_resource_package(const char* name)
  451. {
  452. CE_ASSERT_NOT_NULL(name);
  453. ResourceId package_id = m_resource_manager->load("package", name);
  454. m_resource_manager->flush();
  455. PackageResource* package_res = (PackageResource*) m_resource_manager->data(package_id);
  456. ResourcePackage* package = CE_NEW(default_allocator(), ResourcePackage)(*m_resource_manager, package_id, package_res);
  457. return package;
  458. }
  459. //-----------------------------------------------------------------------------
  460. void Device::destroy_resource_package(ResourcePackage* package)
  461. {
  462. CE_ASSERT_NOT_NULL(package);
  463. m_resource_manager->unload(package->resource_id());
  464. CE_DELETE(default_allocator(), package);
  465. }
  466. //-----------------------------------------------------------------------------
  467. void Device::compile(const char* , const char* , const char* )
  468. {
  469. }
  470. //-----------------------------------------------------------------------------
  471. void Device::reload(ResourceId name)
  472. {
  473. (void)name;
  474. }
  475. //-----------------------------------------------------------------------------
  476. void Device::parse_command_line(int argc, char** argv)
  477. {
  478. static ArgsOption options[] =
  479. {
  480. { "help", AOA_NO_ARGUMENT, NULL, 'i' },
  481. { "source-dir", AOA_REQUIRED_ARGUMENT, NULL, 's' },
  482. { "bundle-dir", AOA_REQUIRED_ARGUMENT, NULL, 'b' },
  483. { "compile", AOA_NO_ARGUMENT, &m_compile, 1 },
  484. { "continue", AOA_NO_ARGUMENT, &m_continue, 1 },
  485. { "width", AOA_REQUIRED_ARGUMENT, NULL, 'w' },
  486. { "height", AOA_REQUIRED_ARGUMENT, NULL, 'h' },
  487. { "fullscreen", AOA_NO_ARGUMENT, &m_preferred_window_fullscreen, 1 },
  488. { "parent-window", AOA_REQUIRED_ARGUMENT, NULL, 'p' },
  489. { "quit-after-init", AOA_NO_ARGUMENT, &m_quit_after_init, 1 },
  490. { NULL, 0, NULL, 0 }
  491. };
  492. Args args(argc, argv, "", options);
  493. int32_t opt;
  494. while ((opt = args.getopt()) != -1)
  495. {
  496. switch (opt)
  497. {
  498. case 0:
  499. {
  500. break;
  501. }
  502. // Source directory
  503. case 's':
  504. {
  505. string::strncpy(m_source_dir, args.optarg(), MAX_PATH_LENGTH);
  506. break;
  507. }
  508. // Bundle directory
  509. case 'b':
  510. {
  511. string::strncpy(m_bundle_dir, args.optarg(), MAX_PATH_LENGTH);
  512. break;
  513. }
  514. // Window width
  515. case 'w':
  516. {
  517. m_preferred_window_width = atoi(args.optarg());
  518. break;
  519. }
  520. // Window height
  521. case 'h':
  522. {
  523. m_preferred_window_height = atoi(args.optarg());
  524. break;
  525. }
  526. // Parent window
  527. case 'p':
  528. {
  529. m_parent_window_handle = string::parse_uint(args.optarg());
  530. break;
  531. }
  532. case 'i':
  533. case '?':
  534. default:
  535. {
  536. print_help_message();
  537. exit(EXIT_FAILURE);
  538. }
  539. }
  540. }
  541. }
  542. //-----------------------------------------------------------------------------
  543. void Device::check_preferred_settings()
  544. {
  545. if (m_compile == 1)
  546. {
  547. if (string::strcmp(m_source_dir, "") == 0)
  548. {
  549. Log::e("You have to specify the source directory when running in compile mode.");
  550. exit(EXIT_FAILURE);
  551. }
  552. if (!os::is_absolute_path(m_source_dir))
  553. {
  554. Log::e("The source directory must be absolute.");
  555. exit(EXIT_FAILURE);
  556. }
  557. }
  558. if (!os::is_absolute_path(m_bundle_dir))
  559. {
  560. Log::e("The bundle directory must be absolute.");
  561. exit(EXIT_FAILURE);
  562. }
  563. if (m_preferred_window_width == 0 || m_preferred_window_height == 0)
  564. {
  565. Log::e("Window width and height must be greater than zero.");
  566. exit(EXIT_FAILURE);
  567. }
  568. }
  569. //-----------------------------------------------------------------------------
  570. void Device::read_engine_settings()
  571. {
  572. // Check crown.config existance
  573. CE_ASSERT(m_filesystem->is_file("crown.config"), "Unable to open crown.config");
  574. // Copy crown config in a buffer
  575. TempAllocator4096 allocator;
  576. File* config_file = m_filesystem->open("crown.config", FOM_READ);
  577. char* json_string = (char*)allocator.allocate(config_file->size());
  578. config_file->read(json_string, config_file->size());
  579. m_filesystem->close(config_file);
  580. // Parse crown.config
  581. JSONParser parser(json_string);
  582. JSONElement root = parser.root();
  583. // Boot
  584. if (root.has_key("boot"))
  585. {
  586. const char* boot = root.key("boot").string_value();
  587. const size_t boot_length = string::strlen(boot) + 1;
  588. string::strncpy(m_boot_file, boot, boot_length);
  589. }
  590. // Window width
  591. if (root.has_key("window_width"))
  592. {
  593. m_preferred_window_width = root.key("window_width").int_value();
  594. }
  595. // Window height
  596. if (root.has_key("window_height"))
  597. {
  598. m_preferred_window_height = root.key("window_height").int_value();
  599. }
  600. allocator.deallocate(json_string);
  601. Log::i("Configuration set");
  602. }
  603. //-----------------------------------------------------------------------------
  604. void Device::print_help_message()
  605. {
  606. os::printf(
  607. "Usage: crown [options]\n"
  608. "Options:\n\n"
  609. "All of the following options take precedence over\n"
  610. "environment variables and configuration files.\n\n"
  611. " --help Show this help.\n"
  612. " --bundle-dir <path> Use <path> as the source directory for compiled resources.\n"
  613. " --width <width> Set the <width> of the main window.\n"
  614. " --height <width> Set the <height> of the main window.\n"
  615. " --fullscreen Start in fullscreen.\n"
  616. " --parent-window <handle> Set the parent window <handle> of the main window.\n"
  617. " Used only by tools.\n"
  618. "\nAvailable only in debug and development builds:\n\n"
  619. " --source-dir <path> Use <path> as the source directory for resource compilation.\n"
  620. " --compile Run the engine as resource compiler.\n"
  621. " --continue Do a full compile of the resources and continue the execution.\n"
  622. " --quit-after-init Quit the engine immediately after the initialization.\n");
  623. }
  624. static Device* g_device = NULL;
  625. //-----------------------------------------------------------------------------
  626. void init()
  627. {
  628. crown::memory::init();
  629. crown::os::init_os();
  630. g_device = CE_NEW(default_allocator(), Device);
  631. }
  632. //-----------------------------------------------------------------------------
  633. void shutdown()
  634. {
  635. CE_DELETE(default_allocator(), g_device);
  636. crown::memory::shutdown();
  637. }
  638. Device* device()
  639. {
  640. return g_device;
  641. }
  642. void nothing(float)
  643. {
  644. device()->renderer()->set_layer_clear(0, CLEAR_COLOR | CLEAR_DEPTH, Color4::LIGHTBLUE, 1.0f);
  645. device()->renderer()->commit(0);
  646. }
  647. } // namespace crown