entry.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /*
  2. * Copyright 2011-2014 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. const uint16_t WindowHandle::invalidHandle = UINT16_MAX;
  16. static uint32_t s_debug = BGFX_DEBUG_NONE;
  17. static uint32_t s_reset = BGFX_RESET_NONE;
  18. static bool s_exit = false;
  19. static bx::FileReaderI* s_fileReader = NULL;
  20. static bx::FileWriterI* s_fileWriter = NULL;
  21. bool setOrToggle(uint32_t& _flags, const char* _name, uint32_t _bit, int _first, int _argc, char const* const* _argv)
  22. {
  23. if (0 == strcmp(_argv[_first], _name) )
  24. {
  25. int arg = _first+1;
  26. if (_argc > arg)
  27. {
  28. _flags &= ~_bit;
  29. _flags |= bx::toBool(_argv[arg]) ? _bit : 0;
  30. }
  31. else
  32. {
  33. _flags ^= _bit;
  34. }
  35. return true;
  36. }
  37. return false;
  38. }
  39. void cmd(const void* _userData)
  40. {
  41. cmdExec( (const char*)_userData);
  42. }
  43. int cmdMouseLock(CmdContext* /*_context*/, void* /*_userData*/, int _argc, char const* const* _argv)
  44. {
  45. if (_argc > 1)
  46. {
  47. inputSetMouseLock(_argc > 1 ? bx::toBool(_argv[1]) : !inputIsMouseLocked() );
  48. return 0;
  49. }
  50. return 1;
  51. }
  52. int cmdGraphics(CmdContext* /*_context*/, void* /*_userData*/, int _argc, char const* const* _argv)
  53. {
  54. if (_argc > 1)
  55. {
  56. if (setOrToggle(s_reset, "vsync", BGFX_RESET_VSYNC, 1, _argc, _argv)
  57. || setOrToggle(s_reset, "hmd", BGFX_RESET_HMD, 1, _argc, _argv)
  58. || setOrToggle(s_reset, "hmddbg", BGFX_RESET_HMD_DEBUG, 1, _argc, _argv)
  59. || setOrToggle(s_reset, "hmdrecenter", BGFX_RESET_HMD_RECENTER, 1, _argc, _argv)
  60. || setOrToggle(s_reset, "msaa", BGFX_RESET_MSAA_X16, 1, _argc, _argv) )
  61. {
  62. return 0;
  63. }
  64. else if (setOrToggle(s_debug, "stats", BGFX_DEBUG_STATS, 1, _argc, _argv)
  65. || setOrToggle(s_debug, "ifh", BGFX_DEBUG_IFH, 1, _argc, _argv)
  66. || setOrToggle(s_debug, "text", BGFX_DEBUG_TEXT, 1, _argc, _argv)
  67. || setOrToggle(s_debug, "wireframe", BGFX_DEBUG_WIREFRAME, 1, _argc, _argv) )
  68. {
  69. bgfx::setDebug(s_debug);
  70. return 0;
  71. }
  72. else if (0 == strcmp(_argv[1], "screenshot") )
  73. {
  74. if (_argc > 2)
  75. {
  76. bgfx::saveScreenShot(_argv[2]);
  77. }
  78. else
  79. {
  80. time_t tt;
  81. time(&tt);
  82. char filePath[256];
  83. bx::snprintf(filePath, sizeof(filePath), "temp/screenshot-%d", tt);
  84. bgfx::saveScreenShot(filePath);
  85. }
  86. return 0;
  87. }
  88. }
  89. return 1;
  90. }
  91. int cmdExit(CmdContext* /*_context*/, void* /*_userData*/, int /*_argc*/, char const* const* /*_argv*/)
  92. {
  93. s_exit = true;
  94. return 0;
  95. }
  96. static const InputBinding s_bindings[] =
  97. {
  98. { entry::Key::KeyQ, entry::Modifier::LeftCtrl, 1, cmd, "exit" },
  99. { entry::Key::F1, entry::Modifier::None, 1, cmd, "graphics stats" },
  100. { entry::Key::F1, entry::Modifier::LeftShift, 1, cmd, "graphics stats 0\ngraphics text 0" },
  101. { entry::Key::F3, entry::Modifier::None, 1, cmd, "graphics wireframe" },
  102. { entry::Key::F4, entry::Modifier::None, 1, cmd, "graphics hmd" },
  103. { entry::Key::F4, entry::Modifier::LeftShift, 1, cmd, "graphics hmdrecenter" },
  104. { entry::Key::F7, entry::Modifier::None, 1, cmd, "graphics vsync" },
  105. { entry::Key::F8, entry::Modifier::None, 1, cmd, "graphics msaa" },
  106. { entry::Key::Print, entry::Modifier::None, 1, cmd, "graphics screenshot" },
  107. INPUT_BINDING_END
  108. };
  109. int main(int _argc, char** _argv)
  110. {
  111. //DBG(BX_COMPILER_NAME " / " BX_CPU_NAME " / " BX_ARCH_NAME " / " BX_PLATFORM_NAME);
  112. #if BX_CONFIG_CRT_FILE_READER_WRITER
  113. s_fileReader = new bx::CrtFileReader;
  114. s_fileWriter = new bx::CrtFileWriter;
  115. #endif // BX_CONFIG_CRT_FILE_READER_WRITER
  116. cmdAdd("mouselock", cmdMouseLock);
  117. cmdAdd("graphics", cmdGraphics );
  118. cmdAdd("exit", cmdExit );
  119. inputAddBindings("bindings", s_bindings);
  120. entry::WindowHandle defaultWindow = { 0 };
  121. entry::setWindowTitle(defaultWindow, bx::baseName(_argv[0]));
  122. int32_t result = ::_main_(_argc, _argv);
  123. #if BX_CONFIG_CRT_FILE_READER_WRITER
  124. delete s_fileReader;
  125. s_fileReader = NULL;
  126. delete s_fileWriter;
  127. s_fileWriter = NULL;
  128. #endif // BX_CONFIG_CRT_FILE_READER_WRITER
  129. return result;
  130. }
  131. char keyToAscii(entry::Key::Enum _key, bool _shiftModifier)
  132. {
  133. static const char s_keyToAscii[entry::Key::Count] =
  134. {
  135. '\0', // None
  136. 0x1b, // Esc
  137. 0x0d, // Return
  138. 0x09, // Tab
  139. 0x20, // Space
  140. 0x08, // Backspace
  141. '\0', // Up
  142. '\0', // Down
  143. '\0', // Left
  144. '\0', // Right
  145. '\0', // PageUp
  146. '\0', // PageDown
  147. '\0', // Home
  148. '\0', // End
  149. '\0', // Print
  150. 0x3d, // Equals
  151. 0x2d, // Minus
  152. '\0', // F1
  153. '\0', // F2
  154. '\0', // F3
  155. '\0', // F4
  156. '\0', // F5
  157. '\0', // F6
  158. '\0', // F7
  159. '\0', // F8
  160. '\0', // F9
  161. '\0', // F10
  162. '\0', // F11
  163. '\0', // F12
  164. 0x30, // NumPad0
  165. 0x31, // NumPad1
  166. 0x32, // NumPad2
  167. 0x33, // NumPad3
  168. 0x34, // NumPad4
  169. 0x35, // NumPad5
  170. 0x36, // NumPad6
  171. 0x37, // NumPad7
  172. 0x38, // NumPad8
  173. 0x39, // NumPad9
  174. 0x30, // Key0
  175. 0x31, // Key1
  176. 0x32, // Key2
  177. 0x33, // Key3
  178. 0x34, // Key4
  179. 0x35, // Key5
  180. 0x36, // Key6
  181. 0x37, // Key7
  182. 0x38, // Key8
  183. 0x39, // Key9
  184. 0x61, // KeyA
  185. 0x62, // KeyB
  186. 0x63, // KeyC
  187. 0x64, // KeyD
  188. 0x65, // KeyE
  189. 0x66, // KeyF
  190. 0x67, // KeyG
  191. 0x68, // KeyH
  192. 0x69, // KeyI
  193. 0x6a, // KeyJ
  194. 0x6b, // KeyK
  195. 0x6c, // KeyL
  196. 0x6d, // KeyM
  197. 0x6e, // KeyN
  198. 0x6f, // KeyO
  199. 0x70, // KeyP
  200. 0x71, // KeyQ
  201. 0x72, // KeyR
  202. 0x73, // KeyS
  203. 0x74, // KeyT
  204. 0x75, // KeyU
  205. 0x76, // KeyV
  206. 0x77, // KeyW
  207. 0x78, // KeyX
  208. 0x79, // KeyY
  209. 0x7a, // KeyZ
  210. };
  211. char ascii = s_keyToAscii[_key];
  212. if (_shiftModifier)
  213. {
  214. // Big letters.
  215. if (ascii >= 'a' && ascii <= 'z')
  216. {
  217. ascii += 'A' - 'a';
  218. }
  219. // Special cases.
  220. else if ('-' == ascii)
  221. {
  222. ascii = '_';
  223. }
  224. else if ('=' == ascii)
  225. {
  226. ascii = '+';
  227. }
  228. }
  229. return ascii;
  230. }
  231. bool processEvents(uint32_t& _width, uint32_t& _height, uint32_t& _debug, uint32_t& _reset, MouseState* _mouse)
  232. {
  233. s_debug = _debug;
  234. s_reset = _reset;
  235. WindowHandle handle = { UINT16_MAX };
  236. bool mouseLock = inputIsMouseLocked();
  237. const Event* ev;
  238. do
  239. {
  240. struct SE { const Event* m_ev; SE() : m_ev(poll() ) {} ~SE() { if (NULL != m_ev) { release(m_ev); } } } scopeEvent;
  241. ev = scopeEvent.m_ev;
  242. if (NULL != ev)
  243. {
  244. switch (ev->m_type)
  245. {
  246. case Event::Exit:
  247. return true;
  248. case Event::Mouse:
  249. {
  250. const MouseEvent* mouse = static_cast<const MouseEvent*>(ev);
  251. handle = mouse->m_handle;
  252. if (mouse->m_move)
  253. {
  254. inputSetMousePos(mouse->m_mx, mouse->m_my, mouse->m_mz);
  255. }
  256. else
  257. {
  258. inputSetMouseButtonState(mouse->m_button, mouse->m_down);
  259. }
  260. if (NULL != _mouse
  261. && !mouseLock)
  262. {
  263. if (mouse->m_move)
  264. {
  265. _mouse->m_mx = mouse->m_mx;
  266. _mouse->m_my = mouse->m_my;
  267. _mouse->m_mz = mouse->m_mz;
  268. }
  269. else
  270. {
  271. _mouse->m_buttons[mouse->m_button] = mouse->m_down;
  272. }
  273. }
  274. }
  275. break;
  276. case Event::Key:
  277. {
  278. const KeyEvent* key = static_cast<const KeyEvent*>(ev);
  279. handle = key->m_handle;
  280. inputSetKeyState(key->m_key, key->m_modifiers, key->m_down);
  281. }
  282. break;
  283. case Event::Char:
  284. {
  285. const CharEvent* chev = static_cast<const CharEvent*>(ev);
  286. inputChar(chev->m_len, chev->m_char);
  287. }
  288. break;
  289. case Event::Size:
  290. {
  291. const SizeEvent* size = static_cast<const SizeEvent*>(ev);
  292. handle = size->m_handle;
  293. _width = size->m_width;
  294. _height = size->m_height;
  295. _reset = !s_reset; // force reset
  296. }
  297. break;
  298. case Event::Window:
  299. break;
  300. default:
  301. break;
  302. }
  303. }
  304. inputProcess();
  305. } while (NULL != ev);
  306. if (handle.idx == 0
  307. && _reset != s_reset)
  308. {
  309. _reset = s_reset;
  310. bgfx::reset(_width, _height, _reset);
  311. inputSetMouseResolution(_width, _height);
  312. }
  313. _debug = s_debug;
  314. return s_exit;
  315. }
  316. WindowState s_window[ENTRY_CONFIG_MAX_WINDOWS];
  317. bool processWindowEvents(WindowState& _state, uint32_t& _debug, uint32_t& _reset)
  318. {
  319. s_debug = _debug;
  320. s_reset = _reset;
  321. WindowHandle handle = { UINT16_MAX };
  322. bool mouseLock = inputIsMouseLocked();
  323. const Event* ev;
  324. do
  325. {
  326. struct SE
  327. {
  328. SE(WindowHandle _handle)
  329. : m_ev(poll(_handle) )
  330. {
  331. }
  332. ~SE()
  333. {
  334. if (NULL != m_ev)
  335. {
  336. release(m_ev);
  337. }
  338. }
  339. const Event* m_ev;
  340. } scopeEvent(handle);
  341. ev = scopeEvent.m_ev;
  342. if (NULL != ev)
  343. {
  344. handle = ev->m_handle;
  345. WindowState& win = s_window[handle.idx];
  346. switch (ev->m_type)
  347. {
  348. case Event::Exit:
  349. return true;
  350. case Event::Mouse:
  351. {
  352. const MouseEvent* mouse = static_cast<const MouseEvent*>(ev);
  353. win.m_handle = mouse->m_handle;
  354. if (mouse->m_move)
  355. {
  356. inputSetMousePos(mouse->m_mx, mouse->m_my, mouse->m_mz);
  357. }
  358. else
  359. {
  360. inputSetMouseButtonState(mouse->m_button, mouse->m_down);
  361. }
  362. if (!mouseLock)
  363. {
  364. if (mouse->m_move)
  365. {
  366. win.m_mouse.m_mx = mouse->m_mx;
  367. win.m_mouse.m_my = mouse->m_my;
  368. win.m_mouse.m_mz = mouse->m_mz;
  369. }
  370. else
  371. {
  372. win.m_mouse.m_buttons[mouse->m_button] = mouse->m_down;
  373. }
  374. }
  375. }
  376. break;
  377. case Event::Key:
  378. {
  379. const KeyEvent* key = static_cast<const KeyEvent*>(ev);
  380. win.m_handle = key->m_handle;
  381. inputSetKeyState(key->m_key, key->m_modifiers, key->m_down);
  382. }
  383. break;
  384. case Event::Char:
  385. {
  386. const CharEvent* chev = static_cast<const CharEvent*>(ev);
  387. win.m_handle = chev->m_handle;
  388. inputChar(chev->m_len, chev->m_char);
  389. }
  390. break;
  391. case Event::Size:
  392. {
  393. const SizeEvent* size = static_cast<const SizeEvent*>(ev);
  394. win.m_handle = size->m_handle;
  395. win.m_width = size->m_width;
  396. win.m_height = size->m_height;
  397. _reset = win.m_handle.idx == 0
  398. ? !s_reset
  399. : _reset
  400. ; // force reset
  401. }
  402. break;
  403. case Event::Window:
  404. {
  405. const WindowEvent* window = static_cast<const WindowEvent*>(ev);
  406. win.m_handle = window->m_handle;
  407. win.m_nwh = window->m_nwh;
  408. ev = NULL;
  409. }
  410. break;
  411. default:
  412. break;
  413. }
  414. }
  415. inputProcess();
  416. } while (NULL != ev);
  417. if (isValid(handle) )
  418. {
  419. const WindowState& win = s_window[handle.idx];
  420. _state = win;
  421. if (handle.idx == 0)
  422. {
  423. inputSetMouseResolution(win.m_width, win.m_height);
  424. }
  425. }
  426. if (_reset != s_reset)
  427. {
  428. _reset = s_reset;
  429. bgfx::reset(s_window[0].m_width, s_window[0].m_height, _reset);
  430. inputSetMouseResolution(s_window[0].m_width, s_window[0].m_height);
  431. }
  432. _debug = s_debug;
  433. return s_exit;
  434. }
  435. bx::FileReaderI* getFileReader()
  436. {
  437. return s_fileReader;
  438. }
  439. bx::FileWriterI* getFileWriter()
  440. {
  441. return s_fileWriter;
  442. }
  443. } // namespace entry
  444. extern "C" bool entry_process_events(uint32_t* _width, uint32_t* _height, uint32_t* _debug, uint32_t* _reset)
  445. {
  446. return entry::processEvents(*_width, *_height, *_debug, *_reset, NULL);
  447. }