2
0

entry.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. /*
  2. * Copyright 2011-2015 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include <bgfx/bgfx.h>
  6. #include <bx/string.h>
  7. #include <bx/readerwriter.h>
  8. #include <time.h>
  9. #if BX_PLATFORM_EMSCRIPTEN
  10. # include <emscripten.h>
  11. #endif // BX_PLATFORM_EMSCRIPTEN
  12. #include "entry_p.h"
  13. #include "cmd.h"
  14. #include "input.h"
  15. extern "C" int _main_(int _argc, char** _argv);
  16. namespace entry
  17. {
  18. static uint32_t s_debug = BGFX_DEBUG_NONE;
  19. static uint32_t s_reset = BGFX_RESET_NONE;
  20. static bool s_exit = false;
  21. static bx::FileReaderI* s_fileReader = NULL;
  22. static bx::FileWriterI* s_fileWriter = NULL;
  23. extern bx::ReallocatorI* getDefaultAllocator();
  24. static bx::ReallocatorI* s_allocator = getDefaultAllocator();
  25. #if ENTRY_CONFIG_IMPLEMENT_DEFAULT_ALLOCATOR
  26. bx::ReallocatorI* getDefaultAllocator()
  27. {
  28. BX_PRAGMA_DIAGNOSTIC_PUSH();
  29. BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4459); // warning C4459: declaration of 's_allocator' hides global declaration
  30. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wshadow");
  31. static bx::CrtAllocator s_allocator;
  32. return &s_allocator;
  33. BX_PRAGMA_DIAGNOSTIC_POP();
  34. }
  35. #endif // ENTRY_CONFIG_IMPLEMENT_DEFAULT_ALLOCATOR
  36. static const char* s_keyName[] =
  37. {
  38. "None",
  39. "Esc",
  40. "Return",
  41. "Tab",
  42. "Space",
  43. "Backspace",
  44. "Up",
  45. "Down",
  46. "Left",
  47. "Right",
  48. "Insert",
  49. "Delete",
  50. "Home",
  51. "End",
  52. "PageUp",
  53. "PageDown",
  54. "Print",
  55. "Plus",
  56. "Minus",
  57. "LeftBracket",
  58. "RightBracket",
  59. "Semicolon",
  60. "Quote",
  61. "Comma",
  62. "Period",
  63. "Slash",
  64. "Backslash",
  65. "Tilde",
  66. "F1",
  67. "F2",
  68. "F3",
  69. "F4",
  70. "F5",
  71. "F6",
  72. "F7",
  73. "F8",
  74. "F9",
  75. "F10",
  76. "F11",
  77. "F12",
  78. "NumPad0",
  79. "NumPad1",
  80. "NumPad2",
  81. "NumPad3",
  82. "NumPad4",
  83. "NumPad5",
  84. "NumPad6",
  85. "NumPad7",
  86. "NumPad8",
  87. "NumPad9",
  88. "Key0",
  89. "Key1",
  90. "Key2",
  91. "Key3",
  92. "Key4",
  93. "Key5",
  94. "Key6",
  95. "Key7",
  96. "Key8",
  97. "Key9",
  98. "KeyA",
  99. "KeyB",
  100. "KeyC",
  101. "KeyD",
  102. "KeyE",
  103. "KeyF",
  104. "KeyG",
  105. "KeyH",
  106. "KeyI",
  107. "KeyJ",
  108. "KeyK",
  109. "KeyL",
  110. "KeyM",
  111. "KeyN",
  112. "KeyO",
  113. "KeyP",
  114. "KeyQ",
  115. "KeyR",
  116. "KeyS",
  117. "KeyT",
  118. "KeyU",
  119. "KeyV",
  120. "KeyW",
  121. "KeyX",
  122. "KeyY",
  123. "KeyZ",
  124. "GamepadA",
  125. "GamepadB",
  126. "GamepadX",
  127. "GamepadY",
  128. "GamepadThumbL",
  129. "GamepadThumbR",
  130. "GamepadShoulderL",
  131. "GamepadShoulderR",
  132. "GamepadUp",
  133. "GamepadDown",
  134. "GamepadLeft",
  135. "GamepadRight",
  136. "GamepadBack",
  137. "GamepadStart",
  138. "GamepadGuide",
  139. };
  140. BX_STATIC_ASSERT(Key::Count == BX_COUNTOF(s_keyName) );
  141. const char* getName(Key::Enum _key)
  142. {
  143. BX_CHECK(_key < Key::Count, "Invalid key %d.", _key);
  144. return s_keyName[_key];
  145. }
  146. char keyToAscii(Key::Enum _key, uint8_t _modifiers)
  147. {
  148. const bool isAscii = (Key::Key0 <= _key && _key <= Key::KeyZ)
  149. || (Key::Esc <= _key && _key <= Key::Minus);
  150. if (!isAscii)
  151. {
  152. return '\0';
  153. }
  154. const bool isNumber = (Key::Key0 <= _key && _key <= Key::Key9);
  155. if (isNumber)
  156. {
  157. return '0' + (_key - Key::Key0);
  158. }
  159. const bool isChar = (Key::KeyA <= _key && _key <= Key::KeyZ);
  160. if (isChar)
  161. {
  162. enum { ShiftMask = Modifier::LeftShift|Modifier::RightShift };
  163. const bool shift = !!(_modifiers&ShiftMask);
  164. return (shift ? 'A' : 'a') + (_key - Key::KeyA);
  165. }
  166. switch (_key)
  167. {
  168. case Key::Esc: return 0x1b;
  169. case Key::Return: return '\n';
  170. case Key::Tab: return '\t';
  171. case Key::Space: return ' ';
  172. case Key::Backspace: return 0x08;
  173. case Key::Plus: return '+';
  174. case Key::Minus: return '-';
  175. default: break;
  176. }
  177. return '\0';
  178. }
  179. bool setOrToggle(uint32_t& _flags, const char* _name, uint32_t _bit, int _first, int _argc, char const* const* _argv)
  180. {
  181. if (0 == strcmp(_argv[_first], _name) )
  182. {
  183. int arg = _first+1;
  184. if (_argc > arg)
  185. {
  186. _flags &= ~_bit;
  187. _flags |= bx::toBool(_argv[arg]) ? _bit : 0;
  188. }
  189. else
  190. {
  191. _flags ^= _bit;
  192. }
  193. return true;
  194. }
  195. return false;
  196. }
  197. int cmdMouseLock(CmdContext* /*_context*/, void* /*_userData*/, int _argc, char const* const* _argv)
  198. {
  199. if (_argc > 1)
  200. {
  201. inputSetMouseLock(_argc > 1 ? bx::toBool(_argv[1]) : !inputIsMouseLocked() );
  202. return 0;
  203. }
  204. return 1;
  205. }
  206. int cmdGraphics(CmdContext* /*_context*/, void* /*_userData*/, int _argc, char const* const* _argv)
  207. {
  208. if (_argc > 1)
  209. {
  210. if (setOrToggle(s_reset, "vsync", BGFX_RESET_VSYNC, 1, _argc, _argv)
  211. || setOrToggle(s_reset, "maxaniso", BGFX_RESET_MAXANISOTROPY, 1, _argc, _argv)
  212. || setOrToggle(s_reset, "hmd", BGFX_RESET_HMD, 1, _argc, _argv)
  213. || setOrToggle(s_reset, "hmddbg", BGFX_RESET_HMD_DEBUG, 1, _argc, _argv)
  214. || setOrToggle(s_reset, "hmdrecenter", BGFX_RESET_HMD_RECENTER, 1, _argc, _argv)
  215. || setOrToggle(s_reset, "msaa", BGFX_RESET_MSAA_X16, 1, _argc, _argv)
  216. || setOrToggle(s_reset, "flush", BGFX_RESET_FLUSH_AFTER_RENDER, 1, _argc, _argv)
  217. || setOrToggle(s_reset, "flip", BGFX_RESET_FLIP_AFTER_RENDER, 1, _argc, _argv)
  218. || setOrToggle(s_reset, "hidpi", BGFX_RESET_HIDPI, 1, _argc, _argv)
  219. )
  220. {
  221. return 0;
  222. }
  223. else if (setOrToggle(s_debug, "stats", BGFX_DEBUG_STATS, 1, _argc, _argv)
  224. || setOrToggle(s_debug, "ifh", BGFX_DEBUG_IFH, 1, _argc, _argv)
  225. || setOrToggle(s_debug, "text", BGFX_DEBUG_TEXT, 1, _argc, _argv)
  226. || setOrToggle(s_debug, "wireframe", BGFX_DEBUG_WIREFRAME, 1, _argc, _argv) )
  227. {
  228. bgfx::setDebug(s_debug);
  229. return 0;
  230. }
  231. else if (0 == strcmp(_argv[1], "screenshot") )
  232. {
  233. if (_argc > 2)
  234. {
  235. bgfx::saveScreenShot(_argv[2]);
  236. }
  237. else
  238. {
  239. time_t tt;
  240. time(&tt);
  241. char filePath[256];
  242. bx::snprintf(filePath, sizeof(filePath), "temp/screenshot-%d", tt);
  243. bgfx::saveScreenShot(filePath);
  244. }
  245. return 0;
  246. }
  247. else if (0 == strcmp(_argv[1], "fullscreen") )
  248. {
  249. WindowHandle window = { 0 };
  250. toggleFullscreen(window);
  251. return 0;
  252. }
  253. }
  254. return 1;
  255. }
  256. int cmdExit(CmdContext* /*_context*/, void* /*_userData*/, int /*_argc*/, char const* const* /*_argv*/)
  257. {
  258. s_exit = true;
  259. return 0;
  260. }
  261. static const InputBinding s_bindings[] =
  262. {
  263. { entry::Key::KeyQ, entry::Modifier::LeftCtrl, 1, NULL, "exit" },
  264. { entry::Key::KeyQ, entry::Modifier::RightCtrl, 1, NULL, "exit" },
  265. { entry::Key::KeyF, entry::Modifier::LeftCtrl, 1, NULL, "graphics fullscreen" },
  266. { entry::Key::KeyF, entry::Modifier::RightCtrl, 1, NULL, "graphics fullscreen" },
  267. { entry::Key::Return, entry::Modifier::RightAlt, 1, NULL, "graphics fullscreen" },
  268. { entry::Key::F1, entry::Modifier::None, 1, NULL, "graphics stats" },
  269. { entry::Key::GamepadStart, entry::Modifier::None, 1, NULL, "graphics stats" },
  270. { entry::Key::F1, entry::Modifier::LeftShift, 1, NULL, "graphics stats 0\ngraphics text 0" },
  271. { entry::Key::F3, entry::Modifier::None, 1, NULL, "graphics wireframe" },
  272. { entry::Key::F4, entry::Modifier::None, 1, NULL, "graphics hmd" },
  273. { entry::Key::F4, entry::Modifier::LeftShift, 1, NULL, "graphics hmdrecenter" },
  274. { entry::Key::F4, entry::Modifier::LeftCtrl, 1, NULL, "graphics hmddbg" },
  275. { entry::Key::F7, entry::Modifier::None, 1, NULL, "graphics vsync" },
  276. { entry::Key::F8, entry::Modifier::None, 1, NULL, "graphics msaa" },
  277. { entry::Key::F9, entry::Modifier::None, 1, NULL, "graphics flush" },
  278. { entry::Key::F10, entry::Modifier::None, 1, NULL, "graphics hidpi" },
  279. { entry::Key::Print, entry::Modifier::None, 1, NULL, "graphics screenshot" },
  280. INPUT_BINDING_END
  281. };
  282. #if BX_PLATFORM_EMSCRIPTEN
  283. static AppI* s_app;
  284. static void updateApp()
  285. {
  286. s_app->update();
  287. }
  288. #endif // BX_PLATFORM_EMSCRIPTEN
  289. int runApp(AppI* _app, int _argc, char** _argv)
  290. {
  291. _app->init(_argc, _argv);
  292. WindowHandle defaultWindow = { 0 };
  293. setWindowSize(defaultWindow, ENTRY_DEFAULT_WIDTH, ENTRY_DEFAULT_HEIGHT);
  294. #if BX_PLATFORM_EMSCRIPTEN
  295. s_app = _app;
  296. emscripten_set_main_loop(&updateApp, -1, 1);
  297. #else
  298. while (_app->update() );
  299. #endif // BX_PLATFORM_EMSCRIPTEN
  300. return _app->shutdown();
  301. }
  302. int main(int _argc, char** _argv)
  303. {
  304. //DBG(BX_COMPILER_NAME " / " BX_CPU_NAME " / " BX_ARCH_NAME " / " BX_PLATFORM_NAME);
  305. #if BX_CONFIG_CRT_FILE_READER_WRITER
  306. s_fileReader = new bx::CrtFileReader;
  307. s_fileWriter = new bx::CrtFileWriter;
  308. #endif // BX_CONFIG_CRT_FILE_READER_WRITER
  309. cmdInit();
  310. cmdAdd("mouselock", cmdMouseLock);
  311. cmdAdd("graphics", cmdGraphics );
  312. cmdAdd("exit", cmdExit );
  313. inputInit();
  314. inputAddBindings("bindings", s_bindings);
  315. entry::WindowHandle defaultWindow = { 0 };
  316. entry::setWindowTitle(defaultWindow, bx::baseName(_argv[0]) );
  317. setWindowSize(defaultWindow, ENTRY_DEFAULT_WIDTH, ENTRY_DEFAULT_HEIGHT);
  318. int32_t result = ::_main_(_argc, _argv);
  319. inputRemoveBindings("bindings");
  320. inputShutdown();
  321. cmdShutdown();
  322. #if BX_CONFIG_CRT_FILE_READER_WRITER
  323. delete s_fileReader;
  324. s_fileReader = NULL;
  325. delete s_fileWriter;
  326. s_fileWriter = NULL;
  327. #endif // BX_CONFIG_CRT_FILE_READER_WRITER
  328. return result;
  329. }
  330. bool processEvents(uint32_t& _width, uint32_t& _height, uint32_t& _debug, uint32_t& _reset, MouseState* _mouse)
  331. {
  332. s_debug = _debug;
  333. s_reset = _reset;
  334. WindowHandle handle = { UINT16_MAX };
  335. bool mouseLock = inputIsMouseLocked();
  336. const Event* ev;
  337. do
  338. {
  339. struct SE { const Event* m_ev; SE() : m_ev(poll() ) {} ~SE() { if (NULL != m_ev) { release(m_ev); } } } scopeEvent;
  340. ev = scopeEvent.m_ev;
  341. if (NULL != ev)
  342. {
  343. switch (ev->m_type)
  344. {
  345. case Event::Axis:
  346. {
  347. const AxisEvent* axis = static_cast<const AxisEvent*>(ev);
  348. inputSetGamepadAxis(axis->m_gamepad, axis->m_axis, axis->m_value);
  349. }
  350. break;
  351. case Event::Char:
  352. {
  353. const CharEvent* chev = static_cast<const CharEvent*>(ev);
  354. inputChar(chev->m_len, chev->m_char);
  355. }
  356. break;
  357. case Event::Exit:
  358. return true;
  359. case Event::Gamepad:
  360. {
  361. const GamepadEvent* gev = static_cast<const GamepadEvent*>(ev);
  362. DBG("gamepad %d, %d", gev->m_gamepad.idx, gev->m_connected);
  363. }
  364. break;
  365. case Event::Mouse:
  366. {
  367. const MouseEvent* mouse = static_cast<const MouseEvent*>(ev);
  368. handle = mouse->m_handle;
  369. if (mouse->m_move)
  370. {
  371. inputSetMousePos(mouse->m_mx, mouse->m_my, mouse->m_mz);
  372. }
  373. else
  374. {
  375. inputSetMouseButtonState(mouse->m_button, mouse->m_down);
  376. }
  377. if (NULL != _mouse
  378. && !mouseLock)
  379. {
  380. if (mouse->m_move)
  381. {
  382. _mouse->m_mx = mouse->m_mx;
  383. _mouse->m_my = mouse->m_my;
  384. _mouse->m_mz = mouse->m_mz;
  385. }
  386. else
  387. {
  388. _mouse->m_buttons[mouse->m_button] = mouse->m_down;
  389. }
  390. }
  391. }
  392. break;
  393. case Event::Key:
  394. {
  395. const KeyEvent* key = static_cast<const KeyEvent*>(ev);
  396. handle = key->m_handle;
  397. inputSetKeyState(key->m_key, key->m_modifiers, key->m_down);
  398. }
  399. break;
  400. case Event::Size:
  401. {
  402. const SizeEvent* size = static_cast<const SizeEvent*>(ev);
  403. handle = size->m_handle;
  404. _width = size->m_width;
  405. _height = size->m_height;
  406. _reset = !s_reset; // force reset
  407. }
  408. break;
  409. case Event::Window:
  410. break;
  411. case Event::Suspend:
  412. break;
  413. default:
  414. break;
  415. }
  416. }
  417. inputProcess();
  418. } while (NULL != ev);
  419. if (handle.idx == 0
  420. && _reset != s_reset)
  421. {
  422. _reset = s_reset;
  423. bgfx::reset(_width, _height, _reset);
  424. inputSetMouseResolution(_width, _height);
  425. }
  426. _debug = s_debug;
  427. return s_exit;
  428. }
  429. WindowState s_window[ENTRY_CONFIG_MAX_WINDOWS];
  430. bool processWindowEvents(WindowState& _state, uint32_t& _debug, uint32_t& _reset)
  431. {
  432. s_debug = _debug;
  433. s_reset = _reset;
  434. WindowHandle handle = { UINT16_MAX };
  435. bool mouseLock = inputIsMouseLocked();
  436. const Event* ev;
  437. do
  438. {
  439. struct SE
  440. {
  441. SE(WindowHandle _handle)
  442. : m_ev(poll(_handle) )
  443. {
  444. }
  445. ~SE()
  446. {
  447. if (NULL != m_ev)
  448. {
  449. release(m_ev);
  450. }
  451. }
  452. const Event* m_ev;
  453. } scopeEvent(handle);
  454. ev = scopeEvent.m_ev;
  455. if (NULL != ev)
  456. {
  457. handle = ev->m_handle;
  458. WindowState& win = s_window[handle.idx];
  459. switch (ev->m_type)
  460. {
  461. case Event::Axis:
  462. {
  463. const AxisEvent* axis = static_cast<const AxisEvent*>(ev);
  464. inputSetGamepadAxis(axis->m_gamepad, axis->m_axis, axis->m_value);
  465. }
  466. break;
  467. case Event::Char:
  468. {
  469. const CharEvent* chev = static_cast<const CharEvent*>(ev);
  470. win.m_handle = chev->m_handle;
  471. inputChar(chev->m_len, chev->m_char);
  472. }
  473. break;
  474. case Event::Exit:
  475. return true;
  476. case Event::Gamepad:
  477. {
  478. const GamepadEvent* gev = static_cast<const GamepadEvent*>(ev);
  479. DBG("gamepad %d, %d", gev->m_gamepad.idx, gev->m_connected);
  480. }
  481. break;
  482. case Event::Mouse:
  483. {
  484. const MouseEvent* mouse = static_cast<const MouseEvent*>(ev);
  485. win.m_handle = mouse->m_handle;
  486. if (mouse->m_move)
  487. {
  488. inputSetMousePos(mouse->m_mx, mouse->m_my, mouse->m_mz);
  489. }
  490. else
  491. {
  492. inputSetMouseButtonState(mouse->m_button, mouse->m_down);
  493. }
  494. if (!mouseLock)
  495. {
  496. if (mouse->m_move)
  497. {
  498. win.m_mouse.m_mx = mouse->m_mx;
  499. win.m_mouse.m_my = mouse->m_my;
  500. win.m_mouse.m_mz = mouse->m_mz;
  501. }
  502. else
  503. {
  504. win.m_mouse.m_buttons[mouse->m_button] = mouse->m_down;
  505. }
  506. }
  507. }
  508. break;
  509. case Event::Key:
  510. {
  511. const KeyEvent* key = static_cast<const KeyEvent*>(ev);
  512. win.m_handle = key->m_handle;
  513. inputSetKeyState(key->m_key, key->m_modifiers, key->m_down);
  514. }
  515. break;
  516. case Event::Size:
  517. {
  518. const SizeEvent* size = static_cast<const SizeEvent*>(ev);
  519. win.m_handle = size->m_handle;
  520. win.m_width = size->m_width;
  521. win.m_height = size->m_height;
  522. _reset = win.m_handle.idx == 0
  523. ? !s_reset
  524. : _reset
  525. ; // force reset
  526. }
  527. break;
  528. case Event::Window:
  529. {
  530. const WindowEvent* window = static_cast<const WindowEvent*>(ev);
  531. win.m_handle = window->m_handle;
  532. win.m_nwh = window->m_nwh;
  533. ev = NULL;
  534. }
  535. break;
  536. case Event::Suspend:
  537. break;
  538. default:
  539. break;
  540. }
  541. }
  542. inputProcess();
  543. } while (NULL != ev);
  544. if (isValid(handle) )
  545. {
  546. const WindowState& win = s_window[handle.idx];
  547. _state = win;
  548. if (handle.idx == 0)
  549. {
  550. inputSetMouseResolution(win.m_width, win.m_height);
  551. }
  552. }
  553. if (_reset != s_reset)
  554. {
  555. _reset = s_reset;
  556. bgfx::reset(s_window[0].m_width, s_window[0].m_height, _reset);
  557. inputSetMouseResolution(s_window[0].m_width, s_window[0].m_height);
  558. }
  559. _debug = s_debug;
  560. return s_exit;
  561. }
  562. bx::FileReaderI* getFileReader()
  563. {
  564. return s_fileReader;
  565. }
  566. bx::FileWriterI* getFileWriter()
  567. {
  568. return s_fileWriter;
  569. }
  570. bx::ReallocatorI* getAllocator()
  571. {
  572. return s_allocator;
  573. }
  574. void* TinyStlAllocator::static_allocate(size_t _bytes)
  575. {
  576. return BX_ALLOC(getAllocator(), _bytes);
  577. }
  578. void TinyStlAllocator::static_deallocate(void* _ptr, size_t /*_bytes*/)
  579. {
  580. if (NULL != _ptr)
  581. {
  582. BX_FREE(getAllocator(), _ptr);
  583. }
  584. }
  585. } // namespace entry
  586. extern "C" bool entry_process_events(uint32_t* _width, uint32_t* _height, uint32_t* _debug, uint32_t* _reset)
  587. {
  588. return entry::processEvents(*_width, *_height, *_debug, *_reset, NULL);
  589. }