entry_windows.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  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. }
  809. void adjust(HWND _hwnd, uint32_t _width, uint32_t _height, bool _windowFrame)
  810. {
  811. m_width = _width;
  812. m_height = _height;
  813. m_aspectRatio = float(_width)/float(_height);
  814. ShowWindow(_hwnd, SW_SHOWNORMAL);
  815. RECT rect;
  816. RECT newrect = {0, 0, (LONG)_width, (LONG)_height};
  817. DWORD style = WS_POPUP|WS_SYSMENU;
  818. if (m_frame)
  819. {
  820. GetWindowRect(_hwnd, &m_rect);
  821. m_style = GetWindowLong(_hwnd, GWL_STYLE);
  822. }
  823. if (_windowFrame)
  824. {
  825. rect = m_rect;
  826. style = m_style;
  827. }
  828. else
  829. {
  830. HMONITOR monitor = MonitorFromWindow(_hwnd, MONITOR_DEFAULTTONEAREST);
  831. MONITORINFO mi;
  832. mi.cbSize = sizeof(mi);
  833. GetMonitorInfo(monitor, &mi);
  834. newrect = mi.rcMonitor;
  835. rect = mi.rcMonitor;
  836. m_aspectRatio = float(newrect.right - newrect.left)/float(newrect.bottom - newrect.top);
  837. }
  838. SetWindowLong(_hwnd, GWL_STYLE, style);
  839. uint32_t prewidth = newrect.right - newrect.left;
  840. uint32_t preheight = newrect.bottom - newrect.top;
  841. AdjustWindowRect(&newrect, style, FALSE);
  842. m_frameWidth = (newrect.right - newrect.left) - prewidth;
  843. m_frameHeight = (newrect.bottom - newrect.top ) - preheight;
  844. UpdateWindow(_hwnd);
  845. if (rect.left == -32000
  846. || rect.top == -32000)
  847. {
  848. rect.left = 0;
  849. rect.top = 0;
  850. }
  851. int32_t left = rect.left;
  852. int32_t top = rect.top;
  853. int32_t width = (newrect.right-newrect.left);
  854. int32_t height = (newrect.bottom-newrect.top);
  855. if (!_windowFrame)
  856. {
  857. float aspectRatio = 1.0f/m_aspectRatio;
  858. width = bx::uint32_max(ENTRY_DEFAULT_WIDTH/4, width);
  859. height = uint32_t(float(width)*aspectRatio);
  860. left = newrect.left+(newrect.right -newrect.left-width)/2;
  861. top = newrect.top +(newrect.bottom-newrect.top-height)/2;
  862. }
  863. SetWindowPos(_hwnd
  864. , HWND_TOP
  865. , left
  866. , top
  867. , width
  868. , height
  869. , SWP_SHOWWINDOW
  870. );
  871. ShowWindow(_hwnd, SW_RESTORE);
  872. m_frame = _windowFrame;
  873. }
  874. void setMousePos(HWND _hwnd, int32_t _mx, int32_t _my)
  875. {
  876. POINT pt = { _mx, _my };
  877. ClientToScreen(_hwnd, &pt);
  878. SetCursorPos(pt.x, pt.y);
  879. }
  880. void setMouseLock(HWND _hwnd, bool _lock)
  881. {
  882. HWND newMouseLock = _lock ? _hwnd : 0;
  883. if (newMouseLock != m_mouseLock)
  884. {
  885. if (_lock)
  886. {
  887. m_mx = m_width/2;
  888. m_my = m_height/2;
  889. ShowCursor(false);
  890. setMousePos(_hwnd, m_mx, m_my);
  891. }
  892. else
  893. {
  894. setMousePos(_hwnd, m_mx, m_my);
  895. ShowCursor(true);
  896. }
  897. m_mouseLock = newMouseLock;
  898. }
  899. }
  900. static LRESULT CALLBACK wndProc(HWND _hwnd, UINT _id, WPARAM _wparam, LPARAM _lparam);
  901. EventQueue m_eventQueue;
  902. WCHAR m_surrogate;
  903. bx::Mutex m_lock;
  904. bx::HandleAllocT<ENTRY_CONFIG_MAX_WINDOWS> m_windowAlloc;
  905. HWND m_hwnd[ENTRY_CONFIG_MAX_WINDOWS];
  906. uint32_t m_flags[ENTRY_CONFIG_MAX_WINDOWS];
  907. RECT m_rect;
  908. DWORD m_style;
  909. uint32_t m_width;
  910. uint32_t m_height;
  911. uint32_t m_oldWidth;
  912. uint32_t m_oldHeight;
  913. uint32_t m_frameWidth;
  914. uint32_t m_frameHeight;
  915. float m_aspectRatio;
  916. int32_t m_mx;
  917. int32_t m_my;
  918. int32_t m_mz;
  919. bool m_frame;
  920. HWND m_mouseLock;
  921. bool m_init;
  922. bool m_exit;
  923. };
  924. static Context s_ctx;
  925. LRESULT CALLBACK Context::wndProc(HWND _hwnd, UINT _id, WPARAM _wparam, LPARAM _lparam)
  926. {
  927. return s_ctx.process(_hwnd, _id, _wparam, _lparam);
  928. }
  929. const Event* poll()
  930. {
  931. return s_ctx.m_eventQueue.poll();
  932. }
  933. const Event* poll(WindowHandle _handle)
  934. {
  935. return s_ctx.m_eventQueue.poll(_handle);
  936. }
  937. void release(const Event* _event)
  938. {
  939. s_ctx.m_eventQueue.release(_event);
  940. }
  941. WindowHandle createWindow(int32_t _x, int32_t _y, uint32_t _width, uint32_t _height, uint32_t _flags, const char* _title)
  942. {
  943. bx::MutexScope scope(s_ctx.m_lock);
  944. WindowHandle handle = { s_ctx.m_windowAlloc.alloc() };
  945. if (UINT16_MAX != handle.idx)
  946. {
  947. Msg* msg = new Msg;
  948. msg->m_x = _x;
  949. msg->m_y = _y;
  950. msg->m_width = _width;
  951. msg->m_height = _height;
  952. msg->m_title = _title;
  953. msg->m_flags = _flags;
  954. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_CREATE, handle.idx, (LPARAM)msg);
  955. }
  956. return handle;
  957. }
  958. void destroyWindow(WindowHandle _handle)
  959. {
  960. if (UINT16_MAX != _handle.idx)
  961. {
  962. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_DESTROY, _handle.idx, 0);
  963. bx::MutexScope scope(s_ctx.m_lock);
  964. s_ctx.m_windowAlloc.free(_handle.idx);
  965. }
  966. }
  967. void setWindowPos(WindowHandle _handle, int32_t _x, int32_t _y)
  968. {
  969. Msg* msg = new Msg;
  970. msg->m_x = _x;
  971. msg->m_y = _y;
  972. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_SET_POS, _handle.idx, (LPARAM)msg);
  973. }
  974. void setWindowSize(WindowHandle _handle, uint32_t _width, uint32_t _height)
  975. {
  976. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_SET_SIZE, _handle.idx, (_height<<16) | (_width&0xffff) );
  977. }
  978. void setWindowTitle(WindowHandle _handle, const char* _title)
  979. {
  980. Msg* msg = new Msg;
  981. msg->m_title = _title;
  982. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_SET_TITLE, _handle.idx, (LPARAM)msg);
  983. }
  984. void setWindowFlags(WindowHandle _handle, uint32_t _flags, bool _enabled)
  985. {
  986. Msg* msg = new Msg;
  987. msg->m_flags = _flags;
  988. msg->m_flagsEnabled = _enabled;
  989. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_SET_FLAGS, _handle.idx, (LPARAM)msg);
  990. }
  991. void toggleFullscreen(WindowHandle _handle)
  992. {
  993. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_TOGGLE_FRAME, _handle.idx, 0);
  994. }
  995. void setMouseLock(WindowHandle _handle, bool _lock)
  996. {
  997. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_MOUSE_LOCK, _handle.idx, _lock);
  998. }
  999. int32_t MainThreadEntry::threadFunc(bx::Thread* /*_thread*/, void* _userData)
  1000. {
  1001. MainThreadEntry* self = (MainThreadEntry*)_userData;
  1002. int32_t result = main(self->m_argc, self->m_argv);
  1003. PostMessage(s_ctx.m_hwnd[0], WM_QUIT, 0, 0);
  1004. return result;
  1005. }
  1006. } // namespace entry
  1007. int main(int _argc, const char* const* _argv)
  1008. {
  1009. using namespace entry;
  1010. return s_ctx.run(_argc, _argv);
  1011. }
  1012. #endif // BX_PLATFORM_WINDOWS