entry.cpp 12 KB

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