entry.cpp 21 KB

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