entry.cpp 12 KB

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