PlatformWindows.cpp 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  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. }
  462. return DefWindowProc(hwnd, msg, wParam, lParam);
  463. }
  464. namespace gameplay
  465. {
  466. struct WindowCreationParams
  467. {
  468. RECT rect;
  469. std::wstring windowName;
  470. bool fullscreen;
  471. int samples;
  472. };
  473. extern void print(const char* format, ...)
  474. {
  475. va_list argptr;
  476. va_start(argptr, format);
  477. int sz = vfprintf(stderr, format, argptr);
  478. if (sz > 0)
  479. {
  480. char* buf = new char[sz + 1];
  481. vsprintf(buf, format, argptr);
  482. buf[sz] = 0;
  483. OutputDebugStringA(buf);
  484. SAFE_DELETE_ARRAY(buf);
  485. }
  486. va_end(argptr);
  487. }
  488. Platform::Platform(Game* game)
  489. : _game(game)
  490. {
  491. }
  492. Platform::~Platform()
  493. {
  494. if (__hwnd)
  495. {
  496. DestroyWindow(__hwnd);
  497. __hwnd = 0;
  498. }
  499. }
  500. bool createWindow(WindowCreationParams* params, HWND* hwnd, HDC* hdc)
  501. {
  502. bool fullscreen = false;
  503. RECT rect = { CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT };
  504. std::wstring windowName;
  505. if (params)
  506. {
  507. windowName = params->windowName;
  508. memcpy(&rect, &params->rect, sizeof(RECT));
  509. fullscreen = params->fullscreen;
  510. }
  511. // Set the window style.
  512. DWORD style = (fullscreen ? WS_POPUP : WS_POPUP | WS_BORDER | WS_CAPTION | WS_SYSMENU) | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
  513. DWORD styleEx = (fullscreen ? WS_EX_APPWINDOW : WS_EX_APPWINDOW | WS_EX_WINDOWEDGE);
  514. // Adjust the window rectangle so the client size is the requested size.
  515. AdjustWindowRectEx(&rect, style, FALSE, styleEx);
  516. // Create the native Windows window.
  517. *hwnd = CreateWindowEx(styleEx, L"gameplay", windowName.c_str(), style, 0, 0, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, __hinstance, NULL);
  518. if (*hwnd == NULL)
  519. {
  520. GP_ERROR("Failed to create window.");
  521. return false;
  522. }
  523. // Get the drawing context.
  524. *hdc = GetDC(*hwnd);
  525. if (*hdc == NULL)
  526. {
  527. GP_ERROR("Failed to get device context.");
  528. return false;
  529. }
  530. // Center the window
  531. GetWindowRect(*hwnd, &rect);
  532. const int screenX = (GetSystemMetrics(SM_CXSCREEN) - rect.right) / 2;
  533. const int screenY = (GetSystemMetrics(SM_CYSCREEN) - rect.bottom) / 2;
  534. SetWindowPos(*hwnd, *hwnd, screenX, screenY, -1, -1, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
  535. return true;
  536. }
  537. bool initializeGL(WindowCreationParams* params)
  538. {
  539. // Create a temporary window and context to we can initialize GLEW and get access
  540. // to additional OpenGL extension functions. This is a neccessary evil since the
  541. // function for querying GL extensions is a GL extension itself.
  542. HWND hwnd = NULL;
  543. HDC hdc = NULL;
  544. if (!createWindow(NULL, &hwnd, &hdc))
  545. return false;
  546. PIXELFORMATDESCRIPTOR pfd;
  547. memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
  548. pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
  549. pfd.nVersion = 1;
  550. pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
  551. pfd.iPixelType = PFD_TYPE_RGBA;
  552. pfd.cColorBits = DEFAULT_COLOR_BUFFER_SIZE;
  553. pfd.cDepthBits = DEFAULT_DEPTH_BUFFER_SIZE;
  554. pfd.cStencilBits = DEFAULT_STENCIL_BUFFER_SIZE;
  555. pfd.iLayerType = PFD_MAIN_PLANE;
  556. int pixelFormat = ChoosePixelFormat(hdc, &pfd);
  557. if (pixelFormat == 0)
  558. {
  559. DestroyWindow(hwnd);
  560. GP_ERROR("Failed to choose a pixel format.");
  561. return false;
  562. }
  563. if (!SetPixelFormat(hdc, pixelFormat, &pfd))
  564. {
  565. DestroyWindow(hwnd);
  566. GP_ERROR("Failed to set the pixel format.");
  567. return false;
  568. }
  569. HGLRC tempContext = wglCreateContext(hdc);
  570. if (!tempContext)
  571. {
  572. DestroyWindow(hwnd);
  573. GP_ERROR("Failed to create temporary context for initialization.");
  574. return false;
  575. }
  576. wglMakeCurrent(hdc, tempContext);
  577. // Initialize GLEW
  578. if (GLEW_OK != glewInit())
  579. {
  580. wglDeleteContext(tempContext);
  581. DestroyWindow(hwnd);
  582. GP_ERROR("Failed to initialize GLEW.");
  583. return false;
  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->samples,
  592. WGL_SAMPLE_BUFFERS_ARB, params->samples > 0 ? 1 : 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->samples > 0;
  603. UINT numFormats;
  604. if (!wglChoosePixelFormatARB(hdc, attribList, NULL, 1, &pixelFormat, &numFormats) || numFormats == 0)
  605. {
  606. bool valid = false;
  607. if (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. // Destroy old window
  633. DestroyWindow(hwnd);
  634. hwnd = NULL;
  635. hdc = NULL;
  636. // Create new/final window if needed
  637. if (params)
  638. {
  639. if (!createWindow(params, &__hwnd, &__hdc))
  640. {
  641. wglDeleteContext(tempContext);
  642. return false;
  643. }
  644. }
  645. // Set final pixel format for window
  646. if (!SetPixelFormat(__hdc, pixelFormat, &pfd))
  647. {
  648. GP_ERROR("Failed to set the pixel format: %d.", (int)GetLastError());
  649. return false;
  650. }
  651. // Create our new GL context
  652. int attribs[] =
  653. {
  654. WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
  655. WGL_CONTEXT_MINOR_VERSION_ARB, 1,
  656. 0
  657. };
  658. if (!(__hrc = wglCreateContextAttribsARB(__hdc, 0, attribs) ) )
  659. {
  660. wglDeleteContext(tempContext);
  661. GP_ERROR("Failed to create OpenGL context.");
  662. return false;
  663. }
  664. // Delete the old/temporary context and window
  665. wglDeleteContext(tempContext);
  666. // Make the new context current
  667. if (!wglMakeCurrent(__hdc, __hrc) || !__hrc)
  668. {
  669. GP_ERROR("Failed to make the window current.");
  670. return false;
  671. }
  672. // Vertical sync.
  673. wglSwapIntervalEXT(__vsync ? 1 : 0);
  674. return true;
  675. }
  676. Platform* Platform::create(Game* game, void* attachToWindow)
  677. {
  678. GP_ASSERT(game);
  679. FileSystem::setResourcePath("./");
  680. Platform* platform = new Platform(game);
  681. // Get the application module handle.
  682. __hinstance = ::GetModuleHandle(NULL);
  683. __attachToWindow = (HWND)attachToWindow;
  684. // Read window settings from config.
  685. WindowCreationParams params;
  686. params.fullscreen = false;
  687. params.rect.left = 0;
  688. params.rect.top = 0;
  689. params.rect.right = 0;
  690. params.rect.bottom = 0;
  691. params.samples = 0;
  692. if (game->getConfig())
  693. {
  694. Properties* config = game->getConfig()->getNamespace("window", true);
  695. if (config)
  696. {
  697. // Read window title.
  698. const char* title = config->getString("title");
  699. if (title)
  700. {
  701. int len = MultiByteToWideChar(CP_ACP, 0, title, -1, NULL, 0);
  702. wchar_t* wtitle = new wchar_t[len];
  703. MultiByteToWideChar(CP_ACP, 0, title, -1, wtitle, len);
  704. params.windowName = wtitle;
  705. SAFE_DELETE_ARRAY(wtitle);
  706. }
  707. // Read fullscreen state.
  708. params.fullscreen = config->getBool("fullscreen");
  709. // Read multisampling state.
  710. params.samples = config->getInt("samples");
  711. // Read window rect.
  712. int x = config->getInt("x");
  713. if (x != 0)
  714. params.rect.left = x;
  715. int y = config->getInt("y");
  716. if (y != 0)
  717. params.rect.top = y;
  718. int width = config->getInt("width");
  719. int height = config->getInt("height");
  720. if (width == 0 && height == 0 && params.fullscreen)
  721. getDesktopResolution(width, height);
  722. if (width != 0)
  723. params.rect.right = params.rect.left + width;
  724. if (height != 0)
  725. params.rect.bottom = params.rect.top + height;
  726. }
  727. }
  728. // If window size was not specified, set it to a default value
  729. if (params.rect.right == 0)
  730. params.rect.right = params.rect.left + DEFAULT_RESOLUTION_X;
  731. if (params.rect.bottom == 0)
  732. params.rect.bottom = params.rect.top + DEFAULT_RESOLUTION_Y;
  733. int width = params.rect.right - params.rect.left;
  734. int height = params.rect.bottom - params.rect.top;
  735. if (params.fullscreen)
  736. {
  737. // Enumerate all supposed display settings
  738. bool modeSupported = false;
  739. DWORD modeNum = 0;
  740. DEVMODE devMode;
  741. memset(&devMode, 0, sizeof(DEVMODE));
  742. devMode.dmSize = sizeof(DEVMODE);
  743. devMode.dmDriverExtra = 0;
  744. while (EnumDisplaySettings(NULL, modeNum++, &devMode) != 0)
  745. {
  746. // Is mode supported?
  747. if (devMode.dmPelsWidth == width &&
  748. devMode.dmPelsHeight == height &&
  749. devMode.dmBitsPerPel == DEFAULT_COLOR_BUFFER_SIZE)
  750. {
  751. modeSupported = true;
  752. break;
  753. }
  754. }
  755. // If the requested mode is not supported, fall back to a safe default
  756. if (!modeSupported)
  757. {
  758. width = DEFAULT_RESOLUTION_X;
  759. height = DEFAULT_RESOLUTION_Y;
  760. params.rect.right = params.rect.left + width;
  761. params.rect.bottom = params.rect.top + height;
  762. }
  763. }
  764. if (!__attachToWindow)
  765. {
  766. // Register our window class.
  767. WNDCLASSEX wc;
  768. wc.cbSize = sizeof(WNDCLASSEX);
  769. wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  770. wc.lpfnWndProc = (WNDPROC)__WndProc;
  771. wc.cbClsExtra = 0;
  772. wc.cbWndExtra = 0;
  773. wc.hInstance = __hinstance;
  774. wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  775. wc.hIconSm = NULL;
  776. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  777. wc.hbrBackground = NULL; // No brush - we are going to paint our own background
  778. wc.lpszMenuName = NULL; // No default menu
  779. wc.lpszClassName = L"gameplay";
  780. if (!::RegisterClassEx(&wc))
  781. {
  782. GP_ERROR("Failed to register window class.");
  783. goto error;
  784. }
  785. if (params.fullscreen)
  786. {
  787. DEVMODE dm;
  788. memset(&dm, 0, sizeof(dm));
  789. dm.dmSize= sizeof(dm);
  790. dm.dmPelsWidth = width;
  791. dm.dmPelsHeight = height;
  792. dm.dmBitsPerPel = DEFAULT_COLOR_BUFFER_SIZE;
  793. dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  794. // Try to set selected mode and get results. NOTE: CDS_FULLSCREEN gets rid of start bar.
  795. if (ChangeDisplaySettings(&dm, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
  796. {
  797. params.fullscreen = false;
  798. GP_ERROR("Failed to start game in full-screen mode with resolution %dx%d.", width, height);
  799. goto error;
  800. }
  801. }
  802. if (!initializeGL(&params))
  803. goto error;
  804. // Show the window.
  805. ShowWindow(__hwnd, SW_SHOW);
  806. }
  807. else
  808. {
  809. // Attach to a previous windows
  810. __hwnd = (HWND)__attachToWindow;
  811. __hdc = GetDC(__hwnd);
  812. SetWindowLongPtr(__hwnd, GWLP_WNDPROC, (LONG)(WNDPROC)__WndProc);
  813. if (!initializeGL(NULL))
  814. goto error;
  815. }
  816. #ifdef USE_XINPUT
  817. // Initialize XInputGamepads.
  818. for (DWORD i = 0; i < XUSER_MAX_COUNT; i++)
  819. {
  820. if (XInputGetState(i, &__xInputState) == NO_ERROR)
  821. {
  822. if (!__connectedXInput[i])
  823. {
  824. // Gamepad is connected.
  825. Platform::gamepadEventConnectedInternal(i, XINPUT_BUTTON_COUNT, XINPUT_JOYSTICK_COUNT, XINPUT_TRIGGER_COUNT, 0, 0, "Microsoft", "XBox360 Controller");
  826. __connectedXInput[i] = true;
  827. }
  828. }
  829. }
  830. #endif
  831. return platform;
  832. error:
  833. exit(0);
  834. return NULL;
  835. }
  836. int Platform::enterMessagePump()
  837. {
  838. GP_ASSERT(_game);
  839. // Get the initial time.
  840. LARGE_INTEGER tps;
  841. QueryPerformanceFrequency(&tps);
  842. __timeTicksPerMillis = (double)(tps.QuadPart / 1000L);
  843. LARGE_INTEGER queryTime;
  844. QueryPerformanceCounter(&queryTime);
  845. GP_ASSERT(__timeTicksPerMillis);
  846. __timeStart = queryTime.QuadPart / __timeTicksPerMillis;
  847. // Set the initial pitch and roll values.
  848. __pitch = 0.0;
  849. __roll = 0.0;
  850. SwapBuffers(__hdc);
  851. if (_game->getState() != Game::RUNNING)
  852. _game->run();
  853. if (__attachToWindow)
  854. return 0;
  855. // Enter event dispatch loop.
  856. MSG msg;
  857. while (true)
  858. {
  859. if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  860. {
  861. TranslateMessage(&msg);
  862. DispatchMessage(&msg);
  863. if (msg.message == WM_QUIT)
  864. {
  865. gameplay::Platform::shutdownInternal();
  866. return msg.wParam;
  867. }
  868. }
  869. else
  870. {
  871. #ifdef USE_XINPUT
  872. // Check for connected XInput gamepads.
  873. for (DWORD i = 0; i < XUSER_MAX_COUNT; i++)
  874. {
  875. if (XInputGetState(i, &__xInputState) == NO_ERROR && !__connectedXInput[i])
  876. {
  877. // Gamepad was just connected.
  878. Platform::gamepadEventConnectedInternal(i, XINPUT_BUTTON_COUNT, XINPUT_JOYSTICK_COUNT, XINPUT_TRIGGER_COUNT, 0, 0, "Microsoft", "XBox360 Controller");
  879. __connectedXInput[i] = true;
  880. }
  881. else if (XInputGetState(i, &__xInputState) != NO_ERROR && __connectedXInput[i])
  882. {
  883. // Gamepad was just disconnected.
  884. __connectedXInput[i] = false;
  885. Platform::gamepadEventDisconnectedInternal(i);
  886. }
  887. }
  888. #endif
  889. _game->frame();
  890. SwapBuffers(__hdc);
  891. }
  892. // If we are done, then exit.
  893. if (_game->getState() == Game::UNINITIALIZED)
  894. break;
  895. }
  896. return 0;
  897. }
  898. void Platform::signalShutdown()
  899. {
  900. // nothing to do
  901. }
  902. bool Platform::canExit()
  903. {
  904. return true;
  905. }
  906. unsigned int Platform::getDisplayWidth()
  907. {
  908. static RECT rect;
  909. GetClientRect(__hwnd, &rect);
  910. return rect.right;
  911. }
  912. unsigned int Platform::getDisplayHeight()
  913. {
  914. static RECT rect;
  915. GetClientRect(__hwnd, &rect);
  916. return rect.bottom;
  917. }
  918. double Platform::getAbsoluteTime()
  919. {
  920. LARGE_INTEGER queryTime;
  921. QueryPerformanceCounter(&queryTime);
  922. GP_ASSERT(__timeTicksPerMillis);
  923. __timeAbsolute = queryTime.QuadPart / __timeTicksPerMillis;
  924. return __timeAbsolute - __timeStart;
  925. }
  926. void Platform::setAbsoluteTime(double time)
  927. {
  928. __timeAbsolute = time;
  929. }
  930. bool Platform::isVsync()
  931. {
  932. return __vsync;
  933. }
  934. void Platform::setVsync(bool enable)
  935. {
  936. wglSwapIntervalEXT(enable ? 1 : 0);
  937. __vsync = enable;
  938. }
  939. void Platform::swapBuffers()
  940. {
  941. if (__hdc)
  942. SwapBuffers(__hdc);
  943. }
  944. void Platform::sleep(long ms)
  945. {
  946. Sleep(ms);
  947. }
  948. void Platform::setMultiSampling(bool enabled)
  949. {
  950. if (enabled == __multiSampling)
  951. {
  952. return;
  953. }
  954. if (enabled)
  955. {
  956. glEnable(GL_MULTISAMPLE);
  957. }
  958. else
  959. {
  960. glDisable(GL_MULTISAMPLE);
  961. }
  962. __multiSampling = enabled;
  963. }
  964. bool Platform::isMultiSampling()
  965. {
  966. return __multiSampling;
  967. }
  968. void Platform::setMultiTouch(bool enabled)
  969. {
  970. // not supported
  971. }
  972. bool Platform::isMultiTouch()
  973. {
  974. return false;
  975. }
  976. void Platform::getAccelerometerValues(float* pitch, float* roll)
  977. {
  978. GP_ASSERT(pitch);
  979. GP_ASSERT(roll);
  980. *pitch = __pitch;
  981. *roll = __roll;
  982. }
  983. bool Platform::hasMouse()
  984. {
  985. return true;
  986. }
  987. void Platform::setMouseCaptured(bool captured)
  988. {
  989. if (captured != __mouseCaptured)
  990. {
  991. if (captured)
  992. {
  993. // Hide the cursor and warp it to the center of the screen
  994. __mouseCapturePoint.x = getDisplayWidth() / 2;
  995. __mouseCapturePoint.y = getDisplayHeight() / 2;
  996. ShowCursor(FALSE);
  997. WarpMouse(__mouseCapturePoint.x, __mouseCapturePoint.y);
  998. }
  999. else
  1000. {
  1001. // Restore cursor
  1002. WarpMouse(__mouseCapturePoint.x, __mouseCapturePoint.y);
  1003. ShowCursor(TRUE);
  1004. }
  1005. __mouseCaptured = captured;
  1006. }
  1007. }
  1008. bool Platform::isMouseCaptured()
  1009. {
  1010. return __mouseCaptured;
  1011. }
  1012. void Platform::setCursorVisible(bool visible)
  1013. {
  1014. if (visible != __cursorVisible)
  1015. {
  1016. ShowCursor(visible ? TRUE : FALSE);
  1017. __cursorVisible = visible;
  1018. }
  1019. }
  1020. bool Platform::isCursorVisible()
  1021. {
  1022. return __cursorVisible;
  1023. }
  1024. void Platform::displayKeyboard(bool display)
  1025. {
  1026. // Do nothing.
  1027. }
  1028. bool Platform::isGestureSupported(Gesture::GestureEvent evt)
  1029. {
  1030. return false;
  1031. }
  1032. void Platform::registerGesture(Gesture::GestureEvent evt)
  1033. {
  1034. }
  1035. void Platform::unregisterGesture(Gesture::GestureEvent evt)
  1036. {
  1037. }
  1038. bool Platform::isGestureRegistered(Gesture::GestureEvent evt)
  1039. {
  1040. return false;
  1041. }
  1042. #ifdef USE_XINPUT
  1043. void Platform::pollGamepadState(Gamepad* gamepad)
  1044. {
  1045. GP_ASSERT(gamepad->_handle < XUSER_MAX_COUNT);
  1046. if (XInputGetState(gamepad->_handle, &__xInputState) == NO_ERROR)
  1047. {
  1048. WORD buttons = __xInputState.Gamepad.wButtons;
  1049. // Map XInput buttons to Gamepad::ButtonMappings enum.
  1050. static const unsigned int xInputMapping[16] = {
  1051. Gamepad::BUTTON_UP, // 0x0001
  1052. Gamepad::BUTTON_DOWN, // 0x0002
  1053. Gamepad::BUTTON_LEFT, // 0x0004
  1054. Gamepad::BUTTON_RIGHT, // 0x0008
  1055. Gamepad::BUTTON_MENU2, // 0x0010
  1056. Gamepad::BUTTON_MENU1, // 0x0020
  1057. Gamepad::BUTTON_L3, // 0x0040
  1058. Gamepad::BUTTON_R3, // 0x0080
  1059. Gamepad::BUTTON_L1, // 0x0100
  1060. Gamepad::BUTTON_R1, // 0x0200
  1061. 0,
  1062. 0,
  1063. Gamepad::BUTTON_A, // 0x1000
  1064. Gamepad::BUTTON_B, // 0x2000
  1065. Gamepad::BUTTON_X, // 0x4000
  1066. Gamepad::BUTTON_Y // 0x8000
  1067. };
  1068. const unsigned int *mapping = xInputMapping;
  1069. for (gamepad->_buttons = 0; buttons; buttons >>= 1, mapping++)
  1070. {
  1071. if (buttons & 1)
  1072. {
  1073. gamepad->_buttons |= (1 << *mapping);
  1074. }
  1075. }
  1076. unsigned int i;
  1077. for (i = 0; i < gamepad->_joystickCount; ++i)
  1078. {
  1079. GP_ASSERT(i < 2);
  1080. float x;
  1081. float y;
  1082. switch (i)
  1083. {
  1084. case 0:
  1085. x = normalizeXInputJoystickAxis(__xInputState.Gamepad.sThumbLX, XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE);
  1086. y = normalizeXInputJoystickAxis(__xInputState.Gamepad.sThumbLY, XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE);
  1087. break;
  1088. case 1:
  1089. x = normalizeXInputJoystickAxis(__xInputState.Gamepad.sThumbRX, XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE);
  1090. y = normalizeXInputJoystickAxis(__xInputState.Gamepad.sThumbRY, XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE);
  1091. break;
  1092. }
  1093. gamepad->_joysticks[i].set(x, y);
  1094. }
  1095. for (i = 0; i < gamepad->_triggerCount; ++i)
  1096. {
  1097. GP_ASSERT(i < 2);
  1098. BYTE trigger;
  1099. switch (i)
  1100. {
  1101. case 0:
  1102. trigger = __xInputState.Gamepad.bLeftTrigger;
  1103. break;
  1104. case 1:
  1105. trigger = __xInputState.Gamepad.bRightTrigger;
  1106. break;
  1107. }
  1108. if (trigger < XINPUT_GAMEPAD_TRIGGER_THRESHOLD)
  1109. {
  1110. gamepad->_triggers[i] = 0.0f;
  1111. }
  1112. else
  1113. {
  1114. gamepad->_triggers[i] = (float)trigger / 255.0f;
  1115. }
  1116. }
  1117. }
  1118. }
  1119. #else
  1120. void Platform::pollGamepadState(Gamepad* gamepad)
  1121. {
  1122. // TODO: Support generic HID gamepads (including XBox controllers) without requiring XInput.
  1123. }
  1124. #endif
  1125. void Platform::touchEventInternal(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  1126. {
  1127. if (!Form::touchEventInternal(evt, x, y, contactIndex))
  1128. {
  1129. Game::getInstance()->touchEvent(evt, x, y, contactIndex);
  1130. Game::getInstance()->getScriptController()->touchEvent(evt, x, y, contactIndex);
  1131. }
  1132. }
  1133. void Platform::keyEventInternal(Keyboard::KeyEvent evt, int key)
  1134. {
  1135. if (!Form::keyEventInternal(evt, key))
  1136. {
  1137. Game::getInstance()->keyEvent(evt, key);
  1138. Game::getInstance()->getScriptController()->keyEvent(evt, key);
  1139. }
  1140. }
  1141. bool Platform::mouseEventInternal(Mouse::MouseEvent evt, int x, int y, int wheelDelta)
  1142. {
  1143. if (Form::mouseEventInternal(evt, x, y, wheelDelta))
  1144. {
  1145. return true;
  1146. }
  1147. else if (Game::getInstance()->mouseEvent(evt, x, y, wheelDelta))
  1148. {
  1149. return true;
  1150. }
  1151. else
  1152. {
  1153. return Game::getInstance()->getScriptController()->mouseEvent(evt, x, y, wheelDelta);
  1154. }
  1155. }
  1156. void Platform::gamepadEventConnectedInternal(GamepadHandle handle, unsigned int buttonCount, unsigned int joystickCount, unsigned int triggerCount,
  1157. unsigned int vendorId, unsigned int productId, const char* vendorString, const char* productString)
  1158. {
  1159. Gamepad::add(handle, buttonCount, joystickCount, triggerCount, vendorId, productId, vendorString, productString);
  1160. }
  1161. void Platform::gamepadEventDisconnectedInternal(GamepadHandle handle)
  1162. {
  1163. Gamepad::remove(handle);
  1164. }
  1165. void Platform::shutdownInternal()
  1166. {
  1167. Game::getInstance()->shutdown();
  1168. }
  1169. bool Platform::launchURL(const char* url)
  1170. {
  1171. if (url == NULL || *url == '\0')
  1172. return false;
  1173. // Success when result code > 32
  1174. int len = MultiByteToWideChar(CP_ACP, 0, url, -1, NULL, 0);
  1175. wchar_t* wurl = new wchar_t[len];
  1176. MultiByteToWideChar(CP_ACP, 0, url, -1, wurl, len);
  1177. int r = (int)ShellExecute(NULL, NULL, wurl, NULL, NULL, SW_SHOWNORMAL);
  1178. SAFE_DELETE_ARRAY(wurl);
  1179. return (r > 32);
  1180. }
  1181. }
  1182. #endif