Device.cpp 16 KB

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