entry.cpp 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  1. /*
  2. * Copyright 2011-2025 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  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. using FixedString4096 = bx::FixedStringT<4096>;
  29. static FixedString4096 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. FixedString4096 filePath(s_currentDir);
  37. filePath.append(_filePath);
  38. return super::open(filePath.getCPtr(), _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. bx::FilePath filePath(s_currentDir);
  48. filePath.join(_filePath);
  49. return super::open(filePath.getCPtr(), _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. static_assert(Key::Count == BX_COUNTOF(s_keyName) );
  172. const char* getName(Key::Enum _key)
  173. {
  174. BX_ASSERT(_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::F6, entry::Modifier::None, 1, NULL, "graphics profiler" },
  317. { entry::Key::F7, entry::Modifier::None, 1, NULL, "graphics vsync" },
  318. { entry::Key::F8, entry::Modifier::None, 1, NULL, "graphics msaa" },
  319. { entry::Key::F9, entry::Modifier::None, 1, NULL, "graphics flush" },
  320. { entry::Key::F10, entry::Modifier::None, 1, NULL, "graphics hidpi" },
  321. { entry::Key::Print, entry::Modifier::None, 1, NULL, "graphics screenshot" },
  322. { entry::Key::KeyP, entry::Modifier::LeftCtrl, 1, NULL, "graphics screenshot" },
  323. INPUT_BINDING_END
  324. };
  325. #if BX_PLATFORM_EMSCRIPTEN
  326. static AppI* s_app;
  327. static void updateApp()
  328. {
  329. s_app->update();
  330. }
  331. #endif // BX_PLATFORM_EMSCRIPTEN
  332. static AppI* s_currentApp = NULL;
  333. static AppI* s_apps = NULL;
  334. static uint32_t s_numApps = 0;
  335. static char s_restartArgs[1024] = { '\0' };
  336. static AppI* getCurrentApp(AppI* _set = NULL)
  337. {
  338. if (NULL != _set)
  339. {
  340. s_currentApp = _set;
  341. }
  342. else if (NULL == s_currentApp)
  343. {
  344. s_currentApp = getFirstApp();
  345. }
  346. return s_currentApp;
  347. }
  348. static AppI* getNextWrap(AppI* _app)
  349. {
  350. AppI* next = _app->getNext();
  351. if (NULL != next)
  352. {
  353. return next;
  354. }
  355. return getFirstApp();
  356. }
  357. int cmdApp(CmdContext* /*_context*/, void* /*_userData*/, int _argc, char const* const* _argv)
  358. {
  359. if (0 == bx::strCmp(_argv[1], "restart") )
  360. {
  361. if (2 == _argc)
  362. {
  363. bx::strCopy(s_restartArgs, BX_COUNTOF(s_restartArgs), getCurrentApp()->getName() );
  364. return bx::kExitSuccess;
  365. }
  366. if (0 == bx::strCmp(_argv[2], "next") )
  367. {
  368. AppI* next = getNextWrap(getCurrentApp() );
  369. bx::strCopy(s_restartArgs, BX_COUNTOF(s_restartArgs), next->getName() );
  370. return bx::kExitSuccess;
  371. }
  372. else if (0 == bx::strCmp(_argv[2], "prev") )
  373. {
  374. AppI* prev = getCurrentApp();
  375. for (AppI* app = getNextWrap(prev); app != getCurrentApp(); app = getNextWrap(app) )
  376. {
  377. prev = app;
  378. }
  379. bx::strCopy(s_restartArgs, BX_COUNTOF(s_restartArgs), prev->getName() );
  380. return bx::kExitSuccess;
  381. }
  382. for (AppI* app = getFirstApp(); NULL != app; app = app->getNext() )
  383. {
  384. if (0 == bx::strCmp(_argv[2], app->getName() ) )
  385. {
  386. bx::strCopy(s_restartArgs, BX_COUNTOF(s_restartArgs), app->getName() );
  387. return bx::kExitSuccess;
  388. }
  389. }
  390. }
  391. return bx::kExitFailure;
  392. }
  393. struct AppInternal
  394. {
  395. AppI* m_next;
  396. const char* m_name;
  397. const char* m_description;
  398. const char* m_url;
  399. };
  400. static ptrdiff_t s_offset = 0;
  401. AppI::AppI(const char* _name, const char* _description, const char* _url)
  402. {
  403. static_assert(sizeof(AppInternal) <= sizeof(m_internal) );
  404. s_offset = BX_OFFSETOF(AppI, m_internal);
  405. AppInternal* ai = (AppInternal*)m_internal;
  406. ai->m_name = _name;
  407. ai->m_description = _description;
  408. ai->m_url = _url;
  409. ai->m_next = s_apps;
  410. s_apps = this;
  411. s_numApps++;
  412. }
  413. AppI::~AppI()
  414. {
  415. for (AppI* prev = NULL, *app = s_apps, *next = app->getNext()
  416. ; NULL != app
  417. ; prev = app, app = next, next = app->getNext() )
  418. {
  419. if (app == this)
  420. {
  421. if (NULL != prev)
  422. {
  423. AppInternal* ai = bx::addressOf<AppInternal>(prev, s_offset);
  424. ai->m_next = next;
  425. }
  426. else
  427. {
  428. s_apps = next;
  429. }
  430. --s_numApps;
  431. break;
  432. }
  433. }
  434. }
  435. const char* AppI::getName() const
  436. {
  437. AppInternal* ai = (AppInternal*)m_internal;
  438. return ai->m_name;
  439. }
  440. const char* AppI::getDescription() const
  441. {
  442. AppInternal* ai = (AppInternal*)m_internal;
  443. return ai->m_description;
  444. }
  445. const char* AppI::getUrl() const
  446. {
  447. AppInternal* ai = (AppInternal*)m_internal;
  448. return ai->m_url;
  449. }
  450. AppI* AppI::getNext()
  451. {
  452. AppInternal* ai = (AppInternal*)m_internal;
  453. return ai->m_next;
  454. }
  455. AppI* getFirstApp()
  456. {
  457. return s_apps;
  458. }
  459. uint32_t getNumApps()
  460. {
  461. return s_numApps;
  462. }
  463. int runApp(AppI* _app, int _argc, const char* const* _argv)
  464. {
  465. setWindowSize(kDefaultWindowHandle, s_width, s_height);
  466. _app->init(_argc, _argv, s_width, s_height);
  467. bgfx::frame();
  468. #if BX_PLATFORM_EMSCRIPTEN
  469. s_app = _app;
  470. emscripten_set_main_loop(&updateApp, -1, 1);
  471. #else
  472. while (_app->update() )
  473. {
  474. if (0 != bx::strLen(s_restartArgs) )
  475. {
  476. break;
  477. }
  478. }
  479. #endif // BX_PLATFORM_EMSCRIPTEN
  480. return _app->shutdown();
  481. }
  482. static int32_t sortApp(const void* _lhs, const void* _rhs)
  483. {
  484. const AppI* lhs = *(const AppI**)_lhs;
  485. const AppI* rhs = *(const AppI**)_rhs;
  486. return bx::strCmpI(lhs->getName(), rhs->getName() );
  487. }
  488. static void sortApps()
  489. {
  490. if (2 > s_numApps)
  491. {
  492. return;
  493. }
  494. AppI** apps = (AppI**)bx::alloc(g_allocator, s_numApps*sizeof(AppI*) );
  495. uint32_t ii = 0;
  496. for (AppI* app = getFirstApp(); NULL != app; app = app->getNext() )
  497. {
  498. apps[ii++] = app;
  499. }
  500. bx::quickSort(apps, s_numApps, sizeof(AppI*), sortApp);
  501. s_apps = apps[0];
  502. for (ii = 1; ii < s_numApps; ++ii)
  503. {
  504. AppI* app = apps[ii-1];
  505. AppInternal* ai = bx::addressOf<AppInternal>(app, s_offset);
  506. ai->m_next = apps[ii];
  507. }
  508. {
  509. AppInternal* ai = bx::addressOf<AppInternal>(apps[s_numApps-1], s_offset);
  510. ai->m_next = NULL;
  511. }
  512. bx::free(g_allocator, apps);
  513. }
  514. int main(int _argc, const char* const* _argv)
  515. {
  516. //DBG(BX_COMPILER_NAME " / " BX_CPU_NAME " / " BX_ARCH_NAME " / " BX_PLATFORM_NAME);
  517. bx::installExceptionHandler();
  518. s_fileReader = BX_NEW(g_allocator, FileReader);
  519. s_fileWriter = BX_NEW(g_allocator, FileWriter);
  520. cmdInit();
  521. cmdAdd("mouselock", cmdMouseLock);
  522. cmdAdd("graphics", cmdGraphics );
  523. cmdAdd("exit", cmdExit );
  524. cmdAdd("app", cmdApp );
  525. inputInit();
  526. inputAddBindings("bindings", s_bindings);
  527. bx::FilePath fp(_argv[0]);
  528. char title[bx::kMaxFilePath];
  529. bx::strCopy(title, BX_COUNTOF(title), fp.getBaseName() );
  530. entry::setWindowTitle(kDefaultWindowHandle, title);
  531. setWindowSize(kDefaultWindowHandle, ENTRY_DEFAULT_WIDTH, ENTRY_DEFAULT_HEIGHT);
  532. sortApps();
  533. const char* find = "";
  534. if (1 < _argc)
  535. {
  536. find = _argv[_argc-1];
  537. }
  538. restart:
  539. AppI* selected = NULL;
  540. for (AppI* app = getFirstApp(); NULL != app; app = app->getNext() )
  541. {
  542. if (NULL == selected
  543. && !bx::strFindI(app->getName(), find).isEmpty() )
  544. {
  545. selected = app;
  546. }
  547. #if 0
  548. DBG("%c %s, %s"
  549. , app == selected ? '>' : ' '
  550. , app->getName()
  551. , app->getDescription()
  552. );
  553. #endif // 0
  554. }
  555. int32_t result = bx::kExitSuccess;
  556. s_restartArgs[0] = '\0';
  557. if (0 == s_numApps)
  558. {
  559. result = ::_main_(_argc, (char**)_argv);
  560. }
  561. else
  562. {
  563. result = runApp(getCurrentApp(selected), _argc, _argv);
  564. }
  565. if (0 != bx::strLen(s_restartArgs) )
  566. {
  567. find = s_restartArgs;
  568. goto restart;
  569. }
  570. setCurrentDir("");
  571. inputRemoveBindings("bindings");
  572. inputShutdown();
  573. cmdShutdown();
  574. bx::deleteObject(g_allocator, s_fileReader);
  575. s_fileReader = NULL;
  576. bx::deleteObject(g_allocator, s_fileWriter);
  577. s_fileWriter = NULL;
  578. return result;
  579. }
  580. WindowState s_window[ENTRY_CONFIG_MAX_WINDOWS];
  581. bool processEvents(uint32_t& _width, uint32_t& _height, uint32_t& _debug, uint32_t& _reset, MouseState* _mouse)
  582. {
  583. bool needReset = s_reset != _reset;
  584. s_debug = _debug;
  585. s_reset = _reset;
  586. WindowHandle handle = { UINT16_MAX };
  587. bool mouseLock = inputIsMouseLocked();
  588. const Event* ev;
  589. do
  590. {
  591. struct SE { const Event* m_ev; SE() : m_ev(poll() ) {} ~SE() { if (NULL != m_ev) { release(m_ev); } } } scopeEvent;
  592. ev = scopeEvent.m_ev;
  593. if (NULL != ev)
  594. {
  595. switch (ev->m_type)
  596. {
  597. case Event::Axis:
  598. {
  599. const AxisEvent* axis = static_cast<const AxisEvent*>(ev);
  600. inputSetGamepadAxis(axis->m_gamepad, axis->m_axis, axis->m_value);
  601. }
  602. break;
  603. case Event::Char:
  604. {
  605. const CharEvent* chev = static_cast<const CharEvent*>(ev);
  606. inputChar(chev->m_len, chev->m_char);
  607. }
  608. break;
  609. case Event::Exit:
  610. return true;
  611. case Event::Gamepad:
  612. {
  613. // const GamepadEvent* gev = static_cast<const GamepadEvent*>(ev);
  614. // DBG("gamepad %d, %d", gev->m_gamepad.idx, gev->m_connected);
  615. }
  616. break;
  617. case Event::Mouse:
  618. {
  619. const MouseEvent* mouse = static_cast<const MouseEvent*>(ev);
  620. handle = mouse->m_handle;
  621. inputSetMousePos(mouse->m_mx, mouse->m_my, mouse->m_mz);
  622. if (!mouse->m_move)
  623. {
  624. inputSetMouseButtonState(mouse->m_button, mouse->m_down);
  625. }
  626. if (NULL != _mouse
  627. && !mouseLock)
  628. {
  629. _mouse->m_mx = mouse->m_mx;
  630. _mouse->m_my = mouse->m_my;
  631. _mouse->m_mz = mouse->m_mz;
  632. if (!mouse->m_move)
  633. {
  634. _mouse->m_buttons[mouse->m_button] = mouse->m_down;
  635. }
  636. }
  637. }
  638. break;
  639. case Event::Key:
  640. {
  641. const KeyEvent* key = static_cast<const KeyEvent*>(ev);
  642. handle = key->m_handle;
  643. inputSetKeyState(key->m_key, key->m_modifiers, key->m_down);
  644. }
  645. break;
  646. case Event::Size:
  647. {
  648. const SizeEvent* size = static_cast<const SizeEvent*>(ev);
  649. WindowState& win = s_window[0];
  650. win.m_handle = size->m_handle;
  651. win.m_width = size->m_width;
  652. win.m_height = size->m_height;
  653. handle = size->m_handle;
  654. _width = size->m_width;
  655. _height = size->m_height;
  656. BX_TRACE("Window resize event: %d: %dx%d", handle, _width, _height);
  657. needReset = true;
  658. }
  659. break;
  660. case Event::Window:
  661. break;
  662. case Event::Suspend:
  663. break;
  664. case Event::DropFile:
  665. {
  666. const DropFileEvent* drop = static_cast<const DropFileEvent*>(ev);
  667. DBG("%s", drop->m_filePath.getCPtr() );
  668. }
  669. break;
  670. default:
  671. break;
  672. }
  673. }
  674. inputProcess();
  675. } while (NULL != ev);
  676. needReset |= _reset != s_reset;
  677. if (handle.idx == 0
  678. && needReset)
  679. {
  680. _reset = s_reset;
  681. BX_TRACE("bgfx::reset(%d, %d, 0x%x)", _width, _height, _reset);
  682. bgfx::reset(_width, _height, _reset);
  683. inputSetMouseResolution(uint16_t(_width), uint16_t(_height) );
  684. }
  685. _debug = s_debug;
  686. s_width = _width;
  687. s_height = _height;
  688. return s_exit;
  689. }
  690. bool processWindowEvents(WindowState& _state, uint32_t& _debug, uint32_t& _reset)
  691. {
  692. bool needReset = s_reset != _reset;
  693. s_debug = _debug;
  694. s_reset = _reset;
  695. WindowHandle handle = { UINT16_MAX };
  696. bool mouseLock = inputIsMouseLocked();
  697. bool clearDropFile = true;
  698. const Event* ev;
  699. do
  700. {
  701. struct SE
  702. {
  703. SE(WindowHandle _handle)
  704. : m_ev(poll(_handle) )
  705. {
  706. }
  707. ~SE()
  708. {
  709. if (NULL != m_ev)
  710. {
  711. release(m_ev);
  712. }
  713. }
  714. const Event* m_ev;
  715. } scopeEvent(handle);
  716. ev = scopeEvent.m_ev;
  717. if (NULL != ev)
  718. {
  719. handle = ev->m_handle;
  720. WindowState& win = s_window[isValid(handle) ? handle.idx : 0];
  721. switch (ev->m_type)
  722. {
  723. case Event::Axis:
  724. {
  725. const AxisEvent* axis = static_cast<const AxisEvent*>(ev);
  726. inputSetGamepadAxis(axis->m_gamepad, axis->m_axis, axis->m_value);
  727. }
  728. break;
  729. case Event::Char:
  730. {
  731. const CharEvent* chev = static_cast<const CharEvent*>(ev);
  732. win.m_handle = chev->m_handle;
  733. inputChar(chev->m_len, chev->m_char);
  734. }
  735. break;
  736. case Event::Exit:
  737. return true;
  738. case Event::Gamepad:
  739. {
  740. const GamepadEvent* gev = static_cast<const GamepadEvent*>(ev);
  741. DBG("gamepad %d, %d", gev->m_gamepad.idx, gev->m_connected);
  742. }
  743. break;
  744. case Event::Mouse:
  745. {
  746. const MouseEvent* mouse = static_cast<const MouseEvent*>(ev);
  747. win.m_handle = mouse->m_handle;
  748. if (mouse->m_move)
  749. {
  750. inputSetMousePos(mouse->m_mx, mouse->m_my, mouse->m_mz);
  751. }
  752. else
  753. {
  754. inputSetMouseButtonState(mouse->m_button, mouse->m_down);
  755. }
  756. if (!mouseLock)
  757. {
  758. if (mouse->m_move)
  759. {
  760. win.m_mouse.m_mx = mouse->m_mx;
  761. win.m_mouse.m_my = mouse->m_my;
  762. win.m_mouse.m_mz = mouse->m_mz;
  763. }
  764. else
  765. {
  766. win.m_mouse.m_buttons[mouse->m_button] = mouse->m_down;
  767. }
  768. }
  769. }
  770. break;
  771. case Event::Key:
  772. {
  773. const KeyEvent* key = static_cast<const KeyEvent*>(ev);
  774. win.m_handle = key->m_handle;
  775. inputSetKeyState(key->m_key, key->m_modifiers, key->m_down);
  776. }
  777. break;
  778. case Event::Size:
  779. {
  780. const SizeEvent* size = static_cast<const SizeEvent*>(ev);
  781. win.m_handle = size->m_handle;
  782. win.m_width = size->m_width;
  783. win.m_height = size->m_height;
  784. needReset = win.m_handle.idx == 0 ? true : needReset;
  785. }
  786. break;
  787. case Event::Window:
  788. {
  789. const WindowEvent* window = static_cast<const WindowEvent*>(ev);
  790. win.m_handle = window->m_handle;
  791. win.m_nwh = window->m_nwh;
  792. ev = NULL;
  793. }
  794. break;
  795. case Event::Suspend:
  796. break;
  797. case Event::DropFile:
  798. {
  799. const DropFileEvent* drop = static_cast<const DropFileEvent*>(ev);
  800. win.m_dropFile = drop->m_filePath;
  801. clearDropFile = false;
  802. }
  803. break;
  804. default:
  805. break;
  806. }
  807. }
  808. inputProcess();
  809. } while (NULL != ev);
  810. if (isValid(handle) )
  811. {
  812. WindowState& win = s_window[handle.idx];
  813. if (clearDropFile)
  814. {
  815. win.m_dropFile.clear();
  816. }
  817. _state = win;
  818. if (handle.idx == 0)
  819. {
  820. inputSetMouseResolution(uint16_t(win.m_width), uint16_t(win.m_height) );
  821. }
  822. }
  823. needReset |= _reset != s_reset;
  824. if (needReset)
  825. {
  826. _reset = s_reset;
  827. BX_TRACE("bgfx::reset(%d, %d, 0x%x)", s_window[0].m_width, s_window[0].m_height, _reset);
  828. bgfx::reset(s_window[0].m_width, s_window[0].m_height, _reset);
  829. inputSetMouseResolution(uint16_t(s_window[0].m_width), uint16_t(s_window[0].m_height) );
  830. }
  831. _debug = s_debug;
  832. return s_exit;
  833. }
  834. bx::FileReaderI* getFileReader()
  835. {
  836. return s_fileReader;
  837. }
  838. bx::FileWriterI* getFileWriter()
  839. {
  840. return s_fileWriter;
  841. }
  842. bx::AllocatorI* getAllocator()
  843. {
  844. if (NULL == g_allocator)
  845. {
  846. g_allocator = getDefaultAllocator();
  847. }
  848. return g_allocator;
  849. }
  850. void* TinyStlAllocator::static_allocate(size_t _bytes)
  851. {
  852. return bx::alloc(getAllocator(), _bytes);
  853. }
  854. void TinyStlAllocator::static_deallocate(void* _ptr, size_t /*_bytes*/)
  855. {
  856. if (NULL != _ptr)
  857. {
  858. bx::free(getAllocator(), _ptr);
  859. }
  860. }
  861. } // namespace entry
  862. extern "C" bool entry_process_events(uint32_t* _width, uint32_t* _height, uint32_t* _debug, uint32_t* _reset)
  863. {
  864. return entry::processEvents(*_width, *_height, *_debug, *_reset, NULL);
  865. }
  866. extern "C" void* entry_get_default_native_window_handle()
  867. {
  868. return entry::getNativeWindowHandle(entry::kDefaultWindowHandle);
  869. }
  870. extern "C" void* entry_get_native_display_handle()
  871. {
  872. return entry::getNativeDisplayHandle();
  873. }
  874. extern "C" bgfx::NativeWindowHandleType::Enum entry_get_native_window_handle_type()
  875. {
  876. return entry::getNativeWindowHandleType();
  877. }