entry_windows.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. /*
  2. * Copyright 2011-2014 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include "entry_p.h"
  6. #if ENTRY_CONFIG_USE_NATIVE && BX_PLATFORM_WINDOWS
  7. #include <bgfxplatform.h>
  8. #include <bx/uint32_t.h>
  9. #include <bx/thread.h>
  10. #include <bx/mutex.h>
  11. #include <bx/handlealloc.h>
  12. #include <tinystl/allocator.h>
  13. #include <tinystl/string.h>
  14. #include <windowsx.h>
  15. namespace entry
  16. {
  17. enum
  18. {
  19. WM_USER_WINDOW_CREATE = WM_USER,
  20. WM_USER_WINDOW_DESTROY,
  21. WM_USER_WINDOW_SET_TITLE,
  22. WM_USER_WINDOW_SET_POS,
  23. WM_USER_WINDOW_SET_SIZE,
  24. WM_USER_WINDOW_TOGGLE_FRAME,
  25. WM_USER_WINDOW_MOUSE_LOCK,
  26. };
  27. struct TranslateKeyModifiers
  28. {
  29. int m_vk;
  30. Modifier::Enum m_modifier;
  31. };
  32. static const TranslateKeyModifiers s_translateKeyModifiers[8] =
  33. {
  34. { VK_LMENU, Modifier::LeftAlt },
  35. { VK_RMENU, Modifier::RightAlt },
  36. { VK_LCONTROL, Modifier::LeftCtrl },
  37. { VK_RCONTROL, Modifier::RightCtrl },
  38. { VK_LSHIFT, Modifier::LeftShift },
  39. { VK_RSHIFT, Modifier::RightShift },
  40. { VK_LWIN, Modifier::LeftMeta },
  41. { VK_RWIN, Modifier::RightMeta },
  42. };
  43. static uint8_t translateKeyModifiers()
  44. {
  45. uint8_t modifiers = 0;
  46. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateKeyModifiers); ++ii)
  47. {
  48. const TranslateKeyModifiers& tkm = s_translateKeyModifiers[ii];
  49. modifiers |= 0 > GetKeyState(tkm.m_vk) ? tkm.m_modifier : Modifier::None;
  50. }
  51. return modifiers;
  52. }
  53. static uint8_t s_translateKey[256];
  54. static Key::Enum translateKey(WPARAM _wparam)
  55. {
  56. return (Key::Enum)s_translateKey[_wparam&0xff];
  57. }
  58. struct MainThreadEntry
  59. {
  60. int m_argc;
  61. char** m_argv;
  62. static int32_t threadFunc(void* _userData);
  63. };
  64. struct Msg
  65. {
  66. Msg()
  67. : m_x(0)
  68. , m_y(0)
  69. , m_width(0)
  70. , m_height(0)
  71. , m_flags(0)
  72. {
  73. }
  74. int32_t m_x;
  75. int32_t m_y;
  76. uint32_t m_width;
  77. uint32_t m_height;
  78. uint32_t m_flags;
  79. tinystl::string m_title;
  80. };
  81. struct Context
  82. {
  83. Context()
  84. : m_mz(0)
  85. , m_frame(true)
  86. , m_mouseLock(NULL)
  87. , m_init(false)
  88. , m_exit(false)
  89. {
  90. memset(s_translateKey, 0, sizeof(s_translateKey) );
  91. s_translateKey[VK_ESCAPE] = Key::Esc;
  92. s_translateKey[VK_RETURN] = Key::Return;
  93. s_translateKey[VK_TAB] = Key::Tab;
  94. s_translateKey[VK_BACK] = Key::Backspace;
  95. s_translateKey[VK_SPACE] = Key::Space;
  96. s_translateKey[VK_UP] = Key::Up;
  97. s_translateKey[VK_DOWN] = Key::Down;
  98. s_translateKey[VK_LEFT] = Key::Left;
  99. s_translateKey[VK_RIGHT] = Key::Right;
  100. s_translateKey[VK_PRIOR] = Key::PageUp;
  101. s_translateKey[VK_NEXT] = Key::PageUp;
  102. s_translateKey[VK_HOME] = Key::Home;
  103. s_translateKey[VK_END] = Key::End;
  104. s_translateKey[VK_SNAPSHOT] = Key::Print;
  105. s_translateKey[VK_OEM_PLUS] = Key::Plus;
  106. s_translateKey[VK_OEM_MINUS] = Key::Minus;
  107. s_translateKey[VK_F1] = Key::F1;
  108. s_translateKey[VK_F2] = Key::F2;
  109. s_translateKey[VK_F3] = Key::F3;
  110. s_translateKey[VK_F4] = Key::F4;
  111. s_translateKey[VK_F5] = Key::F5;
  112. s_translateKey[VK_F6] = Key::F6;
  113. s_translateKey[VK_F7] = Key::F7;
  114. s_translateKey[VK_F8] = Key::F8;
  115. s_translateKey[VK_F9] = Key::F9;
  116. s_translateKey[VK_F10] = Key::F10;
  117. s_translateKey[VK_F11] = Key::F11;
  118. s_translateKey[VK_F12] = Key::F12;
  119. s_translateKey[VK_NUMPAD0] = Key::NumPad0;
  120. s_translateKey[VK_NUMPAD1] = Key::NumPad1;
  121. s_translateKey[VK_NUMPAD2] = Key::NumPad2;
  122. s_translateKey[VK_NUMPAD3] = Key::NumPad3;
  123. s_translateKey[VK_NUMPAD4] = Key::NumPad4;
  124. s_translateKey[VK_NUMPAD5] = Key::NumPad5;
  125. s_translateKey[VK_NUMPAD6] = Key::NumPad6;
  126. s_translateKey[VK_NUMPAD7] = Key::NumPad7;
  127. s_translateKey[VK_NUMPAD8] = Key::NumPad8;
  128. s_translateKey[VK_NUMPAD9] = Key::NumPad9;
  129. s_translateKey[uint8_t('0')] = Key::Key0;
  130. s_translateKey[uint8_t('1')] = Key::Key1;
  131. s_translateKey[uint8_t('2')] = Key::Key2;
  132. s_translateKey[uint8_t('3')] = Key::Key3;
  133. s_translateKey[uint8_t('4')] = Key::Key4;
  134. s_translateKey[uint8_t('5')] = Key::Key5;
  135. s_translateKey[uint8_t('6')] = Key::Key6;
  136. s_translateKey[uint8_t('7')] = Key::Key7;
  137. s_translateKey[uint8_t('8')] = Key::Key8;
  138. s_translateKey[uint8_t('9')] = Key::Key9;
  139. s_translateKey[uint8_t('A')] = Key::KeyA;
  140. s_translateKey[uint8_t('B')] = Key::KeyB;
  141. s_translateKey[uint8_t('C')] = Key::KeyC;
  142. s_translateKey[uint8_t('D')] = Key::KeyD;
  143. s_translateKey[uint8_t('E')] = Key::KeyE;
  144. s_translateKey[uint8_t('F')] = Key::KeyF;
  145. s_translateKey[uint8_t('G')] = Key::KeyG;
  146. s_translateKey[uint8_t('H')] = Key::KeyH;
  147. s_translateKey[uint8_t('I')] = Key::KeyI;
  148. s_translateKey[uint8_t('J')] = Key::KeyJ;
  149. s_translateKey[uint8_t('K')] = Key::KeyK;
  150. s_translateKey[uint8_t('L')] = Key::KeyL;
  151. s_translateKey[uint8_t('M')] = Key::KeyM;
  152. s_translateKey[uint8_t('N')] = Key::KeyN;
  153. s_translateKey[uint8_t('O')] = Key::KeyO;
  154. s_translateKey[uint8_t('P')] = Key::KeyP;
  155. s_translateKey[uint8_t('Q')] = Key::KeyQ;
  156. s_translateKey[uint8_t('R')] = Key::KeyR;
  157. s_translateKey[uint8_t('S')] = Key::KeyS;
  158. s_translateKey[uint8_t('T')] = Key::KeyT;
  159. s_translateKey[uint8_t('U')] = Key::KeyU;
  160. s_translateKey[uint8_t('V')] = Key::KeyV;
  161. s_translateKey[uint8_t('W')] = Key::KeyW;
  162. s_translateKey[uint8_t('X')] = Key::KeyX;
  163. s_translateKey[uint8_t('Y')] = Key::KeyY;
  164. s_translateKey[uint8_t('Z')] = Key::KeyZ;
  165. }
  166. int32_t run(int _argc, char** _argv)
  167. {
  168. SetDllDirectory(".");
  169. HINSTANCE instance = (HINSTANCE)GetModuleHandle(NULL);
  170. WNDCLASSEX wnd;
  171. memset(&wnd, 0, sizeof(wnd) );
  172. wnd.cbSize = sizeof(wnd);
  173. wnd.style = CS_HREDRAW | CS_VREDRAW;
  174. wnd.lpfnWndProc = wndProc;
  175. wnd.hInstance = instance;
  176. wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  177. wnd.hCursor = LoadCursor(NULL, IDC_ARROW);
  178. wnd.lpszClassName = "bgfx";
  179. wnd.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  180. RegisterClassExA(&wnd);
  181. m_windowAlloc.alloc();
  182. m_hwnd[0] = CreateWindowA("bgfx"
  183. , "BGFX"
  184. , WS_OVERLAPPEDWINDOW|WS_VISIBLE
  185. , 0
  186. , 0
  187. , ENTRY_DEFAULT_WIDTH
  188. , ENTRY_DEFAULT_HEIGHT
  189. , NULL
  190. , NULL
  191. , instance
  192. , 0
  193. );
  194. m_flags[0] = 0
  195. | ENTRY_WINDOW_FLAG_ASPECT_RATIO
  196. | ENTRY_WINDOW_FLAG_FRAME
  197. ;
  198. bgfx::winSetHwnd(m_hwnd[0]);
  199. adjust(m_hwnd[0], ENTRY_DEFAULT_WIDTH, ENTRY_DEFAULT_HEIGHT, true);
  200. clear(m_hwnd[0]);
  201. m_width = ENTRY_DEFAULT_WIDTH;
  202. m_height = ENTRY_DEFAULT_HEIGHT;
  203. m_oldWidth = ENTRY_DEFAULT_WIDTH;
  204. m_oldHeight = ENTRY_DEFAULT_HEIGHT;
  205. MainThreadEntry mte;
  206. mte.m_argc = _argc;
  207. mte.m_argv = _argv;
  208. bx::Thread thread;
  209. thread.init(mte.threadFunc, &mte);
  210. m_init = true;
  211. m_eventQueue.postSizeEvent(findHandle(m_hwnd[0]), m_width, m_height);
  212. MSG msg;
  213. msg.message = WM_NULL;
  214. while (!m_exit)
  215. {
  216. WaitMessage();
  217. while (0 != PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE) )
  218. {
  219. TranslateMessage(&msg);
  220. DispatchMessage(&msg);
  221. }
  222. }
  223. thread.shutdown();
  224. DestroyWindow(m_hwnd[0]);
  225. return 0;
  226. }
  227. LRESULT process(HWND _hwnd, UINT _id, WPARAM _wparam, LPARAM _lparam)
  228. {
  229. if (m_init)
  230. {
  231. switch (_id)
  232. {
  233. case WM_USER_WINDOW_CREATE:
  234. {
  235. Msg* msg = (Msg*)_lparam;
  236. HWND hwnd = CreateWindowA("bgfx"
  237. , msg->m_title.c_str()
  238. , WS_OVERLAPPEDWINDOW|WS_VISIBLE
  239. , msg->m_x
  240. , msg->m_y
  241. , msg->m_width
  242. , msg->m_height
  243. , m_hwnd[0]
  244. , NULL
  245. , (HINSTANCE)GetModuleHandle(NULL)
  246. , 0
  247. );
  248. clear(hwnd);
  249. m_hwnd[_wparam] = hwnd;
  250. m_flags[_wparam] = msg->m_flags;
  251. WindowHandle handle = { (uint16_t)_wparam };
  252. m_eventQueue.postSizeEvent(handle, msg->m_width, msg->m_height);
  253. m_eventQueue.postWindowEvent(handle, hwnd);
  254. delete msg;
  255. }
  256. break;
  257. case WM_USER_WINDOW_DESTROY:
  258. {
  259. WindowHandle handle = { (uint16_t)_wparam };
  260. PostMessageA(m_hwnd[_wparam], WM_CLOSE, 0, 0);
  261. m_eventQueue.postWindowEvent(handle);
  262. DestroyWindow(m_hwnd[_wparam]);
  263. m_hwnd[_wparam] = 0;
  264. }
  265. break;
  266. case WM_USER_WINDOW_SET_TITLE:
  267. {
  268. Msg* msg = (Msg*)_lparam;
  269. SetWindowTextA(m_hwnd[_wparam], msg->m_title.c_str() );
  270. delete msg;
  271. }
  272. break;
  273. case WM_USER_WINDOW_SET_POS:
  274. {
  275. Msg* msg = (Msg*)_lparam;
  276. SetWindowPos(m_hwnd[_wparam], 0, msg->m_x, msg->m_y, 0, 0
  277. , SWP_NOACTIVATE
  278. | SWP_NOOWNERZORDER
  279. | SWP_NOSIZE
  280. );
  281. delete msg;
  282. }
  283. break;
  284. case WM_USER_WINDOW_SET_SIZE:
  285. {
  286. uint32_t width = GET_X_LPARAM(_lparam);
  287. uint32_t height = GET_Y_LPARAM(_lparam);
  288. adjust(m_hwnd[_wparam], width, height, true);
  289. }
  290. break;
  291. case WM_USER_WINDOW_TOGGLE_FRAME:
  292. {
  293. if (m_frame)
  294. {
  295. m_oldWidth = m_width;
  296. m_oldHeight = m_height;
  297. }
  298. adjust(m_hwnd[_wparam], m_oldWidth, m_oldHeight, !m_frame);
  299. }
  300. break;
  301. case WM_USER_WINDOW_MOUSE_LOCK:
  302. setMouseLock(m_hwnd[_wparam], !!_lparam);
  303. break;
  304. case WM_DESTROY:
  305. break;
  306. case WM_QUIT:
  307. case WM_CLOSE:
  308. if (_hwnd == m_hwnd[0])
  309. {
  310. m_exit = true;
  311. m_eventQueue.postExitEvent();
  312. }
  313. else
  314. {
  315. destroyWindow(findHandle(_hwnd) );
  316. }
  317. // Don't process message. Window will be destroyed later.
  318. return 0;
  319. case WM_SIZING:
  320. {
  321. WindowHandle handle = findHandle(_hwnd);
  322. if (isValid(handle)
  323. && ENTRY_WINDOW_FLAG_ASPECT_RATIO & m_flags[handle.idx])
  324. {
  325. RECT& rect = *(RECT*)_lparam;
  326. uint32_t width = rect.right - rect.left - m_frameWidth;
  327. uint32_t height = rect.bottom - rect.top - m_frameHeight;
  328. // Recalculate size according to aspect ratio
  329. switch (_wparam)
  330. {
  331. case WMSZ_LEFT:
  332. case WMSZ_RIGHT:
  333. {
  334. float aspectRatio = 1.0f/m_aspectRatio;
  335. width = bx::uint32_max(ENTRY_DEFAULT_WIDTH/4, width);
  336. height = uint32_t(float(width)*aspectRatio);
  337. }
  338. break;
  339. default:
  340. {
  341. float aspectRatio = m_aspectRatio;
  342. height = bx::uint32_max(ENTRY_DEFAULT_HEIGHT/4, height);
  343. width = uint32_t(float(height)*aspectRatio);
  344. }
  345. break;
  346. }
  347. // Recalculate position using different anchor points
  348. switch(_wparam)
  349. {
  350. case WMSZ_LEFT:
  351. case WMSZ_TOPLEFT:
  352. case WMSZ_BOTTOMLEFT:
  353. rect.left = rect.right - width - m_frameWidth;
  354. rect.bottom = rect.top + height + m_frameHeight;
  355. break;
  356. default:
  357. rect.right = rect.left + width + m_frameWidth;
  358. rect.bottom = rect.top + height + m_frameHeight;
  359. break;
  360. }
  361. m_eventQueue.postSizeEvent(findHandle(_hwnd), width, height);
  362. }
  363. }
  364. return 0;
  365. case WM_SIZE:
  366. {
  367. WindowHandle handle = findHandle(_hwnd);
  368. if (isValid(handle) )
  369. {
  370. uint32_t width = GET_X_LPARAM(_lparam);
  371. uint32_t height = GET_Y_LPARAM(_lparam);
  372. m_width = width;
  373. m_height = height;
  374. m_eventQueue.postSizeEvent(handle, m_width, m_height);
  375. }
  376. }
  377. break;
  378. case WM_SYSCOMMAND:
  379. switch (_wparam)
  380. {
  381. case SC_MINIMIZE:
  382. case SC_RESTORE:
  383. {
  384. HWND parent = GetWindow(_hwnd, GW_OWNER);
  385. if (NULL != parent)
  386. {
  387. PostMessage(parent, _id, _wparam, _lparam);
  388. }
  389. }
  390. }
  391. break;
  392. case WM_MOUSEMOVE:
  393. {
  394. int32_t mx = GET_X_LPARAM(_lparam);
  395. int32_t my = GET_Y_LPARAM(_lparam);
  396. if (_hwnd == m_mouseLock)
  397. {
  398. mx -= m_mx;
  399. my -= m_my;
  400. if (0 == mx
  401. && 0 == my)
  402. {
  403. break;
  404. }
  405. setMousePos(_hwnd, m_mx, m_my);
  406. }
  407. m_eventQueue.postMouseEvent(findHandle(_hwnd), mx, my, m_mz);
  408. }
  409. break;
  410. case WM_MOUSEWHEEL:
  411. {
  412. POINT pt = { GET_X_LPARAM(_lparam), GET_Y_LPARAM(_lparam) };
  413. ScreenToClient(_hwnd, &pt);
  414. int32_t mx = pt.x;
  415. int32_t my = pt.y;
  416. m_mz += GET_WHEEL_DELTA_WPARAM(_wparam);
  417. m_eventQueue.postMouseEvent(findHandle(_hwnd), mx, my, m_mz);
  418. }
  419. break;
  420. case WM_LBUTTONDOWN:
  421. case WM_LBUTTONUP:
  422. case WM_LBUTTONDBLCLK:
  423. {
  424. int32_t mx = GET_X_LPARAM(_lparam);
  425. int32_t my = GET_Y_LPARAM(_lparam);
  426. m_eventQueue.postMouseEvent(findHandle(_hwnd), mx, my, m_mz, MouseButton::Left, _id == WM_LBUTTONDOWN);
  427. }
  428. break;
  429. case WM_MBUTTONDOWN:
  430. case WM_MBUTTONUP:
  431. case WM_MBUTTONDBLCLK:
  432. {
  433. int32_t mx = GET_X_LPARAM(_lparam);
  434. int32_t my = GET_Y_LPARAM(_lparam);
  435. m_eventQueue.postMouseEvent(findHandle(_hwnd), mx, my, m_mz, MouseButton::Middle, _id == WM_MBUTTONDOWN);
  436. }
  437. break;
  438. case WM_RBUTTONUP:
  439. case WM_RBUTTONDOWN:
  440. case WM_RBUTTONDBLCLK:
  441. {
  442. int32_t mx = GET_X_LPARAM(_lparam);
  443. int32_t my = GET_Y_LPARAM(_lparam);
  444. m_eventQueue.postMouseEvent(findHandle(_hwnd), mx, my, m_mz, MouseButton::Right, _id == WM_RBUTTONDOWN);
  445. }
  446. break;
  447. case WM_KEYDOWN:
  448. case WM_SYSKEYDOWN:
  449. case WM_KEYUP:
  450. case WM_SYSKEYUP:
  451. {
  452. uint8_t modifiers = translateKeyModifiers();
  453. Key::Enum key = translateKey(_wparam);
  454. WindowHandle handle = findHandle(_hwnd);
  455. if (Key::Print == key
  456. && 0x3 == ( (uint32_t)(_lparam)>>30) )
  457. {
  458. // VK_SNAPSHOT doesn't generate keydown event. Fire on down event when previous
  459. // key state bit is set to 1 and transition state bit is set to 1.
  460. //
  461. // http://msdn.microsoft.com/en-us/library/windows/desktop/ms646280%28v=vs.85%29.aspx
  462. m_eventQueue.postKeyEvent(handle, key, modifiers, true);
  463. }
  464. m_eventQueue.postKeyEvent(handle, key, modifiers, _id == WM_KEYDOWN || _id == WM_SYSKEYDOWN);
  465. }
  466. break;
  467. default:
  468. break;
  469. }
  470. }
  471. return DefWindowProc(_hwnd, _id, _wparam, _lparam);
  472. }
  473. WindowHandle findHandle(HWND _hwnd)
  474. {
  475. bx::LwMutexScope scope(m_lock);
  476. for (uint32_t ii = 0, num = m_windowAlloc.getNumHandles(); ii < num; ++ii)
  477. {
  478. uint16_t idx = m_windowAlloc.getHandleAt(ii);
  479. if (_hwnd == m_hwnd[idx])
  480. {
  481. WindowHandle handle = { idx };
  482. return handle;
  483. }
  484. }
  485. WindowHandle invalid = { UINT16_MAX };
  486. return invalid;
  487. }
  488. void clear(HWND _hwnd)
  489. {
  490. RECT rect;
  491. GetWindowRect(_hwnd, &rect);
  492. HBRUSH brush = CreateSolidBrush(RGB(0, 0, 0) );
  493. HDC hdc = GetDC(_hwnd);
  494. SelectObject(hdc, brush);
  495. FillRect(hdc, &rect, brush);
  496. }
  497. void adjust(HWND _hwnd, uint32_t _width, uint32_t _height, bool _windowFrame)
  498. {
  499. m_width = _width;
  500. m_height = _height;
  501. m_aspectRatio = float(_width)/float(_height);
  502. ShowWindow(_hwnd, SW_SHOWNORMAL);
  503. RECT rect;
  504. RECT newrect = {0, 0, (LONG)_width, (LONG)_height};
  505. DWORD style = WS_POPUP|WS_SYSMENU;
  506. if (m_frame)
  507. {
  508. GetWindowRect(_hwnd, &m_rect);
  509. m_style = GetWindowLong(_hwnd, GWL_STYLE);
  510. }
  511. if (_windowFrame)
  512. {
  513. rect = m_rect;
  514. style = m_style;
  515. }
  516. else
  517. {
  518. #if defined(__MINGW32__)
  519. rect = m_rect;
  520. style = m_style;
  521. #else
  522. HMONITOR monitor = MonitorFromWindow(_hwnd, MONITOR_DEFAULTTONEAREST);
  523. MONITORINFO mi;
  524. mi.cbSize = sizeof(mi);
  525. GetMonitorInfo(monitor, &mi);
  526. newrect = mi.rcMonitor;
  527. rect = mi.rcMonitor;
  528. #endif // !defined(__MINGW__)
  529. }
  530. SetWindowLong(_hwnd, GWL_STYLE, style);
  531. uint32_t prewidth = newrect.right - newrect.left;
  532. uint32_t preheight = newrect.bottom - newrect.top;
  533. AdjustWindowRect(&newrect, style, FALSE);
  534. m_frameWidth = (newrect.right - newrect.left) - prewidth;
  535. m_frameHeight = (newrect.bottom - newrect.top) - preheight;
  536. UpdateWindow(_hwnd);
  537. if (rect.left == -32000
  538. || rect.top == -32000)
  539. {
  540. rect.left = 0;
  541. rect.top = 0;
  542. }
  543. int32_t left = rect.left;
  544. int32_t top = rect.top;
  545. int32_t width = (newrect.right-newrect.left);
  546. int32_t height = (newrect.bottom-newrect.top);
  547. if (!_windowFrame)
  548. {
  549. float aspectRatio = 1.0f/m_aspectRatio;
  550. width = bx::uint32_max(ENTRY_DEFAULT_WIDTH/4, width);
  551. height = uint32_t(float(width)*aspectRatio);
  552. left = newrect.left+(newrect.right-newrect.left-width)/2;
  553. top = newrect.top+(newrect.bottom-newrect.top-height)/2;
  554. }
  555. HWND parent = GetWindow(_hwnd, GW_OWNER);
  556. if (NULL != parent)
  557. {
  558. if (_windowFrame)
  559. {
  560. SetWindowPos(parent
  561. , HWND_TOP
  562. , -32000
  563. , -32000
  564. , 0
  565. , 0
  566. , SWP_SHOWWINDOW
  567. );
  568. }
  569. else
  570. {
  571. SetWindowPos(parent
  572. , HWND_TOP
  573. , newrect.left
  574. , newrect.top
  575. , newrect.right-newrect.left
  576. , newrect.bottom-newrect.top
  577. , SWP_SHOWWINDOW
  578. );
  579. }
  580. }
  581. SetWindowPos(_hwnd
  582. , HWND_TOP
  583. , left
  584. , top
  585. , width
  586. , height
  587. , SWP_SHOWWINDOW
  588. );
  589. ShowWindow(_hwnd, SW_RESTORE);
  590. m_frame = _windowFrame;
  591. }
  592. void setMousePos(HWND _hwnd, int32_t _mx, int32_t _my)
  593. {
  594. POINT pt = { _mx, _my };
  595. ClientToScreen(_hwnd, &pt);
  596. SetCursorPos(pt.x, pt.y);
  597. }
  598. void setMouseLock(HWND _hwnd, bool _lock)
  599. {
  600. if (_hwnd != m_mouseLock)
  601. {
  602. if (_lock)
  603. {
  604. m_mx = m_width/2;
  605. m_my = m_height/2;
  606. ShowCursor(false);
  607. setMousePos(_hwnd, m_mx, m_my);
  608. }
  609. else
  610. {
  611. setMousePos(_hwnd, m_mx, m_my);
  612. ShowCursor(true);
  613. }
  614. m_mouseLock = _hwnd;
  615. }
  616. }
  617. static LRESULT CALLBACK wndProc(HWND _hwnd, UINT _id, WPARAM _wparam, LPARAM _lparam);
  618. EventQueue m_eventQueue;
  619. bx::LwMutex m_lock;
  620. bx::HandleAllocT<ENTRY_CONFIG_MAX_WINDOWS> m_windowAlloc;
  621. HWND m_hwnd[ENTRY_CONFIG_MAX_WINDOWS];
  622. uint32_t m_flags[ENTRY_CONFIG_MAX_WINDOWS];
  623. RECT m_rect;
  624. DWORD m_style;
  625. uint32_t m_width;
  626. uint32_t m_height;
  627. uint32_t m_oldWidth;
  628. uint32_t m_oldHeight;
  629. uint32_t m_frameWidth;
  630. uint32_t m_frameHeight;
  631. float m_aspectRatio;
  632. int32_t m_mx;
  633. int32_t m_my;
  634. int32_t m_mz;
  635. bool m_frame;
  636. HWND m_mouseLock;
  637. bool m_init;
  638. bool m_exit;
  639. };
  640. static Context s_ctx;
  641. LRESULT CALLBACK Context::wndProc(HWND _hwnd, UINT _id, WPARAM _wparam, LPARAM _lparam)
  642. {
  643. return s_ctx.process(_hwnd, _id, _wparam, _lparam);
  644. }
  645. const Event* poll()
  646. {
  647. return s_ctx.m_eventQueue.poll();
  648. }
  649. const Event* poll(WindowHandle _handle)
  650. {
  651. return s_ctx.m_eventQueue.poll(_handle);
  652. }
  653. void release(const Event* _event)
  654. {
  655. s_ctx.m_eventQueue.release(_event);
  656. }
  657. WindowHandle createWindow(int32_t _x, int32_t _y, uint32_t _width, uint32_t _height, uint32_t _flags, const char* _title)
  658. {
  659. bx::LwMutexScope scope(s_ctx.m_lock);
  660. WindowHandle handle = { s_ctx.m_windowAlloc.alloc() };
  661. if (UINT16_MAX != handle.idx)
  662. {
  663. Msg* msg = new Msg;
  664. msg->m_x = _x;
  665. msg->m_y = _y;
  666. msg->m_width = _width;
  667. msg->m_height = _height;
  668. msg->m_title = _title;
  669. msg->m_flags = _flags;
  670. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_CREATE, handle.idx, (LPARAM)msg);
  671. }
  672. return handle;
  673. }
  674. void destroyWindow(WindowHandle _handle)
  675. {
  676. if (UINT16_MAX != _handle.idx)
  677. {
  678. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_DESTROY, _handle.idx, 0);
  679. bx::LwMutexScope scope(s_ctx.m_lock);
  680. s_ctx.m_windowAlloc.free(_handle.idx);
  681. }
  682. }
  683. void setWindowPos(WindowHandle _handle, int32_t _x, int32_t _y)
  684. {
  685. Msg* msg = new Msg;
  686. msg->m_x = _x;
  687. msg->m_y = _y;
  688. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_SET_POS, _handle.idx, (LPARAM)msg);
  689. }
  690. void setWindowSize(WindowHandle _handle, uint32_t _width, uint32_t _height)
  691. {
  692. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_SET_SIZE, _handle.idx, (_height<<16) | (_width&0xffff) );
  693. }
  694. void setWindowTitle(WindowHandle _handle, const char* _title)
  695. {
  696. Msg* msg = new Msg;
  697. msg->m_title = _title;
  698. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_SET_TITLE, _handle.idx, (LPARAM)msg);
  699. }
  700. void toggleWindowFrame(WindowHandle _handle)
  701. {
  702. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_TOGGLE_FRAME, _handle.idx, 0);
  703. }
  704. void setMouseLock(WindowHandle _handle, bool _lock)
  705. {
  706. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_MOUSE_LOCK, _handle.idx, _lock);
  707. }
  708. int32_t MainThreadEntry::threadFunc(void* _userData)
  709. {
  710. MainThreadEntry* self = (MainThreadEntry*)_userData;
  711. int32_t result = main(self->m_argc, self->m_argv);
  712. PostMessage(s_ctx.m_hwnd[0], WM_QUIT, 0, 0);
  713. return result;
  714. }
  715. } // namespace entry
  716. int main(int _argc, char** _argv)
  717. {
  718. using namespace entry;
  719. return s_ctx.run(_argc, _argv);
  720. }
  721. #endif // BX_PLATFORM_WINDOWS