entry.cpp 21 KB

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