device.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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. _data_filesystem = CE_NEW(_allocator, FilesystemDisk)(default_allocator());
  298. {
  299. char cwd[1024];
  300. const char* data_dir = !_device_options._data_dir.empty()
  301. ? _device_options._data_dir.c_str()
  302. : os::getcwd(cwd, sizeof(cwd))
  303. ;
  304. ((FilesystemDisk*)_data_filesystem)->set_prefix(data_dir);
  305. }
  306. _last_log = _data_filesystem->open(CROWN_LAST_LOG, FileOpenMode::WRITE);
  307. #endif // CROWN_PLATFORM_ANDROID
  308. logi(DEVICE, "Initializing Crown Engine %s %s %s", CROWN_VERSION, CROWN_PLATFORM_NAME, CROWN_ARCH_NAME);
  309. profiler_globals::init();
  310. namespace smr = state_machine_internal;
  311. namespace cor = config_resource_internal;
  312. namespace ftr = font_resource_internal;
  313. namespace lur = lua_resource_internal;
  314. namespace lvr = level_resource_internal;
  315. namespace mhr = mesh_resource_internal;
  316. namespace mtr = material_resource_internal;
  317. namespace pcr = physics_config_resource_internal;
  318. namespace phr = physics_resource_internal;
  319. namespace pkr = package_resource_internal;
  320. namespace sar = sprite_animation_resource_internal;
  321. namespace sdr = sound_resource_internal;
  322. namespace shr = shader_resource_internal;
  323. namespace spr = sprite_resource_internal;
  324. namespace txr = texture_resource_internal;
  325. namespace utr = unit_resource_internal;
  326. _resource_loader = CE_NEW(_allocator, ResourceLoader)(*_data_filesystem);
  327. _resource_manager = CE_NEW(_allocator, ResourceManager)(*_resource_loader);
  328. _resource_manager->register_type(RESOURCE_TYPE_CONFIG, RESOURCE_VERSION_CONFIG, cor::load, cor::unload, NULL, NULL );
  329. _resource_manager->register_type(RESOURCE_TYPE_FONT, RESOURCE_VERSION_FONT, NULL, NULL, NULL, NULL );
  330. _resource_manager->register_type(RESOURCE_TYPE_LEVEL, RESOURCE_VERSION_LEVEL, NULL, NULL, NULL, NULL );
  331. _resource_manager->register_type(RESOURCE_TYPE_MATERIAL, RESOURCE_VERSION_MATERIAL, mtr::load, mtr::unload, mtr::online, mtr::offline);
  332. _resource_manager->register_type(RESOURCE_TYPE_MESH, RESOURCE_VERSION_MESH, mhr::load, mhr::unload, mhr::online, mhr::offline);
  333. _resource_manager->register_type(RESOURCE_TYPE_PACKAGE, RESOURCE_VERSION_PACKAGE, pkr::load, pkr::unload, NULL, NULL );
  334. _resource_manager->register_type(RESOURCE_TYPE_PHYSICS, RESOURCE_VERSION_PHYSICS, NULL, NULL, NULL, NULL );
  335. _resource_manager->register_type(RESOURCE_TYPE_PHYSICS_CONFIG, RESOURCE_VERSION_PHYSICS_CONFIG, NULL, NULL, NULL, NULL );
  336. _resource_manager->register_type(RESOURCE_TYPE_SCRIPT, RESOURCE_VERSION_SCRIPT, NULL, NULL, NULL, NULL );
  337. _resource_manager->register_type(RESOURCE_TYPE_SHADER, RESOURCE_VERSION_SHADER, shr::load, shr::unload, shr::online, shr::offline);
  338. _resource_manager->register_type(RESOURCE_TYPE_SOUND, RESOURCE_VERSION_SOUND, NULL, NULL, NULL, NULL );
  339. _resource_manager->register_type(RESOURCE_TYPE_SPRITE, RESOURCE_VERSION_SPRITE, NULL, NULL, NULL, NULL );
  340. _resource_manager->register_type(RESOURCE_TYPE_SPRITE_ANIMATION, RESOURCE_VERSION_SPRITE_ANIMATION, NULL, NULL, NULL, NULL );
  341. _resource_manager->register_type(RESOURCE_TYPE_STATE_MACHINE, RESOURCE_VERSION_STATE_MACHINE, NULL, NULL, NULL, NULL );
  342. _resource_manager->register_type(RESOURCE_TYPE_TEXTURE, RESOURCE_VERSION_TEXTURE, txr::load, txr::unload, txr::online, txr::offline);
  343. _resource_manager->register_type(RESOURCE_TYPE_UNIT, RESOURCE_VERSION_UNIT, NULL, NULL, NULL, NULL );
  344. // Read config
  345. {
  346. TempAllocator512 ta;
  347. DynamicString boot_dir(ta);
  348. if (_device_options._boot_dir != NULL)
  349. {
  350. boot_dir += _device_options._boot_dir;
  351. boot_dir += '/';
  352. }
  353. boot_dir += CROWN_BOOT_CONFIG;
  354. const StringId64 config_name(boot_dir.c_str());
  355. _resource_manager->load(RESOURCE_TYPE_CONFIG, config_name);
  356. _resource_manager->flush();
  357. _boot_config.parse((const char*)_resource_manager->get(RESOURCE_TYPE_CONFIG, config_name));
  358. _resource_manager->unload(RESOURCE_TYPE_CONFIG, config_name);
  359. }
  360. // Init all remaining subsystems
  361. _bgfx_allocator = CE_NEW(_allocator, BgfxAllocator)(default_allocator());
  362. _bgfx_callback = CE_NEW(_allocator, BgfxCallback)();
  363. _display = display::create(_allocator);
  364. _width = _boot_config.window_w;
  365. _height = _boot_config.window_h;
  366. _window = window::create(_allocator);
  367. _window->open(_device_options._window_x
  368. , _device_options._window_y
  369. , _width
  370. , _height
  371. , _device_options._parent_window
  372. );
  373. _window->set_title(_boot_config.window_title.c_str());
  374. _window->set_fullscreen(_boot_config.fullscreen);
  375. _window->bgfx_setup();
  376. bgfx::init(bgfx::RendererType::Count
  377. , BGFX_PCI_ID_NONE
  378. , 0
  379. , _bgfx_callback
  380. , _bgfx_allocator
  381. );
  382. bgfx::reset(_width, _height, (_boot_config.vsync ? BGFX_RESET_VSYNC : BGFX_RESET_NONE));
  383. _shader_manager = CE_NEW(_allocator, ShaderManager)(default_allocator());
  384. _material_manager = CE_NEW(_allocator, MaterialManager)(default_allocator(), *_resource_manager);
  385. _input_manager = CE_NEW(_allocator, InputManager)(default_allocator());
  386. _unit_manager = CE_NEW(_allocator, UnitManager)(default_allocator());
  387. _lua_environment = CE_NEW(_allocator, LuaEnvironment)();
  388. audio_globals::init();
  389. physics_globals::init(_allocator);
  390. ResourcePackage* boot_package = create_resource_package(_boot_config.boot_package_name);
  391. boot_package->load();
  392. boot_package->flush();
  393. _lua_environment->load_libs();
  394. _lua_environment->execute_string(_device_options._lua_string.c_str());
  395. _lua_environment->execute((LuaResource*)_resource_manager->get(RESOURCE_TYPE_SCRIPT, _boot_config.boot_script_name));
  396. _pipeline = CE_NEW(_allocator, Pipeline)();
  397. _pipeline->create(_width, _height);
  398. #if CROWN_TOOLS
  399. tool_init();
  400. #endif
  401. logi(DEVICE, "Initialized");
  402. _lua_environment->call_global("init", 0);
  403. s64 time_last = os::clocktime();
  404. u16 old_width = 0;
  405. u16 old_height = 0;
  406. while (!process_events(_boot_config.vsync) && !_quit)
  407. {
  408. const s64 time = os::clocktime();
  409. const f64 freq = (f64)os::clockfrequency();
  410. const f32 dt = f32(f64(time - time_last) / freq);
  411. time_last = time;
  412. profiler_globals::clear();
  413. _console_server->update();
  414. RECORD_FLOAT("device.dt", dt);
  415. RECORD_FLOAT("device.fps", 1.0f/dt);
  416. if (_width != old_width || _height != old_height)
  417. {
  418. old_width = _width;
  419. old_height = _height;
  420. _pipeline->reset(_width, _height);
  421. }
  422. #if CROWN_TOOLS
  423. tool_update(dt);
  424. #endif
  425. if (!_paused)
  426. {
  427. _resource_manager->complete_requests();
  428. {
  429. const s64 t0 = os::clocktime();
  430. _lua_environment->call_global("update", 1, ARGUMENT_FLOAT, dt);
  431. RECORD_FLOAT("lua.update", f32(f64(os::clocktime() - t0) / freq));
  432. }
  433. {
  434. const s64 t0 = os::clocktime();
  435. _lua_environment->call_global("render", 1, ARGUMENT_FLOAT, dt);
  436. RECORD_FLOAT("lua.render", f32(f64(os::clocktime() - t0) / freq));
  437. }
  438. }
  439. _lua_environment->reset_temporaries();
  440. _input_manager->update();
  441. const bgfx::Stats* stats = bgfx::getStats();
  442. RECORD_FLOAT("bgfx.gpu_time", f32(f64(stats->gpuTimeEnd - stats->gpuTimeBegin)*1000.0/stats->gpuTimerFreq));
  443. RECORD_FLOAT("bgfx.cpu_time", f32(f64(stats->cpuTimeEnd - stats->cpuTimeBegin)*1000.0/stats->cpuTimerFreq));
  444. profiler_globals::flush();
  445. bgfx::frame();
  446. }
  447. #if CROWN_TOOLS
  448. tool_shutdown();
  449. #endif
  450. _lua_environment->call_global("shutdown", 0);
  451. boot_package->unload();
  452. destroy_resource_package(*boot_package);
  453. physics_globals::shutdown(_allocator);
  454. audio_globals::shutdown();
  455. CE_DELETE(_allocator, _pipeline);
  456. CE_DELETE(_allocator, _lua_environment);
  457. CE_DELETE(_allocator, _unit_manager);
  458. CE_DELETE(_allocator, _input_manager);
  459. CE_DELETE(_allocator, _material_manager);
  460. CE_DELETE(_allocator, _shader_manager);
  461. CE_DELETE(_allocator, _resource_manager);
  462. CE_DELETE(_allocator, _resource_loader);
  463. _pipeline->destroy();
  464. bgfx::shutdown();
  465. _window->close();
  466. window::destroy(_allocator, *_window);
  467. display::destroy(_allocator, *_display);
  468. CE_DELETE(_allocator, _bgfx_callback);
  469. CE_DELETE(_allocator, _bgfx_allocator);
  470. if (_last_log)
  471. _data_filesystem->close(*_last_log);
  472. CE_DELETE(_allocator, _data_filesystem);
  473. profiler_globals::shutdown();
  474. _allocator.clear();
  475. }
  476. void Device::quit()
  477. {
  478. _quit = true;
  479. }
  480. void Device::pause()
  481. {
  482. _paused = true;
  483. logi(DEVICE, "Paused");
  484. }
  485. void Device::unpause()
  486. {
  487. _paused = false;
  488. logi(DEVICE, "Unpaused");
  489. }
  490. void Device::resolution(u16& width, u16& height)
  491. {
  492. width = _width;
  493. height = _height;
  494. }
  495. void Device::render(World& world, UnitId camera_unit)
  496. {
  497. float aspect_ratio = (_boot_config.aspect_ratio == -1.0f
  498. ? (float)_width/(float)_height
  499. : _boot_config.aspect_ratio
  500. );
  501. world.camera_set_aspect(camera_unit, aspect_ratio);
  502. world.camera_set_viewport_metrics(camera_unit, 0, 0, _width, _height);
  503. const Matrix4x4 view = world.camera_view_matrix(camera_unit);
  504. const Matrix4x4 proj = world.camera_projection_matrix(camera_unit);
  505. Matrix4x4 ortho_proj;
  506. orthographic(ortho_proj, 0, _width, 0, _height, 0.01f, 1.0f);
  507. bgfx::setViewClear(VIEW_SPRITE_0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x353839ff, 1.0f, 0);
  508. bgfx::setViewTransform(VIEW_SPRITE_0, to_float_ptr(view), to_float_ptr(proj));
  509. bgfx::setViewTransform(VIEW_SPRITE_1, to_float_ptr(view), to_float_ptr(proj));
  510. bgfx::setViewTransform(VIEW_SPRITE_2, to_float_ptr(view), to_float_ptr(proj));
  511. bgfx::setViewTransform(VIEW_SPRITE_3, to_float_ptr(view), to_float_ptr(proj));
  512. bgfx::setViewTransform(VIEW_SPRITE_4, to_float_ptr(view), to_float_ptr(proj));
  513. bgfx::setViewTransform(VIEW_SPRITE_5, to_float_ptr(view), to_float_ptr(proj));
  514. bgfx::setViewTransform(VIEW_SPRITE_6, to_float_ptr(view), to_float_ptr(proj));
  515. bgfx::setViewTransform(VIEW_SPRITE_7, to_float_ptr(view), to_float_ptr(proj));
  516. bgfx::setViewTransform(VIEW_MESH, to_float_ptr(view), to_float_ptr(proj));
  517. bgfx::setViewTransform(VIEW_DEBUG, to_float_ptr(view), to_float_ptr(proj));
  518. bgfx::setViewTransform(VIEW_GUI, to_float_ptr(MATRIX4X4_IDENTITY), to_float_ptr(ortho_proj));
  519. bgfx::setViewRect(VIEW_SPRITE_0, 0, 0, _width, _height);
  520. bgfx::setViewRect(VIEW_SPRITE_1, 0, 0, _width, _height);
  521. bgfx::setViewRect(VIEW_SPRITE_2, 0, 0, _width, _height);
  522. bgfx::setViewRect(VIEW_SPRITE_3, 0, 0, _width, _height);
  523. bgfx::setViewRect(VIEW_SPRITE_4, 0, 0, _width, _height);
  524. bgfx::setViewRect(VIEW_SPRITE_5, 0, 0, _width, _height);
  525. bgfx::setViewRect(VIEW_SPRITE_6, 0, 0, _width, _height);
  526. bgfx::setViewRect(VIEW_SPRITE_7, 0, 0, _width, _height);
  527. bgfx::setViewRect(VIEW_MESH, 0, 0, _width, _height);
  528. bgfx::setViewRect(VIEW_DEBUG, 0, 0, _width, _height);
  529. bgfx::setViewRect(VIEW_GUI, 0, 0, _width, _height);
  530. bgfx::setViewMode(VIEW_SPRITE_0, bgfx::ViewMode::DepthAscending);
  531. bgfx::setViewMode(VIEW_SPRITE_1, bgfx::ViewMode::DepthAscending);
  532. bgfx::setViewMode(VIEW_SPRITE_2, bgfx::ViewMode::DepthAscending);
  533. bgfx::setViewMode(VIEW_SPRITE_3, bgfx::ViewMode::DepthAscending);
  534. bgfx::setViewMode(VIEW_SPRITE_4, bgfx::ViewMode::DepthAscending);
  535. bgfx::setViewMode(VIEW_SPRITE_5, bgfx::ViewMode::DepthAscending);
  536. bgfx::setViewMode(VIEW_SPRITE_6, bgfx::ViewMode::DepthAscending);
  537. bgfx::setViewMode(VIEW_SPRITE_7, bgfx::ViewMode::DepthAscending);
  538. bgfx::setViewMode(VIEW_GUI, bgfx::ViewMode::Sequential);
  539. bgfx::setViewFrameBuffer(VIEW_SPRITE_0, _pipeline->_frame_buffer);
  540. bgfx::setViewFrameBuffer(VIEW_SPRITE_1, _pipeline->_frame_buffer);
  541. bgfx::setViewFrameBuffer(VIEW_SPRITE_2, _pipeline->_frame_buffer);
  542. bgfx::setViewFrameBuffer(VIEW_SPRITE_3, _pipeline->_frame_buffer);
  543. bgfx::setViewFrameBuffer(VIEW_SPRITE_4, _pipeline->_frame_buffer);
  544. bgfx::setViewFrameBuffer(VIEW_SPRITE_5, _pipeline->_frame_buffer);
  545. bgfx::setViewFrameBuffer(VIEW_SPRITE_6, _pipeline->_frame_buffer);
  546. bgfx::setViewFrameBuffer(VIEW_SPRITE_7, _pipeline->_frame_buffer);
  547. bgfx::setViewFrameBuffer(VIEW_MESH, _pipeline->_frame_buffer);
  548. bgfx::setViewFrameBuffer(VIEW_DEBUG, _pipeline->_frame_buffer);
  549. bgfx::setViewFrameBuffer(VIEW_GUI, _pipeline->_frame_buffer);
  550. bgfx::touch(VIEW_SPRITE_0);
  551. bgfx::touch(VIEW_SPRITE_1);
  552. bgfx::touch(VIEW_SPRITE_2);
  553. bgfx::touch(VIEW_SPRITE_3);
  554. bgfx::touch(VIEW_SPRITE_4);
  555. bgfx::touch(VIEW_SPRITE_5);
  556. bgfx::touch(VIEW_SPRITE_6);
  557. bgfx::touch(VIEW_SPRITE_7);
  558. bgfx::touch(VIEW_MESH);
  559. bgfx::touch(VIEW_DEBUG);
  560. bgfx::touch(VIEW_GUI);
  561. world.render(view, proj);
  562. #if !CROWN_TOOLS
  563. _pipeline->render(*_shader_manager, StringId32("blit"), 0, _width, _height);
  564. #endif // CROWN_TOOLS
  565. }
  566. World* Device::create_world()
  567. {
  568. World* w = CE_NEW(default_allocator(), World)(default_allocator()
  569. , *_resource_manager
  570. , *_shader_manager
  571. , *_material_manager
  572. , *_unit_manager
  573. , *_lua_environment
  574. );
  575. array::push_back(_worlds, w);
  576. return w;
  577. }
  578. void Device::destroy_world(World& w)
  579. {
  580. for (u32 i = 0, n = array::size(_worlds); i < n; ++i)
  581. {
  582. if (&w == _worlds[i])
  583. {
  584. CE_DELETE(default_allocator(), &w);
  585. _worlds[i] = _worlds[n-1];
  586. array::pop_back(_worlds);
  587. return;
  588. }
  589. }
  590. CE_FATAL("World not found");
  591. }
  592. ResourcePackage* Device::create_resource_package(StringId64 id)
  593. {
  594. return CE_NEW(default_allocator(), ResourcePackage)(id, *_resource_manager);
  595. }
  596. void Device::destroy_resource_package(ResourcePackage& rp)
  597. {
  598. CE_DELETE(default_allocator(), &rp);
  599. }
  600. void Device::reload(StringId64 type, StringId64 name)
  601. {
  602. StringId64 mix;
  603. mix._id = type._id ^ name._id;
  604. TempAllocator128 ta;
  605. DynamicString path(ta);
  606. mix.to_string(path);
  607. logi(DEVICE, "Reloading #ID(%s)", path.c_str());
  608. _resource_manager->reload(type, name);
  609. const void* new_resource = _resource_manager->get(type, name);
  610. if (type == RESOURCE_TYPE_SCRIPT)
  611. {
  612. _lua_environment->execute((const LuaResource*)new_resource);
  613. }
  614. logi(DEVICE, "Reloaded #ID(%s)", path.c_str());
  615. }
  616. void Device::log(const char* msg)
  617. {
  618. if (_last_log)
  619. {
  620. _last_log->write(msg, strlen32(msg));
  621. _last_log->write("\n", 1);
  622. _last_log->flush();
  623. }
  624. }
  625. char _buffer[sizeof(Device)];
  626. Device* _device = NULL;
  627. void run(const DeviceOptions& opts)
  628. {
  629. CE_ASSERT(_device == NULL, "Crown already initialized");
  630. console_server_globals::init();
  631. _device = new (_buffer) Device(opts, *console_server());
  632. _device->run();
  633. _device->~Device();
  634. _device = NULL;
  635. console_server_globals::shutdown();
  636. }
  637. Device* device()
  638. {
  639. return crown::_device;
  640. }
  641. } // namespace crown