PlatformWin32.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  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::setMultiTouch(bool enabled)
  833. {
  834. // not supported
  835. }
  836. bool Platform::isMultiTouch()
  837. {
  838. return false;
  839. }
  840. void Platform::getAccelerometerValues(float* pitch, float* roll)
  841. {
  842. GP_ASSERT(pitch);
  843. GP_ASSERT(roll);
  844. *pitch = __pitch;
  845. *roll = __roll;
  846. }
  847. bool Platform::hasMouse()
  848. {
  849. return true;
  850. }
  851. void Platform::setMouseCaptured(bool captured)
  852. {
  853. if (captured != __mouseCaptured)
  854. {
  855. if (captured)
  856. {
  857. // Hide the cursor and warp it to the center of the screen
  858. __mouseCapturePoint.x = getDisplayWidth() / 2;
  859. __mouseCapturePoint.y = getDisplayHeight() / 2;
  860. ShowCursor(FALSE);
  861. WarpMouse(__mouseCapturePoint.x, __mouseCapturePoint.y);
  862. }
  863. else
  864. {
  865. // Restore cursor
  866. WarpMouse(__mouseCapturePoint.x, __mouseCapturePoint.y);
  867. ShowCursor(TRUE);
  868. }
  869. __mouseCaptured = captured;
  870. }
  871. }
  872. bool Platform::isMouseCaptured()
  873. {
  874. return __mouseCaptured;
  875. }
  876. void Platform::setCursorVisible(bool visible)
  877. {
  878. if (visible != __cursorVisible)
  879. {
  880. ShowCursor(visible ? TRUE : FALSE);
  881. __cursorVisible = visible;
  882. }
  883. }
  884. bool Platform::isCursorVisible()
  885. {
  886. return __cursorVisible;
  887. }
  888. void Platform::swapBuffers()
  889. {
  890. if (__hdc)
  891. SwapBuffers(__hdc);
  892. }
  893. void Platform::displayKeyboard(bool display)
  894. {
  895. // Do nothing.
  896. }
  897. unsigned int Platform::getGamepadsConnected()
  898. {
  899. // Check to see what xbox360 gamepads are connected.
  900. __gamepadsConnected = 0;
  901. #ifdef USE_XINPUT
  902. for (unsigned int i = 0; i < XUSER_MAX_COUNT; i++)
  903. {
  904. if (isGamepadConnected(i))
  905. __gamepadsConnected++;
  906. }
  907. #endif
  908. return __gamepadsConnected;
  909. }
  910. bool Platform::isGamepadConnected(unsigned int gamepadHandle)
  911. {
  912. #ifdef USE_XINPUT
  913. GP_ASSERT(0 <= gamepadHandle);
  914. GP_ASSERT(gamepadHandle < XUSER_MAX_COUNT);
  915. Game* game = Game::getInstance();
  916. GP_ASSERT(game);
  917. if (__xinputGamepads[gamepadHandle].handle == -1)
  918. __xinputGamepads[gamepadHandle].handle = game->createGamepad(__xinputGamepads[gamepadHandle].id.c_str(), gamepadHandle, __xinputGamepads[gamepadHandle].BUTTON_COUNT,
  919. __xinputGamepads[gamepadHandle].JOYSTICK_COUNT, __xinputGamepads[gamepadHandle].TRIGGER_COUNT);
  920. bool isConnected = __xinputGamepads[gamepadHandle].connected;
  921. if (getXInputState(gamepadHandle))
  922. {
  923. if (!isConnected)
  924. {
  925. __xinputGamepads[gamepadHandle].connected = true;
  926. Gamepad* gamepad = game->getGamepad(__xinputGamepads[gamepadHandle].handle);
  927. GP_ASSERT(gamepad);
  928. if (game->isInitialized())
  929. game->gamepadEvent(Gamepad::CONNECTED_EVENT, game->getGamepad(__xinputGamepads[gamepadHandle].handle));
  930. }
  931. return true;
  932. }
  933. else
  934. {
  935. if (isConnected)
  936. {
  937. // if it was connected, but now isn't pass the detached event to gamepadEvent()
  938. __xinputGamepads[gamepadHandle].connected = false;
  939. Gamepad* gamepad = game->getGamepad(__xinputGamepads[gamepadHandle].handle);
  940. GP_ASSERT(gamepad);
  941. if (game->isInitialized())
  942. game->gamepadEvent(Gamepad::DISCONNECTED_EVENT, game->getGamepad(__xinputGamepads[gamepadHandle].handle));
  943. }
  944. return false;
  945. }
  946. #endif
  947. return false;
  948. }
  949. const char* Platform::getGamepadId(unsigned int gamepadHandle)
  950. {
  951. #ifdef USE_XINPUT
  952. GP_ASSERT(0 <= gamepadHandle);
  953. GP_ASSERT(gamepadHandle < XUSER_MAX_COUNT);
  954. return __xinputGamepads[gamepadHandle].id.c_str();
  955. #endif
  956. return NULL;
  957. }
  958. unsigned int Platform::getGamepadButtonCount(unsigned int gamepadHandle)
  959. {
  960. #ifdef USE_XINPUT
  961. GP_ASSERT(0 <= gamepadHandle);
  962. GP_ASSERT(gamepadHandle < XUSER_MAX_COUNT);
  963. if (!__xinputGamepads[gamepadHandle].connected)
  964. return 0;
  965. return XInputGamepad::BUTTON_COUNT;
  966. #endif
  967. return 0;
  968. }
  969. bool Platform::getGamepadButtonState(unsigned int gamepadHandle, unsigned int buttonIndex)
  970. {
  971. #ifdef USE_XINPUT
  972. GP_ASSERT(0 <= gamepadHandle);
  973. GP_ASSERT(gamepadHandle < XUSER_MAX_COUNT);
  974. return getXInputButtonState(gamepadHandle, buttonIndex);
  975. #endif
  976. return false;
  977. }
  978. unsigned int Platform::getGamepadJoystickCount(unsigned int gamepadHandle)
  979. {
  980. #ifdef USE_XINPUT
  981. GP_ASSERT(0 <= gamepadHandle);
  982. GP_ASSERT(gamepadHandle < XUSER_MAX_COUNT);
  983. if (!__xinputGamepads[gamepadHandle].connected)
  984. return 0;
  985. return XInputGamepad::JOYSTICK_COUNT;
  986. #endif
  987. return 0;
  988. }
  989. float Platform::getGamepadJoystickAxisX(unsigned int gamepadHandle, unsigned int joystickIndex)
  990. {
  991. #ifdef USE_XINPUT
  992. GP_ASSERT(0 <= gamepadHandle);
  993. GP_ASSERT(gamepadHandle < XUSER_MAX_COUNT);
  994. return getXInputJoystickAxisX(gamepadHandle, joystickIndex);
  995. #endif
  996. return 0.0f;
  997. }
  998. float Platform::getGamepadJoystickAxisY(unsigned int gamepadHandle, unsigned int joystickIndex)
  999. {
  1000. #ifdef USE_XINPUT
  1001. GP_ASSERT(0 <= gamepadHandle);
  1002. GP_ASSERT(gamepadHandle < XUSER_MAX_COUNT);
  1003. return getXInputJoystickAxisY(gamepadHandle, joystickIndex);
  1004. #endif
  1005. return 0.0f;
  1006. }
  1007. void Platform::getGamepadJoystickAxisValues(unsigned int gamepadHandle, unsigned int joystickIndex, Vector2* outValue)
  1008. {
  1009. #ifdef USE_XINPUT
  1010. GP_ASSERT(0 <= gamepadHandle);
  1011. GP_ASSERT(gamepadHandle < XUSER_MAX_COUNT);
  1012. getXInputJoystickAxisValues(gamepadHandle, joystickIndex, outValue);
  1013. #endif
  1014. }
  1015. bool Platform::isGamepadJoystickActive(unsigned int gamepadHandle, unsigned int joystickIndex)
  1016. {
  1017. #ifdef USE_XINPUT
  1018. GP_ASSERT(0 <= gamepadHandle);
  1019. GP_ASSERT(gamepadHandle < XUSER_MAX_COUNT);
  1020. return (getXInputJoystickAxisX(gamepadHandle, joystickIndex) != 0.0f || getXInputJoystickAxisY(gamepadHandle, joystickIndex) != 0.0f);
  1021. #endif
  1022. return false;
  1023. }
  1024. unsigned int Platform::getGamepadTriggerCount(unsigned int gamepadHandle)
  1025. {
  1026. #ifdef USE_XINPUT
  1027. GP_ASSERT(0 <= gamepadHandle);
  1028. GP_ASSERT(gamepadHandle < XUSER_MAX_COUNT);
  1029. return XInputGamepad::TRIGGER_COUNT;
  1030. #endif
  1031. return 0;
  1032. }
  1033. float Platform::getGamepadTriggerValue(unsigned int gamepadHandle, unsigned int triggerIndex)
  1034. {
  1035. #ifdef USE_XINPUT
  1036. GP_ASSERT(0 <= gamepadHandle);
  1037. GP_ASSERT(gamepadHandle < XUSER_MAX_COUNT);
  1038. //TODO:
  1039. #endif
  1040. return 0.0f;
  1041. }
  1042. void Platform::touchEventInternal(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  1043. {
  1044. if (!Form::touchEventInternal(evt, x, y, contactIndex))
  1045. {
  1046. Game::getInstance()->touchEvent(evt, x, y, contactIndex);
  1047. Game::getInstance()->getScriptController()->touchEvent(evt, x, y, contactIndex);
  1048. }
  1049. }
  1050. void Platform::keyEventInternal(Keyboard::KeyEvent evt, int key)
  1051. {
  1052. if (!Form::keyEventInternal(evt, key))
  1053. {
  1054. Game::getInstance()->keyEvent(evt, key);
  1055. Game::getInstance()->getScriptController()->keyEvent(evt, key);
  1056. }
  1057. }
  1058. bool Platform::mouseEventInternal(Mouse::MouseEvent evt, int x, int y, int wheelDelta)
  1059. {
  1060. if (Form::mouseEventInternal(evt, x, y, wheelDelta))
  1061. {
  1062. return true;
  1063. }
  1064. else if (Game::getInstance()->mouseEvent(evt, x, y, wheelDelta))
  1065. {
  1066. return true;
  1067. }
  1068. else
  1069. {
  1070. return Game::getInstance()->getScriptController()->mouseEvent(evt, x, y, wheelDelta);
  1071. }
  1072. }
  1073. void Platform::sleep(long ms)
  1074. {
  1075. Sleep(ms);
  1076. }
  1077. }
  1078. #endif