PlatformWin32.cpp 38 KB

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