PlatformWin32.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  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 __attachToWindow = 0;
  21. static HWND __hwnd = 0;
  22. static HDC __hdc = 0;
  23. static HGLRC __hrc = 0;
  24. static bool __mouseCaptured = false;
  25. static POINT __mouseCapturePoint = { 0, 0 };
  26. static bool __cursorVisible = true;
  27. static gameplay::Keyboard::Key getKey(WPARAM win32KeyCode, bool shiftDown)
  28. {
  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. void UpdateCapture(LPARAM lParam)
  249. {
  250. if ((lParam & MK_LBUTTON) || (lParam & MK_MBUTTON) || (lParam & MK_RBUTTON))
  251. SetCapture(__hwnd);
  252. else
  253. ReleaseCapture();
  254. }
  255. void WarpMouse(int clientX, int clientY)
  256. {
  257. POINT p = { clientX, clientY };
  258. ClientToScreen(__hwnd, &p);
  259. SetCursorPos(p.x, p.y);
  260. }
  261. LRESULT CALLBACK __WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  262. {
  263. static gameplay::Game* game = gameplay::Game::getInstance();
  264. if (!game->isInitialized() || hwnd != __hwnd)
  265. {
  266. // Ignore messages that are not for our game window.
  267. return DefWindowProc(hwnd, msg, wParam, lParam);
  268. }
  269. // Scale factors for the mouse movement used to simulate the accelerometer.
  270. GP_ASSERT(__width);
  271. GP_ASSERT(__height);
  272. static const float ACCELEROMETER_X_FACTOR = 90.0f / __width;
  273. static const float ACCELEROMETER_Y_FACTOR = 90.0f / __height;
  274. static int lx, ly;
  275. static bool shiftDown = false;
  276. static bool capsOn = false;
  277. switch (msg)
  278. {
  279. case WM_CLOSE:
  280. DestroyWindow(__hwnd);
  281. return 0;
  282. case WM_DESTROY:
  283. PostQuitMessage(0);
  284. return 0;
  285. case WM_LBUTTONDOWN:
  286. {
  287. int x = GET_X_LPARAM(lParam);
  288. int y = GET_Y_LPARAM(lParam);
  289. UpdateCapture(wParam);
  290. if (!gameplay::Platform::mouseEventInternal(gameplay::Mouse::MOUSE_PRESS_LEFT_BUTTON, x, y, 0))
  291. {
  292. gameplay::Platform::touchEventInternal(gameplay::Touch::TOUCH_PRESS, x, y, 0);
  293. }
  294. return 0;
  295. }
  296. case WM_LBUTTONUP:
  297. {
  298. int x = GET_X_LPARAM(lParam);
  299. int y = GET_Y_LPARAM(lParam);
  300. if (!gameplay::Platform::mouseEventInternal(gameplay::Mouse::MOUSE_RELEASE_LEFT_BUTTON, x, y, 0))
  301. {
  302. gameplay::Platform::touchEventInternal(gameplay::Touch::TOUCH_RELEASE, x, y, 0);
  303. }
  304. UpdateCapture(wParam);
  305. return 0;
  306. }
  307. case WM_RBUTTONDOWN:
  308. UpdateCapture(wParam);
  309. lx = GET_X_LPARAM(lParam);
  310. ly = GET_Y_LPARAM(lParam);
  311. gameplay::Platform::mouseEventInternal(gameplay::Mouse::MOUSE_PRESS_RIGHT_BUTTON, lx, ly, 0);
  312. break;
  313. case WM_RBUTTONUP:
  314. gameplay::Platform::mouseEventInternal(gameplay::Mouse::MOUSE_RELEASE_RIGHT_BUTTON, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), 0);
  315. UpdateCapture(wParam);
  316. break;
  317. case WM_MBUTTONDOWN:
  318. UpdateCapture(wParam);
  319. gameplay::Platform::mouseEventInternal(gameplay::Mouse::MOUSE_PRESS_MIDDLE_BUTTON, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), 0);
  320. break;
  321. case WM_MBUTTONUP:
  322. gameplay::Platform::mouseEventInternal(gameplay::Mouse::MOUSE_RELEASE_MIDDLE_BUTTON, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), 0);
  323. UpdateCapture(wParam);
  324. break;
  325. case WM_MOUSEMOVE:
  326. {
  327. int x = GET_X_LPARAM(lParam);
  328. int y = GET_Y_LPARAM(lParam);
  329. if (__mouseCaptured)
  330. {
  331. // If the incoming position is the mouse capture point, ignore this event
  332. // since this is the event that warped the cursor back.
  333. if (x == __mouseCapturePoint.x && y == __mouseCapturePoint.y)
  334. break;
  335. // Convert to deltas
  336. x -= __mouseCapturePoint.x;
  337. y -= __mouseCapturePoint.y;
  338. // Warp mouse back to center of screen.
  339. WarpMouse(__mouseCapturePoint.x, __mouseCapturePoint.y);
  340. }
  341. // Allow Game::mouseEvent a chance to handle (and possibly consume) the event.
  342. if (!gameplay::Platform::mouseEventInternal(gameplay::Mouse::MOUSE_MOVE, x, y, 0))
  343. {
  344. if ((wParam & MK_LBUTTON) == MK_LBUTTON)
  345. {
  346. // Mouse move events should be interpreted as touch move only if left mouse is held and the game did not consume the mouse event.
  347. gameplay::Platform::touchEventInternal(gameplay::Touch::TOUCH_MOVE, x, y, 0);
  348. return 0;
  349. }
  350. else if ((wParam & MK_RBUTTON) == MK_RBUTTON)
  351. {
  352. // Update the pitch and roll by adding the scaled deltas.
  353. __roll += (float)(x - lx) * ACCELEROMETER_X_FACTOR;
  354. __pitch += -(float)(y - ly) * ACCELEROMETER_Y_FACTOR;
  355. // Clamp the values to the valid range.
  356. __roll = max(min(__roll, 90.0f), -90.0f);
  357. __pitch = max(min(__pitch, 90.0f), -90.0f);
  358. // Update the last X/Y values.
  359. lx = x;
  360. ly = y;
  361. }
  362. }
  363. break;
  364. }
  365. case WM_MOUSEWHEEL:
  366. tagPOINT point;
  367. point.x = GET_X_LPARAM(lParam);
  368. point.y = GET_Y_LPARAM(lParam);
  369. ScreenToClient(__hwnd, &point);
  370. gameplay::Platform::mouseEventInternal(gameplay::Mouse::MOUSE_WHEEL, point.x, point.y, GET_WHEEL_DELTA_WPARAM(wParam) / 120);
  371. break;
  372. case WM_KEYDOWN:
  373. if (wParam == VK_SHIFT || wParam == VK_LSHIFT || wParam == VK_RSHIFT)
  374. shiftDown = true;
  375. if (wParam == VK_CAPITAL)
  376. capsOn = !capsOn;
  377. // Suppress key repeats.
  378. if ((lParam & 0x40000000) == 0)
  379. gameplay::Platform::keyEventInternal(gameplay::Keyboard::KEY_PRESS, getKey(wParam, shiftDown ^ capsOn));
  380. break;
  381. case WM_KEYUP:
  382. if (wParam == VK_SHIFT || wParam == VK_LSHIFT || wParam == VK_RSHIFT)
  383. shiftDown = false;
  384. gameplay::Platform::keyEventInternal(gameplay::Keyboard::KEY_RELEASE, getKey(wParam, shiftDown ^ capsOn));
  385. break;
  386. case WM_CHAR:
  387. // Suppress key repeats.
  388. if ((lParam & 0x40000000) == 0)
  389. gameplay::Platform::keyEventInternal(gameplay::Keyboard::KEY_CHAR, wParam);
  390. break;
  391. case WM_UNICHAR:
  392. // Suppress key repeats.
  393. if ((lParam & 0x40000000) == 0)
  394. gameplay::Platform::keyEventInternal(gameplay::Keyboard::KEY_CHAR, wParam);
  395. break;
  396. case WM_SETFOCUS:
  397. break;
  398. case WM_KILLFOCUS:
  399. break;
  400. }
  401. return DefWindowProc(hwnd, msg, wParam, lParam);
  402. }
  403. namespace gameplay
  404. {
  405. extern void printError(const char* format, ...)
  406. {
  407. va_list argptr;
  408. va_start(argptr, format);
  409. int sz = vfprintf(stderr, format, argptr);
  410. if (sz > 0)
  411. {
  412. char* buf = new char[sz + 1];
  413. vsprintf(buf, format, argptr);
  414. buf[sz] = 0;
  415. OutputDebugStringA(buf);
  416. SAFE_DELETE_ARRAY(buf);
  417. }
  418. va_end(argptr);
  419. }
  420. Platform::Platform(Game* game)
  421. : _game(game)
  422. {
  423. }
  424. Platform::Platform(const Platform& copy)
  425. {
  426. // hidden
  427. }
  428. Platform::~Platform()
  429. {
  430. if (__hwnd)
  431. {
  432. DestroyWindow(__hwnd);
  433. __hwnd = 0;
  434. }
  435. }
  436. bool initializeGL()
  437. {
  438. // Choose pixel format. 32-bit. RGBA.
  439. PIXELFORMATDESCRIPTOR pfd;
  440. memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
  441. pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
  442. pfd.nVersion = 1;
  443. pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
  444. pfd.iPixelType = PFD_TYPE_RGBA;
  445. pfd.cColorBits = 32;
  446. pfd.cDepthBits = 32;
  447. pfd.iLayerType = PFD_MAIN_PLANE;
  448. int pixelFormat = ChoosePixelFormat(__hdc, &pfd);
  449. if (pixelFormat == 0)
  450. {
  451. GP_ERROR("Failed to choose a pixel format.");
  452. return false;
  453. }
  454. if (!SetPixelFormat (__hdc, pixelFormat, &pfd))
  455. {
  456. GP_ERROR("Failed to set the pixel format.");
  457. return false;
  458. }
  459. HGLRC tempContext = wglCreateContext(__hdc);
  460. wglMakeCurrent(__hdc, tempContext);
  461. if (GLEW_OK != glewInit())
  462. {
  463. GP_ERROR("Failed to initialize GLEW.");
  464. return false;
  465. }
  466. int attribs[] =
  467. {
  468. WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
  469. WGL_CONTEXT_MINOR_VERSION_ARB, 1,
  470. 0
  471. };
  472. if (!(__hrc = wglCreateContextAttribsARB(__hdc, 0, attribs) ) )
  473. {
  474. wglDeleteContext(tempContext);
  475. GP_ERROR("Failed to create OpenGL context.");
  476. return false;
  477. }
  478. wglDeleteContext(tempContext);
  479. if (!wglMakeCurrent(__hdc, __hrc) || !__hrc)
  480. {
  481. GP_ERROR("Failed to make the window current.");
  482. return false;
  483. }
  484. // Vertical sync.
  485. wglSwapIntervalEXT(__vsync ? 1 : 0);
  486. return true;
  487. }
  488. Platform* Platform::create(Game* game, void* attachToWindow)
  489. {
  490. GP_ASSERT(game);
  491. FileSystem::setResourcePath("./");
  492. Platform* platform = new Platform(game);
  493. // Get the application module handle.
  494. __hinstance = ::GetModuleHandle(NULL);
  495. __attachToWindow = (HWND)attachToWindow;
  496. if (!__attachToWindow)
  497. {
  498. LPCTSTR windowClass = L"gameplay";
  499. std::wstring windowName;
  500. bool fullscreen = false;
  501. // Read window settings from config.
  502. if (game->getConfig())
  503. {
  504. Properties* config = game->getConfig()->getNamespace("window", true);
  505. if (config)
  506. {
  507. // Read window title.
  508. const char* title = config->getString("title");
  509. if (title)
  510. {
  511. int len = MultiByteToWideChar(CP_ACP, 0, title, -1, NULL, 0);
  512. wchar_t* wtitle = new wchar_t[len];
  513. MultiByteToWideChar(CP_ACP, 0, title, -1, wtitle, len);
  514. windowName = wtitle;
  515. SAFE_DELETE_ARRAY(wtitle);
  516. }
  517. // Read window size.
  518. int width = config->getInt("width");
  519. if (width != 0)
  520. __width = width;
  521. int height = config->getInt("height");
  522. if (height != 0)
  523. __height = height;
  524. // Read fullscreen state.
  525. fullscreen = config->getBool("fullscreen");
  526. }
  527. }
  528. RECT rect = { 0, 0, __width, __height };
  529. // Register our window class.
  530. WNDCLASSEX wc;
  531. wc.cbSize = sizeof(WNDCLASSEX);
  532. wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  533. wc.lpfnWndProc = (WNDPROC)__WndProc;
  534. wc.cbClsExtra = 0;
  535. wc.cbWndExtra = 0;
  536. wc.hInstance = __hinstance;
  537. wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  538. wc.hIconSm = NULL;
  539. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  540. wc.hbrBackground = NULL; // No brush - we are going to paint our own background
  541. wc.lpszMenuName = NULL; // No default menu
  542. wc.lpszClassName = windowClass;
  543. if (!::RegisterClassEx(&wc))
  544. {
  545. GP_ERROR("Failed to register window class.");
  546. goto error;
  547. }
  548. if (fullscreen)
  549. {
  550. DEVMODE dm;
  551. memset(&dm, 0, sizeof(dm));
  552. dm.dmSize= sizeof(dm);
  553. dm.dmPelsWidth = __width;
  554. dm.dmPelsHeight = __height;
  555. dm.dmBitsPerPel = 32;
  556. dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  557. // Try to set selected mode and get results. NOTE: CDS_FULLSCREEN gets rid of start bar.
  558. if (ChangeDisplaySettings(&dm, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
  559. {
  560. fullscreen = false;
  561. GP_ERROR("Failed to start game in full-screen mode with resolution %dx%d.", __width, __height);
  562. goto error;
  563. }
  564. }
  565. // Set the window style.
  566. DWORD style = (fullscreen ? WS_POPUP : WS_POPUP | WS_BORDER | WS_CAPTION | WS_SYSMENU) | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
  567. DWORD styleEx = (fullscreen ? WS_EX_APPWINDOW : WS_EX_APPWINDOW | WS_EX_WINDOWEDGE);
  568. // Adjust the window rectangle so the client size is the requested size.
  569. AdjustWindowRectEx(&rect, style, FALSE, styleEx);
  570. // Create the native Windows window.
  571. __hwnd = CreateWindowEx(styleEx, windowClass, windowName.c_str(), style, 0, 0, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, __hinstance, NULL);
  572. // Get the drawing context.
  573. __hdc = GetDC(__hwnd);
  574. // Center the window
  575. const int screenX = (GetSystemMetrics(SM_CXSCREEN) - __width) / 2;
  576. const int screenY = (GetSystemMetrics(SM_CYSCREEN) - __height) / 2;
  577. ::SetWindowPos(__hwnd, __hwnd, screenX, screenY, -1, -1, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
  578. if (!initializeGL())
  579. goto error;
  580. // Show the window.
  581. ShowWindow(__hwnd, SW_SHOW);
  582. }
  583. else
  584. {
  585. // Attach to a previous windows
  586. __hwnd = (HWND)__attachToWindow;
  587. __hdc = GetDC(__hwnd);
  588. SetWindowLongPtr(__hwnd, GWL_WNDPROC, (LONG)(WNDPROC)__WndProc);
  589. if (!initializeGL())
  590. goto error;
  591. }
  592. return platform;
  593. error:
  594. exit(0);
  595. return NULL;
  596. }
  597. int Platform::enterMessagePump()
  598. {
  599. GP_ASSERT(_game);
  600. int rc = 0;
  601. // Get the initial time.
  602. LARGE_INTEGER tps;
  603. QueryPerformanceFrequency(&tps);
  604. __timeTicksPerMillis = (double)(tps.QuadPart / 1000L);
  605. LARGE_INTEGER queryTime;
  606. QueryPerformanceCounter(&queryTime);
  607. GP_ASSERT(__timeTicksPerMillis);
  608. __timeStart = queryTime.QuadPart / __timeTicksPerMillis;
  609. // Set the initial pitch and roll values.
  610. __pitch = 0.0;
  611. __roll = 0.0;
  612. SwapBuffers(__hdc);
  613. if (_game->getState() != Game::RUNNING)
  614. _game->run();
  615. if (__attachToWindow)
  616. return 0;
  617. // Enter event dispatch loop.
  618. MSG msg;
  619. while (true)
  620. {
  621. if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  622. {
  623. TranslateMessage(&msg);
  624. DispatchMessage(&msg);
  625. if (msg.message == WM_QUIT)
  626. {
  627. _game->exit();
  628. break;
  629. }
  630. }
  631. else
  632. {
  633. _game->frame();
  634. SwapBuffers(__hdc);
  635. }
  636. // If we are done, then exit.
  637. if (_game->getState() == Game::UNINITIALIZED)
  638. break;
  639. }
  640. return msg.wParam;
  641. }
  642. void Platform::signalShutdown()
  643. {
  644. // nothing to do
  645. }
  646. unsigned int Platform::getDisplayWidth()
  647. {
  648. return __width;
  649. }
  650. unsigned int Platform::getDisplayHeight()
  651. {
  652. return __height;
  653. }
  654. double Platform::getAbsoluteTime()
  655. {
  656. LARGE_INTEGER queryTime;
  657. QueryPerformanceCounter(&queryTime);
  658. GP_ASSERT(__timeTicksPerMillis);
  659. __timeAbsolute = queryTime.QuadPart / __timeTicksPerMillis;
  660. return __timeAbsolute;
  661. }
  662. void Platform::setAbsoluteTime(double time)
  663. {
  664. __timeAbsolute = time;
  665. }
  666. bool Platform::isVsync()
  667. {
  668. return __vsync;
  669. }
  670. void Platform::setVsync(bool enable)
  671. {
  672. wglSwapIntervalEXT(enable ? 1 : 0);
  673. __vsync = enable;
  674. }
  675. void Platform::setMultiTouch(bool enabled)
  676. {
  677. // not supported
  678. }
  679. bool Platform::isMultiTouch()
  680. {
  681. return false;
  682. }
  683. void Platform::getAccelerometerValues(float* pitch, float* roll)
  684. {
  685. GP_ASSERT(pitch);
  686. GP_ASSERT(roll);
  687. *pitch = __pitch;
  688. *roll = __roll;
  689. }
  690. bool Platform::hasMouse()
  691. {
  692. return true;
  693. }
  694. void Platform::setMouseCaptured(bool captured)
  695. {
  696. if (captured != __mouseCaptured)
  697. {
  698. if (captured)
  699. {
  700. // Hide the cursor and warp it to the center of the screen
  701. __mouseCapturePoint.x = getDisplayWidth() / 2;
  702. __mouseCapturePoint.y = getDisplayHeight() / 2;
  703. ShowCursor(FALSE);
  704. WarpMouse(__mouseCapturePoint.x, __mouseCapturePoint.y);
  705. }
  706. else
  707. {
  708. // Restore cursor
  709. WarpMouse(__mouseCapturePoint.x, __mouseCapturePoint.y);
  710. ShowCursor(TRUE);
  711. }
  712. __mouseCaptured = captured;
  713. }
  714. }
  715. bool Platform::isMouseCaptured()
  716. {
  717. return __mouseCaptured;
  718. }
  719. void Platform::setCursorVisible(bool visible)
  720. {
  721. if (visible != __cursorVisible)
  722. {
  723. ShowCursor(visible ? TRUE : FALSE);
  724. __cursorVisible = visible;
  725. }
  726. }
  727. bool Platform::isCursorVisible()
  728. {
  729. return __cursorVisible;
  730. }
  731. void Platform::swapBuffers()
  732. {
  733. if (__hdc)
  734. SwapBuffers(__hdc);
  735. }
  736. void Platform::displayKeyboard(bool display)
  737. {
  738. // Do nothing.
  739. }
  740. void Platform::touchEventInternal(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  741. {
  742. if (!Form::touchEventInternal(evt, x, y, contactIndex))
  743. Game::getInstance()->touchEvent(evt, x, y, contactIndex);
  744. }
  745. void Platform::keyEventInternal(Keyboard::KeyEvent evt, int key)
  746. {
  747. if (!Form::keyEventInternal(evt, key))
  748. Game::getInstance()->keyEvent(evt, key);
  749. }
  750. bool Platform::mouseEventInternal(Mouse::MouseEvent evt, int x, int y, int wheelDelta)
  751. {
  752. if (Form::mouseEventInternal(evt, x, y, wheelDelta))
  753. {
  754. return true;
  755. }
  756. else
  757. {
  758. return Game::getInstance()->mouseEvent(evt, x, y, wheelDelta);
  759. }
  760. }
  761. void Platform::sleep(long ms)
  762. {
  763. Sleep(ms);
  764. }
  765. }
  766. #endif