PlatformWindows.cpp 40 KB

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