entry_windows.cpp 20 KB

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