PlatformWin32.cpp 26 KB

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