PlatformWin32.cpp 20 KB

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