device.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. /*
  2. * Copyright (c) 2012-2017 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "array.h"
  6. #include "audio.h"
  7. #include "config.h"
  8. #include "config_resource.h"
  9. #include "console_api.h"
  10. #include "console_server.h"
  11. #include "console_server.h"
  12. #include "device.h"
  13. #include "device_event_queue.h"
  14. #include "file.h"
  15. #include "filesystem.h"
  16. #include "filesystem_apk.h"
  17. #include "filesystem_disk.h"
  18. #include "font_resource.h"
  19. #include "input_device.h"
  20. #include "input_manager.h"
  21. #include "json_object.h"
  22. #include "level_resource.h"
  23. #include "log.h"
  24. #include "lua_environment.h"
  25. #include "lua_resource.h"
  26. #include "map.h"
  27. #include "material_manager.h"
  28. #include "material_resource.h"
  29. #include "matrix4x4.h"
  30. #include "memory.h"
  31. #include "mesh_resource.h"
  32. #include "os.h"
  33. #include "package_resource.h"
  34. #include "path.h"
  35. #include "physics.h"
  36. #include "physics_resource.h"
  37. #include "profiler.h"
  38. #include "proxy_allocator.h"
  39. #include "resource_loader.h"
  40. #include "resource_manager.h"
  41. #include "resource_package.h"
  42. #include "shader_manager.h"
  43. #include "shader_resource.h"
  44. #include "sjson.h"
  45. #include "sound_resource.h"
  46. #include "sprite_resource.h"
  47. #include "string_stream.h"
  48. #include "string_utils.h"
  49. #include "temp_allocator.h"
  50. #include "texture_resource.h"
  51. #include "types.h"
  52. #include "unit_manager.h"
  53. #include "unit_resource.h"
  54. #include "vector3.h"
  55. #include "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. extern bool next_event(OsEvent& ev);
  63. struct BgfxCallback : public bgfx::CallbackI
  64. {
  65. virtual void fatal(bgfx::Fatal::Enum _code, const char* _str)
  66. {
  67. CE_ASSERT(false, "Fatal error: 0x%08x: %s", _code, _str);
  68. CE_UNUSED(_code);
  69. CE_UNUSED(_str);
  70. }
  71. virtual void traceVargs(const char* /*_filePath*/, u16 /*_line*/, const char* _format, va_list _argList)
  72. {
  73. char buf[2048];
  74. strncpy(buf, _format, sizeof(buf));
  75. buf[strlen32(buf)-1] = '\0'; // Remove trailing newline
  76. logiv(DEVICE, buf, _argList);
  77. }
  78. virtual u32 cacheReadSize(u64 /*_id*/)
  79. {
  80. return 0;
  81. }
  82. virtual bool cacheRead(u64 /*_id*/, void* /*_data*/, u32 /*_size*/)
  83. {
  84. return false;
  85. }
  86. virtual void cacheWrite(u64 /*_id*/, const void* /*_data*/, u32 /*_size*/)
  87. {
  88. }
  89. virtual void screenShot(const char* /*_filePath*/, u32 /*_width*/, u32 /*_height*/, u32 /*_pitch*/, const void* /*_data*/, u32 /*_size*/, bool /*_yflip*/)
  90. {
  91. }
  92. virtual void captureBegin(u32 /*_width*/, u32 /*_height*/, u32 /*_pitch*/, bgfx::TextureFormat::Enum /*_format*/, bool /*_yflip*/)
  93. {
  94. }
  95. virtual void captureEnd()
  96. {
  97. }
  98. virtual void captureFrame(const void* /*_data*/, u32 /*_size*/)
  99. {
  100. }
  101. };
  102. struct BgfxAllocator : public bx::AllocatorI
  103. {
  104. BgfxAllocator(Allocator& a)
  105. : _allocator(a, "bgfx")
  106. {
  107. }
  108. virtual void* realloc(void* _ptr, size_t _size, size_t _align, const char* /*_file*/, u32 /*_line*/)
  109. {
  110. if (!_ptr)
  111. return _allocator.allocate((u32)_size, (u32)_align == 0 ? 1 : (u32)_align);
  112. if (_size == 0)
  113. {
  114. _allocator.deallocate(_ptr);
  115. return NULL;
  116. }
  117. // Realloc
  118. void* p = _allocator.allocate((u32)_size, (u32)_align == 0 ? 1 : (u32)_align);
  119. _allocator.deallocate(_ptr);
  120. return p;
  121. }
  122. private:
  123. ProxyAllocator _allocator;
  124. };
  125. Device::Device(const DeviceOptions& opts)
  126. : _allocator(default_allocator(), MAX_SUBSYSTEMS_HEAP)
  127. , _device_options(opts)
  128. , _boot_config(default_allocator())
  129. , _bundle_filesystem(NULL)
  130. , _last_log(NULL)
  131. , _resource_loader(NULL)
  132. , _resource_manager(NULL)
  133. , _bgfx_allocator(NULL)
  134. , _bgfx_callback(NULL)
  135. , _shader_manager(NULL)
  136. , _material_manager(NULL)
  137. , _input_manager(NULL)
  138. , _unit_manager(NULL)
  139. , _lua_environment(NULL)
  140. , _display(NULL)
  141. , _window(NULL)
  142. , _worlds(default_allocator())
  143. , _width(0)
  144. , _height(0)
  145. , _quit(false)
  146. , _paused(false)
  147. , _frame_count(0)
  148. , _last_delta_time(0.0f)
  149. , _time_since_start(0.0)
  150. {
  151. }
  152. bool Device::process_events(s16& mouse_x, s16& mouse_y, s16& mouse_last_x, s16& mouse_last_y, bool vsync)
  153. {
  154. InputManager* im = _input_manager;
  155. bool exit = false;
  156. bool reset = false;
  157. OsEvent event;
  158. while(next_event(event))
  159. {
  160. if (event.type == OsEventType::NONE)
  161. continue;
  162. switch (event.type)
  163. {
  164. case OsEventType::BUTTON:
  165. {
  166. const ButtonEvent ev = event.button;
  167. switch (ev.device_id)
  168. {
  169. case InputDeviceType::KEYBOARD:
  170. im->keyboard()->set_button_state(ev.button_num, ev.pressed);
  171. break;
  172. case InputDeviceType::MOUSE:
  173. im->mouse()->set_button_state(ev.button_num, ev.pressed);
  174. break;
  175. case InputDeviceType::TOUCHSCREEN:
  176. im->touch()->set_button_state(ev.button_num, ev.pressed);
  177. break;
  178. case InputDeviceType::JOYPAD:
  179. im->joypad(ev.device_num)->set_button_state(ev.button_num, ev.pressed);
  180. break;
  181. }
  182. }
  183. break;
  184. case OsEventType::AXIS:
  185. {
  186. const AxisEvent ev = event.axis;
  187. switch (ev.device_id)
  188. {
  189. case InputDeviceType::MOUSE:
  190. im->mouse()->set_axis(ev.axis_num, vector3(ev.axis_x, ev.axis_y, ev.axis_z));
  191. if (ev.axis_num == MouseAxis::CURSOR)
  192. {
  193. mouse_x = (s16)ev.axis_x;
  194. mouse_y = (s16)ev.axis_y;
  195. }
  196. break;
  197. case InputDeviceType::JOYPAD:
  198. im->joypad(ev.device_num)->set_axis(ev.axis_num, vector3(ev.axis_x, ev.axis_y, ev.axis_z));
  199. break;
  200. }
  201. }
  202. break;
  203. case OsEventType::STATUS:
  204. {
  205. const StatusEvent ev = event.status;
  206. switch (ev.device_id)
  207. {
  208. case InputDeviceType::JOYPAD:
  209. im->joypad(ev.device_num)->set_connected(ev.connected);
  210. break;
  211. }
  212. }
  213. break;
  214. case OsEventType::RESOLUTION:
  215. {
  216. const ResolutionEvent& ev = event.resolution;
  217. _width = ev.width;
  218. _height = ev.height;
  219. reset = true;
  220. }
  221. break;
  222. case OsEventType::EXIT:
  223. exit = true;
  224. break;
  225. case OsEventType::PAUSE:
  226. pause();
  227. break;
  228. case OsEventType::RESUME:
  229. unpause();
  230. break;
  231. default:
  232. CE_FATAL("Unknown OS event");
  233. break;
  234. }
  235. }
  236. const s16 dt_x = mouse_x - mouse_last_x;
  237. const s16 dt_y = mouse_y - mouse_last_y;
  238. im->mouse()->set_axis(MouseAxis::CURSOR_DELTA, vector3(dt_x, dt_y, 0.0f));
  239. mouse_last_x = mouse_x;
  240. mouse_last_y = mouse_y;
  241. if (reset)
  242. bgfx::reset(_width, _height, (vsync ? BGFX_RESET_VSYNC : BGFX_RESET_NONE));
  243. return exit;
  244. }
  245. void Device::run()
  246. {
  247. console_server_globals::init();
  248. load_console_api(*console_server());
  249. console_server()->listen(_device_options._console_port, _device_options._wait_console);
  250. namespace cor = config_resource_internal;
  251. namespace ftr = font_resource_internal;
  252. namespace lur = lua_resource_internal;
  253. namespace lvr = level_resource_internal;
  254. namespace mhr = mesh_resource_internal;
  255. namespace mtr = material_resource_internal;
  256. namespace pcr = physics_config_resource_internal;
  257. namespace phr = physics_resource_internal;
  258. namespace pkr = package_resource_internal;
  259. namespace sar = sprite_animation_resource_internal;
  260. namespace sdr = sound_resource_internal;
  261. namespace shr = shader_resource_internal;
  262. namespace spr = sprite_resource_internal;
  263. namespace txr = texture_resource_internal;
  264. namespace utr = unit_resource_internal;
  265. #if CROWN_PLATFORM_ANDROID
  266. _bundle_filesystem = CE_NEW(_allocator, FilesystemApk)(default_allocator(), const_cast<AAssetManager*>((AAssetManager*)_device_options._asset_manager));
  267. #else
  268. const char* data_dir = _device_options._data_dir.c_str();
  269. if (!data_dir)
  270. {
  271. char buf[1024];
  272. data_dir = os::getcwd(buf, sizeof(buf));
  273. }
  274. _bundle_filesystem = CE_NEW(_allocator, FilesystemDisk)(default_allocator());
  275. ((FilesystemDisk*)_bundle_filesystem)->set_prefix(data_dir);
  276. if (!_bundle_filesystem->exists(data_dir))
  277. _bundle_filesystem->create_directory(data_dir);
  278. _last_log = _bundle_filesystem->open(CROWN_LAST_LOG, FileOpenMode::WRITE);
  279. #endif // CROWN_PLATFORM_ANDROID
  280. logi(DEVICE, "Initializing Crown Engine %s %s %s", version(), platform(), architecture());
  281. profiler_globals::init();
  282. _resource_loader = CE_NEW(_allocator, ResourceLoader)(*_bundle_filesystem);
  283. _resource_manager = CE_NEW(_allocator, ResourceManager)(*_resource_loader);
  284. _resource_manager->register_type(RESOURCE_TYPE_SCRIPT, lur::load, lur::unload, NULL, NULL );
  285. _resource_manager->register_type(RESOURCE_TYPE_TEXTURE, txr::load, txr::unload, txr::online, txr::offline);
  286. _resource_manager->register_type(RESOURCE_TYPE_MESH, mhr::load, mhr::unload, mhr::online, mhr::offline);
  287. _resource_manager->register_type(RESOURCE_TYPE_SOUND, sdr::load, sdr::unload, NULL, NULL );
  288. _resource_manager->register_type(RESOURCE_TYPE_UNIT, utr::load, utr::unload, NULL, NULL );
  289. _resource_manager->register_type(RESOURCE_TYPE_SPRITE, spr::load, spr::unload, NULL, NULL );
  290. _resource_manager->register_type(RESOURCE_TYPE_PACKAGE, pkr::load, pkr::unload, NULL, NULL );
  291. _resource_manager->register_type(RESOURCE_TYPE_PHYSICS, phr::load, phr::unload, NULL, NULL );
  292. _resource_manager->register_type(RESOURCE_TYPE_MATERIAL, mtr::load, mtr::unload, mtr::online, mtr::offline);
  293. _resource_manager->register_type(RESOURCE_TYPE_PHYSICS_CONFIG, pcr::load, pcr::unload, NULL, NULL );
  294. _resource_manager->register_type(RESOURCE_TYPE_FONT, ftr::load, ftr::unload, NULL, NULL );
  295. _resource_manager->register_type(RESOURCE_TYPE_LEVEL, lvr::load, lvr::unload, NULL, NULL );
  296. _resource_manager->register_type(RESOURCE_TYPE_SHADER, shr::load, shr::unload, shr::online, shr::offline);
  297. _resource_manager->register_type(RESOURCE_TYPE_SPRITE_ANIMATION, sar::load, sar::unload, NULL, NULL );
  298. _resource_manager->register_type(RESOURCE_TYPE_CONFIG, cor::load, cor::unload, NULL, NULL );
  299. // Read config
  300. {
  301. TempAllocator512 ta;
  302. DynamicString boot_dir(ta);
  303. if (_device_options._boot_dir != NULL)
  304. {
  305. boot_dir += _device_options._boot_dir;
  306. boot_dir += '/';
  307. }
  308. boot_dir += CROWN_BOOT_CONFIG;
  309. const StringId64 config_name(boot_dir.c_str());
  310. _resource_manager->load(RESOURCE_TYPE_CONFIG, config_name);
  311. _resource_manager->flush();
  312. _boot_config.parse((const char*)_resource_manager->get(RESOURCE_TYPE_CONFIG, config_name));
  313. _resource_manager->unload(RESOURCE_TYPE_CONFIG, config_name);
  314. }
  315. // Init all remaining subsystems
  316. _bgfx_allocator = CE_NEW(_allocator, BgfxAllocator)(default_allocator());
  317. _bgfx_callback = CE_NEW(_allocator, BgfxCallback)();
  318. _display = display::create(_allocator);
  319. _window = window::create(_allocator);
  320. _window->open(_device_options._window_x
  321. , _device_options._window_y
  322. , _boot_config.window_w
  323. , _boot_config.window_h
  324. , _device_options._parent_window
  325. );
  326. _window->set_title(_boot_config.window_title.c_str());
  327. _window->set_fullscreen(_boot_config.fullscreen);
  328. _window->bgfx_setup();
  329. bgfx::init(bgfx::RendererType::Count
  330. , BGFX_PCI_ID_NONE
  331. , 0
  332. , _bgfx_callback
  333. , _bgfx_allocator
  334. );
  335. _shader_manager = CE_NEW(_allocator, ShaderManager)(default_allocator());
  336. _material_manager = CE_NEW(_allocator, MaterialManager)(default_allocator(), *_resource_manager);
  337. _input_manager = CE_NEW(_allocator, InputManager)(default_allocator());
  338. _unit_manager = CE_NEW(_allocator, UnitManager)(default_allocator());
  339. _lua_environment = CE_NEW(_allocator, LuaEnvironment)();
  340. audio_globals::init();
  341. physics_globals::init(_allocator);
  342. ResourcePackage* boot_package = create_resource_package(_boot_config.boot_package_name);
  343. boot_package->load();
  344. boot_package->flush();
  345. _lua_environment->load_libs();
  346. _lua_environment->execute((LuaResource*)_resource_manager->get(RESOURCE_TYPE_SCRIPT, _boot_config.boot_script_name));
  347. _lua_environment->call_global("init", 0);
  348. logi(DEVICE, "Initialized");
  349. s16 mouse_x = 0;
  350. s16 mouse_y = 0;
  351. s16 mouse_last_x = 0;
  352. s16 mouse_last_y = 0;
  353. s64 last_time = os::clocktime();
  354. s64 curr_time;
  355. while (!process_events(mouse_x, mouse_y, mouse_last_x, mouse_last_y, _boot_config.vsync) && !_quit)
  356. {
  357. curr_time = os::clocktime();
  358. const s64 time = curr_time - last_time;
  359. last_time = curr_time;
  360. const f64 freq = (f64)os::clockfrequency();
  361. _last_delta_time = f32(time * (1.0 / freq));
  362. _time_since_start += _last_delta_time;
  363. profiler_globals::clear();
  364. console_server()->update();
  365. RECORD_FLOAT("device.dt", _last_delta_time);
  366. RECORD_FLOAT("device.fps", 1.0f/_last_delta_time);
  367. if (!_paused)
  368. {
  369. _resource_manager->complete_requests();
  370. {
  371. const s64 t0 = os::clocktime();
  372. _lua_environment->call_global("update", 1, ARGUMENT_FLOAT, last_delta_time());
  373. const s64 t1 = os::clocktime();
  374. RECORD_FLOAT("lua.update", f32((t1 - t0)*(1.0 / freq)));
  375. }
  376. {
  377. const s64 t0 = os::clocktime();
  378. _lua_environment->call_global("render", 1, ARGUMENT_FLOAT, last_delta_time());
  379. const s64 t1 = os::clocktime();
  380. RECORD_FLOAT("lua.render", f32((t1 - t0)*(1.0 / freq)));
  381. }
  382. }
  383. _input_manager->update();
  384. const bgfx::Stats* stats = bgfx::getStats();
  385. RECORD_FLOAT("bgfx.gpu_time", f32(f64(stats->gpuTimeEnd - stats->gpuTimeBegin)*1000.0/stats->gpuTimerFreq));
  386. RECORD_FLOAT("bgfx.cpu_time", f32(f64(stats->cpuTimeEnd - stats->cpuTimeBegin)*1000.0/stats->cpuTimerFreq));
  387. bgfx::frame();
  388. profiler_globals::flush();
  389. _lua_environment->reset_temporaries();
  390. _frame_count++;
  391. }
  392. _lua_environment->call_global("shutdown", 0);
  393. boot_package->unload();
  394. destroy_resource_package(*boot_package);
  395. physics_globals::shutdown(_allocator);
  396. audio_globals::shutdown();
  397. CE_DELETE(_allocator, _lua_environment);
  398. CE_DELETE(_allocator, _unit_manager);
  399. CE_DELETE(_allocator, _input_manager);
  400. CE_DELETE(_allocator, _material_manager);
  401. CE_DELETE(_allocator, _shader_manager);
  402. CE_DELETE(_allocator, _resource_manager);
  403. CE_DELETE(_allocator, _resource_loader);
  404. bgfx::shutdown();
  405. _window->close();
  406. window::destroy(_allocator, *_window);
  407. display::destroy(_allocator, *_display);
  408. CE_DELETE(_allocator, _bgfx_callback);
  409. CE_DELETE(_allocator, _bgfx_allocator);
  410. if (_last_log)
  411. _bundle_filesystem->close(*_last_log);
  412. CE_DELETE(_allocator, _bundle_filesystem);
  413. profiler_globals::shutdown();
  414. console_server_globals::shutdown();
  415. _allocator.clear();
  416. }
  417. void Device::quit()
  418. {
  419. _quit = true;
  420. }
  421. void Device::pause()
  422. {
  423. _paused = true;
  424. logi(DEVICE, "Paused");
  425. }
  426. void Device::unpause()
  427. {
  428. _paused = false;
  429. logi(DEVICE, "Unpaused");
  430. }
  431. void Device::resolution(u16& width, u16& height)
  432. {
  433. width = _width;
  434. height = _height;
  435. }
  436. u64 Device::frame_count() const
  437. {
  438. return _frame_count;
  439. }
  440. f32 Device::last_delta_time() const
  441. {
  442. return _last_delta_time;
  443. }
  444. f64 Device::time_since_start() const
  445. {
  446. return _time_since_start;
  447. }
  448. void Device::render(World& world, CameraInstance camera)
  449. {
  450. bgfx::setViewClear(0
  451. , BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
  452. , 0x353839ff
  453. , 1.0f
  454. , 0
  455. );
  456. bgfx::setViewRect(0, 0, 0, _width, _height);
  457. bgfx::setViewRect(1, 0, 0, _width, _height);
  458. bgfx::setViewRect(2, 0, 0, _width, _height);
  459. const Matrix4x4 view = world.camera_view_matrix(camera);
  460. const Matrix4x4 proj = world.camera_projection_matrix(camera);
  461. bgfx::setViewTransform(0, to_float_ptr(view), to_float_ptr(proj));
  462. bgfx::setViewTransform(1, to_float_ptr(view), to_float_ptr(proj));
  463. bgfx::setViewTransform(2, to_float_ptr(MATRIX4X4_IDENTITY), to_float_ptr(MATRIX4X4_IDENTITY));
  464. bgfx::setViewSeq(2, true);
  465. bgfx::touch(0);
  466. bgfx::touch(1);
  467. bgfx::touch(2);
  468. float aspect_ratio = (_boot_config.aspect_ratio == -1.0f
  469. ? (float)_width/(float)_height
  470. : _boot_config.aspect_ratio
  471. );
  472. world.camera_set_aspect(camera, aspect_ratio);
  473. world.camera_set_viewport_metrics(camera, 0, 0, _width, _height);
  474. world.render(view, proj);
  475. }
  476. World* Device::create_world()
  477. {
  478. World* w = CE_NEW(default_allocator(), World)(default_allocator()
  479. , *_resource_manager
  480. , *_shader_manager
  481. , *_material_manager
  482. , *_unit_manager
  483. , *_lua_environment
  484. );
  485. array::push_back(_worlds, w);
  486. return w;
  487. }
  488. void Device::destroy_world(World& w)
  489. {
  490. for (u32 i = 0, n = array::size(_worlds); i < n; ++i)
  491. {
  492. if (&w == _worlds[i])
  493. {
  494. CE_DELETE(default_allocator(), &w);
  495. _worlds[i] = _worlds[n - 1];
  496. array::pop_back(_worlds);
  497. return;
  498. }
  499. }
  500. CE_FATAL("Bad world");
  501. }
  502. ResourcePackage* Device::create_resource_package(StringId64 id)
  503. {
  504. return CE_NEW(default_allocator(), ResourcePackage)(id, *_resource_manager);
  505. }
  506. void Device::destroy_resource_package(ResourcePackage& rp)
  507. {
  508. CE_DELETE(default_allocator(), &rp);
  509. }
  510. void Device::reload(StringId64 type, StringId64 name)
  511. {
  512. TempAllocator64 ta;
  513. DynamicString type_str(ta);
  514. DynamicString name_str(ta);
  515. type.to_string(type_str);
  516. name.to_string(name_str);
  517. logi(DEVICE, "Reloading #ID(%s-%s)", type_str.c_str(), name_str.c_str());
  518. _resource_manager->reload(type, name);
  519. const void* new_resource = _resource_manager->get(type, name);
  520. if (type == RESOURCE_TYPE_SCRIPT)
  521. {
  522. _lua_environment->execute((const LuaResource*)new_resource);
  523. }
  524. logi(DEVICE, "Reloaded #ID(%s-%s)", type_str.c_str(), name_str.c_str());
  525. }
  526. void Device::log(const char* msg)
  527. {
  528. if (_last_log)
  529. {
  530. _last_log->write(msg, strlen32(msg));
  531. _last_log->write("\n", 1);
  532. _last_log->flush();
  533. }
  534. }
  535. char _buffer[sizeof(Device)];
  536. Device* _device = NULL;
  537. void run(const DeviceOptions& opts)
  538. {
  539. CE_ASSERT(_device == NULL, "Crown already initialized");
  540. _device = new (_buffer) Device(opts);
  541. _device->run();
  542. _device->~Device();
  543. _device = NULL;
  544. }
  545. Device* device()
  546. {
  547. return crown::_device;
  548. }
  549. } // namespace crown