entry_windows.cpp 28 KB

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