entry.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. 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. bool setOrToggle(uint32_t& _flags, const char* _name, uint32_t _bit, int _first, int _argc, char const* const* _argv)
  21. {
  22. if (0 == strcmp(_argv[_first], _name) )
  23. {
  24. int arg = _first+1;
  25. if (_argc > arg)
  26. {
  27. _flags &= ~_bit;
  28. _flags |= bx::toBool(_argv[arg]) ? _bit : 0;
  29. }
  30. else
  31. {
  32. _flags ^= _bit;
  33. }
  34. return true;
  35. }
  36. return false;
  37. }
  38. void cmd(const void* _userData)
  39. {
  40. cmdExec( (const char*)_userData);
  41. }
  42. int cmdMouseLock(CmdContext* /*_context*/, void* /*_userData*/, int _argc, char const* const* _argv)
  43. {
  44. if (_argc > 1)
  45. {
  46. inputSetMouseLock(_argc > 1 ? bx::toBool(_argv[1]) : !inputIsMouseLocked() );
  47. return 0;
  48. }
  49. return 1;
  50. }
  51. int cmdGraphics(CmdContext* /*_context*/, void* /*_userData*/, int _argc, char const* const* _argv)
  52. {
  53. if (_argc > 1)
  54. {
  55. if (setOrToggle(s_reset, "vsync", BGFX_RESET_VSYNC, 1, _argc, _argv)
  56. || setOrToggle(s_reset, "msaa", BGFX_RESET_MSAA_X16, 1, _argc, _argv) )
  57. {
  58. return 0;
  59. }
  60. else if (setOrToggle(s_debug, "stats", BGFX_DEBUG_STATS, 1, _argc, _argv)
  61. || setOrToggle(s_debug, "ifh", BGFX_DEBUG_IFH, 1, _argc, _argv)
  62. || setOrToggle(s_debug, "text", BGFX_DEBUG_TEXT, 1, _argc, _argv)
  63. || setOrToggle(s_debug, "wireframe", BGFX_DEBUG_WIREFRAME, 1, _argc, _argv) )
  64. {
  65. bgfx::setDebug(s_debug);
  66. return 0;
  67. }
  68. else if (0 == strcmp(_argv[1], "screenshot") )
  69. {
  70. if (_argc > 2)
  71. {
  72. bgfx::saveScreenShot(_argv[2]);
  73. }
  74. else
  75. {
  76. time_t tt;
  77. time(&tt);
  78. char filePath[256];
  79. bx::snprintf(filePath, sizeof(filePath), "temp/screenshot-%d", tt);
  80. bgfx::saveScreenShot(filePath);
  81. }
  82. return 0;
  83. }
  84. }
  85. return 1;
  86. }
  87. int cmdExit(CmdContext* /*_context*/, void* /*_userData*/, int /*_argc*/, char const* const* /*_argv*/)
  88. {
  89. s_exit = true;
  90. return 0;
  91. }
  92. static const InputBinding s_bindings[] =
  93. {
  94. { entry::Key::KeyQ, entry::Modifier::LeftCtrl, 1, cmd, "exit" },
  95. { entry::Key::F1, entry::Modifier::None, 1, cmd, "graphics stats" },
  96. { entry::Key::F1, entry::Modifier::LeftShift, 1, cmd, "graphics stats 0\ngraphics text 0" },
  97. { entry::Key::F3, entry::Modifier::None, 1, cmd, "graphics wireframe" },
  98. { entry::Key::F7, entry::Modifier::None, 1, cmd, "graphics vsync" },
  99. { entry::Key::F8, entry::Modifier::None, 1, cmd, "graphics msaa" },
  100. { entry::Key::Print, entry::Modifier::None, 1, cmd, "graphics screenshot" },
  101. INPUT_BINDING_END
  102. };
  103. int main(int _argc, char** _argv)
  104. {
  105. //DBG(BX_COMPILER_NAME " / " BX_CPU_NAME " / " BX_ARCH_NAME " / " BX_PLATFORM_NAME);
  106. #if BX_CONFIG_CRT_FILE_READER_WRITER
  107. s_fileReader = new bx::CrtFileReader;
  108. s_fileWriter = new bx::CrtFileWriter;
  109. #endif // BX_CONFIG_CRT_FILE_READER_WRITER
  110. cmdAdd("mouselock", cmdMouseLock);
  111. cmdAdd("graphics", cmdGraphics );
  112. cmdAdd("exit", cmdExit );
  113. inputAddBindings("bindings", s_bindings);
  114. int32_t result = ::_main_(_argc, _argv);
  115. #if BX_CONFIG_CRT_FILE_READER_WRITER
  116. delete s_fileReader;
  117. s_fileReader = NULL;
  118. delete s_fileWriter;
  119. s_fileWriter = NULL;
  120. #endif // BX_CONFIG_CRT_FILE_READER_WRITER
  121. return result;
  122. }
  123. char keyToAscii(entry::Key::Enum _key, bool _shiftModifier)
  124. {
  125. static const char s_keyToAscii[entry::Key::Count] =
  126. {
  127. '\0', // None
  128. 0x1b, // Esc
  129. 0x0d, // Return
  130. 0x09, // Tab
  131. 0x20, // Space
  132. 0x08, // Backspace
  133. '\0', // Up
  134. '\0', // Down
  135. '\0', // Left
  136. '\0', // Right
  137. '\0', // PageUp
  138. '\0', // PageDown
  139. '\0', // Home
  140. '\0', // End
  141. '\0', // Print
  142. 0x3d, // Equals
  143. 0x2d, // Minus
  144. '\0', // F1
  145. '\0', // F2
  146. '\0', // F3
  147. '\0', // F4
  148. '\0', // F5
  149. '\0', // F6
  150. '\0', // F7
  151. '\0', // F8
  152. '\0', // F9
  153. '\0', // F10
  154. '\0', // F11
  155. '\0', // F12
  156. 0x30, // NumPad0
  157. 0x31, // NumPad1
  158. 0x32, // NumPad2
  159. 0x33, // NumPad3
  160. 0x34, // NumPad4
  161. 0x35, // NumPad5
  162. 0x36, // NumPad6
  163. 0x37, // NumPad7
  164. 0x38, // NumPad8
  165. 0x39, // NumPad9
  166. 0x30, // Key0
  167. 0x31, // Key1
  168. 0x32, // Key2
  169. 0x33, // Key3
  170. 0x34, // Key4
  171. 0x35, // Key5
  172. 0x36, // Key6
  173. 0x37, // Key7
  174. 0x38, // Key8
  175. 0x39, // Key9
  176. 0x61, // KeyA
  177. 0x62, // KeyB
  178. 0x63, // KeyC
  179. 0x64, // KeyD
  180. 0x65, // KeyE
  181. 0x66, // KeyF
  182. 0x67, // KeyG
  183. 0x68, // KeyH
  184. 0x69, // KeyI
  185. 0x6a, // KeyJ
  186. 0x6b, // KeyK
  187. 0x6c, // KeyL
  188. 0x6d, // KeyM
  189. 0x6e, // KeyN
  190. 0x6f, // KeyO
  191. 0x70, // KeyP
  192. 0x71, // KeyQ
  193. 0x72, // KeyR
  194. 0x73, // KeyS
  195. 0x74, // KeyT
  196. 0x75, // KeyU
  197. 0x76, // KeyV
  198. 0x77, // KeyW
  199. 0x78, // KeyX
  200. 0x79, // KeyY
  201. 0x7a, // KeyZ
  202. };
  203. char ascii = s_keyToAscii[_key];
  204. if (_shiftModifier)
  205. {
  206. // Big letters.
  207. if(ascii >= 'a' && ascii <= 'z')
  208. {
  209. ascii += 'A' - 'a';
  210. }
  211. // Special cases.
  212. else if('-' == ascii)
  213. {
  214. ascii = '_';
  215. }
  216. else if ('=' == ascii)
  217. {
  218. ascii = '+';
  219. }
  220. }
  221. return ascii;
  222. }
  223. bool processEvents(uint32_t& _width, uint32_t& _height, uint32_t& _debug, uint32_t& _reset, MouseState* _mouse, KeyState* _keyboard)
  224. {
  225. s_debug = _debug;
  226. s_reset = _reset;
  227. bool mouseLock = inputIsMouseLocked();
  228. if (NULL != _keyboard)
  229. {
  230. _keyboard->m_modifiers = 0;
  231. memset(_keyboard->m_keysDown, 0, sizeof(_keyboard->m_keysDown) );
  232. }
  233. const Event* ev;
  234. do
  235. {
  236. struct SE { const Event* m_ev; SE() : m_ev(poll() ) {} ~SE() { if (NULL != m_ev) { release(m_ev); } } } scopeEvent;
  237. ev = scopeEvent.m_ev;
  238. if (NULL != ev)
  239. {
  240. switch (ev->m_type)
  241. {
  242. case Event::Exit:
  243. return true;
  244. case Event::Mouse:
  245. {
  246. const MouseEvent* mouse = static_cast<const MouseEvent*>(ev);
  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. inputSetKeyState(key->m_key, key->m_modifiers, key->m_down);
  275. if (NULL != _keyboard)
  276. {
  277. _keyboard->m_modifiers |= key->m_modifiers;
  278. if (key->m_down)
  279. {
  280. _keyboard->m_keysDown[key->m_key] = true;
  281. }
  282. }
  283. }
  284. break;
  285. case Event::Size:
  286. {
  287. const SizeEvent* size = static_cast<const SizeEvent*>(ev);
  288. _width = size->m_width;
  289. _height = size->m_height;
  290. _reset = !s_reset; // force reset
  291. }
  292. break;
  293. default:
  294. break;
  295. }
  296. }
  297. inputProcess();
  298. } while (NULL != ev);
  299. if (_reset != s_reset)
  300. {
  301. _reset = s_reset;
  302. bgfx::reset(_width, _height, _reset);
  303. inputSetMouseResolution(_width, _height);
  304. }
  305. _debug = s_debug;
  306. return s_exit;
  307. }
  308. bx::FileReaderI* getFileReader()
  309. {
  310. return s_fileReader;
  311. }
  312. bx::FileWriterI* getFileWriter()
  313. {
  314. return s_fileWriter;
  315. }
  316. } // namespace entry
  317. extern "C" bool entry_process_events(uint32_t* _width, uint32_t* _height, uint32_t* _debug, uint32_t* _reset)
  318. {
  319. return entry::processEvents(*_width, *_height, *_debug, *_reset, NULL);
  320. }