Device.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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. */
  23. #include <cstdlib>
  24. #include "Config.h"
  25. #include "Device.h"
  26. #include "Filesystem.h"
  27. #include "InputManager.h"
  28. #include "Log.h"
  29. #include "OS.h"
  30. #include "Renderer.h"
  31. #include "DebugRenderer.h"
  32. #include "Types.h"
  33. #include "StringUtils.h"
  34. #include "Args.h"
  35. #include "ArchiveBundle.h"
  36. #include "FileBundle.h"
  37. #include "ResourceManager.h"
  38. #include "TextureResource.h"
  39. #include "Keyboard.h"
  40. #include "Mouse.h"
  41. #include "Touch.h"
  42. #include "Accelerometer.h"
  43. #include "OsWindow.h"
  44. #include "JSONParser.h"
  45. #include "DiskFile.h"
  46. #include "Memory.h"
  47. #include "LuaEnvironment.h"
  48. #include "ConsoleServer.h"
  49. #include "TextReader.h"
  50. #include "SoundResource.h"
  51. #include "DiskMountPoint.h"
  52. #include "AndroidMountPoint.h"
  53. namespace crown
  54. {
  55. //-----------------------------------------------------------------------------
  56. Device::Device() :
  57. m_allocator(m_subsystems_heap, MAX_SUBSYSTEMS_HEAP),
  58. m_preferred_window_width(1000),
  59. m_preferred_window_height(625),
  60. m_preferred_window_fullscreen(0),
  61. m_preferred_mode(MODE_RELEASE),
  62. m_quit_after_init(0),
  63. m_is_init(false),
  64. m_is_running(false),
  65. m_frame_count(0),
  66. m_last_time(0),
  67. m_current_time(0),
  68. m_last_delta_time(0.0f),
  69. m_filesystem(NULL),
  70. m_input_manager(NULL),
  71. m_lua_environment(NULL),
  72. m_renderer(NULL),
  73. m_debug_renderer(NULL),
  74. m_resource_manager(NULL),
  75. m_resource_bundle(NULL),
  76. m_console_server(NULL)
  77. {
  78. // Select executable dir by default
  79. string::strncpy(m_preferred_root_path, os::get_cwd(), MAX_PATH_LENGTH);
  80. }
  81. //-----------------------------------------------------------------------------
  82. Device::~Device()
  83. {
  84. }
  85. //-----------------------------------------------------------------------------
  86. bool Device::init(int argc, char** argv)
  87. {
  88. if (is_init())
  89. {
  90. Log::e("Crown Engine is already initialized.");
  91. return false;
  92. }
  93. parse_command_line(argc, argv);
  94. check_preferred_settings();
  95. // Initialize
  96. Log::i("Initializing Crown Engine %d.%d.%d...", CROWN_VERSION_MAJOR, CROWN_VERSION_MINOR, CROWN_VERSION_MICRO);
  97. create_filesystem();
  98. create_resource_manager();
  99. create_input_manager();
  100. create_window();
  101. create_renderer();
  102. create_debug_renderer();
  103. create_lua_environment();
  104. // create_console_server();
  105. read_engine_settings();
  106. Log::i("Crown Engine initialized.");
  107. Log::i("Initializing Game...");
  108. // Initialize the game through init game function
  109. m_lua_environment->game_init();
  110. m_is_init = true;
  111. start();
  112. if (m_quit_after_init == 1)
  113. {
  114. shutdown();
  115. }
  116. return true;
  117. }
  118. //-----------------------------------------------------------------------------
  119. void Device::shutdown()
  120. {
  121. if (is_init() == false)
  122. {
  123. Log::e("Crown Engine is not initialized.");
  124. return;
  125. }
  126. // Shutdowns the game
  127. m_lua_environment->game_shutdown();
  128. Log::i("Releasing ConsoleServer...");
  129. if (m_console_server)
  130. {
  131. //m_console_server->shutdown();
  132. CE_DELETE(m_allocator, m_console_server);
  133. }
  134. Log::i("Releasing LuaEnvironment...");
  135. if (m_lua_environment)
  136. {
  137. m_lua_environment->shutdown();
  138. CE_DELETE(m_allocator, m_lua_environment);
  139. }
  140. Log::i("Releasing InputManager...");
  141. if (m_input_manager)
  142. {
  143. CE_DELETE(m_allocator, m_input_manager);
  144. }
  145. Log::i("Releasing DebugRenderer...");
  146. if (m_debug_renderer)
  147. {
  148. CE_DELETE(m_allocator, m_debug_renderer);
  149. }
  150. Log::i("Releasing Renderer...");
  151. if (m_renderer)
  152. {
  153. m_renderer->shutdown();
  154. Renderer::destroy(m_allocator, m_renderer);
  155. }
  156. Log::i("Releasing Window...");
  157. if (m_window)
  158. {
  159. CE_DELETE(m_allocator, m_window);
  160. }
  161. Log::i("Releasing ResourceManager...");
  162. if (m_resource_bundle)
  163. {
  164. CE_DELETE(m_allocator, m_resource_bundle);
  165. }
  166. if (m_resource_manager)
  167. {
  168. CE_DELETE(m_allocator, m_resource_manager);
  169. }
  170. Log::i("Releasing Filesystem...");
  171. if (m_filesystem)
  172. {
  173. CE_DELETE(m_allocator, m_filesystem);
  174. }
  175. m_allocator.clear();
  176. m_is_init = false;
  177. }
  178. //-----------------------------------------------------------------------------
  179. bool Device::is_init() const
  180. {
  181. return m_is_init;
  182. }
  183. //-----------------------------------------------------------------------------
  184. Filesystem* Device::filesystem()
  185. {
  186. return m_filesystem;
  187. }
  188. //-----------------------------------------------------------------------------
  189. ResourceManager* Device::resource_manager()
  190. {
  191. return m_resource_manager;
  192. }
  193. //-----------------------------------------------------------------------------
  194. InputManager* Device::input_manager()
  195. {
  196. return m_input_manager;
  197. }
  198. //-----------------------------------------------------------------------------
  199. LuaEnvironment* Device::lua_environment()
  200. {
  201. return m_lua_environment;
  202. }
  203. //-----------------------------------------------------------------------------
  204. OsWindow* Device::window()
  205. {
  206. return m_window;
  207. }
  208. //-----------------------------------------------------------------------------
  209. Renderer* Device::renderer()
  210. {
  211. return m_renderer;
  212. }
  213. //-----------------------------------------------------------------------------
  214. DebugRenderer* Device::debug_renderer()
  215. {
  216. return m_debug_renderer;
  217. }
  218. //-----------------------------------------------------------------------------
  219. Keyboard* Device::keyboard()
  220. {
  221. return m_input_manager->keyboard();
  222. }
  223. //-----------------------------------------------------------------------------
  224. Mouse* Device::mouse()
  225. {
  226. return m_input_manager->mouse();
  227. }
  228. //-----------------------------------------------------------------------------
  229. Touch* Device::touch()
  230. {
  231. return m_input_manager->touch();
  232. }
  233. //-----------------------------------------------------------------------------
  234. Accelerometer* Device::accelerometer()
  235. {
  236. return m_input_manager->accelerometer();
  237. }
  238. ConsoleServer* Device::console_server()
  239. {
  240. return m_console_server;
  241. }
  242. //-----------------------------------------------------------------------------
  243. void Device::start()
  244. {
  245. if (is_init() == false)
  246. {
  247. Log::e("Cannot start uninitialized engine.");
  248. return;
  249. }
  250. m_is_running = true;
  251. m_last_time = os::milliseconds();
  252. }
  253. //-----------------------------------------------------------------------------
  254. void Device::stop()
  255. {
  256. if (is_init() == false)
  257. {
  258. Log::e("Cannot stop uninitialized engine.");
  259. return;
  260. }
  261. m_is_running = false;
  262. }
  263. //-----------------------------------------------------------------------------
  264. bool Device::is_running() const
  265. {
  266. return m_is_running;
  267. }
  268. //-----------------------------------------------------------------------------
  269. uint64_t Device::frame_count() const
  270. {
  271. return m_frame_count;
  272. }
  273. //-----------------------------------------------------------------------------
  274. float Device::last_delta_time() const
  275. {
  276. return m_last_delta_time;
  277. }
  278. //-----------------------------------------------------------------------------
  279. void Device::frame()
  280. {
  281. m_current_time = os::microseconds();
  282. m_last_delta_time = (m_current_time - m_last_time) / 1000000.0f;
  283. m_last_time = m_current_time;
  284. m_resource_manager->poll_resource_loader();
  285. m_window->frame();
  286. m_input_manager->frame(frame_count());
  287. m_lua_environment->game_frame(last_delta_time());
  288. // m_console_server->execute();
  289. m_debug_renderer->draw_all();
  290. m_renderer->frame();
  291. m_frame_count++;
  292. }
  293. //-----------------------------------------------------------------------------
  294. void Device::reload(ResourceId name)
  295. {
  296. (void)name;
  297. }
  298. //-----------------------------------------------------------------------------
  299. void Device::create_filesystem()
  300. {
  301. m_filesystem = CE_NEW(m_allocator, Filesystem)();
  302. // DiskMountPoint* root = CE_NEW(m_allocator, DiskMountPoint)(m_preferred_root_path);
  303. AndroidMountPoint* root = CE_NEW(m_allocator, AndroidMountPoint)();
  304. m_filesystem->mount(*root);
  305. Log::d("Filesystem created.");
  306. Log::d("Filesystem root path: %s",root->root_path());
  307. }
  308. //-----------------------------------------------------------------------------
  309. void Device::create_resource_manager()
  310. {
  311. // Select appropriate resource archive
  312. if (m_preferred_mode == MODE_DEVELOPMENT)
  313. {
  314. m_resource_bundle = CE_NEW(m_allocator, FileBundle)(*m_filesystem);
  315. }
  316. else
  317. {
  318. m_resource_bundle = CE_NEW(m_allocator, ArchiveBundle)(*m_filesystem);
  319. }
  320. // Read resource seed
  321. DiskFile* seed_file = (DiskFile*)filesystem()->open("android", "seed.ini", FOM_READ);
  322. TextReader reader(*seed_file);
  323. char tmp_buf[32];
  324. reader.read_string(tmp_buf, 32);
  325. filesystem()->close(seed_file);
  326. uint32_t seed = string::parse_uint(tmp_buf);
  327. // Create resource manager
  328. m_resource_manager = CE_NEW(m_allocator, ResourceManager)(*m_resource_bundle, seed);
  329. ResourceId rid = m_resource_manager->load("wav", "beep");
  330. m_resource_manager->flush();
  331. SoundResource* res = (SoundResource*)m_resource_manager->data(rid);
  332. Log::d("Resource manager created.");
  333. Log::d("Resource seed: %d", m_resource_manager->seed());
  334. }
  335. //-----------------------------------------------------------------------------
  336. void Device::create_input_manager()
  337. {
  338. // Create input manager
  339. m_input_manager = CE_NEW(m_allocator, InputManager)();
  340. Log::d("Input manager created.");
  341. }
  342. //-----------------------------------------------------------------------------
  343. void Device::create_window()
  344. {
  345. m_window = CE_NEW(m_allocator, OsWindow)(m_preferred_window_width, m_preferred_window_height);
  346. CE_ASSERT(m_window != NULL, "Unable to create the window");
  347. m_window->set_title("Crown Game Engine");
  348. m_window->show();
  349. Log::d("Window created.");
  350. }
  351. //-----------------------------------------------------------------------------
  352. void Device::create_renderer()
  353. {
  354. m_renderer = Renderer::create(m_allocator);
  355. m_renderer->init();
  356. Log::d("Renderer created.");
  357. }
  358. //-----------------------------------------------------------------------------
  359. void Device::create_debug_renderer()
  360. {
  361. // Create debug renderer
  362. m_debug_renderer = CE_NEW(m_allocator, DebugRenderer)(*m_renderer);
  363. Log::d("Debug renderer created.");
  364. }
  365. //-----------------------------------------------------------------------------
  366. void Device::create_lua_environment()
  367. {
  368. m_lua_environment = CE_NEW(m_allocator, LuaEnvironment)();
  369. m_lua_environment->init();
  370. Log::d("Lua environment created.");
  371. }
  372. void Device::create_console_server()
  373. {
  374. m_console_server = NULL;//CE_NEW(m_allocator, ConsoleServer)();
  375. //m_console_server->init();
  376. Log::d("Console server created.");
  377. }
  378. //-----------------------------------------------------------------------------
  379. void Device::parse_command_line(int argc, char** argv)
  380. {
  381. static ArgsOption options[] =
  382. {
  383. { "help", AOA_NO_ARGUMENT, NULL, 'i' },
  384. { "root-path", AOA_REQUIRED_ARGUMENT, NULL, 'r' },
  385. { "width", AOA_REQUIRED_ARGUMENT, NULL, 'w' },
  386. { "height", AOA_REQUIRED_ARGUMENT, NULL, 'h' },
  387. { "fullscreen", AOA_NO_ARGUMENT, &m_preferred_window_fullscreen, 1 },
  388. { "dev", AOA_NO_ARGUMENT, &m_preferred_mode, MODE_DEVELOPMENT },
  389. { "quit-after-init", AOA_NO_ARGUMENT, &m_quit_after_init, 1 },
  390. { NULL, 0, NULL, 0 }
  391. };
  392. Args args(argc, argv, "", options);
  393. int32_t opt;
  394. while ((opt = args.getopt()) != -1)
  395. {
  396. switch (opt)
  397. {
  398. case 0:
  399. {
  400. break;
  401. }
  402. // Root path
  403. case 'r':
  404. {
  405. string::strncpy(m_preferred_root_path, 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. case 'i':
  421. case '?':
  422. default:
  423. {
  424. print_help_message();
  425. exit(EXIT_FAILURE);
  426. }
  427. }
  428. }
  429. }
  430. //-----------------------------------------------------------------------------
  431. void Device::check_preferred_settings()
  432. {
  433. if (!os::is_absolute_path(m_preferred_root_path))
  434. {
  435. Log::e("The root path must be absolute.");
  436. exit(EXIT_FAILURE);
  437. }
  438. if (m_preferred_window_width == 0 || m_preferred_window_height == 0)
  439. {
  440. Log::e("Window width and height must be greater than zero.");
  441. exit(EXIT_FAILURE);
  442. }
  443. }
  444. //-----------------------------------------------------------------------------
  445. void Device::read_engine_settings()
  446. {
  447. }
  448. //-----------------------------------------------------------------------------
  449. void Device::print_help_message()
  450. {
  451. os::printf(
  452. "Usage: crown [options]\n"
  453. "Options:\n\n"
  454. "All of the following options take precedence over\n"
  455. "environment variables and configuration files.\n\n"
  456. " --help Show this help.\n"
  457. " --root-path <path> Use <path> as the filesystem root path.\n"
  458. " --width <width> Set the <width> of the render window.\n"
  459. " --height <width> Set the <height> of the render window.\n"
  460. " --fullscreen Start in fullscreen.\n"
  461. " --dev Run the engine in development mode\n"
  462. " --quit-after-init Quit the engine immediately after the\n"
  463. " initialization. Used only for debugging.\n");
  464. }
  465. Device g_device;
  466. Device* device()
  467. {
  468. return &g_device;
  469. }
  470. } // namespace crown