PlatformWindows.cpp 44 KB

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