entry.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  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. {
  258. bgfx::setDebug(s_debug);
  259. return bx::kExitSuccess;
  260. }
  261. else if (0 == bx::strCmp(_argv[1], "screenshot") )
  262. {
  263. bgfx::FrameBufferHandle fbh = BGFX_INVALID_HANDLE;
  264. if (_argc > 2)
  265. {
  266. bgfx::requestScreenShot(fbh, _argv[2]);
  267. }
  268. else
  269. {
  270. time_t tt;
  271. time(&tt);
  272. char filePath[256];
  273. bx::snprintf(filePath, sizeof(filePath), "temp/screenshot-%d", tt);
  274. bgfx::requestScreenShot(fbh, filePath);
  275. }
  276. return bx::kExitSuccess;
  277. }
  278. else if (0 == bx::strCmp(_argv[1], "fullscreen") )
  279. {
  280. WindowHandle window = { 0 };
  281. toggleFullscreen(window);
  282. return bx::kExitSuccess;
  283. }
  284. }
  285. return bx::kExitFailure;
  286. }
  287. int cmdExit(CmdContext* /*_context*/, void* /*_userData*/, int /*_argc*/, char const* const* /*_argv*/)
  288. {
  289. s_exit = true;
  290. return bx::kExitSuccess;
  291. }
  292. static const InputBinding s_bindings[] =
  293. {
  294. { entry::Key::KeyQ, entry::Modifier::LeftCtrl, 1, NULL, "exit" },
  295. { entry::Key::KeyQ, entry::Modifier::RightCtrl, 1, NULL, "exit" },
  296. { entry::Key::KeyF, entry::Modifier::LeftCtrl, 1, NULL, "graphics fullscreen" },
  297. { entry::Key::KeyF, entry::Modifier::RightCtrl, 1, NULL, "graphics fullscreen" },
  298. { entry::Key::Return, entry::Modifier::RightAlt, 1, NULL, "graphics fullscreen" },
  299. { entry::Key::F1, entry::Modifier::None, 1, NULL, "graphics stats" },
  300. { entry::Key::F1, entry::Modifier::LeftCtrl, 1, NULL, "graphics ifh" },
  301. { entry::Key::GamepadStart, entry::Modifier::None, 1, NULL, "graphics stats" },
  302. { entry::Key::F1, entry::Modifier::LeftShift, 1, NULL, "graphics stats 0\ngraphics text 0" },
  303. { entry::Key::F3, entry::Modifier::None, 1, NULL, "graphics wireframe" },
  304. { entry::Key::F4, entry::Modifier::None, 1, NULL, "graphics hmd" },
  305. { entry::Key::F4, entry::Modifier::LeftShift, 1, NULL, "graphics hmdrecenter" },
  306. { entry::Key::F4, entry::Modifier::LeftCtrl, 1, NULL, "graphics hmddbg" },
  307. { entry::Key::F7, entry::Modifier::None, 1, NULL, "graphics vsync" },
  308. { entry::Key::F8, entry::Modifier::None, 1, NULL, "graphics msaa" },
  309. { entry::Key::F9, entry::Modifier::None, 1, NULL, "graphics flush" },
  310. { entry::Key::F10, entry::Modifier::None, 1, NULL, "graphics hidpi" },
  311. { entry::Key::Print, entry::Modifier::None, 1, NULL, "graphics screenshot" },
  312. { entry::Key::KeyP, entry::Modifier::LeftCtrl, 1, NULL, "graphics screenshot" },
  313. INPUT_BINDING_END
  314. };
  315. #if BX_PLATFORM_EMSCRIPTEN
  316. static AppI* s_app;
  317. static void updateApp()
  318. {
  319. s_app->update();
  320. }
  321. #endif // BX_PLATFORM_EMSCRIPTEN
  322. static AppI* s_currentApp = NULL;
  323. static AppI* s_apps = NULL;
  324. static uint32_t s_numApps = 0;
  325. static char s_restartArgs[1024] = { '\0' };
  326. static AppI* getCurrentApp(AppI* _set = NULL)
  327. {
  328. if (NULL != _set)
  329. {
  330. s_currentApp = _set;
  331. }
  332. else if (NULL == s_currentApp)
  333. {
  334. s_currentApp = getFirstApp();
  335. }
  336. return s_currentApp;
  337. }
  338. static AppI* getNextWrap(AppI* _app)
  339. {
  340. AppI* next = _app->getNext();
  341. if (NULL != next)
  342. {
  343. return next;
  344. }
  345. return getFirstApp();
  346. }
  347. int cmdApp(CmdContext* /*_context*/, void* /*_userData*/, int _argc, char const* const* _argv)
  348. {
  349. if (0 == bx::strCmp(_argv[1], "restart") )
  350. {
  351. if (2 == _argc)
  352. {
  353. bx::strCopy(s_restartArgs, BX_COUNTOF(s_restartArgs), getCurrentApp()->getName() );
  354. return bx::kExitSuccess;
  355. }
  356. if (0 == bx::strCmp(_argv[2], "next") )
  357. {
  358. AppI* next = getNextWrap(getCurrentApp() );
  359. bx::strCopy(s_restartArgs, BX_COUNTOF(s_restartArgs), next->getName() );
  360. return bx::kExitSuccess;
  361. }
  362. else if (0 == bx::strCmp(_argv[2], "prev") )
  363. {
  364. AppI* prev = getCurrentApp();
  365. for (AppI* app = getNextWrap(prev); app != getCurrentApp(); app = getNextWrap(app) )
  366. {
  367. prev = app;
  368. }
  369. bx::strCopy(s_restartArgs, BX_COUNTOF(s_restartArgs), prev->getName() );
  370. return bx::kExitSuccess;
  371. }
  372. for (AppI* app = getFirstApp(); NULL != app; app = app->getNext() )
  373. {
  374. if (0 == bx::strCmp(_argv[2], app->getName() ) )
  375. {
  376. bx::strCopy(s_restartArgs, BX_COUNTOF(s_restartArgs), app->getName() );
  377. return bx::kExitSuccess;
  378. }
  379. }
  380. }
  381. return bx::kExitFailure;
  382. }
  383. AppI::AppI(const char* _name, const char* _description)
  384. {
  385. m_name = _name;
  386. m_description = _description;
  387. m_next = s_apps;
  388. s_apps = this;
  389. s_numApps++;
  390. }
  391. AppI::~AppI()
  392. {
  393. for (AppI* prev = NULL, *app = s_apps, *next = app->getNext()
  394. ; NULL != app
  395. ; prev = app, app = next, next = app->getNext() )
  396. {
  397. if (app == this)
  398. {
  399. if (NULL != prev)
  400. {
  401. prev->m_next = next;
  402. }
  403. else
  404. {
  405. s_apps = next;
  406. }
  407. --s_numApps;
  408. break;
  409. }
  410. }
  411. }
  412. const char* AppI::getName() const
  413. {
  414. return m_name;
  415. }
  416. const char* AppI::getDescription() const
  417. {
  418. return m_description;
  419. }
  420. AppI* AppI::getNext()
  421. {
  422. return m_next;
  423. }
  424. AppI* getFirstApp()
  425. {
  426. return s_apps;
  427. }
  428. uint32_t getNumApps()
  429. {
  430. return s_numApps;
  431. }
  432. int runApp(AppI* _app, int _argc, const char* const* _argv)
  433. {
  434. _app->init(_argc, _argv, ENTRY_DEFAULT_WIDTH, ENTRY_DEFAULT_HEIGHT);
  435. bgfx::frame();
  436. WindowHandle defaultWindow = { 0 };
  437. setWindowSize(defaultWindow, ENTRY_DEFAULT_WIDTH, ENTRY_DEFAULT_HEIGHT);
  438. #if BX_PLATFORM_EMSCRIPTEN
  439. s_app = _app;
  440. emscripten_set_main_loop(&updateApp, -1, 1);
  441. #else
  442. while (_app->update() )
  443. {
  444. if (0 != bx::strLen(s_restartArgs) )
  445. {
  446. break;
  447. }
  448. }
  449. #endif // BX_PLATFORM_EMSCRIPTEN
  450. return _app->shutdown();
  451. }
  452. static int32_t sortApp(const void* _lhs, const void* _rhs)
  453. {
  454. const AppI* lhs = *(const AppI**)_lhs;
  455. const AppI* rhs = *(const AppI**)_rhs;
  456. return bx::strCmpI(lhs->getName(), rhs->getName() );
  457. }
  458. static void sortApps()
  459. {
  460. if (2 > s_numApps)
  461. {
  462. return;
  463. }
  464. AppI** apps = (AppI**)BX_ALLOC(g_allocator, s_numApps*sizeof(AppI*) );
  465. uint32_t ii = 0;
  466. for (AppI* app = getFirstApp(); NULL != app; app = app->getNext() )
  467. {
  468. apps[ii++] = app;
  469. }
  470. bx::quickSort(apps, s_numApps, sizeof(AppI*), sortApp);
  471. s_apps = apps[0];
  472. for (ii = 1; ii < s_numApps; ++ii)
  473. {
  474. AppI* app = apps[ii-1];
  475. app->m_next = apps[ii];
  476. }
  477. apps[s_numApps-1]->m_next = NULL;
  478. BX_FREE(g_allocator, apps);
  479. }
  480. int main(int _argc, const char* const* _argv)
  481. {
  482. //DBG(BX_COMPILER_NAME " / " BX_CPU_NAME " / " BX_ARCH_NAME " / " BX_PLATFORM_NAME);
  483. s_fileReader = BX_NEW(g_allocator, FileReader);
  484. s_fileWriter = BX_NEW(g_allocator, FileWriter);
  485. cmdInit();
  486. cmdAdd("mouselock", cmdMouseLock);
  487. cmdAdd("graphics", cmdGraphics );
  488. cmdAdd("exit", cmdExit );
  489. cmdAdd("app", cmdApp );
  490. inputInit();
  491. inputAddBindings("bindings", s_bindings);
  492. entry::WindowHandle defaultWindow = { 0 };
  493. bx::FilePath fp(_argv[0]);
  494. char title[bx::kMaxFilePath];
  495. bx::strCopy(title, BX_COUNTOF(title), fp.getBaseName() );
  496. entry::setWindowTitle(defaultWindow, title);
  497. setWindowSize(defaultWindow, ENTRY_DEFAULT_WIDTH, ENTRY_DEFAULT_HEIGHT);
  498. sortApps();
  499. const char* find = "";
  500. if (1 < _argc)
  501. {
  502. find = _argv[_argc-1];
  503. }
  504. restart:
  505. AppI* selected = NULL;
  506. for (AppI* app = getFirstApp(); NULL != app; app = app->getNext() )
  507. {
  508. if (NULL == selected
  509. && bx::strFindI(app->getName(), find) )
  510. {
  511. selected = app;
  512. }
  513. #if 0
  514. DBG("%c %s, %s"
  515. , app == selected ? '>' : ' '
  516. , app->getName()
  517. , app->getDescription()
  518. );
  519. #endif // 0
  520. }
  521. int32_t result = bx::kExitSuccess;
  522. s_restartArgs[0] = '\0';
  523. if (0 == s_numApps)
  524. {
  525. result = ::_main_(_argc, (char**)_argv);
  526. }
  527. else
  528. {
  529. result = runApp(getCurrentApp(selected), _argc, _argv);
  530. }
  531. if (0 != bx::strLen(s_restartArgs) )
  532. {
  533. find = s_restartArgs;
  534. goto restart;
  535. }
  536. setCurrentDir("");
  537. inputRemoveBindings("bindings");
  538. inputShutdown();
  539. cmdShutdown();
  540. BX_DELETE(g_allocator, s_fileReader);
  541. s_fileReader = NULL;
  542. BX_DELETE(g_allocator, s_fileWriter);
  543. s_fileWriter = NULL;
  544. return result;
  545. }
  546. bool processEvents(uint32_t& _width, uint32_t& _height, uint32_t& _debug, uint32_t& _reset, MouseState* _mouse)
  547. {
  548. s_debug = _debug;
  549. s_reset = _reset;
  550. WindowHandle handle = { UINT16_MAX };
  551. bool mouseLock = inputIsMouseLocked();
  552. const Event* ev;
  553. do
  554. {
  555. struct SE { const Event* m_ev; SE() : m_ev(poll() ) {} ~SE() { if (NULL != m_ev) { release(m_ev); } } } scopeEvent;
  556. ev = scopeEvent.m_ev;
  557. if (NULL != ev)
  558. {
  559. switch (ev->m_type)
  560. {
  561. case Event::Axis:
  562. {
  563. const AxisEvent* axis = static_cast<const AxisEvent*>(ev);
  564. inputSetGamepadAxis(axis->m_gamepad, axis->m_axis, axis->m_value);
  565. }
  566. break;
  567. case Event::Char:
  568. {
  569. const CharEvent* chev = static_cast<const CharEvent*>(ev);
  570. inputChar(chev->m_len, chev->m_char);
  571. }
  572. break;
  573. case Event::Exit:
  574. return true;
  575. case Event::Gamepad:
  576. {
  577. // const GamepadEvent* gev = static_cast<const GamepadEvent*>(ev);
  578. // DBG("gamepad %d, %d", gev->m_gamepad.idx, gev->m_connected);
  579. }
  580. break;
  581. case Event::Mouse:
  582. {
  583. const MouseEvent* mouse = static_cast<const MouseEvent*>(ev);
  584. handle = mouse->m_handle;
  585. inputSetMousePos(mouse->m_mx, mouse->m_my, mouse->m_mz);
  586. if (!mouse->m_move)
  587. {
  588. inputSetMouseButtonState(mouse->m_button, mouse->m_down);
  589. }
  590. if (NULL != _mouse
  591. && !mouseLock)
  592. {
  593. _mouse->m_mx = mouse->m_mx;
  594. _mouse->m_my = mouse->m_my;
  595. _mouse->m_mz = mouse->m_mz;
  596. if (!mouse->m_move)
  597. {
  598. _mouse->m_buttons[mouse->m_button] = mouse->m_down;
  599. }
  600. }
  601. }
  602. break;
  603. case Event::Key:
  604. {
  605. const KeyEvent* key = static_cast<const KeyEvent*>(ev);
  606. handle = key->m_handle;
  607. inputSetKeyState(key->m_key, key->m_modifiers, key->m_down);
  608. }
  609. break;
  610. case Event::Size:
  611. {
  612. const SizeEvent* size = static_cast<const SizeEvent*>(ev);
  613. handle = size->m_handle;
  614. _width = size->m_width;
  615. _height = size->m_height;
  616. _reset = !s_reset; // force reset
  617. }
  618. break;
  619. case Event::Window:
  620. break;
  621. case Event::Suspend:
  622. break;
  623. default:
  624. break;
  625. }
  626. }
  627. inputProcess();
  628. } while (NULL != ev);
  629. if (handle.idx == 0
  630. && _reset != s_reset)
  631. {
  632. _reset = s_reset;
  633. bgfx::reset(_width, _height, _reset);
  634. inputSetMouseResolution(uint16_t(_width), uint16_t(_height) );
  635. }
  636. _debug = s_debug;
  637. return s_exit;
  638. }
  639. WindowState s_window[ENTRY_CONFIG_MAX_WINDOWS];
  640. bool processWindowEvents(WindowState& _state, uint32_t& _debug, uint32_t& _reset)
  641. {
  642. s_debug = _debug;
  643. s_reset = _reset;
  644. WindowHandle handle = { UINT16_MAX };
  645. bool mouseLock = inputIsMouseLocked();
  646. const Event* ev;
  647. do
  648. {
  649. struct SE
  650. {
  651. SE(WindowHandle _handle)
  652. : m_ev(poll(_handle) )
  653. {
  654. }
  655. ~SE()
  656. {
  657. if (NULL != m_ev)
  658. {
  659. release(m_ev);
  660. }
  661. }
  662. const Event* m_ev;
  663. } scopeEvent(handle);
  664. ev = scopeEvent.m_ev;
  665. if (NULL != ev)
  666. {
  667. handle = ev->m_handle;
  668. WindowState& win = s_window[handle.idx];
  669. switch (ev->m_type)
  670. {
  671. case Event::Axis:
  672. {
  673. const AxisEvent* axis = static_cast<const AxisEvent*>(ev);
  674. inputSetGamepadAxis(axis->m_gamepad, axis->m_axis, axis->m_value);
  675. }
  676. break;
  677. case Event::Char:
  678. {
  679. const CharEvent* chev = static_cast<const CharEvent*>(ev);
  680. win.m_handle = chev->m_handle;
  681. inputChar(chev->m_len, chev->m_char);
  682. }
  683. break;
  684. case Event::Exit:
  685. return true;
  686. case Event::Gamepad:
  687. {
  688. const GamepadEvent* gev = static_cast<const GamepadEvent*>(ev);
  689. DBG("gamepad %d, %d", gev->m_gamepad.idx, gev->m_connected);
  690. }
  691. break;
  692. case Event::Mouse:
  693. {
  694. const MouseEvent* mouse = static_cast<const MouseEvent*>(ev);
  695. win.m_handle = mouse->m_handle;
  696. if (mouse->m_move)
  697. {
  698. inputSetMousePos(mouse->m_mx, mouse->m_my, mouse->m_mz);
  699. }
  700. else
  701. {
  702. inputSetMouseButtonState(mouse->m_button, mouse->m_down);
  703. }
  704. if (!mouseLock)
  705. {
  706. if (mouse->m_move)
  707. {
  708. win.m_mouse.m_mx = mouse->m_mx;
  709. win.m_mouse.m_my = mouse->m_my;
  710. win.m_mouse.m_mz = mouse->m_mz;
  711. }
  712. else
  713. {
  714. win.m_mouse.m_buttons[mouse->m_button] = mouse->m_down;
  715. }
  716. }
  717. }
  718. break;
  719. case Event::Key:
  720. {
  721. const KeyEvent* key = static_cast<const KeyEvent*>(ev);
  722. win.m_handle = key->m_handle;
  723. inputSetKeyState(key->m_key, key->m_modifiers, key->m_down);
  724. }
  725. break;
  726. case Event::Size:
  727. {
  728. const SizeEvent* size = static_cast<const SizeEvent*>(ev);
  729. win.m_handle = size->m_handle;
  730. win.m_width = size->m_width;
  731. win.m_height = size->m_height;
  732. _reset = win.m_handle.idx == 0
  733. ? !s_reset
  734. : _reset
  735. ; // force reset
  736. }
  737. break;
  738. case Event::Window:
  739. {
  740. const WindowEvent* window = static_cast<const WindowEvent*>(ev);
  741. win.m_handle = window->m_handle;
  742. win.m_nwh = window->m_nwh;
  743. ev = NULL;
  744. }
  745. break;
  746. case Event::Suspend:
  747. break;
  748. default:
  749. break;
  750. }
  751. }
  752. inputProcess();
  753. } while (NULL != ev);
  754. if (isValid(handle) )
  755. {
  756. const WindowState& win = s_window[handle.idx];
  757. _state = win;
  758. if (handle.idx == 0)
  759. {
  760. inputSetMouseResolution(uint16_t(win.m_width), uint16_t(win.m_height) );
  761. }
  762. }
  763. if (_reset != s_reset)
  764. {
  765. _reset = s_reset;
  766. bgfx::reset(s_window[0].m_width, s_window[0].m_height, _reset);
  767. inputSetMouseResolution(uint16_t(s_window[0].m_width), uint16_t(s_window[0].m_height) );
  768. }
  769. _debug = s_debug;
  770. return s_exit;
  771. }
  772. bx::FileReaderI* getFileReader()
  773. {
  774. return s_fileReader;
  775. }
  776. bx::FileWriterI* getFileWriter()
  777. {
  778. return s_fileWriter;
  779. }
  780. bx::AllocatorI* getAllocator()
  781. {
  782. return g_allocator;
  783. }
  784. void* TinyStlAllocator::static_allocate(size_t _bytes)
  785. {
  786. return BX_ALLOC(getAllocator(), _bytes);
  787. }
  788. void TinyStlAllocator::static_deallocate(void* _ptr, size_t /*_bytes*/)
  789. {
  790. if (NULL != _ptr)
  791. {
  792. BX_FREE(getAllocator(), _ptr);
  793. }
  794. }
  795. } // namespace entry
  796. extern "C" bool entry_process_events(uint32_t* _width, uint32_t* _height, uint32_t* _debug, uint32_t* _reset)
  797. {
  798. return entry::processEvents(*_width, *_height, *_debug, *_reset, NULL);
  799. }