device.cpp 22 KB

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