entry_windows.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. /*
  2. * Copyright 2011-2013 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include <bx/bx.h>
  6. #if BX_PLATFORM_WINDOWS
  7. #include "entry_p.h"
  8. #include <bx/countof.h>
  9. #include <bx/uint32_t.h>
  10. #include <bx/thread.h>
  11. #include <windowsx.h>
  12. #define DEFAULT_WIDTH 1280
  13. #define DEFAULT_HEIGHT 720
  14. #define WM_USER_SET_WINDOW_SIZE (WM_USER+0)
  15. #define WM_USER_TOGGLE_WINDOW_FRAME (WM_USER+1)
  16. #define WM_USER_MOUSE_LOCK (WM_USER+2)
  17. extern int _main_(int _argc, char** _argv);
  18. namespace entry
  19. {
  20. struct TranslateKeyModifiers
  21. {
  22. int m_vk;
  23. Modifier::Enum m_modifier;
  24. };
  25. static const TranslateKeyModifiers s_translateKeyModifiers[8] =
  26. {
  27. { VK_LMENU, Modifier::LeftAlt },
  28. { VK_RMENU, Modifier::RightAlt },
  29. { VK_LCONTROL, Modifier::LeftCtrl },
  30. { VK_RCONTROL, Modifier::RightCtrl },
  31. { VK_LSHIFT, Modifier::LeftShift },
  32. { VK_RSHIFT, Modifier::RightShift },
  33. { VK_LWIN, Modifier::LeftMeta },
  34. { VK_RWIN, Modifier::RightMeta },
  35. };
  36. static uint8_t translateKeyModifiers()
  37. {
  38. uint8_t modifiers = 0;
  39. for (uint32_t ii = 0; ii < countof(s_translateKeyModifiers); ++ii)
  40. {
  41. const TranslateKeyModifiers& tkm = s_translateKeyModifiers[ii];
  42. modifiers |= 0 > GetKeyState(tkm.m_vk) ? tkm.m_modifier : Modifier::None;
  43. }
  44. return modifiers;
  45. }
  46. static uint8_t s_translateKey[256];
  47. static Key::Enum translateKey(WPARAM _wparam)
  48. {
  49. return (Key::Enum)s_translateKey[_wparam&0xff];
  50. }
  51. struct MainThreadEntry
  52. {
  53. int m_argc;
  54. char** m_argv;
  55. static int32_t threadFunc(void* _userData);
  56. };
  57. struct Context
  58. {
  59. Context()
  60. : m_frame(true)
  61. , m_init(false)
  62. , m_exit(false)
  63. {
  64. memset(s_translateKey, 0, sizeof(s_translateKey) );
  65. s_translateKey[VK_ESCAPE] = Key::Esc;
  66. s_translateKey[VK_RETURN] = Key::Return;
  67. s_translateKey[VK_TAB] = Key::Tab;
  68. s_translateKey[VK_BACK] = Key::Backspace;
  69. s_translateKey[VK_SPACE] = Key::Space;
  70. s_translateKey[VK_UP] = Key::Up;
  71. s_translateKey[VK_DOWN] = Key::Down;
  72. s_translateKey[VK_LEFT] = Key::Left;
  73. s_translateKey[VK_RIGHT] = Key::Right;
  74. s_translateKey[VK_PRIOR] = Key::PageUp;
  75. s_translateKey[VK_NEXT] = Key::PageUp;
  76. s_translateKey[VK_HOME] = Key::Home;
  77. s_translateKey[VK_END] = Key::End;
  78. s_translateKey[VK_SNAPSHOT] = Key::Print;
  79. s_translateKey[VK_OEM_PLUS] = Key::Plus;
  80. s_translateKey[VK_OEM_MINUS] = Key::Minus;
  81. s_translateKey[VK_F1] = Key::F1;
  82. s_translateKey[VK_F2] = Key::F2;
  83. s_translateKey[VK_F3] = Key::F3;
  84. s_translateKey[VK_F4] = Key::F4;
  85. s_translateKey[VK_F5] = Key::F5;
  86. s_translateKey[VK_F6] = Key::F6;
  87. s_translateKey[VK_F7] = Key::F7;
  88. s_translateKey[VK_F8] = Key::F8;
  89. s_translateKey[VK_F9] = Key::F9;
  90. s_translateKey[VK_F10] = Key::F10;
  91. s_translateKey[VK_F11] = Key::F11;
  92. s_translateKey[VK_F12] = Key::F12;
  93. s_translateKey[VK_NUMPAD0] = Key::NumPad0;
  94. s_translateKey[VK_NUMPAD1] = Key::NumPad1;
  95. s_translateKey[VK_NUMPAD2] = Key::NumPad2;
  96. s_translateKey[VK_NUMPAD3] = Key::NumPad3;
  97. s_translateKey[VK_NUMPAD4] = Key::NumPad4;
  98. s_translateKey[VK_NUMPAD5] = Key::NumPad5;
  99. s_translateKey[VK_NUMPAD6] = Key::NumPad6;
  100. s_translateKey[VK_NUMPAD7] = Key::NumPad7;
  101. s_translateKey[VK_NUMPAD8] = Key::NumPad8;
  102. s_translateKey[VK_NUMPAD9] = Key::NumPad9;
  103. s_translateKey['0'] = Key::Key0;
  104. s_translateKey['1'] = Key::Key1;
  105. s_translateKey['2'] = Key::Key2;
  106. s_translateKey['3'] = Key::Key3;
  107. s_translateKey['4'] = Key::Key4;
  108. s_translateKey['5'] = Key::Key5;
  109. s_translateKey['6'] = Key::Key6;
  110. s_translateKey['7'] = Key::Key7;
  111. s_translateKey['8'] = Key::Key8;
  112. s_translateKey['9'] = Key::Key9;
  113. s_translateKey['A'] = Key::KeyA;
  114. s_translateKey['B'] = Key::KeyB;
  115. s_translateKey['C'] = Key::KeyC;
  116. s_translateKey['D'] = Key::KeyD;
  117. s_translateKey['E'] = Key::KeyE;
  118. s_translateKey['F'] = Key::KeyF;
  119. s_translateKey['G'] = Key::KeyG;
  120. s_translateKey['H'] = Key::KeyH;
  121. s_translateKey['I'] = Key::KeyI;
  122. s_translateKey['J'] = Key::KeyJ;
  123. s_translateKey['K'] = Key::KeyK;
  124. s_translateKey['L'] = Key::KeyL;
  125. s_translateKey['M'] = Key::KeyM;
  126. s_translateKey['N'] = Key::KeyN;
  127. s_translateKey['O'] = Key::KeyO;
  128. s_translateKey['P'] = Key::KeyP;
  129. s_translateKey['Q'] = Key::KeyQ;
  130. s_translateKey['R'] = Key::KeyR;
  131. s_translateKey['S'] = Key::KeyS;
  132. s_translateKey['T'] = Key::KeyT;
  133. s_translateKey['U'] = Key::KeyU;
  134. s_translateKey['V'] = Key::KeyV;
  135. s_translateKey['W'] = Key::KeyW;
  136. s_translateKey['X'] = Key::KeyX;
  137. s_translateKey['Y'] = Key::KeyY;
  138. s_translateKey['Z'] = Key::KeyZ;
  139. }
  140. int32_t main(int _argc, char** _argv)
  141. {
  142. HINSTANCE instance = (HINSTANCE)GetModuleHandle(NULL);
  143. WNDCLASSEX wnd;
  144. memset(&wnd, 0, sizeof(wnd) );
  145. wnd.cbSize = sizeof(wnd);
  146. wnd.lpfnWndProc = DefWindowProc;
  147. wnd.hInstance = instance;
  148. wnd.hIcon = LoadIcon(instance, IDI_APPLICATION);
  149. wnd.hCursor = LoadCursor(instance, IDC_ARROW);
  150. wnd.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  151. wnd.lpszClassName = "bgfx_letterbox";
  152. wnd.hIconSm = LoadIcon(instance, IDI_APPLICATION);
  153. RegisterClassExA(&wnd);
  154. memset(&wnd, 0, sizeof(wnd) );
  155. wnd.cbSize = sizeof(wnd);
  156. wnd.style = CS_HREDRAW | CS_VREDRAW;
  157. wnd.lpfnWndProc = wndProc;
  158. wnd.hInstance = instance;
  159. wnd.hIcon = LoadIcon(instance, IDI_APPLICATION);
  160. wnd.hCursor = LoadCursor(instance, IDC_ARROW);
  161. wnd.lpszClassName = "bgfx";
  162. wnd.hIconSm = LoadIcon(instance, IDI_APPLICATION);
  163. RegisterClassExA(&wnd);
  164. HWND hwnd = CreateWindowA("bgfx_letterbox"
  165. , "BGFX"
  166. , WS_POPUP|WS_SYSMENU
  167. , -32000
  168. , -32000
  169. , 0
  170. , 0
  171. , NULL
  172. , NULL
  173. , instance
  174. , 0
  175. );
  176. m_hwnd = CreateWindowA("bgfx"
  177. , "BGFX"
  178. , WS_OVERLAPPEDWINDOW|WS_VISIBLE
  179. , 0
  180. , 0
  181. , DEFAULT_WIDTH
  182. , DEFAULT_HEIGHT
  183. , hwnd
  184. , NULL
  185. , instance
  186. , 0
  187. );
  188. bgfx::winSetHwnd(m_hwnd);
  189. adjust(DEFAULT_WIDTH, DEFAULT_HEIGHT, true);
  190. m_width = DEFAULT_WIDTH;
  191. m_height = DEFAULT_HEIGHT;
  192. m_oldWidth = DEFAULT_WIDTH;
  193. m_oldHeight = DEFAULT_HEIGHT;
  194. MainThreadEntry mte;
  195. mte.m_argc = _argc;
  196. mte.m_argv = _argv;
  197. bx::Thread thread;
  198. thread.init(mte.threadFunc, &mte);
  199. m_init = true;
  200. m_eventQueue.postSizeEvent(m_width, m_height);
  201. MSG msg;
  202. msg.message = WM_NULL;
  203. while (!m_exit)
  204. {
  205. WaitMessage();
  206. while (0 != PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE) )
  207. {
  208. TranslateMessage(&msg);
  209. DispatchMessage(&msg);
  210. }
  211. }
  212. thread.shutdown();
  213. DestroyWindow(m_hwnd);
  214. DestroyWindow(hwnd);
  215. return 0;
  216. }
  217. LRESULT process(HWND _hwnd, UINT _id, WPARAM _wparam, LPARAM _lparam)
  218. {
  219. if (m_init)
  220. {
  221. switch (_id)
  222. {
  223. case WM_USER_SET_WINDOW_SIZE:
  224. {
  225. uint32_t width = GET_X_LPARAM(_lparam);
  226. uint32_t height = GET_Y_LPARAM(_lparam);
  227. adjust(width, height, true);
  228. }
  229. break;
  230. case WM_USER_TOGGLE_WINDOW_FRAME:
  231. {
  232. if (m_frame)
  233. {
  234. m_oldWidth = m_width;
  235. m_oldHeight = m_height;
  236. }
  237. adjust(m_oldWidth, m_oldHeight, !m_frame);
  238. }
  239. break;
  240. case WM_USER_MOUSE_LOCK:
  241. setMouseLock(!!_lparam);
  242. break;
  243. case WM_DESTROY:
  244. break;
  245. case WM_QUIT:
  246. case WM_CLOSE:
  247. m_exit = true;
  248. m_eventQueue.postExitEvent();
  249. break;
  250. case WM_SIZING:
  251. {
  252. RECT& rect = *(RECT*)_lparam;
  253. uint32_t width = rect.right - rect.left - m_frameWidth;
  254. uint32_t height = rect.bottom - rect.top - m_frameHeight;
  255. //Recalculate size according to aspect ratio
  256. switch (_wparam)
  257. {
  258. case WMSZ_LEFT:
  259. case WMSZ_RIGHT:
  260. {
  261. float aspectRatio = 1.0f/m_aspectRatio;
  262. width = bx::uint32_max(DEFAULT_WIDTH/4, width);
  263. height = uint32_t(float(width)*aspectRatio);
  264. }
  265. break;
  266. default:
  267. {
  268. float aspectRatio = m_aspectRatio;
  269. height = bx::uint32_max(DEFAULT_HEIGHT/4, height);
  270. width = uint32_t(float(height)*aspectRatio);
  271. }
  272. break;
  273. }
  274. //Recalculate position using different anchor points
  275. switch(_wparam)
  276. {
  277. case WMSZ_LEFT:
  278. case WMSZ_TOPLEFT:
  279. case WMSZ_BOTTOMLEFT:
  280. rect.left = rect.right - width - m_frameWidth;
  281. rect.bottom = rect.top + height + m_frameHeight;
  282. break;
  283. default:
  284. rect.right = rect.left + width + m_frameWidth;
  285. rect.bottom = rect.top + height + m_frameHeight;
  286. break;
  287. }
  288. m_eventQueue.postSizeEvent(m_width, m_height);
  289. }
  290. return 0;
  291. case WM_SIZE:
  292. {
  293. uint32_t width = GET_X_LPARAM(_lparam);
  294. uint32_t height = GET_Y_LPARAM(_lparam);
  295. m_width = width;
  296. m_height = height;
  297. m_eventQueue.postSizeEvent(m_width, m_height);
  298. }
  299. break;
  300. case WM_SYSCOMMAND:
  301. switch (_wparam)
  302. {
  303. case SC_MINIMIZE:
  304. case SC_RESTORE:
  305. {
  306. HWND parent = GetWindow(_hwnd, GW_OWNER);
  307. if (NULL != parent)
  308. {
  309. PostMessage(parent, _id, _wparam, _lparam);
  310. }
  311. }
  312. }
  313. break;
  314. case WM_MOUSEMOVE:
  315. {
  316. int32_t mx = GET_X_LPARAM(_lparam);
  317. int32_t my = GET_Y_LPARAM(_lparam);
  318. if (m_mouseLock)
  319. {
  320. mx -= m_mx;
  321. my -= m_my;
  322. if (0 == mx
  323. && 0 == my)
  324. {
  325. break;
  326. }
  327. setMousePos(m_mx, m_my);
  328. }
  329. m_eventQueue.postMouseEvent(mx, my);
  330. }
  331. break;
  332. case WM_LBUTTONDOWN:
  333. case WM_LBUTTONUP:
  334. case WM_LBUTTONDBLCLK:
  335. {
  336. int32_t mx = GET_X_LPARAM(_lparam);
  337. int32_t my = GET_Y_LPARAM(_lparam);
  338. m_eventQueue.postMouseEvent(mx, my, MouseButton::Left, _id == WM_LBUTTONDOWN);
  339. }
  340. break;
  341. case WM_MBUTTONDOWN:
  342. case WM_MBUTTONUP:
  343. case WM_MBUTTONDBLCLK:
  344. {
  345. int32_t mx = GET_X_LPARAM(_lparam);
  346. int32_t my = GET_Y_LPARAM(_lparam);
  347. m_eventQueue.postMouseEvent(mx, my, MouseButton::Middle, _id == WM_MBUTTONDOWN);
  348. }
  349. break;
  350. case WM_RBUTTONUP:
  351. case WM_RBUTTONDOWN:
  352. case WM_RBUTTONDBLCLK:
  353. {
  354. int32_t mx = GET_X_LPARAM(_lparam);
  355. int32_t my = GET_Y_LPARAM(_lparam);
  356. m_eventQueue.postMouseEvent(mx, my, MouseButton::Right, _id == WM_RBUTTONDOWN);
  357. }
  358. break;
  359. case WM_KEYDOWN:
  360. case WM_SYSKEYDOWN:
  361. case WM_KEYUP:
  362. case WM_SYSKEYUP:
  363. {
  364. uint8_t modifiers = translateKeyModifiers();
  365. Key::Enum key = translateKey(_wparam);
  366. m_eventQueue.postKeyEvent(key, modifiers, _id == WM_KEYDOWN || _id == WM_SYSKEYDOWN);
  367. }
  368. break;
  369. default:
  370. break;
  371. }
  372. }
  373. return DefWindowProc(_hwnd, _id, _wparam, _lparam);
  374. }
  375. void adjust(uint32_t _width, uint32_t _height, bool _windowFrame)
  376. {
  377. m_width = _width;
  378. m_height = _height;
  379. m_aspectRatio = float(_width)/float(_height);
  380. ShowWindow(m_hwnd, SW_SHOWNORMAL);
  381. RECT rect;
  382. RECT newrect = {0, 0, (LONG)_width, (LONG)_height};
  383. DWORD style = WS_POPUP|WS_SYSMENU;
  384. if (m_frame)
  385. {
  386. GetWindowRect(m_hwnd, &m_rect);
  387. m_style = GetWindowLong(m_hwnd, GWL_STYLE);
  388. }
  389. if (_windowFrame)
  390. {
  391. rect = m_rect;
  392. style = m_style;
  393. }
  394. else
  395. {
  396. #if defined(__MINGW32__)
  397. rect = m_rect;
  398. style = m_style;
  399. #else
  400. HMONITOR monitor = MonitorFromWindow(m_hwnd, MONITOR_DEFAULTTONEAREST);
  401. MONITORINFO mi;
  402. mi.cbSize = sizeof(mi);
  403. GetMonitorInfo(monitor, &mi);
  404. newrect = mi.rcMonitor;
  405. rect = mi.rcMonitor;
  406. #endif // !defined(__MINGW__)
  407. }
  408. SetWindowLong(m_hwnd, GWL_STYLE, style);
  409. uint32_t prewidth = newrect.right - newrect.left;
  410. uint32_t preheight = newrect.bottom - newrect.top;
  411. AdjustWindowRect(&newrect, style, FALSE);
  412. m_frameWidth = (newrect.right - newrect.left) - prewidth;
  413. m_frameHeight = (newrect.bottom - newrect.top) - preheight;
  414. UpdateWindow(m_hwnd);
  415. if (rect.left == -32000
  416. || rect.top == -32000)
  417. {
  418. rect.left = 0;
  419. rect.top = 0;
  420. }
  421. int32_t left = rect.left;
  422. int32_t top = rect.top;
  423. int32_t width = (newrect.right-newrect.left);
  424. int32_t height = (newrect.bottom-newrect.top);
  425. if (!_windowFrame)
  426. {
  427. float aspectRatio = 1.0f/m_aspectRatio;
  428. width = bx::uint32_max(DEFAULT_WIDTH/4, width);
  429. height = uint32_t(float(width)*aspectRatio);
  430. left = newrect.left+(newrect.right-newrect.left-width)/2;
  431. top = newrect.top+(newrect.bottom-newrect.top-height)/2;
  432. }
  433. HWND parent = GetWindow(m_hwnd, GW_OWNER);
  434. if (NULL != parent)
  435. {
  436. if (_windowFrame)
  437. {
  438. SetWindowPos(parent
  439. , HWND_TOP
  440. , -32000
  441. , -32000
  442. , 0
  443. , 0
  444. , SWP_SHOWWINDOW
  445. );
  446. }
  447. else
  448. {
  449. SetWindowPos(parent
  450. , HWND_TOP
  451. , newrect.left
  452. , newrect.top
  453. , newrect.right-newrect.left
  454. , newrect.bottom-newrect.top
  455. , SWP_SHOWWINDOW
  456. );
  457. }
  458. }
  459. SetWindowPos(m_hwnd
  460. , HWND_TOP
  461. , left
  462. , top
  463. , width
  464. , height
  465. , SWP_SHOWWINDOW
  466. );
  467. ShowWindow(m_hwnd, SW_RESTORE);
  468. m_frame = _windowFrame;
  469. }
  470. void setMousePos(int32_t _mx, int32_t _my)
  471. {
  472. POINT pt = { _mx, _my };
  473. ClientToScreen(m_hwnd, &pt);
  474. SetCursorPos(pt.x, pt.y);
  475. }
  476. void setMouseLock(bool _lock)
  477. {
  478. if (_lock != m_mouseLock)
  479. {
  480. if (_lock)
  481. {
  482. m_mx = m_width/2;
  483. m_my = m_height/2;
  484. ShowCursor(false);
  485. setMousePos(m_mx, m_my);
  486. }
  487. else
  488. {
  489. setMousePos(m_mx, m_my);
  490. ShowCursor(true);
  491. }
  492. m_mouseLock = _lock;
  493. }
  494. }
  495. static LRESULT CALLBACK wndProc(HWND _hwnd, UINT _id, WPARAM _wparam, LPARAM _lparam);
  496. EventQueue m_eventQueue;
  497. HWND m_hwnd;
  498. RECT m_rect;
  499. DWORD m_style;
  500. uint32_t m_width;
  501. uint32_t m_height;
  502. uint32_t m_oldWidth;
  503. uint32_t m_oldHeight;
  504. uint32_t m_frameWidth;
  505. uint32_t m_frameHeight;
  506. float m_aspectRatio;
  507. int32_t m_mx;
  508. int32_t m_my;
  509. bool m_frame;
  510. bool m_mouseLock;
  511. bool m_init;
  512. bool m_exit;
  513. };
  514. static Context s_ctx;
  515. LRESULT CALLBACK Context::wndProc(HWND _hwnd, UINT _id, WPARAM _wparam, LPARAM _lparam)
  516. {
  517. return s_ctx.process(_hwnd, _id, _wparam, _lparam);
  518. }
  519. const Event* poll()
  520. {
  521. return s_ctx.m_eventQueue.poll();
  522. }
  523. void release(const Event* _event)
  524. {
  525. s_ctx.m_eventQueue.release(_event);
  526. }
  527. void setWindowSize(uint32_t _width, uint32_t _height)
  528. {
  529. PostMessage(s_ctx.m_hwnd, WM_USER_SET_WINDOW_SIZE, 0, (_height<<16) | (_width&0xffff) );
  530. }
  531. void toggleWindowFrame()
  532. {
  533. PostMessage(s_ctx.m_hwnd, WM_USER_TOGGLE_WINDOW_FRAME, 0, 0);
  534. }
  535. void setMouseLock(bool _lock)
  536. {
  537. PostMessage(s_ctx.m_hwnd, WM_USER_MOUSE_LOCK, 0, _lock);
  538. }
  539. int32_t MainThreadEntry::threadFunc(void* _userData)
  540. {
  541. MainThreadEntry* self = (MainThreadEntry*)_userData;
  542. int32_t result = _main_(self->m_argc, self->m_argv);
  543. PostMessage(s_ctx.m_hwnd, WM_QUIT, 0, 0);
  544. return result;
  545. }
  546. } // namespace entry
  547. int main(int _argc, char** _argv)
  548. {
  549. using namespace entry;
  550. return s_ctx.main(_argc, _argv);
  551. }
  552. #endif // BX_PLATFORM_WINDOWS