PlatformWin32.cpp 21 KB

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