entry_windows.cpp 30 KB

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