Device.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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 renderer
  146. m_renderer = CE_NEW(m_allocator, Renderer)(m_allocator);
  147. m_renderer->init();
  148. Log::d("Renderer created.");
  149. // Create debug renderer
  150. m_debug_renderer = CE_NEW(m_allocator, DebugRenderer)(*m_renderer);
  151. Log::d("Debug renderer created.");
  152. m_lua_environment = CE_NEW(m_allocator, LuaEnvironment)();
  153. m_lua_environment->init();
  154. Log::d("Lua environment created.");
  155. Log::i("Crown Engine initialized.");
  156. Log::i("Initializing Game...");
  157. m_is_init = true;
  158. start();
  159. // Execute lua boot file
  160. if (m_lua_environment->load_and_execute(m_boot_file))
  161. {
  162. if (!m_lua_environment->call_global("init", 0))
  163. {
  164. pause();
  165. }
  166. }
  167. else
  168. {
  169. pause();
  170. }
  171. Log::d("Total allocated size: %llu", m_allocator.allocated_size());
  172. if (m_quit_after_init == 1)
  173. {
  174. stop();
  175. shutdown();
  176. }
  177. }
  178. //-----------------------------------------------------------------------------
  179. void Device::shutdown()
  180. {
  181. CE_ASSERT(is_init(), "Engine is not initialized");
  182. // Shutdowns the game
  183. m_lua_environment->call_global("shutdown", 0);
  184. Log::i("Releasing LuaEnvironment...");
  185. if (m_lua_environment)
  186. {
  187. m_lua_environment->shutdown();
  188. CE_DELETE(m_allocator, m_lua_environment);
  189. }
  190. Log::i("Releasing DebugRenderer...");
  191. if (m_debug_renderer)
  192. {
  193. CE_DELETE(m_allocator, m_debug_renderer);
  194. }
  195. Log::i("Releasing Renderer...");
  196. if (m_renderer)
  197. {
  198. m_renderer->shutdown();
  199. CE_DELETE(m_allocator, m_renderer);
  200. }
  201. Log::i("Releasing ResourceManager...");
  202. if (m_resource_manager)
  203. {
  204. CE_DELETE(m_allocator, m_resource_manager);
  205. }
  206. if (m_resource_bundle)
  207. {
  208. Bundle::destroy(m_allocator, m_resource_bundle);
  209. }
  210. Log::i("Releasing Filesystem...");
  211. if (m_filesystem)
  212. {
  213. CE_DELETE(m_allocator, m_filesystem);
  214. }
  215. #if (defined(LINUX) || defined(WINDOWS)) && (defined(CROWN_DEBUG) || defined(CROWN_DEVELOPMENT))
  216. Log::i("Releasing BundleCompiler...");
  217. if (m_bundle_compiler)
  218. {
  219. CE_DELETE(m_allocator, m_bundle_compiler);
  220. }
  221. #endif
  222. m_allocator.clear();
  223. m_is_init = false;
  224. }
  225. //-----------------------------------------------------------------------------
  226. bool Device::is_init() const
  227. {
  228. return m_is_init;
  229. }
  230. //-----------------------------------------------------------------------------
  231. bool Device::is_paused() const
  232. {
  233. return m_is_paused;
  234. }
  235. //-----------------------------------------------------------------------------
  236. Filesystem* Device::filesystem()
  237. {
  238. return m_filesystem;
  239. }
  240. //-----------------------------------------------------------------------------
  241. ResourceManager* Device::resource_manager()
  242. {
  243. return m_resource_manager;
  244. }
  245. //-----------------------------------------------------------------------------
  246. LuaEnvironment* Device::lua_environment()
  247. {
  248. return m_lua_environment;
  249. }
  250. //-----------------------------------------------------------------------------
  251. Renderer* Device::renderer()
  252. {
  253. return m_renderer;
  254. }
  255. //-----------------------------------------------------------------------------
  256. DebugRenderer* Device::debug_renderer()
  257. {
  258. return m_debug_renderer;
  259. }
  260. //-----------------------------------------------------------------------------
  261. Keyboard* Device::keyboard()
  262. {
  263. return NULL;
  264. }
  265. //-----------------------------------------------------------------------------
  266. Mouse* Device::mouse()
  267. {
  268. return NULL;
  269. }
  270. //-----------------------------------------------------------------------------
  271. Touch* Device::touch()
  272. {
  273. return NULL;
  274. }
  275. //-----------------------------------------------------------------------------
  276. Accelerometer* Device::accelerometer()
  277. {
  278. return NULL;
  279. }
  280. //-----------------------------------------------------------------------------
  281. void Device::start()
  282. {
  283. CE_ASSERT(m_is_init, "Cannot start uninitialized engine.");
  284. m_is_running = true;
  285. m_last_time = os::milliseconds();
  286. }
  287. //-----------------------------------------------------------------------------
  288. void Device::stop()
  289. {
  290. CE_ASSERT(m_is_init, "Cannot stop uninitialized engine.");
  291. m_is_running = false;
  292. }
  293. //-----------------------------------------------------------------------------
  294. void Device::pause()
  295. {
  296. m_is_paused = true;
  297. }
  298. //-----------------------------------------------------------------------------
  299. void Device::unpause()
  300. {
  301. m_is_paused = false;
  302. }
  303. //-----------------------------------------------------------------------------
  304. bool Device::is_running() const
  305. {
  306. return m_is_running;
  307. }
  308. //-----------------------------------------------------------------------------
  309. uint64_t Device::frame_count() const
  310. {
  311. return m_frame_count;
  312. }
  313. //-----------------------------------------------------------------------------
  314. float Device::last_delta_time() const
  315. {
  316. return m_last_delta_time;
  317. }
  318. //-----------------------------------------------------------------------------
  319. double Device::time_since_start() const
  320. {
  321. return m_time_since_start;
  322. }
  323. //-----------------------------------------------------------------------------
  324. void Device::frame()
  325. {
  326. m_current_time = os::microseconds();
  327. m_last_delta_time = (m_current_time - m_last_time) / 1000000.0f;
  328. m_last_time = m_current_time;
  329. m_time_since_start += m_last_delta_time;
  330. if (!m_is_paused)
  331. {
  332. m_resource_manager->poll_resource_loader();
  333. m_renderer->set_layer_clear(0, CLEAR_COLOR | CLEAR_DEPTH, Color4::LIGHTBLUE, 1.0f);
  334. m_renderer->commit(0);
  335. if (!m_lua_environment->call_global("frame", 1, ARGUMENT_FLOAT, last_delta_time()))
  336. {
  337. pause();
  338. }
  339. m_renderer->frame();
  340. }
  341. m_frame_count++;
  342. }
  343. //-----------------------------------------------------------------------------
  344. ResourcePackage* Device::create_resource_package(const char* name)
  345. {
  346. CE_ASSERT_NOT_NULL(name);
  347. ResourceId package_id = m_resource_manager->load("package", name);
  348. m_resource_manager->flush();
  349. PackageResource* package_res = (PackageResource*) m_resource_manager->data(package_id);
  350. ResourcePackage* package = CE_NEW(default_allocator(), ResourcePackage)(*m_resource_manager, package_id, package_res);
  351. return package;
  352. }
  353. //-----------------------------------------------------------------------------
  354. void Device::destroy_resource_package(ResourcePackage* package)
  355. {
  356. CE_ASSERT_NOT_NULL(package);
  357. m_resource_manager->unload(package->resource_id());
  358. CE_DELETE(default_allocator(), package);
  359. }
  360. //-----------------------------------------------------------------------------
  361. void Device::compile(const char* , const char* , const char* )
  362. {
  363. }
  364. //-----------------------------------------------------------------------------
  365. void Device::reload(ResourceId name)
  366. {
  367. (void)name;
  368. }
  369. //-----------------------------------------------------------------------------
  370. void Device::parse_command_line(int argc, char** argv)
  371. {
  372. static ArgsOption options[] =
  373. {
  374. { "help", AOA_NO_ARGUMENT, NULL, 'i' },
  375. { "source-dir", AOA_REQUIRED_ARGUMENT, NULL, 's' },
  376. { "bundle-dir", AOA_REQUIRED_ARGUMENT, NULL, 'b' },
  377. { "compile", AOA_NO_ARGUMENT, &m_compile, 1 },
  378. { "continue", AOA_NO_ARGUMENT, &m_continue, 1 },
  379. { "width", AOA_REQUIRED_ARGUMENT, NULL, 'w' },
  380. { "height", AOA_REQUIRED_ARGUMENT, NULL, 'h' },
  381. { "fullscreen", AOA_NO_ARGUMENT, &m_preferred_window_fullscreen, 1 },
  382. { "parent-window", AOA_REQUIRED_ARGUMENT, NULL, 'p' },
  383. { "quit-after-init", AOA_NO_ARGUMENT, &m_quit_after_init, 1 },
  384. { NULL, 0, NULL, 0 }
  385. };
  386. Args args(argc, argv, "", options);
  387. int32_t opt;
  388. while ((opt = args.getopt()) != -1)
  389. {
  390. switch (opt)
  391. {
  392. case 0:
  393. {
  394. break;
  395. }
  396. // Source directory
  397. case 's':
  398. {
  399. string::strncpy(m_source_dir, args.optarg(), MAX_PATH_LENGTH);
  400. break;
  401. }
  402. // Bundle directory
  403. case 'b':
  404. {
  405. string::strncpy(m_bundle_dir, args.optarg(), MAX_PATH_LENGTH);
  406. break;
  407. }
  408. // Window width
  409. case 'w':
  410. {
  411. m_preferred_window_width = atoi(args.optarg());
  412. break;
  413. }
  414. // Window height
  415. case 'h':
  416. {
  417. m_preferred_window_height = atoi(args.optarg());
  418. break;
  419. }
  420. // Parent window
  421. case 'p':
  422. {
  423. m_parent_window_handle = string::parse_uint(args.optarg());
  424. break;
  425. }
  426. case 'i':
  427. case '?':
  428. default:
  429. {
  430. print_help_message();
  431. exit(EXIT_FAILURE);
  432. }
  433. }
  434. }
  435. }
  436. //-----------------------------------------------------------------------------
  437. void Device::check_preferred_settings()
  438. {
  439. if (m_compile == 1)
  440. {
  441. if (string::strcmp(m_source_dir, "") == 0)
  442. {
  443. Log::e("You have to specify the source directory when running in compile mode.");
  444. exit(EXIT_FAILURE);
  445. }
  446. if (!os::is_absolute_path(m_source_dir))
  447. {
  448. Log::e("The source directory must be absolute.");
  449. exit(EXIT_FAILURE);
  450. }
  451. }
  452. if (!os::is_absolute_path(m_bundle_dir))
  453. {
  454. Log::e("The bundle directory must be absolute.");
  455. exit(EXIT_FAILURE);
  456. }
  457. if (m_preferred_window_width == 0 || m_preferred_window_height == 0)
  458. {
  459. Log::e("Window width and height must be greater than zero.");
  460. exit(EXIT_FAILURE);
  461. }
  462. }
  463. //-----------------------------------------------------------------------------
  464. void Device::read_engine_settings()
  465. {
  466. // Check crown.config existance
  467. CE_ASSERT(m_filesystem->is_file("crown.config"), "Unable to open crown.config");
  468. // Copy crown config in a buffer
  469. TempAllocator4096 allocator;
  470. File* config_file = m_filesystem->open("crown.config", FOM_READ);
  471. char* json_string = (char*)allocator.allocate(config_file->size());
  472. config_file->read(json_string, config_file->size());
  473. m_filesystem->close(config_file);
  474. // Parse crown.config
  475. JSONParser parser(json_string);
  476. JSONElement root = parser.root();
  477. // Boot
  478. if (root.has_key("boot"))
  479. {
  480. const char* boot = root.key("boot").string_value();
  481. const size_t boot_length = string::strlen(boot) + 1;
  482. string::strncpy(m_boot_file, boot, boot_length);
  483. }
  484. // Window width
  485. if (root.has_key("window_width"))
  486. {
  487. m_preferred_window_width = root.key("window_width").int_value();
  488. }
  489. // Window height
  490. if (root.has_key("window_height"))
  491. {
  492. m_preferred_window_height = root.key("window_height").int_value();
  493. }
  494. allocator.deallocate(json_string);
  495. Log::i("Configuration set");
  496. }
  497. //-----------------------------------------------------------------------------
  498. void Device::print_help_message()
  499. {
  500. os::printf(
  501. "Usage: crown [options]\n"
  502. "Options:\n\n"
  503. "All of the following options take precedence over\n"
  504. "environment variables and configuration files.\n\n"
  505. " --help Show this help.\n"
  506. " --bundle-dir <path> Use <path> as the source directory for compiled resources.\n"
  507. " --width <width> Set the <width> of the main window.\n"
  508. " --height <width> Set the <height> of the main window.\n"
  509. " --fullscreen Start in fullscreen.\n"
  510. " --parent-window <handle> Set the parent window <handle> of the main window.\n"
  511. " Used only by tools.\n"
  512. "\nAvailable only in debug and development builds:\n\n"
  513. " --source-dir <path> Use <path> as the source directory for resource compilation.\n"
  514. " --compile Run the engine as resource compiler.\n"
  515. " --continue Do a full compile of the resources and continue the execution.\n"
  516. " --quit-after-init Quit the engine immediately after the initialization.\n");
  517. }
  518. static Device* g_device;
  519. void set_device(Device* device)
  520. {
  521. g_device = device;
  522. }
  523. Device* device()
  524. {
  525. return g_device;
  526. }
  527. } // namespace crown