PlatformWin32.cpp 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454
  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, GWL_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. break;
  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 msg.wParam;
  963. }
  964. void Platform::signalShutdown()
  965. {
  966. // nothing to do
  967. }
  968. unsigned int Platform::getDisplayWidth()
  969. {
  970. static RECT rect;
  971. GetClientRect(__hwnd, &rect);
  972. return rect.right;
  973. }
  974. unsigned int Platform::getDisplayHeight()
  975. {
  976. static RECT rect;
  977. GetClientRect(__hwnd, &rect);
  978. return rect.bottom;
  979. }
  980. double Platform::getAbsoluteTime()
  981. {
  982. LARGE_INTEGER queryTime;
  983. QueryPerformanceCounter(&queryTime);
  984. GP_ASSERT(__timeTicksPerMillis);
  985. __timeAbsolute = queryTime.QuadPart / __timeTicksPerMillis;
  986. return __timeAbsolute;
  987. }
  988. void Platform::setAbsoluteTime(double time)
  989. {
  990. __timeAbsolute = time;
  991. }
  992. bool Platform::isVsync()
  993. {
  994. return __vsync;
  995. }
  996. void Platform::setVsync(bool enable)
  997. {
  998. wglSwapIntervalEXT(enable ? 1 : 0);
  999. __vsync = enable;
  1000. }
  1001. void Platform::swapBuffers()
  1002. {
  1003. if (__hdc)
  1004. SwapBuffers(__hdc);
  1005. }
  1006. void Platform::sleep(long ms)
  1007. {
  1008. Sleep(ms);
  1009. }
  1010. void Platform::setMultiTouch(bool enabled)
  1011. {
  1012. // not supported
  1013. }
  1014. bool Platform::isMultiTouch()
  1015. {
  1016. return false;
  1017. }
  1018. void Platform::getAccelerometerValues(float* pitch, float* roll)
  1019. {
  1020. GP_ASSERT(pitch);
  1021. GP_ASSERT(roll);
  1022. *pitch = __pitch;
  1023. *roll = __roll;
  1024. }
  1025. bool Platform::hasMouse()
  1026. {
  1027. return true;
  1028. }
  1029. void Platform::setMouseCaptured(bool captured)
  1030. {
  1031. if (captured != __mouseCaptured)
  1032. {
  1033. if (captured)
  1034. {
  1035. // Hide the cursor and warp it to the center of the screen
  1036. __mouseCapturePoint.x = getDisplayWidth() / 2;
  1037. __mouseCapturePoint.y = getDisplayHeight() / 2;
  1038. ShowCursor(FALSE);
  1039. WarpMouse(__mouseCapturePoint.x, __mouseCapturePoint.y);
  1040. }
  1041. else
  1042. {
  1043. // Restore cursor
  1044. WarpMouse(__mouseCapturePoint.x, __mouseCapturePoint.y);
  1045. ShowCursor(TRUE);
  1046. }
  1047. __mouseCaptured = captured;
  1048. }
  1049. }
  1050. bool Platform::isMouseCaptured()
  1051. {
  1052. return __mouseCaptured;
  1053. }
  1054. void Platform::setCursorVisible(bool visible)
  1055. {
  1056. if (visible != __cursorVisible)
  1057. {
  1058. ShowCursor(visible ? TRUE : FALSE);
  1059. __cursorVisible = visible;
  1060. }
  1061. }
  1062. bool Platform::isCursorVisible()
  1063. {
  1064. return __cursorVisible;
  1065. }
  1066. void Platform::displayKeyboard(bool display)
  1067. {
  1068. // Do nothing.
  1069. }
  1070. bool Platform::isGestureSupported(Gesture::GestureEvent evt)
  1071. {
  1072. return false;
  1073. }
  1074. void Platform::registerGesture(Gesture::GestureEvent evt)
  1075. {
  1076. }
  1077. void Platform::unregisterGesture(Gesture::GestureEvent evt)
  1078. {
  1079. }
  1080. bool Platform::isGestureRegistered(Gesture::GestureEvent evt)
  1081. {
  1082. return false;
  1083. }
  1084. unsigned int Platform::getGamepadsConnected()
  1085. {
  1086. // Check to see what xbox360 gamepads are connected.
  1087. __gamepadsConnected = 0;
  1088. #ifdef USE_XINPUT
  1089. for (unsigned int i = 0; i < XUSER_MAX_COUNT; i++)
  1090. {
  1091. if (isGamepadConnected(i))
  1092. __gamepadsConnected++;
  1093. }
  1094. #endif
  1095. return __gamepadsConnected;
  1096. }
  1097. bool Platform::isGamepadConnected(unsigned int gamepadHandle)
  1098. {
  1099. #ifdef USE_XINPUT
  1100. GP_ASSERT(0 <= gamepadHandle);
  1101. GP_ASSERT(gamepadHandle < XUSER_MAX_COUNT);
  1102. Game* game = Game::getInstance();
  1103. GP_ASSERT(game);
  1104. if (__xinputGamepads[gamepadHandle].handle == -1)
  1105. __xinputGamepads[gamepadHandle].handle = game->createGamepad(__xinputGamepads[gamepadHandle].id.c_str(), gamepadHandle, __xinputGamepads[gamepadHandle].BUTTON_COUNT,
  1106. __xinputGamepads[gamepadHandle].JOYSTICK_COUNT, __xinputGamepads[gamepadHandle].TRIGGER_COUNT);
  1107. bool isConnected = __xinputGamepads[gamepadHandle].connected;
  1108. if (getXInputState(gamepadHandle))
  1109. {
  1110. if (!isConnected)
  1111. {
  1112. __xinputGamepads[gamepadHandle].connected = true;
  1113. Gamepad* gamepad = game->getGamepad(__xinputGamepads[gamepadHandle].handle);
  1114. GP_ASSERT(gamepad);
  1115. if (game->isInitialized())
  1116. game->gamepadEvent(Gamepad::CONNECTED_EVENT, game->getGamepad(__xinputGamepads[gamepadHandle].handle));
  1117. }
  1118. return true;
  1119. }
  1120. else
  1121. {
  1122. if (isConnected)
  1123. {
  1124. // if it was connected, but now isn't pass the detached event to gamepadEvent()
  1125. __xinputGamepads[gamepadHandle].connected = false;
  1126. Gamepad* gamepad = game->getGamepad(__xinputGamepads[gamepadHandle].handle);
  1127. GP_ASSERT(gamepad);
  1128. if (game->isInitialized())
  1129. game->gamepadEvent(Gamepad::DISCONNECTED_EVENT, game->getGamepad(__xinputGamepads[gamepadHandle].handle));
  1130. }
  1131. return false;
  1132. }
  1133. #endif
  1134. return false;
  1135. }
  1136. const char* Platform::getGamepadId(unsigned int gamepadHandle)
  1137. {
  1138. #ifdef USE_XINPUT
  1139. GP_ASSERT(0 <= gamepadHandle);
  1140. GP_ASSERT(gamepadHandle < XUSER_MAX_COUNT);
  1141. return __xinputGamepads[gamepadHandle].id.c_str();
  1142. #endif
  1143. return NULL;
  1144. }
  1145. unsigned int Platform::getGamepadButtonCount(unsigned int gamepadHandle)
  1146. {
  1147. #ifdef USE_XINPUT
  1148. GP_ASSERT(0 <= gamepadHandle);
  1149. GP_ASSERT(gamepadHandle < XUSER_MAX_COUNT);
  1150. if (!__xinputGamepads[gamepadHandle].connected)
  1151. return 0;
  1152. return XInputGamepad::BUTTON_COUNT;
  1153. #endif
  1154. return 0;
  1155. }
  1156. bool Platform::getGamepadButtonState(unsigned int gamepadHandle, unsigned int buttonIndex)
  1157. {
  1158. #ifdef USE_XINPUT
  1159. GP_ASSERT(0 <= gamepadHandle);
  1160. GP_ASSERT(gamepadHandle < XUSER_MAX_COUNT);
  1161. return getXInputButtonState(gamepadHandle, buttonIndex);
  1162. #endif
  1163. return false;
  1164. }
  1165. unsigned int Platform::getGamepadJoystickCount(unsigned int gamepadHandle)
  1166. {
  1167. #ifdef USE_XINPUT
  1168. GP_ASSERT(0 <= gamepadHandle);
  1169. GP_ASSERT(gamepadHandle < XUSER_MAX_COUNT);
  1170. if (!__xinputGamepads[gamepadHandle].connected)
  1171. return 0;
  1172. return XInputGamepad::JOYSTICK_COUNT;
  1173. #endif
  1174. return 0;
  1175. }
  1176. float Platform::getGamepadJoystickAxisX(unsigned int gamepadHandle, unsigned int joystickIndex)
  1177. {
  1178. #ifdef USE_XINPUT
  1179. GP_ASSERT(0 <= gamepadHandle);
  1180. GP_ASSERT(gamepadHandle < XUSER_MAX_COUNT);
  1181. return getXInputJoystickAxisX(gamepadHandle, joystickIndex);
  1182. #endif
  1183. return 0.0f;
  1184. }
  1185. float Platform::getGamepadJoystickAxisY(unsigned int gamepadHandle, unsigned int joystickIndex)
  1186. {
  1187. #ifdef USE_XINPUT
  1188. GP_ASSERT(0 <= gamepadHandle);
  1189. GP_ASSERT(gamepadHandle < XUSER_MAX_COUNT);
  1190. return getXInputJoystickAxisY(gamepadHandle, joystickIndex);
  1191. #endif
  1192. return 0.0f;
  1193. }
  1194. void Platform::getGamepadJoystickAxisValues(unsigned int gamepadHandle, unsigned int joystickIndex, Vector2* outValue)
  1195. {
  1196. #ifdef USE_XINPUT
  1197. GP_ASSERT(0 <= gamepadHandle);
  1198. GP_ASSERT(gamepadHandle < XUSER_MAX_COUNT);
  1199. getXInputJoystickAxisValues(gamepadHandle, joystickIndex, outValue);
  1200. #endif
  1201. }
  1202. bool Platform::isGamepadJoystickActive(unsigned int gamepadHandle, unsigned int joystickIndex)
  1203. {
  1204. #ifdef USE_XINPUT
  1205. GP_ASSERT(0 <= gamepadHandle);
  1206. GP_ASSERT(gamepadHandle < XUSER_MAX_COUNT);
  1207. return (getXInputJoystickAxisX(gamepadHandle, joystickIndex) != 0.0f || getXInputJoystickAxisY(gamepadHandle, joystickIndex) != 0.0f);
  1208. #endif
  1209. return false;
  1210. }
  1211. unsigned int Platform::getGamepadTriggerCount(unsigned int gamepadHandle)
  1212. {
  1213. #ifdef USE_XINPUT
  1214. GP_ASSERT(0 <= gamepadHandle);
  1215. GP_ASSERT(gamepadHandle < XUSER_MAX_COUNT);
  1216. return XInputGamepad::TRIGGER_COUNT;
  1217. #endif
  1218. return 0;
  1219. }
  1220. float Platform::getGamepadTriggerValue(unsigned int gamepadHandle, unsigned int triggerIndex)
  1221. {
  1222. #ifdef USE_XINPUT
  1223. GP_ASSERT(0 <= gamepadHandle);
  1224. GP_ASSERT(gamepadHandle < XUSER_MAX_COUNT);
  1225. //TODO:
  1226. #endif
  1227. return 0.0f;
  1228. }
  1229. void Platform::touchEventInternal(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  1230. {
  1231. if (!Form::touchEventInternal(evt, x, y, contactIndex))
  1232. {
  1233. Game::getInstance()->touchEvent(evt, x, y, contactIndex);
  1234. Game::getInstance()->getScriptController()->touchEvent(evt, x, y, contactIndex);
  1235. }
  1236. }
  1237. void Platform::keyEventInternal(Keyboard::KeyEvent evt, int key)
  1238. {
  1239. if (!Form::keyEventInternal(evt, key))
  1240. {
  1241. Game::getInstance()->keyEvent(evt, key);
  1242. Game::getInstance()->getScriptController()->keyEvent(evt, key);
  1243. }
  1244. }
  1245. bool Platform::mouseEventInternal(Mouse::MouseEvent evt, int x, int y, int wheelDelta)
  1246. {
  1247. if (Form::mouseEventInternal(evt, x, y, wheelDelta))
  1248. {
  1249. return true;
  1250. }
  1251. else if (Game::getInstance()->mouseEvent(evt, x, y, wheelDelta))
  1252. {
  1253. return true;
  1254. }
  1255. else
  1256. {
  1257. return Game::getInstance()->getScriptController()->mouseEvent(evt, x, y, wheelDelta);
  1258. }
  1259. }
  1260. }
  1261. #endif