entry_windows.cpp 14 KB

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