PlatformWindows.cpp 40 KB

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