entry.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. entry::setWindowTitle(bx::baseName(_argv[0]));
  115. int32_t result = ::_main_(_argc, _argv);
  116. #if BX_CONFIG_CRT_FILE_READER_WRITER
  117. delete s_fileReader;
  118. s_fileReader = NULL;
  119. delete s_fileWriter;
  120. s_fileWriter = NULL;
  121. #endif // BX_CONFIG_CRT_FILE_READER_WRITER
  122. return result;
  123. }
  124. char keyToAscii(entry::Key::Enum _key, bool _shiftModifier)
  125. {
  126. static const char s_keyToAscii[entry::Key::Count] =
  127. {
  128. '\0', // None
  129. 0x1b, // Esc
  130. 0x0d, // Return
  131. 0x09, // Tab
  132. 0x20, // Space
  133. 0x08, // Backspace
  134. '\0', // Up
  135. '\0', // Down
  136. '\0', // Left
  137. '\0', // Right
  138. '\0', // PageUp
  139. '\0', // PageDown
  140. '\0', // Home
  141. '\0', // End
  142. '\0', // Print
  143. 0x3d, // Equals
  144. 0x2d, // Minus
  145. '\0', // F1
  146. '\0', // F2
  147. '\0', // F3
  148. '\0', // F4
  149. '\0', // F5
  150. '\0', // F6
  151. '\0', // F7
  152. '\0', // F8
  153. '\0', // F9
  154. '\0', // F10
  155. '\0', // F11
  156. '\0', // F12
  157. 0x30, // NumPad0
  158. 0x31, // NumPad1
  159. 0x32, // NumPad2
  160. 0x33, // NumPad3
  161. 0x34, // NumPad4
  162. 0x35, // NumPad5
  163. 0x36, // NumPad6
  164. 0x37, // NumPad7
  165. 0x38, // NumPad8
  166. 0x39, // NumPad9
  167. 0x30, // Key0
  168. 0x31, // Key1
  169. 0x32, // Key2
  170. 0x33, // Key3
  171. 0x34, // Key4
  172. 0x35, // Key5
  173. 0x36, // Key6
  174. 0x37, // Key7
  175. 0x38, // Key8
  176. 0x39, // Key9
  177. 0x61, // KeyA
  178. 0x62, // KeyB
  179. 0x63, // KeyC
  180. 0x64, // KeyD
  181. 0x65, // KeyE
  182. 0x66, // KeyF
  183. 0x67, // KeyG
  184. 0x68, // KeyH
  185. 0x69, // KeyI
  186. 0x6a, // KeyJ
  187. 0x6b, // KeyK
  188. 0x6c, // KeyL
  189. 0x6d, // KeyM
  190. 0x6e, // KeyN
  191. 0x6f, // KeyO
  192. 0x70, // KeyP
  193. 0x71, // KeyQ
  194. 0x72, // KeyR
  195. 0x73, // KeyS
  196. 0x74, // KeyT
  197. 0x75, // KeyU
  198. 0x76, // KeyV
  199. 0x77, // KeyW
  200. 0x78, // KeyX
  201. 0x79, // KeyY
  202. 0x7a, // KeyZ
  203. };
  204. char ascii = s_keyToAscii[_key];
  205. if (_shiftModifier)
  206. {
  207. // Big letters.
  208. if(ascii >= 'a' && ascii <= 'z')
  209. {
  210. ascii += 'A' - 'a';
  211. }
  212. // Special cases.
  213. else if('-' == ascii)
  214. {
  215. ascii = '_';
  216. }
  217. else if ('=' == ascii)
  218. {
  219. ascii = '+';
  220. }
  221. }
  222. return ascii;
  223. }
  224. bool processEvents(uint32_t& _width, uint32_t& _height, uint32_t& _debug, uint32_t& _reset, MouseState* _mouse, KeyState* _keyboard)
  225. {
  226. s_debug = _debug;
  227. s_reset = _reset;
  228. bool mouseLock = inputIsMouseLocked();
  229. if (NULL != _keyboard)
  230. {
  231. _keyboard->m_modifiers = 0;
  232. memset(_keyboard->m_keysDown, 0, sizeof(_keyboard->m_keysDown) );
  233. }
  234. const Event* ev;
  235. do
  236. {
  237. struct SE { const Event* m_ev; SE() : m_ev(poll() ) {} ~SE() { if (NULL != m_ev) { release(m_ev); } } } scopeEvent;
  238. ev = scopeEvent.m_ev;
  239. if (NULL != ev)
  240. {
  241. switch (ev->m_type)
  242. {
  243. case Event::Exit:
  244. return true;
  245. case Event::Mouse:
  246. {
  247. const MouseEvent* mouse = static_cast<const MouseEvent*>(ev);
  248. if (mouse->m_move)
  249. {
  250. inputSetMousePos(mouse->m_mx, mouse->m_my, mouse->m_mz);
  251. }
  252. else
  253. {
  254. inputSetMouseButtonState(mouse->m_button, mouse->m_down);
  255. }
  256. if (NULL != _mouse
  257. && !mouseLock)
  258. {
  259. if (mouse->m_move)
  260. {
  261. _mouse->m_mx = mouse->m_mx;
  262. _mouse->m_my = mouse->m_my;
  263. _mouse->m_mz = mouse->m_mz;
  264. }
  265. else
  266. {
  267. _mouse->m_buttons[mouse->m_button] = mouse->m_down;
  268. }
  269. }
  270. }
  271. break;
  272. case Event::Key:
  273. {
  274. const KeyEvent* key = static_cast<const KeyEvent*>(ev);
  275. inputSetKeyState(key->m_key, key->m_modifiers, key->m_down);
  276. if (NULL != _keyboard)
  277. {
  278. _keyboard->m_modifiers |= key->m_modifiers;
  279. if (key->m_down)
  280. {
  281. _keyboard->m_keysDown[key->m_key] = true;
  282. }
  283. }
  284. }
  285. break;
  286. case Event::Size:
  287. {
  288. const SizeEvent* size = static_cast<const SizeEvent*>(ev);
  289. _width = size->m_width;
  290. _height = size->m_height;
  291. _reset = !s_reset; // force reset
  292. }
  293. break;
  294. default:
  295. break;
  296. }
  297. }
  298. inputProcess();
  299. } while (NULL != ev);
  300. if (_reset != s_reset)
  301. {
  302. _reset = s_reset;
  303. bgfx::reset(_width, _height, _reset);
  304. inputSetMouseResolution(_width, _height);
  305. }
  306. _debug = s_debug;
  307. return s_exit;
  308. }
  309. bx::FileReaderI* getFileReader()
  310. {
  311. return s_fileReader;
  312. }
  313. bx::FileWriterI* getFileWriter()
  314. {
  315. return s_fileWriter;
  316. }
  317. } // namespace entry
  318. extern "C" bool entry_process_events(uint32_t* _width, uint32_t* _height, uint32_t* _debug, uint32_t* _reset)
  319. {
  320. return entry::processEvents(*_width, *_height, *_debug, *_reset, NULL);
  321. }