device.cpp 23 KB

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