PlatformWin32.cpp 24 KB

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