device.cpp 10.0 KB

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