entry_windows.cpp 28 KB

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