PlatformWin32.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. #ifdef WIN32
  2. #include "Base.h"
  3. #include "Platform.h"
  4. #include "FileSystem.h"
  5. #include "Game.h"
  6. #include <GL/wglew.h>
  7. static long __timeTicksPerMillis;
  8. static long __timeStart;
  9. static long __timeAbsolute;
  10. static bool __vsync = WINDOW_VSYNC;
  11. static float __roll;
  12. static float __pitch;
  13. static HINSTANCE __hinstance = 0;
  14. static HWND __hwnd = 0;
  15. static HDC __hdc = 0;
  16. static HGLRC __hrc = 0;
  17. // Gets the gameplay::Keyboard::Key enumeration constant that corresponds
  18. // to the given key and shift modifier combination.
  19. static gameplay::Keyboard::Key getKey(WPARAM win32KeyCode, bool shiftDown)
  20. {
  21. // TODO: Handle the following keys
  22. //gameplay::Keyboard::KEY_SYSREQ
  23. //gameplay::Keyboard::KEY_BREAK
  24. //gameplay::Keyboard::KEY_MENU
  25. //gameplay::Keyboard::KEY_KP_ENTER
  26. switch (win32KeyCode)
  27. {
  28. case VK_PAUSE:
  29. return gameplay::Keyboard::KEY_PAUSE;
  30. case VK_SCROLL:
  31. return gameplay::Keyboard::KEY_SCROLL_LOCK;
  32. case VK_PRINT:
  33. return gameplay::Keyboard::KEY_PRINT;
  34. case VK_ESCAPE:
  35. return gameplay::Keyboard::KEY_ESCAPE;
  36. case VK_BACK:
  37. return gameplay::Keyboard::KEY_BACKSPACE;
  38. case VK_TAB:
  39. return shiftDown ? gameplay::Keyboard::KEY_BACK_TAB : gameplay::Keyboard::KEY_TAB;
  40. case VK_RETURN:
  41. return gameplay::Keyboard::KEY_RETURN;
  42. case VK_CAPITAL:
  43. return gameplay::Keyboard::KEY_CAPS_LOCK;
  44. case VK_LSHIFT:
  45. return gameplay::Keyboard::KEY_LEFT_SHIFT;
  46. case VK_RSHIFT:
  47. return gameplay::Keyboard::KEY_RIGHT_SHIFT;
  48. case VK_LCONTROL:
  49. return gameplay::Keyboard::KEY_LEFT_CTRL;
  50. case VK_RCONTROL:
  51. return gameplay::Keyboard::KEY_RIGHT_CTRL;
  52. case VK_LMENU:
  53. return gameplay::Keyboard::KEY_LEFT_ALT;
  54. case VK_RMENU:
  55. return gameplay::Keyboard::KEY_RIGHT_ALT;
  56. case VK_LWIN:
  57. return gameplay::Keyboard::KEY_LEFT_HYPER;
  58. case VK_RWIN:
  59. return gameplay::Keyboard::KEY_RIGHT_HYPER;
  60. case VK_INSERT:
  61. return gameplay::Keyboard::KEY_INSERT;
  62. case VK_HOME:
  63. return gameplay::Keyboard::KEY_HOME;
  64. case VK_PRIOR:
  65. return gameplay::Keyboard::KEY_PG_UP;
  66. case VK_DELETE:
  67. return gameplay::Keyboard::KEY_DELETE;
  68. case VK_END:
  69. return gameplay::Keyboard::KEY_END;
  70. case VK_NEXT:
  71. return gameplay::Keyboard::KEY_PG_DOWN;
  72. case VK_LEFT:
  73. return gameplay::Keyboard::KEY_LEFT_ARROW;
  74. case VK_RIGHT:
  75. return gameplay::Keyboard::KEY_RIGHT_ARROW;
  76. case VK_UP:
  77. return gameplay::Keyboard::KEY_UP_ARROW;
  78. case VK_DOWN:
  79. return gameplay::Keyboard::KEY_DOWN_ARROW;
  80. case VK_NUMLOCK:
  81. return gameplay::Keyboard::KEY_NUM_LOCK;
  82. case VK_ADD:
  83. return gameplay::Keyboard::KEY_KP_PLUS;
  84. case VK_SUBTRACT:
  85. return gameplay::Keyboard::KEY_KP_MINUS;
  86. case VK_MULTIPLY:
  87. return gameplay::Keyboard::KEY_KP_MULTIPLY;
  88. case VK_DIVIDE:
  89. return gameplay::Keyboard::KEY_KP_DIVIDE;
  90. case VK_NUMPAD7:
  91. return gameplay::Keyboard::KEY_KP_HOME;
  92. case VK_NUMPAD8:
  93. return gameplay::Keyboard::KEY_KP_UP;
  94. case VK_NUMPAD9:
  95. return gameplay::Keyboard::KEY_KP_PG_UP;
  96. case VK_NUMPAD4:
  97. return gameplay::Keyboard::KEY_KP_LEFT;
  98. case VK_NUMPAD5:
  99. return gameplay::Keyboard::KEY_KP_FIVE;
  100. case VK_NUMPAD6:
  101. return gameplay::Keyboard::KEY_KP_RIGHT;
  102. case VK_NUMPAD1:
  103. return gameplay::Keyboard::KEY_KP_END;
  104. case VK_NUMPAD2:
  105. return gameplay::Keyboard::KEY_KP_DOWN;
  106. case VK_NUMPAD3:
  107. return gameplay::Keyboard::KEY_KP_PG_DOWN;
  108. case VK_NUMPAD0:
  109. return gameplay::Keyboard::KEY_KP_INSERT;
  110. case VK_DECIMAL:
  111. return gameplay::Keyboard::KEY_KP_DELETE;
  112. case VK_F1:
  113. return gameplay::Keyboard::KEY_F1;
  114. case VK_F2:
  115. return gameplay::Keyboard::KEY_F2;
  116. case VK_F3:
  117. return gameplay::Keyboard::KEY_F3;
  118. case VK_F4:
  119. return gameplay::Keyboard::KEY_F4;
  120. case VK_F5:
  121. return gameplay::Keyboard::KEY_F5;
  122. case VK_F6:
  123. return gameplay::Keyboard::KEY_F6;
  124. case VK_F7:
  125. return gameplay::Keyboard::KEY_F7;
  126. case VK_F8:
  127. return gameplay::Keyboard::KEY_F8;
  128. case VK_F9:
  129. return gameplay::Keyboard::KEY_F9;
  130. case VK_F10:
  131. return gameplay::Keyboard::KEY_F10;
  132. case VK_F11:
  133. return gameplay::Keyboard::KEY_F11;
  134. case VK_F12:
  135. return gameplay::Keyboard::KEY_F12;
  136. case VK_SPACE:
  137. return gameplay::Keyboard::KEY_SPACE;
  138. case 0x30:
  139. return shiftDown ? gameplay::Keyboard::KEY_RIGHT_PARENTHESIS : gameplay::Keyboard::KEY_ZERO;
  140. case 0x31:
  141. return shiftDown ? gameplay::Keyboard::KEY_EXCLAM : gameplay::Keyboard::KEY_ONE;
  142. case 0x32:
  143. return shiftDown ? gameplay::Keyboard::KEY_AT : gameplay::Keyboard::KEY_TWO;
  144. case 0x33:
  145. return shiftDown ? gameplay::Keyboard::KEY_NUMBER : gameplay::Keyboard::KEY_THREE;
  146. case 0x34:
  147. return shiftDown ? gameplay::Keyboard::KEY_DOLLAR : gameplay::Keyboard::KEY_FOUR;
  148. case 0x35:
  149. return shiftDown ? gameplay::Keyboard::KEY_PERCENT : gameplay::Keyboard::KEY_FIVE;
  150. case 0x36:
  151. return shiftDown ? gameplay::Keyboard::KEY_CIRCUMFLEX : gameplay::Keyboard::KEY_SIX;
  152. case 0x37:
  153. return shiftDown ? gameplay::Keyboard::KEY_AMPERSAND : gameplay::Keyboard::KEY_SEVEN;
  154. case 0x38:
  155. return shiftDown ? gameplay::Keyboard::KEY_ASTERISK : gameplay::Keyboard::KEY_EIGHT;
  156. case 0x39:
  157. return shiftDown ? gameplay::Keyboard::KEY_LEFT_PARENTHESIS : gameplay::Keyboard::KEY_NINE;
  158. case VK_OEM_PLUS:
  159. return shiftDown ? gameplay::Keyboard::KEY_EQUAL : gameplay::Keyboard::KEY_PLUS;
  160. case VK_OEM_COMMA:
  161. return shiftDown ? gameplay::Keyboard::KEY_LESS_THAN : gameplay::Keyboard::KEY_COMMA;
  162. case VK_OEM_MINUS:
  163. return shiftDown ? gameplay::Keyboard::KEY_UNDERSCORE : gameplay::Keyboard::KEY_MINUS;
  164. case VK_OEM_PERIOD:
  165. return shiftDown ? gameplay::Keyboard::KEY_GREATER_THAN : gameplay::Keyboard::KEY_PERIOD;
  166. case VK_OEM_1:
  167. return shiftDown ? gameplay::Keyboard::KEY_COLON : gameplay::Keyboard::KEY_SEMICOLON;
  168. case VK_OEM_2:
  169. return shiftDown ? gameplay::Keyboard::KEY_QUESTION : gameplay::Keyboard::KEY_SLASH;
  170. case VK_OEM_3:
  171. return shiftDown ? gameplay::Keyboard::KEY_GRAVE : gameplay::Keyboard::KEY_TILDE;
  172. case VK_OEM_4:
  173. return shiftDown ? gameplay::Keyboard::KEY_LEFT_BRACE : gameplay::Keyboard::KEY_LEFT_BRACKET;
  174. case VK_OEM_5:
  175. return shiftDown ? gameplay::Keyboard::KEY_BAR : gameplay::Keyboard::KEY_BACK_SLASH;
  176. case VK_OEM_6:
  177. return shiftDown ? gameplay::Keyboard::KEY_RIGHT_BRACE : gameplay::Keyboard::KEY_RIGHT_BRACKET;
  178. case VK_OEM_7:
  179. return shiftDown ? gameplay::Keyboard::KEY_QUOTE : gameplay::Keyboard::KEY_APOSTROPHE;
  180. case 0x41:
  181. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_A : gameplay::Keyboard::KEY_A;
  182. case 0x42:
  183. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_B : gameplay::Keyboard::KEY_B;
  184. case 0x43:
  185. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_C : gameplay::Keyboard::KEY_C;
  186. case 0x44:
  187. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_D : gameplay::Keyboard::KEY_D;
  188. case 0x45:
  189. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_E : gameplay::Keyboard::KEY_E;
  190. case 0x46:
  191. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_F : gameplay::Keyboard::KEY_F;
  192. case 0x47:
  193. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_G : gameplay::Keyboard::KEY_G;
  194. case 0x48:
  195. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_H : gameplay::Keyboard::KEY_H;
  196. case 0x49:
  197. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_I : gameplay::Keyboard::KEY_I;
  198. case 0x4A:
  199. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_J : gameplay::Keyboard::KEY_J;
  200. case 0x4B:
  201. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_K : gameplay::Keyboard::KEY_K;
  202. case 0x4C:
  203. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_L : gameplay::Keyboard::KEY_L;
  204. case 0x4D:
  205. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_M : gameplay::Keyboard::KEY_M;
  206. case 0x4E:
  207. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_N : gameplay::Keyboard::KEY_N;
  208. case 0x4F:
  209. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_O : gameplay::Keyboard::KEY_O;
  210. case 0x50:
  211. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_P : gameplay::Keyboard::KEY_P;
  212. case 0x51:
  213. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_Q : gameplay::Keyboard::KEY_Q;
  214. case 0x52:
  215. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_R : gameplay::Keyboard::KEY_R;
  216. case 0x53:
  217. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_S : gameplay::Keyboard::KEY_S;
  218. case 0x54:
  219. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_T : gameplay::Keyboard::KEY_T;
  220. case 0x55:
  221. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_U : gameplay::Keyboard::KEY_U;
  222. case 0x56:
  223. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_V : gameplay::Keyboard::KEY_V;
  224. case 0x57:
  225. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_W : gameplay::Keyboard::KEY_W;
  226. case 0x58:
  227. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_X : gameplay::Keyboard::KEY_X;
  228. case 0x59:
  229. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_Y : gameplay::Keyboard::KEY_Y;
  230. case 0x5A:
  231. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_Z : gameplay::Keyboard::KEY_Z;
  232. default:
  233. return gameplay::Keyboard::KEY_NONE;
  234. }
  235. }
  236. LRESULT CALLBACK __WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  237. {
  238. // Scale factors for the mouse movement used to simulate the accelerometer.
  239. static const float ACCELEROMETER_X_FACTOR = 90.0f / WINDOW_WIDTH;
  240. static const float ACCELEROMETER_Y_FACTOR = 90.0f / WINDOW_HEIGHT;
  241. static bool hasMouse = false;
  242. static bool lMouseDown = false;
  243. static bool rMouseDown = false;
  244. static int lx, ly;
  245. static bool shiftDown = false;
  246. switch (msg)
  247. {
  248. case WM_PAINT:
  249. gameplay::Game::getInstance()->frame();
  250. SwapBuffers(__hdc);
  251. return 0;
  252. case WM_CLOSE:
  253. DestroyWindow(__hwnd);
  254. return 0;
  255. case WM_DESTROY:
  256. PostQuitMessage(0);
  257. return 0;
  258. case WM_LBUTTONDOWN:
  259. if (!gameplay::Game::getInstance()->mouseEvent(gameplay::Mouse::MOUSE_LEFT_BUTTON_PRESS, LOWORD(lParam), HIWORD(lParam)))
  260. {
  261. gameplay::Game::getInstance()->touchEvent(gameplay::Touch::TOUCH_PRESS, LOWORD(lParam), HIWORD(lParam), 0);
  262. }
  263. lMouseDown = true;
  264. return 0;
  265. case WM_LBUTTONUP:
  266. lMouseDown = false;
  267. if (!gameplay::Game::getInstance()->mouseEvent(gameplay::Mouse::MOUSE_LEFT_BUTTON_RELEASE, LOWORD(lParam), HIWORD(lParam)))
  268. {
  269. gameplay::Game::getInstance()->touchEvent(gameplay::Touch::TOUCH_RELEASE, LOWORD(lParam), HIWORD(lParam), 0);
  270. }
  271. return 0;
  272. case WM_RBUTTONDOWN:
  273. gameplay::Game::getInstance()->mouseEvent(gameplay::Mouse::MOUSE_RIGHT_BUTTON_PRESS, LOWORD(lParam), HIWORD(lParam));
  274. rMouseDown = true;
  275. lx = LOWORD(lParam);
  276. ly = HIWORD(lParam);
  277. break;
  278. case WM_RBUTTONUP:
  279. gameplay::Game::getInstance()->mouseEvent(gameplay::Mouse::MOUSE_RIGHT_BUTTON_RELEASE, LOWORD(lParam), HIWORD(lParam));
  280. rMouseDown = false;
  281. break;
  282. case WM_MBUTTONDOWN:
  283. gameplay::Game::getInstance()->mouseEvent(gameplay::Mouse::MOUSE_MIDDLE_BUTTON_PRESS, LOWORD(lParam), HIWORD(lParam));
  284. break;
  285. case WM_MBUTTONUP:
  286. gameplay::Game::getInstance()->mouseEvent(gameplay::Mouse::MOUSE_MIDDLE_BUTTON_RELEASE, LOWORD(lParam), HIWORD(lParam));
  287. break;
  288. case WM_MOUSEMOVE:
  289. {
  290. if (!hasMouse)
  291. {
  292. hasMouse = true;
  293. // Call TrackMouseEvent to detect the next WM_MOUSE_LEAVE.
  294. TRACKMOUSEEVENT tme;
  295. tme.cbSize = sizeof(TRACKMOUSEEVENT);
  296. tme.dwFlags = TME_LEAVE;
  297. tme.hwndTrack = __hwnd;
  298. TrackMouseEvent(&tme);
  299. }
  300. bool consumed = gameplay::Game::getInstance()->mouseEvent(gameplay::Mouse::MOUSE_MOVE, LOWORD(lParam), HIWORD(lParam));
  301. if (lMouseDown && !consumed)
  302. {
  303. // Mouse move events should be interpreted as touch move only if left mouse is held and the game did not consume the mouse event.
  304. gameplay::Game::getInstance()->touchEvent(gameplay::Touch::TOUCH_MOVE, LOWORD(lParam), HIWORD(lParam), 0);
  305. return 0;
  306. }
  307. else if (rMouseDown)
  308. {
  309. // Update the pitch and roll by adding the scaled deltas.
  310. __roll += -(float)(LOWORD(lParam) - lx) * ACCELEROMETER_X_FACTOR;
  311. __pitch += (float)(HIWORD(lParam) - ly) * ACCELEROMETER_Y_FACTOR;
  312. // Clamp the values to the valid range.
  313. __roll = max(min(__roll, 90.0f), -90.0f);
  314. __pitch = max(min(__pitch, 90.0f), -90.0f);
  315. // Update the last X/Y values.
  316. lx = LOWORD(lParam);
  317. ly = HIWORD(lParam);
  318. }
  319. break;
  320. }
  321. case WM_MOUSELEAVE:
  322. hasMouse = false;
  323. lMouseDown = false;
  324. rMouseDown = false;
  325. break;
  326. case WM_MOUSEWHEEL:
  327. gameplay::Game::getInstance()->mouseWheelEvent(LOWORD(lParam), HIWORD(lParam), GET_WHEEL_DELTA_WPARAM(wParam) / 120);
  328. break;
  329. case WM_KEYDOWN:
  330. if (wParam == VK_LSHIFT || wParam == VK_RSHIFT)
  331. shiftDown = true;
  332. gameplay::Game::getInstance()->keyEvent(gameplay::Keyboard::KEY_PRESS, getKey(wParam, shiftDown));
  333. break;
  334. case WM_KEYUP:
  335. if (wParam == VK_LSHIFT || wParam == VK_RSHIFT)
  336. shiftDown = false;
  337. gameplay::Game::getInstance()->keyEvent(gameplay::Keyboard::KEY_RELEASE, getKey(wParam, shiftDown));
  338. break;
  339. case WM_SETFOCUS:
  340. break;
  341. case WM_KILLFOCUS:
  342. break;
  343. }
  344. return DefWindowProc(hwnd, msg, wParam, lParam);
  345. }
  346. namespace gameplay
  347. {
  348. extern void printError(const char* format, ...)
  349. {
  350. va_list argptr;
  351. va_start(argptr, format);
  352. fprintf(stderr, "\n");
  353. int sz = vfprintf(stderr, format, argptr);
  354. if (sz > 0)
  355. {
  356. char* buf = new char[sz + 2];
  357. vsprintf(buf, format, argptr);
  358. buf[sz] = '\n';
  359. buf[sz+1] = 0;
  360. OutputDebugStringA(buf);
  361. SAFE_DELETE_ARRAY(buf);
  362. }
  363. va_end(argptr);
  364. }
  365. Platform::Platform(Game* game)
  366. : _game(game)
  367. {
  368. }
  369. Platform::Platform(const Platform& copy)
  370. {
  371. // hidden
  372. }
  373. Platform::~Platform()
  374. {
  375. if (__hwnd)
  376. {
  377. DestroyWindow(__hwnd);
  378. __hwnd = 0;
  379. }
  380. }
  381. // TODO: Fix Fullscreen + More error handling.
  382. Platform* Platform::create(Game* game)
  383. {
  384. FileSystem::setResourcePath("./");
  385. Platform* platform = new Platform(game);
  386. // Get the application module handle.
  387. __hinstance = ::GetModuleHandle(NULL);
  388. LPCTSTR windowClass = L"gameplay";
  389. LPCTSTR windowName = L"";
  390. RECT rect = { 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT };
  391. // Register our window class.
  392. WNDCLASSEX wc;
  393. wc.cbSize = sizeof(WNDCLASSEX);
  394. wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  395. wc.lpfnWndProc = (WNDPROC)__WndProc;
  396. wc.cbClsExtra = 0;
  397. wc.cbWndExtra = 0;
  398. wc.hInstance = __hinstance;
  399. wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  400. wc.hIconSm = NULL;
  401. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  402. wc.hbrBackground = NULL; // No brush - we are going to paint our own background
  403. wc.lpszMenuName = NULL; // No default menu
  404. wc.lpszClassName = windowClass;
  405. if (!::RegisterClassEx(&wc))
  406. goto error;
  407. // Set the window style.
  408. DWORD style = ( WINDOW_FULLSCREEN ? WS_POPUP : WS_POPUP | WS_BORDER | WS_CAPTION | WS_SYSMENU) | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
  409. DWORD styleEx = (WINDOW_FULLSCREEN ? WS_EX_APPWINDOW : WS_EX_APPWINDOW | WS_EX_WINDOWEDGE);
  410. // Adjust the window rectangle so the client size is the requested size.
  411. AdjustWindowRectEx(&rect, style, FALSE, styleEx);
  412. // Create the native Windows window.
  413. __hwnd = CreateWindowEx(styleEx, windowClass, windowName, style, 0, 0, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, __hinstance, NULL);
  414. // Get the drawing context.
  415. __hdc = GetDC(__hwnd);
  416. // Choose pixel format. 32-bit. RGBA.
  417. PIXELFORMATDESCRIPTOR pfd;
  418. memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
  419. pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
  420. pfd.nVersion = 1;
  421. pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
  422. pfd.iPixelType = PFD_TYPE_RGBA;
  423. pfd.cColorBits = 32;
  424. pfd.cDepthBits = 32;
  425. pfd.iLayerType = PFD_MAIN_PLANE;
  426. int pixelFormat = ChoosePixelFormat(__hdc, &pfd);
  427. if (pixelFormat == 0 || !SetPixelFormat (__hdc, pixelFormat, &pfd))
  428. goto error;
  429. HGLRC tempContext = wglCreateContext(__hdc);
  430. wglMakeCurrent(__hdc, tempContext);
  431. if (GLEW_OK != glewInit())
  432. goto error;
  433. int attribs[] =
  434. {
  435. WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
  436. WGL_CONTEXT_MINOR_VERSION_ARB, 1,
  437. 0
  438. };
  439. if (!(__hrc = wglCreateContextAttribsARB(__hdc, 0, attribs) ) )
  440. {
  441. wglDeleteContext(tempContext);
  442. goto error;
  443. }
  444. wglDeleteContext(tempContext);
  445. if (!wglMakeCurrent(__hdc, __hrc) || !__hrc)
  446. goto error;
  447. // Vertical sync.
  448. wglSwapIntervalEXT(__vsync ? 1 : 0);
  449. // Show the window
  450. ShowWindow(__hwnd, SW_SHOW);
  451. return platform;
  452. error:
  453. // TODO: cleanup
  454. exit(0);
  455. return NULL;
  456. }
  457. int Platform::enterMessagePump()
  458. {
  459. int rc = 0;
  460. // Get the initial time.
  461. LARGE_INTEGER tps;
  462. QueryPerformanceFrequency(&tps);
  463. __timeTicksPerMillis = (long)(tps.QuadPart / 1000L);
  464. LARGE_INTEGER queryTime;
  465. QueryPerformanceCounter(&queryTime);
  466. __timeStart = queryTime.QuadPart / __timeTicksPerMillis;
  467. // Set the initial pitch and roll values.
  468. __pitch = 0.0;
  469. __roll = 0.0;
  470. SwapBuffers(__hdc);
  471. if (_game->getState() != Game::RUNNING)
  472. _game->run(WINDOW_WIDTH, WINDOW_HEIGHT);
  473. // Enter event dispatch loop.
  474. MSG msg;
  475. while (true)
  476. {
  477. if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  478. {
  479. TranslateMessage(&msg);
  480. DispatchMessage(&msg);
  481. if (msg.message == WM_QUIT)
  482. {
  483. _game->exit();
  484. break;
  485. }
  486. }
  487. // If we are done, then exit.
  488. if (_game->getState() == Game::UNINITIALIZED)
  489. break;
  490. }
  491. return msg.wParam;
  492. }
  493. long Platform::getAbsoluteTime()
  494. {
  495. LARGE_INTEGER queryTime;
  496. QueryPerformanceCounter(&queryTime);
  497. __timeAbsolute = queryTime.QuadPart / __timeTicksPerMillis;
  498. return __timeAbsolute;
  499. }
  500. void Platform::setAbsoluteTime(long time)
  501. {
  502. __timeAbsolute = time;
  503. }
  504. bool Platform::isVsync()
  505. {
  506. return __vsync;
  507. }
  508. void Platform::setVsync(bool enable)
  509. {
  510. wglSwapIntervalEXT(enable ? 1 : 0);
  511. __vsync = enable;
  512. }
  513. int Platform::getOrientationAngle()
  514. {
  515. return 0;
  516. }
  517. void Platform::setMultiTouch(bool enabled)
  518. {
  519. }
  520. bool Platform::isMultiTouch()
  521. {
  522. return false;
  523. }
  524. void Platform::getAccelerometerValues(float* pitch, float* roll)
  525. {
  526. *pitch = __pitch;
  527. *roll = __roll;
  528. }
  529. void Platform::swapBuffers()
  530. {
  531. if (__hdc)
  532. SwapBuffers(__hdc);
  533. }
  534. void Platform::displayKeyboard(bool display)
  535. {
  536. // Do nothing.
  537. }
  538. }
  539. #endif