PlatformWin32.cpp 23 KB

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