PlatformWindows.cpp 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  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 "Vector2.h"
  8. #include "ScriptController.h"
  9. #include <GL/wglew.h>
  10. #include <windowsx.h>
  11. #include <shellapi.h>
  12. #ifdef USE_XINPUT
  13. #include <XInput.h>
  14. #endif
  15. using gameplay::print;
  16. // Window defaults
  17. #define DEFAULT_RESOLUTION_X 1024
  18. #define DEFAULT_RESOLUTION_Y 768
  19. #define DEFAULT_COLOR_BUFFER_SIZE 32
  20. #define DEFAULT_DEPTH_BUFFER_SIZE 24
  21. #define DEFAULT_STENCIL_BUFFER_SIZE 8
  22. static double __timeTicksPerMillis;
  23. static double __timeStart;
  24. static double __timeAbsolute;
  25. static bool __vsync = WINDOW_VSYNC;
  26. static float __roll;
  27. static float __pitch;
  28. static HINSTANCE __hinstance = 0;
  29. static HWND __attachToWindow = 0;
  30. static HWND __hwnd = 0;
  31. static HDC __hdc = 0;
  32. static HGLRC __hrc = 0;
  33. static bool __mouseCaptured = false;
  34. static POINT __mouseCapturePoint = { 0, 0 };
  35. static bool __multiSampling = false;
  36. static bool __cursorVisible = true;
  37. static unsigned int __gamepadsConnected = 0;
  38. #ifdef USE_XINPUT
  39. static const unsigned int XINPUT_BUTTON_COUNT = 14;
  40. static const unsigned int XINPUT_JOYSTICK_COUNT = 2;
  41. static const unsigned int XINPUT_TRIGGER_COUNT = 2;
  42. #endif
  43. #ifdef USE_XINPUT
  44. static XINPUT_STATE __xInputState;
  45. static bool __connectedXInput[4];
  46. static float normalizeXInputJoystickAxis(int axisValue, int deadZone)
  47. {
  48. int absAxisValue = abs(axisValue);
  49. if (absAxisValue < deadZone)
  50. {
  51. return 0.0f;
  52. }
  53. else
  54. {
  55. int value = axisValue;
  56. int maxVal;
  57. if (value < 0)
  58. {
  59. value = -1;
  60. maxVal = 32768;
  61. }
  62. else if (value > 0)
  63. {
  64. value = 1;
  65. maxVal = 32767;
  66. }
  67. else
  68. {
  69. return 0;
  70. }
  71. return value * (absAxisValue - deadZone) / (float)(maxVal - deadZone);
  72. }
  73. }
  74. #endif
  75. static gameplay::Keyboard::Key getKey(WPARAM win32KeyCode, bool shiftDown)
  76. {
  77. switch (win32KeyCode)
  78. {
  79. case VK_PAUSE:
  80. return gameplay::Keyboard::KEY_PAUSE;
  81. case VK_SCROLL:
  82. return gameplay::Keyboard::KEY_SCROLL_LOCK;
  83. case VK_PRINT:
  84. return gameplay::Keyboard::KEY_PRINT;
  85. case VK_ESCAPE:
  86. return gameplay::Keyboard::KEY_ESCAPE;
  87. case VK_BACK:
  88. return gameplay::Keyboard::KEY_BACKSPACE;
  89. case VK_TAB:
  90. return shiftDown ? gameplay::Keyboard::KEY_BACK_TAB : gameplay::Keyboard::KEY_TAB;
  91. case VK_RETURN:
  92. return gameplay::Keyboard::KEY_RETURN;
  93. case VK_CAPITAL:
  94. return gameplay::Keyboard::KEY_CAPS_LOCK;
  95. case VK_SHIFT:
  96. return gameplay::Keyboard::KEY_SHIFT;
  97. case VK_CONTROL:
  98. return gameplay::Keyboard::KEY_CTRL;
  99. case VK_MENU:
  100. return gameplay::Keyboard::KEY_ALT;
  101. case VK_APPS:
  102. return gameplay::Keyboard::KEY_MENU;
  103. case VK_LSHIFT:
  104. return gameplay::Keyboard::KEY_SHIFT;
  105. case VK_RSHIFT:
  106. return gameplay::Keyboard::KEY_SHIFT;
  107. case VK_LCONTROL:
  108. return gameplay::Keyboard::KEY_CTRL;
  109. case VK_RCONTROL:
  110. return gameplay::Keyboard::KEY_CTRL;
  111. case VK_LMENU:
  112. return gameplay::Keyboard::KEY_ALT;
  113. case VK_RMENU:
  114. return gameplay::Keyboard::KEY_ALT;
  115. case VK_LWIN:
  116. case VK_RWIN:
  117. return gameplay::Keyboard::KEY_HYPER;
  118. case VK_BROWSER_SEARCH:
  119. return gameplay::Keyboard::KEY_SEARCH;
  120. case VK_INSERT:
  121. return gameplay::Keyboard::KEY_INSERT;
  122. case VK_HOME:
  123. return gameplay::Keyboard::KEY_HOME;
  124. case VK_PRIOR:
  125. return gameplay::Keyboard::KEY_PG_UP;
  126. case VK_DELETE:
  127. return gameplay::Keyboard::KEY_DELETE;
  128. case VK_END:
  129. return gameplay::Keyboard::KEY_END;
  130. case VK_NEXT:
  131. return gameplay::Keyboard::KEY_PG_DOWN;
  132. case VK_LEFT:
  133. return gameplay::Keyboard::KEY_LEFT_ARROW;
  134. case VK_RIGHT:
  135. return gameplay::Keyboard::KEY_RIGHT_ARROW;
  136. case VK_UP:
  137. return gameplay::Keyboard::KEY_UP_ARROW;
  138. case VK_DOWN:
  139. return gameplay::Keyboard::KEY_DOWN_ARROW;
  140. case VK_NUMLOCK:
  141. return gameplay::Keyboard::KEY_NUM_LOCK;
  142. case VK_ADD:
  143. return gameplay::Keyboard::KEY_KP_PLUS;
  144. case VK_SUBTRACT:
  145. return gameplay::Keyboard::KEY_KP_MINUS;
  146. case VK_MULTIPLY:
  147. return gameplay::Keyboard::KEY_KP_MULTIPLY;
  148. case VK_DIVIDE:
  149. return gameplay::Keyboard::KEY_KP_DIVIDE;
  150. case VK_NUMPAD7:
  151. return gameplay::Keyboard::KEY_KP_HOME;
  152. case VK_NUMPAD8:
  153. return gameplay::Keyboard::KEY_KP_UP;
  154. case VK_NUMPAD9:
  155. return gameplay::Keyboard::KEY_KP_PG_UP;
  156. case VK_NUMPAD4:
  157. return gameplay::Keyboard::KEY_KP_LEFT;
  158. case VK_NUMPAD5:
  159. return gameplay::Keyboard::KEY_KP_FIVE;
  160. case VK_NUMPAD6:
  161. return gameplay::Keyboard::KEY_KP_RIGHT;
  162. case VK_NUMPAD1:
  163. return gameplay::Keyboard::KEY_KP_END;
  164. case VK_NUMPAD2:
  165. return gameplay::Keyboard::KEY_KP_DOWN;
  166. case VK_NUMPAD3:
  167. return gameplay::Keyboard::KEY_KP_PG_DOWN;
  168. case VK_NUMPAD0:
  169. return gameplay::Keyboard::KEY_KP_INSERT;
  170. case VK_DECIMAL:
  171. return gameplay::Keyboard::KEY_KP_DELETE;
  172. case VK_F1:
  173. return gameplay::Keyboard::KEY_F1;
  174. case VK_F2:
  175. return gameplay::Keyboard::KEY_F2;
  176. case VK_F3:
  177. return gameplay::Keyboard::KEY_F3;
  178. case VK_F4:
  179. return gameplay::Keyboard::KEY_F4;
  180. case VK_F5:
  181. return gameplay::Keyboard::KEY_F5;
  182. case VK_F6:
  183. return gameplay::Keyboard::KEY_F6;
  184. case VK_F7:
  185. return gameplay::Keyboard::KEY_F7;
  186. case VK_F8:
  187. return gameplay::Keyboard::KEY_F8;
  188. case VK_F9:
  189. return gameplay::Keyboard::KEY_F9;
  190. case VK_F10:
  191. return gameplay::Keyboard::KEY_F10;
  192. case VK_F11:
  193. return gameplay::Keyboard::KEY_F11;
  194. case VK_F12:
  195. return gameplay::Keyboard::KEY_F12;
  196. case VK_SPACE:
  197. return gameplay::Keyboard::KEY_SPACE;
  198. case 0x30:
  199. return shiftDown ? gameplay::Keyboard::KEY_RIGHT_PARENTHESIS : gameplay::Keyboard::KEY_ZERO;
  200. case 0x31:
  201. return shiftDown ? gameplay::Keyboard::KEY_EXCLAM : gameplay::Keyboard::KEY_ONE;
  202. case 0x32:
  203. return shiftDown ? gameplay::Keyboard::KEY_AT : gameplay::Keyboard::KEY_TWO;
  204. case 0x33:
  205. return shiftDown ? gameplay::Keyboard::KEY_NUMBER : gameplay::Keyboard::KEY_THREE;
  206. case 0x34:
  207. return shiftDown ? gameplay::Keyboard::KEY_DOLLAR : gameplay::Keyboard::KEY_FOUR;
  208. case 0x35:
  209. return shiftDown ? gameplay::Keyboard::KEY_PERCENT : gameplay::Keyboard::KEY_FIVE;
  210. case 0x36:
  211. return shiftDown ? gameplay::Keyboard::KEY_CIRCUMFLEX : gameplay::Keyboard::KEY_SIX;
  212. case 0x37:
  213. return shiftDown ? gameplay::Keyboard::KEY_AMPERSAND : gameplay::Keyboard::KEY_SEVEN;
  214. case 0x38:
  215. return shiftDown ? gameplay::Keyboard::KEY_ASTERISK : gameplay::Keyboard::KEY_EIGHT;
  216. case 0x39:
  217. return shiftDown ? gameplay::Keyboard::KEY_LEFT_PARENTHESIS : gameplay::Keyboard::KEY_NINE;
  218. case VK_OEM_PLUS:
  219. return shiftDown ? gameplay::Keyboard::KEY_EQUAL : gameplay::Keyboard::KEY_PLUS;
  220. case VK_OEM_COMMA:
  221. return shiftDown ? gameplay::Keyboard::KEY_LESS_THAN : gameplay::Keyboard::KEY_COMMA;
  222. case VK_OEM_MINUS:
  223. return shiftDown ? gameplay::Keyboard::KEY_UNDERSCORE : gameplay::Keyboard::KEY_MINUS;
  224. case VK_OEM_PERIOD:
  225. return shiftDown ? gameplay::Keyboard::KEY_GREATER_THAN : gameplay::Keyboard::KEY_PERIOD;
  226. case VK_OEM_1:
  227. return shiftDown ? gameplay::Keyboard::KEY_COLON : gameplay::Keyboard::KEY_SEMICOLON;
  228. case VK_OEM_2:
  229. return shiftDown ? gameplay::Keyboard::KEY_QUESTION : gameplay::Keyboard::KEY_SLASH;
  230. case VK_OEM_3:
  231. return shiftDown ? gameplay::Keyboard::KEY_TILDE : gameplay::Keyboard::KEY_GRAVE;
  232. case VK_OEM_4:
  233. return shiftDown ? gameplay::Keyboard::KEY_LEFT_BRACE : gameplay::Keyboard::KEY_LEFT_BRACKET;
  234. case VK_OEM_5:
  235. return shiftDown ? gameplay::Keyboard::KEY_BAR : gameplay::Keyboard::KEY_BACK_SLASH;
  236. case VK_OEM_6:
  237. return shiftDown ? gameplay::Keyboard::KEY_RIGHT_BRACE : gameplay::Keyboard::KEY_RIGHT_BRACKET;
  238. case VK_OEM_7:
  239. return shiftDown ? gameplay::Keyboard::KEY_QUOTE : gameplay::Keyboard::KEY_APOSTROPHE;
  240. case 0x41:
  241. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_A : gameplay::Keyboard::KEY_A;
  242. case 0x42:
  243. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_B : gameplay::Keyboard::KEY_B;
  244. case 0x43:
  245. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_C : gameplay::Keyboard::KEY_C;
  246. case 0x44:
  247. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_D : gameplay::Keyboard::KEY_D;
  248. case 0x45:
  249. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_E : gameplay::Keyboard::KEY_E;
  250. case 0x46:
  251. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_F : gameplay::Keyboard::KEY_F;
  252. case 0x47:
  253. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_G : gameplay::Keyboard::KEY_G;
  254. case 0x48:
  255. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_H : gameplay::Keyboard::KEY_H;
  256. case 0x49:
  257. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_I : gameplay::Keyboard::KEY_I;
  258. case 0x4A:
  259. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_J : gameplay::Keyboard::KEY_J;
  260. case 0x4B:
  261. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_K : gameplay::Keyboard::KEY_K;
  262. case 0x4C:
  263. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_L : gameplay::Keyboard::KEY_L;
  264. case 0x4D:
  265. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_M : gameplay::Keyboard::KEY_M;
  266. case 0x4E:
  267. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_N : gameplay::Keyboard::KEY_N;
  268. case 0x4F:
  269. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_O : gameplay::Keyboard::KEY_O;
  270. case 0x50:
  271. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_P : gameplay::Keyboard::KEY_P;
  272. case 0x51:
  273. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_Q : gameplay::Keyboard::KEY_Q;
  274. case 0x52:
  275. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_R : gameplay::Keyboard::KEY_R;
  276. case 0x53:
  277. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_S : gameplay::Keyboard::KEY_S;
  278. case 0x54:
  279. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_T : gameplay::Keyboard::KEY_T;
  280. case 0x55:
  281. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_U : gameplay::Keyboard::KEY_U;
  282. case 0x56:
  283. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_V : gameplay::Keyboard::KEY_V;
  284. case 0x57:
  285. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_W : gameplay::Keyboard::KEY_W;
  286. case 0x58:
  287. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_X : gameplay::Keyboard::KEY_X;
  288. case 0x59:
  289. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_Y : gameplay::Keyboard::KEY_Y;
  290. case 0x5A:
  291. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_Z : gameplay::Keyboard::KEY_Z;
  292. default:
  293. return gameplay::Keyboard::KEY_NONE;
  294. }
  295. }
  296. static void UpdateCapture(LPARAM lParam)
  297. {
  298. if ((lParam & MK_LBUTTON) || (lParam & MK_MBUTTON) || (lParam & MK_RBUTTON))
  299. SetCapture(__hwnd);
  300. else
  301. ReleaseCapture();
  302. }
  303. static void WarpMouse(int clientX, int clientY)
  304. {
  305. POINT p = { clientX, clientY };
  306. ClientToScreen(__hwnd, &p);
  307. SetCursorPos(p.x, p.y);
  308. }
  309. /**
  310. * Gets the width and height of the screen in pixels.
  311. */
  312. static void getDesktopResolution(int& width, int& height)
  313. {
  314. RECT desktop;
  315. const HWND hDesktop = GetDesktopWindow();
  316. // Get the size of screen to the variable desktop
  317. GetWindowRect(hDesktop, &desktop);
  318. width = desktop.right;
  319. height = desktop.bottom;
  320. }
  321. LRESULT CALLBACK __WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  322. {
  323. static gameplay::Game* game = gameplay::Game::getInstance();
  324. if (!game->isInitialized() || hwnd != __hwnd)
  325. {
  326. // Ignore messages that are not for our game window.
  327. return DefWindowProc(hwnd, msg, wParam, lParam);
  328. }
  329. static int lx, ly;
  330. static bool shiftDown = false;
  331. static bool capsOn = false;
  332. switch (msg)
  333. {
  334. case WM_CLOSE:
  335. DestroyWindow(__hwnd);
  336. return 0;
  337. case WM_DESTROY:
  338. gameplay::Platform::shutdownInternal();
  339. PostQuitMessage(0);
  340. return 0;
  341. case WM_LBUTTONDOWN:
  342. {
  343. int x = GET_X_LPARAM(lParam);
  344. int y = GET_Y_LPARAM(lParam);
  345. UpdateCapture(wParam);
  346. if (!gameplay::Platform::mouseEventInternal(gameplay::Mouse::MOUSE_PRESS_LEFT_BUTTON, x, y, 0))
  347. {
  348. gameplay::Platform::touchEventInternal(gameplay::Touch::TOUCH_PRESS, x, y, 0);
  349. }
  350. return 0;
  351. }
  352. case WM_LBUTTONUP:
  353. {
  354. int x = GET_X_LPARAM(lParam);
  355. int y = GET_Y_LPARAM(lParam);
  356. if (!gameplay::Platform::mouseEventInternal(gameplay::Mouse::MOUSE_RELEASE_LEFT_BUTTON, x, y, 0))
  357. {
  358. gameplay::Platform::touchEventInternal(gameplay::Touch::TOUCH_RELEASE, x, y, 0);
  359. }
  360. UpdateCapture(wParam);
  361. return 0;
  362. }
  363. case WM_RBUTTONDOWN:
  364. UpdateCapture(wParam);
  365. lx = GET_X_LPARAM(lParam);
  366. ly = GET_Y_LPARAM(lParam);
  367. gameplay::Platform::mouseEventInternal(gameplay::Mouse::MOUSE_PRESS_RIGHT_BUTTON, lx, ly, 0);
  368. break;
  369. case WM_RBUTTONUP:
  370. gameplay::Platform::mouseEventInternal(gameplay::Mouse::MOUSE_RELEASE_RIGHT_BUTTON, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), 0);
  371. UpdateCapture(wParam);
  372. break;
  373. case WM_MBUTTONDOWN:
  374. UpdateCapture(wParam);
  375. gameplay::Platform::mouseEventInternal(gameplay::Mouse::MOUSE_PRESS_MIDDLE_BUTTON, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), 0);
  376. break;
  377. case WM_MBUTTONUP:
  378. gameplay::Platform::mouseEventInternal(gameplay::Mouse::MOUSE_RELEASE_MIDDLE_BUTTON, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), 0);
  379. UpdateCapture(wParam);
  380. break;
  381. case WM_MOUSEMOVE:
  382. {
  383. int x = GET_X_LPARAM(lParam);
  384. int y = GET_Y_LPARAM(lParam);
  385. if (__mouseCaptured)
  386. {
  387. // If the incoming position is the mouse capture point, ignore this event
  388. // since this is the event that warped the cursor back.
  389. if (x == __mouseCapturePoint.x && y == __mouseCapturePoint.y)
  390. break;
  391. // Convert to deltas
  392. x -= __mouseCapturePoint.x;
  393. y -= __mouseCapturePoint.y;
  394. // Warp mouse back to center of screen.
  395. WarpMouse(__mouseCapturePoint.x, __mouseCapturePoint.y);
  396. }
  397. // Allow Game::mouseEvent a chance to handle (and possibly consume) the event.
  398. if (!gameplay::Platform::mouseEventInternal(gameplay::Mouse::MOUSE_MOVE, x, y, 0))
  399. {
  400. if ((wParam & MK_LBUTTON) == MK_LBUTTON)
  401. {
  402. // Mouse move events should be interpreted as touch move only if left mouse is held and the game did not consume the mouse event.
  403. gameplay::Platform::touchEventInternal(gameplay::Touch::TOUCH_MOVE, x, y, 0);
  404. return 0;
  405. }
  406. else if ((wParam & MK_RBUTTON) == MK_RBUTTON)
  407. {
  408. // Scale factors for the mouse movement used to simulate the accelerometer.
  409. RECT clientRect;
  410. GetClientRect(__hwnd, &clientRect);
  411. const float xFactor = 90.0f / clientRect.right;
  412. const float yFactor = 90.0f / clientRect.bottom;
  413. // Update the pitch and roll by adding the scaled deltas.
  414. __roll += (float)(x - lx) * xFactor;
  415. __pitch += -(float)(y - ly) * yFactor;
  416. // Clamp the values to the valid range.
  417. __roll = max(min(__roll, 90.0f), -90.0f);
  418. __pitch = max(min(__pitch, 90.0f), -90.0f);
  419. // Update the last X/Y values.
  420. lx = x;
  421. ly = y;
  422. }
  423. }
  424. break;
  425. }
  426. case WM_MOUSEWHEEL:
  427. tagPOINT point;
  428. point.x = GET_X_LPARAM(lParam);
  429. point.y = GET_Y_LPARAM(lParam);
  430. ScreenToClient(__hwnd, &point);
  431. gameplay::Platform::mouseEventInternal(gameplay::Mouse::MOUSE_WHEEL, point.x, point.y, GET_WHEEL_DELTA_WPARAM(wParam) / 120);
  432. break;
  433. case WM_KEYDOWN:
  434. if (wParam == VK_SHIFT || wParam == VK_LSHIFT || wParam == VK_RSHIFT)
  435. shiftDown = true;
  436. if (wParam == VK_CAPITAL)
  437. capsOn = !capsOn;
  438. // Suppress key repeats.
  439. if ((lParam & 0x40000000) == 0)
  440. gameplay::Platform::keyEventInternal(gameplay::Keyboard::KEY_PRESS, getKey(wParam, shiftDown ^ capsOn));
  441. break;
  442. case WM_KEYUP:
  443. if (wParam == VK_SHIFT || wParam == VK_LSHIFT || wParam == VK_RSHIFT)
  444. shiftDown = false;
  445. gameplay::Platform::keyEventInternal(gameplay::Keyboard::KEY_RELEASE, getKey(wParam, shiftDown ^ capsOn));
  446. break;
  447. case WM_CHAR:
  448. // Suppress key repeats.
  449. if ((lParam & 0x40000000) == 0)
  450. gameplay::Platform::keyEventInternal(gameplay::Keyboard::KEY_CHAR, wParam);
  451. break;
  452. case WM_UNICHAR:
  453. // Suppress key repeats.
  454. if ((lParam & 0x40000000) == 0)
  455. gameplay::Platform::keyEventInternal(gameplay::Keyboard::KEY_CHAR, wParam);
  456. break;
  457. case WM_SETFOCUS:
  458. break;
  459. case WM_KILLFOCUS:
  460. break;
  461. case WM_SIZE:
  462. // Window was resized.
  463. gameplay::Platform::resizeEventInternal((unsigned int)(short)LOWORD(lParam), (unsigned int)(short)HIWORD(lParam));
  464. break;
  465. }
  466. return DefWindowProc(hwnd, msg, wParam, lParam);
  467. }
  468. namespace gameplay
  469. {
  470. struct WindowCreationParams
  471. {
  472. RECT rect;
  473. std::wstring windowName;
  474. bool fullscreen;
  475. bool resizable;
  476. int samples;
  477. };
  478. extern void print(const char* format, ...)
  479. {
  480. va_list argptr;
  481. va_start(argptr, format);
  482. int sz = vfprintf(stderr, format, argptr);
  483. if (sz > 0)
  484. {
  485. char* buf = new char[sz + 1];
  486. vsprintf(buf, format, argptr);
  487. buf[sz] = 0;
  488. OutputDebugStringA(buf);
  489. SAFE_DELETE_ARRAY(buf);
  490. }
  491. va_end(argptr);
  492. }
  493. Platform::Platform(Game* game)
  494. : _game(game)
  495. {
  496. }
  497. Platform::~Platform()
  498. {
  499. if (__hwnd)
  500. {
  501. DestroyWindow(__hwnd);
  502. __hwnd = 0;
  503. }
  504. }
  505. bool createWindow(WindowCreationParams* params, HWND* hwnd, HDC* hdc)
  506. {
  507. bool fullscreen = false;
  508. bool resizable = false;
  509. RECT rect = { CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT };
  510. std::wstring windowName;
  511. if (params)
  512. {
  513. windowName = params->windowName;
  514. memcpy(&rect, &params->rect, sizeof(RECT));
  515. fullscreen = params->fullscreen;
  516. resizable = params->resizable;
  517. }
  518. // Set the window style.
  519. DWORD style, styleEx;
  520. if (fullscreen)
  521. {
  522. style = WS_POPUP;
  523. styleEx = WS_EX_APPWINDOW;
  524. }
  525. else
  526. {
  527. if (resizable)
  528. style = WS_OVERLAPPEDWINDOW;
  529. else
  530. style = WS_POPUP | WS_BORDER | WS_CAPTION | WS_SYSMENU;
  531. styleEx = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
  532. }
  533. style |= WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
  534. // Adjust the window rectangle so the client size is the requested size.
  535. AdjustWindowRectEx(&rect, style, FALSE, styleEx);
  536. // Create the native Windows window.
  537. *hwnd = CreateWindowEx(styleEx, L"gameplay", windowName.c_str(), style, 0, 0, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, __hinstance, NULL);
  538. if (*hwnd == NULL)
  539. {
  540. GP_ERROR("Failed to create window.");
  541. return false;
  542. }
  543. // Get the drawing context.
  544. *hdc = GetDC(*hwnd);
  545. if (*hdc == NULL)
  546. {
  547. GP_ERROR("Failed to get device context.");
  548. return false;
  549. }
  550. // Center the window
  551. GetWindowRect(*hwnd, &rect);
  552. const int screenX = (GetSystemMetrics(SM_CXSCREEN) - rect.right) / 2;
  553. const int screenY = (GetSystemMetrics(SM_CYSCREEN) - rect.bottom) / 2;
  554. SetWindowPos(*hwnd, *hwnd, screenX, screenY, -1, -1, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
  555. return true;
  556. }
  557. bool initializeGL(WindowCreationParams* params)
  558. {
  559. // Create a temporary window and context to we can initialize GLEW and get access
  560. // to additional OpenGL extension functions. This is a neccessary evil since the
  561. // function for querying GL extensions is a GL extension itself.
  562. HWND hwnd = NULL;
  563. HDC hdc = NULL;
  564. if (!createWindow(NULL, &hwnd, &hdc))
  565. return false;
  566. PIXELFORMATDESCRIPTOR pfd;
  567. memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
  568. pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
  569. pfd.nVersion = 1;
  570. pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
  571. pfd.iPixelType = PFD_TYPE_RGBA;
  572. pfd.cColorBits = DEFAULT_COLOR_BUFFER_SIZE;
  573. pfd.cDepthBits = DEFAULT_DEPTH_BUFFER_SIZE;
  574. pfd.cStencilBits = DEFAULT_STENCIL_BUFFER_SIZE;
  575. pfd.iLayerType = PFD_MAIN_PLANE;
  576. int pixelFormat = ChoosePixelFormat(hdc, &pfd);
  577. if (pixelFormat == 0)
  578. {
  579. DestroyWindow(hwnd);
  580. GP_ERROR("Failed to choose a pixel format.");
  581. return false;
  582. }
  583. if (!SetPixelFormat(hdc, pixelFormat, &pfd))
  584. {
  585. DestroyWindow(hwnd);
  586. GP_ERROR("Failed to set the pixel format.");
  587. return false;
  588. }
  589. HGLRC tempContext = wglCreateContext(hdc);
  590. if (!tempContext)
  591. {
  592. DestroyWindow(hwnd);
  593. GP_ERROR("Failed to create temporary context for initialization.");
  594. return false;
  595. }
  596. wglMakeCurrent(hdc, tempContext);
  597. // Initialize GLEW
  598. if (GLEW_OK != glewInit())
  599. {
  600. wglDeleteContext(tempContext);
  601. DestroyWindow(hwnd);
  602. GP_ERROR("Failed to initialize GLEW.");
  603. return false;
  604. }
  605. // Choose pixel format using wglChoosePixelFormatARB, which allows us to specify
  606. // additional attributes such as multisampling.
  607. //
  608. // Note: Keep multisampling attributes at the start of the attribute lists since code below
  609. // assumes they are array elements 0 through 3.
  610. int attribList[] = {
  611. WGL_SAMPLES_ARB, params->samples,
  612. WGL_SAMPLE_BUFFERS_ARB, params->samples > 0 ? 1 : 0,
  613. WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
  614. WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
  615. WGL_DOUBLE_BUFFER_ARB, GL_TRUE,
  616. WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB,
  617. WGL_COLOR_BITS_ARB, DEFAULT_COLOR_BUFFER_SIZE,
  618. WGL_DEPTH_BITS_ARB, DEFAULT_DEPTH_BUFFER_SIZE,
  619. WGL_STENCIL_BITS_ARB, DEFAULT_STENCIL_BUFFER_SIZE,
  620. 0
  621. };
  622. __multiSampling = params->samples > 0;
  623. UINT numFormats;
  624. if (!wglChoosePixelFormatARB(hdc, attribList, NULL, 1, &pixelFormat, &numFormats) || numFormats == 0)
  625. {
  626. bool valid = false;
  627. if (params->samples > 0)
  628. {
  629. GP_WARN("Failed to choose pixel format with WGL_SAMPLES_ARB == %d. Attempting to fallback to lower samples setting.", params->samples);
  630. while (params->samples > 0)
  631. {
  632. params->samples /= 2;
  633. attribList[1] = params->samples;
  634. attribList[3] = params->samples > 0 ? 1 : 0;
  635. if (wglChoosePixelFormatARB(hdc, attribList, NULL, 1, &pixelFormat, &numFormats) && numFormats > 0)
  636. {
  637. valid = true;
  638. GP_WARN("Found pixel format with WGL_SAMPLES_ARB == %d.", params->samples);
  639. break;
  640. }
  641. }
  642. __multiSampling = params->samples > 0;
  643. }
  644. if (!valid)
  645. {
  646. wglDeleteContext(tempContext);
  647. DestroyWindow(hwnd);
  648. GP_ERROR("Failed to choose a pixel format.");
  649. return false;
  650. }
  651. }
  652. // Destroy old window
  653. DestroyWindow(hwnd);
  654. hwnd = NULL;
  655. hdc = NULL;
  656. // Create new/final window if needed
  657. if (params)
  658. {
  659. if (!createWindow(params, &__hwnd, &__hdc))
  660. {
  661. wglDeleteContext(tempContext);
  662. return false;
  663. }
  664. }
  665. // Set final pixel format for window
  666. if (!SetPixelFormat(__hdc, pixelFormat, &pfd))
  667. {
  668. GP_ERROR("Failed to set the pixel format: %d.", (int)GetLastError());
  669. return false;
  670. }
  671. // Create our new GL context
  672. int attribs[] =
  673. {
  674. WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
  675. WGL_CONTEXT_MINOR_VERSION_ARB, 1,
  676. 0
  677. };
  678. if (!(__hrc = wglCreateContextAttribsARB(__hdc, 0, attribs) ) )
  679. {
  680. wglDeleteContext(tempContext);
  681. GP_ERROR("Failed to create OpenGL context.");
  682. return false;
  683. }
  684. // Delete the old/temporary context and window
  685. wglDeleteContext(tempContext);
  686. // Make the new context current
  687. if (!wglMakeCurrent(__hdc, __hrc) || !__hrc)
  688. {
  689. GP_ERROR("Failed to make the window current.");
  690. return false;
  691. }
  692. // Vertical sync.
  693. wglSwapIntervalEXT(__vsync ? 1 : 0);
  694. return true;
  695. }
  696. Platform* Platform::create(Game* game, void* attachToWindow)
  697. {
  698. GP_ASSERT(game);
  699. FileSystem::setResourcePath("./");
  700. Platform* platform = new Platform(game);
  701. // Get the application module handle.
  702. __hinstance = ::GetModuleHandle(NULL);
  703. __attachToWindow = (HWND)attachToWindow;
  704. // Read window settings from config.
  705. WindowCreationParams params;
  706. params.fullscreen = false;
  707. params.resizable = false;
  708. params.rect.left = 0;
  709. params.rect.top = 0;
  710. params.rect.right = 0;
  711. params.rect.bottom = 0;
  712. params.samples = 0;
  713. if (game->getConfig())
  714. {
  715. Properties* config = game->getConfig()->getNamespace("window", true);
  716. if (config)
  717. {
  718. // Read window title.
  719. const char* title = config->getString("title");
  720. if (title)
  721. {
  722. int len = MultiByteToWideChar(CP_ACP, 0, title, -1, NULL, 0);
  723. wchar_t* wtitle = new wchar_t[len];
  724. MultiByteToWideChar(CP_ACP, 0, title, -1, wtitle, len);
  725. params.windowName = wtitle;
  726. SAFE_DELETE_ARRAY(wtitle);
  727. }
  728. // Read fullscreen state.
  729. params.fullscreen = config->getBool("fullscreen");
  730. // Read resizable state.
  731. params.resizable = config->getBool("resizable");
  732. // Read multisampling state.
  733. params.samples = config->getInt("samples");
  734. // Read window rect.
  735. int x = config->getInt("x");
  736. if (x != 0)
  737. params.rect.left = x;
  738. int y = config->getInt("y");
  739. if (y != 0)
  740. params.rect.top = y;
  741. int width = config->getInt("width");
  742. int height = config->getInt("height");
  743. if (width == 0 && height == 0 && params.fullscreen)
  744. getDesktopResolution(width, height);
  745. if (width != 0)
  746. params.rect.right = params.rect.left + width;
  747. if (height != 0)
  748. params.rect.bottom = params.rect.top + height;
  749. }
  750. }
  751. // If window size was not specified, set it to a default value
  752. if (params.rect.right == 0)
  753. params.rect.right = params.rect.left + DEFAULT_RESOLUTION_X;
  754. if (params.rect.bottom == 0)
  755. params.rect.bottom = params.rect.top + DEFAULT_RESOLUTION_Y;
  756. int width = params.rect.right - params.rect.left;
  757. int height = params.rect.bottom - params.rect.top;
  758. if (params.fullscreen)
  759. {
  760. // Enumerate all supposed display settings
  761. bool modeSupported = false;
  762. DWORD modeNum = 0;
  763. DEVMODE devMode;
  764. memset(&devMode, 0, sizeof(DEVMODE));
  765. devMode.dmSize = sizeof(DEVMODE);
  766. devMode.dmDriverExtra = 0;
  767. while (EnumDisplaySettings(NULL, modeNum++, &devMode) != 0)
  768. {
  769. // Is mode supported?
  770. if (devMode.dmPelsWidth == width &&
  771. devMode.dmPelsHeight == height &&
  772. devMode.dmBitsPerPel == DEFAULT_COLOR_BUFFER_SIZE)
  773. {
  774. modeSupported = true;
  775. break;
  776. }
  777. }
  778. // If the requested mode is not supported, fall back to a safe default
  779. if (!modeSupported)
  780. {
  781. width = DEFAULT_RESOLUTION_X;
  782. height = DEFAULT_RESOLUTION_Y;
  783. params.rect.right = params.rect.left + width;
  784. params.rect.bottom = params.rect.top + height;
  785. }
  786. }
  787. if (!__attachToWindow)
  788. {
  789. // Register our window class.
  790. WNDCLASSEX wc;
  791. wc.cbSize = sizeof(WNDCLASSEX);
  792. wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  793. wc.lpfnWndProc = (WNDPROC)__WndProc;
  794. wc.cbClsExtra = 0;
  795. wc.cbWndExtra = 0;
  796. wc.hInstance = __hinstance;
  797. wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  798. wc.hIconSm = NULL;
  799. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  800. wc.hbrBackground = NULL; // No brush - we are going to paint our own background
  801. wc.lpszMenuName = NULL; // No default menu
  802. wc.lpszClassName = L"gameplay";
  803. if (!::RegisterClassEx(&wc))
  804. {
  805. GP_ERROR("Failed to register window class.");
  806. goto error;
  807. }
  808. if (params.fullscreen)
  809. {
  810. DEVMODE dm;
  811. memset(&dm, 0, sizeof(dm));
  812. dm.dmSize= sizeof(dm);
  813. dm.dmPelsWidth = width;
  814. dm.dmPelsHeight = height;
  815. dm.dmBitsPerPel = DEFAULT_COLOR_BUFFER_SIZE;
  816. dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  817. // Try to set selected mode and get results. NOTE: CDS_FULLSCREEN gets rid of start bar.
  818. if (ChangeDisplaySettings(&dm, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
  819. {
  820. params.fullscreen = false;
  821. GP_ERROR("Failed to start game in full-screen mode with resolution %dx%d.", width, height);
  822. goto error;
  823. }
  824. }
  825. if (!initializeGL(&params))
  826. goto error;
  827. // Show the window.
  828. ShowWindow(__hwnd, SW_SHOW);
  829. }
  830. else
  831. {
  832. // Attach to a previous windows
  833. __hwnd = (HWND)__attachToWindow;
  834. __hdc = GetDC(__hwnd);
  835. SetWindowLongPtr(__hwnd, GWLP_WNDPROC, (LONG)(WNDPROC)__WndProc);
  836. if (!initializeGL(NULL))
  837. goto error;
  838. }
  839. #ifdef USE_XINPUT
  840. // Initialize XInputGamepads.
  841. for (DWORD i = 0; i < XUSER_MAX_COUNT; i++)
  842. {
  843. if (XInputGetState(i, &__xInputState) == NO_ERROR)
  844. {
  845. if (!__connectedXInput[i])
  846. {
  847. // Gamepad is connected.
  848. Platform::gamepadEventConnectedInternal(i, XINPUT_BUTTON_COUNT, XINPUT_JOYSTICK_COUNT, XINPUT_TRIGGER_COUNT, 0, 0, "Microsoft", "XBox360 Controller");
  849. __connectedXInput[i] = true;
  850. }
  851. }
  852. }
  853. #endif
  854. return platform;
  855. error:
  856. exit(0);
  857. return NULL;
  858. }
  859. int Platform::enterMessagePump()
  860. {
  861. GP_ASSERT(_game);
  862. // Get the initial time.
  863. LARGE_INTEGER tps;
  864. QueryPerformanceFrequency(&tps);
  865. __timeTicksPerMillis = (double)(tps.QuadPart / 1000L);
  866. LARGE_INTEGER queryTime;
  867. QueryPerformanceCounter(&queryTime);
  868. GP_ASSERT(__timeTicksPerMillis);
  869. __timeStart = queryTime.QuadPart / __timeTicksPerMillis;
  870. // Set the initial pitch and roll values.
  871. __pitch = 0.0;
  872. __roll = 0.0;
  873. SwapBuffers(__hdc);
  874. if (_game->getState() != Game::RUNNING)
  875. _game->run();
  876. if (__attachToWindow)
  877. return 0;
  878. // Enter event dispatch loop.
  879. MSG msg;
  880. while (true)
  881. {
  882. if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  883. {
  884. TranslateMessage(&msg);
  885. DispatchMessage(&msg);
  886. if (msg.message == WM_QUIT)
  887. {
  888. gameplay::Platform::shutdownInternal();
  889. return msg.wParam;
  890. }
  891. }
  892. else
  893. {
  894. #ifdef USE_XINPUT
  895. // Check for connected XInput gamepads.
  896. for (DWORD i = 0; i < XUSER_MAX_COUNT; i++)
  897. {
  898. if (XInputGetState(i, &__xInputState) == NO_ERROR && !__connectedXInput[i])
  899. {
  900. // Gamepad was just connected.
  901. Platform::gamepadEventConnectedInternal(i, XINPUT_BUTTON_COUNT, XINPUT_JOYSTICK_COUNT, XINPUT_TRIGGER_COUNT, 0, 0, "Microsoft", "XBox360 Controller");
  902. __connectedXInput[i] = true;
  903. }
  904. else if (XInputGetState(i, &__xInputState) != NO_ERROR && __connectedXInput[i])
  905. {
  906. // Gamepad was just disconnected.
  907. __connectedXInput[i] = false;
  908. Platform::gamepadEventDisconnectedInternal(i);
  909. }
  910. }
  911. #endif
  912. _game->frame();
  913. SwapBuffers(__hdc);
  914. }
  915. // If we are done, then exit.
  916. if (_game->getState() == Game::UNINITIALIZED)
  917. break;
  918. }
  919. return 0;
  920. }
  921. void Platform::signalShutdown()
  922. {
  923. // nothing to do
  924. }
  925. bool Platform::canExit()
  926. {
  927. return true;
  928. }
  929. unsigned int Platform::getDisplayWidth()
  930. {
  931. static RECT rect;
  932. GetClientRect(__hwnd, &rect);
  933. return rect.right;
  934. }
  935. unsigned int Platform::getDisplayHeight()
  936. {
  937. static RECT rect;
  938. GetClientRect(__hwnd, &rect);
  939. return rect.bottom;
  940. }
  941. double Platform::getAbsoluteTime()
  942. {
  943. LARGE_INTEGER queryTime;
  944. QueryPerformanceCounter(&queryTime);
  945. GP_ASSERT(__timeTicksPerMillis);
  946. __timeAbsolute = queryTime.QuadPart / __timeTicksPerMillis;
  947. return __timeAbsolute - __timeStart;
  948. }
  949. void Platform::setAbsoluteTime(double time)
  950. {
  951. __timeAbsolute = time;
  952. }
  953. bool Platform::isVsync()
  954. {
  955. return __vsync;
  956. }
  957. void Platform::setVsync(bool enable)
  958. {
  959. wglSwapIntervalEXT(enable ? 1 : 0);
  960. __vsync = enable;
  961. }
  962. void Platform::swapBuffers()
  963. {
  964. if (__hdc)
  965. SwapBuffers(__hdc);
  966. }
  967. void Platform::sleep(long ms)
  968. {
  969. Sleep(ms);
  970. }
  971. void Platform::setMultiSampling(bool enabled)
  972. {
  973. if (enabled == __multiSampling)
  974. {
  975. return;
  976. }
  977. if (enabled)
  978. {
  979. glEnable(GL_MULTISAMPLE);
  980. }
  981. else
  982. {
  983. glDisable(GL_MULTISAMPLE);
  984. }
  985. __multiSampling = enabled;
  986. }
  987. bool Platform::isMultiSampling()
  988. {
  989. return __multiSampling;
  990. }
  991. void Platform::setMultiTouch(bool enabled)
  992. {
  993. // not supported
  994. }
  995. bool Platform::isMultiTouch()
  996. {
  997. return false;
  998. }
  999. void Platform::getAccelerometerValues(float* pitch, float* roll)
  1000. {
  1001. GP_ASSERT(pitch);
  1002. GP_ASSERT(roll);
  1003. *pitch = __pitch;
  1004. *roll = __roll;
  1005. }
  1006. bool Platform::hasMouse()
  1007. {
  1008. return true;
  1009. }
  1010. void Platform::setMouseCaptured(bool captured)
  1011. {
  1012. if (captured != __mouseCaptured)
  1013. {
  1014. if (captured)
  1015. {
  1016. // Hide the cursor and warp it to the center of the screen
  1017. __mouseCapturePoint.x = getDisplayWidth() / 2;
  1018. __mouseCapturePoint.y = getDisplayHeight() / 2;
  1019. ShowCursor(FALSE);
  1020. WarpMouse(__mouseCapturePoint.x, __mouseCapturePoint.y);
  1021. }
  1022. else
  1023. {
  1024. // Restore cursor
  1025. WarpMouse(__mouseCapturePoint.x, __mouseCapturePoint.y);
  1026. ShowCursor(TRUE);
  1027. }
  1028. __mouseCaptured = captured;
  1029. }
  1030. }
  1031. bool Platform::isMouseCaptured()
  1032. {
  1033. return __mouseCaptured;
  1034. }
  1035. void Platform::setCursorVisible(bool visible)
  1036. {
  1037. if (visible != __cursorVisible)
  1038. {
  1039. ShowCursor(visible ? TRUE : FALSE);
  1040. __cursorVisible = visible;
  1041. }
  1042. }
  1043. bool Platform::isCursorVisible()
  1044. {
  1045. return __cursorVisible;
  1046. }
  1047. void Platform::displayKeyboard(bool display)
  1048. {
  1049. // Do nothing.
  1050. }
  1051. bool Platform::isGestureSupported(Gesture::GestureEvent evt)
  1052. {
  1053. return false;
  1054. }
  1055. void Platform::registerGesture(Gesture::GestureEvent evt)
  1056. {
  1057. }
  1058. void Platform::unregisterGesture(Gesture::GestureEvent evt)
  1059. {
  1060. }
  1061. bool Platform::isGestureRegistered(Gesture::GestureEvent evt)
  1062. {
  1063. return false;
  1064. }
  1065. #ifdef USE_XINPUT
  1066. void Platform::pollGamepadState(Gamepad* gamepad)
  1067. {
  1068. GP_ASSERT(gamepad->_handle < XUSER_MAX_COUNT);
  1069. if (XInputGetState(gamepad->_handle, &__xInputState) == NO_ERROR)
  1070. {
  1071. WORD buttons = __xInputState.Gamepad.wButtons;
  1072. // Map XInput buttons to Gamepad::ButtonMappings enum.
  1073. static const unsigned int xInputMapping[16] = {
  1074. Gamepad::BUTTON_UP, // 0x0001
  1075. Gamepad::BUTTON_DOWN, // 0x0002
  1076. Gamepad::BUTTON_LEFT, // 0x0004
  1077. Gamepad::BUTTON_RIGHT, // 0x0008
  1078. Gamepad::BUTTON_MENU2, // 0x0010
  1079. Gamepad::BUTTON_MENU1, // 0x0020
  1080. Gamepad::BUTTON_L3, // 0x0040
  1081. Gamepad::BUTTON_R3, // 0x0080
  1082. Gamepad::BUTTON_L1, // 0x0100
  1083. Gamepad::BUTTON_R1, // 0x0200
  1084. 0,
  1085. 0,
  1086. Gamepad::BUTTON_A, // 0x1000
  1087. Gamepad::BUTTON_B, // 0x2000
  1088. Gamepad::BUTTON_X, // 0x4000
  1089. Gamepad::BUTTON_Y // 0x8000
  1090. };
  1091. const unsigned int *mapping = xInputMapping;
  1092. for (gamepad->_buttons = 0; buttons; buttons >>= 1, mapping++)
  1093. {
  1094. if (buttons & 1)
  1095. {
  1096. gamepad->_buttons |= (1 << *mapping);
  1097. }
  1098. }
  1099. unsigned int i;
  1100. for (i = 0; i < gamepad->_joystickCount; ++i)
  1101. {
  1102. GP_ASSERT(i < 2);
  1103. float x;
  1104. float y;
  1105. switch (i)
  1106. {
  1107. case 0:
  1108. x = normalizeXInputJoystickAxis(__xInputState.Gamepad.sThumbLX, XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE);
  1109. y = normalizeXInputJoystickAxis(__xInputState.Gamepad.sThumbLY, XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE);
  1110. break;
  1111. case 1:
  1112. x = normalizeXInputJoystickAxis(__xInputState.Gamepad.sThumbRX, XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE);
  1113. y = normalizeXInputJoystickAxis(__xInputState.Gamepad.sThumbRY, XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE);
  1114. break;
  1115. }
  1116. gamepad->_joysticks[i].set(x, y);
  1117. }
  1118. for (i = 0; i < gamepad->_triggerCount; ++i)
  1119. {
  1120. GP_ASSERT(i < 2);
  1121. BYTE trigger;
  1122. switch (i)
  1123. {
  1124. case 0:
  1125. trigger = __xInputState.Gamepad.bLeftTrigger;
  1126. break;
  1127. case 1:
  1128. trigger = __xInputState.Gamepad.bRightTrigger;
  1129. break;
  1130. }
  1131. if (trigger < XINPUT_GAMEPAD_TRIGGER_THRESHOLD)
  1132. {
  1133. gamepad->_triggers[i] = 0.0f;
  1134. }
  1135. else
  1136. {
  1137. gamepad->_triggers[i] = (float)trigger / 255.0f;
  1138. }
  1139. }
  1140. }
  1141. }
  1142. #else
  1143. void Platform::pollGamepadState(Gamepad* gamepad)
  1144. {
  1145. // TODO: Support generic HID gamepads (including XBox controllers) without requiring XInput.
  1146. }
  1147. #endif
  1148. void Platform::shutdownInternal()
  1149. {
  1150. Game::getInstance()->shutdown();
  1151. }
  1152. bool Platform::launchURL(const char* url)
  1153. {
  1154. if (url == NULL || *url == '\0')
  1155. return false;
  1156. // Success when result code > 32
  1157. int len = MultiByteToWideChar(CP_ACP, 0, url, -1, NULL, 0);
  1158. wchar_t* wurl = new wchar_t[len];
  1159. MultiByteToWideChar(CP_ACP, 0, url, -1, wurl, len);
  1160. int r = (int)ShellExecute(NULL, NULL, wurl, NULL, NULL, SW_SHOWNORMAL);
  1161. SAFE_DELETE_ARRAY(wurl);
  1162. return (r > 32);
  1163. }
  1164. }
  1165. #endif