entry.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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. char keyToAscii(Key::Enum _key, uint8_t _modifiers)
  34. {
  35. const bool isAscii = (Key::Key0 <= _key && _key <= Key::KeyZ)
  36. || (Key::Esc <= _key && _key <= Key::Minus);
  37. if (!isAscii)
  38. {
  39. return '\0';
  40. }
  41. const bool isNumber = (Key::Key0 <= _key && _key <= Key::Key9);
  42. if (isNumber)
  43. {
  44. return '0' + (_key - Key::Key0);
  45. }
  46. const bool isChar = (Key::KeyA <= _key && _key <= Key::KeyZ);
  47. if (isChar)
  48. {
  49. enum { ShiftMask = Modifier::LeftShift|Modifier::RightShift };
  50. const bool shift = !!(_modifiers&ShiftMask);
  51. return (shift ? 'A' : 'a') + (_key - Key::KeyA);
  52. }
  53. switch (_key)
  54. {
  55. case Key::Esc: return 0x1b;
  56. case Key::Return: return '\n';
  57. case Key::Tab: return '\t';
  58. case Key::Space: return ' ';
  59. case Key::Backspace: return 0x08;
  60. case Key::Plus: return '+';
  61. case Key::Minus: return '-';
  62. default: break;
  63. }
  64. return '\0';
  65. }
  66. bool setOrToggle(uint32_t& _flags, const char* _name, uint32_t _bit, int _first, int _argc, char const* const* _argv)
  67. {
  68. if (0 == strcmp(_argv[_first], _name) )
  69. {
  70. int arg = _first+1;
  71. if (_argc > arg)
  72. {
  73. _flags &= ~_bit;
  74. _flags |= bx::toBool(_argv[arg]) ? _bit : 0;
  75. }
  76. else
  77. {
  78. _flags ^= _bit;
  79. }
  80. return true;
  81. }
  82. return false;
  83. }
  84. void cmd(const void* _userData)
  85. {
  86. cmdExec( (const char*)_userData);
  87. }
  88. int cmdMouseLock(CmdContext* /*_context*/, void* /*_userData*/, int _argc, char const* const* _argv)
  89. {
  90. if (_argc > 1)
  91. {
  92. inputSetMouseLock(_argc > 1 ? bx::toBool(_argv[1]) : !inputIsMouseLocked() );
  93. return 0;
  94. }
  95. return 1;
  96. }
  97. int cmdGraphics(CmdContext* /*_context*/, void* /*_userData*/, int _argc, char const* const* _argv)
  98. {
  99. if (_argc > 1)
  100. {
  101. if (setOrToggle(s_reset, "vsync", BGFX_RESET_VSYNC, 1, _argc, _argv)
  102. || setOrToggle(s_reset, "maxaniso", BGFX_RESET_MAXANISOTROPY, 1, _argc, _argv)
  103. || setOrToggle(s_reset, "hmd", BGFX_RESET_HMD, 1, _argc, _argv)
  104. || setOrToggle(s_reset, "hmddbg", BGFX_RESET_HMD_DEBUG, 1, _argc, _argv)
  105. || setOrToggle(s_reset, "hmdrecenter", BGFX_RESET_HMD_RECENTER, 1, _argc, _argv)
  106. || setOrToggle(s_reset, "msaa", BGFX_RESET_MSAA_X16, 1, _argc, _argv)
  107. || setOrToggle(s_reset, "flush", BGFX_RESET_FLUSH_AFTER_RENDER, 1, _argc, _argv)
  108. || setOrToggle(s_reset, "flip", BGFX_RESET_FLIP_AFTER_RENDER, 1, _argc, _argv)
  109. )
  110. {
  111. return 0;
  112. }
  113. else if (setOrToggle(s_debug, "stats", BGFX_DEBUG_STATS, 1, _argc, _argv)
  114. || setOrToggle(s_debug, "ifh", BGFX_DEBUG_IFH, 1, _argc, _argv)
  115. || setOrToggle(s_debug, "text", BGFX_DEBUG_TEXT, 1, _argc, _argv)
  116. || setOrToggle(s_debug, "wireframe", BGFX_DEBUG_WIREFRAME, 1, _argc, _argv) )
  117. {
  118. bgfx::setDebug(s_debug);
  119. return 0;
  120. }
  121. else if (0 == strcmp(_argv[1], "screenshot") )
  122. {
  123. if (_argc > 2)
  124. {
  125. bgfx::saveScreenShot(_argv[2]);
  126. }
  127. else
  128. {
  129. time_t tt;
  130. time(&tt);
  131. char filePath[256];
  132. bx::snprintf(filePath, sizeof(filePath), "temp/screenshot-%d", tt);
  133. bgfx::saveScreenShot(filePath);
  134. }
  135. return 0;
  136. }
  137. }
  138. return 1;
  139. }
  140. int cmdExit(CmdContext* /*_context*/, void* /*_userData*/, int /*_argc*/, char const* const* /*_argv*/)
  141. {
  142. s_exit = true;
  143. return 0;
  144. }
  145. static const InputBinding s_bindings[] =
  146. {
  147. { entry::Key::KeyQ, entry::Modifier::LeftCtrl, 1, cmd, "exit" },
  148. { entry::Key::F1, entry::Modifier::None, 1, cmd, "graphics stats" },
  149. { entry::Key::GamepadStart, entry::Modifier::None, 1, cmd, "graphics stats" },
  150. { entry::Key::F1, entry::Modifier::LeftShift, 1, cmd, "graphics stats 0\ngraphics text 0" },
  151. { entry::Key::F3, entry::Modifier::None, 1, cmd, "graphics wireframe" },
  152. { entry::Key::F4, entry::Modifier::None, 1, cmd, "graphics hmd" },
  153. { entry::Key::F4, entry::Modifier::LeftShift, 1, cmd, "graphics hmdrecenter" },
  154. { entry::Key::F4, entry::Modifier::LeftCtrl, 1, cmd, "graphics hmddbg" },
  155. { entry::Key::F7, entry::Modifier::None, 1, cmd, "graphics vsync" },
  156. { entry::Key::F8, entry::Modifier::None, 1, cmd, "graphics msaa" },
  157. { entry::Key::F9, entry::Modifier::None, 1, cmd, "graphics flush" },
  158. { entry::Key::Print, entry::Modifier::None, 1, cmd, "graphics screenshot" },
  159. INPUT_BINDING_END
  160. };
  161. int main(int _argc, char** _argv)
  162. {
  163. //DBG(BX_COMPILER_NAME " / " BX_CPU_NAME " / " BX_ARCH_NAME " / " BX_PLATFORM_NAME);
  164. #if BX_CONFIG_CRT_FILE_READER_WRITER
  165. s_fileReader = new bx::CrtFileReader;
  166. s_fileWriter = new bx::CrtFileWriter;
  167. #endif // BX_CONFIG_CRT_FILE_READER_WRITER
  168. cmdInit();
  169. cmdAdd("mouselock", cmdMouseLock);
  170. cmdAdd("graphics", cmdGraphics );
  171. cmdAdd("exit", cmdExit );
  172. inputInit();
  173. inputAddBindings("bindings", s_bindings);
  174. entry::WindowHandle defaultWindow = { 0 };
  175. entry::setWindowTitle(defaultWindow, bx::baseName(_argv[0]));
  176. int32_t result = ::_main_(_argc, _argv);
  177. inputRemoveBindings("bindings");
  178. inputShutdown();
  179. cmdShutdown();
  180. #if BX_CONFIG_CRT_FILE_READER_WRITER
  181. delete s_fileReader;
  182. s_fileReader = NULL;
  183. delete s_fileWriter;
  184. s_fileWriter = NULL;
  185. #endif // BX_CONFIG_CRT_FILE_READER_WRITER
  186. return result;
  187. }
  188. bool processEvents(uint32_t& _width, uint32_t& _height, uint32_t& _debug, uint32_t& _reset, MouseState* _mouse)
  189. {
  190. s_debug = _debug;
  191. s_reset = _reset;
  192. WindowHandle handle = { UINT16_MAX };
  193. bool mouseLock = inputIsMouseLocked();
  194. const Event* ev;
  195. do
  196. {
  197. struct SE { const Event* m_ev; SE() : m_ev(poll() ) {} ~SE() { if (NULL != m_ev) { release(m_ev); } } } scopeEvent;
  198. ev = scopeEvent.m_ev;
  199. if (NULL != ev)
  200. {
  201. switch (ev->m_type)
  202. {
  203. case Event::Axis:
  204. {
  205. const AxisEvent* axis = static_cast<const AxisEvent*>(ev);
  206. inputSetGamepadAxis(axis->m_gamepad, axis->m_axis, axis->m_value);
  207. }
  208. break;
  209. case Event::Char:
  210. {
  211. const CharEvent* chev = static_cast<const CharEvent*>(ev);
  212. inputChar(chev->m_len, chev->m_char);
  213. }
  214. break;
  215. case Event::Exit:
  216. return true;
  217. case Event::Gamepad:
  218. {
  219. const GamepadEvent* gev = static_cast<const GamepadEvent*>(ev);
  220. DBG("gamepad %d, %d", gev->m_gamepad.idx, gev->m_connected);
  221. }
  222. break;
  223. case Event::Mouse:
  224. {
  225. const MouseEvent* mouse = static_cast<const MouseEvent*>(ev);
  226. handle = mouse->m_handle;
  227. if (mouse->m_move)
  228. {
  229. inputSetMousePos(mouse->m_mx, mouse->m_my, mouse->m_mz);
  230. }
  231. else
  232. {
  233. inputSetMouseButtonState(mouse->m_button, mouse->m_down);
  234. }
  235. if (NULL != _mouse
  236. && !mouseLock)
  237. {
  238. if (mouse->m_move)
  239. {
  240. _mouse->m_mx = mouse->m_mx;
  241. _mouse->m_my = mouse->m_my;
  242. _mouse->m_mz = mouse->m_mz;
  243. }
  244. else
  245. {
  246. _mouse->m_buttons[mouse->m_button] = mouse->m_down;
  247. }
  248. }
  249. }
  250. break;
  251. case Event::Key:
  252. {
  253. const KeyEvent* key = static_cast<const KeyEvent*>(ev);
  254. handle = key->m_handle;
  255. inputSetKeyState(key->m_key, key->m_modifiers, key->m_down);
  256. }
  257. break;
  258. case Event::Size:
  259. {
  260. const SizeEvent* size = static_cast<const SizeEvent*>(ev);
  261. handle = size->m_handle;
  262. _width = size->m_width;
  263. _height = size->m_height;
  264. _reset = !s_reset; // force reset
  265. }
  266. break;
  267. case Event::Window:
  268. break;
  269. default:
  270. break;
  271. }
  272. }
  273. inputProcess();
  274. } while (NULL != ev);
  275. if (handle.idx == 0
  276. && _reset != s_reset)
  277. {
  278. _reset = s_reset;
  279. bgfx::reset(_width, _height, _reset);
  280. inputSetMouseResolution(_width, _height);
  281. }
  282. _debug = s_debug;
  283. return s_exit;
  284. }
  285. WindowState s_window[ENTRY_CONFIG_MAX_WINDOWS];
  286. bool processWindowEvents(WindowState& _state, uint32_t& _debug, uint32_t& _reset)
  287. {
  288. s_debug = _debug;
  289. s_reset = _reset;
  290. WindowHandle handle = { UINT16_MAX };
  291. bool mouseLock = inputIsMouseLocked();
  292. const Event* ev;
  293. do
  294. {
  295. struct SE
  296. {
  297. SE(WindowHandle _handle)
  298. : m_ev(poll(_handle) )
  299. {
  300. }
  301. ~SE()
  302. {
  303. if (NULL != m_ev)
  304. {
  305. release(m_ev);
  306. }
  307. }
  308. const Event* m_ev;
  309. } scopeEvent(handle);
  310. ev = scopeEvent.m_ev;
  311. if (NULL != ev)
  312. {
  313. handle = ev->m_handle;
  314. WindowState& win = s_window[handle.idx];
  315. switch (ev->m_type)
  316. {
  317. case Event::Axis:
  318. {
  319. const AxisEvent* axis = static_cast<const AxisEvent*>(ev);
  320. inputSetGamepadAxis(axis->m_gamepad, axis->m_axis, axis->m_value);
  321. }
  322. break;
  323. case Event::Char:
  324. {
  325. const CharEvent* chev = static_cast<const CharEvent*>(ev);
  326. win.m_handle = chev->m_handle;
  327. inputChar(chev->m_len, chev->m_char);
  328. }
  329. break;
  330. case Event::Exit:
  331. return true;
  332. case Event::Gamepad:
  333. {
  334. const GamepadEvent* gev = static_cast<const GamepadEvent*>(ev);
  335. DBG("gamepad %d, %d", gev->m_gamepad.idx, gev->m_connected);
  336. }
  337. break;
  338. case Event::Mouse:
  339. {
  340. const MouseEvent* mouse = static_cast<const MouseEvent*>(ev);
  341. win.m_handle = mouse->m_handle;
  342. if (mouse->m_move)
  343. {
  344. inputSetMousePos(mouse->m_mx, mouse->m_my, mouse->m_mz);
  345. }
  346. else
  347. {
  348. inputSetMouseButtonState(mouse->m_button, mouse->m_down);
  349. }
  350. if (!mouseLock)
  351. {
  352. if (mouse->m_move)
  353. {
  354. win.m_mouse.m_mx = mouse->m_mx;
  355. win.m_mouse.m_my = mouse->m_my;
  356. win.m_mouse.m_mz = mouse->m_mz;
  357. }
  358. else
  359. {
  360. win.m_mouse.m_buttons[mouse->m_button] = mouse->m_down;
  361. }
  362. }
  363. }
  364. break;
  365. case Event::Key:
  366. {
  367. const KeyEvent* key = static_cast<const KeyEvent*>(ev);
  368. win.m_handle = key->m_handle;
  369. inputSetKeyState(key->m_key, key->m_modifiers, key->m_down);
  370. }
  371. break;
  372. case Event::Size:
  373. {
  374. const SizeEvent* size = static_cast<const SizeEvent*>(ev);
  375. win.m_handle = size->m_handle;
  376. win.m_width = size->m_width;
  377. win.m_height = size->m_height;
  378. _reset = win.m_handle.idx == 0
  379. ? !s_reset
  380. : _reset
  381. ; // force reset
  382. }
  383. break;
  384. case Event::Window:
  385. {
  386. const WindowEvent* window = static_cast<const WindowEvent*>(ev);
  387. win.m_handle = window->m_handle;
  388. win.m_nwh = window->m_nwh;
  389. ev = NULL;
  390. }
  391. break;
  392. default:
  393. break;
  394. }
  395. }
  396. inputProcess();
  397. } while (NULL != ev);
  398. if (isValid(handle) )
  399. {
  400. const WindowState& win = s_window[handle.idx];
  401. _state = win;
  402. if (handle.idx == 0)
  403. {
  404. inputSetMouseResolution(win.m_width, win.m_height);
  405. }
  406. }
  407. if (_reset != s_reset)
  408. {
  409. _reset = s_reset;
  410. bgfx::reset(s_window[0].m_width, s_window[0].m_height, _reset);
  411. inputSetMouseResolution(s_window[0].m_width, s_window[0].m_height);
  412. }
  413. _debug = s_debug;
  414. return s_exit;
  415. }
  416. bx::FileReaderI* getFileReader()
  417. {
  418. return s_fileReader;
  419. }
  420. bx::FileWriterI* getFileWriter()
  421. {
  422. return s_fileWriter;
  423. }
  424. bx::ReallocatorI* getAllocator()
  425. {
  426. return s_allocator;
  427. }
  428. void* TinyStlAllocator::static_allocate(size_t _bytes)
  429. {
  430. return BX_ALLOC(getAllocator(), _bytes);
  431. }
  432. void TinyStlAllocator::static_deallocate(void* _ptr, size_t /*_bytes*/)
  433. {
  434. if (NULL != _ptr)
  435. {
  436. BX_FREE(getAllocator(), _ptr);
  437. }
  438. }
  439. } // namespace entry
  440. extern "C" bool entry_process_events(uint32_t* _width, uint32_t* _height, uint32_t* _debug, uint32_t* _reset)
  441. {
  442. return entry::processEvents(*_width, *_height, *_debug, *_reset, NULL);
  443. }