entry_windows.cpp 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  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_NATIVE && BX_PLATFORM_WINDOWS
  7. #include <bgfx/bgfxplatform.h>
  8. #include <bx/uint32_t.h>
  9. #include <bx/thread.h>
  10. #include <bx/mutex.h>
  11. #include <bx/handlealloc.h>
  12. #include <bx/timer.h>
  13. #include <tinystl/allocator.h>
  14. #include <tinystl/string.h>
  15. #include <windowsx.h>
  16. #include <xinput.h>
  17. #ifndef XINPUT_GAMEPAD_GUIDE
  18. # define XINPUT_GAMEPAD_GUIDE 0x400
  19. #endif // XINPUT_GAMEPAD_GUIDE
  20. #ifndef XINPUT_DLL_A
  21. # define XINPUT_DLL_A "xinput.dll"
  22. #endif // XINPUT_DLL_A
  23. namespace entry
  24. {
  25. ///
  26. inline void winSetHwnd(::HWND _window)
  27. {
  28. bgfx::PlatformData pd;
  29. pd.ndt = NULL;
  30. pd.nwh = _window;
  31. pd.context = NULL;
  32. pd.backBuffer = NULL;
  33. pd.backBufferDS = NULL;
  34. bgfx::setPlatformData(pd);
  35. }
  36. typedef DWORD (WINAPI* PFN_XINPUT_GET_STATE)(DWORD dwUserIndex, XINPUT_STATE* pState);
  37. typedef void (WINAPI* PFN_XINPUT_ENABLE)(BOOL enable); // 1.4+
  38. PFN_XINPUT_GET_STATE XInputGetState;
  39. PFN_XINPUT_ENABLE XInputEnable;
  40. struct XInputRemap
  41. {
  42. uint16_t m_bit;
  43. Key::Enum m_key;
  44. };
  45. static XInputRemap s_xinputRemap[] =
  46. {
  47. { XINPUT_GAMEPAD_DPAD_UP, Key::GamepadUp },
  48. { XINPUT_GAMEPAD_DPAD_DOWN, Key::GamepadDown },
  49. { XINPUT_GAMEPAD_DPAD_LEFT, Key::GamepadLeft },
  50. { XINPUT_GAMEPAD_DPAD_RIGHT, Key::GamepadRight },
  51. { XINPUT_GAMEPAD_START, Key::GamepadStart },
  52. { XINPUT_GAMEPAD_BACK, Key::GamepadBack },
  53. { XINPUT_GAMEPAD_LEFT_THUMB, Key::GamepadThumbL },
  54. { XINPUT_GAMEPAD_RIGHT_THUMB, Key::GamepadThumbR },
  55. { XINPUT_GAMEPAD_LEFT_SHOULDER, Key::GamepadShoulderL },
  56. { XINPUT_GAMEPAD_RIGHT_SHOULDER, Key::GamepadShoulderR },
  57. { XINPUT_GAMEPAD_GUIDE, Key::GamepadGuide },
  58. { XINPUT_GAMEPAD_A, Key::GamepadA },
  59. { XINPUT_GAMEPAD_B, Key::GamepadB },
  60. { XINPUT_GAMEPAD_X, Key::GamepadX },
  61. { XINPUT_GAMEPAD_Y, Key::GamepadY },
  62. };
  63. struct XInput
  64. {
  65. XInput()
  66. : m_xinputdll(NULL)
  67. {
  68. memset(m_connected, 0, sizeof(m_connected) );
  69. memset(m_state, 0, sizeof(m_state) );
  70. m_deadzone[GamepadAxis::LeftX ] =
  71. m_deadzone[GamepadAxis::LeftY ] = XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE;
  72. m_deadzone[GamepadAxis::RightX] =
  73. m_deadzone[GamepadAxis::RightY] = XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE;
  74. m_deadzone[GamepadAxis::LeftZ ] =
  75. m_deadzone[GamepadAxis::RightZ] = XINPUT_GAMEPAD_TRIGGER_THRESHOLD;
  76. memset(m_flip, 1, sizeof(m_flip) );
  77. m_flip[GamepadAxis::LeftY ] =
  78. m_flip[GamepadAxis::RightY] = -1;
  79. }
  80. void init()
  81. {
  82. m_xinputdll = bx::dlopen(XINPUT_DLL_A);
  83. if (NULL != m_xinputdll)
  84. {
  85. XInputGetState = (PFN_XINPUT_GET_STATE)bx::dlsym(m_xinputdll, "XInputGetState");
  86. // XInputEnable = (PFN_XINPUT_ENABLE )bx::dlsym(m_xinputdll, "XInputEnable" );
  87. if (NULL == XInputGetState)
  88. {
  89. shutdown();
  90. }
  91. }
  92. }
  93. void shutdown()
  94. {
  95. if (NULL != m_xinputdll)
  96. {
  97. bx::dlclose(m_xinputdll);
  98. m_xinputdll = NULL;
  99. }
  100. }
  101. bool filter(GamepadAxis::Enum _axis, int32_t _old, int32_t* _value)
  102. {
  103. const int32_t deadzone = m_deadzone[_axis];
  104. int32_t value = *_value;
  105. value = value > deadzone || value < -deadzone ? value : 0;
  106. *_value = value * m_flip[_axis];
  107. return _old != value;
  108. }
  109. void update(EventQueue& _eventQueue)
  110. {
  111. int64_t now = bx::getHPCounter();
  112. static int64_t next = now;
  113. if (now < next)
  114. {
  115. return;
  116. }
  117. const int64_t timerFreq = bx::getHPFrequency();
  118. next = now + timerFreq/60;
  119. if (NULL == m_xinputdll)
  120. {
  121. return;
  122. }
  123. WindowHandle defaultWindow = { 0 };
  124. GamepadHandle handle = { 0 };
  125. for (uint32_t ii = 0; ii < BX_COUNTOF(m_state); ++ii)
  126. {
  127. XINPUT_STATE state;
  128. DWORD result = XInputGetState(ii, &state);
  129. bool connected = ERROR_SUCCESS == result;
  130. if (connected != m_connected[ii])
  131. {
  132. _eventQueue.postGamepadEvent(defaultWindow, handle, connected);
  133. }
  134. m_connected[ii] = connected;
  135. if (connected
  136. && m_state[ii].dwPacketNumber != state.dwPacketNumber)
  137. {
  138. XINPUT_GAMEPAD& gamepad = m_state[ii].Gamepad;
  139. const uint16_t changed = gamepad.wButtons ^ state.Gamepad.wButtons;
  140. const uint16_t current = gamepad.wButtons;
  141. if (0 != changed)
  142. {
  143. for (uint32_t jj = 0; jj < BX_COUNTOF(s_xinputRemap); ++jj)
  144. {
  145. uint16_t bit = s_xinputRemap[jj].m_bit;
  146. if (bit & changed)
  147. {
  148. _eventQueue.postKeyEvent(defaultWindow, s_xinputRemap[jj].m_key, 0, 0 == (current & bit) );
  149. }
  150. }
  151. gamepad.wButtons = state.Gamepad.wButtons;
  152. }
  153. if (gamepad.bLeftTrigger != state.Gamepad.bLeftTrigger)
  154. {
  155. int32_t value = state.Gamepad.bLeftTrigger;
  156. if (filter(GamepadAxis::LeftZ, gamepad.bLeftTrigger, &value) )
  157. {
  158. _eventQueue.postAxisEvent(defaultWindow, handle, GamepadAxis::LeftZ, value);
  159. }
  160. gamepad.bLeftTrigger = state.Gamepad.bLeftTrigger;
  161. }
  162. if (gamepad.bRightTrigger != state.Gamepad.bRightTrigger)
  163. {
  164. int32_t value = state.Gamepad.bRightTrigger;
  165. if (filter(GamepadAxis::RightZ, gamepad.bRightTrigger, &value) )
  166. {
  167. _eventQueue.postAxisEvent(defaultWindow, handle, GamepadAxis::RightZ, value);
  168. }
  169. gamepad.bRightTrigger = state.Gamepad.bRightTrigger;
  170. }
  171. if (gamepad.sThumbLX != state.Gamepad.sThumbLX)
  172. {
  173. int32_t value = state.Gamepad.sThumbLX;
  174. if (filter(GamepadAxis::LeftX, gamepad.sThumbLX, &value) )
  175. {
  176. _eventQueue.postAxisEvent(defaultWindow, handle, GamepadAxis::LeftX, value);
  177. }
  178. gamepad.sThumbLX = state.Gamepad.sThumbLX;
  179. }
  180. if (gamepad.sThumbLY != state.Gamepad.sThumbLY)
  181. {
  182. int32_t value = state.Gamepad.sThumbLY;
  183. if (filter(GamepadAxis::LeftY, gamepad.sThumbLY, &value) )
  184. {
  185. _eventQueue.postAxisEvent(defaultWindow, handle, GamepadAxis::LeftY, value);
  186. }
  187. gamepad.sThumbLY = state.Gamepad.sThumbLY;
  188. }
  189. if (gamepad.sThumbRX != state.Gamepad.sThumbRX)
  190. {
  191. int32_t value = state.Gamepad.sThumbRX;
  192. if (filter(GamepadAxis::RightX, gamepad.sThumbRX, &value) )
  193. {
  194. _eventQueue.postAxisEvent(defaultWindow, handle, GamepadAxis::RightX, value);
  195. }
  196. gamepad.sThumbRX = state.Gamepad.sThumbRX;
  197. }
  198. if (gamepad.sThumbRY != state.Gamepad.sThumbRY)
  199. {
  200. int32_t value = state.Gamepad.sThumbRY;
  201. if (filter(GamepadAxis::RightY, gamepad.sThumbRY, &value) )
  202. {
  203. _eventQueue.postAxisEvent(defaultWindow, handle, GamepadAxis::RightY, value);
  204. }
  205. gamepad.sThumbRY = state.Gamepad.sThumbRY;
  206. }
  207. }
  208. }
  209. }
  210. void* m_xinputdll;
  211. int32_t m_deadzone[GamepadAxis::Count];
  212. int8_t m_flip[GamepadAxis::Count];
  213. XINPUT_STATE m_state[ENTRY_CONFIG_MAX_GAMEPADS];
  214. bool m_connected[ENTRY_CONFIG_MAX_GAMEPADS];
  215. };
  216. XInput s_xinput;
  217. enum
  218. {
  219. WM_USER_WINDOW_CREATE = WM_USER,
  220. WM_USER_WINDOW_DESTROY,
  221. WM_USER_WINDOW_SET_TITLE,
  222. WM_USER_WINDOW_SET_POS,
  223. WM_USER_WINDOW_SET_SIZE,
  224. WM_USER_WINDOW_TOGGLE_FRAME,
  225. WM_USER_WINDOW_MOUSE_LOCK,
  226. };
  227. struct TranslateKeyModifiers
  228. {
  229. int m_vk;
  230. Modifier::Enum m_modifier;
  231. };
  232. static const TranslateKeyModifiers s_translateKeyModifiers[8] =
  233. {
  234. { VK_LMENU, Modifier::LeftAlt },
  235. { VK_RMENU, Modifier::RightAlt },
  236. { VK_LCONTROL, Modifier::LeftCtrl },
  237. { VK_RCONTROL, Modifier::RightCtrl },
  238. { VK_LSHIFT, Modifier::LeftShift },
  239. { VK_RSHIFT, Modifier::RightShift },
  240. { VK_LWIN, Modifier::LeftMeta },
  241. { VK_RWIN, Modifier::RightMeta },
  242. };
  243. static uint8_t translateKeyModifiers()
  244. {
  245. uint8_t modifiers = 0;
  246. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateKeyModifiers); ++ii)
  247. {
  248. const TranslateKeyModifiers& tkm = s_translateKeyModifiers[ii];
  249. modifiers |= 0 > GetKeyState(tkm.m_vk) ? tkm.m_modifier : Modifier::None;
  250. }
  251. return modifiers;
  252. }
  253. static uint8_t s_translateKey[256];
  254. static Key::Enum translateKey(WPARAM _wparam)
  255. {
  256. return (Key::Enum)s_translateKey[_wparam&0xff];
  257. }
  258. struct MainThreadEntry
  259. {
  260. int m_argc;
  261. char** m_argv;
  262. static int32_t threadFunc(void* _userData);
  263. };
  264. struct Msg
  265. {
  266. Msg()
  267. : m_x(0)
  268. , m_y(0)
  269. , m_width(0)
  270. , m_height(0)
  271. , m_flags(0)
  272. {
  273. }
  274. int32_t m_x;
  275. int32_t m_y;
  276. uint32_t m_width;
  277. uint32_t m_height;
  278. uint32_t m_flags;
  279. tinystl::string m_title;
  280. };
  281. static void mouseCapture(HWND _hwnd, bool _capture)
  282. {
  283. if (_capture)
  284. {
  285. SetCapture(_hwnd);
  286. }
  287. else
  288. {
  289. ReleaseCapture();
  290. }
  291. }
  292. struct Context
  293. {
  294. Context()
  295. : m_mz(0)
  296. , m_frame(true)
  297. , m_mouseLock(NULL)
  298. , m_init(false)
  299. , m_exit(false)
  300. {
  301. memset(s_translateKey, 0, sizeof(s_translateKey) );
  302. s_translateKey[VK_ESCAPE] = Key::Esc;
  303. s_translateKey[VK_RETURN] = Key::Return;
  304. s_translateKey[VK_TAB] = Key::Tab;
  305. s_translateKey[VK_BACK] = Key::Backspace;
  306. s_translateKey[VK_SPACE] = Key::Space;
  307. s_translateKey[VK_UP] = Key::Up;
  308. s_translateKey[VK_DOWN] = Key::Down;
  309. s_translateKey[VK_LEFT] = Key::Left;
  310. s_translateKey[VK_RIGHT] = Key::Right;
  311. s_translateKey[VK_INSERT] = Key::Insert;
  312. s_translateKey[VK_DELETE] = Key::Delete;
  313. s_translateKey[VK_HOME] = Key::Home;
  314. s_translateKey[VK_END] = Key::End;
  315. s_translateKey[VK_PRIOR] = Key::PageUp;
  316. s_translateKey[VK_NEXT] = Key::PageDown;
  317. s_translateKey[VK_SNAPSHOT] = Key::Print;
  318. s_translateKey[VK_OEM_PLUS] = Key::Plus;
  319. s_translateKey[VK_OEM_MINUS] = Key::Minus;
  320. s_translateKey[VK_OEM_4] = Key::LeftBracket;
  321. s_translateKey[VK_OEM_6] = Key::RightBracket;
  322. s_translateKey[VK_OEM_1] = Key::Semicolon;
  323. s_translateKey[VK_OEM_7] = Key::Quote;
  324. s_translateKey[VK_OEM_COMMA] = Key::Comma;
  325. s_translateKey[VK_OEM_PERIOD] = Key::Period;
  326. s_translateKey[VK_DECIMAL] = Key::Period;
  327. s_translateKey[VK_OEM_2] = Key::Slash;
  328. s_translateKey[VK_OEM_5] = Key::Backslash;
  329. s_translateKey[VK_OEM_3] = Key::Tilde;
  330. s_translateKey[VK_F1] = Key::F1;
  331. s_translateKey[VK_F2] = Key::F2;
  332. s_translateKey[VK_F3] = Key::F3;
  333. s_translateKey[VK_F4] = Key::F4;
  334. s_translateKey[VK_F5] = Key::F5;
  335. s_translateKey[VK_F6] = Key::F6;
  336. s_translateKey[VK_F7] = Key::F7;
  337. s_translateKey[VK_F8] = Key::F8;
  338. s_translateKey[VK_F9] = Key::F9;
  339. s_translateKey[VK_F10] = Key::F10;
  340. s_translateKey[VK_F11] = Key::F11;
  341. s_translateKey[VK_F12] = Key::F12;
  342. s_translateKey[VK_NUMPAD0] = Key::NumPad0;
  343. s_translateKey[VK_NUMPAD1] = Key::NumPad1;
  344. s_translateKey[VK_NUMPAD2] = Key::NumPad2;
  345. s_translateKey[VK_NUMPAD3] = Key::NumPad3;
  346. s_translateKey[VK_NUMPAD4] = Key::NumPad4;
  347. s_translateKey[VK_NUMPAD5] = Key::NumPad5;
  348. s_translateKey[VK_NUMPAD6] = Key::NumPad6;
  349. s_translateKey[VK_NUMPAD7] = Key::NumPad7;
  350. s_translateKey[VK_NUMPAD8] = Key::NumPad8;
  351. s_translateKey[VK_NUMPAD9] = Key::NumPad9;
  352. s_translateKey[uint8_t('0')] = Key::Key0;
  353. s_translateKey[uint8_t('1')] = Key::Key1;
  354. s_translateKey[uint8_t('2')] = Key::Key2;
  355. s_translateKey[uint8_t('3')] = Key::Key3;
  356. s_translateKey[uint8_t('4')] = Key::Key4;
  357. s_translateKey[uint8_t('5')] = Key::Key5;
  358. s_translateKey[uint8_t('6')] = Key::Key6;
  359. s_translateKey[uint8_t('7')] = Key::Key7;
  360. s_translateKey[uint8_t('8')] = Key::Key8;
  361. s_translateKey[uint8_t('9')] = Key::Key9;
  362. s_translateKey[uint8_t('A')] = Key::KeyA;
  363. s_translateKey[uint8_t('B')] = Key::KeyB;
  364. s_translateKey[uint8_t('C')] = Key::KeyC;
  365. s_translateKey[uint8_t('D')] = Key::KeyD;
  366. s_translateKey[uint8_t('E')] = Key::KeyE;
  367. s_translateKey[uint8_t('F')] = Key::KeyF;
  368. s_translateKey[uint8_t('G')] = Key::KeyG;
  369. s_translateKey[uint8_t('H')] = Key::KeyH;
  370. s_translateKey[uint8_t('I')] = Key::KeyI;
  371. s_translateKey[uint8_t('J')] = Key::KeyJ;
  372. s_translateKey[uint8_t('K')] = Key::KeyK;
  373. s_translateKey[uint8_t('L')] = Key::KeyL;
  374. s_translateKey[uint8_t('M')] = Key::KeyM;
  375. s_translateKey[uint8_t('N')] = Key::KeyN;
  376. s_translateKey[uint8_t('O')] = Key::KeyO;
  377. s_translateKey[uint8_t('P')] = Key::KeyP;
  378. s_translateKey[uint8_t('Q')] = Key::KeyQ;
  379. s_translateKey[uint8_t('R')] = Key::KeyR;
  380. s_translateKey[uint8_t('S')] = Key::KeyS;
  381. s_translateKey[uint8_t('T')] = Key::KeyT;
  382. s_translateKey[uint8_t('U')] = Key::KeyU;
  383. s_translateKey[uint8_t('V')] = Key::KeyV;
  384. s_translateKey[uint8_t('W')] = Key::KeyW;
  385. s_translateKey[uint8_t('X')] = Key::KeyX;
  386. s_translateKey[uint8_t('Y')] = Key::KeyY;
  387. s_translateKey[uint8_t('Z')] = Key::KeyZ;
  388. }
  389. int32_t run(int _argc, char** _argv)
  390. {
  391. SetDllDirectoryA(".");
  392. s_xinput.init();
  393. HINSTANCE instance = (HINSTANCE)GetModuleHandle(NULL);
  394. WNDCLASSEXA wnd;
  395. memset(&wnd, 0, sizeof(wnd) );
  396. wnd.cbSize = sizeof(wnd);
  397. wnd.style = CS_HREDRAW | CS_VREDRAW;
  398. wnd.lpfnWndProc = wndProc;
  399. wnd.hInstance = instance;
  400. wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  401. wnd.hCursor = LoadCursor(NULL, IDC_ARROW);
  402. wnd.lpszClassName = "bgfx";
  403. wnd.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  404. RegisterClassExA(&wnd);
  405. m_windowAlloc.alloc();
  406. m_hwnd[0] = CreateWindowA("bgfx"
  407. , "BGFX"
  408. , WS_OVERLAPPEDWINDOW|WS_VISIBLE
  409. , 0
  410. , 0
  411. , ENTRY_DEFAULT_WIDTH
  412. , ENTRY_DEFAULT_HEIGHT
  413. , NULL
  414. , NULL
  415. , instance
  416. , 0
  417. );
  418. m_flags[0] = 0
  419. | ENTRY_WINDOW_FLAG_ASPECT_RATIO
  420. | ENTRY_WINDOW_FLAG_FRAME
  421. ;
  422. winSetHwnd(m_hwnd[0]);
  423. adjust(m_hwnd[0], ENTRY_DEFAULT_WIDTH, ENTRY_DEFAULT_HEIGHT, true);
  424. clear(m_hwnd[0]);
  425. m_width = ENTRY_DEFAULT_WIDTH;
  426. m_height = ENTRY_DEFAULT_HEIGHT;
  427. m_oldWidth = ENTRY_DEFAULT_WIDTH;
  428. m_oldHeight = ENTRY_DEFAULT_HEIGHT;
  429. MainThreadEntry mte;
  430. mte.m_argc = _argc;
  431. mte.m_argv = _argv;
  432. bx::Thread thread;
  433. thread.init(mte.threadFunc, &mte);
  434. m_init = true;
  435. m_eventQueue.postSizeEvent(findHandle(m_hwnd[0]), m_width, m_height);
  436. MSG msg;
  437. msg.message = WM_NULL;
  438. while (!m_exit)
  439. {
  440. s_xinput.update(m_eventQueue);
  441. WaitForInputIdle(GetCurrentProcess(), 16);
  442. while (0 != PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE) )
  443. {
  444. TranslateMessage(&msg);
  445. DispatchMessage(&msg);
  446. }
  447. }
  448. thread.shutdown();
  449. DestroyWindow(m_hwnd[0]);
  450. s_xinput.shutdown();
  451. return thread.getExitCode();
  452. }
  453. LRESULT process(HWND _hwnd, UINT _id, WPARAM _wparam, LPARAM _lparam)
  454. {
  455. if (m_init)
  456. {
  457. switch (_id)
  458. {
  459. case WM_USER_WINDOW_CREATE:
  460. {
  461. Msg* msg = (Msg*)_lparam;
  462. HWND hwnd = CreateWindowA("bgfx"
  463. , msg->m_title.c_str()
  464. , WS_OVERLAPPEDWINDOW|WS_VISIBLE
  465. , msg->m_x
  466. , msg->m_y
  467. , msg->m_width
  468. , msg->m_height
  469. , NULL
  470. , NULL
  471. , (HINSTANCE)GetModuleHandle(NULL)
  472. , 0
  473. );
  474. clear(hwnd);
  475. m_hwnd[_wparam] = hwnd;
  476. m_flags[_wparam] = msg->m_flags;
  477. WindowHandle handle = { (uint16_t)_wparam };
  478. m_eventQueue.postSizeEvent(handle, msg->m_width, msg->m_height);
  479. m_eventQueue.postWindowEvent(handle, hwnd);
  480. delete msg;
  481. }
  482. break;
  483. case WM_USER_WINDOW_DESTROY:
  484. {
  485. WindowHandle handle = { (uint16_t)_wparam };
  486. PostMessageA(m_hwnd[_wparam], WM_CLOSE, 0, 0);
  487. m_eventQueue.postWindowEvent(handle);
  488. DestroyWindow(m_hwnd[_wparam]);
  489. m_hwnd[_wparam] = 0;
  490. }
  491. break;
  492. case WM_USER_WINDOW_SET_TITLE:
  493. {
  494. Msg* msg = (Msg*)_lparam;
  495. SetWindowTextA(m_hwnd[_wparam], msg->m_title.c_str() );
  496. delete msg;
  497. }
  498. break;
  499. case WM_USER_WINDOW_SET_POS:
  500. {
  501. Msg* msg = (Msg*)_lparam;
  502. SetWindowPos(m_hwnd[_wparam], 0, msg->m_x, msg->m_y, 0, 0
  503. , SWP_NOACTIVATE
  504. | SWP_NOOWNERZORDER
  505. | SWP_NOSIZE
  506. );
  507. delete msg;
  508. }
  509. break;
  510. case WM_USER_WINDOW_SET_SIZE:
  511. {
  512. uint32_t width = GET_X_LPARAM(_lparam);
  513. uint32_t height = GET_Y_LPARAM(_lparam);
  514. adjust(m_hwnd[_wparam], width, height, true);
  515. }
  516. break;
  517. case WM_USER_WINDOW_TOGGLE_FRAME:
  518. {
  519. if (m_frame)
  520. {
  521. m_oldWidth = m_width;
  522. m_oldHeight = m_height;
  523. }
  524. adjust(m_hwnd[_wparam], m_oldWidth, m_oldHeight, !m_frame);
  525. }
  526. break;
  527. case WM_USER_WINDOW_MOUSE_LOCK:
  528. setMouseLock(m_hwnd[_wparam], !!_lparam);
  529. break;
  530. case WM_DESTROY:
  531. break;
  532. case WM_QUIT:
  533. case WM_CLOSE:
  534. if (_hwnd == m_hwnd[0])
  535. {
  536. m_exit = true;
  537. m_eventQueue.postExitEvent();
  538. }
  539. else
  540. {
  541. destroyWindow(findHandle(_hwnd) );
  542. }
  543. // Don't process message. Window will be destroyed later.
  544. return 0;
  545. case WM_SIZING:
  546. {
  547. WindowHandle handle = findHandle(_hwnd);
  548. if (isValid(handle)
  549. && ENTRY_WINDOW_FLAG_ASPECT_RATIO & m_flags[handle.idx])
  550. {
  551. RECT& rect = *(RECT*)_lparam;
  552. uint32_t width = rect.right - rect.left - m_frameWidth;
  553. uint32_t height = rect.bottom - rect.top - m_frameHeight;
  554. // Recalculate size according to aspect ratio
  555. switch (_wparam)
  556. {
  557. case WMSZ_LEFT:
  558. case WMSZ_RIGHT:
  559. {
  560. float aspectRatio = 1.0f/m_aspectRatio;
  561. width = bx::uint32_max(ENTRY_DEFAULT_WIDTH/4, width);
  562. height = uint32_t(float(width)*aspectRatio);
  563. }
  564. break;
  565. default:
  566. {
  567. float aspectRatio = m_aspectRatio;
  568. height = bx::uint32_max(ENTRY_DEFAULT_HEIGHT/4, height);
  569. width = uint32_t(float(height)*aspectRatio);
  570. }
  571. break;
  572. }
  573. // Recalculate position using different anchor points
  574. switch(_wparam)
  575. {
  576. case WMSZ_LEFT:
  577. case WMSZ_TOPLEFT:
  578. case WMSZ_BOTTOMLEFT:
  579. rect.left = rect.right - width - m_frameWidth;
  580. rect.bottom = rect.top + height + m_frameHeight;
  581. break;
  582. default:
  583. rect.right = rect.left + width + m_frameWidth;
  584. rect.bottom = rect.top + height + m_frameHeight;
  585. break;
  586. }
  587. m_eventQueue.postSizeEvent(findHandle(_hwnd), width, height);
  588. }
  589. }
  590. return 0;
  591. case WM_SIZE:
  592. {
  593. WindowHandle handle = findHandle(_hwnd);
  594. if (isValid(handle) )
  595. {
  596. uint32_t width = GET_X_LPARAM(_lparam);
  597. uint32_t height = GET_Y_LPARAM(_lparam);
  598. m_width = width;
  599. m_height = height;
  600. m_eventQueue.postSizeEvent(handle, m_width, m_height);
  601. }
  602. }
  603. break;
  604. case WM_SYSCOMMAND:
  605. switch (_wparam)
  606. {
  607. case SC_MINIMIZE:
  608. case SC_RESTORE:
  609. {
  610. HWND parent = GetWindow(_hwnd, GW_OWNER);
  611. if (NULL != parent)
  612. {
  613. PostMessage(parent, _id, _wparam, _lparam);
  614. }
  615. }
  616. }
  617. break;
  618. case WM_MOUSEMOVE:
  619. {
  620. int32_t mx = GET_X_LPARAM(_lparam);
  621. int32_t my = GET_Y_LPARAM(_lparam);
  622. if (_hwnd == m_mouseLock)
  623. {
  624. mx -= m_mx;
  625. my -= m_my;
  626. if (0 == mx
  627. && 0 == my)
  628. {
  629. break;
  630. }
  631. setMousePos(_hwnd, m_mx, m_my);
  632. }
  633. m_eventQueue.postMouseEvent(findHandle(_hwnd), mx, my, m_mz);
  634. }
  635. break;
  636. case WM_MOUSEWHEEL:
  637. {
  638. POINT pt = { GET_X_LPARAM(_lparam), GET_Y_LPARAM(_lparam) };
  639. ScreenToClient(_hwnd, &pt);
  640. int32_t mx = pt.x;
  641. int32_t my = pt.y;
  642. m_mz += GET_WHEEL_DELTA_WPARAM(_wparam)/WHEEL_DELTA;
  643. m_eventQueue.postMouseEvent(findHandle(_hwnd), mx, my, m_mz);
  644. }
  645. break;
  646. case WM_LBUTTONDOWN:
  647. case WM_LBUTTONUP:
  648. case WM_LBUTTONDBLCLK:
  649. {
  650. mouseCapture(_hwnd, _id == WM_LBUTTONDOWN);
  651. int32_t mx = GET_X_LPARAM(_lparam);
  652. int32_t my = GET_Y_LPARAM(_lparam);
  653. m_eventQueue.postMouseEvent(findHandle(_hwnd), mx, my, m_mz, MouseButton::Left, _id == WM_LBUTTONDOWN);
  654. }
  655. break;
  656. case WM_MBUTTONDOWN:
  657. case WM_MBUTTONUP:
  658. case WM_MBUTTONDBLCLK:
  659. {
  660. mouseCapture(_hwnd, _id == WM_MBUTTONDOWN);
  661. int32_t mx = GET_X_LPARAM(_lparam);
  662. int32_t my = GET_Y_LPARAM(_lparam);
  663. m_eventQueue.postMouseEvent(findHandle(_hwnd), mx, my, m_mz, MouseButton::Middle, _id == WM_MBUTTONDOWN);
  664. }
  665. break;
  666. case WM_RBUTTONDOWN:
  667. case WM_RBUTTONUP:
  668. case WM_RBUTTONDBLCLK:
  669. {
  670. mouseCapture(_hwnd, _id == WM_RBUTTONDOWN);
  671. int32_t mx = GET_X_LPARAM(_lparam);
  672. int32_t my = GET_Y_LPARAM(_lparam);
  673. m_eventQueue.postMouseEvent(findHandle(_hwnd), mx, my, m_mz, MouseButton::Right, _id == WM_RBUTTONDOWN);
  674. }
  675. break;
  676. case WM_KEYDOWN:
  677. case WM_SYSKEYDOWN:
  678. case WM_KEYUP:
  679. case WM_SYSKEYUP:
  680. {
  681. uint8_t modifiers = translateKeyModifiers();
  682. Key::Enum key = translateKey(_wparam);
  683. WindowHandle handle = findHandle(_hwnd);
  684. if (Key::Print == key
  685. && 0x3 == ( (uint32_t)(_lparam)>>30) )
  686. {
  687. // VK_SNAPSHOT doesn't generate keydown event. Fire on down event when previous
  688. // key state bit is set to 1 and transition state bit is set to 1.
  689. //
  690. // http://msdn.microsoft.com/en-us/library/windows/desktop/ms646280%28v=vs.85%29.aspx
  691. m_eventQueue.postKeyEvent(handle, key, modifiers, true);
  692. }
  693. m_eventQueue.postKeyEvent(handle, key, modifiers, _id == WM_KEYDOWN || _id == WM_SYSKEYDOWN);
  694. }
  695. break;
  696. case WM_CHAR:
  697. {
  698. uint8_t utf8[4] = {};
  699. uint8_t len = (uint8_t)WideCharToMultiByte(CP_UTF8
  700. , 0
  701. , (LPCWSTR)&_wparam
  702. , 1
  703. , (LPSTR)utf8
  704. , BX_COUNTOF(utf8)
  705. , NULL
  706. , NULL
  707. );
  708. if (0 != len)
  709. {
  710. WindowHandle handle = findHandle(_hwnd);
  711. m_eventQueue.postCharEvent(handle, len, utf8);
  712. }
  713. }
  714. break;
  715. default:
  716. break;
  717. }
  718. }
  719. return DefWindowProc(_hwnd, _id, _wparam, _lparam);
  720. }
  721. WindowHandle findHandle(HWND _hwnd)
  722. {
  723. bx::LwMutexScope scope(m_lock);
  724. for (uint16_t ii = 0, num = m_windowAlloc.getNumHandles(); ii < num; ++ii)
  725. {
  726. uint16_t idx = m_windowAlloc.getHandleAt(ii);
  727. if (_hwnd == m_hwnd[idx])
  728. {
  729. WindowHandle handle = { idx };
  730. return handle;
  731. }
  732. }
  733. WindowHandle invalid = { UINT16_MAX };
  734. return invalid;
  735. }
  736. void clear(HWND _hwnd)
  737. {
  738. RECT rect;
  739. GetWindowRect(_hwnd, &rect);
  740. HBRUSH brush = CreateSolidBrush(RGB(0, 0, 0) );
  741. HDC hdc = GetDC(_hwnd);
  742. SelectObject(hdc, brush);
  743. FillRect(hdc, &rect, brush);
  744. }
  745. void adjust(HWND _hwnd, uint32_t _width, uint32_t _height, bool _windowFrame)
  746. {
  747. m_width = _width;
  748. m_height = _height;
  749. m_aspectRatio = float(_width)/float(_height);
  750. ShowWindow(_hwnd, SW_SHOWNORMAL);
  751. RECT rect;
  752. RECT newrect = {0, 0, (LONG)_width, (LONG)_height};
  753. DWORD style = WS_POPUP|WS_SYSMENU;
  754. if (m_frame)
  755. {
  756. GetWindowRect(_hwnd, &m_rect);
  757. m_style = GetWindowLong(_hwnd, GWL_STYLE);
  758. }
  759. if (_windowFrame)
  760. {
  761. rect = m_rect;
  762. style = m_style;
  763. }
  764. else
  765. {
  766. #if defined(__MINGW32__)
  767. rect = m_rect;
  768. style = m_style;
  769. #else
  770. HMONITOR monitor = MonitorFromWindow(_hwnd, MONITOR_DEFAULTTONEAREST);
  771. MONITORINFO mi;
  772. mi.cbSize = sizeof(mi);
  773. GetMonitorInfo(monitor, &mi);
  774. newrect = mi.rcMonitor;
  775. rect = mi.rcMonitor;
  776. m_aspectRatio = float(newrect.right - newrect.left)/float(newrect.bottom - newrect.top);
  777. #endif // !defined(__MINGW__)
  778. }
  779. SetWindowLong(_hwnd, GWL_STYLE, style);
  780. uint32_t prewidth = newrect.right - newrect.left;
  781. uint32_t preheight = newrect.bottom - newrect.top;
  782. AdjustWindowRect(&newrect, style, FALSE);
  783. m_frameWidth = (newrect.right - newrect.left) - prewidth;
  784. m_frameHeight = (newrect.bottom - newrect.top ) - preheight;
  785. UpdateWindow(_hwnd);
  786. if (rect.left == -32000
  787. || rect.top == -32000)
  788. {
  789. rect.left = 0;
  790. rect.top = 0;
  791. }
  792. int32_t left = rect.left;
  793. int32_t top = rect.top;
  794. int32_t width = (newrect.right-newrect.left);
  795. int32_t height = (newrect.bottom-newrect.top);
  796. if (!_windowFrame)
  797. {
  798. float aspectRatio = 1.0f/m_aspectRatio;
  799. width = bx::uint32_max(ENTRY_DEFAULT_WIDTH/4, width);
  800. height = uint32_t(float(width)*aspectRatio);
  801. left = newrect.left+(newrect.right -newrect.left-width)/2;
  802. top = newrect.top +(newrect.bottom-newrect.top-height)/2;
  803. }
  804. SetWindowPos(_hwnd
  805. , HWND_TOP
  806. , left
  807. , top
  808. , width
  809. , height
  810. , SWP_SHOWWINDOW
  811. );
  812. ShowWindow(_hwnd, SW_RESTORE);
  813. m_frame = _windowFrame;
  814. }
  815. void setMousePos(HWND _hwnd, int32_t _mx, int32_t _my)
  816. {
  817. POINT pt = { _mx, _my };
  818. ClientToScreen(_hwnd, &pt);
  819. SetCursorPos(pt.x, pt.y);
  820. }
  821. void setMouseLock(HWND _hwnd, bool _lock)
  822. {
  823. if (_hwnd != m_mouseLock)
  824. {
  825. if (_lock)
  826. {
  827. m_mx = m_width/2;
  828. m_my = m_height/2;
  829. ShowCursor(false);
  830. setMousePos(_hwnd, m_mx, m_my);
  831. }
  832. else
  833. {
  834. setMousePos(_hwnd, m_mx, m_my);
  835. ShowCursor(true);
  836. }
  837. m_mouseLock = _hwnd;
  838. }
  839. }
  840. static LRESULT CALLBACK wndProc(HWND _hwnd, UINT _id, WPARAM _wparam, LPARAM _lparam);
  841. EventQueue m_eventQueue;
  842. bx::LwMutex m_lock;
  843. bx::HandleAllocT<ENTRY_CONFIG_MAX_WINDOWS> m_windowAlloc;
  844. HWND m_hwnd[ENTRY_CONFIG_MAX_WINDOWS];
  845. uint32_t m_flags[ENTRY_CONFIG_MAX_WINDOWS];
  846. RECT m_rect;
  847. DWORD m_style;
  848. uint32_t m_width;
  849. uint32_t m_height;
  850. uint32_t m_oldWidth;
  851. uint32_t m_oldHeight;
  852. uint32_t m_frameWidth;
  853. uint32_t m_frameHeight;
  854. float m_aspectRatio;
  855. int32_t m_mx;
  856. int32_t m_my;
  857. int32_t m_mz;
  858. bool m_frame;
  859. HWND m_mouseLock;
  860. bool m_init;
  861. bool m_exit;
  862. };
  863. static Context s_ctx;
  864. LRESULT CALLBACK Context::wndProc(HWND _hwnd, UINT _id, WPARAM _wparam, LPARAM _lparam)
  865. {
  866. return s_ctx.process(_hwnd, _id, _wparam, _lparam);
  867. }
  868. const Event* poll()
  869. {
  870. return s_ctx.m_eventQueue.poll();
  871. }
  872. const Event* poll(WindowHandle _handle)
  873. {
  874. return s_ctx.m_eventQueue.poll(_handle);
  875. }
  876. void release(const Event* _event)
  877. {
  878. s_ctx.m_eventQueue.release(_event);
  879. }
  880. WindowHandle createWindow(int32_t _x, int32_t _y, uint32_t _width, uint32_t _height, uint32_t _flags, const char* _title)
  881. {
  882. bx::LwMutexScope scope(s_ctx.m_lock);
  883. WindowHandle handle = { s_ctx.m_windowAlloc.alloc() };
  884. if (UINT16_MAX != handle.idx)
  885. {
  886. Msg* msg = new Msg;
  887. msg->m_x = _x;
  888. msg->m_y = _y;
  889. msg->m_width = _width;
  890. msg->m_height = _height;
  891. msg->m_title = _title;
  892. msg->m_flags = _flags;
  893. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_CREATE, handle.idx, (LPARAM)msg);
  894. }
  895. return handle;
  896. }
  897. void destroyWindow(WindowHandle _handle)
  898. {
  899. if (UINT16_MAX != _handle.idx)
  900. {
  901. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_DESTROY, _handle.idx, 0);
  902. bx::LwMutexScope scope(s_ctx.m_lock);
  903. s_ctx.m_windowAlloc.free(_handle.idx);
  904. }
  905. }
  906. void setWindowPos(WindowHandle _handle, int32_t _x, int32_t _y)
  907. {
  908. Msg* msg = new Msg;
  909. msg->m_x = _x;
  910. msg->m_y = _y;
  911. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_SET_POS, _handle.idx, (LPARAM)msg);
  912. }
  913. void setWindowSize(WindowHandle _handle, uint32_t _width, uint32_t _height)
  914. {
  915. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_SET_SIZE, _handle.idx, (_height<<16) | (_width&0xffff) );
  916. }
  917. void setWindowTitle(WindowHandle _handle, const char* _title)
  918. {
  919. Msg* msg = new Msg;
  920. msg->m_title = _title;
  921. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_SET_TITLE, _handle.idx, (LPARAM)msg);
  922. }
  923. void toggleWindowFrame(WindowHandle _handle)
  924. {
  925. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_TOGGLE_FRAME, _handle.idx, 0);
  926. }
  927. void toggleFullscreen(WindowHandle _handle)
  928. {
  929. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_TOGGLE_FRAME, _handle.idx, 0);
  930. }
  931. void setMouseLock(WindowHandle _handle, bool _lock)
  932. {
  933. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_MOUSE_LOCK, _handle.idx, _lock);
  934. }
  935. int32_t MainThreadEntry::threadFunc(void* _userData)
  936. {
  937. MainThreadEntry* self = (MainThreadEntry*)_userData;
  938. int32_t result = main(self->m_argc, self->m_argv);
  939. PostMessage(s_ctx.m_hwnd[0], WM_QUIT, 0, 0);
  940. return result;
  941. }
  942. } // namespace entry
  943. int main(int _argc, char** _argv)
  944. {
  945. using namespace entry;
  946. return s_ctx.run(_argc, _argv);
  947. }
  948. #endif // BX_PLATFORM_WINDOWS