2
0

PlatformWindows.cpp 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439
  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. if (wglSwapIntervalEXT)
  689. wglSwapIntervalEXT(__vsync ? 1 : 0);
  690. else
  691. __vsync = false;
  692. // Some old graphics cards support EXT_framebuffer_object instead of ARB_framebuffer_object.
  693. // Patch ARB_framebuffer_object functions to EXT_framebuffer_object ones since semantic is same.
  694. if( !GLEW_ARB_framebuffer_object && GLEW_EXT_framebuffer_object )
  695. {
  696. glBindFramebuffer = glBindFramebufferEXT;
  697. glBindRenderbuffer = glBindRenderbufferEXT;
  698. glBlitFramebuffer = glBlitFramebufferEXT;
  699. glCheckFramebufferStatus = glCheckFramebufferStatusEXT;
  700. glDeleteFramebuffers = glDeleteFramebuffersEXT;
  701. glDeleteRenderbuffers = glDeleteRenderbuffersEXT;
  702. glFramebufferRenderbuffer = glFramebufferRenderbufferEXT;
  703. glFramebufferTexture1D = glFramebufferTexture1DEXT;
  704. glFramebufferTexture2D = glFramebufferTexture2DEXT;
  705. glFramebufferTexture3D = glFramebufferTexture3DEXT;
  706. glFramebufferTextureLayer = glFramebufferTextureLayerEXT;
  707. glGenFramebuffers = glGenFramebuffersEXT;
  708. glGenRenderbuffers = glGenRenderbuffersEXT;
  709. glGenerateMipmap = glGenerateMipmapEXT;
  710. glGetFramebufferAttachmentParameteriv = glGetFramebufferAttachmentParameterivEXT;
  711. glGetRenderbufferParameteriv = glGetRenderbufferParameterivEXT;
  712. glIsFramebuffer = glIsFramebufferEXT;
  713. glIsRenderbuffer = glIsRenderbufferEXT;
  714. glRenderbufferStorage = glRenderbufferStorageEXT;
  715. glRenderbufferStorageMultisample = glRenderbufferStorageMultisampleEXT;
  716. }
  717. return true;
  718. }
  719. Platform* Platform::create(Game* game)
  720. {
  721. GP_ASSERT(game);
  722. FileSystem::setResourcePath("./");
  723. Platform* platform = new Platform(game);
  724. // Get the application module handle.
  725. __hinstance = ::GetModuleHandle(NULL);
  726. // Read window settings from config.
  727. WindowCreationParams params;
  728. params.fullscreen = false;
  729. params.resizable = false;
  730. params.rect.left = 0;
  731. params.rect.top = 0;
  732. params.rect.right = 0;
  733. params.rect.bottom = 0;
  734. params.samples = 0;
  735. if (game->getConfig())
  736. {
  737. Properties* config = game->getConfig()->getNamespace("window", true);
  738. if (config)
  739. {
  740. // Read window title.
  741. const char* title = config->getString("title");
  742. if (title)
  743. {
  744. int len = MultiByteToWideChar(CP_ACP, 0, title, -1, NULL, 0);
  745. wchar_t* wtitle = new wchar_t[len];
  746. MultiByteToWideChar(CP_ACP, 0, title, -1, wtitle, len);
  747. params.windowName = wtitle;
  748. SAFE_DELETE_ARRAY(wtitle);
  749. }
  750. // Read fullscreen state.
  751. params.fullscreen = config->getBool("fullscreen");
  752. // Read resizable state.
  753. params.resizable = config->getBool("resizable");
  754. // Read multisampling state.
  755. params.samples = config->getInt("samples");
  756. // Read window rect.
  757. int x = config->getInt("x");
  758. if (x != 0)
  759. params.rect.left = x;
  760. int y = config->getInt("y");
  761. if (y != 0)
  762. params.rect.top = y;
  763. int width = config->getInt("width");
  764. int height = config->getInt("height");
  765. if (width == 0 && height == 0 && params.fullscreen)
  766. getDesktopResolution(width, height);
  767. if (width != 0)
  768. params.rect.right = params.rect.left + width;
  769. if (height != 0)
  770. params.rect.bottom = params.rect.top + height;
  771. }
  772. }
  773. // If window size was not specified, set it to a default value
  774. if (params.rect.right == 0)
  775. params.rect.right = params.rect.left + DEFAULT_RESOLUTION_X;
  776. if (params.rect.bottom == 0)
  777. params.rect.bottom = params.rect.top + DEFAULT_RESOLUTION_Y;
  778. int width = params.rect.right - params.rect.left;
  779. int height = params.rect.bottom - params.rect.top;
  780. if (params.fullscreen)
  781. {
  782. // Enumerate all supposed display settings
  783. bool modeSupported = false;
  784. DWORD modeNum = 0;
  785. DEVMODE devMode;
  786. memset(&devMode, 0, sizeof(DEVMODE));
  787. devMode.dmSize = sizeof(DEVMODE);
  788. devMode.dmDriverExtra = 0;
  789. while (EnumDisplaySettings(NULL, modeNum++, &devMode) != 0)
  790. {
  791. // Is mode supported?
  792. if (devMode.dmPelsWidth == width &&
  793. devMode.dmPelsHeight == height &&
  794. devMode.dmBitsPerPel == DEFAULT_COLOR_BUFFER_SIZE)
  795. {
  796. modeSupported = true;
  797. break;
  798. }
  799. }
  800. // If the requested mode is not supported, fall back to a safe default
  801. if (!modeSupported)
  802. {
  803. width = DEFAULT_RESOLUTION_X;
  804. height = DEFAULT_RESOLUTION_Y;
  805. params.rect.right = params.rect.left + width;
  806. params.rect.bottom = params.rect.top + height;
  807. }
  808. }
  809. // Register our window class.
  810. WNDCLASSEX wc;
  811. wc.cbSize = sizeof(WNDCLASSEX);
  812. wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  813. wc.lpfnWndProc = (WNDPROC)__WndProc;
  814. wc.cbClsExtra = 0;
  815. wc.cbWndExtra = 0;
  816. wc.hInstance = __hinstance;
  817. wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  818. wc.hIconSm = NULL;
  819. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  820. wc.hbrBackground = NULL; // No brush - we are going to paint our own background
  821. wc.lpszMenuName = NULL; // No default menu
  822. wc.lpszClassName = L"gameplay";
  823. if (!::RegisterClassEx(&wc))
  824. {
  825. GP_ERROR("Failed to register window class.");
  826. goto error;
  827. }
  828. if (params.fullscreen)
  829. {
  830. DEVMODE dm;
  831. memset(&dm, 0, sizeof(dm));
  832. dm.dmSize= sizeof(dm);
  833. dm.dmPelsWidth = width;
  834. dm.dmPelsHeight = height;
  835. dm.dmBitsPerPel = DEFAULT_COLOR_BUFFER_SIZE;
  836. dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  837. // Try to set selected mode and get results. NOTE: CDS_FULLSCREEN gets rid of start bar.
  838. if (ChangeDisplaySettings(&dm, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
  839. {
  840. params.fullscreen = false;
  841. GP_ERROR("Failed to start game in full-screen mode with resolution %dx%d.", width, height);
  842. goto error;
  843. }
  844. }
  845. if (!initializeGL(&params))
  846. goto error;
  847. // Show the window.
  848. ShowWindow(__hwnd, SW_SHOW);
  849. #ifdef GP_USE_GAMEPAD
  850. // Initialize XInputGamepads.
  851. for (DWORD i = 0; i < XUSER_MAX_COUNT; i++)
  852. {
  853. if (XInputGetState(i, &__xInputState) == NO_ERROR)
  854. {
  855. if (!__connectedXInput[i])
  856. {
  857. // Gamepad is connected.
  858. Platform::gamepadEventConnectedInternal(i, XINPUT_BUTTON_COUNT, XINPUT_JOYSTICK_COUNT, XINPUT_TRIGGER_COUNT, "Microsoft XBox360 Controller");
  859. __connectedXInput[i] = true;
  860. }
  861. }
  862. }
  863. #endif
  864. return platform;
  865. error:
  866. exit(0);
  867. return NULL;
  868. }
  869. int Platform::enterMessagePump()
  870. {
  871. GP_ASSERT(_game);
  872. // Get the initial time.
  873. LARGE_INTEGER tps;
  874. QueryPerformanceFrequency(&tps);
  875. __timeTicksPerMillis = (double)(tps.QuadPart / 1000L);
  876. LARGE_INTEGER queryTime;
  877. QueryPerformanceCounter(&queryTime);
  878. GP_ASSERT(__timeTicksPerMillis);
  879. __timeStart = queryTime.QuadPart / __timeTicksPerMillis;
  880. SwapBuffers(__hdc);
  881. if (_game->getState() != Game::RUNNING)
  882. _game->run();
  883. // Enter event dispatch loop.
  884. MSG msg;
  885. while (true)
  886. {
  887. if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  888. {
  889. TranslateMessage(&msg);
  890. DispatchMessage(&msg);
  891. if (msg.message == WM_QUIT)
  892. {
  893. gameplay::Platform::shutdownInternal();
  894. return msg.wParam;
  895. }
  896. }
  897. else
  898. {
  899. #ifdef GP_USE_GAMEPAD
  900. // Check for connected XInput gamepads.
  901. for (DWORD i = 0; i < XUSER_MAX_COUNT; i++)
  902. {
  903. if (XInputGetState(i, &__xInputState) == NO_ERROR && !__connectedXInput[i])
  904. {
  905. // Gamepad was just connected.
  906. Platform::gamepadEventConnectedInternal(i, XINPUT_BUTTON_COUNT, XINPUT_JOYSTICK_COUNT, XINPUT_TRIGGER_COUNT, "Microsoft XBox360 Controller");
  907. __connectedXInput[i] = true;
  908. }
  909. else if (XInputGetState(i, &__xInputState) != NO_ERROR && __connectedXInput[i])
  910. {
  911. // Gamepad was just disconnected.
  912. __connectedXInput[i] = false;
  913. Platform::gamepadEventDisconnectedInternal(i);
  914. }
  915. }
  916. #endif
  917. _game->frame();
  918. SwapBuffers(__hdc);
  919. }
  920. // If we are done, then exit.
  921. if (_game->getState() == Game::UNINITIALIZED)
  922. break;
  923. }
  924. return 0;
  925. }
  926. void Platform::signalShutdown()
  927. {
  928. // nothing to do
  929. }
  930. bool Platform::canExit()
  931. {
  932. return true;
  933. }
  934. unsigned int Platform::getDisplayWidth()
  935. {
  936. static RECT rect;
  937. GetClientRect(__hwnd, &rect);
  938. return rect.right;
  939. }
  940. unsigned int Platform::getDisplayHeight()
  941. {
  942. static RECT rect;
  943. GetClientRect(__hwnd, &rect);
  944. return rect.bottom;
  945. }
  946. double Platform::getAbsoluteTime()
  947. {
  948. LARGE_INTEGER queryTime;
  949. QueryPerformanceCounter(&queryTime);
  950. GP_ASSERT(__timeTicksPerMillis);
  951. __timeAbsolute = queryTime.QuadPart / __timeTicksPerMillis;
  952. return __timeAbsolute - __timeStart;
  953. }
  954. void Platform::setAbsoluteTime(double time)
  955. {
  956. __timeAbsolute = time;
  957. }
  958. bool Platform::isVsync()
  959. {
  960. return __vsync;
  961. }
  962. void Platform::setVsync(bool enable)
  963. {
  964. __vsync = enable;
  965. if (wglSwapIntervalEXT)
  966. wglSwapIntervalEXT(__vsync ? 1 : 0);
  967. else
  968. __vsync = false;
  969. }
  970. void Platform::swapBuffers()
  971. {
  972. if (__hdc)
  973. SwapBuffers(__hdc);
  974. }
  975. void Platform::sleep(long ms)
  976. {
  977. Sleep(ms);
  978. }
  979. void Platform::setMultiSampling(bool enabled)
  980. {
  981. if (enabled == __multiSampling)
  982. {
  983. return;
  984. }
  985. if (enabled)
  986. {
  987. glEnable(GL_MULTISAMPLE);
  988. }
  989. else
  990. {
  991. glDisable(GL_MULTISAMPLE);
  992. }
  993. __multiSampling = enabled;
  994. }
  995. bool Platform::isMultiSampling()
  996. {
  997. return __multiSampling;
  998. }
  999. void Platform::setMultiTouch(bool enabled)
  1000. {
  1001. // not supported
  1002. }
  1003. bool Platform::isMultiTouch()
  1004. {
  1005. return false;
  1006. }
  1007. bool Platform::hasAccelerometer()
  1008. {
  1009. return false;
  1010. }
  1011. void Platform::getAccelerometerValues(float* pitch, float* roll)
  1012. {
  1013. GP_ASSERT(pitch);
  1014. GP_ASSERT(roll);
  1015. *pitch = 0;
  1016. *roll = 0;
  1017. }
  1018. void Platform::getSensorValues(float* accelX, float* accelY, float* accelZ, float* gyroX, float* gyroY, float* gyroZ)
  1019. {
  1020. if (accelX)
  1021. {
  1022. *accelX = 0;
  1023. }
  1024. if (accelY)
  1025. {
  1026. *accelY = 0;
  1027. }
  1028. if (accelZ)
  1029. {
  1030. *accelZ = 0;
  1031. }
  1032. if (gyroX)
  1033. {
  1034. *gyroX = 0;
  1035. }
  1036. if (gyroY)
  1037. {
  1038. *gyroY = 0;
  1039. }
  1040. if (gyroZ)
  1041. {
  1042. *gyroZ = 0;
  1043. }
  1044. }
  1045. void Platform::getArguments(int* argc, char*** argv)
  1046. {
  1047. if (argc)
  1048. *argc = __argc;
  1049. if (argv)
  1050. *argv = __argv;
  1051. }
  1052. bool Platform::hasMouse()
  1053. {
  1054. return true;
  1055. }
  1056. void Platform::setMouseCaptured(bool captured)
  1057. {
  1058. if (captured != __mouseCaptured)
  1059. {
  1060. if (captured)
  1061. {
  1062. // Hide the cursor and warp it to the center of the screen
  1063. __mouseCapturePoint.x = getDisplayWidth() / 2;
  1064. __mouseCapturePoint.y = getDisplayHeight() / 2;
  1065. ShowCursor(FALSE);
  1066. WarpMouse(__mouseCapturePoint.x, __mouseCapturePoint.y);
  1067. }
  1068. else
  1069. {
  1070. // Restore cursor
  1071. WarpMouse(__mouseCapturePoint.x, __mouseCapturePoint.y);
  1072. ShowCursor(TRUE);
  1073. }
  1074. __mouseCaptured = captured;
  1075. }
  1076. }
  1077. bool Platform::isMouseCaptured()
  1078. {
  1079. return __mouseCaptured;
  1080. }
  1081. void Platform::setCursorVisible(bool visible)
  1082. {
  1083. if (visible != __cursorVisible)
  1084. {
  1085. ShowCursor(visible ? TRUE : FALSE);
  1086. __cursorVisible = visible;
  1087. }
  1088. }
  1089. bool Platform::isCursorVisible()
  1090. {
  1091. return __cursorVisible;
  1092. }
  1093. void Platform::displayKeyboard(bool display)
  1094. {
  1095. // Do nothing.
  1096. }
  1097. bool Platform::isGestureSupported(Gesture::GestureEvent evt)
  1098. {
  1099. return false;
  1100. }
  1101. void Platform::registerGesture(Gesture::GestureEvent evt)
  1102. {
  1103. }
  1104. void Platform::unregisterGesture(Gesture::GestureEvent evt)
  1105. {
  1106. }
  1107. bool Platform::isGestureRegistered(Gesture::GestureEvent evt)
  1108. {
  1109. return false;
  1110. }
  1111. #ifdef GP_USE_GAMEPAD
  1112. void Platform::pollGamepadState(Gamepad* gamepad)
  1113. {
  1114. GP_ASSERT(gamepad->_handle < XUSER_MAX_COUNT);
  1115. if (XInputGetState(gamepad->_handle, &__xInputState) == NO_ERROR)
  1116. {
  1117. WORD buttons = __xInputState.Gamepad.wButtons;
  1118. // Map XInput buttons to Gamepad::ButtonMappings enum.
  1119. static const unsigned int xInputMapping[16] = {
  1120. Gamepad::BUTTON_UP, // 0x0001
  1121. Gamepad::BUTTON_DOWN, // 0x0002
  1122. Gamepad::BUTTON_LEFT, // 0x0004
  1123. Gamepad::BUTTON_RIGHT, // 0x0008
  1124. Gamepad::BUTTON_MENU2, // 0x0010
  1125. Gamepad::BUTTON_MENU1, // 0x0020
  1126. Gamepad::BUTTON_L3, // 0x0040
  1127. Gamepad::BUTTON_R3, // 0x0080
  1128. Gamepad::BUTTON_L1, // 0x0100
  1129. Gamepad::BUTTON_R1, // 0x0200
  1130. 0,
  1131. 0,
  1132. Gamepad::BUTTON_A, // 0x1000
  1133. Gamepad::BUTTON_B, // 0x2000
  1134. Gamepad::BUTTON_X, // 0x4000
  1135. Gamepad::BUTTON_Y // 0x8000
  1136. };
  1137. const unsigned int *mapping = xInputMapping;
  1138. unsigned int mappedButtons;
  1139. for (mappedButtons = 0; buttons; buttons >>= 1, mapping++)
  1140. {
  1141. if (buttons & 1)
  1142. {
  1143. mappedButtons |= (1 << *mapping);
  1144. }
  1145. }
  1146. gamepad->setButtons(mappedButtons);
  1147. unsigned int i;
  1148. for (i = 0; i < gamepad->_joystickCount; ++i)
  1149. {
  1150. GP_ASSERT(i < 2);
  1151. float x;
  1152. float y;
  1153. switch (i)
  1154. {
  1155. case 0:
  1156. x = normalizeXInputJoystickAxis(__xInputState.Gamepad.sThumbLX, XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE);
  1157. y = normalizeXInputJoystickAxis(__xInputState.Gamepad.sThumbLY, XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE);
  1158. break;
  1159. case 1:
  1160. x = normalizeXInputJoystickAxis(__xInputState.Gamepad.sThumbRX, XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE);
  1161. y = normalizeXInputJoystickAxis(__xInputState.Gamepad.sThumbRY, XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE);
  1162. break;
  1163. }
  1164. gamepad->setJoystickValue(i, x, y);
  1165. }
  1166. for (i = 0; i < gamepad->_triggerCount; ++i)
  1167. {
  1168. GP_ASSERT(i < 2);
  1169. BYTE trigger;
  1170. switch (i)
  1171. {
  1172. case 0:
  1173. trigger = __xInputState.Gamepad.bLeftTrigger;
  1174. break;
  1175. case 1:
  1176. trigger = __xInputState.Gamepad.bRightTrigger;
  1177. break;
  1178. }
  1179. if (trigger < XINPUT_GAMEPAD_TRIGGER_THRESHOLD)
  1180. {
  1181. gamepad->setTriggerValue(i, 0.0f);
  1182. }
  1183. else
  1184. {
  1185. gamepad->setTriggerValue(i, (float)trigger / 255.0f);
  1186. }
  1187. }
  1188. }
  1189. }
  1190. #else
  1191. void Platform::pollGamepadState(Gamepad* gamepad) { }
  1192. #endif
  1193. void Platform::shutdownInternal()
  1194. {
  1195. Game::getInstance()->shutdown();
  1196. }
  1197. bool Platform::launchURL(const char* url)
  1198. {
  1199. if (url == NULL || *url == '\0')
  1200. return false;
  1201. // Success when result code > 32
  1202. int len = MultiByteToWideChar(CP_ACP, 0, url, -1, NULL, 0);
  1203. wchar_t* wurl = new wchar_t[len];
  1204. MultiByteToWideChar(CP_ACP, 0, url, -1, wurl, len);
  1205. int r = (int)ShellExecute(NULL, NULL, wurl, NULL, NULL, SW_SHOWNORMAL);
  1206. SAFE_DELETE_ARRAY(wurl);
  1207. return (r > 32);
  1208. }
  1209. std::string Platform::displayFileDialog(size_t mode, const char* title, const char* filterDescription, const char* filterExtensions, const char* initialDirectory)
  1210. {
  1211. std::string filename;
  1212. OPENFILENAMEA ofn;
  1213. memset(&ofn, 0, sizeof(ofn));
  1214. char currentDir[1024];
  1215. char absPath[1024];
  1216. std::string initialDirectoryStr;
  1217. if (initialDirectory == NULL)
  1218. {
  1219. GetCurrentDirectoryA(1024, currentDir);
  1220. initialDirectoryStr = currentDir;
  1221. }
  1222. else
  1223. {
  1224. GetFullPathNameA(initialDirectory, 1024, absPath, 0);
  1225. initialDirectoryStr = absPath;
  1226. }
  1227. // Filter on extensions
  1228. std::istringstream f(filterExtensions);
  1229. std::string s;
  1230. unsigned int count = 0;
  1231. std::string descStr = filterDescription;
  1232. descStr += " (";
  1233. std::string extStr = "";
  1234. while (std::getline(f, s, ';'))
  1235. {
  1236. if (count > 0)
  1237. extStr += ";";
  1238. extStr += "*.";
  1239. extStr += s;
  1240. count++;
  1241. }
  1242. descStr += extStr;
  1243. descStr += ")";
  1244. char filter[1024];
  1245. memset(filter, 0, 1024);
  1246. strcpy(filter, descStr.c_str());
  1247. strcpy(filter + descStr.length() + 1, extStr.c_str());
  1248. char szFileName[1024] = "";
  1249. ofn.lpstrFile = szFileName;
  1250. ofn.lStructSize = sizeof(ofn);
  1251. ofn.hwndOwner = GetForegroundWindow();
  1252. ofn.lpstrTitle = title;
  1253. ofn.lpstrFilter = filter;
  1254. ofn.lpstrInitialDir = initialDirectoryStr.c_str();
  1255. ofn.nMaxFile = 1024;
  1256. ofn.lpstrDefExt = filter;
  1257. if (mode == FileSystem::OPEN)
  1258. {
  1259. ofn.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST;
  1260. GetOpenFileNameA(&ofn);
  1261. }
  1262. else
  1263. {
  1264. ofn.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
  1265. GetSaveFileNameA(&ofn);
  1266. }
  1267. filename = szFileName;
  1268. return filename;
  1269. }
  1270. }
  1271. #endif