entry_windows.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  1. /*
  2. * Copyright 2011-2020 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. HWND newMouseLock = _lock ? _hwnd : 0;
  881. if (newMouseLock != m_mouseLock)
  882. {
  883. if (_lock)
  884. {
  885. m_mx = m_width/2;
  886. m_my = m_height/2;
  887. ShowCursor(false);
  888. setMousePos(_hwnd, m_mx, m_my);
  889. }
  890. else
  891. {
  892. setMousePos(_hwnd, m_mx, m_my);
  893. ShowCursor(true);
  894. }
  895. m_mouseLock = newMouseLock;
  896. }
  897. }
  898. static LRESULT CALLBACK wndProc(HWND _hwnd, UINT _id, WPARAM _wparam, LPARAM _lparam);
  899. EventQueue m_eventQueue;
  900. WCHAR m_surrogate;
  901. bx::Mutex m_lock;
  902. bx::HandleAllocT<ENTRY_CONFIG_MAX_WINDOWS> m_windowAlloc;
  903. HWND m_hwnd[ENTRY_CONFIG_MAX_WINDOWS];
  904. uint32_t m_flags[ENTRY_CONFIG_MAX_WINDOWS];
  905. RECT m_rect;
  906. DWORD m_style;
  907. uint32_t m_width;
  908. uint32_t m_height;
  909. uint32_t m_oldWidth;
  910. uint32_t m_oldHeight;
  911. uint32_t m_frameWidth;
  912. uint32_t m_frameHeight;
  913. float m_aspectRatio;
  914. int32_t m_mx;
  915. int32_t m_my;
  916. int32_t m_mz;
  917. bool m_frame;
  918. HWND m_mouseLock;
  919. bool m_init;
  920. bool m_exit;
  921. };
  922. static Context s_ctx;
  923. LRESULT CALLBACK Context::wndProc(HWND _hwnd, UINT _id, WPARAM _wparam, LPARAM _lparam)
  924. {
  925. return s_ctx.process(_hwnd, _id, _wparam, _lparam);
  926. }
  927. const Event* poll()
  928. {
  929. return s_ctx.m_eventQueue.poll();
  930. }
  931. const Event* poll(WindowHandle _handle)
  932. {
  933. return s_ctx.m_eventQueue.poll(_handle);
  934. }
  935. void release(const Event* _event)
  936. {
  937. s_ctx.m_eventQueue.release(_event);
  938. }
  939. WindowHandle createWindow(int32_t _x, int32_t _y, uint32_t _width, uint32_t _height, uint32_t _flags, const char* _title)
  940. {
  941. bx::MutexScope scope(s_ctx.m_lock);
  942. WindowHandle handle = { s_ctx.m_windowAlloc.alloc() };
  943. if (UINT16_MAX != handle.idx)
  944. {
  945. Msg* msg = new Msg;
  946. msg->m_x = _x;
  947. msg->m_y = _y;
  948. msg->m_width = _width;
  949. msg->m_height = _height;
  950. msg->m_title = _title;
  951. msg->m_flags = _flags;
  952. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_CREATE, handle.idx, (LPARAM)msg);
  953. }
  954. return handle;
  955. }
  956. void destroyWindow(WindowHandle _handle)
  957. {
  958. if (UINT16_MAX != _handle.idx)
  959. {
  960. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_DESTROY, _handle.idx, 0);
  961. bx::MutexScope scope(s_ctx.m_lock);
  962. s_ctx.m_windowAlloc.free(_handle.idx);
  963. }
  964. }
  965. void setWindowPos(WindowHandle _handle, int32_t _x, int32_t _y)
  966. {
  967. Msg* msg = new Msg;
  968. msg->m_x = _x;
  969. msg->m_y = _y;
  970. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_SET_POS, _handle.idx, (LPARAM)msg);
  971. }
  972. void setWindowSize(WindowHandle _handle, uint32_t _width, uint32_t _height)
  973. {
  974. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_SET_SIZE, _handle.idx, (_height<<16) | (_width&0xffff) );
  975. }
  976. void setWindowTitle(WindowHandle _handle, const char* _title)
  977. {
  978. Msg* msg = new Msg;
  979. msg->m_title = _title;
  980. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_SET_TITLE, _handle.idx, (LPARAM)msg);
  981. }
  982. void setWindowFlags(WindowHandle _handle, uint32_t _flags, bool _enabled)
  983. {
  984. Msg* msg = new Msg;
  985. msg->m_flags = _flags;
  986. msg->m_flagsEnabled = _enabled;
  987. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_SET_FLAGS, _handle.idx, (LPARAM)msg);
  988. }
  989. void toggleFullscreen(WindowHandle _handle)
  990. {
  991. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_TOGGLE_FRAME, _handle.idx, 0);
  992. }
  993. void setMouseLock(WindowHandle _handle, bool _lock)
  994. {
  995. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_MOUSE_LOCK, _handle.idx, _lock);
  996. }
  997. int32_t MainThreadEntry::threadFunc(bx::Thread* /*_thread*/, void* _userData)
  998. {
  999. MainThreadEntry* self = (MainThreadEntry*)_userData;
  1000. int32_t result = main(self->m_argc, self->m_argv);
  1001. PostMessage(s_ctx.m_hwnd[0], WM_QUIT, 0, 0);
  1002. return result;
  1003. }
  1004. } // namespace entry
  1005. int main(int _argc, const char* const* _argv)
  1006. {
  1007. using namespace entry;
  1008. return s_ctx.run(_argc, _argv);
  1009. }
  1010. #endif // BX_PLATFORM_WINDOWS