PlatformWin32.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  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 "ScriptController.h"
  8. #include <GL/wglew.h>
  9. #include <windowsx.h>
  10. using gameplay::printError;
  11. // Default to 720p
  12. static int __width = 1280;
  13. static int __height = 720;
  14. static double __timeTicksPerMillis;
  15. static double __timeStart;
  16. static double __timeAbsolute;
  17. static bool __vsync = WINDOW_VSYNC;
  18. static float __roll;
  19. static float __pitch;
  20. static HINSTANCE __hinstance = 0;
  21. static HWND __attachToWindow = 0;
  22. static HWND __hwnd = 0;
  23. static HDC __hdc = 0;
  24. static HGLRC __hrc = 0;
  25. static bool __mouseCaptured = false;
  26. static POINT __mouseCapturePoint = { 0, 0 };
  27. static bool __cursorVisible = true;
  28. static gameplay::Keyboard::Key getKey(WPARAM win32KeyCode, bool shiftDown)
  29. {
  30. switch (win32KeyCode)
  31. {
  32. case VK_PAUSE:
  33. return gameplay::Keyboard::KEY_PAUSE;
  34. case VK_SCROLL:
  35. return gameplay::Keyboard::KEY_SCROLL_LOCK;
  36. case VK_PRINT:
  37. return gameplay::Keyboard::KEY_PRINT;
  38. case VK_ESCAPE:
  39. return gameplay::Keyboard::KEY_ESCAPE;
  40. case VK_BACK:
  41. return gameplay::Keyboard::KEY_BACKSPACE;
  42. case VK_TAB:
  43. return shiftDown ? gameplay::Keyboard::KEY_BACK_TAB : gameplay::Keyboard::KEY_TAB;
  44. case VK_RETURN:
  45. return gameplay::Keyboard::KEY_RETURN;
  46. case VK_CAPITAL:
  47. return gameplay::Keyboard::KEY_CAPS_LOCK;
  48. case VK_SHIFT:
  49. return gameplay::Keyboard::KEY_SHIFT;
  50. case VK_CONTROL:
  51. return gameplay::Keyboard::KEY_CTRL;
  52. case VK_MENU:
  53. return gameplay::Keyboard::KEY_ALT;
  54. case VK_APPS:
  55. return gameplay::Keyboard::KEY_MENU;
  56. case VK_LSHIFT:
  57. return gameplay::Keyboard::KEY_SHIFT;
  58. case VK_RSHIFT:
  59. return gameplay::Keyboard::KEY_SHIFT;
  60. case VK_LCONTROL:
  61. return gameplay::Keyboard::KEY_CTRL;
  62. case VK_RCONTROL:
  63. return gameplay::Keyboard::KEY_CTRL;
  64. case VK_LMENU:
  65. return gameplay::Keyboard::KEY_ALT;
  66. case VK_RMENU:
  67. return gameplay::Keyboard::KEY_ALT;
  68. case VK_LWIN:
  69. case VK_RWIN:
  70. return gameplay::Keyboard::KEY_HYPER;
  71. case VK_BROWSER_SEARCH:
  72. return gameplay::Keyboard::KEY_SEARCH;
  73. case VK_INSERT:
  74. return gameplay::Keyboard::KEY_INSERT;
  75. case VK_HOME:
  76. return gameplay::Keyboard::KEY_HOME;
  77. case VK_PRIOR:
  78. return gameplay::Keyboard::KEY_PG_UP;
  79. case VK_DELETE:
  80. return gameplay::Keyboard::KEY_DELETE;
  81. case VK_END:
  82. return gameplay::Keyboard::KEY_END;
  83. case VK_NEXT:
  84. return gameplay::Keyboard::KEY_PG_DOWN;
  85. case VK_LEFT:
  86. return gameplay::Keyboard::KEY_LEFT_ARROW;
  87. case VK_RIGHT:
  88. return gameplay::Keyboard::KEY_RIGHT_ARROW;
  89. case VK_UP:
  90. return gameplay::Keyboard::KEY_UP_ARROW;
  91. case VK_DOWN:
  92. return gameplay::Keyboard::KEY_DOWN_ARROW;
  93. case VK_NUMLOCK:
  94. return gameplay::Keyboard::KEY_NUM_LOCK;
  95. case VK_ADD:
  96. return gameplay::Keyboard::KEY_KP_PLUS;
  97. case VK_SUBTRACT:
  98. return gameplay::Keyboard::KEY_KP_MINUS;
  99. case VK_MULTIPLY:
  100. return gameplay::Keyboard::KEY_KP_MULTIPLY;
  101. case VK_DIVIDE:
  102. return gameplay::Keyboard::KEY_KP_DIVIDE;
  103. case VK_NUMPAD7:
  104. return gameplay::Keyboard::KEY_KP_HOME;
  105. case VK_NUMPAD8:
  106. return gameplay::Keyboard::KEY_KP_UP;
  107. case VK_NUMPAD9:
  108. return gameplay::Keyboard::KEY_KP_PG_UP;
  109. case VK_NUMPAD4:
  110. return gameplay::Keyboard::KEY_KP_LEFT;
  111. case VK_NUMPAD5:
  112. return gameplay::Keyboard::KEY_KP_FIVE;
  113. case VK_NUMPAD6:
  114. return gameplay::Keyboard::KEY_KP_RIGHT;
  115. case VK_NUMPAD1:
  116. return gameplay::Keyboard::KEY_KP_END;
  117. case VK_NUMPAD2:
  118. return gameplay::Keyboard::KEY_KP_DOWN;
  119. case VK_NUMPAD3:
  120. return gameplay::Keyboard::KEY_KP_PG_DOWN;
  121. case VK_NUMPAD0:
  122. return gameplay::Keyboard::KEY_KP_INSERT;
  123. case VK_DECIMAL:
  124. return gameplay::Keyboard::KEY_KP_DELETE;
  125. case VK_F1:
  126. return gameplay::Keyboard::KEY_F1;
  127. case VK_F2:
  128. return gameplay::Keyboard::KEY_F2;
  129. case VK_F3:
  130. return gameplay::Keyboard::KEY_F3;
  131. case VK_F4:
  132. return gameplay::Keyboard::KEY_F4;
  133. case VK_F5:
  134. return gameplay::Keyboard::KEY_F5;
  135. case VK_F6:
  136. return gameplay::Keyboard::KEY_F6;
  137. case VK_F7:
  138. return gameplay::Keyboard::KEY_F7;
  139. case VK_F8:
  140. return gameplay::Keyboard::KEY_F8;
  141. case VK_F9:
  142. return gameplay::Keyboard::KEY_F9;
  143. case VK_F10:
  144. return gameplay::Keyboard::KEY_F10;
  145. case VK_F11:
  146. return gameplay::Keyboard::KEY_F11;
  147. case VK_F12:
  148. return gameplay::Keyboard::KEY_F12;
  149. case VK_SPACE:
  150. return gameplay::Keyboard::KEY_SPACE;
  151. case 0x30:
  152. return shiftDown ? gameplay::Keyboard::KEY_RIGHT_PARENTHESIS : gameplay::Keyboard::KEY_ZERO;
  153. case 0x31:
  154. return shiftDown ? gameplay::Keyboard::KEY_EXCLAM : gameplay::Keyboard::KEY_ONE;
  155. case 0x32:
  156. return shiftDown ? gameplay::Keyboard::KEY_AT : gameplay::Keyboard::KEY_TWO;
  157. case 0x33:
  158. return shiftDown ? gameplay::Keyboard::KEY_NUMBER : gameplay::Keyboard::KEY_THREE;
  159. case 0x34:
  160. return shiftDown ? gameplay::Keyboard::KEY_DOLLAR : gameplay::Keyboard::KEY_FOUR;
  161. case 0x35:
  162. return shiftDown ? gameplay::Keyboard::KEY_PERCENT : gameplay::Keyboard::KEY_FIVE;
  163. case 0x36:
  164. return shiftDown ? gameplay::Keyboard::KEY_CIRCUMFLEX : gameplay::Keyboard::KEY_SIX;
  165. case 0x37:
  166. return shiftDown ? gameplay::Keyboard::KEY_AMPERSAND : gameplay::Keyboard::KEY_SEVEN;
  167. case 0x38:
  168. return shiftDown ? gameplay::Keyboard::KEY_ASTERISK : gameplay::Keyboard::KEY_EIGHT;
  169. case 0x39:
  170. return shiftDown ? gameplay::Keyboard::KEY_LEFT_PARENTHESIS : gameplay::Keyboard::KEY_NINE;
  171. case VK_OEM_PLUS:
  172. return shiftDown ? gameplay::Keyboard::KEY_EQUAL : gameplay::Keyboard::KEY_PLUS;
  173. case VK_OEM_COMMA:
  174. return shiftDown ? gameplay::Keyboard::KEY_LESS_THAN : gameplay::Keyboard::KEY_COMMA;
  175. case VK_OEM_MINUS:
  176. return shiftDown ? gameplay::Keyboard::KEY_UNDERSCORE : gameplay::Keyboard::KEY_MINUS;
  177. case VK_OEM_PERIOD:
  178. return shiftDown ? gameplay::Keyboard::KEY_GREATER_THAN : gameplay::Keyboard::KEY_PERIOD;
  179. case VK_OEM_1:
  180. return shiftDown ? gameplay::Keyboard::KEY_COLON : gameplay::Keyboard::KEY_SEMICOLON;
  181. case VK_OEM_2:
  182. return shiftDown ? gameplay::Keyboard::KEY_QUESTION : gameplay::Keyboard::KEY_SLASH;
  183. case VK_OEM_3:
  184. return shiftDown ? gameplay::Keyboard::KEY_TILDE : gameplay::Keyboard::KEY_GRAVE;
  185. case VK_OEM_4:
  186. return shiftDown ? gameplay::Keyboard::KEY_LEFT_BRACE : gameplay::Keyboard::KEY_LEFT_BRACKET;
  187. case VK_OEM_5:
  188. return shiftDown ? gameplay::Keyboard::KEY_BAR : gameplay::Keyboard::KEY_BACK_SLASH;
  189. case VK_OEM_6:
  190. return shiftDown ? gameplay::Keyboard::KEY_RIGHT_BRACE : gameplay::Keyboard::KEY_RIGHT_BRACKET;
  191. case VK_OEM_7:
  192. return shiftDown ? gameplay::Keyboard::KEY_QUOTE : gameplay::Keyboard::KEY_APOSTROPHE;
  193. case 0x41:
  194. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_A : gameplay::Keyboard::KEY_A;
  195. case 0x42:
  196. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_B : gameplay::Keyboard::KEY_B;
  197. case 0x43:
  198. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_C : gameplay::Keyboard::KEY_C;
  199. case 0x44:
  200. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_D : gameplay::Keyboard::KEY_D;
  201. case 0x45:
  202. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_E : gameplay::Keyboard::KEY_E;
  203. case 0x46:
  204. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_F : gameplay::Keyboard::KEY_F;
  205. case 0x47:
  206. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_G : gameplay::Keyboard::KEY_G;
  207. case 0x48:
  208. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_H : gameplay::Keyboard::KEY_H;
  209. case 0x49:
  210. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_I : gameplay::Keyboard::KEY_I;
  211. case 0x4A:
  212. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_J : gameplay::Keyboard::KEY_J;
  213. case 0x4B:
  214. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_K : gameplay::Keyboard::KEY_K;
  215. case 0x4C:
  216. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_L : gameplay::Keyboard::KEY_L;
  217. case 0x4D:
  218. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_M : gameplay::Keyboard::KEY_M;
  219. case 0x4E:
  220. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_N : gameplay::Keyboard::KEY_N;
  221. case 0x4F:
  222. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_O : gameplay::Keyboard::KEY_O;
  223. case 0x50:
  224. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_P : gameplay::Keyboard::KEY_P;
  225. case 0x51:
  226. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_Q : gameplay::Keyboard::KEY_Q;
  227. case 0x52:
  228. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_R : gameplay::Keyboard::KEY_R;
  229. case 0x53:
  230. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_S : gameplay::Keyboard::KEY_S;
  231. case 0x54:
  232. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_T : gameplay::Keyboard::KEY_T;
  233. case 0x55:
  234. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_U : gameplay::Keyboard::KEY_U;
  235. case 0x56:
  236. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_V : gameplay::Keyboard::KEY_V;
  237. case 0x57:
  238. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_W : gameplay::Keyboard::KEY_W;
  239. case 0x58:
  240. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_X : gameplay::Keyboard::KEY_X;
  241. case 0x59:
  242. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_Y : gameplay::Keyboard::KEY_Y;
  243. case 0x5A:
  244. return shiftDown ? gameplay::Keyboard::KEY_CAPITAL_Z : gameplay::Keyboard::KEY_Z;
  245. default:
  246. return gameplay::Keyboard::KEY_NONE;
  247. }
  248. }
  249. void UpdateCapture(LPARAM lParam)
  250. {
  251. if ((lParam & MK_LBUTTON) || (lParam & MK_MBUTTON) || (lParam & MK_RBUTTON))
  252. SetCapture(__hwnd);
  253. else
  254. ReleaseCapture();
  255. }
  256. void WarpMouse(int clientX, int clientY)
  257. {
  258. POINT p = { clientX, clientY };
  259. ClientToScreen(__hwnd, &p);
  260. SetCursorPos(p.x, p.y);
  261. }
  262. LRESULT CALLBACK __WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  263. {
  264. static gameplay::Game* game = gameplay::Game::getInstance();
  265. if (!game->isInitialized() || hwnd != __hwnd)
  266. {
  267. // Ignore messages that are not for our game window.
  268. return DefWindowProc(hwnd, msg, wParam, lParam);
  269. }
  270. // Scale factors for the mouse movement used to simulate the accelerometer.
  271. GP_ASSERT(__width);
  272. GP_ASSERT(__height);
  273. static const float ACCELEROMETER_X_FACTOR = 90.0f / __width;
  274. static const float ACCELEROMETER_Y_FACTOR = 90.0f / __height;
  275. static int lx, ly;
  276. static bool shiftDown = false;
  277. static bool capsOn = false;
  278. switch (msg)
  279. {
  280. case WM_CLOSE:
  281. DestroyWindow(__hwnd);
  282. return 0;
  283. case WM_DESTROY:
  284. PostQuitMessage(0);
  285. return 0;
  286. case WM_LBUTTONDOWN:
  287. {
  288. int x = GET_X_LPARAM(lParam);
  289. int y = GET_Y_LPARAM(lParam);
  290. UpdateCapture(wParam);
  291. if (!gameplay::Platform::mouseEventInternal(gameplay::Mouse::MOUSE_PRESS_LEFT_BUTTON, x, y, 0))
  292. {
  293. gameplay::Platform::touchEventInternal(gameplay::Touch::TOUCH_PRESS, x, y, 0);
  294. }
  295. return 0;
  296. }
  297. case WM_LBUTTONUP:
  298. {
  299. int x = GET_X_LPARAM(lParam);
  300. int y = GET_Y_LPARAM(lParam);
  301. if (!gameplay::Platform::mouseEventInternal(gameplay::Mouse::MOUSE_RELEASE_LEFT_BUTTON, x, y, 0))
  302. {
  303. gameplay::Platform::touchEventInternal(gameplay::Touch::TOUCH_RELEASE, x, y, 0);
  304. }
  305. UpdateCapture(wParam);
  306. return 0;
  307. }
  308. case WM_RBUTTONDOWN:
  309. UpdateCapture(wParam);
  310. lx = GET_X_LPARAM(lParam);
  311. ly = GET_Y_LPARAM(lParam);
  312. gameplay::Platform::mouseEventInternal(gameplay::Mouse::MOUSE_PRESS_RIGHT_BUTTON, lx, ly, 0);
  313. break;
  314. case WM_RBUTTONUP:
  315. gameplay::Platform::mouseEventInternal(gameplay::Mouse::MOUSE_RELEASE_RIGHT_BUTTON, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), 0);
  316. UpdateCapture(wParam);
  317. break;
  318. case WM_MBUTTONDOWN:
  319. UpdateCapture(wParam);
  320. gameplay::Platform::mouseEventInternal(gameplay::Mouse::MOUSE_PRESS_MIDDLE_BUTTON, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), 0);
  321. break;
  322. case WM_MBUTTONUP:
  323. gameplay::Platform::mouseEventInternal(gameplay::Mouse::MOUSE_RELEASE_MIDDLE_BUTTON, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), 0);
  324. UpdateCapture(wParam);
  325. break;
  326. case WM_MOUSEMOVE:
  327. {
  328. int x = GET_X_LPARAM(lParam);
  329. int y = GET_Y_LPARAM(lParam);
  330. if (__mouseCaptured)
  331. {
  332. // If the incoming position is the mouse capture point, ignore this event
  333. // since this is the event that warped the cursor back.
  334. if (x == __mouseCapturePoint.x && y == __mouseCapturePoint.y)
  335. break;
  336. // Convert to deltas
  337. x -= __mouseCapturePoint.x;
  338. y -= __mouseCapturePoint.y;
  339. // Warp mouse back to center of screen.
  340. WarpMouse(__mouseCapturePoint.x, __mouseCapturePoint.y);
  341. }
  342. // Allow Game::mouseEvent a chance to handle (and possibly consume) the event.
  343. if (!gameplay::Platform::mouseEventInternal(gameplay::Mouse::MOUSE_MOVE, x, y, 0))
  344. {
  345. if ((wParam & MK_LBUTTON) == MK_LBUTTON)
  346. {
  347. // Mouse move events should be interpreted as touch move only if left mouse is held and the game did not consume the mouse event.
  348. gameplay::Platform::touchEventInternal(gameplay::Touch::TOUCH_MOVE, x, y, 0);
  349. return 0;
  350. }
  351. else if ((wParam & MK_RBUTTON) == MK_RBUTTON)
  352. {
  353. // Update the pitch and roll by adding the scaled deltas.
  354. __roll += (float)(x - lx) * ACCELEROMETER_X_FACTOR;
  355. __pitch += -(float)(y - ly) * ACCELEROMETER_Y_FACTOR;
  356. // Clamp the values to the valid range.
  357. __roll = max(min(__roll, 90.0f), -90.0f);
  358. __pitch = max(min(__pitch, 90.0f), -90.0f);
  359. // Update the last X/Y values.
  360. lx = x;
  361. ly = y;
  362. }
  363. }
  364. break;
  365. }
  366. case WM_MOUSEWHEEL:
  367. tagPOINT point;
  368. point.x = GET_X_LPARAM(lParam);
  369. point.y = GET_Y_LPARAM(lParam);
  370. ScreenToClient(__hwnd, &point);
  371. gameplay::Platform::mouseEventInternal(gameplay::Mouse::MOUSE_WHEEL, point.x, point.y, GET_WHEEL_DELTA_WPARAM(wParam) / 120);
  372. break;
  373. case WM_KEYDOWN:
  374. if (wParam == VK_SHIFT || wParam == VK_LSHIFT || wParam == VK_RSHIFT)
  375. shiftDown = true;
  376. if (wParam == VK_CAPITAL)
  377. capsOn = !capsOn;
  378. // Suppress key repeats.
  379. if ((lParam & 0x40000000) == 0)
  380. gameplay::Platform::keyEventInternal(gameplay::Keyboard::KEY_PRESS, getKey(wParam, shiftDown ^ capsOn));
  381. break;
  382. case WM_KEYUP:
  383. if (wParam == VK_SHIFT || wParam == VK_LSHIFT || wParam == VK_RSHIFT)
  384. shiftDown = false;
  385. gameplay::Platform::keyEventInternal(gameplay::Keyboard::KEY_RELEASE, getKey(wParam, shiftDown ^ capsOn));
  386. break;
  387. case WM_CHAR:
  388. // Suppress key repeats.
  389. if ((lParam & 0x40000000) == 0)
  390. gameplay::Platform::keyEventInternal(gameplay::Keyboard::KEY_CHAR, wParam);
  391. break;
  392. case WM_UNICHAR:
  393. // Suppress key repeats.
  394. if ((lParam & 0x40000000) == 0)
  395. gameplay::Platform::keyEventInternal(gameplay::Keyboard::KEY_CHAR, wParam);
  396. break;
  397. case WM_SETFOCUS:
  398. break;
  399. case WM_KILLFOCUS:
  400. break;
  401. }
  402. return DefWindowProc(hwnd, msg, wParam, lParam);
  403. }
  404. namespace gameplay
  405. {
  406. extern void printError(const char* format, ...)
  407. {
  408. va_list argptr;
  409. va_start(argptr, format);
  410. int sz = vfprintf(stderr, format, argptr);
  411. if (sz > 0)
  412. {
  413. char* buf = new char[sz + 1];
  414. vsprintf(buf, format, argptr);
  415. buf[sz] = 0;
  416. OutputDebugStringA(buf);
  417. SAFE_DELETE_ARRAY(buf);
  418. }
  419. va_end(argptr);
  420. }
  421. Platform::Platform(Game* game)
  422. : _game(game)
  423. {
  424. }
  425. Platform::~Platform()
  426. {
  427. if (__hwnd)
  428. {
  429. DestroyWindow(__hwnd);
  430. __hwnd = 0;
  431. }
  432. }
  433. bool initializeGL()
  434. {
  435. // Choose pixel format. 32-bit. RGBA.
  436. PIXELFORMATDESCRIPTOR pfd;
  437. memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
  438. pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
  439. pfd.nVersion = 1;
  440. pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
  441. pfd.iPixelType = PFD_TYPE_RGBA;
  442. pfd.cColorBits = 32;
  443. pfd.cDepthBits = 32;
  444. pfd.iLayerType = PFD_MAIN_PLANE;
  445. int pixelFormat = ChoosePixelFormat(__hdc, &pfd);
  446. if (pixelFormat == 0)
  447. {
  448. GP_ERROR("Failed to choose a pixel format.");
  449. return false;
  450. }
  451. if (!SetPixelFormat (__hdc, pixelFormat, &pfd))
  452. {
  453. GP_ERROR("Failed to set the pixel format.");
  454. return false;
  455. }
  456. HGLRC tempContext = wglCreateContext(__hdc);
  457. wglMakeCurrent(__hdc, tempContext);
  458. if (GLEW_OK != glewInit())
  459. {
  460. GP_ERROR("Failed to initialize GLEW.");
  461. return false;
  462. }
  463. int attribs[] =
  464. {
  465. WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
  466. WGL_CONTEXT_MINOR_VERSION_ARB, 1,
  467. 0
  468. };
  469. if (!(__hrc = wglCreateContextAttribsARB(__hdc, 0, attribs) ) )
  470. {
  471. wglDeleteContext(tempContext);
  472. GP_ERROR("Failed to create OpenGL context.");
  473. return false;
  474. }
  475. wglDeleteContext(tempContext);
  476. if (!wglMakeCurrent(__hdc, __hrc) || !__hrc)
  477. {
  478. GP_ERROR("Failed to make the window current.");
  479. return false;
  480. }
  481. // Vertical sync.
  482. wglSwapIntervalEXT(__vsync ? 1 : 0);
  483. return true;
  484. }
  485. Platform* Platform::create(Game* game, void* attachToWindow)
  486. {
  487. GP_ASSERT(game);
  488. FileSystem::setResourcePath("./");
  489. Platform* platform = new Platform(game);
  490. // Get the application module handle.
  491. __hinstance = ::GetModuleHandle(NULL);
  492. __attachToWindow = (HWND)attachToWindow;
  493. if (!__attachToWindow)
  494. {
  495. LPCTSTR windowClass = L"gameplay";
  496. std::wstring windowName;
  497. bool fullscreen = false;
  498. // Read window settings from config.
  499. if (game->getConfig())
  500. {
  501. Properties* config = game->getConfig()->getNamespace("window", true);
  502. if (config)
  503. {
  504. // Read window title.
  505. const char* title = config->getString("title");
  506. if (title)
  507. {
  508. int len = MultiByteToWideChar(CP_ACP, 0, title, -1, NULL, 0);
  509. wchar_t* wtitle = new wchar_t[len];
  510. MultiByteToWideChar(CP_ACP, 0, title, -1, wtitle, len);
  511. windowName = wtitle;
  512. SAFE_DELETE_ARRAY(wtitle);
  513. }
  514. // Read window size.
  515. int width = config->getInt("width");
  516. if (width != 0)
  517. __width = width;
  518. int height = config->getInt("height");
  519. if (height != 0)
  520. __height = height;
  521. // Read fullscreen state.
  522. fullscreen = config->getBool("fullscreen");
  523. }
  524. }
  525. RECT rect = { 0, 0, __width, __height };
  526. // Register our window class.
  527. WNDCLASSEX wc;
  528. wc.cbSize = sizeof(WNDCLASSEX);
  529. wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  530. wc.lpfnWndProc = (WNDPROC)__WndProc;
  531. wc.cbClsExtra = 0;
  532. wc.cbWndExtra = 0;
  533. wc.hInstance = __hinstance;
  534. wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  535. wc.hIconSm = NULL;
  536. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  537. wc.hbrBackground = NULL; // No brush - we are going to paint our own background
  538. wc.lpszMenuName = NULL; // No default menu
  539. wc.lpszClassName = windowClass;
  540. if (!::RegisterClassEx(&wc))
  541. {
  542. GP_ERROR("Failed to register window class.");
  543. goto error;
  544. }
  545. if (fullscreen)
  546. {
  547. DEVMODE dm;
  548. memset(&dm, 0, sizeof(dm));
  549. dm.dmSize= sizeof(dm);
  550. dm.dmPelsWidth = __width;
  551. dm.dmPelsHeight = __height;
  552. dm.dmBitsPerPel = 32;
  553. dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  554. // Try to set selected mode and get results. NOTE: CDS_FULLSCREEN gets rid of start bar.
  555. if (ChangeDisplaySettings(&dm, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
  556. {
  557. fullscreen = false;
  558. GP_ERROR("Failed to start game in full-screen mode with resolution %dx%d.", __width, __height);
  559. goto error;
  560. }
  561. }
  562. // Set the window style.
  563. DWORD style = (fullscreen ? WS_POPUP : WS_POPUP | WS_BORDER | WS_CAPTION | WS_SYSMENU) | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
  564. DWORD styleEx = (fullscreen ? WS_EX_APPWINDOW : WS_EX_APPWINDOW | WS_EX_WINDOWEDGE);
  565. // Adjust the window rectangle so the client size is the requested size.
  566. AdjustWindowRectEx(&rect, style, FALSE, styleEx);
  567. // Create the native Windows window.
  568. __hwnd = CreateWindowEx(styleEx, windowClass, windowName.c_str(), style, 0, 0, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, __hinstance, NULL);
  569. // Get the drawing context.
  570. __hdc = GetDC(__hwnd);
  571. // Center the window
  572. const int screenX = (GetSystemMetrics(SM_CXSCREEN) - __width) / 2;
  573. const int screenY = (GetSystemMetrics(SM_CYSCREEN) - __height) / 2;
  574. ::SetWindowPos(__hwnd, __hwnd, screenX, screenY, -1, -1, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
  575. if (!initializeGL())
  576. goto error;
  577. // Show the window.
  578. ShowWindow(__hwnd, SW_SHOW);
  579. }
  580. else
  581. {
  582. // Attach to a previous windows
  583. __hwnd = (HWND)__attachToWindow;
  584. __hdc = GetDC(__hwnd);
  585. SetWindowLongPtr(__hwnd, GWL_WNDPROC, (LONG)(WNDPROC)__WndProc);
  586. if (!initializeGL())
  587. goto error;
  588. }
  589. return platform;
  590. error:
  591. exit(0);
  592. return NULL;
  593. }
  594. int Platform::enterMessagePump()
  595. {
  596. GP_ASSERT(_game);
  597. int rc = 0;
  598. // Get the initial time.
  599. LARGE_INTEGER tps;
  600. QueryPerformanceFrequency(&tps);
  601. __timeTicksPerMillis = (double)(tps.QuadPart / 1000L);
  602. LARGE_INTEGER queryTime;
  603. QueryPerformanceCounter(&queryTime);
  604. GP_ASSERT(__timeTicksPerMillis);
  605. __timeStart = queryTime.QuadPart / __timeTicksPerMillis;
  606. // Set the initial pitch and roll values.
  607. __pitch = 0.0;
  608. __roll = 0.0;
  609. SwapBuffers(__hdc);
  610. if (_game->getState() != Game::RUNNING)
  611. _game->run();
  612. if (__attachToWindow)
  613. return 0;
  614. // Enter event dispatch loop.
  615. MSG msg;
  616. while (true)
  617. {
  618. if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  619. {
  620. TranslateMessage(&msg);
  621. DispatchMessage(&msg);
  622. if (msg.message == WM_QUIT)
  623. {
  624. _game->exit();
  625. break;
  626. }
  627. }
  628. else
  629. {
  630. _game->frame();
  631. SwapBuffers(__hdc);
  632. }
  633. // If we are done, then exit.
  634. if (_game->getState() == Game::UNINITIALIZED)
  635. break;
  636. }
  637. return msg.wParam;
  638. }
  639. void Platform::signalShutdown()
  640. {
  641. // nothing to do
  642. }
  643. unsigned int Platform::getDisplayWidth()
  644. {
  645. return __width;
  646. }
  647. unsigned int Platform::getDisplayHeight()
  648. {
  649. return __height;
  650. }
  651. double Platform::getAbsoluteTime()
  652. {
  653. LARGE_INTEGER queryTime;
  654. QueryPerformanceCounter(&queryTime);
  655. GP_ASSERT(__timeTicksPerMillis);
  656. __timeAbsolute = queryTime.QuadPart / __timeTicksPerMillis;
  657. return __timeAbsolute;
  658. }
  659. void Platform::setAbsoluteTime(double time)
  660. {
  661. __timeAbsolute = time;
  662. }
  663. bool Platform::isVsync()
  664. {
  665. return __vsync;
  666. }
  667. void Platform::setVsync(bool enable)
  668. {
  669. wglSwapIntervalEXT(enable ? 1 : 0);
  670. __vsync = enable;
  671. }
  672. void Platform::setMultiTouch(bool enabled)
  673. {
  674. // not supported
  675. }
  676. bool Platform::isMultiTouch()
  677. {
  678. return false;
  679. }
  680. void Platform::getAccelerometerValues(float* pitch, float* roll)
  681. {
  682. GP_ASSERT(pitch);
  683. GP_ASSERT(roll);
  684. *pitch = __pitch;
  685. *roll = __roll;
  686. }
  687. bool Platform::hasMouse()
  688. {
  689. return true;
  690. }
  691. void Platform::setMouseCaptured(bool captured)
  692. {
  693. if (captured != __mouseCaptured)
  694. {
  695. if (captured)
  696. {
  697. // Hide the cursor and warp it to the center of the screen
  698. __mouseCapturePoint.x = getDisplayWidth() / 2;
  699. __mouseCapturePoint.y = getDisplayHeight() / 2;
  700. ShowCursor(FALSE);
  701. WarpMouse(__mouseCapturePoint.x, __mouseCapturePoint.y);
  702. }
  703. else
  704. {
  705. // Restore cursor
  706. WarpMouse(__mouseCapturePoint.x, __mouseCapturePoint.y);
  707. ShowCursor(TRUE);
  708. }
  709. __mouseCaptured = captured;
  710. }
  711. }
  712. bool Platform::isMouseCaptured()
  713. {
  714. return __mouseCaptured;
  715. }
  716. void Platform::setCursorVisible(bool visible)
  717. {
  718. if (visible != __cursorVisible)
  719. {
  720. ShowCursor(visible ? TRUE : FALSE);
  721. __cursorVisible = visible;
  722. }
  723. }
  724. bool Platform::isCursorVisible()
  725. {
  726. return __cursorVisible;
  727. }
  728. void Platform::swapBuffers()
  729. {
  730. if (__hdc)
  731. SwapBuffers(__hdc);
  732. }
  733. void Platform::displayKeyboard(bool display)
  734. {
  735. // Do nothing.
  736. }
  737. void Platform::touchEventInternal(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  738. {
  739. if (!Form::touchEventInternal(evt, x, y, contactIndex))
  740. {
  741. Game::getInstance()->touchEvent(evt, x, y, contactIndex);
  742. Game::getInstance()->getScriptController()->touchEvent(evt, x, y, contactIndex);
  743. }
  744. }
  745. void Platform::keyEventInternal(Keyboard::KeyEvent evt, int key)
  746. {
  747. if (!Form::keyEventInternal(evt, key))
  748. {
  749. Game::getInstance()->keyEvent(evt, key);
  750. Game::getInstance()->getScriptController()->keyEvent(evt, key);
  751. }
  752. }
  753. bool Platform::mouseEventInternal(Mouse::MouseEvent evt, int x, int y, int wheelDelta)
  754. {
  755. if (Form::mouseEventInternal(evt, x, y, wheelDelta))
  756. {
  757. return true;
  758. }
  759. else if (Game::getInstance()->mouseEvent(evt, x, y, wheelDelta))
  760. {
  761. return true;
  762. }
  763. else
  764. {
  765. return Game::getInstance()->getScriptController()->mouseEvent(evt, x, y, wheelDelta);
  766. }
  767. }
  768. void Platform::sleep(long ms)
  769. {
  770. Sleep(ms);
  771. }
  772. }
  773. #endif