Device.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. /*
  2. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include "Config.h"
  23. #include "Device.h"
  24. #include "Filesystem.h"
  25. #include "InputManager.h"
  26. #include "Log.h"
  27. #include "OS.h"
  28. #include "Renderer.h"
  29. #include "DebugRenderer.h"
  30. #include "Types.h"
  31. #include "String.h"
  32. #include "Args.h"
  33. #include "Game.h"
  34. #include <cstdlib>
  35. #include "ArchiveResourceArchive.h"
  36. #include "FileResourceArchive.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 "ScriptSystem.h"
  44. #ifdef CROWN_BUILD_OPENGL
  45. #include "renderers/gl/GLRenderer.h"
  46. #endif
  47. #ifdef CROWN_BUILD_OPENGLES
  48. #include "renderers/gles/GLESRenderer.h"
  49. #endif
  50. namespace crown
  51. {
  52. static void (*game_init)(void) = NULL;
  53. static void (*game_shutdown)(void) = NULL;
  54. static void (*game_frame)(float) = NULL;
  55. //-----------------------------------------------------------------------------
  56. Device::Device() :
  57. m_preferred_window_width(1000),
  58. m_preferred_window_height(625),
  59. m_preferred_window_fullscreen(0),
  60. m_preferred_renderer(RENDERER_GL),
  61. m_preferred_mode(MODE_RELEASE),
  62. m_is_init(false),
  63. m_is_running(false),
  64. m_frame_count(0),
  65. m_last_time(0),
  66. m_current_time(0),
  67. m_last_delta_time(0.0f),
  68. m_filesystem(NULL),
  69. m_input_manager(NULL),
  70. m_renderer(NULL),
  71. m_debug_renderer(NULL),
  72. m_script_system(NULL),
  73. m_resource_manager(NULL),
  74. m_resource_archive(NULL),
  75. m_game_library(NULL)
  76. {
  77. string::strcpy(m_preferred_root_path, string::EMPTY);
  78. string::strcpy(m_preferred_user_path, string::EMPTY);
  79. }
  80. //-----------------------------------------------------------------------------
  81. Device::~Device()
  82. {
  83. }
  84. //-----------------------------------------------------------------------------
  85. bool Device::init(int argc, char** argv)
  86. {
  87. if (is_init())
  88. {
  89. Log::e("Crown Engine is already initialized.");
  90. return false;
  91. }
  92. if (parse_command_line(argc, argv) == false)
  93. {
  94. return false;
  95. }
  96. // Initialize
  97. Log::i("Initializing Crown Engine %d.%d.%d...", CROWN_VERSION_MAJOR, CROWN_VERSION_MINOR, CROWN_VERSION_MICRO);
  98. create_filesystem();
  99. Log::d("Filesystem created.");
  100. Log::d("Filesystem root path: %s", m_filesystem->root_path());
  101. create_resource_manager();
  102. Log::d("Resource manager created.");
  103. create_input_manager();
  104. Log::d("Input manager created.");
  105. create_renderer();
  106. Log::d("Renderer created.");
  107. create_debug_renderer();
  108. Log::d("Debug renderer created.");
  109. create_script_system();
  110. Log::d("Script system created.");
  111. Log::i("Crown Engine initialized.");
  112. Log::i("Initializing Game...");
  113. // Try to locate the game library
  114. if (!m_filesystem->exists(GAME_LIBRARY_NAME))
  115. {
  116. Log::e("Unable to find the game library in the root path.", GAME_LIBRARY_NAME);
  117. return false;
  118. }
  119. // Try to load the game library and bind functions
  120. const char* game_library_path = m_filesystem->os_path(GAME_LIBRARY_NAME);
  121. m_game_library = os::open_library(game_library_path);
  122. if (m_game_library == NULL)
  123. {
  124. Log::e("Unable to load the game.");
  125. return false;
  126. }
  127. *(void**)(&game_init) = os::lookup_symbol(m_game_library, "init");
  128. *(void**)(&game_shutdown) = os::lookup_symbol(m_game_library, "shutdown");
  129. *(void**)(&game_frame) = os::lookup_symbol(m_game_library, "frame");
  130. // Initialize the game
  131. game_init();
  132. m_is_init = true;
  133. start();
  134. return true;
  135. }
  136. //-----------------------------------------------------------------------------
  137. void Device::shutdown()
  138. {
  139. if (is_init() == false)
  140. {
  141. Log::e("Crown Engine is not initialized.");
  142. return;
  143. }
  144. // Shutdowns the game
  145. game_shutdown();
  146. // Unload the game library
  147. if (m_game_library)
  148. {
  149. os::close_library(m_game_library);
  150. }
  151. if (m_input_manager)
  152. {
  153. delete m_input_manager;
  154. }
  155. Log::i("Releasing Renderer...");
  156. if (m_renderer)
  157. {
  158. delete m_renderer;
  159. }
  160. Log::i("Releasing DebugRenderer...");
  161. if (m_debug_renderer)
  162. {
  163. delete m_debug_renderer;
  164. }
  165. Log::i("Releasing ResourceManager...");
  166. if (m_resource_archive)
  167. {
  168. delete m_resource_archive;
  169. }
  170. if (m_resource_manager)
  171. {
  172. delete m_resource_manager;
  173. }
  174. Log::i("Releasing Filesystem...");
  175. if (m_filesystem)
  176. {
  177. delete m_filesystem;
  178. }
  179. m_is_init = false;
  180. }
  181. //-----------------------------------------------------------------------------
  182. bool Device::is_init() const
  183. {
  184. return m_is_init;
  185. }
  186. //-----------------------------------------------------------------------------
  187. Filesystem* Device::filesystem()
  188. {
  189. return m_filesystem;
  190. }
  191. //-----------------------------------------------------------------------------
  192. ResourceManager* Device::resource_manager()
  193. {
  194. return m_resource_manager;
  195. }
  196. //-----------------------------------------------------------------------------
  197. InputManager* Device::input_manager()
  198. {
  199. return m_input_manager;
  200. }
  201. //-----------------------------------------------------------------------------
  202. Renderer* Device::renderer()
  203. {
  204. return m_renderer;
  205. }
  206. //-----------------------------------------------------------------------------
  207. DebugRenderer* Device::debug_renderer()
  208. {
  209. return m_debug_renderer;
  210. }
  211. //-----------------------------------------------------------------------------
  212. ScriptSystem* Device::script_system()
  213. {
  214. return m_script_system;
  215. }
  216. //-----------------------------------------------------------------------------
  217. Keyboard* Device::keyboard()
  218. {
  219. return m_input_manager->keyboard();
  220. }
  221. //-----------------------------------------------------------------------------
  222. Mouse* Device::mouse()
  223. {
  224. return m_input_manager->mouse();
  225. }
  226. //-----------------------------------------------------------------------------
  227. Touch* Device::touch()
  228. {
  229. return m_input_manager->touch();
  230. }
  231. //-----------------------------------------------------------------------------
  232. Accelerometer* Device::accelerometer()
  233. {
  234. return m_input_manager->accelerometer();
  235. }
  236. //-----------------------------------------------------------------------------
  237. void Device::start()
  238. {
  239. if (is_init() == false)
  240. {
  241. Log::e("Cannot start uninitialized engine.");
  242. return;
  243. }
  244. m_is_running = true;
  245. m_last_time = os::milliseconds();
  246. }
  247. //-----------------------------------------------------------------------------
  248. void Device::stop()
  249. {
  250. if (is_init() == false)
  251. {
  252. Log::e("Cannot stop uninitialized engine.");
  253. return;
  254. }
  255. m_is_running = false;
  256. }
  257. //-----------------------------------------------------------------------------
  258. bool Device::is_running() const
  259. {
  260. return m_is_running;
  261. }
  262. //-----------------------------------------------------------------------------
  263. uint64_t Device::frame_count() const
  264. {
  265. return m_frame_count;
  266. }
  267. //-----------------------------------------------------------------------------
  268. float Device::last_delta_time() const
  269. {
  270. return m_last_delta_time;
  271. }
  272. //-----------------------------------------------------------------------------
  273. void Device::frame()
  274. {
  275. m_current_time = os::microseconds();
  276. m_last_delta_time = (m_current_time - m_last_time) / 1000000.0f;
  277. m_last_time = m_current_time;
  278. m_resource_manager->check_load_queue();
  279. m_resource_manager->bring_loaded_online();
  280. m_script_system->execute();
  281. m_input_manager->event_loop();
  282. m_renderer->begin_frame();
  283. game_frame(last_delta_time());
  284. m_debug_renderer->draw_all();
  285. m_renderer->end_frame();
  286. m_frame_count++;
  287. }
  288. //-----------------------------------------------------------------------------
  289. ResourceId Device::load(const char* name)
  290. {
  291. return m_resource_manager->load(name);
  292. }
  293. //-----------------------------------------------------------------------------
  294. void Device::unload(ResourceId name)
  295. {
  296. m_resource_manager->unload(name);
  297. }
  298. //-----------------------------------------------------------------------------
  299. void Device::reload(ResourceId name)
  300. {
  301. const void* old_resource = m_resource_manager->data(name);
  302. m_resource_manager->reload(name);
  303. const void* new_resource = m_resource_manager->data(name);
  304. }
  305. //-----------------------------------------------------------------------------
  306. bool Device::is_loaded(ResourceId name)
  307. {
  308. return m_resource_manager->is_loaded(name);
  309. }
  310. //-----------------------------------------------------------------------------
  311. const void* Device::data(ResourceId name)
  312. {
  313. return m_resource_manager->data(name);
  314. }
  315. //-----------------------------------------------------------------------------
  316. void Device::create_filesystem()
  317. {
  318. // Select current dir if no root path provided
  319. if (string::strcmp(m_preferred_root_path, string::EMPTY) == 0)
  320. {
  321. m_filesystem = new Filesystem(os::get_cwd());
  322. }
  323. else
  324. {
  325. m_filesystem = new Filesystem(m_preferred_root_path);
  326. }
  327. }
  328. //-----------------------------------------------------------------------------
  329. void Device::create_resource_manager()
  330. {
  331. // Select appropriate resource archive
  332. if (m_preferred_mode == MODE_DEVELOPMENT)
  333. {
  334. m_resource_archive = new FileResourceArchive(*m_filesystem);
  335. }
  336. else
  337. {
  338. m_resource_archive = new ArchiveResourceArchive(*m_filesystem);
  339. }
  340. // Create resource manager
  341. m_resource_manager = new ResourceManager(*m_resource_archive, m_resource_allocator);
  342. }
  343. //-----------------------------------------------------------------------------
  344. void Device::create_input_manager()
  345. {
  346. // Create input manager
  347. m_input_manager = new InputManager();
  348. }
  349. //-----------------------------------------------------------------------------
  350. void Device::create_renderer()
  351. {
  352. // Select appropriate renderer
  353. if (m_preferred_renderer == RENDERER_GL)
  354. {
  355. #ifdef CROWN_BUILD_OPENGL
  356. m_renderer = new GLRenderer;
  357. #else
  358. Log::e("Crown Engine was not built with OpenGL support.");
  359. assert(false);
  360. #endif
  361. }
  362. else if (m_preferred_renderer == RENDERER_GLES)
  363. {
  364. #ifdef CROWN_BUILD_OPENGLES
  365. m_renderer = new GLESRenderer;
  366. #else
  367. Log::e("Crown Engine was not built with OpenGL|ES support.");
  368. assert(false);
  369. #endif
  370. }
  371. }
  372. //-----------------------------------------------------------------------------
  373. void Device::create_debug_renderer()
  374. {
  375. // Create debug renderer
  376. m_debug_renderer = new DebugRenderer(*m_renderer);
  377. }
  378. //-----------------------------------------------------------------------------
  379. void Device::create_script_system()
  380. {
  381. m_script_system = scripter();
  382. }
  383. //-----------------------------------------------------------------------------
  384. bool Device::parse_command_line(int argc, char** argv)
  385. {
  386. ArgsOption options[] =
  387. {
  388. "help", AOA_NO_ARGUMENT, NULL, 'i',
  389. "root-path", AOA_REQUIRED_ARGUMENT, NULL, 'r',
  390. "user-path", AOA_REQUIRED_ARGUMENT, NULL, 'u',
  391. "width", AOA_REQUIRED_ARGUMENT, NULL, 'w',
  392. "height", AOA_REQUIRED_ARGUMENT, NULL, 'h',
  393. "fullscreen", AOA_NO_ARGUMENT, &m_preferred_window_fullscreen, 1,
  394. "gl", AOA_NO_ARGUMENT, &m_preferred_renderer, RENDERER_GL,
  395. "gles", AOA_NO_ARGUMENT, &m_preferred_renderer, RENDERER_GLES,
  396. "dev", AOA_NO_ARGUMENT, &m_preferred_mode, MODE_DEVELOPMENT,
  397. NULL, 0, NULL, 0
  398. };
  399. Args args(argc, argv, "", options);
  400. while (1)
  401. {
  402. int32_t ret = args.next_option();
  403. switch (ret)
  404. {
  405. case -1:
  406. {
  407. return true;
  408. }
  409. // Help
  410. case 'i':
  411. {
  412. print_help_message();
  413. return false;
  414. }
  415. // Root path
  416. case 'r':
  417. {
  418. if (args.option_argument() == NULL)
  419. {
  420. os::printf("%s: error: missing absolute path after `-root-path`\n", argv[0]);
  421. return false;
  422. }
  423. if (!os::is_absolute_path(args.option_argument()))
  424. {
  425. os::printf("%s: error: the root path must be absolute.\n", argv[0]);
  426. return false;
  427. }
  428. string::strcpy(m_preferred_root_path, args.option_argument());
  429. break;
  430. }
  431. // User path
  432. case 'u':
  433. {
  434. if (args.option_argument() == NULL)
  435. {
  436. os::printf("%s: error: missing absolute path after `--user-path`\n", argv[0]);
  437. return false;
  438. }
  439. if (!os::is_absolute_path(args.option_argument()))
  440. {
  441. os::printf("%s: error: the user path must be absolute.\n", argv[0]);
  442. return false;
  443. }
  444. string::strcpy(m_preferred_user_path, args.option_argument());
  445. break;
  446. }
  447. // Window width
  448. case 'w':
  449. {
  450. if (args.option_argument() == NULL)
  451. {
  452. os::printf("%s: error: missing width value after `--width`\n", argv[0]);
  453. return false;
  454. }
  455. m_preferred_window_width = atoi(args.option_argument());
  456. break;
  457. }
  458. // Window height
  459. case 'h':
  460. {
  461. if (args.option_argument() == NULL)
  462. {
  463. os::printf("%s: error: missing height value after `--height`\n", argv[0]);
  464. return false;
  465. }
  466. m_preferred_window_height = atoi(args.option_argument());
  467. break;
  468. }
  469. default:
  470. {
  471. break;
  472. }
  473. }
  474. }
  475. return true;
  476. }
  477. //-----------------------------------------------------------------------------
  478. void Device::print_help_message()
  479. {
  480. os::printf(
  481. "Usage: crown [options]\n"
  482. "Options:\n\n"
  483. "All of the following options take precedence over\n"
  484. "environment variables and configuration files.\n\n"
  485. " --help Show this help.\n"
  486. " --root-path <path> Use <path> as the filesystem root path.\n"
  487. " --user-path <path> Use <path> as the filesystem user path.\n"
  488. " --width <width> Set the <width> of the render window.\n"
  489. " --height <width> Set the <height> of the render window.\n"
  490. " --fullscreen Start in fullscreen.\n"
  491. " --gl Use OpenGL as rendering backend.\n"
  492. " --gles Use OpenGL|ES as rendering backend.\n"
  493. " --dev Run the engine in development mode\n");
  494. }
  495. Device g_device;
  496. Device* device()
  497. {
  498. return &g_device;
  499. }
  500. } // namespace crown