entry.cpp 10 KB

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