Device.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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. #if defined(LINUX) || defined(WINDOWS)
  51. #include "BundleCompiler.h"
  52. #endif
  53. #if defined(ANDROID)
  54. #include "ApkFilesystem.h"
  55. #endif
  56. namespace crown
  57. {
  58. //-----------------------------------------------------------------------------
  59. Device::Device() :
  60. m_allocator(m_subsystems_heap, MAX_SUBSYSTEMS_HEAP),
  61. m_preferred_window_width(1000),
  62. m_preferred_window_height(625),
  63. m_preferred_window_fullscreen(0),
  64. m_parent_window_handle(0),
  65. m_compile(0),
  66. m_continue(0),
  67. m_quit_after_init(0),
  68. m_is_init(false),
  69. m_is_running(false),
  70. m_frame_count(0),
  71. m_last_time(0),
  72. m_current_time(0),
  73. m_last_delta_time(0.0f),
  74. m_filesystem(NULL),
  75. m_input_manager(NULL),
  76. m_lua_environment(NULL),
  77. m_renderer(NULL),
  78. m_debug_renderer(NULL),
  79. m_resource_manager(NULL),
  80. m_resource_bundle(NULL),
  81. m_console_server(NULL)
  82. {
  83. // Bundle dir is current dir by default.
  84. string::strncpy(m_bundle_dir, os::get_cwd(), MAX_PATH_LENGTH);
  85. string::strncpy(m_source_dir, "", MAX_PATH_LENGTH);
  86. }
  87. //-----------------------------------------------------------------------------
  88. Device::~Device()
  89. {
  90. }
  91. //-----------------------------------------------------------------------------
  92. bool Device::init(int argc, char** argv)
  93. {
  94. CE_ASSERT(!is_init(), "Engine already initialized");
  95. parse_command_line(argc, argv);
  96. check_preferred_settings();
  97. // Resource compilation only in debug or development mode and only on linux or windows builds
  98. #if (defined(LINUX) || defined(WINDOWS)) && (defined(CROWN_DEBUG) || defined(CROWN_DEVELOPMENT))
  99. if (m_compile == 1)
  100. {
  101. m_bundle_compiler = CE_NEW(m_allocator, BundleCompiler);
  102. if (!m_bundle_compiler->compile(m_bundle_dir, m_source_dir))
  103. {
  104. CE_DELETE(m_allocator, m_bundle_compiler);
  105. Log::e("Exiting.");
  106. exit(EXIT_FAILURE);
  107. }
  108. if (!m_continue)
  109. {
  110. CE_DELETE(m_allocator, m_bundle_compiler);
  111. exit(EXIT_SUCCESS);
  112. }
  113. }
  114. #endif
  115. // Initialize
  116. Log::i("Initializing Crown Engine %d.%d.%d...", CROWN_VERSION_MAJOR, CROWN_VERSION_MINOR, CROWN_VERSION_MICRO);
  117. // Default bundle filesystem
  118. #if defined (LINUX) || defined(WINDOWS)
  119. m_filesystem = CE_NEW(m_allocator, DiskFilesystem)(m_bundle_dir);
  120. #elif defined(ANDROID)
  121. m_filesystem = CE_NEW(m_allocator, ApkFilesystem)();
  122. #endif
  123. Log::d("Filesystem created.");
  124. m_resource_bundle = Bundle::create(m_allocator, *m_filesystem);
  125. // // Read resource seed
  126. // DiskFile* seed_file = (DiskFile*)filesystem()->open(g_default_mountpoint.value(), "seed.ini", FOM_READ);
  127. // TextReader reader(*seed_file);
  128. // char tmp_buf[32];
  129. // reader.read_string(tmp_buf, 32);
  130. // filesystem()->close(seed_file);
  131. // uint32_t seed = string::parse_uint(tmp_buf);
  132. // Create resource manager
  133. m_resource_manager = CE_NEW(m_allocator, ResourceManager)(*m_resource_bundle, 0);
  134. Log::d("Resource manager created.");
  135. Log::d("Resource seed: %d", m_resource_manager->seed());
  136. // Create input manager
  137. m_input_manager = CE_NEW(m_allocator, InputManager)();
  138. Log::d("Input manager created.");
  139. m_window = CE_NEW(m_allocator, OsWindow)(m_preferred_window_width, m_preferred_window_height, m_parent_window_handle);
  140. CE_ASSERT(m_window != NULL, "Unable to create the window");
  141. m_window->set_title("Crown Game Engine");
  142. m_window->show();
  143. Log::d("Window created.");
  144. m_renderer = Renderer::create(m_allocator);
  145. m_renderer->init();
  146. Log::d("Renderer created.");
  147. // Create debug renderer
  148. m_debug_renderer = CE_NEW(m_allocator, DebugRenderer)(*m_renderer);
  149. Log::d("Debug renderer created.");
  150. m_lua_environment = CE_NEW(m_allocator, LuaEnvironment)();
  151. m_lua_environment->init();
  152. Log::d("Lua environment created.");
  153. Log::i("Crown Engine initialized.");
  154. Log::i("Initializing Game...");
  155. m_is_init = true;
  156. start();
  157. ResourceId luagame_id = m_resource_manager->load("lua", "lua/game");
  158. m_resource_manager->flush();
  159. m_lua_environment->load((LuaResource*) m_resource_manager->data(luagame_id));
  160. m_lua_environment->call_global("init", 0);
  161. if (m_quit_after_init == 1)
  162. {
  163. stop();
  164. shutdown();
  165. }
  166. return true;
  167. }
  168. //-----------------------------------------------------------------------------
  169. void Device::shutdown()
  170. {
  171. if (is_init() == false)
  172. {
  173. Log::e("Crown Engine is not initialized.");
  174. return;
  175. }
  176. // Shutdowns the game
  177. m_lua_environment->call_global("shutdown", 0);
  178. Log::i("Releasing ConsoleServer...");
  179. if (m_console_server)
  180. {
  181. //m_console_server->shutdown();
  182. CE_DELETE(m_allocator, m_console_server);
  183. }
  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 InputManager...");
  191. if (m_input_manager)
  192. {
  193. CE_DELETE(m_allocator, m_input_manager);
  194. }
  195. Log::i("Releasing DebugRenderer...");
  196. if (m_debug_renderer)
  197. {
  198. CE_DELETE(m_allocator, m_debug_renderer);
  199. }
  200. Log::i("Releasing Renderer...");
  201. if (m_renderer)
  202. {
  203. m_renderer->shutdown();
  204. Renderer::destroy(m_allocator, m_renderer);
  205. }
  206. Log::i("Releasing Window...");
  207. if (m_window)
  208. {
  209. CE_DELETE(m_allocator, m_window);
  210. }
  211. Log::i("Releasing ResourceManager...");
  212. if (m_resource_manager)
  213. {
  214. CE_DELETE(m_allocator, m_resource_manager);
  215. }
  216. if (m_resource_bundle)
  217. {
  218. Bundle::destroy(m_allocator, m_resource_bundle);
  219. }
  220. Log::i("Releasing Filesystem...");
  221. if (m_filesystem)
  222. {
  223. CE_DELETE(m_allocator, m_filesystem);
  224. }
  225. m_allocator.clear();
  226. m_is_init = false;
  227. }
  228. //-----------------------------------------------------------------------------
  229. bool Device::is_init() const
  230. {
  231. return m_is_init;
  232. }
  233. //-----------------------------------------------------------------------------
  234. Filesystem* Device::filesystem()
  235. {
  236. return m_filesystem;
  237. }
  238. //-----------------------------------------------------------------------------
  239. ResourceManager* Device::resource_manager()
  240. {
  241. return m_resource_manager;
  242. }
  243. //-----------------------------------------------------------------------------
  244. InputManager* Device::input_manager()
  245. {
  246. return m_input_manager;
  247. }
  248. //-----------------------------------------------------------------------------
  249. LuaEnvironment* Device::lua_environment()
  250. {
  251. return m_lua_environment;
  252. }
  253. //-----------------------------------------------------------------------------
  254. OsWindow* Device::window()
  255. {
  256. return m_window;
  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_input_manager->keyboard();
  272. }
  273. //-----------------------------------------------------------------------------
  274. Mouse* Device::mouse()
  275. {
  276. return m_input_manager->mouse();
  277. }
  278. //-----------------------------------------------------------------------------
  279. Touch* Device::touch()
  280. {
  281. return m_input_manager->touch();
  282. }
  283. //-----------------------------------------------------------------------------
  284. Accelerometer* Device::accelerometer()
  285. {
  286. return m_input_manager->accelerometer();
  287. }
  288. ConsoleServer* Device::console_server()
  289. {
  290. return m_console_server;
  291. }
  292. //-----------------------------------------------------------------------------
  293. void Device::start()
  294. {
  295. CE_ASSERT(m_is_init, "Cannot start uninitialized engine.");
  296. m_is_running = true;
  297. m_last_time = os::milliseconds();
  298. }
  299. //-----------------------------------------------------------------------------
  300. void Device::stop()
  301. {
  302. CE_ASSERT(m_is_init, "Cannot stop uninitialized engine.");
  303. m_is_running = false;
  304. }
  305. //-----------------------------------------------------------------------------
  306. bool Device::is_running() const
  307. {
  308. return m_is_running;
  309. }
  310. //-----------------------------------------------------------------------------
  311. uint64_t Device::frame_count() const
  312. {
  313. return m_frame_count;
  314. }
  315. //-----------------------------------------------------------------------------
  316. float Device::last_delta_time() const
  317. {
  318. return m_last_delta_time;
  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_resource_manager->poll_resource_loader();
  327. m_window->frame();
  328. m_input_manager->frame(frame_count());
  329. m_lua_environment->call_global("frame", 1, ARGUMENT_FLOAT, last_delta_time());
  330. // m_console_server->execute();
  331. m_debug_renderer->draw_all();
  332. m_renderer->frame();
  333. m_frame_count++;
  334. }
  335. //-----------------------------------------------------------------------------
  336. void Device::compile(const char* , const char* , const char* )
  337. {
  338. }
  339. //-----------------------------------------------------------------------------
  340. void Device::reload(ResourceId name)
  341. {
  342. (void)name;
  343. }
  344. //-----------------------------------------------------------------------------
  345. void Device::parse_command_line(int argc, char** argv)
  346. {
  347. static ArgsOption options[] =
  348. {
  349. { "help", AOA_NO_ARGUMENT, NULL, 'i' },
  350. { "source-dir", AOA_REQUIRED_ARGUMENT, NULL, 's' },
  351. { "bundle-dir", AOA_REQUIRED_ARGUMENT, NULL, 'b' },
  352. { "compile", AOA_NO_ARGUMENT, &m_compile, 1 },
  353. { "continue", AOA_NO_ARGUMENT, &m_continue, 1 },
  354. { "width", AOA_REQUIRED_ARGUMENT, NULL, 'w' },
  355. { "height", AOA_REQUIRED_ARGUMENT, NULL, 'h' },
  356. { "fullscreen", AOA_NO_ARGUMENT, &m_preferred_window_fullscreen, 1 },
  357. { "parent-window", AOA_REQUIRED_ARGUMENT, NULL, 'p' },
  358. { "quit-after-init", AOA_NO_ARGUMENT, &m_quit_after_init, 1 },
  359. { NULL, 0, NULL, 0 }
  360. };
  361. Args args(argc, argv, "", options);
  362. int32_t opt;
  363. while ((opt = args.getopt()) != -1)
  364. {
  365. switch (opt)
  366. {
  367. case 0:
  368. {
  369. break;
  370. }
  371. // Source directory
  372. case 's':
  373. {
  374. string::strncpy(m_source_dir, args.optarg(), MAX_PATH_LENGTH);
  375. break;
  376. }
  377. // Bundle directory
  378. case 'b':
  379. {
  380. string::strncpy(m_bundle_dir, args.optarg(), MAX_PATH_LENGTH);
  381. break;
  382. }
  383. // Window width
  384. case 'w':
  385. {
  386. m_preferred_window_width = atoi(args.optarg());
  387. break;
  388. }
  389. // Window height
  390. case 'h':
  391. {
  392. m_preferred_window_height = atoi(args.optarg());
  393. break;
  394. }
  395. // Parent window
  396. case 'p':
  397. {
  398. m_parent_window_handle = string::parse_uint(args.optarg());
  399. break;
  400. }
  401. case 'i':
  402. case '?':
  403. default:
  404. {
  405. print_help_message();
  406. exit(EXIT_FAILURE);
  407. }
  408. }
  409. }
  410. }
  411. //-----------------------------------------------------------------------------
  412. void Device::check_preferred_settings()
  413. {
  414. if (!os::is_absolute_path(m_bundle_dir))
  415. {
  416. Log::e("The root path must be absolute.");
  417. exit(EXIT_FAILURE);
  418. }
  419. if (m_preferred_window_width == 0 || m_preferred_window_height == 0)
  420. {
  421. Log::e("Window width and height must be greater than zero.");
  422. exit(EXIT_FAILURE);
  423. }
  424. }
  425. //-----------------------------------------------------------------------------
  426. void Device::read_engine_settings()
  427. {
  428. }
  429. //-----------------------------------------------------------------------------
  430. void Device::print_help_message()
  431. {
  432. os::printf(
  433. "Usage: crown [options]\n"
  434. "Options:\n\n"
  435. "All of the following options take precedence over\n"
  436. "environment variables and configuration files.\n\n"
  437. " --help Show this help.\n"
  438. " --bundle-dir <path> Use <path> as the source directory for compiled resources.\n"
  439. " --width <width> Set the <width> of the main window.\n"
  440. " --height <width> Set the <height> of the main window.\n"
  441. " --fullscreen Start in fullscreen.\n"
  442. " --parent-window <handle> Set the parent window <handle> of the main window.\n"
  443. " Used only by tools.\n"
  444. "\nAvailable only in debug and development builds:\n\n"
  445. " --source-dir <path> Use <path> as the source directory for resource compilation.\n"
  446. " --compile Run the engine as resource compiler.\n"
  447. " --continue Do a full compile of the resources and continue the execution.\n"
  448. " --quit-after-init Quit the engine immediately after the initialization.\n");
  449. }
  450. Device g_device;
  451. Device* device()
  452. {
  453. return &g_device;
  454. }
  455. } // namespace crown