PlatformWin32.cpp 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461
  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 fullscreen is specified, and width is not, interpret this
  804. // as meaning, "use the current resolution".
  805. if (params.fullscreen && params.rect.right == 0){
  806. params.rect.right = params.rect.left + GetSystemMetrics(SM_CXSCREEN);
  807. params.rect.bottom = params.rect.top + GetSystemMetrics(SM_CYSCREEN);
  808. }
  809. // If window size was not specified, set it to a default value
  810. if (params.rect.right == 0)
  811. params.rect.right = params.rect.left + DEFAULT_RESOLUTION_X;
  812. if (params.rect.bottom == 0)
  813. params.rect.bottom = params.rect.top + DEFAULT_RESOLUTION_Y;
  814. int width = params.rect.right - params.rect.left;
  815. int height = params.rect.bottom - params.rect.top;
  816. if (params.fullscreen)
  817. {
  818. // Enumerate all supposed display settings
  819. bool modeSupported = false;
  820. DWORD modeNum = 0;
  821. DEVMODE devMode;
  822. memset(&devMode, 0, sizeof(DEVMODE));
  823. devMode.dmSize = sizeof(DEVMODE);
  824. devMode.dmDriverExtra = 0;
  825. while (EnumDisplaySettings(NULL, modeNum++, &devMode) != 0)
  826. {
  827. // Is mode supported?
  828. if (devMode.dmPelsWidth == width &&
  829. devMode.dmPelsHeight == height &&
  830. devMode.dmBitsPerPel == DEFAULT_COLOR_BUFFER_SIZE)
  831. {
  832. modeSupported = true;
  833. break;
  834. }
  835. }
  836. // If the requested mode is not supported, fall back to a safe default
  837. if (!modeSupported)
  838. {
  839. width = DEFAULT_RESOLUTION_X;
  840. height = DEFAULT_RESOLUTION_Y;
  841. params.rect.right = params.rect.left + width;
  842. params.rect.bottom = params.rect.top + height;
  843. }
  844. }
  845. if (!__attachToWindow)
  846. {
  847. // Register our window class.
  848. WNDCLASSEX wc;
  849. wc.cbSize = sizeof(WNDCLASSEX);
  850. wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  851. wc.lpfnWndProc = (WNDPROC)__WndProc;
  852. wc.cbClsExtra = 0;
  853. wc.cbWndExtra = 0;
  854. wc.hInstance = __hinstance;
  855. wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  856. wc.hIconSm = NULL;
  857. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  858. wc.hbrBackground = NULL; // No brush - we are going to paint our own background
  859. wc.lpszMenuName = NULL; // No default menu
  860. wc.lpszClassName = L"gameplay";
  861. if (!::RegisterClassEx(&wc))
  862. {
  863. GP_ERROR("Failed to register window class.");
  864. goto error;
  865. }
  866. if (params.fullscreen)
  867. {
  868. DEVMODE dm;
  869. memset(&dm, 0, sizeof(dm));
  870. dm.dmSize= sizeof(dm);
  871. dm.dmPelsWidth = width;
  872. dm.dmPelsHeight = height;
  873. dm.dmBitsPerPel = DEFAULT_COLOR_BUFFER_SIZE;
  874. dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  875. // Try to set selected mode and get results. NOTE: CDS_FULLSCREEN gets rid of start bar.
  876. if (ChangeDisplaySettings(&dm, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
  877. {
  878. params.fullscreen = false;
  879. GP_ERROR("Failed to start game in full-screen mode with resolution %dx%d.", width, height);
  880. goto error;
  881. }
  882. }
  883. if (!initializeGL(&params))
  884. goto error;
  885. // Show the window.
  886. ShowWindow(__hwnd, SW_SHOW);
  887. }
  888. else
  889. {
  890. // Attach to a previous windows
  891. __hwnd = (HWND)__attachToWindow;
  892. __hdc = GetDC(__hwnd);
  893. SetWindowLongPtr(__hwnd, GWL_WNDPROC, (LONG)(WNDPROC)__WndProc);
  894. if (!initializeGL(NULL))
  895. goto error;
  896. }
  897. #ifdef USE_XINPUT
  898. // Initialize XInputGamepads.
  899. __xinputGamepads = new XInputGamepad[XUSER_MAX_COUNT];
  900. for (unsigned int i = 0; i < XUSER_MAX_COUNT; i++)
  901. {
  902. switch(i)
  903. {
  904. case 0:
  905. __xinputGamepads[i].id = "XINPUT 1";
  906. break;
  907. case 1:
  908. __xinputGamepads[i].id = "XINPUT 2";
  909. break;
  910. case 2:
  911. __xinputGamepads[i].id = "XINPUT 3";
  912. break;
  913. case 3:
  914. __xinputGamepads[i].id = "XINPUT 4";
  915. break;
  916. }
  917. __xinputGamepads[i].connected = false;
  918. __xinputGamepads[i].handle = -1;
  919. }
  920. #endif
  921. return platform;
  922. error:
  923. exit(0);
  924. return NULL;
  925. }
  926. int Platform::enterMessagePump()
  927. {
  928. GP_ASSERT(_game);
  929. // Get the initial time.
  930. LARGE_INTEGER tps;
  931. QueryPerformanceFrequency(&tps);
  932. __timeTicksPerMillis = (double)(tps.QuadPart / 1000L);
  933. LARGE_INTEGER queryTime;
  934. QueryPerformanceCounter(&queryTime);
  935. GP_ASSERT(__timeTicksPerMillis);
  936. __timeStart = queryTime.QuadPart / __timeTicksPerMillis;
  937. // Set the initial pitch and roll values.
  938. __pitch = 0.0;
  939. __roll = 0.0;
  940. SwapBuffers(__hdc);
  941. if (_game->getState() != Game::RUNNING)
  942. _game->run();
  943. if (__attachToWindow)
  944. return 0;
  945. // Enter event dispatch loop.
  946. MSG msg;
  947. while (true)
  948. {
  949. if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  950. {
  951. TranslateMessage(&msg);
  952. DispatchMessage(&msg);
  953. if (msg.message == WM_QUIT)
  954. {
  955. _game->exit();
  956. break;
  957. }
  958. }
  959. else
  960. {
  961. _game->frame();
  962. SwapBuffers(__hdc);
  963. }
  964. // If we are done, then exit.
  965. if (_game->getState() == Game::UNINITIALIZED)
  966. break;
  967. }
  968. return msg.wParam;
  969. }
  970. void Platform::signalShutdown()
  971. {
  972. // nothing to do
  973. }
  974. unsigned int Platform::getDisplayWidth()
  975. {
  976. static RECT rect;
  977. GetClientRect(__hwnd, &rect);
  978. return rect.right;
  979. }
  980. unsigned int Platform::getDisplayHeight()
  981. {
  982. static RECT rect;
  983. GetClientRect(__hwnd, &rect);
  984. return rect.bottom;
  985. }
  986. double Platform::getAbsoluteTime()
  987. {
  988. LARGE_INTEGER queryTime;
  989. QueryPerformanceCounter(&queryTime);
  990. GP_ASSERT(__timeTicksPerMillis);
  991. __timeAbsolute = queryTime.QuadPart / __timeTicksPerMillis;
  992. return __timeAbsolute;
  993. }
  994. void Platform::setAbsoluteTime(double time)
  995. {
  996. __timeAbsolute = time;
  997. }
  998. bool Platform::isVsync()
  999. {
  1000. return __vsync;
  1001. }
  1002. void Platform::setVsync(bool enable)
  1003. {
  1004. wglSwapIntervalEXT(enable ? 1 : 0);
  1005. __vsync = enable;
  1006. }
  1007. void Platform::swapBuffers()
  1008. {
  1009. if (__hdc)
  1010. SwapBuffers(__hdc);
  1011. }
  1012. void Platform::sleep(long ms)
  1013. {
  1014. Sleep(ms);
  1015. }
  1016. void Platform::setMultiTouch(bool enabled)
  1017. {
  1018. // not supported
  1019. }
  1020. bool Platform::isMultiTouch()
  1021. {
  1022. return false;
  1023. }
  1024. void Platform::getAccelerometerValues(float* pitch, float* roll)
  1025. {
  1026. GP_ASSERT(pitch);
  1027. GP_ASSERT(roll);
  1028. *pitch = __pitch;
  1029. *roll = __roll;
  1030. }
  1031. bool Platform::hasMouse()
  1032. {
  1033. return true;
  1034. }
  1035. void Platform::setMouseCaptured(bool captured)
  1036. {
  1037. if (captured != __mouseCaptured)
  1038. {
  1039. if (captured)
  1040. {
  1041. // Hide the cursor and warp it to the center of the screen
  1042. __mouseCapturePoint.x = getDisplayWidth() / 2;
  1043. __mouseCapturePoint.y = getDisplayHeight() / 2;
  1044. ShowCursor(FALSE);
  1045. WarpMouse(__mouseCapturePoint.x, __mouseCapturePoint.y);
  1046. }
  1047. else
  1048. {
  1049. // Restore cursor
  1050. WarpMouse(__mouseCapturePoint.x, __mouseCapturePoint.y);
  1051. ShowCursor(TRUE);
  1052. }
  1053. __mouseCaptured = captured;
  1054. }
  1055. }
  1056. bool Platform::isMouseCaptured()
  1057. {
  1058. return __mouseCaptured;
  1059. }
  1060. void Platform::setCursorVisible(bool visible)
  1061. {
  1062. if (visible != __cursorVisible)
  1063. {
  1064. ShowCursor(visible ? TRUE : FALSE);
  1065. __cursorVisible = visible;
  1066. }
  1067. }
  1068. bool Platform::isCursorVisible()
  1069. {
  1070. return __cursorVisible;
  1071. }
  1072. void Platform::displayKeyboard(bool display)
  1073. {
  1074. // Do nothing.
  1075. }
  1076. bool Platform::isGestureSupported(Gesture::GestureEvent evt)
  1077. {
  1078. return false;
  1079. }
  1080. void Platform::registerGesture(Gesture::GestureEvent evt)
  1081. {
  1082. }
  1083. void Platform::unregisterGesture(Gesture::GestureEvent evt)
  1084. {
  1085. }
  1086. bool Platform::isGestureRegistered(Gesture::GestureEvent evt)
  1087. {
  1088. return false;
  1089. }
  1090. unsigned int Platform::getGamepadsConnected()
  1091. {
  1092. // Check to see what xbox360 gamepads are connected.
  1093. __gamepadsConnected = 0;
  1094. #ifdef USE_XINPUT
  1095. for (unsigned int i = 0; i < XUSER_MAX_COUNT; i++)
  1096. {
  1097. if (isGamepadConnected(i))
  1098. __gamepadsConnected++;
  1099. }
  1100. #endif
  1101. return __gamepadsConnected;
  1102. }
  1103. bool Platform::isGamepadConnected(unsigned int gamepadHandle)
  1104. {
  1105. #ifdef USE_XINPUT
  1106. GP_ASSERT(0 <= gamepadHandle);
  1107. GP_ASSERT(gamepadHandle < XUSER_MAX_COUNT);
  1108. Game* game = Game::getInstance();
  1109. GP_ASSERT(game);
  1110. if (__xinputGamepads[gamepadHandle].handle == -1)
  1111. __xinputGamepads[gamepadHandle].handle = game->createGamepad(__xinputGamepads[gamepadHandle].id.c_str(), gamepadHandle, __xinputGamepads[gamepadHandle].BUTTON_COUNT,
  1112. __xinputGamepads[gamepadHandle].JOYSTICK_COUNT, __xinputGamepads[gamepadHandle].TRIGGER_COUNT);
  1113. bool isConnected = __xinputGamepads[gamepadHandle].connected;
  1114. if (getXInputState(gamepadHandle))
  1115. {
  1116. if (!isConnected)
  1117. {
  1118. __xinputGamepads[gamepadHandle].connected = true;
  1119. Gamepad* gamepad = game->getGamepad(__xinputGamepads[gamepadHandle].handle);
  1120. GP_ASSERT(gamepad);
  1121. if (game->isInitialized())
  1122. game->gamepadEvent(Gamepad::CONNECTED_EVENT, game->getGamepad(__xinputGamepads[gamepadHandle].handle));
  1123. }
  1124. return true;
  1125. }
  1126. else
  1127. {
  1128. if (isConnected)
  1129. {
  1130. // if it was connected, but now isn't pass the detached event to gamepadEvent()
  1131. __xinputGamepads[gamepadHandle].connected = false;
  1132. Gamepad* gamepad = game->getGamepad(__xinputGamepads[gamepadHandle].handle);
  1133. GP_ASSERT(gamepad);
  1134. if (game->isInitialized())
  1135. game->gamepadEvent(Gamepad::DISCONNECTED_EVENT, game->getGamepad(__xinputGamepads[gamepadHandle].handle));
  1136. }
  1137. return false;
  1138. }
  1139. #endif
  1140. return false;
  1141. }
  1142. const char* Platform::getGamepadId(unsigned int gamepadHandle)
  1143. {
  1144. #ifdef USE_XINPUT
  1145. GP_ASSERT(0 <= gamepadHandle);
  1146. GP_ASSERT(gamepadHandle < XUSER_MAX_COUNT);
  1147. return __xinputGamepads[gamepadHandle].id.c_str();
  1148. #endif
  1149. return NULL;
  1150. }
  1151. unsigned int Platform::getGamepadButtonCount(unsigned int gamepadHandle)
  1152. {
  1153. #ifdef USE_XINPUT
  1154. GP_ASSERT(0 <= gamepadHandle);
  1155. GP_ASSERT(gamepadHandle < XUSER_MAX_COUNT);
  1156. if (!__xinputGamepads[gamepadHandle].connected)
  1157. return 0;
  1158. return XInputGamepad::BUTTON_COUNT;
  1159. #endif
  1160. return 0;
  1161. }
  1162. bool Platform::getGamepadButtonState(unsigned int gamepadHandle, unsigned int buttonIndex)
  1163. {
  1164. #ifdef USE_XINPUT
  1165. GP_ASSERT(0 <= gamepadHandle);
  1166. GP_ASSERT(gamepadHandle < XUSER_MAX_COUNT);
  1167. return getXInputButtonState(gamepadHandle, buttonIndex);
  1168. #endif
  1169. return false;
  1170. }
  1171. unsigned int Platform::getGamepadJoystickCount(unsigned int gamepadHandle)
  1172. {
  1173. #ifdef USE_XINPUT
  1174. GP_ASSERT(0 <= gamepadHandle);
  1175. GP_ASSERT(gamepadHandle < XUSER_MAX_COUNT);
  1176. if (!__xinputGamepads[gamepadHandle].connected)
  1177. return 0;
  1178. return XInputGamepad::JOYSTICK_COUNT;
  1179. #endif
  1180. return 0;
  1181. }
  1182. float Platform::getGamepadJoystickAxisX(unsigned int gamepadHandle, unsigned int joystickIndex)
  1183. {
  1184. #ifdef USE_XINPUT
  1185. GP_ASSERT(0 <= gamepadHandle);
  1186. GP_ASSERT(gamepadHandle < XUSER_MAX_COUNT);
  1187. return getXInputJoystickAxisX(gamepadHandle, joystickIndex);
  1188. #endif
  1189. return 0.0f;
  1190. }
  1191. float Platform::getGamepadJoystickAxisY(unsigned int gamepadHandle, unsigned int joystickIndex)
  1192. {
  1193. #ifdef USE_XINPUT
  1194. GP_ASSERT(0 <= gamepadHandle);
  1195. GP_ASSERT(gamepadHandle < XUSER_MAX_COUNT);
  1196. return getXInputJoystickAxisY(gamepadHandle, joystickIndex);
  1197. #endif
  1198. return 0.0f;
  1199. }
  1200. void Platform::getGamepadJoystickAxisValues(unsigned int gamepadHandle, unsigned int joystickIndex, Vector2* outValue)
  1201. {
  1202. #ifdef USE_XINPUT
  1203. GP_ASSERT(0 <= gamepadHandle);
  1204. GP_ASSERT(gamepadHandle < XUSER_MAX_COUNT);
  1205. getXInputJoystickAxisValues(gamepadHandle, joystickIndex, outValue);
  1206. #endif
  1207. }
  1208. bool Platform::isGamepadJoystickActive(unsigned int gamepadHandle, unsigned int joystickIndex)
  1209. {
  1210. #ifdef USE_XINPUT
  1211. GP_ASSERT(0 <= gamepadHandle);
  1212. GP_ASSERT(gamepadHandle < XUSER_MAX_COUNT);
  1213. return (getXInputJoystickAxisX(gamepadHandle, joystickIndex) != 0.0f || getXInputJoystickAxisY(gamepadHandle, joystickIndex) != 0.0f);
  1214. #endif
  1215. return false;
  1216. }
  1217. unsigned int Platform::getGamepadTriggerCount(unsigned int gamepadHandle)
  1218. {
  1219. #ifdef USE_XINPUT
  1220. GP_ASSERT(0 <= gamepadHandle);
  1221. GP_ASSERT(gamepadHandle < XUSER_MAX_COUNT);
  1222. return XInputGamepad::TRIGGER_COUNT;
  1223. #endif
  1224. return 0;
  1225. }
  1226. float Platform::getGamepadTriggerValue(unsigned int gamepadHandle, unsigned int triggerIndex)
  1227. {
  1228. #ifdef USE_XINPUT
  1229. GP_ASSERT(0 <= gamepadHandle);
  1230. GP_ASSERT(gamepadHandle < XUSER_MAX_COUNT);
  1231. //TODO:
  1232. #endif
  1233. return 0.0f;
  1234. }
  1235. void Platform::touchEventInternal(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  1236. {
  1237. if (!Form::touchEventInternal(evt, x, y, contactIndex))
  1238. {
  1239. Game::getInstance()->touchEvent(evt, x, y, contactIndex);
  1240. Game::getInstance()->getScriptController()->touchEvent(evt, x, y, contactIndex);
  1241. }
  1242. }
  1243. void Platform::keyEventInternal(Keyboard::KeyEvent evt, int key)
  1244. {
  1245. if (!Form::keyEventInternal(evt, key))
  1246. {
  1247. Game::getInstance()->keyEvent(evt, key);
  1248. Game::getInstance()->getScriptController()->keyEvent(evt, key);
  1249. }
  1250. }
  1251. bool Platform::mouseEventInternal(Mouse::MouseEvent evt, int x, int y, int wheelDelta)
  1252. {
  1253. if (Form::mouseEventInternal(evt, x, y, wheelDelta))
  1254. {
  1255. return true;
  1256. }
  1257. else if (Game::getInstance()->mouseEvent(evt, x, y, wheelDelta))
  1258. {
  1259. return true;
  1260. }
  1261. else
  1262. {
  1263. return Game::getInstance()->getScriptController()->mouseEvent(evt, x, y, wheelDelta);
  1264. }
  1265. }
  1266. }
  1267. #endif