entry_windows.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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. #include <tinystl/allocator.h>
  14. #include <tinystl/string.h>
  15. enum
  16. {
  17. WM_USER_WINDOW_CREATE = WM_USER,
  18. WM_USER_WINDOW_DESTROY,
  19. WM_USER_WINDOW_SET_TITLE,
  20. WM_USER_WINDOW_SET_POS,
  21. WM_USER_WINDOW_SET_SIZE,
  22. WM_USER_WINDOW_TOGGLE_FRAME,
  23. WM_USER_WINDOW_MOUSE_LOCK,
  24. };
  25. namespace entry
  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['0'] = Key::Key0;
  130. s_translateKey['1'] = Key::Key1;
  131. s_translateKey['2'] = Key::Key2;
  132. s_translateKey['3'] = Key::Key3;
  133. s_translateKey['4'] = Key::Key4;
  134. s_translateKey['5'] = Key::Key5;
  135. s_translateKey['6'] = Key::Key6;
  136. s_translateKey['7'] = Key::Key7;
  137. s_translateKey['8'] = Key::Key8;
  138. s_translateKey['9'] = Key::Key9;
  139. s_translateKey['A'] = Key::KeyA;
  140. s_translateKey['B'] = Key::KeyB;
  141. s_translateKey['C'] = Key::KeyC;
  142. s_translateKey['D'] = Key::KeyD;
  143. s_translateKey['E'] = Key::KeyE;
  144. s_translateKey['F'] = Key::KeyF;
  145. s_translateKey['G'] = Key::KeyG;
  146. s_translateKey['H'] = Key::KeyH;
  147. s_translateKey['I'] = Key::KeyI;
  148. s_translateKey['J'] = Key::KeyJ;
  149. s_translateKey['K'] = Key::KeyK;
  150. s_translateKey['L'] = Key::KeyL;
  151. s_translateKey['M'] = Key::KeyM;
  152. s_translateKey['N'] = Key::KeyN;
  153. s_translateKey['O'] = Key::KeyO;
  154. s_translateKey['P'] = Key::KeyP;
  155. s_translateKey['Q'] = Key::KeyQ;
  156. s_translateKey['R'] = Key::KeyR;
  157. s_translateKey['S'] = Key::KeyS;
  158. s_translateKey['T'] = Key::KeyT;
  159. s_translateKey['U'] = Key::KeyU;
  160. s_translateKey['V'] = Key::KeyV;
  161. s_translateKey['W'] = Key::KeyW;
  162. s_translateKey['X'] = Key::KeyX;
  163. s_translateKey['Y'] = Key::KeyY;
  164. s_translateKey['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] = ENTRY_WINDOW_FLAG_ASPECT_RATIO;
  195. bgfx::winSetHwnd(m_hwnd[0]);
  196. adjust(m_hwnd[0], ENTRY_DEFAULT_WIDTH, ENTRY_DEFAULT_HEIGHT, true);
  197. clear(m_hwnd[0]);
  198. m_width = ENTRY_DEFAULT_WIDTH;
  199. m_height = ENTRY_DEFAULT_HEIGHT;
  200. m_oldWidth = ENTRY_DEFAULT_WIDTH;
  201. m_oldHeight = ENTRY_DEFAULT_HEIGHT;
  202. MainThreadEntry mte;
  203. mte.m_argc = _argc;
  204. mte.m_argv = _argv;
  205. bx::Thread thread;
  206. thread.init(mte.threadFunc, &mte);
  207. m_init = true;
  208. m_eventQueue.postSizeEvent(findHandle(m_hwnd[0]), m_width, m_height);
  209. MSG msg;
  210. msg.message = WM_NULL;
  211. while (!m_exit)
  212. {
  213. WaitMessage();
  214. while (0 != PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE) )
  215. {
  216. TranslateMessage(&msg);
  217. DispatchMessage(&msg);
  218. }
  219. }
  220. thread.shutdown();
  221. DestroyWindow(m_hwnd[0]);
  222. return 0;
  223. }
  224. LRESULT process(HWND _hwnd, UINT _id, WPARAM _wparam, LPARAM _lparam)
  225. {
  226. if (m_init)
  227. {
  228. switch (_id)
  229. {
  230. case WM_USER_WINDOW_CREATE:
  231. {
  232. Msg* msg = (Msg*)_lparam;
  233. HWND hwnd = CreateWindowA("bgfx"
  234. , msg->m_title.c_str()
  235. , WS_OVERLAPPEDWINDOW|WS_VISIBLE
  236. , msg->m_x
  237. , msg->m_y
  238. , msg->m_width
  239. , msg->m_height
  240. , m_hwnd[0]
  241. , NULL
  242. , (HINSTANCE)GetModuleHandle(NULL)
  243. , 0
  244. );
  245. clear(hwnd);
  246. m_hwnd[_wparam] = hwnd;
  247. m_flags[_wparam] = msg->m_flags;
  248. WindowHandle handle = { (uint16_t)_wparam };
  249. m_eventQueue.postSizeEvent(handle, msg->m_width, msg->m_height);
  250. m_eventQueue.postWindowEvent(handle, hwnd);
  251. delete msg;
  252. }
  253. break;
  254. case WM_USER_WINDOW_DESTROY:
  255. {
  256. WindowHandle handle = { (uint16_t)_wparam };
  257. PostMessageA(m_hwnd[_wparam], WM_CLOSE, 0, 0);
  258. m_eventQueue.postWindowEvent(handle);
  259. DestroyWindow(m_hwnd[_wparam]);
  260. m_hwnd[_wparam] = 0;
  261. }
  262. break;
  263. case WM_USER_WINDOW_SET_TITLE:
  264. {
  265. Msg* msg = (Msg*)_lparam;
  266. SetWindowTextA(m_hwnd[_wparam], msg->m_title.c_str() );
  267. delete msg;
  268. }
  269. break;
  270. case WM_USER_WINDOW_SET_POS:
  271. {
  272. Msg* msg = (Msg*)_lparam;
  273. SetWindowPos(m_hwnd[_wparam], 0, msg->m_x, msg->m_y, 0, 0
  274. , SWP_NOACTIVATE
  275. | SWP_NOOWNERZORDER
  276. | SWP_NOSIZE
  277. );
  278. delete msg;
  279. }
  280. break;
  281. case WM_USER_WINDOW_SET_SIZE:
  282. {
  283. uint32_t width = GET_X_LPARAM(_lparam);
  284. uint32_t height = GET_Y_LPARAM(_lparam);
  285. adjust(m_hwnd[_wparam], width, height, true);
  286. }
  287. break;
  288. case WM_USER_WINDOW_TOGGLE_FRAME:
  289. {
  290. if (m_frame)
  291. {
  292. m_oldWidth = m_width;
  293. m_oldHeight = m_height;
  294. }
  295. adjust(m_hwnd[_wparam], m_oldWidth, m_oldHeight, !m_frame);
  296. }
  297. break;
  298. case WM_USER_WINDOW_MOUSE_LOCK:
  299. setMouseLock(m_hwnd[_wparam], !!_lparam);
  300. break;
  301. case WM_DESTROY:
  302. break;
  303. case WM_QUIT:
  304. case WM_CLOSE:
  305. if (_hwnd == m_hwnd[0])
  306. {
  307. m_exit = true;
  308. m_eventQueue.postExitEvent();
  309. }
  310. else
  311. {
  312. destroyWindow(findHandle(_hwnd) );
  313. }
  314. // Don't process message. Window will be destroyed later.
  315. return 0;
  316. case WM_SIZING:
  317. {
  318. WindowHandle handle = findHandle(_hwnd);
  319. if (isValid(handle)
  320. && ENTRY_WINDOW_FLAG_ASPECT_RATIO & m_flags[handle.idx])
  321. {
  322. RECT& rect = *(RECT*)_lparam;
  323. uint32_t width = rect.right - rect.left - m_frameWidth;
  324. uint32_t height = rect.bottom - rect.top - m_frameHeight;
  325. // Recalculate size according to aspect ratio
  326. switch (_wparam)
  327. {
  328. case WMSZ_LEFT:
  329. case WMSZ_RIGHT:
  330. {
  331. float aspectRatio = 1.0f/m_aspectRatio;
  332. width = bx::uint32_max(ENTRY_DEFAULT_WIDTH/4, width);
  333. height = uint32_t(float(width)*aspectRatio);
  334. }
  335. break;
  336. default:
  337. {
  338. float aspectRatio = m_aspectRatio;
  339. height = bx::uint32_max(ENTRY_DEFAULT_HEIGHT/4, height);
  340. width = uint32_t(float(height)*aspectRatio);
  341. }
  342. break;
  343. }
  344. // Recalculate position using different anchor points
  345. switch(_wparam)
  346. {
  347. case WMSZ_LEFT:
  348. case WMSZ_TOPLEFT:
  349. case WMSZ_BOTTOMLEFT:
  350. rect.left = rect.right - width - m_frameWidth;
  351. rect.bottom = rect.top + height + m_frameHeight;
  352. break;
  353. default:
  354. rect.right = rect.left + width + m_frameWidth;
  355. rect.bottom = rect.top + height + m_frameHeight;
  356. break;
  357. }
  358. m_eventQueue.postSizeEvent(findHandle(_hwnd), width, height);
  359. }
  360. }
  361. return 0;
  362. case WM_SIZE:
  363. {
  364. WindowHandle handle = findHandle(_hwnd);
  365. if (isValid(handle) )
  366. {
  367. uint32_t width = GET_X_LPARAM(_lparam);
  368. uint32_t height = GET_Y_LPARAM(_lparam);
  369. m_width = width;
  370. m_height = height;
  371. m_eventQueue.postSizeEvent(handle, m_width, m_height);
  372. }
  373. }
  374. break;
  375. case WM_SYSCOMMAND:
  376. switch (_wparam)
  377. {
  378. case SC_MINIMIZE:
  379. case SC_RESTORE:
  380. {
  381. HWND parent = GetWindow(_hwnd, GW_OWNER);
  382. if (NULL != parent)
  383. {
  384. PostMessage(parent, _id, _wparam, _lparam);
  385. }
  386. }
  387. }
  388. break;
  389. case WM_MOUSEMOVE:
  390. {
  391. int32_t mx = GET_X_LPARAM(_lparam);
  392. int32_t my = GET_Y_LPARAM(_lparam);
  393. if (_hwnd == m_mouseLock)
  394. {
  395. mx -= m_mx;
  396. my -= m_my;
  397. if (0 == mx
  398. && 0 == my)
  399. {
  400. break;
  401. }
  402. setMousePos(_hwnd, m_mx, m_my);
  403. }
  404. m_eventQueue.postMouseEvent(findHandle(_hwnd), mx, my, m_mz);
  405. }
  406. break;
  407. case WM_MOUSEWHEEL:
  408. {
  409. POINT pt = { GET_X_LPARAM(_lparam), GET_Y_LPARAM(_lparam) };
  410. ScreenToClient(_hwnd, &pt);
  411. int32_t mx = pt.x;
  412. int32_t my = pt.y;
  413. m_mz += GET_WHEEL_DELTA_WPARAM(_wparam);
  414. m_eventQueue.postMouseEvent(findHandle(_hwnd), mx, my, m_mz);
  415. }
  416. break;
  417. case WM_LBUTTONDOWN:
  418. case WM_LBUTTONUP:
  419. case WM_LBUTTONDBLCLK:
  420. {
  421. int32_t mx = GET_X_LPARAM(_lparam);
  422. int32_t my = GET_Y_LPARAM(_lparam);
  423. m_eventQueue.postMouseEvent(findHandle(_hwnd), mx, my, m_mz, MouseButton::Left, _id == WM_LBUTTONDOWN);
  424. }
  425. break;
  426. case WM_MBUTTONDOWN:
  427. case WM_MBUTTONUP:
  428. case WM_MBUTTONDBLCLK:
  429. {
  430. int32_t mx = GET_X_LPARAM(_lparam);
  431. int32_t my = GET_Y_LPARAM(_lparam);
  432. m_eventQueue.postMouseEvent(findHandle(_hwnd), mx, my, m_mz, MouseButton::Middle, _id == WM_MBUTTONDOWN);
  433. }
  434. break;
  435. case WM_RBUTTONUP:
  436. case WM_RBUTTONDOWN:
  437. case WM_RBUTTONDBLCLK:
  438. {
  439. int32_t mx = GET_X_LPARAM(_lparam);
  440. int32_t my = GET_Y_LPARAM(_lparam);
  441. m_eventQueue.postMouseEvent(findHandle(_hwnd), mx, my, m_mz, MouseButton::Right, _id == WM_RBUTTONDOWN);
  442. }
  443. break;
  444. case WM_KEYDOWN:
  445. case WM_SYSKEYDOWN:
  446. case WM_KEYUP:
  447. case WM_SYSKEYUP:
  448. {
  449. uint8_t modifiers = translateKeyModifiers();
  450. Key::Enum key = translateKey(_wparam);
  451. WindowHandle handle = findHandle(_hwnd);
  452. if (Key::Print == key
  453. && 0x3 == ( (uint32_t)(_lparam)>>30) )
  454. {
  455. // VK_SNAPSHOT doesn't generate keydown event. Fire on down event when previous
  456. // key state bit is set to 1 and transition state bit is set to 1.
  457. //
  458. // http://msdn.microsoft.com/en-us/library/windows/desktop/ms646280%28v=vs.85%29.aspx
  459. m_eventQueue.postKeyEvent(handle, key, modifiers, true);
  460. }
  461. m_eventQueue.postKeyEvent(handle, key, modifiers, _id == WM_KEYDOWN || _id == WM_SYSKEYDOWN);
  462. }
  463. break;
  464. default:
  465. break;
  466. }
  467. }
  468. return DefWindowProc(_hwnd, _id, _wparam, _lparam);
  469. }
  470. WindowHandle findHandle(HWND _hwnd)
  471. {
  472. bx::LwMutexScope scope(m_lock);
  473. for (uint32_t ii = 0, num = m_windowAlloc.getNumHandles(); ii < num; ++ii)
  474. {
  475. uint16_t idx = m_windowAlloc.getHandleAt(ii);
  476. if (_hwnd == m_hwnd[idx])
  477. {
  478. WindowHandle handle = { idx };
  479. return handle;
  480. }
  481. }
  482. WindowHandle invalid = { UINT16_MAX };
  483. return invalid;
  484. }
  485. void clear(HWND _hwnd)
  486. {
  487. RECT rect;
  488. GetWindowRect(_hwnd, &rect);
  489. HBRUSH brush = CreateSolidBrush(RGB(0, 0, 0) );
  490. HDC hdc = GetDC(_hwnd);
  491. SelectObject(hdc, brush);
  492. FillRect(hdc, &rect, brush);
  493. }
  494. void adjust(HWND _hwnd, uint32_t _width, uint32_t _height, bool _windowFrame)
  495. {
  496. m_width = _width;
  497. m_height = _height;
  498. m_aspectRatio = float(_width)/float(_height);
  499. ShowWindow(_hwnd, SW_SHOWNORMAL);
  500. RECT rect;
  501. RECT newrect = {0, 0, (LONG)_width, (LONG)_height};
  502. DWORD style = WS_POPUP|WS_SYSMENU;
  503. if (m_frame)
  504. {
  505. GetWindowRect(_hwnd, &m_rect);
  506. m_style = GetWindowLong(_hwnd, GWL_STYLE);
  507. }
  508. if (_windowFrame)
  509. {
  510. rect = m_rect;
  511. style = m_style;
  512. }
  513. else
  514. {
  515. #if defined(__MINGW32__)
  516. rect = m_rect;
  517. style = m_style;
  518. #else
  519. HMONITOR monitor = MonitorFromWindow(_hwnd, MONITOR_DEFAULTTONEAREST);
  520. MONITORINFO mi;
  521. mi.cbSize = sizeof(mi);
  522. GetMonitorInfo(monitor, &mi);
  523. newrect = mi.rcMonitor;
  524. rect = mi.rcMonitor;
  525. #endif // !defined(__MINGW__)
  526. }
  527. SetWindowLong(_hwnd, GWL_STYLE, style);
  528. uint32_t prewidth = newrect.right - newrect.left;
  529. uint32_t preheight = newrect.bottom - newrect.top;
  530. AdjustWindowRect(&newrect, style, FALSE);
  531. m_frameWidth = (newrect.right - newrect.left) - prewidth;
  532. m_frameHeight = (newrect.bottom - newrect.top) - preheight;
  533. UpdateWindow(_hwnd);
  534. if (rect.left == -32000
  535. || rect.top == -32000)
  536. {
  537. rect.left = 0;
  538. rect.top = 0;
  539. }
  540. int32_t left = rect.left;
  541. int32_t top = rect.top;
  542. int32_t width = (newrect.right-newrect.left);
  543. int32_t height = (newrect.bottom-newrect.top);
  544. if (!_windowFrame)
  545. {
  546. float aspectRatio = 1.0f/m_aspectRatio;
  547. width = bx::uint32_max(ENTRY_DEFAULT_WIDTH/4, width);
  548. height = uint32_t(float(width)*aspectRatio);
  549. left = newrect.left+(newrect.right-newrect.left-width)/2;
  550. top = newrect.top+(newrect.bottom-newrect.top-height)/2;
  551. }
  552. HWND parent = GetWindow(_hwnd, GW_OWNER);
  553. if (NULL != parent)
  554. {
  555. if (_windowFrame)
  556. {
  557. SetWindowPos(parent
  558. , HWND_TOP
  559. , -32000
  560. , -32000
  561. , 0
  562. , 0
  563. , SWP_SHOWWINDOW
  564. );
  565. }
  566. else
  567. {
  568. SetWindowPos(parent
  569. , HWND_TOP
  570. , newrect.left
  571. , newrect.top
  572. , newrect.right-newrect.left
  573. , newrect.bottom-newrect.top
  574. , SWP_SHOWWINDOW
  575. );
  576. }
  577. }
  578. SetWindowPos(_hwnd
  579. , HWND_TOP
  580. , left
  581. , top
  582. , width
  583. , height
  584. , SWP_SHOWWINDOW
  585. );
  586. ShowWindow(_hwnd, SW_RESTORE);
  587. m_frame = _windowFrame;
  588. }
  589. void setMousePos(HWND _hwnd, int32_t _mx, int32_t _my)
  590. {
  591. POINT pt = { _mx, _my };
  592. ClientToScreen(_hwnd, &pt);
  593. SetCursorPos(pt.x, pt.y);
  594. }
  595. void setMouseLock(HWND _hwnd, bool _lock)
  596. {
  597. if (_hwnd != m_mouseLock)
  598. {
  599. if (_lock)
  600. {
  601. m_mx = m_width/2;
  602. m_my = m_height/2;
  603. ShowCursor(false);
  604. setMousePos(_hwnd, m_mx, m_my);
  605. }
  606. else
  607. {
  608. setMousePos(_hwnd, m_mx, m_my);
  609. ShowCursor(true);
  610. }
  611. m_mouseLock = _hwnd;
  612. }
  613. }
  614. static LRESULT CALLBACK wndProc(HWND _hwnd, UINT _id, WPARAM _wparam, LPARAM _lparam);
  615. EventQueue m_eventQueue;
  616. bx::LwMutex m_lock;
  617. bx::HandleAllocT<ENTRY_CONFIG_MAX_WINDOWS> m_windowAlloc;
  618. HWND m_hwnd[ENTRY_CONFIG_MAX_WINDOWS];
  619. uint32_t m_flags[ENTRY_CONFIG_MAX_WINDOWS];
  620. RECT m_rect;
  621. DWORD m_style;
  622. uint32_t m_width;
  623. uint32_t m_height;
  624. uint32_t m_oldWidth;
  625. uint32_t m_oldHeight;
  626. uint32_t m_frameWidth;
  627. uint32_t m_frameHeight;
  628. float m_aspectRatio;
  629. int32_t m_mx;
  630. int32_t m_my;
  631. int32_t m_mz;
  632. bool m_frame;
  633. HWND m_mouseLock;
  634. bool m_init;
  635. bool m_exit;
  636. };
  637. static Context s_ctx;
  638. LRESULT CALLBACK Context::wndProc(HWND _hwnd, UINT _id, WPARAM _wparam, LPARAM _lparam)
  639. {
  640. return s_ctx.process(_hwnd, _id, _wparam, _lparam);
  641. }
  642. const Event* poll()
  643. {
  644. return s_ctx.m_eventQueue.poll();
  645. }
  646. const Event* poll(WindowHandle _handle)
  647. {
  648. return s_ctx.m_eventQueue.poll(_handle);
  649. }
  650. void release(const Event* _event)
  651. {
  652. s_ctx.m_eventQueue.release(_event);
  653. }
  654. WindowHandle createWindow(int32_t _x, int32_t _y, uint32_t _width, uint32_t _height, uint32_t _flags, const char* _title)
  655. {
  656. bx::LwMutexScope scope(s_ctx.m_lock);
  657. WindowHandle handle = { s_ctx.m_windowAlloc.alloc() };
  658. if (UINT16_MAX != handle.idx)
  659. {
  660. Msg* msg = new Msg;
  661. msg->m_x = _x;
  662. msg->m_y = _y;
  663. msg->m_width = _width;
  664. msg->m_height = _height;
  665. msg->m_title = _title;
  666. msg->m_flags = _flags;
  667. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_CREATE, handle.idx, (LPARAM)msg);
  668. }
  669. return handle;
  670. }
  671. void destroyWindow(WindowHandle _handle)
  672. {
  673. if (UINT16_MAX != _handle.idx)
  674. {
  675. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_DESTROY, _handle.idx, 0);
  676. bx::LwMutexScope scope(s_ctx.m_lock);
  677. s_ctx.m_windowAlloc.free(_handle.idx);
  678. }
  679. }
  680. void setWindowPos(WindowHandle _handle, int32_t _x, int32_t _y)
  681. {
  682. Msg* msg = new Msg;
  683. msg->m_x = _x;
  684. msg->m_y = _y;
  685. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_SET_POS, _handle.idx, (LPARAM)msg);
  686. }
  687. void setWindowSize(WindowHandle _handle, uint32_t _width, uint32_t _height)
  688. {
  689. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_SET_SIZE, _handle.idx, (_height<<16) | (_width&0xffff) );
  690. }
  691. void setWindowTitle(WindowHandle _handle, const char* _title)
  692. {
  693. Msg* msg = new Msg;
  694. msg->m_title = _title;
  695. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_SET_TITLE, _handle.idx, (LPARAM)msg);
  696. }
  697. void toggleWindowFrame(WindowHandle _handle)
  698. {
  699. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_TOGGLE_FRAME, _handle.idx, 0);
  700. }
  701. void setMouseLock(WindowHandle _handle, bool _lock)
  702. {
  703. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_MOUSE_LOCK, _handle.idx, _lock);
  704. }
  705. int32_t MainThreadEntry::threadFunc(void* _userData)
  706. {
  707. MainThreadEntry* self = (MainThreadEntry*)_userData;
  708. int32_t result = main(self->m_argc, self->m_argv);
  709. PostMessage(s_ctx.m_hwnd[0], WM_QUIT, 0, 0);
  710. return result;
  711. }
  712. } // namespace entry
  713. int main(int _argc, char** _argv)
  714. {
  715. using namespace entry;
  716. return s_ctx.run(_argc, _argv);
  717. }
  718. #endif // BX_PLATFORM_WINDOWS