entry.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. /*
  2. * Copyright 2011-2017 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include <bx/bx.h>
  6. #include <bgfx/bgfx.h>
  7. #include <bx/string.h>
  8. #include <bx/crtimpl.h>
  9. #include <time.h>
  10. #if BX_PLATFORM_EMSCRIPTEN
  11. # include <emscripten.h>
  12. #endif // BX_PLATFORM_EMSCRIPTEN
  13. #include "entry_p.h"
  14. #include "cmd.h"
  15. #include "input.h"
  16. #define RMT_ENABLED ENTRY_CONFIG_PROFILER
  17. #include <remotery/lib/Remotery.h>
  18. extern "C" int _main_(int _argc, char** _argv);
  19. namespace entry
  20. {
  21. static uint32_t s_debug = BGFX_DEBUG_NONE;
  22. static uint32_t s_reset = BGFX_RESET_NONE;
  23. static bool s_exit = false;
  24. static Remotery* s_rmt = NULL;
  25. static bx::FileReaderI* s_fileReader = NULL;
  26. static bx::FileWriterI* s_fileWriter = NULL;
  27. extern bx::AllocatorI* getDefaultAllocator();
  28. bx::AllocatorI* g_allocator = getDefaultAllocator();
  29. typedef bx::StringT<&g_allocator> String;
  30. void* rmtMalloc(void* /*_context*/, rmtU32 _size)
  31. {
  32. return BX_ALLOC(g_allocator, _size);
  33. }
  34. void* rmtRealloc(void* /*_context*/, void* _ptr, rmtU32 _size)
  35. {
  36. return BX_REALLOC(g_allocator, _ptr, _size);
  37. }
  38. void rmtFree(void* /*_context*/, void* _ptr)
  39. {
  40. BX_FREE(g_allocator, _ptr);
  41. }
  42. static String s_currentDir;
  43. #if BX_CONFIG_CRT_FILE_READER_WRITER
  44. class FileReader : public bx::CrtFileReader
  45. {
  46. typedef bx::CrtFileReader super;
  47. public:
  48. virtual bool open(const char* _filePath, bx::Error* _err) BX_OVERRIDE
  49. {
  50. String filePath(s_currentDir);
  51. filePath.append(_filePath);
  52. return super::open(filePath.getPtr(), _err);
  53. }
  54. };
  55. class FileWriter : public bx::CrtFileWriter
  56. {
  57. typedef bx::CrtFileWriter super;
  58. public:
  59. virtual bool open(const char* _filePath, bool _append, bx::Error* _err) BX_OVERRIDE
  60. {
  61. String filePath(s_currentDir);
  62. filePath.append(_filePath);
  63. return super::open(filePath.getPtr(), _append, _err);
  64. }
  65. };
  66. #endif // BX_CONFIG_CRT_FILE_READER_WRITER
  67. void setCurrentDir(const char* _dir)
  68. {
  69. s_currentDir.set(_dir);
  70. }
  71. #if ENTRY_CONFIG_IMPLEMENT_DEFAULT_ALLOCATOR
  72. bx::AllocatorI* getDefaultAllocator()
  73. {
  74. BX_PRAGMA_DIAGNOSTIC_PUSH();
  75. BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4459); // warning C4459: declaration of 's_allocator' hides global declaration
  76. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wshadow");
  77. static bx::DefaultAllocator s_allocator;
  78. return &s_allocator;
  79. BX_PRAGMA_DIAGNOSTIC_POP();
  80. }
  81. #endif // ENTRY_CONFIG_IMPLEMENT_DEFAULT_ALLOCATOR
  82. static const char* s_keyName[] =
  83. {
  84. "None",
  85. "Esc",
  86. "Return",
  87. "Tab",
  88. "Space",
  89. "Backspace",
  90. "Up",
  91. "Down",
  92. "Left",
  93. "Right",
  94. "Insert",
  95. "Delete",
  96. "Home",
  97. "End",
  98. "PageUp",
  99. "PageDown",
  100. "Print",
  101. "Plus",
  102. "Minus",
  103. "LeftBracket",
  104. "RightBracket",
  105. "Semicolon",
  106. "Quote",
  107. "Comma",
  108. "Period",
  109. "Slash",
  110. "Backslash",
  111. "Tilde",
  112. "F1",
  113. "F2",
  114. "F3",
  115. "F4",
  116. "F5",
  117. "F6",
  118. "F7",
  119. "F8",
  120. "F9",
  121. "F10",
  122. "F11",
  123. "F12",
  124. "NumPad0",
  125. "NumPad1",
  126. "NumPad2",
  127. "NumPad3",
  128. "NumPad4",
  129. "NumPad5",
  130. "NumPad6",
  131. "NumPad7",
  132. "NumPad8",
  133. "NumPad9",
  134. "Key0",
  135. "Key1",
  136. "Key2",
  137. "Key3",
  138. "Key4",
  139. "Key5",
  140. "Key6",
  141. "Key7",
  142. "Key8",
  143. "Key9",
  144. "KeyA",
  145. "KeyB",
  146. "KeyC",
  147. "KeyD",
  148. "KeyE",
  149. "KeyF",
  150. "KeyG",
  151. "KeyH",
  152. "KeyI",
  153. "KeyJ",
  154. "KeyK",
  155. "KeyL",
  156. "KeyM",
  157. "KeyN",
  158. "KeyO",
  159. "KeyP",
  160. "KeyQ",
  161. "KeyR",
  162. "KeyS",
  163. "KeyT",
  164. "KeyU",
  165. "KeyV",
  166. "KeyW",
  167. "KeyX",
  168. "KeyY",
  169. "KeyZ",
  170. "GamepadA",
  171. "GamepadB",
  172. "GamepadX",
  173. "GamepadY",
  174. "GamepadThumbL",
  175. "GamepadThumbR",
  176. "GamepadShoulderL",
  177. "GamepadShoulderR",
  178. "GamepadUp",
  179. "GamepadDown",
  180. "GamepadLeft",
  181. "GamepadRight",
  182. "GamepadBack",
  183. "GamepadStart",
  184. "GamepadGuide",
  185. };
  186. BX_STATIC_ASSERT(Key::Count == BX_COUNTOF(s_keyName) );
  187. const char* getName(Key::Enum _key)
  188. {
  189. BX_CHECK(_key < Key::Count, "Invalid key %d.", _key);
  190. return s_keyName[_key];
  191. }
  192. char keyToAscii(Key::Enum _key, uint8_t _modifiers)
  193. {
  194. const bool isAscii = (Key::Key0 <= _key && _key <= Key::KeyZ)
  195. || (Key::Esc <= _key && _key <= Key::Minus);
  196. if (!isAscii)
  197. {
  198. return '\0';
  199. }
  200. const bool isNumber = (Key::Key0 <= _key && _key <= Key::Key9);
  201. if (isNumber)
  202. {
  203. return '0' + char(_key - Key::Key0);
  204. }
  205. const bool isChar = (Key::KeyA <= _key && _key <= Key::KeyZ);
  206. if (isChar)
  207. {
  208. enum { ShiftMask = Modifier::LeftShift|Modifier::RightShift };
  209. const bool shift = !!(_modifiers&ShiftMask);
  210. return (shift ? 'A' : 'a') + char(_key - Key::KeyA);
  211. }
  212. switch (_key)
  213. {
  214. case Key::Esc: return 0x1b;
  215. case Key::Return: return '\n';
  216. case Key::Tab: return '\t';
  217. case Key::Space: return ' ';
  218. case Key::Backspace: return 0x08;
  219. case Key::Plus: return '+';
  220. case Key::Minus: return '-';
  221. default: break;
  222. }
  223. return '\0';
  224. }
  225. bool setOrToggle(uint32_t& _flags, const char* _name, uint32_t _bit, int _first, int _argc, char const* const* _argv)
  226. {
  227. if (0 == bx::strCmp(_argv[_first], _name) )
  228. {
  229. int arg = _first+1;
  230. if (_argc > arg)
  231. {
  232. _flags &= ~_bit;
  233. _flags |= bx::toBool(_argv[arg]) ? _bit : 0;
  234. }
  235. else
  236. {
  237. _flags ^= _bit;
  238. }
  239. return true;
  240. }
  241. return false;
  242. }
  243. int cmdMouseLock(CmdContext* /*_context*/, void* /*_userData*/, int _argc, char const* const* _argv)
  244. {
  245. if (_argc > 1)
  246. {
  247. inputSetMouseLock(_argc > 1 ? bx::toBool(_argv[1]) : !inputIsMouseLocked() );
  248. return 0;
  249. }
  250. return 1;
  251. }
  252. int cmdGraphics(CmdContext* /*_context*/, void* /*_userData*/, int _argc, char const* const* _argv)
  253. {
  254. if (_argc > 1)
  255. {
  256. if (setOrToggle(s_reset, "vsync", BGFX_RESET_VSYNC, 1, _argc, _argv)
  257. || setOrToggle(s_reset, "maxaniso", BGFX_RESET_MAXANISOTROPY, 1, _argc, _argv)
  258. || setOrToggle(s_reset, "hmd", BGFX_RESET_HMD, 1, _argc, _argv)
  259. || setOrToggle(s_reset, "hmddbg", BGFX_RESET_HMD_DEBUG, 1, _argc, _argv)
  260. || setOrToggle(s_reset, "hmdrecenter", BGFX_RESET_HMD_RECENTER, 1, _argc, _argv)
  261. || setOrToggle(s_reset, "msaa", BGFX_RESET_MSAA_X16, 1, _argc, _argv)
  262. || setOrToggle(s_reset, "flush", BGFX_RESET_FLUSH_AFTER_RENDER, 1, _argc, _argv)
  263. || setOrToggle(s_reset, "flip", BGFX_RESET_FLIP_AFTER_RENDER, 1, _argc, _argv)
  264. || setOrToggle(s_reset, "hidpi", BGFX_RESET_HIDPI, 1, _argc, _argv)
  265. || setOrToggle(s_reset, "depthclamp", BGFX_RESET_DEPTH_CLAMP, 1, _argc, _argv)
  266. )
  267. {
  268. return 0;
  269. }
  270. else if (setOrToggle(s_debug, "stats", BGFX_DEBUG_STATS, 1, _argc, _argv)
  271. || setOrToggle(s_debug, "ifh", BGFX_DEBUG_IFH, 1, _argc, _argv)
  272. || setOrToggle(s_debug, "text", BGFX_DEBUG_TEXT, 1, _argc, _argv)
  273. || setOrToggle(s_debug, "wireframe", BGFX_DEBUG_WIREFRAME, 1, _argc, _argv) )
  274. {
  275. bgfx::setDebug(s_debug);
  276. return 0;
  277. }
  278. else if (0 == bx::strCmp(_argv[1], "screenshot") )
  279. {
  280. bgfx::FrameBufferHandle fbh = BGFX_INVALID_HANDLE;
  281. if (_argc > 2)
  282. {
  283. bgfx::requestScreenShot(fbh, _argv[2]);
  284. }
  285. else
  286. {
  287. time_t tt;
  288. time(&tt);
  289. char filePath[256];
  290. bx::snprintf(filePath, sizeof(filePath), "temp/screenshot-%d", tt);
  291. bgfx::requestScreenShot(fbh, filePath);
  292. }
  293. return 0;
  294. }
  295. else if (0 == bx::strCmp(_argv[1], "fullscreen") )
  296. {
  297. WindowHandle window = { 0 };
  298. toggleFullscreen(window);
  299. return 0;
  300. }
  301. }
  302. return 1;
  303. }
  304. int cmdExit(CmdContext* /*_context*/, void* /*_userData*/, int /*_argc*/, char const* const* /*_argv*/)
  305. {
  306. s_exit = true;
  307. return 0;
  308. }
  309. static const InputBinding s_bindings[] =
  310. {
  311. { entry::Key::KeyQ, entry::Modifier::LeftCtrl, 1, NULL, "exit" },
  312. { entry::Key::KeyQ, entry::Modifier::RightCtrl, 1, NULL, "exit" },
  313. { entry::Key::KeyF, entry::Modifier::LeftCtrl, 1, NULL, "graphics fullscreen" },
  314. { entry::Key::KeyF, entry::Modifier::RightCtrl, 1, NULL, "graphics fullscreen" },
  315. { entry::Key::Return, entry::Modifier::RightAlt, 1, NULL, "graphics fullscreen" },
  316. { entry::Key::F1, entry::Modifier::None, 1, NULL, "graphics stats" },
  317. { entry::Key::F1, entry::Modifier::LeftCtrl, 1, NULL, "graphics ifh" },
  318. { entry::Key::GamepadStart, entry::Modifier::None, 1, NULL, "graphics stats" },
  319. { entry::Key::F1, entry::Modifier::LeftShift, 1, NULL, "graphics stats 0\ngraphics text 0" },
  320. { entry::Key::F3, entry::Modifier::None, 1, NULL, "graphics wireframe" },
  321. { entry::Key::F4, entry::Modifier::None, 1, NULL, "graphics hmd" },
  322. { entry::Key::F4, entry::Modifier::LeftShift, 1, NULL, "graphics hmdrecenter" },
  323. { entry::Key::F4, entry::Modifier::LeftCtrl, 1, NULL, "graphics hmddbg" },
  324. { entry::Key::F7, entry::Modifier::None, 1, NULL, "graphics vsync" },
  325. { entry::Key::F8, entry::Modifier::None, 1, NULL, "graphics msaa" },
  326. { entry::Key::F9, entry::Modifier::None, 1, NULL, "graphics flush" },
  327. { entry::Key::F10, entry::Modifier::None, 1, NULL, "graphics hidpi" },
  328. { entry::Key::Print, entry::Modifier::None, 1, NULL, "graphics screenshot" },
  329. { entry::Key::KeyP, entry::Modifier::LeftCtrl, 1, NULL, "graphics screenshot" },
  330. INPUT_BINDING_END
  331. };
  332. #if BX_PLATFORM_EMSCRIPTEN
  333. static AppI* s_app;
  334. static void updateApp()
  335. {
  336. s_app->update();
  337. }
  338. #endif // BX_PLATFORM_EMSCRIPTEN
  339. static App* s_apps = NULL;
  340. App::App(const char* _name)
  341. {
  342. m_name = _name;
  343. m_next = s_apps;
  344. s_apps = this;
  345. }
  346. App::~App()
  347. {
  348. }
  349. App* getFirstApp()
  350. {
  351. return s_apps;
  352. }
  353. int runApp(AppI* _app, int _argc, char** _argv)
  354. {
  355. _app->init(_argc, _argv);
  356. bgfx::frame();
  357. WindowHandle defaultWindow = { 0 };
  358. setWindowSize(defaultWindow, ENTRY_DEFAULT_WIDTH, ENTRY_DEFAULT_HEIGHT);
  359. #if BX_PLATFORM_EMSCRIPTEN
  360. s_app = _app;
  361. emscripten_set_main_loop(&updateApp, -1, 1);
  362. #else
  363. while (_app->update() );
  364. #endif // BX_PLATFORM_EMSCRIPTEN
  365. return _app->shutdown();
  366. }
  367. int main(int _argc, char** _argv)
  368. {
  369. //DBG(BX_COMPILER_NAME " / " BX_CPU_NAME " / " BX_ARCH_NAME " / " BX_PLATFORM_NAME);
  370. if (BX_ENABLED(ENTRY_CONFIG_PROFILER) )
  371. {
  372. rmtSettings* settings = rmt_Settings();
  373. BX_WARN(NULL != settings, "Remotery is not enabled.");
  374. if (NULL != settings)
  375. {
  376. settings->malloc = rmtMalloc;
  377. settings->realloc = rmtRealloc;
  378. settings->free = rmtFree;
  379. rmtError err = rmt_CreateGlobalInstance(&s_rmt);
  380. BX_WARN(RMT_ERROR_NONE != err, "Remotery failed to create global instance.");
  381. if (RMT_ERROR_NONE == err)
  382. {
  383. rmt_SetCurrentThreadName("Main");
  384. }
  385. else
  386. {
  387. s_rmt = NULL;
  388. }
  389. }
  390. }
  391. #if BX_CONFIG_CRT_FILE_READER_WRITER
  392. s_fileReader = BX_NEW(g_allocator, FileReader);
  393. s_fileWriter = BX_NEW(g_allocator, FileWriter);
  394. #endif // BX_CONFIG_CRT_FILE_READER_WRITER
  395. cmdInit();
  396. cmdAdd("mouselock", cmdMouseLock);
  397. cmdAdd("graphics", cmdGraphics );
  398. cmdAdd("exit", cmdExit );
  399. inputInit();
  400. inputAddBindings("bindings", s_bindings);
  401. entry::WindowHandle defaultWindow = { 0 };
  402. entry::setWindowTitle(defaultWindow, bx::baseName(_argv[0]) );
  403. setWindowSize(defaultWindow, ENTRY_DEFAULT_WIDTH, ENTRY_DEFAULT_HEIGHT);
  404. int32_t result = ::_main_(_argc, _argv);
  405. setCurrentDir("");
  406. inputRemoveBindings("bindings");
  407. inputShutdown();
  408. cmdShutdown();
  409. #if BX_CONFIG_CRT_FILE_READER_WRITER
  410. BX_DELETE(g_allocator, s_fileReader);
  411. s_fileReader = NULL;
  412. BX_DELETE(g_allocator, s_fileWriter);
  413. s_fileWriter = NULL;
  414. #endif // BX_CONFIG_CRT_FILE_READER_WRITER
  415. if (BX_ENABLED(ENTRY_CONFIG_PROFILER)
  416. && NULL != s_rmt)
  417. {
  418. rmt_DestroyGlobalInstance(s_rmt);
  419. }
  420. return result;
  421. }
  422. bool processEvents(uint32_t& _width, uint32_t& _height, uint32_t& _debug, uint32_t& _reset, MouseState* _mouse)
  423. {
  424. s_debug = _debug;
  425. s_reset = _reset;
  426. WindowHandle handle = { UINT16_MAX };
  427. bool mouseLock = inputIsMouseLocked();
  428. const Event* ev;
  429. do
  430. {
  431. struct SE { const Event* m_ev; SE() : m_ev(poll() ) {} ~SE() { if (NULL != m_ev) { release(m_ev); } } } scopeEvent;
  432. ev = scopeEvent.m_ev;
  433. if (NULL != ev)
  434. {
  435. switch (ev->m_type)
  436. {
  437. case Event::Axis:
  438. {
  439. const AxisEvent* axis = static_cast<const AxisEvent*>(ev);
  440. inputSetGamepadAxis(axis->m_gamepad, axis->m_axis, axis->m_value);
  441. }
  442. break;
  443. case Event::Char:
  444. {
  445. const CharEvent* chev = static_cast<const CharEvent*>(ev);
  446. inputChar(chev->m_len, chev->m_char);
  447. }
  448. break;
  449. case Event::Exit:
  450. return true;
  451. case Event::Gamepad:
  452. {
  453. // const GamepadEvent* gev = static_cast<const GamepadEvent*>(ev);
  454. // DBG("gamepad %d, %d", gev->m_gamepad.idx, gev->m_connected);
  455. }
  456. break;
  457. case Event::Mouse:
  458. {
  459. const MouseEvent* mouse = static_cast<const MouseEvent*>(ev);
  460. handle = mouse->m_handle;
  461. inputSetMousePos(mouse->m_mx, mouse->m_my, mouse->m_mz);
  462. if (!mouse->m_move)
  463. {
  464. inputSetMouseButtonState(mouse->m_button, mouse->m_down);
  465. }
  466. if (NULL != _mouse
  467. && !mouseLock)
  468. {
  469. _mouse->m_mx = mouse->m_mx;
  470. _mouse->m_my = mouse->m_my;
  471. _mouse->m_mz = mouse->m_mz;
  472. if (!mouse->m_move)
  473. {
  474. _mouse->m_buttons[mouse->m_button] = mouse->m_down;
  475. }
  476. }
  477. }
  478. break;
  479. case Event::Key:
  480. {
  481. const KeyEvent* key = static_cast<const KeyEvent*>(ev);
  482. handle = key->m_handle;
  483. inputSetKeyState(key->m_key, key->m_modifiers, key->m_down);
  484. }
  485. break;
  486. case Event::Size:
  487. {
  488. const SizeEvent* size = static_cast<const SizeEvent*>(ev);
  489. handle = size->m_handle;
  490. _width = size->m_width;
  491. _height = size->m_height;
  492. _reset = !s_reset; // force reset
  493. }
  494. break;
  495. case Event::Window:
  496. break;
  497. case Event::Suspend:
  498. break;
  499. default:
  500. break;
  501. }
  502. }
  503. inputProcess();
  504. } while (NULL != ev);
  505. if (handle.idx == 0
  506. && _reset != s_reset)
  507. {
  508. _reset = s_reset;
  509. bgfx::reset(_width, _height, _reset);
  510. inputSetMouseResolution(uint16_t(_width), uint16_t(_height) );
  511. }
  512. _debug = s_debug;
  513. return s_exit;
  514. }
  515. WindowState s_window[ENTRY_CONFIG_MAX_WINDOWS];
  516. bool processWindowEvents(WindowState& _state, uint32_t& _debug, uint32_t& _reset)
  517. {
  518. s_debug = _debug;
  519. s_reset = _reset;
  520. WindowHandle handle = { UINT16_MAX };
  521. bool mouseLock = inputIsMouseLocked();
  522. const Event* ev;
  523. do
  524. {
  525. struct SE
  526. {
  527. SE(WindowHandle _handle)
  528. : m_ev(poll(_handle) )
  529. {
  530. }
  531. ~SE()
  532. {
  533. if (NULL != m_ev)
  534. {
  535. release(m_ev);
  536. }
  537. }
  538. const Event* m_ev;
  539. } scopeEvent(handle);
  540. ev = scopeEvent.m_ev;
  541. if (NULL != ev)
  542. {
  543. handle = ev->m_handle;
  544. WindowState& win = s_window[handle.idx];
  545. switch (ev->m_type)
  546. {
  547. case Event::Axis:
  548. {
  549. const AxisEvent* axis = static_cast<const AxisEvent*>(ev);
  550. inputSetGamepadAxis(axis->m_gamepad, axis->m_axis, axis->m_value);
  551. }
  552. break;
  553. case Event::Char:
  554. {
  555. const CharEvent* chev = static_cast<const CharEvent*>(ev);
  556. win.m_handle = chev->m_handle;
  557. inputChar(chev->m_len, chev->m_char);
  558. }
  559. break;
  560. case Event::Exit:
  561. return true;
  562. case Event::Gamepad:
  563. {
  564. const GamepadEvent* gev = static_cast<const GamepadEvent*>(ev);
  565. DBG("gamepad %d, %d", gev->m_gamepad.idx, gev->m_connected);
  566. }
  567. break;
  568. case Event::Mouse:
  569. {
  570. const MouseEvent* mouse = static_cast<const MouseEvent*>(ev);
  571. win.m_handle = mouse->m_handle;
  572. if (mouse->m_move)
  573. {
  574. inputSetMousePos(mouse->m_mx, mouse->m_my, mouse->m_mz);
  575. }
  576. else
  577. {
  578. inputSetMouseButtonState(mouse->m_button, mouse->m_down);
  579. }
  580. if (!mouseLock)
  581. {
  582. if (mouse->m_move)
  583. {
  584. win.m_mouse.m_mx = mouse->m_mx;
  585. win.m_mouse.m_my = mouse->m_my;
  586. win.m_mouse.m_mz = mouse->m_mz;
  587. }
  588. else
  589. {
  590. win.m_mouse.m_buttons[mouse->m_button] = mouse->m_down;
  591. }
  592. }
  593. }
  594. break;
  595. case Event::Key:
  596. {
  597. const KeyEvent* key = static_cast<const KeyEvent*>(ev);
  598. win.m_handle = key->m_handle;
  599. inputSetKeyState(key->m_key, key->m_modifiers, key->m_down);
  600. }
  601. break;
  602. case Event::Size:
  603. {
  604. const SizeEvent* size = static_cast<const SizeEvent*>(ev);
  605. win.m_handle = size->m_handle;
  606. win.m_width = size->m_width;
  607. win.m_height = size->m_height;
  608. _reset = win.m_handle.idx == 0
  609. ? !s_reset
  610. : _reset
  611. ; // force reset
  612. }
  613. break;
  614. case Event::Window:
  615. {
  616. const WindowEvent* window = static_cast<const WindowEvent*>(ev);
  617. win.m_handle = window->m_handle;
  618. win.m_nwh = window->m_nwh;
  619. ev = NULL;
  620. }
  621. break;
  622. case Event::Suspend:
  623. break;
  624. default:
  625. break;
  626. }
  627. }
  628. inputProcess();
  629. } while (NULL != ev);
  630. if (isValid(handle) )
  631. {
  632. const WindowState& win = s_window[handle.idx];
  633. _state = win;
  634. if (handle.idx == 0)
  635. {
  636. inputSetMouseResolution(uint16_t(win.m_width), uint16_t(win.m_height) );
  637. }
  638. }
  639. if (_reset != s_reset)
  640. {
  641. _reset = s_reset;
  642. bgfx::reset(s_window[0].m_width, s_window[0].m_height, _reset);
  643. inputSetMouseResolution(uint16_t(s_window[0].m_width), uint16_t(s_window[0].m_height) );
  644. }
  645. _debug = s_debug;
  646. return s_exit;
  647. }
  648. bx::FileReaderI* getFileReader()
  649. {
  650. return s_fileReader;
  651. }
  652. bx::FileWriterI* getFileWriter()
  653. {
  654. return s_fileWriter;
  655. }
  656. bx::AllocatorI* getAllocator()
  657. {
  658. return g_allocator;
  659. }
  660. void* TinyStlAllocator::static_allocate(size_t _bytes)
  661. {
  662. return BX_ALLOC(getAllocator(), _bytes);
  663. }
  664. void TinyStlAllocator::static_deallocate(void* _ptr, size_t /*_bytes*/)
  665. {
  666. if (NULL != _ptr)
  667. {
  668. BX_FREE(getAllocator(), _ptr);
  669. }
  670. }
  671. } // namespace entry
  672. extern "C" bool entry_process_events(uint32_t* _width, uint32_t* _height, uint32_t* _debug, uint32_t* _reset)
  673. {
  674. return entry::processEvents(*_width, *_height, *_debug, *_reset, NULL);
  675. }