entry_windows.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  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. m_hwnd[_wparam] = 0;
  260. }
  261. break;
  262. case WM_USER_WINDOW_SET_TITLE:
  263. {
  264. Msg* msg = (Msg*)_lparam;
  265. SetWindowTextA(m_hwnd[_wparam], msg->m_title.c_str() );
  266. delete msg;
  267. }
  268. break;
  269. case WM_USER_WINDOW_SET_POS:
  270. {
  271. Msg* msg = (Msg*)_lparam;
  272. SetWindowPos(m_hwnd[_wparam], 0, msg->m_x, msg->m_y, 0, 0
  273. , SWP_NOACTIVATE
  274. | SWP_NOOWNERZORDER
  275. | SWP_NOSIZE
  276. );
  277. delete msg;
  278. }
  279. break;
  280. case WM_USER_WINDOW_SET_SIZE:
  281. {
  282. uint32_t width = GET_X_LPARAM(_lparam);
  283. uint32_t height = GET_Y_LPARAM(_lparam);
  284. adjust(m_hwnd[_wparam], width, height, true);
  285. }
  286. break;
  287. case WM_USER_WINDOW_TOGGLE_FRAME:
  288. {
  289. if (m_frame)
  290. {
  291. m_oldWidth = m_width;
  292. m_oldHeight = m_height;
  293. }
  294. adjust(m_hwnd[_wparam], m_oldWidth, m_oldHeight, !m_frame);
  295. }
  296. break;
  297. case WM_USER_WINDOW_MOUSE_LOCK:
  298. setMouseLock(m_hwnd[_wparam], !!_lparam);
  299. break;
  300. case WM_DESTROY:
  301. break;
  302. case WM_QUIT:
  303. case WM_CLOSE:
  304. if (_hwnd == m_hwnd[0])
  305. {
  306. m_exit = true;
  307. m_eventQueue.postExitEvent();
  308. }
  309. else
  310. {
  311. destroyWindow(findHandle(_hwnd) );
  312. }
  313. break;
  314. case WM_SIZING:
  315. {
  316. WindowHandle handle = findHandle(_hwnd);
  317. if (isValid(handle)
  318. && ENTRY_WINDOW_FLAG_ASPECT_RATIO & m_flags[handle.idx])
  319. {
  320. RECT& rect = *(RECT*)_lparam;
  321. uint32_t width = rect.right - rect.left - m_frameWidth;
  322. uint32_t height = rect.bottom - rect.top - m_frameHeight;
  323. // Recalculate size according to aspect ratio
  324. switch (_wparam)
  325. {
  326. case WMSZ_LEFT:
  327. case WMSZ_RIGHT:
  328. {
  329. float aspectRatio = 1.0f/m_aspectRatio;
  330. width = bx::uint32_max(ENTRY_DEFAULT_WIDTH/4, width);
  331. height = uint32_t(float(width)*aspectRatio);
  332. }
  333. break;
  334. default:
  335. {
  336. float aspectRatio = m_aspectRatio;
  337. height = bx::uint32_max(ENTRY_DEFAULT_HEIGHT/4, height);
  338. width = uint32_t(float(height)*aspectRatio);
  339. }
  340. break;
  341. }
  342. // Recalculate position using different anchor points
  343. switch(_wparam)
  344. {
  345. case WMSZ_LEFT:
  346. case WMSZ_TOPLEFT:
  347. case WMSZ_BOTTOMLEFT:
  348. rect.left = rect.right - width - m_frameWidth;
  349. rect.bottom = rect.top + height + m_frameHeight;
  350. break;
  351. default:
  352. rect.right = rect.left + width + m_frameWidth;
  353. rect.bottom = rect.top + height + m_frameHeight;
  354. break;
  355. }
  356. m_eventQueue.postSizeEvent(findHandle(_hwnd), width, height);
  357. }
  358. }
  359. return 0;
  360. case WM_SIZE:
  361. {
  362. WindowHandle handle = findHandle(_hwnd);
  363. if (isValid(handle) )
  364. {
  365. uint32_t width = GET_X_LPARAM(_lparam);
  366. uint32_t height = GET_Y_LPARAM(_lparam);
  367. m_width = width;
  368. m_height = height;
  369. m_eventQueue.postSizeEvent(handle, m_width, m_height);
  370. }
  371. }
  372. break;
  373. case WM_SYSCOMMAND:
  374. switch (_wparam)
  375. {
  376. case SC_MINIMIZE:
  377. case SC_RESTORE:
  378. {
  379. HWND parent = GetWindow(_hwnd, GW_OWNER);
  380. if (NULL != parent)
  381. {
  382. PostMessage(parent, _id, _wparam, _lparam);
  383. }
  384. }
  385. }
  386. break;
  387. case WM_MOUSEMOVE:
  388. {
  389. int32_t mx = GET_X_LPARAM(_lparam);
  390. int32_t my = GET_Y_LPARAM(_lparam);
  391. if (_hwnd == m_mouseLock)
  392. {
  393. mx -= m_mx;
  394. my -= m_my;
  395. if (0 == mx
  396. && 0 == my)
  397. {
  398. break;
  399. }
  400. setMousePos(_hwnd, m_mx, m_my);
  401. }
  402. m_eventQueue.postMouseEvent(findHandle(_hwnd), mx, my, m_mz);
  403. }
  404. break;
  405. case WM_MOUSEWHEEL:
  406. {
  407. POINT pt = { GET_X_LPARAM(_lparam), GET_Y_LPARAM(_lparam) };
  408. ScreenToClient(_hwnd, &pt);
  409. int32_t mx = pt.x;
  410. int32_t my = pt.y;
  411. m_mz += GET_WHEEL_DELTA_WPARAM(_wparam);
  412. m_eventQueue.postMouseEvent(findHandle(_hwnd), mx, my, m_mz);
  413. }
  414. break;
  415. case WM_LBUTTONDOWN:
  416. case WM_LBUTTONUP:
  417. case WM_LBUTTONDBLCLK:
  418. {
  419. int32_t mx = GET_X_LPARAM(_lparam);
  420. int32_t my = GET_Y_LPARAM(_lparam);
  421. m_eventQueue.postMouseEvent(findHandle(_hwnd), mx, my, m_mz, MouseButton::Left, _id == WM_LBUTTONDOWN);
  422. }
  423. break;
  424. case WM_MBUTTONDOWN:
  425. case WM_MBUTTONUP:
  426. case WM_MBUTTONDBLCLK:
  427. {
  428. int32_t mx = GET_X_LPARAM(_lparam);
  429. int32_t my = GET_Y_LPARAM(_lparam);
  430. m_eventQueue.postMouseEvent(findHandle(_hwnd), mx, my, m_mz, MouseButton::Middle, _id == WM_MBUTTONDOWN);
  431. }
  432. break;
  433. case WM_RBUTTONUP:
  434. case WM_RBUTTONDOWN:
  435. case WM_RBUTTONDBLCLK:
  436. {
  437. int32_t mx = GET_X_LPARAM(_lparam);
  438. int32_t my = GET_Y_LPARAM(_lparam);
  439. m_eventQueue.postMouseEvent(findHandle(_hwnd), mx, my, m_mz, MouseButton::Right, _id == WM_RBUTTONDOWN);
  440. }
  441. break;
  442. case WM_KEYDOWN:
  443. case WM_SYSKEYDOWN:
  444. case WM_KEYUP:
  445. case WM_SYSKEYUP:
  446. {
  447. uint8_t modifiers = translateKeyModifiers();
  448. Key::Enum key = translateKey(_wparam);
  449. WindowHandle handle = findHandle(_hwnd);
  450. if (Key::Print == key
  451. && 0x3 == ( (uint32_t)(_lparam)>>30) )
  452. {
  453. // VK_SNAPSHOT doesn't generate keydown event. Fire on down event when previous
  454. // key state bit is set to 1 and transition state bit is set to 1.
  455. //
  456. // http://msdn.microsoft.com/en-us/library/windows/desktop/ms646280%28v=vs.85%29.aspx
  457. m_eventQueue.postKeyEvent(handle, key, modifiers, true);
  458. }
  459. m_eventQueue.postKeyEvent(handle, key, modifiers, _id == WM_KEYDOWN || _id == WM_SYSKEYDOWN);
  460. }
  461. break;
  462. default:
  463. break;
  464. }
  465. }
  466. return DefWindowProc(_hwnd, _id, _wparam, _lparam);
  467. }
  468. WindowHandle findHandle(HWND _hwnd)
  469. {
  470. bx::LwMutexScope scope(m_lock);
  471. for (uint32_t ii = 0, num = m_windowAlloc.getNumHandles(); ii < num; ++ii)
  472. {
  473. uint16_t idx = m_windowAlloc.getHandleAt(ii);
  474. if (_hwnd == m_hwnd[idx])
  475. {
  476. WindowHandle handle = { idx };
  477. return handle;
  478. }
  479. }
  480. WindowHandle invalid = { UINT16_MAX };
  481. return invalid;
  482. }
  483. void clear(HWND _hwnd)
  484. {
  485. RECT rect;
  486. GetWindowRect(_hwnd, &rect);
  487. HBRUSH brush = CreateSolidBrush(RGB(0, 0, 0) );
  488. HDC hdc = GetDC(_hwnd);
  489. SelectObject(hdc, brush);
  490. FillRect(hdc, &rect, brush);
  491. }
  492. void adjust(HWND _hwnd, uint32_t _width, uint32_t _height, bool _windowFrame)
  493. {
  494. m_width = _width;
  495. m_height = _height;
  496. m_aspectRatio = float(_width)/float(_height);
  497. ShowWindow(_hwnd, SW_SHOWNORMAL);
  498. RECT rect;
  499. RECT newrect = {0, 0, (LONG)_width, (LONG)_height};
  500. DWORD style = WS_POPUP|WS_SYSMENU;
  501. if (m_frame)
  502. {
  503. GetWindowRect(_hwnd, &m_rect);
  504. m_style = GetWindowLong(_hwnd, GWL_STYLE);
  505. }
  506. if (_windowFrame)
  507. {
  508. rect = m_rect;
  509. style = m_style;
  510. }
  511. else
  512. {
  513. #if defined(__MINGW32__)
  514. rect = m_rect;
  515. style = m_style;
  516. #else
  517. HMONITOR monitor = MonitorFromWindow(_hwnd, MONITOR_DEFAULTTONEAREST);
  518. MONITORINFO mi;
  519. mi.cbSize = sizeof(mi);
  520. GetMonitorInfo(monitor, &mi);
  521. newrect = mi.rcMonitor;
  522. rect = mi.rcMonitor;
  523. #endif // !defined(__MINGW__)
  524. }
  525. SetWindowLong(_hwnd, GWL_STYLE, style);
  526. uint32_t prewidth = newrect.right - newrect.left;
  527. uint32_t preheight = newrect.bottom - newrect.top;
  528. AdjustWindowRect(&newrect, style, FALSE);
  529. m_frameWidth = (newrect.right - newrect.left) - prewidth;
  530. m_frameHeight = (newrect.bottom - newrect.top) - preheight;
  531. UpdateWindow(_hwnd);
  532. if (rect.left == -32000
  533. || rect.top == -32000)
  534. {
  535. rect.left = 0;
  536. rect.top = 0;
  537. }
  538. int32_t left = rect.left;
  539. int32_t top = rect.top;
  540. int32_t width = (newrect.right-newrect.left);
  541. int32_t height = (newrect.bottom-newrect.top);
  542. if (!_windowFrame)
  543. {
  544. float aspectRatio = 1.0f/m_aspectRatio;
  545. width = bx::uint32_max(ENTRY_DEFAULT_WIDTH/4, width);
  546. height = uint32_t(float(width)*aspectRatio);
  547. left = newrect.left+(newrect.right-newrect.left-width)/2;
  548. top = newrect.top+(newrect.bottom-newrect.top-height)/2;
  549. }
  550. HWND parent = GetWindow(_hwnd, GW_OWNER);
  551. if (NULL != parent)
  552. {
  553. if (_windowFrame)
  554. {
  555. SetWindowPos(parent
  556. , HWND_TOP
  557. , -32000
  558. , -32000
  559. , 0
  560. , 0
  561. , SWP_SHOWWINDOW
  562. );
  563. }
  564. else
  565. {
  566. SetWindowPos(parent
  567. , HWND_TOP
  568. , newrect.left
  569. , newrect.top
  570. , newrect.right-newrect.left
  571. , newrect.bottom-newrect.top
  572. , SWP_SHOWWINDOW
  573. );
  574. }
  575. }
  576. SetWindowPos(_hwnd
  577. , HWND_TOP
  578. , left
  579. , top
  580. , width
  581. , height
  582. , SWP_SHOWWINDOW
  583. );
  584. ShowWindow(_hwnd, SW_RESTORE);
  585. m_frame = _windowFrame;
  586. }
  587. void setMousePos(HWND _hwnd, int32_t _mx, int32_t _my)
  588. {
  589. POINT pt = { _mx, _my };
  590. ClientToScreen(_hwnd, &pt);
  591. SetCursorPos(pt.x, pt.y);
  592. }
  593. void setMouseLock(HWND _hwnd, bool _lock)
  594. {
  595. if (_hwnd != m_mouseLock)
  596. {
  597. if (_lock)
  598. {
  599. m_mx = m_width/2;
  600. m_my = m_height/2;
  601. ShowCursor(false);
  602. setMousePos(_hwnd, m_mx, m_my);
  603. }
  604. else
  605. {
  606. setMousePos(_hwnd, m_mx, m_my);
  607. ShowCursor(true);
  608. }
  609. m_mouseLock = _hwnd;
  610. }
  611. }
  612. static LRESULT CALLBACK wndProc(HWND _hwnd, UINT _id, WPARAM _wparam, LPARAM _lparam);
  613. EventQueue m_eventQueue;
  614. bx::LwMutex m_lock;
  615. bx::HandleAllocT<ENTRY_CONFIG_MAX_WINDOWS> m_windowAlloc;
  616. HWND m_hwnd[ENTRY_CONFIG_MAX_WINDOWS];
  617. uint32_t m_flags[ENTRY_CONFIG_MAX_WINDOWS];
  618. RECT m_rect;
  619. DWORD m_style;
  620. uint32_t m_width;
  621. uint32_t m_height;
  622. uint32_t m_oldWidth;
  623. uint32_t m_oldHeight;
  624. uint32_t m_frameWidth;
  625. uint32_t m_frameHeight;
  626. float m_aspectRatio;
  627. int32_t m_mx;
  628. int32_t m_my;
  629. int32_t m_mz;
  630. bool m_frame;
  631. HWND m_mouseLock;
  632. bool m_init;
  633. bool m_exit;
  634. };
  635. static Context s_ctx;
  636. LRESULT CALLBACK Context::wndProc(HWND _hwnd, UINT _id, WPARAM _wparam, LPARAM _lparam)
  637. {
  638. return s_ctx.process(_hwnd, _id, _wparam, _lparam);
  639. }
  640. const Event* poll()
  641. {
  642. return s_ctx.m_eventQueue.poll();
  643. }
  644. const Event* poll(WindowHandle _handle)
  645. {
  646. return s_ctx.m_eventQueue.poll(_handle);
  647. }
  648. void release(const Event* _event)
  649. {
  650. s_ctx.m_eventQueue.release(_event);
  651. }
  652. WindowHandle createWindow(int32_t _x, int32_t _y, uint32_t _width, uint32_t _height, uint32_t _flags, const char* _title)
  653. {
  654. bx::LwMutexScope scope(s_ctx.m_lock);
  655. WindowHandle handle = { s_ctx.m_windowAlloc.alloc() };
  656. if (UINT16_MAX != handle.idx)
  657. {
  658. Msg* msg = new Msg;
  659. msg->m_x = _x;
  660. msg->m_y = _y;
  661. msg->m_width = _width;
  662. msg->m_height = _height;
  663. msg->m_title = _title;
  664. msg->m_flags = _flags;
  665. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_CREATE, handle.idx, (LPARAM)msg);
  666. }
  667. return handle;
  668. }
  669. void destroyWindow(WindowHandle _handle)
  670. {
  671. if (UINT16_MAX != _handle.idx)
  672. {
  673. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_DESTROY, _handle.idx, 0);
  674. bx::LwMutexScope scope(s_ctx.m_lock);
  675. s_ctx.m_windowAlloc.free(_handle.idx);
  676. }
  677. }
  678. void setWindowPos(WindowHandle _handle, int32_t _x, int32_t _y)
  679. {
  680. Msg* msg = new Msg;
  681. msg->m_x = _x;
  682. msg->m_y = _y;
  683. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_SET_POS, _handle.idx, (LPARAM)msg);
  684. }
  685. void setWindowSize(WindowHandle _handle, uint32_t _width, uint32_t _height)
  686. {
  687. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_SET_SIZE, _handle.idx, (_height<<16) | (_width&0xffff) );
  688. }
  689. void setWindowTitle(WindowHandle _handle, const char* _title)
  690. {
  691. Msg* msg = new Msg;
  692. msg->m_title = _title;
  693. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_SET_TITLE, _handle.idx, (LPARAM)msg);
  694. }
  695. void toggleWindowFrame(WindowHandle _handle)
  696. {
  697. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_TOGGLE_FRAME, _handle.idx, 0);
  698. }
  699. void setMouseLock(WindowHandle _handle, bool _lock)
  700. {
  701. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_MOUSE_LOCK, _handle.idx, _lock);
  702. }
  703. int32_t MainThreadEntry::threadFunc(void* _userData)
  704. {
  705. MainThreadEntry* self = (MainThreadEntry*)_userData;
  706. int32_t result = main(self->m_argc, self->m_argv);
  707. PostMessage(s_ctx.m_hwnd[0], WM_QUIT, 0, 0);
  708. return result;
  709. }
  710. } // namespace entry
  711. int main(int _argc, char** _argv)
  712. {
  713. using namespace entry;
  714. return s_ctx.run(_argc, _argv);
  715. }
  716. #endif // BX_PLATFORM_WINDOWS