Device.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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 "String.h"
  34. #include "Args.h"
  35. #include "Game.h"
  36. #include <cstdlib>
  37. #include "ArchiveBundle.h"
  38. #include "FileBundle.h"
  39. #include "ResourceManager.h"
  40. #include "TextureResource.h"
  41. #include "Keyboard.h"
  42. #include "Mouse.h"
  43. #include "Touch.h"
  44. #include "Accelerometer.h"
  45. #include "JSONParser.h"
  46. #include "DiskFile.h"
  47. #include "Memory.h"
  48. #ifdef CROWN_BUILD_OPENGL
  49. #include "renderers/gl/GLRenderer.h"
  50. #endif
  51. #ifdef CROWN_BUILD_OPENGLES
  52. #include "renderers/gles/GLESRenderer.h"
  53. #endif
  54. namespace crown
  55. {
  56. static void (*game_init)(void) = NULL;
  57. static void (*game_shutdown)(void) = NULL;
  58. static void (*game_frame)(float) = NULL;
  59. //-----------------------------------------------------------------------------
  60. Device::Device() :
  61. m_allocator(m_subsystems_heap, MAX_SUBSYSTEMS_HEAP),
  62. m_preferred_window_width(1000),
  63. m_preferred_window_height(625),
  64. m_preferred_window_fullscreen(0),
  65. m_preferred_renderer(RENDERER_GL),
  66. m_preferred_mode(MODE_RELEASE),
  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_renderer(NULL),
  77. m_debug_renderer(NULL),
  78. m_resource_manager(NULL),
  79. m_resource_bundle(NULL),
  80. m_game_library(NULL)
  81. {
  82. // Select executable dir by default
  83. string::strncpy(m_preferred_root_path, os::get_cwd(), os::MAX_PATH_LENGTH);
  84. }
  85. //-----------------------------------------------------------------------------
  86. Device::~Device()
  87. {
  88. }
  89. //-----------------------------------------------------------------------------
  90. bool Device::init(int argc, char** argv)
  91. {
  92. if (is_init())
  93. {
  94. Log::e("Crown Engine is already initialized.");
  95. return false;
  96. }
  97. parse_command_line(argc, argv);
  98. check_preferred_settings();
  99. // Initialize
  100. Log::i("Initializing Crown Engine %d.%d.%d...", CROWN_VERSION_MAJOR, CROWN_VERSION_MINOR, CROWN_VERSION_MICRO);
  101. create_filesystem();
  102. create_resource_manager();
  103. create_input_manager();
  104. create_renderer();
  105. create_debug_renderer();
  106. read_engine_settings();
  107. Log::i("Crown Engine initialized.");
  108. Log::i("Initializing Game...");
  109. // Try to locate the game library
  110. if (!m_filesystem->exists(GAME_LIBRARY_NAME))
  111. {
  112. Log::e("Unable to find the game library in the root path.", GAME_LIBRARY_NAME);
  113. return false;
  114. }
  115. // Try to load the game library and bind functions
  116. const char* game_library_path = m_filesystem->os_path(GAME_LIBRARY_NAME);
  117. m_game_library = os::open_library(game_library_path);
  118. if (m_game_library == NULL)
  119. {
  120. Log::e("Unable to load the game.");
  121. return false;
  122. }
  123. *(void**)(&game_init) = os::lookup_symbol(m_game_library, "init");
  124. *(void**)(&game_shutdown) = os::lookup_symbol(m_game_library, "shutdown");
  125. *(void**)(&game_frame) = os::lookup_symbol(m_game_library, "frame");
  126. // Initialize the game
  127. game_init();
  128. m_is_init = true;
  129. start();
  130. if (m_quit_after_init == 1)
  131. {
  132. shutdown();
  133. }
  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. CE_DELETE(m_allocator, m_input_manager);
  154. }
  155. Log::i("Releasing Renderer...");
  156. if (m_renderer)
  157. {
  158. CE_DELETE(m_allocator, m_renderer);
  159. }
  160. Log::i("Releasing DebugRenderer...");
  161. if (m_debug_renderer)
  162. {
  163. CE_DELETE(m_allocator, m_debug_renderer);
  164. }
  165. Log::i("Releasing ResourceManager...");
  166. if (m_resource_bundle)
  167. {
  168. CE_DELETE(m_allocator, m_resource_bundle);
  169. }
  170. if (m_resource_manager)
  171. {
  172. CE_DELETE(m_allocator, m_resource_manager);
  173. }
  174. Log::i("Releasing Filesystem...");
  175. if (m_filesystem)
  176. {
  177. CE_DELETE(m_allocator, m_filesystem);
  178. }
  179. m_allocator.clear();
  180. m_is_init = false;
  181. }
  182. //-----------------------------------------------------------------------------
  183. bool Device::is_init() const
  184. {
  185. return m_is_init;
  186. }
  187. //-----------------------------------------------------------------------------
  188. Filesystem* Device::filesystem()
  189. {
  190. return m_filesystem;
  191. }
  192. //-----------------------------------------------------------------------------
  193. ResourceManager* Device::resource_manager()
  194. {
  195. return m_resource_manager;
  196. }
  197. //-----------------------------------------------------------------------------
  198. InputManager* Device::input_manager()
  199. {
  200. return m_input_manager;
  201. }
  202. //-----------------------------------------------------------------------------
  203. Renderer* Device::renderer()
  204. {
  205. return m_renderer;
  206. }
  207. //-----------------------------------------------------------------------------
  208. DebugRenderer* Device::debug_renderer()
  209. {
  210. return m_debug_renderer;
  211. }
  212. //-----------------------------------------------------------------------------
  213. Keyboard* Device::keyboard()
  214. {
  215. return m_input_manager->keyboard();
  216. }
  217. //-----------------------------------------------------------------------------
  218. Mouse* Device::mouse()
  219. {
  220. return m_input_manager->mouse();
  221. }
  222. //-----------------------------------------------------------------------------
  223. Touch* Device::touch()
  224. {
  225. return m_input_manager->touch();
  226. }
  227. //-----------------------------------------------------------------------------
  228. Accelerometer* Device::accelerometer()
  229. {
  230. return m_input_manager->accelerometer();
  231. }
  232. //-----------------------------------------------------------------------------
  233. void Device::start()
  234. {
  235. if (is_init() == false)
  236. {
  237. Log::e("Cannot start uninitialized engine.");
  238. return;
  239. }
  240. m_is_running = true;
  241. m_last_time = os::milliseconds();
  242. }
  243. //-----------------------------------------------------------------------------
  244. void Device::stop()
  245. {
  246. if (is_init() == false)
  247. {
  248. Log::e("Cannot stop uninitialized engine.");
  249. return;
  250. }
  251. m_is_running = false;
  252. }
  253. //-----------------------------------------------------------------------------
  254. bool Device::is_running() const
  255. {
  256. return m_is_running;
  257. }
  258. //-----------------------------------------------------------------------------
  259. uint64_t Device::frame_count() const
  260. {
  261. return m_frame_count;
  262. }
  263. //-----------------------------------------------------------------------------
  264. float Device::last_delta_time() const
  265. {
  266. return m_last_delta_time;
  267. }
  268. //-----------------------------------------------------------------------------
  269. void Device::frame()
  270. {
  271. m_current_time = os::microseconds();
  272. m_last_delta_time = (m_current_time - m_last_time) / 1000000.0f;
  273. m_last_time = m_current_time;
  274. m_resource_manager->check_load_queue();
  275. m_resource_manager->bring_loaded_online();
  276. m_input_manager->event_loop();
  277. m_renderer->begin_frame();
  278. game_frame(last_delta_time());
  279. m_debug_renderer->draw_all();
  280. m_renderer->end_frame();
  281. m_frame_count++;
  282. }
  283. //-----------------------------------------------------------------------------
  284. ResourceId Device::load(const char* name)
  285. {
  286. return m_resource_manager->load(name);
  287. }
  288. //-----------------------------------------------------------------------------
  289. void Device::unload(ResourceId name)
  290. {
  291. m_resource_manager->unload(name);
  292. }
  293. //-----------------------------------------------------------------------------
  294. void Device::reload(ResourceId name)
  295. {
  296. (void)name;
  297. }
  298. //-----------------------------------------------------------------------------
  299. bool Device::is_loaded(ResourceId name)
  300. {
  301. return m_resource_manager->is_loaded(name);
  302. }
  303. //-----------------------------------------------------------------------------
  304. const void* Device::data(ResourceId name)
  305. {
  306. return m_resource_manager->data(name);
  307. }
  308. //-----------------------------------------------------------------------------
  309. void Device::create_filesystem()
  310. {
  311. m_filesystem = CE_NEW(m_allocator, Filesystem)(m_preferred_root_path);
  312. Log::d("Filesystem created.");
  313. Log::d("Filesystem root path: %s", m_filesystem->root_path());
  314. }
  315. //-----------------------------------------------------------------------------
  316. void Device::create_resource_manager()
  317. {
  318. // Select appropriate resource archive
  319. if (m_preferred_mode == MODE_DEVELOPMENT)
  320. {
  321. m_resource_bundle = CE_NEW(m_allocator, FileBundle)(*m_filesystem);
  322. }
  323. else
  324. {
  325. m_resource_bundle = CE_NEW(m_allocator, ArchiveBundle)(*m_filesystem);
  326. }
  327. // Create resource manager
  328. m_resource_manager = CE_NEW(m_allocator, ResourceManager)(*m_resource_bundle);
  329. Log::d("Resource manager created.");
  330. Log::d("Resource seed: %d", m_resource_manager->seed());
  331. }
  332. //-----------------------------------------------------------------------------
  333. void Device::create_input_manager()
  334. {
  335. // Create input manager
  336. m_input_manager = CE_NEW(m_allocator, InputManager)();
  337. Log::d("Input manager created.");
  338. }
  339. //-----------------------------------------------------------------------------
  340. void Device::create_renderer()
  341. {
  342. // Select appropriate renderer
  343. if (m_preferred_renderer == RENDERER_GL)
  344. {
  345. #ifdef CROWN_BUILD_OPENGL
  346. m_renderer = CE_NEW(m_allocator, GLRenderer)();
  347. #else
  348. Log::e("Crown Engine was not built with OpenGL support.");
  349. exit(EXIT_FAILURE);
  350. #endif
  351. }
  352. else if (m_preferred_renderer == RENDERER_GLES)
  353. {
  354. #ifdef CROWN_BUILD_OPENGLES
  355. m_renderer = CE_NEW(m_allocator, GLESRenderer)();
  356. #else
  357. Log::e("Crown Engine was not built with OpenGL|ES support.");
  358. exit(EXIT_FAILURE);
  359. #endif
  360. }
  361. Log::d("Renderer created.");
  362. }
  363. //-----------------------------------------------------------------------------
  364. void Device::create_debug_renderer()
  365. {
  366. // Create debug renderer
  367. m_debug_renderer = CE_NEW(m_allocator, DebugRenderer)(*m_renderer);
  368. Log::d("Debug renderer created.");
  369. }
  370. //-----------------------------------------------------------------------------
  371. void Device::parse_command_line(int argc, char** argv)
  372. {
  373. static ArgsOption options[] =
  374. {
  375. "help", AOA_NO_ARGUMENT, NULL, 'i',
  376. "root-path", AOA_REQUIRED_ARGUMENT, NULL, 'r',
  377. "width", AOA_REQUIRED_ARGUMENT, NULL, 'w',
  378. "height", AOA_REQUIRED_ARGUMENT, NULL, 'h',
  379. "fullscreen", AOA_NO_ARGUMENT, &m_preferred_window_fullscreen, 1,
  380. "gl", AOA_NO_ARGUMENT, &m_preferred_renderer, RENDERER_GL,
  381. "gles", AOA_NO_ARGUMENT, &m_preferred_renderer, RENDERER_GLES,
  382. "dev", AOA_NO_ARGUMENT, &m_preferred_mode, MODE_DEVELOPMENT,
  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. // Root path
  397. case 'r':
  398. {
  399. string::strcpy(m_preferred_root_path, args.optarg());
  400. break;
  401. }
  402. // Window width
  403. case 'w':
  404. {
  405. m_preferred_window_width = atoi(args.optarg());
  406. break;
  407. }
  408. // Window height
  409. case 'h':
  410. {
  411. m_preferred_window_height = atoi(args.optarg());
  412. break;
  413. }
  414. case 'i':
  415. case '?':
  416. default:
  417. {
  418. print_help_message();
  419. exit(EXIT_FAILURE);
  420. }
  421. }
  422. }
  423. }
  424. //-----------------------------------------------------------------------------
  425. void Device::check_preferred_settings()
  426. {
  427. if (!os::is_absolute_path(m_preferred_root_path))
  428. {
  429. Log::e("The root path must be absolute.");
  430. exit(EXIT_FAILURE);
  431. }
  432. if (m_preferred_window_width == 0 || m_preferred_window_height == 0)
  433. {
  434. Log::e("Window width and height must be greater than zero.");
  435. exit(EXIT_FAILURE);
  436. }
  437. }
  438. //-----------------------------------------------------------------------------
  439. void Device::read_engine_settings()
  440. {
  441. // DiskFile* file = m_filesystem->open("crown.cfg", FOM_READ);
  442. // HeapAllocator allocator;
  443. // JSONParser json(allocator, file);
  444. // json.get_root().get_number("width").to_int(m_preferred_window_width);
  445. // json.get_root().get_number("height").to_int(m_preferred_window_height);
  446. // m_filesystem->close(file);
  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