PlatformWindows.cpp 40 KB

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