device.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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 "json_parser.h"
  21. #include "filesystem.h"
  22. #include "path.h"
  23. #include "disk_filesystem.h"
  24. #include "physics.h"
  25. #include "audio.h"
  26. #include "profiler.h"
  27. #include "console_server.h"
  28. #include "input_device.h"
  29. #include "profiler.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. #if CROWN_PLATFORM_ANDROID
  70. _bundle_filesystem = CE_NEW(_allocator, ApkFilesystem)(_device_options.asset_manager());
  71. #else
  72. _bundle_filesystem = CE_NEW(_allocator, DiskFilesystem)(_device_options.bundle_dir());
  73. #endif // CROWN_PLATFORM_ANDROID
  74. read_config();
  75. profiler_globals::init();
  76. audio_globals::init();
  77. physics_globals::init();
  78. bgfx::init(bgfx::RendererType::Count
  79. , BGFX_PCI_ID_NONE
  80. , 0
  81. , &_bgfx_callback
  82. , &_bgfx_allocator
  83. );
  84. _resource_loader = CE_NEW(_allocator, ResourceLoader)(*_bundle_filesystem);
  85. _resource_manager = CE_NEW(_allocator, ResourceManager)(*_resource_loader);
  86. CE_LOGD("Creating material manager...");
  87. material_manager::init();
  88. debug_line::init();
  89. _lua_environment = CE_NEW(_allocator, LuaEnvironment)();
  90. _lua_environment->load_libs();
  91. _input_manager = CE_NEW(_allocator, InputManager)();
  92. CE_LOGD("Crown Engine initialized.");
  93. CE_LOGD("Initializing Game...");
  94. _is_init = true;
  95. _is_running = true;
  96. _last_time = os::clocktime();
  97. _boot_package = create_resource_package(_boot_package_id);
  98. _boot_package->load();
  99. _boot_package->flush();
  100. _lua_environment->execute((LuaResource*)_resource_manager->get(SCRIPT_TYPE, _boot_script_id));
  101. _lua_environment->call_global("init", 0);
  102. }
  103. void Device::shutdown()
  104. {
  105. CE_ASSERT(_is_init, "Engine is not initialized");
  106. CE_DELETE(_allocator, _input_manager);
  107. // Shutdowns the game
  108. _lua_environment->call_global("shutdown", 0);
  109. _boot_package->unload();
  110. destroy_resource_package(*_boot_package);
  111. CE_DELETE(_allocator, _lua_environment);
  112. CE_LOGD("Releasing material manager...");
  113. debug_line::shutdown();
  114. material_manager::shutdown();
  115. CE_DELETE(_allocator, _resource_manager);
  116. CE_DELETE(_allocator, _resource_loader);
  117. bgfx::shutdown();
  118. physics_globals::shutdown();
  119. audio_globals::shutdown();
  120. profiler_globals::shutdown();
  121. CE_DELETE(_allocator, _bundle_filesystem);
  122. _allocator.clear();
  123. _is_init = false;
  124. }
  125. void Device::quit()
  126. {
  127. _is_running = false;
  128. }
  129. void Device::pause()
  130. {
  131. _is_paused = true;
  132. CE_LOGI("Engine paused.");
  133. }
  134. void Device::unpause()
  135. {
  136. _is_paused = false;
  137. CE_LOGI("Engine unpaused.");
  138. }
  139. void Device::resolution(uint16_t& width, uint16_t& height)
  140. {
  141. width = _width;
  142. height = _height;
  143. }
  144. bool Device::is_running() const
  145. {
  146. return _is_running;
  147. }
  148. uint64_t Device::frame_count() const
  149. {
  150. return _frame_count;
  151. }
  152. float Device::last_delta_time() const
  153. {
  154. return _last_delta_time;
  155. }
  156. double Device::time_since_start() const
  157. {
  158. return _time_since_start;
  159. }
  160. void Device::update()
  161. {
  162. while (!process_events() && _is_running)
  163. {
  164. _current_time = os::clocktime();
  165. const int64_t time = _current_time - _last_time;
  166. _last_time = _current_time;
  167. const double freq = (double) os::clockfrequency();
  168. _last_delta_time = time * (1.0 / freq);
  169. _time_since_start += _last_delta_time;
  170. profiler_globals::clear();
  171. console_server_globals::update();
  172. RECORD_FLOAT("device.dt", _last_delta_time);
  173. RECORD_FLOAT("device.fps", 1.0f/_last_delta_time);
  174. if (!_is_paused)
  175. {
  176. _resource_manager->complete_requests();
  177. _lua_environment->call_global("update", 1, ARGUMENT_FLOAT, last_delta_time());
  178. _lua_environment->call_global("render", 1, ARGUMENT_FLOAT, last_delta_time());
  179. }
  180. _input_manager->update();
  181. const bgfx::Stats* stats = bgfx::getStats();
  182. RECORD_FLOAT("bgfx.gpu_time", double(stats->gpuTimeEnd - stats->gpuTimeBegin)*1000.0/stats->gpuTimerFreq);
  183. RECORD_FLOAT("bgfx.cpu_time", double(stats->cpuTimeEnd - stats->cpuTimeBegin)*1000.0/stats->cpuTimerFreq);
  184. bgfx::frame();
  185. profiler_globals::flush();
  186. _lua_environment->clear_temporaries();
  187. _frame_count++;
  188. }
  189. }
  190. void Device::render_world(World& world, Camera* camera)
  191. {
  192. world.render(camera);
  193. }
  194. World* Device::create_world()
  195. {
  196. World* w = CE_NEW(default_allocator(), World)(*_resource_manager, *_lua_environment);
  197. array::push_back(_worlds, w);
  198. return w;
  199. }
  200. void Device::destroy_world(World& w)
  201. {
  202. for (uint32_t i = 0, n = array::size(_worlds); i < n; ++i)
  203. {
  204. if (&w == _worlds[i])
  205. {
  206. CE_DELETE(default_allocator(), &w);
  207. _worlds[i] = _worlds[n - 1];
  208. array::pop_back(_worlds);
  209. return;
  210. }
  211. }
  212. CE_ASSERT(false, "Bad world");
  213. }
  214. ResourcePackage* Device::create_resource_package(StringId64 id)
  215. {
  216. return CE_NEW(default_allocator(), ResourcePackage)(id, *_resource_manager);
  217. }
  218. void Device::destroy_resource_package(ResourcePackage& package)
  219. {
  220. CE_DELETE(default_allocator(), &package);
  221. }
  222. void Device::reload(StringId64 type, StringId64 name)
  223. {
  224. const void* old_resource = _resource_manager->get(type, name);
  225. _resource_manager->reload(type, name);
  226. const void* new_resource = _resource_manager->get(type, name);
  227. if (type == SCRIPT_TYPE)
  228. {
  229. _lua_environment->execute((const LuaResource*)new_resource);
  230. }
  231. }
  232. ResourceManager* Device::resource_manager()
  233. {
  234. return _resource_manager;
  235. }
  236. LuaEnvironment* Device::lua_environment()
  237. {
  238. return _lua_environment;
  239. }
  240. InputManager* Device::input_manager()
  241. {
  242. return _input_manager;
  243. }
  244. bool Device::process_events()
  245. {
  246. OsEvent event;
  247. bool exit = false;
  248. InputManager* im = _input_manager;
  249. const int16_t dt_x = _mouse_curr_x - _mouse_last_x;
  250. const int16_t dt_y = _mouse_curr_y - _mouse_last_y;
  251. im->mouse()->set_axis(MouseAxis::CURSOR_DELTA, vector3(dt_x, dt_y, 0.0f));
  252. _mouse_last_x = _mouse_curr_x;
  253. _mouse_last_y = _mouse_curr_y;
  254. while(next_event(event))
  255. {
  256. if (event.type == OsEvent::NONE) continue;
  257. switch (event.type)
  258. {
  259. case OsEvent::TOUCH:
  260. {
  261. const OsTouchEvent& ev = event.touch;
  262. switch (ev.type)
  263. {
  264. case OsTouchEvent::POINTER:
  265. im->touch()->set_button_state(ev.pointer_id, ev.pressed);
  266. break;
  267. case OsTouchEvent::MOVE:
  268. im->touch()->set_axis(ev.pointer_id, vector3(ev.x, ev.y, 0.0f));
  269. break;
  270. default:
  271. CE_FATAL("Unknown touch event type");
  272. break;
  273. }
  274. break;
  275. }
  276. case OsEvent::MOUSE:
  277. {
  278. const OsMouseEvent& ev = event.mouse;
  279. switch (ev.type)
  280. {
  281. case OsMouseEvent::BUTTON:
  282. im->mouse()->set_button_state(ev.button, ev.pressed);
  283. break;
  284. case OsMouseEvent::MOVE:
  285. _mouse_curr_x = ev.x;
  286. _mouse_curr_y = ev.y;
  287. im->mouse()->set_axis(MouseAxis::CURSOR, vector3(ev.x, ev.y, 0.0f));
  288. break;
  289. case OsMouseEvent::WHEEL:
  290. im->mouse()->set_axis(MouseAxis::WHEEL, vector3(0.0f, ev.wheel, 0.0f));
  291. break;
  292. default:
  293. CE_FATAL("Unknown mouse event type");
  294. break;
  295. }
  296. break;
  297. }
  298. case OsEvent::KEYBOARD:
  299. {
  300. const OsKeyboardEvent& ev = event.keyboard;
  301. im->keyboard()->set_button_state(ev.button, ev.pressed);
  302. break;
  303. }
  304. case OsEvent::JOYPAD:
  305. {
  306. const OsJoypadEvent& ev = event.joypad;
  307. switch (ev.type)
  308. {
  309. case OsJoypadEvent::CONNECTED:
  310. im->joypad(ev.index)->set_connected(ev.connected);
  311. break;
  312. case OsJoypadEvent::BUTTON:
  313. im->joypad(ev.index)->set_button_state(ev.button, ev.pressed);
  314. break;
  315. case OsJoypadEvent::AXIS:
  316. im->joypad(ev.index)->set_axis(ev.button, vector3(ev.x, ev.y, ev.z));
  317. break;
  318. default:
  319. CE_FATAL("Unknown joypad event");
  320. break;
  321. }
  322. break;
  323. }
  324. case OsEvent::METRICS:
  325. {
  326. const OsMetricsEvent& ev = event.metrics;
  327. _width = ev.width;
  328. _height = ev.height;
  329. bgfx::reset(ev.width, ev.height, BGFX_RESET_VSYNC);
  330. break;
  331. }
  332. case OsEvent::EXIT:
  333. {
  334. exit = true;
  335. break;
  336. }
  337. case OsEvent::PAUSE:
  338. {
  339. pause();
  340. break;
  341. }
  342. case OsEvent::RESUME:
  343. {
  344. unpause();
  345. break;
  346. }
  347. default:
  348. {
  349. CE_FATAL("Unknown Os Event");
  350. break;
  351. }
  352. }
  353. }
  354. return exit;
  355. }
  356. void Device::read_config()
  357. {
  358. TempAllocator1024 ta;
  359. DynamicString project_path(ta);
  360. if (_device_options.project() != NULL)
  361. {
  362. project_path += _device_options.project();
  363. project_path += path::SEPARATOR;
  364. }
  365. project_path += "crown.config";
  366. File* tmpfile = _bundle_filesystem->open(project_path.c_str(), FOM_READ);
  367. JSONParser config(*tmpfile);
  368. _bundle_filesystem->close(tmpfile);
  369. JSONElement root = config.root();
  370. _boot_script_id = root.key("boot_script").to_resource_id();
  371. _boot_package_id = root.key("boot_package").to_resource_id();
  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