PlatformWindows.cpp 41 KB

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