PlatformWindows.cpp 41 KB

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