device.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. /*
  2. * Copyright (c) 2012-2017 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/dbartolini/crown/blob/master/LICENSE
  4. */
  5. #include "config.h"
  6. #include "core/containers/array.h"
  7. #include "core/containers/map.h"
  8. #include "core/filesystem/file.h"
  9. #include "core/filesystem/filesystem.h"
  10. #include "core/filesystem/filesystem_apk.h"
  11. #include "core/filesystem/filesystem_disk.h"
  12. #include "core/filesystem/path.h"
  13. #include "core/json/json_object.h"
  14. #include "core/json/sjson.h"
  15. #include "core/math/matrix4x4.h"
  16. #include "core/math/vector3.h"
  17. #include "core/memory/memory.h"
  18. #include "core/memory/proxy_allocator.h"
  19. #include "core/memory/temp_allocator.h"
  20. #include "core/os.h"
  21. #include "core/strings/string.h"
  22. #include "core/strings/string_stream.h"
  23. #include "core/types.h"
  24. #include "device/console_server.h"
  25. #include "device/device.h"
  26. #include "device/device_event_queue.h"
  27. #include "device/input_device.h"
  28. #include "device/input_manager.h"
  29. #include "device/log.h"
  30. #include "device/pipeline.h"
  31. #include "device/profiler.h"
  32. #include "lua/lua_environment.h"
  33. #include "resource/config_resource.h"
  34. #include "resource/font_resource.h"
  35. #include "resource/level_resource.h"
  36. #include "resource/lua_resource.h"
  37. #include "resource/material_resource.h"
  38. #include "resource/mesh_resource.h"
  39. #include "resource/package_resource.h"
  40. #include "resource/physics_resource.h"
  41. #include "resource/resource_loader.h"
  42. #include "resource/resource_manager.h"
  43. #include "resource/resource_package.h"
  44. #include "resource/shader_resource.h"
  45. #include "resource/sound_resource.h"
  46. #include "resource/sprite_resource.h"
  47. #include "resource/state_machine_resource.h"
  48. #include "resource/texture_resource.h"
  49. #include "resource/unit_resource.h"
  50. #include "world/audio.h"
  51. #include "world/material_manager.h"
  52. #include "world/physics.h"
  53. #include "world/shader_manager.h"
  54. #include "world/unit_manager.h"
  55. #include "world/world.h"
  56. #include <bgfx/bgfx.h>
  57. #include <bx/allocator.h>
  58. #define MAX_SUBSYSTEMS_HEAP 8 * 1024 * 1024
  59. namespace { const crown::log_internal::System DEVICE = { "Device" }; }
  60. namespace crown
  61. {
  62. #if CROWN_TOOLS
  63. extern void tool_init(void);
  64. extern void tool_update(float);
  65. extern void tool_shutdown(void);
  66. extern bool tool_process_events();
  67. #endif
  68. extern bool next_event(OsEvent& ev);
  69. struct BgfxCallback : public bgfx::CallbackI
  70. {
  71. virtual void fatal(bgfx::Fatal::Enum _code, const char* _str)
  72. {
  73. CE_ASSERT(false, "Fatal error: 0x%08x: %s", _code, _str);
  74. CE_UNUSED(_code);
  75. CE_UNUSED(_str);
  76. }
  77. virtual void traceVargs(const char* /*_filePath*/, u16 /*_line*/, const char* _format, va_list _argList)
  78. {
  79. char buf[2048];
  80. strncpy(buf, _format, sizeof(buf));
  81. buf[strlen32(buf)-1] = '\0'; // Remove trailing newline
  82. logiv(DEVICE, buf, _argList);
  83. }
  84. virtual void profilerBegin(const char* /*_name*/, uint32_t /*_abgr*/, const char* /*_filePath*/, uint16_t /*_line*/)
  85. {
  86. }
  87. virtual void profilerBeginLiteral(const char* /*_name*/, uint32_t /*_abgr*/, const char* /*_filePath*/, uint16_t /*_line*/)
  88. {
  89. }
  90. virtual void profilerEnd()
  91. {
  92. }
  93. virtual u32 cacheReadSize(u64 /*_id*/)
  94. {
  95. return 0;
  96. }
  97. virtual bool cacheRead(u64 /*_id*/, void* /*_data*/, u32 /*_size*/)
  98. {
  99. return false;
  100. }
  101. virtual void cacheWrite(u64 /*_id*/, const void* /*_data*/, u32 /*_size*/)
  102. {
  103. }
  104. virtual void screenShot(const char* /*_filePath*/, u32 /*_width*/, u32 /*_height*/, u32 /*_pitch*/, const void* /*_data*/, u32 /*_size*/, bool /*_yflip*/)
  105. {
  106. }
  107. virtual void captureBegin(u32 /*_width*/, u32 /*_height*/, u32 /*_pitch*/, bgfx::TextureFormat::Enum /*_format*/, bool /*_yflip*/)
  108. {
  109. }
  110. virtual void captureEnd()
  111. {
  112. }
  113. virtual void captureFrame(const void* /*_data*/, u32 /*_size*/)
  114. {
  115. }
  116. };
  117. struct BgfxAllocator : public bx::AllocatorI
  118. {
  119. ProxyAllocator _allocator;
  120. BgfxAllocator(Allocator& a)
  121. : _allocator(a, "bgfx")
  122. {
  123. }
  124. virtual void* realloc(void* _ptr, size_t _size, size_t _align, const char* /*_file*/, u32 /*_line*/)
  125. {
  126. if (!_ptr)
  127. return _allocator.allocate((u32)_size, (u32)_align == 0 ? 1 : (u32)_align);
  128. if (_size == 0)
  129. {
  130. _allocator.deallocate(_ptr);
  131. return NULL;
  132. }
  133. // Realloc
  134. void* p = _allocator.allocate((u32)_size, (u32)_align == 0 ? 1 : (u32)_align);
  135. _allocator.deallocate(_ptr);
  136. return p;
  137. }
  138. };
  139. static void console_command_script(ConsoleServer& /*cs*/, TCPSocket /*client*/, const char* json, void* user_data)
  140. {
  141. TempAllocator4096 ta;
  142. JsonObject obj(ta);
  143. DynamicString script(ta);
  144. sjson::parse(json, obj);
  145. sjson::parse_string(obj["script"], script);
  146. ((Device*)user_data)->_lua_environment->execute_string(script.c_str());
  147. }
  148. static void console_command(ConsoleServer& cs, TCPSocket client, const char* json, void* user_data)
  149. {
  150. TempAllocator4096 ta;
  151. JsonObject obj(ta);
  152. JsonArray args(ta);
  153. sjson::parse(json, obj);
  154. sjson::parse_array(obj["args"], args);
  155. DynamicString cmd(ta);
  156. sjson::parse_string(args[0], cmd);
  157. if (cmd == "pause")
  158. device()->pause();
  159. else if (cmd == "unpause")
  160. device()->unpause();
  161. else if (cmd == "reload")
  162. {
  163. if (array::size(args) != 3)
  164. {
  165. cs.error(client, "Usage: reload type name");
  166. return;
  167. }
  168. DynamicString type(ta);
  169. DynamicString name(ta);
  170. sjson::parse_string(args[1], type);
  171. sjson::parse_string(args[2], name);
  172. ((Device*)user_data)->reload(ResourceId(type.c_str()), ResourceId(name.c_str()));
  173. }
  174. }
  175. Device::Device(const DeviceOptions& opts, ConsoleServer& cs)
  176. : _allocator(default_allocator(), MAX_SUBSYSTEMS_HEAP)
  177. , _device_options(opts)
  178. , _boot_config(default_allocator())
  179. , _console_server(&cs)
  180. , _data_filesystem(NULL)
  181. , _last_log(NULL)
  182. , _resource_loader(NULL)
  183. , _resource_manager(NULL)
  184. , _bgfx_allocator(NULL)
  185. , _bgfx_callback(NULL)
  186. , _shader_manager(NULL)
  187. , _material_manager(NULL)
  188. , _input_manager(NULL)
  189. , _unit_manager(NULL)
  190. , _lua_environment(NULL)
  191. , _pipeline(NULL)
  192. , _display(NULL)
  193. , _window(NULL)
  194. , _worlds(default_allocator())
  195. , _width(0)
  196. , _height(0)
  197. , _quit(false)
  198. , _paused(false)
  199. {
  200. }
  201. bool Device::process_events(bool vsync)
  202. {
  203. #if CROWN_TOOLS
  204. return tool_process_events();
  205. #endif
  206. InputManager* im = _input_manager;
  207. bool exit = false;
  208. bool reset = false;
  209. OsEvent event;
  210. while (next_event(event))
  211. {
  212. if (event.type == OsEventType::NONE)
  213. continue;
  214. switch (event.type)
  215. {
  216. case OsEventType::BUTTON:
  217. {
  218. const ButtonEvent ev = event.button;
  219. switch (ev.device_id)
  220. {
  221. case InputDeviceType::KEYBOARD:
  222. im->keyboard()->set_button(ev.button_num, ev.pressed);
  223. break;
  224. case InputDeviceType::MOUSE:
  225. im->mouse()->set_button(ev.button_num, ev.pressed);
  226. break;
  227. case InputDeviceType::TOUCHSCREEN:
  228. im->touch()->set_button(ev.button_num, ev.pressed);
  229. break;
  230. case InputDeviceType::JOYPAD:
  231. im->joypad(ev.device_num)->set_button(ev.button_num, ev.pressed);
  232. break;
  233. }
  234. }
  235. break;
  236. case OsEventType::AXIS:
  237. {
  238. const AxisEvent ev = event.axis;
  239. switch (ev.device_id)
  240. {
  241. case InputDeviceType::MOUSE:
  242. im->mouse()->set_axis(ev.axis_num, vector3(ev.axis_x, ev.axis_y, ev.axis_z));
  243. break;
  244. case InputDeviceType::JOYPAD:
  245. im->joypad(ev.device_num)->set_axis(ev.axis_num, vector3(ev.axis_x, ev.axis_y, ev.axis_z));
  246. break;
  247. }
  248. }
  249. break;
  250. case OsEventType::STATUS:
  251. {
  252. const StatusEvent ev = event.status;
  253. switch (ev.device_id)
  254. {
  255. case InputDeviceType::JOYPAD:
  256. im->joypad(ev.device_num)->_connected = ev.connected;
  257. break;
  258. }
  259. }
  260. break;
  261. case OsEventType::RESOLUTION:
  262. {
  263. const ResolutionEvent& ev = event.resolution;
  264. _width = ev.width;
  265. _height = ev.height;
  266. reset = true;
  267. }
  268. break;
  269. case OsEventType::EXIT:
  270. exit = true;
  271. break;
  272. case OsEventType::PAUSE:
  273. pause();
  274. break;
  275. case OsEventType::RESUME:
  276. unpause();
  277. break;
  278. case OsEventType::TEXT:
  279. break;
  280. default:
  281. CE_FATAL("Unknown OS event");
  282. break;
  283. }
  284. }
  285. if (reset)
  286. bgfx::reset(_width, _height, (vsync ? BGFX_RESET_VSYNC : BGFX_RESET_NONE));
  287. return exit;
  288. }
  289. void Device::run()
  290. {
  291. _console_server->register_command("command", console_command, this);
  292. _console_server->register_command("script", console_command_script, this);
  293. _console_server->listen(_device_options._console_port, _device_options._wait_console);
  294. #if CROWN_PLATFORM_ANDROID
  295. _data_filesystem = CE_NEW(_allocator, FilesystemApk)(default_allocator(), const_cast<AAssetManager*>((AAssetManager*)_device_options._asset_manager));
  296. #else
  297. const char* data_dir = _device_options._data_dir.c_str();
  298. if (!data_dir)
  299. {
  300. char buf[1024];
  301. data_dir = os::getcwd(buf, sizeof(buf));
  302. }
  303. _data_filesystem = CE_NEW(_allocator, FilesystemDisk)(default_allocator());
  304. ((FilesystemDisk*)_data_filesystem)->set_prefix(data_dir);
  305. if (!_data_filesystem->exists(data_dir))
  306. _data_filesystem->create_directory(data_dir);
  307. _last_log = _data_filesystem->open(CROWN_LAST_LOG, FileOpenMode::WRITE);
  308. #endif // CROWN_PLATFORM_ANDROID
  309. logi(DEVICE, "Initializing Crown Engine %s %s %s", CROWN_VERSION, CROWN_PLATFORM_NAME, CROWN_ARCH_NAME);
  310. profiler_globals::init();
  311. namespace smr = state_machine_internal;
  312. namespace cor = config_resource_internal;
  313. namespace ftr = font_resource_internal;
  314. namespace lur = lua_resource_internal;
  315. namespace lvr = level_resource_internal;
  316. namespace mhr = mesh_resource_internal;
  317. namespace mtr = material_resource_internal;
  318. namespace pcr = physics_config_resource_internal;
  319. namespace phr = physics_resource_internal;
  320. namespace pkr = package_resource_internal;
  321. namespace sar = sprite_animation_resource_internal;
  322. namespace sdr = sound_resource_internal;
  323. namespace shr = shader_resource_internal;
  324. namespace spr = sprite_resource_internal;
  325. namespace txr = texture_resource_internal;
  326. namespace utr = unit_resource_internal;
  327. _resource_loader = CE_NEW(_allocator, ResourceLoader)(*_data_filesystem);
  328. _resource_manager = CE_NEW(_allocator, ResourceManager)(*_resource_loader);
  329. _resource_manager->register_type(RESOURCE_TYPE_CONFIG, RESOURCE_VERSION_CONFIG, cor::load, cor::unload, NULL, NULL );
  330. _resource_manager->register_type(RESOURCE_TYPE_FONT, RESOURCE_VERSION_FONT, NULL, NULL, NULL, NULL );
  331. _resource_manager->register_type(RESOURCE_TYPE_LEVEL, RESOURCE_VERSION_LEVEL, NULL, NULL, NULL, NULL );
  332. _resource_manager->register_type(RESOURCE_TYPE_MATERIAL, RESOURCE_VERSION_MATERIAL, mtr::load, mtr::unload, mtr::online, mtr::offline);
  333. _resource_manager->register_type(RESOURCE_TYPE_MESH, RESOURCE_VERSION_MESH, mhr::load, mhr::unload, mhr::online, mhr::offline);
  334. _resource_manager->register_type(RESOURCE_TYPE_PACKAGE, RESOURCE_VERSION_PACKAGE, pkr::load, pkr::unload, NULL, NULL );
  335. _resource_manager->register_type(RESOURCE_TYPE_PHYSICS, RESOURCE_VERSION_PHYSICS, NULL, NULL, NULL, NULL );
  336. _resource_manager->register_type(RESOURCE_TYPE_PHYSICS_CONFIG, RESOURCE_VERSION_PHYSICS_CONFIG, NULL, NULL, NULL, NULL );
  337. _resource_manager->register_type(RESOURCE_TYPE_SCRIPT, RESOURCE_VERSION_SCRIPT, NULL, NULL, NULL, NULL );
  338. _resource_manager->register_type(RESOURCE_TYPE_SHADER, RESOURCE_VERSION_SHADER, shr::load, shr::unload, shr::online, shr::offline);
  339. _resource_manager->register_type(RESOURCE_TYPE_SOUND, RESOURCE_VERSION_SOUND, NULL, NULL, NULL, NULL );
  340. _resource_manager->register_type(RESOURCE_TYPE_SPRITE, RESOURCE_VERSION_SPRITE, NULL, NULL, NULL, NULL );
  341. _resource_manager->register_type(RESOURCE_TYPE_SPRITE_ANIMATION, RESOURCE_VERSION_SPRITE_ANIMATION, NULL, NULL, NULL, NULL );
  342. _resource_manager->register_type(RESOURCE_TYPE_STATE_MACHINE, RESOURCE_VERSION_STATE_MACHINE, NULL, NULL, NULL, NULL );
  343. _resource_manager->register_type(RESOURCE_TYPE_TEXTURE, RESOURCE_VERSION_TEXTURE, txr::load, txr::unload, txr::online, txr::offline);
  344. _resource_manager->register_type(RESOURCE_TYPE_UNIT, RESOURCE_VERSION_UNIT, NULL, NULL, NULL, NULL );
  345. // Read config
  346. {
  347. TempAllocator512 ta;
  348. DynamicString boot_dir(ta);
  349. if (_device_options._boot_dir != NULL)
  350. {
  351. boot_dir += _device_options._boot_dir;
  352. boot_dir += '/';
  353. }
  354. boot_dir += CROWN_BOOT_CONFIG;
  355. const StringId64 config_name(boot_dir.c_str());
  356. _resource_manager->load(RESOURCE_TYPE_CONFIG, config_name);
  357. _resource_manager->flush();
  358. _boot_config.parse((const char*)_resource_manager->get(RESOURCE_TYPE_CONFIG, config_name));
  359. _resource_manager->unload(RESOURCE_TYPE_CONFIG, config_name);
  360. }
  361. // Init all remaining subsystems
  362. _bgfx_allocator = CE_NEW(_allocator, BgfxAllocator)(default_allocator());
  363. _bgfx_callback = CE_NEW(_allocator, BgfxCallback)();
  364. _display = display::create(_allocator);
  365. _width = _boot_config.window_w;
  366. _height = _boot_config.window_h;
  367. _window = window::create(_allocator);
  368. _window->open(_device_options._window_x
  369. , _device_options._window_y
  370. , _width
  371. , _height
  372. , _device_options._parent_window
  373. );
  374. _window->set_title(_boot_config.window_title.c_str());
  375. _window->set_fullscreen(_boot_config.fullscreen);
  376. _window->bgfx_setup();
  377. bgfx::init(bgfx::RendererType::Count
  378. , BGFX_PCI_ID_NONE
  379. , 0
  380. , _bgfx_callback
  381. , _bgfx_allocator
  382. );
  383. bgfx::reset(_width, _height, (_boot_config.vsync ? BGFX_RESET_VSYNC : BGFX_RESET_NONE));
  384. _shader_manager = CE_NEW(_allocator, ShaderManager)(default_allocator());
  385. _material_manager = CE_NEW(_allocator, MaterialManager)(default_allocator(), *_resource_manager);
  386. _input_manager = CE_NEW(_allocator, InputManager)(default_allocator());
  387. _unit_manager = CE_NEW(_allocator, UnitManager)(default_allocator());
  388. _lua_environment = CE_NEW(_allocator, LuaEnvironment)();
  389. audio_globals::init();
  390. physics_globals::init(_allocator);
  391. ResourcePackage* boot_package = create_resource_package(_boot_config.boot_package_name);
  392. boot_package->load();
  393. boot_package->flush();
  394. _lua_environment->load_libs();
  395. _lua_environment->execute_string(_device_options._lua_string.c_str());
  396. _lua_environment->execute((LuaResource*)_resource_manager->get(RESOURCE_TYPE_SCRIPT, _boot_config.boot_script_name));
  397. _pipeline = CE_NEW(_allocator, Pipeline)();
  398. _pipeline->create(_width, _height);
  399. #if CROWN_TOOLS
  400. tool_init();
  401. #endif
  402. logi(DEVICE, "Initialized");
  403. _lua_environment->call_global("init", 0);
  404. s64 time_last = os::clocktime();
  405. u16 old_width = 0;
  406. u16 old_height = 0;
  407. while (!process_events(_boot_config.vsync) && !_quit)
  408. {
  409. const s64 time = os::clocktime();
  410. const f64 freq = (f64)os::clockfrequency();
  411. const f32 dt = f32(f64(time - time_last) / freq);
  412. time_last = time;
  413. profiler_globals::clear();
  414. _console_server->update();
  415. RECORD_FLOAT("device.dt", dt);
  416. RECORD_FLOAT("device.fps", 1.0f/dt);
  417. if (_width != old_width || _height != old_height)
  418. {
  419. old_width = _width;
  420. old_height = _height;
  421. _pipeline->reset(_width, _height);
  422. }
  423. #if CROWN_TOOLS
  424. tool_update(dt);
  425. #endif
  426. if (!_paused)
  427. {
  428. _resource_manager->complete_requests();
  429. {
  430. const s64 t0 = os::clocktime();
  431. _lua_environment->call_global("update", 1, ARGUMENT_FLOAT, dt);
  432. RECORD_FLOAT("lua.update", f32(f64(os::clocktime() - t0) / freq));
  433. }
  434. {
  435. const s64 t0 = os::clocktime();
  436. _lua_environment->call_global("render", 1, ARGUMENT_FLOAT, dt);
  437. RECORD_FLOAT("lua.render", f32(f64(os::clocktime() - t0) / freq));
  438. }
  439. }
  440. _lua_environment->reset_temporaries();
  441. _input_manager->update();
  442. const bgfx::Stats* stats = bgfx::getStats();
  443. RECORD_FLOAT("bgfx.gpu_time", f32(f64(stats->gpuTimeEnd - stats->gpuTimeBegin)*1000.0/stats->gpuTimerFreq));
  444. RECORD_FLOAT("bgfx.cpu_time", f32(f64(stats->cpuTimeEnd - stats->cpuTimeBegin)*1000.0/stats->cpuTimerFreq));
  445. profiler_globals::flush();
  446. bgfx::frame();
  447. }
  448. #if CROWN_TOOLS
  449. tool_shutdown();
  450. #endif
  451. _lua_environment->call_global("shutdown", 0);
  452. boot_package->unload();
  453. destroy_resource_package(*boot_package);
  454. physics_globals::shutdown(_allocator);
  455. audio_globals::shutdown();
  456. CE_DELETE(_allocator, _pipeline);
  457. CE_DELETE(_allocator, _lua_environment);
  458. CE_DELETE(_allocator, _unit_manager);
  459. CE_DELETE(_allocator, _input_manager);
  460. CE_DELETE(_allocator, _material_manager);
  461. CE_DELETE(_allocator, _shader_manager);
  462. CE_DELETE(_allocator, _resource_manager);
  463. CE_DELETE(_allocator, _resource_loader);
  464. _pipeline->destroy();
  465. bgfx::shutdown();
  466. _window->close();
  467. window::destroy(_allocator, *_window);
  468. display::destroy(_allocator, *_display);
  469. CE_DELETE(_allocator, _bgfx_callback);
  470. CE_DELETE(_allocator, _bgfx_allocator);
  471. if (_last_log)
  472. _data_filesystem->close(*_last_log);
  473. CE_DELETE(_allocator, _data_filesystem);
  474. profiler_globals::shutdown();
  475. _allocator.clear();
  476. }
  477. void Device::quit()
  478. {
  479. _quit = true;
  480. }
  481. void Device::pause()
  482. {
  483. _paused = true;
  484. logi(DEVICE, "Paused");
  485. }
  486. void Device::unpause()
  487. {
  488. _paused = false;
  489. logi(DEVICE, "Unpaused");
  490. }
  491. void Device::resolution(u16& width, u16& height)
  492. {
  493. width = _width;
  494. height = _height;
  495. }
  496. void Device::render(World& world, UnitId camera_unit)
  497. {
  498. float aspect_ratio = (_boot_config.aspect_ratio == -1.0f
  499. ? (float)_width/(float)_height
  500. : _boot_config.aspect_ratio
  501. );
  502. world.camera_set_aspect(camera_unit, aspect_ratio);
  503. world.camera_set_viewport_metrics(camera_unit, 0, 0, _width, _height);
  504. const Matrix4x4 view = world.camera_view_matrix(camera_unit);
  505. const Matrix4x4 proj = world.camera_projection_matrix(camera_unit);
  506. Matrix4x4 ortho_proj;
  507. orthographic(ortho_proj, 0, _width, 0, _height, 0.01f, 1.0f);
  508. bgfx::setViewClear(VIEW_SPRITE_0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x353839ff, 1.0f, 0);
  509. bgfx::setViewTransform(VIEW_SPRITE_0, to_float_ptr(view), to_float_ptr(proj));
  510. bgfx::setViewTransform(VIEW_SPRITE_1, to_float_ptr(view), to_float_ptr(proj));
  511. bgfx::setViewTransform(VIEW_SPRITE_2, to_float_ptr(view), to_float_ptr(proj));
  512. bgfx::setViewTransform(VIEW_SPRITE_3, to_float_ptr(view), to_float_ptr(proj));
  513. bgfx::setViewTransform(VIEW_SPRITE_4, to_float_ptr(view), to_float_ptr(proj));
  514. bgfx::setViewTransform(VIEW_SPRITE_5, to_float_ptr(view), to_float_ptr(proj));
  515. bgfx::setViewTransform(VIEW_SPRITE_6, to_float_ptr(view), to_float_ptr(proj));
  516. bgfx::setViewTransform(VIEW_SPRITE_7, to_float_ptr(view), to_float_ptr(proj));
  517. bgfx::setViewTransform(VIEW_MESH, to_float_ptr(view), to_float_ptr(proj));
  518. bgfx::setViewTransform(VIEW_DEBUG, to_float_ptr(view), to_float_ptr(proj));
  519. bgfx::setViewTransform(VIEW_GUI, to_float_ptr(MATRIX4X4_IDENTITY), to_float_ptr(ortho_proj));
  520. bgfx::setViewRect(VIEW_SPRITE_0, 0, 0, _width, _height);
  521. bgfx::setViewRect(VIEW_SPRITE_1, 0, 0, _width, _height);
  522. bgfx::setViewRect(VIEW_SPRITE_2, 0, 0, _width, _height);
  523. bgfx::setViewRect(VIEW_SPRITE_3, 0, 0, _width, _height);
  524. bgfx::setViewRect(VIEW_SPRITE_4, 0, 0, _width, _height);
  525. bgfx::setViewRect(VIEW_SPRITE_5, 0, 0, _width, _height);
  526. bgfx::setViewRect(VIEW_SPRITE_6, 0, 0, _width, _height);
  527. bgfx::setViewRect(VIEW_SPRITE_7, 0, 0, _width, _height);
  528. bgfx::setViewRect(VIEW_MESH, 0, 0, _width, _height);
  529. bgfx::setViewRect(VIEW_DEBUG, 0, 0, _width, _height);
  530. bgfx::setViewRect(VIEW_GUI, 0, 0, _width, _height);
  531. bgfx::setViewMode(VIEW_SPRITE_0, bgfx::ViewMode::DepthAscending);
  532. bgfx::setViewMode(VIEW_SPRITE_1, bgfx::ViewMode::DepthAscending);
  533. bgfx::setViewMode(VIEW_SPRITE_2, bgfx::ViewMode::DepthAscending);
  534. bgfx::setViewMode(VIEW_SPRITE_3, bgfx::ViewMode::DepthAscending);
  535. bgfx::setViewMode(VIEW_SPRITE_4, bgfx::ViewMode::DepthAscending);
  536. bgfx::setViewMode(VIEW_SPRITE_5, bgfx::ViewMode::DepthAscending);
  537. bgfx::setViewMode(VIEW_SPRITE_6, bgfx::ViewMode::DepthAscending);
  538. bgfx::setViewMode(VIEW_SPRITE_7, bgfx::ViewMode::DepthAscending);
  539. bgfx::setViewMode(VIEW_GUI, bgfx::ViewMode::Sequential);
  540. bgfx::setViewFrameBuffer(VIEW_SPRITE_0, _pipeline->_frame_buffer);
  541. bgfx::setViewFrameBuffer(VIEW_SPRITE_1, _pipeline->_frame_buffer);
  542. bgfx::setViewFrameBuffer(VIEW_SPRITE_2, _pipeline->_frame_buffer);
  543. bgfx::setViewFrameBuffer(VIEW_SPRITE_3, _pipeline->_frame_buffer);
  544. bgfx::setViewFrameBuffer(VIEW_SPRITE_4, _pipeline->_frame_buffer);
  545. bgfx::setViewFrameBuffer(VIEW_SPRITE_5, _pipeline->_frame_buffer);
  546. bgfx::setViewFrameBuffer(VIEW_SPRITE_6, _pipeline->_frame_buffer);
  547. bgfx::setViewFrameBuffer(VIEW_SPRITE_7, _pipeline->_frame_buffer);
  548. bgfx::setViewFrameBuffer(VIEW_MESH, _pipeline->_frame_buffer);
  549. bgfx::setViewFrameBuffer(VIEW_DEBUG, _pipeline->_frame_buffer);
  550. bgfx::setViewFrameBuffer(VIEW_GUI, _pipeline->_frame_buffer);
  551. bgfx::touch(VIEW_SPRITE_0);
  552. bgfx::touch(VIEW_SPRITE_1);
  553. bgfx::touch(VIEW_SPRITE_2);
  554. bgfx::touch(VIEW_SPRITE_3);
  555. bgfx::touch(VIEW_SPRITE_4);
  556. bgfx::touch(VIEW_SPRITE_5);
  557. bgfx::touch(VIEW_SPRITE_6);
  558. bgfx::touch(VIEW_SPRITE_7);
  559. bgfx::touch(VIEW_MESH);
  560. bgfx::touch(VIEW_DEBUG);
  561. bgfx::touch(VIEW_GUI);
  562. world.render(view, proj);
  563. #if !CROWN_TOOLS
  564. _pipeline->render(*_shader_manager, StringId32("blit"), 0, _width, _height);
  565. #endif // CROWN_TOOLS
  566. }
  567. World* Device::create_world()
  568. {
  569. World* w = CE_NEW(default_allocator(), World)(default_allocator()
  570. , *_resource_manager
  571. , *_shader_manager
  572. , *_material_manager
  573. , *_unit_manager
  574. , *_lua_environment
  575. );
  576. array::push_back(_worlds, w);
  577. return w;
  578. }
  579. void Device::destroy_world(World& w)
  580. {
  581. for (u32 i = 0, n = array::size(_worlds); i < n; ++i)
  582. {
  583. if (&w == _worlds[i])
  584. {
  585. CE_DELETE(default_allocator(), &w);
  586. _worlds[i] = _worlds[n-1];
  587. array::pop_back(_worlds);
  588. return;
  589. }
  590. }
  591. CE_FATAL("World not found");
  592. }
  593. ResourcePackage* Device::create_resource_package(StringId64 id)
  594. {
  595. return CE_NEW(default_allocator(), ResourcePackage)(id, *_resource_manager);
  596. }
  597. void Device::destroy_resource_package(ResourcePackage& rp)
  598. {
  599. CE_DELETE(default_allocator(), &rp);
  600. }
  601. void Device::reload(StringId64 type, StringId64 name)
  602. {
  603. StringId64 mix;
  604. mix._id = type._id ^ name._id;
  605. TempAllocator128 ta;
  606. DynamicString path(ta);
  607. mix.to_string(path);
  608. logi(DEVICE, "Reloading #ID(%s)", path.c_str());
  609. _resource_manager->reload(type, name);
  610. const void* new_resource = _resource_manager->get(type, name);
  611. if (type == RESOURCE_TYPE_SCRIPT)
  612. {
  613. _lua_environment->execute((const LuaResource*)new_resource);
  614. }
  615. logi(DEVICE, "Reloaded #ID(%s)", path.c_str());
  616. }
  617. void Device::log(const char* msg)
  618. {
  619. if (_last_log)
  620. {
  621. _last_log->write(msg, strlen32(msg));
  622. _last_log->write("\n", 1);
  623. _last_log->flush();
  624. }
  625. }
  626. char _buffer[sizeof(Device)];
  627. Device* _device = NULL;
  628. void run(const DeviceOptions& opts)
  629. {
  630. CE_ASSERT(_device == NULL, "Crown already initialized");
  631. console_server_globals::init();
  632. _device = new (_buffer) Device(opts, *console_server());
  633. _device->run();
  634. _device->~Device();
  635. _device = NULL;
  636. console_server_globals::shutdown();
  637. }
  638. Device* device()
  639. {
  640. return crown::_device;
  641. }
  642. } // namespace crown