entry.cpp 22 KB

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