entry.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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. #if BX_PLATFORM_EMSCRIPTEN
  293. s_app = _app;
  294. emscripten_set_main_loop(&updateApp, -1, 1);
  295. #else
  296. while (_app->update() );
  297. #endif // BX_PLATFORM_EMSCRIPTEN
  298. return _app->shutdown();
  299. }
  300. int main(int _argc, char** _argv)
  301. {
  302. //DBG(BX_COMPILER_NAME " / " BX_CPU_NAME " / " BX_ARCH_NAME " / " BX_PLATFORM_NAME);
  303. #if BX_CONFIG_CRT_FILE_READER_WRITER
  304. s_fileReader = new bx::CrtFileReader;
  305. s_fileWriter = new bx::CrtFileWriter;
  306. #endif // BX_CONFIG_CRT_FILE_READER_WRITER
  307. cmdInit();
  308. cmdAdd("mouselock", cmdMouseLock);
  309. cmdAdd("graphics", cmdGraphics );
  310. cmdAdd("exit", cmdExit );
  311. inputInit();
  312. inputAddBindings("bindings", s_bindings);
  313. entry::WindowHandle defaultWindow = { 0 };
  314. entry::setWindowTitle(defaultWindow, bx::baseName(_argv[0]) );
  315. int32_t result = ::_main_(_argc, _argv);
  316. inputRemoveBindings("bindings");
  317. inputShutdown();
  318. cmdShutdown();
  319. #if BX_CONFIG_CRT_FILE_READER_WRITER
  320. delete s_fileReader;
  321. s_fileReader = NULL;
  322. delete s_fileWriter;
  323. s_fileWriter = NULL;
  324. #endif // BX_CONFIG_CRT_FILE_READER_WRITER
  325. return result;
  326. }
  327. bool processEvents(uint32_t& _width, uint32_t& _height, uint32_t& _debug, uint32_t& _reset, MouseState* _mouse)
  328. {
  329. s_debug = _debug;
  330. s_reset = _reset;
  331. WindowHandle handle = { UINT16_MAX };
  332. bool mouseLock = inputIsMouseLocked();
  333. const Event* ev;
  334. do
  335. {
  336. struct SE { const Event* m_ev; SE() : m_ev(poll() ) {} ~SE() { if (NULL != m_ev) { release(m_ev); } } } scopeEvent;
  337. ev = scopeEvent.m_ev;
  338. if (NULL != ev)
  339. {
  340. switch (ev->m_type)
  341. {
  342. case Event::Axis:
  343. {
  344. const AxisEvent* axis = static_cast<const AxisEvent*>(ev);
  345. inputSetGamepadAxis(axis->m_gamepad, axis->m_axis, axis->m_value);
  346. }
  347. break;
  348. case Event::Char:
  349. {
  350. const CharEvent* chev = static_cast<const CharEvent*>(ev);
  351. inputChar(chev->m_len, chev->m_char);
  352. }
  353. break;
  354. case Event::Exit:
  355. return true;
  356. case Event::Gamepad:
  357. {
  358. const GamepadEvent* gev = static_cast<const GamepadEvent*>(ev);
  359. DBG("gamepad %d, %d", gev->m_gamepad.idx, gev->m_connected);
  360. }
  361. break;
  362. case Event::Mouse:
  363. {
  364. const MouseEvent* mouse = static_cast<const MouseEvent*>(ev);
  365. handle = mouse->m_handle;
  366. if (mouse->m_move)
  367. {
  368. inputSetMousePos(mouse->m_mx, mouse->m_my, mouse->m_mz);
  369. }
  370. else
  371. {
  372. inputSetMouseButtonState(mouse->m_button, mouse->m_down);
  373. }
  374. if (NULL != _mouse
  375. && !mouseLock)
  376. {
  377. if (mouse->m_move)
  378. {
  379. _mouse->m_mx = mouse->m_mx;
  380. _mouse->m_my = mouse->m_my;
  381. _mouse->m_mz = mouse->m_mz;
  382. }
  383. else
  384. {
  385. _mouse->m_buttons[mouse->m_button] = mouse->m_down;
  386. }
  387. }
  388. }
  389. break;
  390. case Event::Key:
  391. {
  392. const KeyEvent* key = static_cast<const KeyEvent*>(ev);
  393. handle = key->m_handle;
  394. inputSetKeyState(key->m_key, key->m_modifiers, key->m_down);
  395. }
  396. break;
  397. case Event::Size:
  398. {
  399. const SizeEvent* size = static_cast<const SizeEvent*>(ev);
  400. handle = size->m_handle;
  401. _width = size->m_width;
  402. _height = size->m_height;
  403. _reset = !s_reset; // force reset
  404. }
  405. break;
  406. case Event::Window:
  407. break;
  408. case Event::Suspend:
  409. break;
  410. default:
  411. break;
  412. }
  413. }
  414. inputProcess();
  415. } while (NULL != ev);
  416. if (handle.idx == 0
  417. && _reset != s_reset)
  418. {
  419. _reset = s_reset;
  420. bgfx::reset(_width, _height, _reset);
  421. inputSetMouseResolution(_width, _height);
  422. }
  423. _debug = s_debug;
  424. return s_exit;
  425. }
  426. WindowState s_window[ENTRY_CONFIG_MAX_WINDOWS];
  427. bool processWindowEvents(WindowState& _state, uint32_t& _debug, uint32_t& _reset)
  428. {
  429. s_debug = _debug;
  430. s_reset = _reset;
  431. WindowHandle handle = { UINT16_MAX };
  432. bool mouseLock = inputIsMouseLocked();
  433. const Event* ev;
  434. do
  435. {
  436. struct SE
  437. {
  438. SE(WindowHandle _handle)
  439. : m_ev(poll(_handle) )
  440. {
  441. }
  442. ~SE()
  443. {
  444. if (NULL != m_ev)
  445. {
  446. release(m_ev);
  447. }
  448. }
  449. const Event* m_ev;
  450. } scopeEvent(handle);
  451. ev = scopeEvent.m_ev;
  452. if (NULL != ev)
  453. {
  454. handle = ev->m_handle;
  455. WindowState& win = s_window[handle.idx];
  456. switch (ev->m_type)
  457. {
  458. case Event::Axis:
  459. {
  460. const AxisEvent* axis = static_cast<const AxisEvent*>(ev);
  461. inputSetGamepadAxis(axis->m_gamepad, axis->m_axis, axis->m_value);
  462. }
  463. break;
  464. case Event::Char:
  465. {
  466. const CharEvent* chev = static_cast<const CharEvent*>(ev);
  467. win.m_handle = chev->m_handle;
  468. inputChar(chev->m_len, chev->m_char);
  469. }
  470. break;
  471. case Event::Exit:
  472. return true;
  473. case Event::Gamepad:
  474. {
  475. const GamepadEvent* gev = static_cast<const GamepadEvent*>(ev);
  476. DBG("gamepad %d, %d", gev->m_gamepad.idx, gev->m_connected);
  477. }
  478. break;
  479. case Event::Mouse:
  480. {
  481. const MouseEvent* mouse = static_cast<const MouseEvent*>(ev);
  482. win.m_handle = mouse->m_handle;
  483. if (mouse->m_move)
  484. {
  485. inputSetMousePos(mouse->m_mx, mouse->m_my, mouse->m_mz);
  486. }
  487. else
  488. {
  489. inputSetMouseButtonState(mouse->m_button, mouse->m_down);
  490. }
  491. if (!mouseLock)
  492. {
  493. if (mouse->m_move)
  494. {
  495. win.m_mouse.m_mx = mouse->m_mx;
  496. win.m_mouse.m_my = mouse->m_my;
  497. win.m_mouse.m_mz = mouse->m_mz;
  498. }
  499. else
  500. {
  501. win.m_mouse.m_buttons[mouse->m_button] = mouse->m_down;
  502. }
  503. }
  504. }
  505. break;
  506. case Event::Key:
  507. {
  508. const KeyEvent* key = static_cast<const KeyEvent*>(ev);
  509. win.m_handle = key->m_handle;
  510. inputSetKeyState(key->m_key, key->m_modifiers, key->m_down);
  511. }
  512. break;
  513. case Event::Size:
  514. {
  515. const SizeEvent* size = static_cast<const SizeEvent*>(ev);
  516. win.m_handle = size->m_handle;
  517. win.m_width = size->m_width;
  518. win.m_height = size->m_height;
  519. _reset = win.m_handle.idx == 0
  520. ? !s_reset
  521. : _reset
  522. ; // force reset
  523. }
  524. break;
  525. case Event::Window:
  526. {
  527. const WindowEvent* window = static_cast<const WindowEvent*>(ev);
  528. win.m_handle = window->m_handle;
  529. win.m_nwh = window->m_nwh;
  530. ev = NULL;
  531. }
  532. break;
  533. default:
  534. break;
  535. }
  536. }
  537. inputProcess();
  538. } while (NULL != ev);
  539. if (isValid(handle) )
  540. {
  541. const WindowState& win = s_window[handle.idx];
  542. _state = win;
  543. if (handle.idx == 0)
  544. {
  545. inputSetMouseResolution(win.m_width, win.m_height);
  546. }
  547. }
  548. if (_reset != s_reset)
  549. {
  550. _reset = s_reset;
  551. bgfx::reset(s_window[0].m_width, s_window[0].m_height, _reset);
  552. inputSetMouseResolution(s_window[0].m_width, s_window[0].m_height);
  553. }
  554. _debug = s_debug;
  555. return s_exit;
  556. }
  557. bx::FileReaderI* getFileReader()
  558. {
  559. return s_fileReader;
  560. }
  561. bx::FileWriterI* getFileWriter()
  562. {
  563. return s_fileWriter;
  564. }
  565. bx::ReallocatorI* getAllocator()
  566. {
  567. return s_allocator;
  568. }
  569. void* TinyStlAllocator::static_allocate(size_t _bytes)
  570. {
  571. return BX_ALLOC(getAllocator(), _bytes);
  572. }
  573. void TinyStlAllocator::static_deallocate(void* _ptr, size_t /*_bytes*/)
  574. {
  575. if (NULL != _ptr)
  576. {
  577. BX_FREE(getAllocator(), _ptr);
  578. }
  579. }
  580. } // namespace entry
  581. extern "C" bool entry_process_events(uint32_t* _width, uint32_t* _height, uint32_t* _debug, uint32_t* _reset)
  582. {
  583. return entry::processEvents(*_width, *_height, *_debug, *_reset, NULL);
  584. }