device.cpp 22 KB

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