PlatformWin32.cpp 22 KB

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