PlatformWindows.cpp 41 KB

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