entry.cpp 21 KB

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