entry.cpp 14 KB

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