device.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /*
  2. * Copyright (c) 2012-2016 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "array.h"
  6. #include "audio.h"
  7. #include "config.h"
  8. #include "console_server.h"
  9. #include "device.h"
  10. #include "disk_filesystem.h"
  11. #include "filesystem.h"
  12. #include "input_device.h"
  13. #include "input_manager.h"
  14. #include "log.h"
  15. #include "lua_environment.h"
  16. #include "map.h"
  17. #include "material_manager.h"
  18. #include "memory.h"
  19. #include "os.h"
  20. #include "path.h"
  21. #include "physics.h"
  22. #include "profiler.h"
  23. #include "resource_loader.h"
  24. #include "resource_manager.h"
  25. #include "resource_package.h"
  26. #include "shader_manager.h"
  27. #include "sjson.h"
  28. #include "types.h"
  29. #include "unit_manager.h"
  30. #include "vector3.h"
  31. #include "world.h"
  32. #if CROWN_PLATFORM_ANDROID
  33. #include "apk_filesystem.h"
  34. #endif // CROWN_PLATFORM_ANDROID
  35. #define MAX_SUBSYSTEMS_HEAP 8 * 1024 * 1024
  36. namespace crown
  37. {
  38. Device::Device(const DeviceOptions& opts)
  39. : _allocator(default_allocator(), MAX_SUBSYSTEMS_HEAP)
  40. , _width(0)
  41. , _height(0)
  42. , _mouse_curr_x(0)
  43. , _mouse_curr_y(0)
  44. , _mouse_last_x(0)
  45. , _mouse_last_y(0)
  46. , _is_init(false)
  47. , _is_running(false)
  48. , _is_paused(false)
  49. , _frame_count(0)
  50. , _last_time(0)
  51. , _current_time(0)
  52. , _last_delta_time(0.0f)
  53. , _time_since_start(0.0)
  54. , _device_options(opts)
  55. , _bundle_filesystem(NULL)
  56. , _boot_package_id(uint64_t(0))
  57. , _boot_script_id(uint64_t(0))
  58. , _boot_package(NULL)
  59. , _lua_environment(NULL)
  60. , _resource_loader(NULL)
  61. , _resource_manager(NULL)
  62. , _input_manager(NULL)
  63. , _shader_manager(NULL)
  64. , _material_manager(NULL)
  65. , _unit_manager(NULL)
  66. , _worlds(default_allocator())
  67. , _bgfx_allocator(default_allocator())
  68. {
  69. }
  70. void Device::init()
  71. {
  72. // Initialize
  73. CE_LOGI("Initializing Crown Engine %s...", version());
  74. profiler_globals::init();
  75. #if CROWN_PLATFORM_ANDROID
  76. _bundle_filesystem = CE_NEW(_allocator, ApkFilesystem)(const_cast<AAssetManager*>(_device_options.asset_manager()));
  77. #else
  78. _bundle_filesystem = CE_NEW(_allocator, DiskFilesystem)(_device_options.bundle_dir());
  79. #endif // CROWN_PLATFORM_ANDROID
  80. _resource_loader = CE_NEW(_allocator, ResourceLoader)(*_bundle_filesystem);
  81. _resource_manager = CE_NEW(_allocator, ResourceManager)(*_resource_loader);
  82. read_config();
  83. bgfx::init(bgfx::RendererType::Count
  84. , BGFX_PCI_ID_NONE
  85. , 0
  86. , &_bgfx_callback
  87. , &_bgfx_allocator
  88. );
  89. _shader_manager = CE_NEW(_allocator, ShaderManager)(default_allocator());
  90. _material_manager = CE_NEW(_allocator, MaterialManager)(default_allocator(), *_resource_manager);
  91. _input_manager = CE_NEW(_allocator, InputManager)(default_allocator());
  92. _unit_manager = CE_NEW(_allocator, UnitManager)(default_allocator());
  93. _lua_environment = CE_NEW(_allocator, LuaEnvironment)();
  94. audio_globals::init();
  95. physics_globals::init();
  96. _boot_package = create_resource_package(_boot_package_id);
  97. _boot_package->load();
  98. _boot_package->flush();
  99. _lua_environment->load_libs();
  100. _lua_environment->execute((LuaResource*)_resource_manager->get(SCRIPT_TYPE, _boot_script_id));
  101. _lua_environment->call_global("init", 0);
  102. _is_init = true;
  103. _is_running = true;
  104. _last_time = os::clocktime();
  105. CE_LOGD("Engine initialized");
  106. }
  107. void Device::shutdown()
  108. {
  109. CE_ASSERT(_is_init, "Engine is not initialized");
  110. _is_running = false;
  111. _is_init = false;
  112. _lua_environment->call_global("shutdown", 0);
  113. _boot_package->unload();
  114. destroy_resource_package(*_boot_package);
  115. physics_globals::shutdown();
  116. audio_globals::shutdown();
  117. CE_DELETE(_allocator, _lua_environment);
  118. CE_DELETE(_allocator, _unit_manager);
  119. CE_DELETE(_allocator, _input_manager);
  120. CE_DELETE(_allocator, _material_manager);
  121. CE_DELETE(_allocator, _shader_manager);
  122. CE_DELETE(_allocator, _resource_manager);
  123. CE_DELETE(_allocator, _resource_loader);
  124. CE_DELETE(_allocator, _bundle_filesystem);
  125. bgfx::shutdown();
  126. profiler_globals::shutdown();
  127. _allocator.clear();
  128. }
  129. void Device::quit()
  130. {
  131. _is_running = false;
  132. }
  133. void Device::pause()
  134. {
  135. _is_paused = true;
  136. CE_LOGI("Engine paused.");
  137. }
  138. void Device::unpause()
  139. {
  140. _is_paused = false;
  141. CE_LOGI("Engine unpaused.");
  142. }
  143. void Device::resolution(uint16_t& width, uint16_t& height)
  144. {
  145. width = _width;
  146. height = _height;
  147. }
  148. bool Device::is_running() const
  149. {
  150. return _is_running;
  151. }
  152. uint64_t Device::frame_count() const
  153. {
  154. return _frame_count;
  155. }
  156. float Device::last_delta_time() const
  157. {
  158. return _last_delta_time;
  159. }
  160. double Device::time_since_start() const
  161. {
  162. return _time_since_start;
  163. }
  164. void Device::update()
  165. {
  166. while (!process_events() && _is_running)
  167. {
  168. _current_time = os::clocktime();
  169. const int64_t time = _current_time - _last_time;
  170. _last_time = _current_time;
  171. const double freq = (double) os::clockfrequency();
  172. _last_delta_time = time * (1.0 / freq);
  173. _time_since_start += _last_delta_time;
  174. profiler_globals::clear();
  175. console_server_globals::update();
  176. RECORD_FLOAT("device.dt", _last_delta_time);
  177. RECORD_FLOAT("device.fps", 1.0f/_last_delta_time);
  178. if (!_is_paused)
  179. {
  180. _resource_manager->complete_requests();
  181. _lua_environment->call_global("update", 1, ARGUMENT_FLOAT, last_delta_time());
  182. _lua_environment->call_global("render", 1, ARGUMENT_FLOAT, last_delta_time());
  183. }
  184. _input_manager->update();
  185. const bgfx::Stats* stats = bgfx::getStats();
  186. RECORD_FLOAT("bgfx.gpu_time", double(stats->gpuTimeEnd - stats->gpuTimeBegin)*1000.0/stats->gpuTimerFreq);
  187. RECORD_FLOAT("bgfx.cpu_time", double(stats->cpuTimeEnd - stats->cpuTimeBegin)*1000.0/stats->cpuTimerFreq);
  188. bgfx::frame();
  189. profiler_globals::flush();
  190. _lua_environment->clear_temporaries();
  191. _frame_count++;
  192. }
  193. }
  194. void Device::render_world(World& world, CameraInstance camera)
  195. {
  196. world.render(camera);
  197. }
  198. World* Device::create_world()
  199. {
  200. World* w = CE_NEW(default_allocator(), World)(default_allocator()
  201. , *_resource_manager
  202. , *_lua_environment
  203. , *_material_manager
  204. , *_unit_manager
  205. );
  206. array::push_back(_worlds, w);
  207. return w;
  208. }
  209. void Device::destroy_world(World& w)
  210. {
  211. for (uint32_t i = 0, n = array::size(_worlds); i < n; ++i)
  212. {
  213. if (&w == _worlds[i])
  214. {
  215. CE_DELETE(default_allocator(), &w);
  216. _worlds[i] = _worlds[n - 1];
  217. array::pop_back(_worlds);
  218. return;
  219. }
  220. }
  221. CE_ASSERT(false, "Bad world");
  222. }
  223. ResourcePackage* Device::create_resource_package(StringId64 id)
  224. {
  225. return CE_NEW(default_allocator(), ResourcePackage)(id, *_resource_manager);
  226. }
  227. void Device::destroy_resource_package(ResourcePackage& rp)
  228. {
  229. CE_DELETE(default_allocator(), &rp);
  230. }
  231. void Device::reload(StringId64 type, StringId64 name)
  232. {
  233. const void* old_resource = _resource_manager->get(type, name);
  234. _resource_manager->reload(type, name);
  235. const void* new_resource = _resource_manager->get(type, name);
  236. if (type == SCRIPT_TYPE)
  237. {
  238. _lua_environment->execute((const LuaResource*)new_resource);
  239. }
  240. }
  241. ResourceManager* Device::resource_manager()
  242. {
  243. return _resource_manager;
  244. }
  245. LuaEnvironment* Device::lua_environment()
  246. {
  247. return _lua_environment;
  248. }
  249. InputManager* Device::input_manager()
  250. {
  251. return _input_manager;
  252. }
  253. ShaderManager* Device::shader_manager()
  254. {
  255. return _shader_manager;
  256. }
  257. MaterialManager* Device::material_manager()
  258. {
  259. return _material_manager;
  260. }
  261. UnitManager* Device::unit_manager()
  262. {
  263. return _unit_manager;
  264. }
  265. bool Device::process_events()
  266. {
  267. OsEvent event;
  268. bool exit = false;
  269. InputManager* im = _input_manager;
  270. const int16_t dt_x = _mouse_curr_x - _mouse_last_x;
  271. const int16_t dt_y = _mouse_curr_y - _mouse_last_y;
  272. im->mouse()->set_axis(MouseAxis::CURSOR_DELTA, vector3(dt_x, dt_y, 0.0f));
  273. _mouse_last_x = _mouse_curr_x;
  274. _mouse_last_y = _mouse_curr_y;
  275. while(next_event(event))
  276. {
  277. if (event.type == OsEvent::NONE) continue;
  278. switch (event.type)
  279. {
  280. case OsEvent::TOUCH:
  281. {
  282. const OsTouchEvent& ev = event.touch;
  283. switch (ev.type)
  284. {
  285. case OsTouchEvent::POINTER:
  286. im->touch()->set_button_state(ev.pointer_id, ev.pressed);
  287. break;
  288. case OsTouchEvent::MOVE:
  289. im->touch()->set_axis(ev.pointer_id, vector3(ev.x, ev.y, 0.0f));
  290. break;
  291. default:
  292. CE_FATAL("Unknown touch event type");
  293. break;
  294. }
  295. break;
  296. }
  297. case OsEvent::MOUSE:
  298. {
  299. const OsMouseEvent& ev = event.mouse;
  300. switch (ev.type)
  301. {
  302. case OsMouseEvent::BUTTON:
  303. im->mouse()->set_button_state(ev.button, ev.pressed);
  304. break;
  305. case OsMouseEvent::MOVE:
  306. _mouse_curr_x = ev.x;
  307. _mouse_curr_y = ev.y;
  308. im->mouse()->set_axis(MouseAxis::CURSOR, vector3(ev.x, ev.y, 0.0f));
  309. break;
  310. case OsMouseEvent::WHEEL:
  311. im->mouse()->set_axis(MouseAxis::WHEEL, vector3(0.0f, ev.wheel, 0.0f));
  312. break;
  313. default:
  314. CE_FATAL("Unknown mouse event type");
  315. break;
  316. }
  317. break;
  318. }
  319. case OsEvent::KEYBOARD:
  320. {
  321. const OsKeyboardEvent& ev = event.keyboard;
  322. im->keyboard()->set_button_state(ev.button, ev.pressed);
  323. break;
  324. }
  325. case OsEvent::JOYPAD:
  326. {
  327. const OsJoypadEvent& ev = event.joypad;
  328. switch (ev.type)
  329. {
  330. case OsJoypadEvent::CONNECTED:
  331. im->joypad(ev.index)->set_connected(ev.connected);
  332. break;
  333. case OsJoypadEvent::BUTTON:
  334. im->joypad(ev.index)->set_button_state(ev.button, ev.pressed);
  335. break;
  336. case OsJoypadEvent::AXIS:
  337. im->joypad(ev.index)->set_axis(ev.button, vector3(ev.x, ev.y, ev.z));
  338. break;
  339. default:
  340. CE_FATAL("Unknown joypad event");
  341. break;
  342. }
  343. break;
  344. }
  345. case OsEvent::METRICS:
  346. {
  347. const OsMetricsEvent& ev = event.metrics;
  348. _width = ev.width;
  349. _height = ev.height;
  350. bgfx::reset(ev.width, ev.height, BGFX_RESET_VSYNC);
  351. break;
  352. }
  353. case OsEvent::EXIT:
  354. {
  355. exit = true;
  356. break;
  357. }
  358. case OsEvent::PAUSE:
  359. {
  360. pause();
  361. break;
  362. }
  363. case OsEvent::RESUME:
  364. {
  365. unpause();
  366. break;
  367. }
  368. default:
  369. {
  370. CE_FATAL("Unknown Os Event");
  371. break;
  372. }
  373. }
  374. }
  375. return exit;
  376. }
  377. void Device::read_config()
  378. {
  379. TempAllocator4096 ta;
  380. DynamicString project_path(ta);
  381. if (_device_options.project() != NULL)
  382. {
  383. project_path += _device_options.project();
  384. project_path += '/';
  385. }
  386. project_path += CROWN_BOOT_CONFIG;
  387. const StringId64 config_name(project_path.c_str());
  388. _resource_manager->load(CONFIG_TYPE, config_name);
  389. _resource_manager->flush();
  390. const char* cfile = (const char*)_resource_manager->get(CONFIG_TYPE, config_name);
  391. JsonObject config(ta);
  392. sjson::parse(cfile, config);
  393. _boot_script_id = sjson::parse_resource_id(config["boot_script"]);
  394. _boot_package_id = sjson::parse_resource_id(config["boot_package"]);
  395. _resource_manager->unload(CONFIG_TYPE, config_name);
  396. }
  397. char _buffer[sizeof(Device)];
  398. Device* _device = NULL;
  399. void init(const DeviceOptions& opts)
  400. {
  401. CE_ASSERT(_device == NULL, "Crown already initialized");
  402. _device = new (_buffer) Device(opts);
  403. _device->init();
  404. }
  405. void update()
  406. {
  407. _device->update();
  408. }
  409. void shutdown()
  410. {
  411. _device->shutdown();
  412. _device->~Device();
  413. _device = NULL;
  414. }
  415. Device* device()
  416. {
  417. return crown::_device;
  418. }
  419. } // namespace crown