device.cpp 9.5 KB

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