entry_windows.cpp 30 KB

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