PlatformWin32.cpp 22 KB

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