entry_glfw.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. /*
  2. * Copyright 2011-2016 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include "entry_p.h"
  6. #if ENTRY_CONFIG_USE_GLFW
  7. #define GLFW_INCLUDE_NONE
  8. #include <GLFW/glfw3.h>
  9. #if GLFW_VERSION_MINOR < 2
  10. # error "GLFW 3.2 or later is required"
  11. #endif // GLFW_VERSION_MINOR < 2
  12. #if BX_PLATFORM_LINUX || BX_PLATFORM_BSD
  13. # define GLFW_EXPOSE_NATIVE_X11
  14. # define GLFW_EXPOSE_NATIVE_GLX
  15. #elif BX_PLATFORM_OSX
  16. # define GLFW_EXPOSE_NATIVE_COCOA
  17. # define GLFW_EXPOSE_NATIVE_NSGL
  18. #elif BX_PLATFORM_WINDOWS
  19. # define GLFW_EXPOSE_NATIVE_WIN32
  20. # define GLFW_EXPOSE_NATIVE_WGL
  21. #endif //
  22. #include <GLFW/glfw3native.h>
  23. #include <bgfx/platform.h>
  24. #include <bx/thread.h>
  25. #include <bx/handlealloc.h>
  26. #include <tinystl/string.h>
  27. #include "dbg.h"
  28. namespace entry
  29. {
  30. inline void glfwSetWindow(GLFWwindow* _window)
  31. {
  32. bgfx::PlatformData pd;
  33. # if BX_PLATFORM_LINUX || BX_PLATFORM_BSD
  34. pd.ndt = glfwGetX11Display();
  35. pd.nwh = (void*)(uintptr_t)glfwGetX11Window(_window);
  36. # elif BX_PLATFORM_OSX
  37. pd.ndt = NULL;
  38. pd.nwh = glfwGetCocoaWindow(_window);
  39. # elif BX_PLATFORM_WINDOWS
  40. pd.ndt = NULL;
  41. pd.nwh = glfwGetWin32Window(_window);
  42. pd.context = NULL;
  43. # endif // BX_PLATFORM_WINDOWS
  44. pd.backBuffer = NULL;
  45. pd.backBufferDS = NULL;
  46. bgfx::setPlatformData(pd);
  47. }
  48. static uint8_t translateKeyModifiers(int _glfw)
  49. {
  50. uint8_t modifiers = 0;
  51. if (_glfw & GLFW_MOD_ALT)
  52. {
  53. modifiers |= Modifier::LeftAlt;
  54. }
  55. if (_glfw & GLFW_MOD_CONTROL)
  56. {
  57. modifiers |= Modifier::LeftCtrl;
  58. }
  59. if (_glfw & GLFW_MOD_SUPER)
  60. {
  61. modifiers |= Modifier::LeftMeta;
  62. }
  63. if (_glfw & GLFW_MOD_SHIFT)
  64. {
  65. modifiers |= Modifier::LeftShift;
  66. }
  67. return modifiers;
  68. }
  69. static Key::Enum s_translateKey[GLFW_KEY_LAST + 1];
  70. static Key::Enum translateKey(int _key)
  71. {
  72. return s_translateKey[_key];
  73. }
  74. static MouseButton::Enum translateMouseButton(int _button)
  75. {
  76. if (_button == GLFW_MOUSE_BUTTON_LEFT)
  77. {
  78. return MouseButton::Left;
  79. }
  80. else if (_button == GLFW_MOUSE_BUTTON_RIGHT)
  81. {
  82. return MouseButton::Right;
  83. }
  84. return MouseButton::Middle;
  85. }
  86. static GamepadAxis::Enum translateGamepadAxis(int _axis)
  87. {
  88. // HACK: Map XInput 360 controller until GLFW gamepad API
  89. static GamepadAxis::Enum axes[] =
  90. {
  91. GamepadAxis::LeftX,
  92. GamepadAxis::LeftY,
  93. GamepadAxis::RightX,
  94. GamepadAxis::RightY,
  95. GamepadAxis::LeftZ,
  96. GamepadAxis::RightZ,
  97. };
  98. return axes[_axis];
  99. }
  100. static Key::Enum translateGamepadButton(int _button)
  101. {
  102. // HACK: Map XInput 360 controller until GLFW gamepad API
  103. static Key::Enum buttons[] =
  104. {
  105. Key::GamepadA,
  106. Key::GamepadB,
  107. Key::GamepadX,
  108. Key::GamepadY,
  109. Key::GamepadShoulderL,
  110. Key::GamepadShoulderR,
  111. Key::GamepadBack,
  112. Key::GamepadStart,
  113. Key::GamepadThumbL,
  114. Key::GamepadThumbR,
  115. Key::GamepadUp,
  116. Key::GamepadRight,
  117. Key::GamepadDown,
  118. Key::GamepadLeft,
  119. Key::GamepadGuide,
  120. };
  121. return buttons[_button];
  122. }
  123. struct GamepadGLFW
  124. {
  125. GamepadGLFW()
  126. : m_connected(false)
  127. {
  128. memset(m_axes, 0, sizeof(m_axes));
  129. memset(m_buttons, 0, sizeof(m_buttons));
  130. }
  131. void update(EventQueue& _eventQueue)
  132. {
  133. int numButtons, numAxes;
  134. const unsigned char* buttons = glfwGetJoystickButtons(m_handle.idx, &numButtons);
  135. const float* axes = glfwGetJoystickAxes(m_handle.idx, &numAxes);
  136. if (NULL == buttons || NULL == axes)
  137. {
  138. return;
  139. }
  140. if (numAxes > GamepadAxis::Count)
  141. {
  142. numAxes = GamepadAxis::Count;
  143. }
  144. if (numButtons > Key::Count - Key::GamepadA)
  145. {
  146. numButtons = Key::Count - Key::GamepadA;
  147. }
  148. WindowHandle defaultWindow = { 0 };
  149. for (int ii = 0; ii < numAxes; ++ii)
  150. {
  151. GamepadAxis::Enum axis = translateGamepadAxis(ii);
  152. int32_t value = (int32_t) (axes[ii] * 32768.f);
  153. if (GamepadAxis::LeftY == axis || GamepadAxis::RightY == axis)
  154. {
  155. value = -value;
  156. }
  157. if (m_axes[ii] != value)
  158. {
  159. m_axes[ii] = value;
  160. _eventQueue.postAxisEvent(defaultWindow
  161. , m_handle
  162. , axis
  163. , value);
  164. }
  165. }
  166. for (int ii = 0; ii < numButtons; ++ii)
  167. {
  168. Key::Enum key = translateGamepadButton(ii);
  169. if (m_buttons[ii] != buttons[ii])
  170. {
  171. m_buttons[ii] = buttons[ii];
  172. _eventQueue.postKeyEvent(defaultWindow
  173. , key
  174. , 0
  175. , buttons[ii] != 0);
  176. }
  177. }
  178. }
  179. bool m_connected;
  180. GamepadHandle m_handle;
  181. int32_t m_axes[GamepadAxis::Count];
  182. uint8_t m_buttons[Key::Count - Key::GamepadA];
  183. };
  184. struct MainThreadEntry
  185. {
  186. int m_argc;
  187. char** m_argv;
  188. static int32_t threadFunc(void* _userData);
  189. };
  190. static void* glfwNativeWindowHandle(GLFWwindow* _window)
  191. {
  192. # if BX_PLATFORM_LINUX || BX_PLATFORM_BSD
  193. return (void*)(uintptr_t)glfwGetX11Window(_window);
  194. # elif BX_PLATFORM_OSX
  195. return glfwGetCocoaWindow(_window);
  196. # elif BX_PLATFORM_WINDOWS
  197. return glfwGetWin32Window(_window);
  198. # endif // BX_PLATFORM_
  199. }
  200. enum MsgType
  201. {
  202. GLFW_WINDOW_CREATE,
  203. GLFW_WINDOW_DESTROY,
  204. GLFW_WINDOW_SET_TITLE,
  205. GLFW_WINDOW_SET_POS,
  206. GLFW_WINDOW_SET_SIZE,
  207. GLFW_WINDOW_TOGGLE_FRAME,
  208. GLFW_WINDOW_TOGGLE_FULL_SCREEN,
  209. GLFW_WINDOW_MOUSE_LOCK,
  210. };
  211. struct Msg
  212. {
  213. Msg(MsgType _type)
  214. : m_type(_type)
  215. , m_x(0)
  216. , m_y(0)
  217. , m_width(0)
  218. , m_height(0)
  219. , m_value(false)
  220. {
  221. }
  222. MsgType m_type;
  223. int32_t m_x;
  224. int32_t m_y;
  225. uint32_t m_width;
  226. uint32_t m_height;
  227. uint32_t m_flags;
  228. bool m_value;
  229. tinystl::string m_title;
  230. WindowHandle m_handle;
  231. };
  232. static void errorCb(int _error, const char* _description)
  233. {
  234. DBG("GLFW error %d: %s", _error, _description);
  235. }
  236. static void joystickCb(int _jid, int _action);
  237. // Based on cutef8 by Jeff Bezanson (Public Domain)
  238. static uint8_t encodeUTF8(uint8_t _chars[4], unsigned int _scancode)
  239. {
  240. uint8_t length = 0;
  241. if (_scancode < 0x80)
  242. {
  243. _chars[length++] = (char) _scancode;
  244. }
  245. else if (_scancode < 0x800)
  246. {
  247. _chars[length++] = (_scancode >> 6) | 0xc0;
  248. _chars[length++] = (_scancode & 0x3f) | 0x80;
  249. }
  250. else if (_scancode < 0x10000)
  251. {
  252. _chars[length++] = (_scancode >> 12) | 0xe0;
  253. _chars[length++] = ((_scancode >> 6) & 0x3f) | 0x80;
  254. _chars[length++] = (_scancode & 0x3f) | 0x80;
  255. }
  256. else if (_scancode < 0x110000)
  257. {
  258. _chars[length++] = (_scancode >> 18) | 0xf0;
  259. _chars[length++] = ((_scancode >> 12) & 0x3f) | 0x80;
  260. _chars[length++] = ((_scancode >> 6) & 0x3f) | 0x80;
  261. _chars[length++] = (_scancode & 0x3f) | 0x80;
  262. }
  263. return length;
  264. }
  265. struct Context
  266. {
  267. Context()
  268. : m_scrollPos(0.0)
  269. {
  270. memset(s_translateKey, 0, sizeof(s_translateKey));
  271. s_translateKey[GLFW_KEY_ESCAPE] = Key::Esc;
  272. s_translateKey[GLFW_KEY_ENTER] = Key::Return;
  273. s_translateKey[GLFW_KEY_TAB] = Key::Tab;
  274. s_translateKey[GLFW_KEY_BACKSPACE] = Key::Backspace;
  275. s_translateKey[GLFW_KEY_SPACE] = Key::Space;
  276. s_translateKey[GLFW_KEY_UP] = Key::Up;
  277. s_translateKey[GLFW_KEY_DOWN] = Key::Down;
  278. s_translateKey[GLFW_KEY_LEFT] = Key::Left;
  279. s_translateKey[GLFW_KEY_RIGHT] = Key::Right;
  280. s_translateKey[GLFW_KEY_PAGE_UP] = Key::PageUp;
  281. s_translateKey[GLFW_KEY_PAGE_DOWN] = Key::PageDown;
  282. s_translateKey[GLFW_KEY_HOME] = Key::Home;
  283. s_translateKey[GLFW_KEY_END] = Key::End;
  284. s_translateKey[GLFW_KEY_PRINT_SCREEN] = Key::Print;
  285. s_translateKey[GLFW_KEY_KP_ADD] = Key::Plus;
  286. s_translateKey[GLFW_KEY_KP_SUBTRACT] = Key::Minus;
  287. s_translateKey[GLFW_KEY_F1] = Key::F1;
  288. s_translateKey[GLFW_KEY_F2] = Key::F2;
  289. s_translateKey[GLFW_KEY_F3] = Key::F3;
  290. s_translateKey[GLFW_KEY_F4] = Key::F4;
  291. s_translateKey[GLFW_KEY_F5] = Key::F5;
  292. s_translateKey[GLFW_KEY_F6] = Key::F6;
  293. s_translateKey[GLFW_KEY_F7] = Key::F7;
  294. s_translateKey[GLFW_KEY_F8] = Key::F8;
  295. s_translateKey[GLFW_KEY_F9] = Key::F9;
  296. s_translateKey[GLFW_KEY_F10] = Key::F10;
  297. s_translateKey[GLFW_KEY_F11] = Key::F11;
  298. s_translateKey[GLFW_KEY_F12] = Key::F12;
  299. s_translateKey[GLFW_KEY_KP_0] = Key::NumPad0;
  300. s_translateKey[GLFW_KEY_KP_1] = Key::NumPad1;
  301. s_translateKey[GLFW_KEY_KP_2] = Key::NumPad2;
  302. s_translateKey[GLFW_KEY_KP_3] = Key::NumPad3;
  303. s_translateKey[GLFW_KEY_KP_4] = Key::NumPad4;
  304. s_translateKey[GLFW_KEY_KP_5] = Key::NumPad5;
  305. s_translateKey[GLFW_KEY_KP_6] = Key::NumPad6;
  306. s_translateKey[GLFW_KEY_KP_7] = Key::NumPad7;
  307. s_translateKey[GLFW_KEY_KP_8] = Key::NumPad8;
  308. s_translateKey[GLFW_KEY_KP_9] = Key::NumPad9;
  309. s_translateKey[GLFW_KEY_0] = Key::Key0;
  310. s_translateKey[GLFW_KEY_1] = Key::Key1;
  311. s_translateKey[GLFW_KEY_2] = Key::Key2;
  312. s_translateKey[GLFW_KEY_3] = Key::Key3;
  313. s_translateKey[GLFW_KEY_4] = Key::Key4;
  314. s_translateKey[GLFW_KEY_5] = Key::Key5;
  315. s_translateKey[GLFW_KEY_6] = Key::Key6;
  316. s_translateKey[GLFW_KEY_7] = Key::Key7;
  317. s_translateKey[GLFW_KEY_8] = Key::Key8;
  318. s_translateKey[GLFW_KEY_9] = Key::Key9;
  319. s_translateKey[GLFW_KEY_A] = Key::KeyA;
  320. s_translateKey[GLFW_KEY_B] = Key::KeyB;
  321. s_translateKey[GLFW_KEY_C] = Key::KeyC;
  322. s_translateKey[GLFW_KEY_D] = Key::KeyD;
  323. s_translateKey[GLFW_KEY_E] = Key::KeyE;
  324. s_translateKey[GLFW_KEY_F] = Key::KeyF;
  325. s_translateKey[GLFW_KEY_G] = Key::KeyG;
  326. s_translateKey[GLFW_KEY_H] = Key::KeyH;
  327. s_translateKey[GLFW_KEY_I] = Key::KeyI;
  328. s_translateKey[GLFW_KEY_J] = Key::KeyJ;
  329. s_translateKey[GLFW_KEY_K] = Key::KeyK;
  330. s_translateKey[GLFW_KEY_L] = Key::KeyL;
  331. s_translateKey[GLFW_KEY_M] = Key::KeyM;
  332. s_translateKey[GLFW_KEY_N] = Key::KeyN;
  333. s_translateKey[GLFW_KEY_O] = Key::KeyO;
  334. s_translateKey[GLFW_KEY_P] = Key::KeyP;
  335. s_translateKey[GLFW_KEY_Q] = Key::KeyQ;
  336. s_translateKey[GLFW_KEY_R] = Key::KeyR;
  337. s_translateKey[GLFW_KEY_S] = Key::KeyS;
  338. s_translateKey[GLFW_KEY_T] = Key::KeyT;
  339. s_translateKey[GLFW_KEY_U] = Key::KeyU;
  340. s_translateKey[GLFW_KEY_V] = Key::KeyV;
  341. s_translateKey[GLFW_KEY_W] = Key::KeyW;
  342. s_translateKey[GLFW_KEY_X] = Key::KeyX;
  343. s_translateKey[GLFW_KEY_Y] = Key::KeyY;
  344. s_translateKey[GLFW_KEY_Z] = Key::KeyZ;
  345. }
  346. int run(int _argc, char** _argv)
  347. {
  348. m_mte.m_argc = _argc;
  349. m_mte.m_argv = _argv;
  350. glfwSetErrorCallback(errorCb);
  351. glfwSetJoystickCallback(joystickCb);
  352. if (!glfwInit() )
  353. {
  354. DBG("glfwInit failed!");
  355. return EXIT_FAILURE;
  356. }
  357. glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
  358. WindowHandle handle = { m_windowAlloc.alloc() };
  359. m_windows[0] = glfwCreateWindow(ENTRY_DEFAULT_WIDTH
  360. , ENTRY_DEFAULT_HEIGHT
  361. , "bgfx"
  362. , NULL
  363. , NULL
  364. );
  365. if (!m_windows[0])
  366. {
  367. DBG("glfwCreateWindow failed!");
  368. glfwTerminate();
  369. return EXIT_FAILURE;
  370. }
  371. glfwSetKeyCallback(m_windows[0], keyCb);
  372. glfwSetCharCallback(m_windows[0], charCb);
  373. glfwSetScrollCallback(m_windows[0], scrollCb);
  374. glfwSetCursorPosCallback(m_windows[0], cursorPosCb);
  375. glfwSetMouseButtonCallback(m_windows[0], mouseButtonCb);
  376. glfwSetWindowSizeCallback(m_windows[0], windowSizeCb);
  377. glfwSetWindow(m_windows[0]);
  378. m_eventQueue.postSizeEvent(handle, ENTRY_DEFAULT_WIDTH, ENTRY_DEFAULT_HEIGHT);
  379. for (uint32_t ii = 0; ii < ENTRY_CONFIG_MAX_GAMEPADS; ++ii)
  380. {
  381. m_gamepad[ii].m_handle.idx = ii;
  382. if (glfwJoystickPresent(ii))
  383. {
  384. m_gamepad[ii].m_connected = true;
  385. m_eventQueue.postGamepadEvent(handle
  386. , m_gamepad[ii].m_handle
  387. , true);
  388. }
  389. }
  390. m_thread.init(MainThreadEntry::threadFunc, &m_mte);
  391. while (NULL != m_windows[0]
  392. && !glfwWindowShouldClose(m_windows[0]))
  393. {
  394. glfwWaitEvents();
  395. for (uint32_t ii = 0; ii < ENTRY_CONFIG_MAX_GAMEPADS; ++ii)
  396. {
  397. if (m_gamepad[ii].m_connected)
  398. {
  399. m_gamepad[ii].update(m_eventQueue);
  400. }
  401. }
  402. while (Msg* msg = m_msgs.pop())
  403. {
  404. switch (msg->m_type)
  405. {
  406. case GLFW_WINDOW_CREATE:
  407. {
  408. GLFWwindow* window = glfwCreateWindow(msg->m_width
  409. , msg->m_height
  410. , msg->m_title.c_str()
  411. , NULL
  412. , NULL);
  413. if (!window)
  414. {
  415. break;
  416. }
  417. glfwSetWindowPos(window, msg->m_x, msg->m_y);
  418. if (msg->m_flags & ENTRY_WINDOW_FLAG_ASPECT_RATIO)
  419. {
  420. glfwSetWindowAspectRatio(window, msg->m_width, msg->m_height);
  421. }
  422. glfwSetKeyCallback(window, keyCb);
  423. glfwSetCharCallback(window, charCb);
  424. glfwSetScrollCallback(window, scrollCb);
  425. glfwSetCursorPosCallback(window, cursorPosCb);
  426. glfwSetMouseButtonCallback(window, mouseButtonCb);
  427. glfwSetWindowSizeCallback(window, windowSizeCb);
  428. m_windows[msg->m_handle.idx] = window;
  429. m_eventQueue.postSizeEvent(msg->m_handle, msg->m_width, msg->m_height);
  430. m_eventQueue.postWindowEvent(msg->m_handle, glfwNativeWindowHandle(window));
  431. }
  432. break;
  433. case GLFW_WINDOW_DESTROY:
  434. {
  435. if (isValid(msg->m_handle) )
  436. {
  437. GLFWwindow* window = m_windows[msg->m_handle.idx];
  438. m_eventQueue.postWindowEvent(msg->m_handle);
  439. glfwDestroyWindow(window);
  440. m_windows[msg->m_handle.idx] = NULL;
  441. }
  442. }
  443. break;
  444. case GLFW_WINDOW_SET_TITLE:
  445. {
  446. GLFWwindow* window = m_windows[msg->m_handle.idx];
  447. glfwSetWindowTitle(window, msg->m_title.c_str());
  448. }
  449. break;
  450. case GLFW_WINDOW_SET_POS:
  451. {
  452. GLFWwindow* window = m_windows[msg->m_handle.idx];
  453. glfwSetWindowPos(window, msg->m_x, msg->m_y);
  454. }
  455. break;
  456. case GLFW_WINDOW_SET_SIZE:
  457. {
  458. GLFWwindow* window = m_windows[msg->m_handle.idx];
  459. glfwSetWindowSize(window, msg->m_width, msg->m_height);
  460. }
  461. break;
  462. case GLFW_WINDOW_TOGGLE_FRAME:
  463. {
  464. // Wait for glfwSetWindowDecorated to exist
  465. }
  466. break;
  467. case GLFW_WINDOW_TOGGLE_FULL_SCREEN:
  468. {
  469. GLFWwindow* window = m_windows[msg->m_handle.idx];
  470. if (glfwGetWindowMonitor(window))
  471. {
  472. int width, height;
  473. glfwGetWindowSize(window, &width, &height);
  474. glfwSetWindowMonitor(window, NULL, 0, 0, width, height, 0);
  475. }
  476. else
  477. {
  478. GLFWmonitor* monitor = glfwGetPrimaryMonitor();
  479. if (NULL != monitor)
  480. {
  481. const GLFWvidmode* mode = glfwGetVideoMode(monitor);
  482. glfwSetWindowMonitor(window
  483. , monitor
  484. , 0
  485. , 0
  486. , mode->width
  487. , mode->height
  488. , mode->refreshRate
  489. );
  490. }
  491. }
  492. }
  493. break;
  494. case GLFW_WINDOW_MOUSE_LOCK:
  495. {
  496. GLFWwindow* window = m_windows[msg->m_handle.idx];
  497. if (msg->m_value)
  498. {
  499. glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
  500. }
  501. else
  502. {
  503. glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
  504. }
  505. }
  506. break;
  507. }
  508. delete msg;
  509. }
  510. }
  511. m_eventQueue.postExitEvent();
  512. m_thread.shutdown();
  513. glfwDestroyWindow(m_windows[0]);
  514. glfwTerminate();
  515. return m_thread.getExitCode();
  516. }
  517. WindowHandle findHandle(GLFWwindow* _window)
  518. {
  519. bx::LwMutexScope scope(m_lock);
  520. for (uint32_t ii = 0, num = m_windowAlloc.getNumHandles(); ii < num; ++ii)
  521. {
  522. uint16_t idx = m_windowAlloc.getHandleAt(ii);
  523. if (_window == m_windows[idx])
  524. {
  525. WindowHandle handle = { idx };
  526. return handle;
  527. }
  528. }
  529. WindowHandle invalid = { UINT16_MAX };
  530. return invalid;
  531. }
  532. static void keyCb(GLFWwindow* _window, int _key, int _scancode, int _action, int _mods);
  533. static void charCb(GLFWwindow* _window, unsigned int _scancode);
  534. static void scrollCb(GLFWwindow* _window, double _dx, double _dy);
  535. static void cursorPosCb(GLFWwindow* _window, double _mx, double _my);
  536. static void mouseButtonCb(GLFWwindow* _window, int _button, int _action, int _mods);
  537. static void windowSizeCb(GLFWwindow* _window, int _width, int _height);
  538. MainThreadEntry m_mte;
  539. bx::Thread m_thread;
  540. EventQueue m_eventQueue;
  541. bx::LwMutex m_lock;
  542. GLFWwindow* m_windows[ENTRY_CONFIG_MAX_WINDOWS];
  543. bx::HandleAllocT<ENTRY_CONFIG_MAX_WINDOWS> m_windowAlloc;
  544. GamepadGLFW m_gamepad[ENTRY_CONFIG_MAX_GAMEPADS];
  545. bx::SpScUnboundedQueueLf<Msg> m_msgs;
  546. double m_scrollPos;
  547. };
  548. Context s_ctx;
  549. void Context::keyCb(GLFWwindow* _window, int _key, int _scancode, int _action, int _mods)
  550. {
  551. BX_UNUSED(_scancode);
  552. if (_key == GLFW_KEY_UNKNOWN)
  553. {
  554. return;
  555. }
  556. WindowHandle handle = s_ctx.findHandle(_window);
  557. int mods = translateKeyModifiers(_mods);
  558. Key::Enum key = translateKey(_key);
  559. bool down = (_action == GLFW_PRESS || _action == GLFW_REPEAT);
  560. s_ctx.m_eventQueue.postKeyEvent(handle, key, mods, down);
  561. }
  562. void Context::charCb(GLFWwindow* _window, unsigned int _scancode)
  563. {
  564. WindowHandle handle = s_ctx.findHandle(_window);
  565. uint8_t chars[4];
  566. uint8_t length = encodeUTF8(chars, _scancode);
  567. if (!length)
  568. {
  569. return;
  570. }
  571. s_ctx.m_eventQueue.postCharEvent(handle, length, chars);
  572. }
  573. void Context::scrollCb(GLFWwindow* _window, double _dx, double _dy)
  574. {
  575. BX_UNUSED(_dx);
  576. WindowHandle handle = s_ctx.findHandle(_window);
  577. double mx, my;
  578. glfwGetCursorPos(_window, &mx, &my);
  579. s_ctx.m_scrollPos += _dy;
  580. s_ctx.m_eventQueue.postMouseEvent(handle
  581. , (int32_t) mx
  582. , (int32_t) my
  583. , (int32_t) s_ctx.m_scrollPos
  584. );
  585. }
  586. void Context::cursorPosCb(GLFWwindow* _window, double _mx, double _my)
  587. {
  588. WindowHandle handle = s_ctx.findHandle(_window);
  589. s_ctx.m_eventQueue.postMouseEvent(handle
  590. , (int32_t) _mx
  591. , (int32_t) _my
  592. , (int32_t) s_ctx.m_scrollPos
  593. );
  594. }
  595. void Context::mouseButtonCb(GLFWwindow* _window, int _button, int _action, int _mods)
  596. {
  597. BX_UNUSED(_mods);
  598. WindowHandle handle = s_ctx.findHandle(_window);
  599. bool down = _action == GLFW_PRESS;
  600. double mx, my;
  601. glfwGetCursorPos(_window, &mx, &my);
  602. s_ctx.m_eventQueue.postMouseEvent(handle
  603. , (int32_t) mx
  604. , (int32_t) my
  605. , (int32_t) s_ctx.m_scrollPos
  606. , translateMouseButton(_button)
  607. , down
  608. );
  609. }
  610. void Context::windowSizeCb(GLFWwindow* _window, int _width, int _height)
  611. {
  612. WindowHandle handle = s_ctx.findHandle(_window);
  613. s_ctx.m_eventQueue.postSizeEvent(handle, _width, _height);
  614. }
  615. static void joystickCb(int _jid, int _action)
  616. {
  617. if (_jid >= ENTRY_CONFIG_MAX_GAMEPADS)
  618. {
  619. return;
  620. }
  621. WindowHandle defaultWindow = { 0 };
  622. GamepadHandle handle = { (uint16_t) _jid };
  623. if (_action == GLFW_CONNECTED)
  624. {
  625. s_ctx.m_gamepad[_jid].m_connected = true;
  626. s_ctx.m_eventQueue.postGamepadEvent(defaultWindow, handle, true);
  627. }
  628. else if (_action == GLFW_DISCONNECTED)
  629. {
  630. s_ctx.m_gamepad[_jid].m_connected = false;
  631. s_ctx.m_eventQueue.postGamepadEvent(defaultWindow, handle, false);
  632. }
  633. }
  634. const Event* poll()
  635. {
  636. glfwPostEmptyEvent();
  637. return s_ctx.m_eventQueue.poll();
  638. }
  639. const Event* poll(WindowHandle _handle)
  640. {
  641. glfwPostEmptyEvent();
  642. return s_ctx.m_eventQueue.poll(_handle);
  643. }
  644. void release(const Event* _event)
  645. {
  646. s_ctx.m_eventQueue.release(_event);
  647. }
  648. WindowHandle createWindow(int32_t _x, int32_t _y, uint32_t _width, uint32_t _height, uint32_t _flags, const char* _title)
  649. {
  650. Msg* msg = new Msg(GLFW_WINDOW_CREATE);
  651. msg->m_x = _x;
  652. msg->m_y = _y;
  653. msg->m_width = _width;
  654. msg->m_height = _height;
  655. msg->m_flags = _flags;
  656. msg->m_title = _title;
  657. msg->m_handle.idx = s_ctx.m_windowAlloc.alloc();
  658. s_ctx.m_msgs.push(msg);
  659. glfwPostEmptyEvent();
  660. return msg->m_handle;
  661. }
  662. void destroyWindow(WindowHandle _handle)
  663. {
  664. Msg* msg = new Msg(GLFW_WINDOW_DESTROY);
  665. msg->m_handle = _handle;
  666. s_ctx.m_msgs.push(msg);
  667. glfwPostEmptyEvent();
  668. }
  669. void setWindowPos(WindowHandle _handle, int32_t _x, int32_t _y)
  670. {
  671. Msg* msg = new Msg(GLFW_WINDOW_SET_POS);
  672. msg->m_x = _x;
  673. msg->m_y = _y;
  674. msg->m_handle = _handle;
  675. s_ctx.m_msgs.push(msg);
  676. glfwPostEmptyEvent();
  677. }
  678. void setWindowSize(WindowHandle _handle, uint32_t _width, uint32_t _height)
  679. {
  680. Msg* msg = new Msg(GLFW_WINDOW_SET_SIZE);
  681. msg->m_width = _width;
  682. msg->m_height = _height;
  683. msg->m_handle = _handle;
  684. s_ctx.m_msgs.push(msg);
  685. glfwPostEmptyEvent();
  686. }
  687. void setWindowTitle(WindowHandle _handle, const char* _title)
  688. {
  689. Msg* msg = new Msg(GLFW_WINDOW_SET_TITLE);
  690. msg->m_title = _title;
  691. msg->m_handle = _handle;
  692. s_ctx.m_msgs.push(msg);
  693. glfwPostEmptyEvent();
  694. }
  695. void toggleWindowFrame(WindowHandle _handle)
  696. {
  697. Msg* msg = new Msg(GLFW_WINDOW_TOGGLE_FRAME);
  698. msg->m_handle = _handle;
  699. s_ctx.m_msgs.push(msg);
  700. glfwPostEmptyEvent();
  701. }
  702. void toggleFullscreen(WindowHandle _handle)
  703. {
  704. Msg* msg = new Msg(GLFW_WINDOW_TOGGLE_FULL_SCREEN);
  705. msg->m_handle = _handle;
  706. s_ctx.m_msgs.push(msg);
  707. glfwPostEmptyEvent();
  708. }
  709. void setMouseLock(WindowHandle _handle, bool _lock)
  710. {
  711. Msg* msg = new Msg(GLFW_WINDOW_MOUSE_LOCK);
  712. msg->m_value = _lock;
  713. msg->m_handle = _handle;
  714. s_ctx.m_msgs.push(msg);
  715. glfwPostEmptyEvent();
  716. }
  717. int32_t MainThreadEntry::threadFunc(void* _userData)
  718. {
  719. MainThreadEntry* self = (MainThreadEntry*)_userData;
  720. int32_t result = main(self->m_argc, self->m_argv);
  721. // Destroy main window on exit...
  722. Msg* msg = new Msg(GLFW_WINDOW_DESTROY);
  723. msg->m_handle.idx = 0;
  724. s_ctx.m_msgs.push(msg);
  725. glfwPostEmptyEvent();
  726. return result;
  727. }
  728. }
  729. int main(int _argc, char** _argv)
  730. {
  731. using namespace entry;
  732. return s_ctx.run(_argc, _argv);
  733. }
  734. #endif // ENTRY_CONFIG_USE_GLFW