Win32Window.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. #ifdef _WIN32
  2. /*
  3. Copyright (c) 2012 Advanced Micro Devices, Inc.
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. //Originally written by Erwin Coumans
  14. #include "Win32Window.h"
  15. #include "OpenGLInclude.h"
  16. #include <wchar.h>
  17. static InternalData2* sData = 0;
  18. #include "Win32InternalWindowData.h"
  19. enum
  20. {
  21. INTERNAL_SHIFT_MODIFIER = 1,
  22. INTERNAL_ALT_MODIFIER = 2,
  23. INTERNAL_CONTROL_MODIFIER = 4,
  24. };
  25. void Win32Window::pumpMessage()
  26. {
  27. MSG msg;
  28. // check for messages
  29. //'if' instead of 'while' can make mainloop smoother.
  30. //@todo: use separate threads for input and rendering
  31. while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  32. {
  33. // handle or dispatch messages
  34. if (msg.message == WM_QUIT)
  35. {
  36. m_data->m_quit = TRUE;
  37. }
  38. else
  39. {
  40. TranslateMessage(&msg);
  41. DispatchMessage(&msg);
  42. }
  43. // gDemoApplication->displayCallback();
  44. };
  45. }
  46. int getSpecialKeyFromVirtualKeycode(int virtualKeyCode)
  47. {
  48. int keycode = -1;
  49. if (virtualKeyCode >= 'A' && virtualKeyCode <= 'Z')
  50. {
  51. return virtualKeyCode + 32; //todo: fix the ascii A vs a input
  52. }
  53. if (virtualKeyCode >= '0' && virtualKeyCode <= '9')
  54. {
  55. return virtualKeyCode;
  56. }
  57. switch (virtualKeyCode)
  58. {
  59. case VK_SPACE:
  60. {
  61. keycode = B3G_SPACE;
  62. break;
  63. }
  64. case VK_RETURN:
  65. {
  66. keycode = B3G_RETURN;
  67. break;
  68. };
  69. case VK_ESCAPE:
  70. {
  71. keycode = B3G_ESCAPE;
  72. break;
  73. };
  74. case VK_F1:
  75. {
  76. keycode = B3G_F1;
  77. break;
  78. }
  79. case VK_F2:
  80. {
  81. keycode = B3G_F2;
  82. break;
  83. }
  84. case VK_F3:
  85. {
  86. keycode = B3G_F3;
  87. break;
  88. }
  89. case VK_F4:
  90. {
  91. keycode = B3G_F4;
  92. break;
  93. }
  94. case VK_F5:
  95. {
  96. keycode = B3G_F5;
  97. break;
  98. }
  99. case VK_F6:
  100. {
  101. keycode = B3G_F6;
  102. break;
  103. }
  104. case VK_F7:
  105. {
  106. keycode = B3G_F7;
  107. break;
  108. }
  109. case VK_F8:
  110. {
  111. keycode = B3G_F8;
  112. break;
  113. }
  114. case VK_F9:
  115. {
  116. keycode = B3G_F9;
  117. break;
  118. }
  119. case VK_F10:
  120. {
  121. keycode = B3G_F10;
  122. break;
  123. }
  124. //case VK_SPACE: {keycode= ' '; break;}
  125. case VK_NEXT:
  126. {
  127. keycode = B3G_PAGE_DOWN;
  128. break;
  129. }
  130. case VK_PRIOR:
  131. {
  132. keycode = B3G_PAGE_UP;
  133. break;
  134. }
  135. case VK_INSERT:
  136. {
  137. keycode = B3G_INSERT;
  138. break;
  139. }
  140. case VK_BACK:
  141. {
  142. keycode = B3G_BACKSPACE;
  143. break;
  144. }
  145. case VK_DELETE:
  146. {
  147. keycode = B3G_DELETE;
  148. break;
  149. }
  150. case VK_END:
  151. {
  152. keycode = B3G_END;
  153. break;
  154. }
  155. case VK_HOME:
  156. {
  157. keycode = B3G_HOME;
  158. break;
  159. }
  160. case VK_LEFT:
  161. {
  162. keycode = B3G_LEFT_ARROW;
  163. break;
  164. }
  165. case VK_UP:
  166. {
  167. keycode = B3G_UP_ARROW;
  168. break;
  169. }
  170. case VK_RIGHT:
  171. {
  172. keycode = B3G_RIGHT_ARROW;
  173. break;
  174. }
  175. case VK_DOWN:
  176. {
  177. keycode = B3G_DOWN_ARROW;
  178. break;
  179. }
  180. case VK_SHIFT:
  181. {
  182. keycode = B3G_SHIFT;
  183. break;
  184. }
  185. case VK_MENU:
  186. {
  187. keycode = B3G_ALT;
  188. break;
  189. }
  190. case VK_CONTROL:
  191. {
  192. keycode = B3G_CONTROL;
  193. break;
  194. }
  195. default:
  196. {
  197. //keycode = MapVirtualKey( virtualKeyCode, MAPVK_VK_TO_CHAR ) & 0x0000FFFF;
  198. }
  199. };
  200. return keycode;
  201. }
  202. int getAsciiCodeFromVirtualKeycode(int virtualKeyCode)
  203. {
  204. int keycode = 0xffffffff;
  205. if (virtualKeyCode >= 'a' && virtualKeyCode <= 'z')
  206. {
  207. return virtualKeyCode;
  208. }
  209. if (virtualKeyCode >= 'A' && virtualKeyCode <= 'Z')
  210. {
  211. return virtualKeyCode + 32; //todo: fix the ascii A vs a input
  212. }
  213. return keycode;
  214. }
  215. bool Win32Window::isModifierKeyPressed(int key)
  216. {
  217. bool isPressed = false;
  218. switch (key)
  219. {
  220. case B3G_ALT:
  221. {
  222. isPressed = ((sData->m_internalKeyModifierFlags & INTERNAL_ALT_MODIFIER) != 0);
  223. break;
  224. };
  225. case B3G_SHIFT:
  226. {
  227. isPressed = ((sData->m_internalKeyModifierFlags & INTERNAL_SHIFT_MODIFIER) != 0);
  228. break;
  229. };
  230. case B3G_CONTROL:
  231. {
  232. isPressed = ((sData->m_internalKeyModifierFlags & INTERNAL_CONTROL_MODIFIER) != 0);
  233. break;
  234. };
  235. default:
  236. {
  237. }
  238. };
  239. return isPressed; //m_internalKeyModifierFlags
  240. }
  241. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  242. {
  243. //printf("msg = %d\n", message);
  244. switch (message)
  245. {
  246. case WM_PAINT:
  247. {
  248. PAINTSTRUCT ps;
  249. BeginPaint(hWnd, &ps);
  250. EndPaint(hWnd, &ps);
  251. }
  252. return 1;
  253. case WM_ERASEBKGND:
  254. return 1;
  255. case WM_CLOSE:
  256. if (sData)
  257. sData->m_quit = true;
  258. //PostQuitMessage(0);
  259. return 1;
  260. case WM_DESTROY:
  261. if (sData)
  262. sData->m_quit = true;
  263. //PostQuitMessage(0);
  264. return 1;
  265. case WM_SYSKEYUP:
  266. case WM_KEYUP:
  267. {
  268. int keycode = getSpecialKeyFromVirtualKeycode(wParam);
  269. switch (keycode)
  270. {
  271. case B3G_ALT:
  272. {
  273. sData->m_internalKeyModifierFlags &= ~INTERNAL_ALT_MODIFIER;
  274. break;
  275. };
  276. case B3G_SHIFT:
  277. {
  278. sData->m_internalKeyModifierFlags &= ~INTERNAL_SHIFT_MODIFIER;
  279. break;
  280. };
  281. case B3G_CONTROL:
  282. {
  283. sData->m_internalKeyModifierFlags &= ~INTERNAL_CONTROL_MODIFIER;
  284. break;
  285. };
  286. }
  287. if (keycode >= 0 && sData && sData->m_keyboardCallback)
  288. {
  289. int state = 0;
  290. (*sData->m_keyboardCallback)(keycode, state);
  291. }
  292. return 0;
  293. }
  294. case WM_CHAR:
  295. {
  296. #if 0
  297. //skip 'enter' key, it is processed in WM_KEYUP/WM_KEYDOWN
  298. int keycode = getAsciiCodeFromVirtualKeycode(wParam);
  299. if (keycode < 0)
  300. {
  301. if (sData && sData->m_keyboardCallback && ((HIWORD(lParam) & KF_REPEAT) == 0))
  302. {
  303. int state = 1;
  304. (*sData->m_keyboardCallback)(wParam, state);
  305. }
  306. }
  307. #endif
  308. return 0;
  309. }
  310. case WM_SYSKEYDOWN:
  311. case WM_KEYDOWN:
  312. {
  313. int keycode = getSpecialKeyFromVirtualKeycode(wParam);
  314. switch (keycode)
  315. {
  316. case B3G_ALT:
  317. {
  318. sData->m_internalKeyModifierFlags |= INTERNAL_ALT_MODIFIER;
  319. break;
  320. };
  321. case B3G_SHIFT:
  322. {
  323. sData->m_internalKeyModifierFlags |= INTERNAL_SHIFT_MODIFIER;
  324. break;
  325. };
  326. case B3G_CONTROL:
  327. {
  328. sData->m_internalKeyModifierFlags |= INTERNAL_CONTROL_MODIFIER;
  329. break;
  330. };
  331. }
  332. if (keycode >= 0 && sData && sData->m_keyboardCallback && ((HIWORD(lParam) & KF_REPEAT) == 0))
  333. {
  334. int state = 1;
  335. (*sData->m_keyboardCallback)(keycode, state);
  336. return 1;
  337. }
  338. return 0;
  339. }
  340. case WM_MBUTTONUP:
  341. {
  342. int xPos = LOWORD(lParam);
  343. int yPos = HIWORD(lParam);
  344. if (sData)
  345. {
  346. sData->m_mouseMButton = 0;
  347. sData->m_mouseXpos = xPos;
  348. sData->m_mouseYpos = yPos;
  349. if (sData && sData->m_mouseButtonCallback)
  350. (*sData->m_mouseButtonCallback)(1, 0, xPos, yPos);
  351. }
  352. break;
  353. }
  354. case WM_MBUTTONDOWN:
  355. {
  356. int xPos = LOWORD(lParam);
  357. int yPos = HIWORD(lParam);
  358. if (sData)
  359. {
  360. sData->m_mouseMButton = 1;
  361. sData->m_mouseXpos = xPos;
  362. sData->m_mouseYpos = yPos;
  363. if (sData && sData->m_mouseButtonCallback)
  364. (*sData->m_mouseButtonCallback)(1, 1, xPos, yPos);
  365. }
  366. break;
  367. }
  368. case WM_LBUTTONUP:
  369. {
  370. int xPos = LOWORD(lParam);
  371. int yPos = HIWORD(lParam);
  372. if (sData)
  373. {
  374. sData->m_mouseLButton = 0;
  375. sData->m_mouseXpos = xPos;
  376. sData->m_mouseYpos = yPos;
  377. if (sData && sData->m_mouseButtonCallback)
  378. (*sData->m_mouseButtonCallback)(0, 0, xPos, yPos);
  379. }
  380. // gDemoApplication->mouseFunc(0,1,xPos,yPos);
  381. break;
  382. }
  383. case WM_LBUTTONDOWN:
  384. {
  385. int xPos = LOWORD(lParam);
  386. int yPos = HIWORD(lParam);
  387. if (sData)
  388. {
  389. sData->m_mouseLButton = 1;
  390. sData->m_mouseXpos = xPos;
  391. sData->m_mouseYpos = yPos;
  392. if (sData && sData->m_mouseButtonCallback)
  393. (*sData->m_mouseButtonCallback)(0, 1, xPos, yPos);
  394. }
  395. break;
  396. }
  397. case 0x020e: //WM_MOUSEWHEEL_LEFT_RIGHT
  398. {
  399. int zDelta = (short)HIWORD(wParam);
  400. int xPos = LOWORD(lParam);
  401. int yPos = HIWORD(lParam);
  402. //m_cameraDistance -= zDelta*0.01;
  403. if (sData && sData->m_wheelCallback)
  404. (*sData->m_wheelCallback)(-float(zDelta) * 0.05f, 0);
  405. return 1;
  406. break;
  407. }
  408. case 0x020A: //WM_MOUSEWHEEL:
  409. {
  410. int zDelta = (short)HIWORD(wParam);
  411. int xPos = LOWORD(lParam);
  412. int yPos = HIWORD(lParam);
  413. //m_cameraDistance -= zDelta*0.01;
  414. if (sData && sData->m_wheelCallback)
  415. (*sData->m_wheelCallback)(0, float(zDelta) * 0.05f);
  416. return 1;
  417. break;
  418. }
  419. case WM_MOUSEMOVE:
  420. {
  421. int xPos = LOWORD(lParam);
  422. int yPos = HIWORD(lParam);
  423. sData->m_mouseXpos = xPos;
  424. sData->m_mouseYpos = yPos;
  425. if (sData && sData->m_mouseMoveCallback)
  426. (*sData->m_mouseMoveCallback)(xPos, yPos);
  427. break;
  428. }
  429. case WM_RBUTTONUP:
  430. {
  431. int xPos = LOWORD(lParam);
  432. int yPos = HIWORD(lParam);
  433. sData->m_mouseRButton = 1;
  434. if (sData && sData->m_mouseButtonCallback)
  435. (*sData->m_mouseButtonCallback)(2, 0, sData->m_mouseXpos, sData->m_mouseYpos);
  436. //gDemoApplication->mouseFunc(2,1,xPos,yPos);
  437. break;
  438. }
  439. case WM_RBUTTONDOWN:
  440. {
  441. int xPos = LOWORD(lParam);
  442. int yPos = HIWORD(lParam);
  443. sData->m_mouseRButton = 0;
  444. if (sData && sData->m_mouseButtonCallback)
  445. (*sData->m_mouseButtonCallback)(2, 1, sData->m_mouseXpos, sData->m_mouseYpos);
  446. break;
  447. }
  448. case WM_QUIT:
  449. {
  450. return 0;
  451. break;
  452. }
  453. case WM_SIZE: // Size Action Has Taken Place
  454. RECT clientRect;
  455. GetClientRect(hWnd, &clientRect);
  456. switch (wParam) // Evaluate Size Action
  457. {
  458. case SIZE_MINIMIZED: // Was Window Minimized?
  459. return 0; // Return
  460. case SIZE_MAXIMIZED: // Was Window Maximized?
  461. case SIZE_RESTORED: // Was Window Restored?
  462. RECT wr;
  463. GetWindowRect(hWnd, &wr);
  464. sData->m_fullWindowWidth = wr.right - wr.left;
  465. sData->m_fullWindowHeight = wr.bottom - wr.top; //LOWORD (lParam) HIWORD (lParam);
  466. sData->m_openglViewportWidth = clientRect.right;
  467. sData->m_openglViewportHeight = clientRect.bottom;
  468. if (sData->m_resizeCallback)
  469. {
  470. glViewport(0, 0, sData->m_openglViewportWidth, sData->m_openglViewportHeight);
  471. (*sData->m_resizeCallback)(sData->m_openglViewportWidth, sData->m_openglViewportHeight);
  472. }
  473. //if (sOpenGLInitialized)
  474. //{
  475. // //gDemoApplication->reshape(sWidth,sHeight);
  476. //}
  477. return 0; // Return
  478. }
  479. break;
  480. default:
  481. {
  482. }
  483. };
  484. return DefWindowProc(hWnd, message, wParam, lParam);
  485. }
  486. void Win32Window::setWindowTitle(const char* titleChar)
  487. {
  488. #ifdef _WIN64
  489. SetWindowTextA(m_data->m_hWnd, titleChar);
  490. #else
  491. #ifdef UNICODE
  492. DWORD dwResult;
  493. SendMessageTimeoutA(m_data->m_hWnd, WM_SETTEXT, 0,
  494. reinterpret_cast<LPARAM>(titleChar),
  495. SMTO_ABORTIFHUNG, 2000, &dwResult);
  496. #else
  497. DWORD dwResult;
  498. SendMessageTimeout(m_data->m_hWnd, WM_SETTEXT, 0,
  499. reinterpret_cast<LPARAM>(titleChar),
  500. SMTO_ABORTIFHUNG, 2000, &dwResult);
  501. #endif
  502. #endif
  503. }
  504. void Win32Window::createWindow(const b3gWindowConstructionInfo& ci)
  505. {
  506. int oglViewportWidth = ci.m_width;
  507. int oglViewportHeight = ci.m_height;
  508. bool fullscreen = ci.m_fullscreen;
  509. int colorBitsPerPixel = ci.m_colorBitsPerPixel;
  510. void* windowHandle = ci.m_windowHandle;
  511. // get handle to exe file
  512. HINSTANCE hInstance = GetModuleHandle(0);
  513. // create the window if we need to and we do not use the null device
  514. if (!windowHandle)
  515. {
  516. #ifdef UNICODE
  517. const wchar_t* ClassName = L"DeviceWin32";
  518. const wchar_t* emptyString = L"";
  519. #else
  520. const char* ClassName = "DeviceWin32";
  521. const char* emptyString = "";
  522. #endif
  523. // Register Class
  524. WNDCLASSEX wcex;
  525. wcex.cbSize = sizeof(WNDCLASSEX);
  526. wcex.style = CS_HREDRAW | CS_VREDRAW;
  527. wcex.lpfnWndProc = WndProc;
  528. wcex.cbClsExtra = 0;
  529. wcex.cbWndExtra = 0;
  530. wcex.hInstance = hInstance;
  531. wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION); //(HICON)LoadImage(hInstance, "bullet_ico.ico", IMAGE_ICON, 0,0, LR_LOADTRANSPARENT);//LR_LOADFROMFILE);
  532. wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  533. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  534. wcex.lpszMenuName = 0;
  535. wcex.lpszClassName = ClassName;
  536. wcex.hIconSm = 0;
  537. // if there is an icon, load it
  538. // wcex.hIcon = (HICON)LoadImage(hInstance, "bullet.ico", IMAGE_ICON, 0,0, LR_LOADFROMFILE);
  539. RegisterClassEx(&wcex);
  540. // calculate client size
  541. RECT clientSize;
  542. clientSize.top = 0;
  543. clientSize.left = 0;
  544. clientSize.right = oglViewportWidth;
  545. clientSize.bottom = oglViewportHeight;
  546. DWORD style = WS_POPUP;
  547. if (!fullscreen)
  548. style = WS_SYSMENU | WS_BORDER | WS_CAPTION | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_SIZEBOX;
  549. AdjustWindowRect(&clientSize, style, false);
  550. m_data->m_fullWindowWidth = clientSize.right - clientSize.left;
  551. m_data->m_fullWindowHeight = clientSize.bottom - clientSize.top;
  552. int windowLeft = (GetSystemMetrics(SM_CXSCREEN) - m_data->m_fullWindowWidth) / 2;
  553. int windowTop = (GetSystemMetrics(SM_CYSCREEN) - m_data->m_fullWindowHeight) / 2;
  554. if (fullscreen)
  555. {
  556. windowLeft = 0;
  557. windowTop = 0;
  558. }
  559. // create window
  560. m_data->m_hWnd = CreateWindow(ClassName, emptyString, style, windowLeft, windowTop,
  561. m_data->m_fullWindowWidth, m_data->m_fullWindowHeight, NULL, NULL, hInstance, NULL);
  562. RECT clientRect;
  563. GetClientRect(m_data->m_hWnd, &clientRect);
  564. ShowWindow(m_data->m_hWnd, SW_SHOW);
  565. UpdateWindow(m_data->m_hWnd);
  566. MoveWindow(m_data->m_hWnd, windowLeft, windowTop, m_data->m_fullWindowWidth, m_data->m_fullWindowHeight, TRUE);
  567. GetClientRect(m_data->m_hWnd, &clientRect);
  568. int w = clientRect.right - clientRect.left;
  569. int h = clientRect.bottom - clientRect.top;
  570. // printf("actual client OpenGL viewport width / height = %d, %d\n",w,h);
  571. m_data->m_openglViewportHeight = h;
  572. m_data->m_openglViewportWidth = w;
  573. }
  574. else if (windowHandle)
  575. {
  576. // attach external window
  577. m_data->m_hWnd = static_cast<HWND>(windowHandle);
  578. RECT r;
  579. GetWindowRect(m_data->m_hWnd, &r);
  580. m_data->m_fullWindowWidth = r.right - r.left;
  581. m_data->m_fullWindowHeight = r.bottom - r.top;
  582. //sFullScreen = false;
  583. //sExternalWindow = true;
  584. }
  585. if (fullscreen)
  586. {
  587. DEVMODE dm;
  588. memset(&dm, 0, sizeof(dm));
  589. dm.dmSize = sizeof(dm);
  590. // use default values from current setting
  591. EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &dm);
  592. m_data->m_oldScreenWidth = dm.dmPelsWidth;
  593. m_data->m_oldHeight = dm.dmPelsHeight;
  594. m_data->m_oldBitsPerPel = dm.dmBitsPerPel;
  595. dm.dmPelsWidth = oglViewportWidth;
  596. dm.dmPelsHeight = oglViewportHeight;
  597. if (colorBitsPerPixel)
  598. {
  599. dm.dmBitsPerPel = colorBitsPerPixel;
  600. }
  601. dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFREQUENCY;
  602. LONG res = ChangeDisplaySettings(&dm, CDS_FULLSCREEN);
  603. if (res != DISP_CHANGE_SUCCESSFUL)
  604. { // try again without forcing display frequency
  605. dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  606. res = ChangeDisplaySettings(&dm, CDS_FULLSCREEN);
  607. }
  608. }
  609. }
  610. void Win32Window::switchFullScreen(bool fullscreen, int width, int height, int colorBitsPerPixel)
  611. {
  612. LONG res;
  613. DEVMODE dm;
  614. memset(&dm, 0, sizeof(dm));
  615. dm.dmSize = sizeof(dm);
  616. // use default values from current setting
  617. EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &dm);
  618. dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFREQUENCY;
  619. if (fullscreen && !m_data->m_oldScreenWidth)
  620. {
  621. m_data->m_oldScreenWidth = dm.dmPelsWidth;
  622. m_data->m_oldHeight = dm.dmPelsHeight;
  623. m_data->m_oldBitsPerPel = dm.dmBitsPerPel;
  624. if (width && height)
  625. {
  626. dm.dmPelsWidth = width;
  627. dm.dmPelsHeight = height;
  628. }
  629. else
  630. {
  631. dm.dmPelsWidth = m_data->m_fullWindowWidth;
  632. dm.dmPelsHeight = m_data->m_fullWindowHeight;
  633. }
  634. if (colorBitsPerPixel)
  635. {
  636. dm.dmBitsPerPel = colorBitsPerPixel;
  637. }
  638. }
  639. else
  640. {
  641. if (m_data->m_oldScreenWidth)
  642. {
  643. dm.dmPelsWidth = m_data->m_oldScreenWidth;
  644. dm.dmPelsHeight = m_data->m_oldHeight;
  645. dm.dmBitsPerPel = m_data->m_oldBitsPerPel;
  646. }
  647. }
  648. if (fullscreen)
  649. {
  650. res = ChangeDisplaySettings(&dm, CDS_FULLSCREEN);
  651. if (!res)
  652. {
  653. dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  654. res = ChangeDisplaySettings(&dm, CDS_FULLSCREEN);
  655. }
  656. DWORD style = WS_POPUP;
  657. SetWindowLong(m_data->m_hWnd, GWL_STYLE, style);
  658. MoveWindow(m_data->m_hWnd, 0, 0, m_data->m_fullWindowWidth, m_data->m_fullWindowHeight, TRUE);
  659. SetWindowPos(m_data->m_hWnd, NULL, 0, 0, (int)width, (int)height,
  660. SWP_FRAMECHANGED | SWP_SHOWWINDOW); //|SWP_NOACTIVATE | SWP_NOCOPYBITS | SWP_NOOWNERZORDER | SWP_NOREPOSITION | SWP_NOZORDER);
  661. }
  662. else
  663. {
  664. res = ChangeDisplaySettings(&dm, 0);
  665. DWORD style = WS_SYSMENU | WS_BORDER | WS_CAPTION | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_SIZEBOX;
  666. SetWindowLong(m_data->m_hWnd, GWL_STYLE, style);
  667. SetWindowPos(m_data->m_hWnd, NULL, 0, 0, (int)width, (int)height,
  668. SWP_FRAMECHANGED | SWP_SHOWWINDOW);
  669. //|SWP_NOACTIVATE | SWP_NOCOPYBITS | SWP_NOOWNERZORDER | SWP_NOREPOSITION | SWP_NOZORDER);
  670. }
  671. }
  672. Win32Window::Win32Window()
  673. {
  674. m_data = new InternalData2();
  675. sData = m_data;
  676. }
  677. Win32Window::~Win32Window()
  678. {
  679. setKeyboardCallback(0);
  680. setMouseMoveCallback(0);
  681. setMouseButtonCallback(0);
  682. setWheelCallback(0);
  683. setResizeCallback(0);
  684. sData = 0;
  685. delete m_data;
  686. }
  687. void Win32Window::setRenderCallback(b3RenderCallback renderCallback)
  688. {
  689. }
  690. void Win32Window::closeWindow()
  691. {
  692. setKeyboardCallback(0);
  693. setMouseMoveCallback(0);
  694. setMouseButtonCallback(0);
  695. setWheelCallback(0);
  696. setResizeCallback(0);
  697. setRenderCallback(0);
  698. DestroyWindow(this->m_data->m_hWnd);
  699. }
  700. void Win32Window::getMouseCoordinates(int& x, int& y)
  701. {
  702. x = m_data->m_mouseXpos;
  703. y = m_data->m_mouseYpos;
  704. }
  705. void Win32Window::runMainLoop()
  706. {
  707. }
  708. void Win32Window::startRendering()
  709. {
  710. pumpMessage();
  711. }
  712. void Win32Window::renderAllObjects()
  713. {
  714. }
  715. void Win32Window::endRendering()
  716. {
  717. SwapBuffers(m_data->m_hDC);
  718. }
  719. float Win32Window::getTimeInSeconds()
  720. {
  721. return 0.f;
  722. }
  723. void Win32Window::setDebugMessage(int x, int y, const char* message)
  724. {
  725. }
  726. void Win32Window::setRequestExit()
  727. {
  728. m_data->m_quit = true;
  729. }
  730. bool Win32Window::requestedExit() const
  731. {
  732. return m_data->m_quit;
  733. }
  734. void Win32Window::setWheelCallback(b3WheelCallback wheelCallback)
  735. {
  736. m_data->m_wheelCallback = wheelCallback;
  737. }
  738. void Win32Window::setMouseMoveCallback(b3MouseMoveCallback mouseCallback)
  739. {
  740. m_data->m_mouseMoveCallback = mouseCallback;
  741. }
  742. void Win32Window::setMouseButtonCallback(b3MouseButtonCallback mouseCallback)
  743. {
  744. m_data->m_mouseButtonCallback = mouseCallback;
  745. }
  746. void Win32Window::setResizeCallback(b3ResizeCallback resizeCallback)
  747. {
  748. m_data->m_resizeCallback = resizeCallback;
  749. if (m_data->m_resizeCallback)
  750. (*m_data->m_resizeCallback)(m_data->m_openglViewportWidth, m_data->m_openglViewportHeight);
  751. }
  752. void Win32Window::setKeyboardCallback(b3KeyboardCallback keyboardCallback)
  753. {
  754. m_data->m_keyboardCallback = keyboardCallback;
  755. }
  756. b3KeyboardCallback Win32Window::getKeyboardCallback()
  757. {
  758. return m_data->m_keyboardCallback;
  759. }
  760. b3MouseMoveCallback Win32Window::getMouseMoveCallback()
  761. {
  762. return m_data->m_mouseMoveCallback;
  763. }
  764. b3MouseButtonCallback Win32Window::getMouseButtonCallback()
  765. {
  766. return m_data->m_mouseButtonCallback;
  767. }
  768. b3ResizeCallback Win32Window::getResizeCallback()
  769. {
  770. return m_data->m_resizeCallback;
  771. }
  772. b3WheelCallback Win32Window::getWheelCallback()
  773. {
  774. return m_data->m_wheelCallback;
  775. }
  776. #endif