PolyWinCore.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "PolyWinCore.h"
  20. #include "PolyGLHeaders.h"
  21. #include "PolyCoreInput.h"
  22. #include "PolyCoreServices.h"
  23. #include "PolyInputEvent.h"
  24. #include "PolyGLRenderer.h"
  25. #include "PolyGLSLShaderModule.h"
  26. #include "PolyLogger.h"
  27. #include "PolyThreaded.h"
  28. #include <GL/gl.h>
  29. #include <GL/glext.h>
  30. #ifndef _MINGW
  31. #include <GL/wglext.h>
  32. #endif
  33. PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT = NULL;
  34. PFNWGLGETSWAPINTERVALEXTPROC wglGetSwapIntervalEXT = NULL;
  35. using namespace Polycode;
  36. long getThreadID() {
  37. return 0;
  38. }
  39. extern Win32Core *core;
  40. void ClientResize(HWND hWnd, int nWidth, int nHeight)
  41. {
  42. RECT rcClient, rcWindow;
  43. POINT ptDiff;
  44. GetClientRect(hWnd, &rcClient);
  45. GetWindowRect(hWnd, &rcWindow);
  46. ptDiff.x = (rcWindow.right - rcWindow.left) - rcClient.right;
  47. ptDiff.y = (rcWindow.bottom - rcWindow.top) - rcClient.bottom;
  48. MoveWindow(hWnd,rcWindow.left, rcWindow.top, nWidth + ptDiff.x, nHeight + ptDiff.y, TRUE);
  49. }
  50. void Core::getScreenInfo(int *width, int *height, int *hz) {
  51. DEVMODE mode = {}; // Zero initialize
  52. mode.dmSize = sizeof(DEVMODE);
  53. EnumDisplaySettings(0, ENUM_CURRENT_SETTINGS, &mode);
  54. // Store the current display settings.
  55. if (width) *width = mode.dmPelsWidth;
  56. if (height) *height = mode.dmPelsHeight;
  57. if (hz) *hz = mode.dmDisplayFrequency;
  58. }
  59. Win32Core::Win32Core(PolycodeViewBase *view, int _xRes, int _yRes, bool fullScreen, bool vSync, int aaLevel, int anisotropyLevel, int frameRate, int monitorIndex)
  60. : Core(_xRes, _yRes, fullScreen, vSync, aaLevel, anisotropyLevel, frameRate, monitorIndex) {
  61. hWnd = *((HWND*)view->windowData);
  62. core = this;
  63. initKeymap();
  64. initGamepad();
  65. initTouch();
  66. hDC = NULL;
  67. hRC = NULL;
  68. PixelFormat = 0;
  69. lastMouseX = -1;
  70. lastMouseY = -1;
  71. eventMutex = createMutex();
  72. isFullScreen = fullScreen;
  73. renderer = new OpenGLRenderer();
  74. services->setRenderer(renderer);
  75. setVideoMode(xRes, yRes, fullScreen, vSync, aaLevel, anisotropyLevel);
  76. WSADATA WsaData;
  77. if(WSAStartup( MAKEWORD(2,2), &WsaData ) != NO_ERROR ){
  78. Logger::log("Error initializing sockets!\n");
  79. }
  80. ((OpenGLRenderer*)renderer)->initOSSpecific();
  81. wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC) wglGetProcAddress("wglSwapIntervalEXT");
  82. wglGetSwapIntervalEXT = (PFNWGLGETSWAPINTERVALEXTPROC) wglGetProcAddress("wglGetSwapIntervalEXT");
  83. setVSync(vSync);
  84. CoreServices::getInstance()->installModule(new GLSLShaderModule());
  85. }
  86. Win32Core::~Win32Core() {
  87. shutdownGamepad();
  88. destroyContext();
  89. }
  90. void Win32Core::enableMouse(bool newval) {
  91. ShowCursor(newval);
  92. }
  93. void Win32Core::warpCursor(int x, int y) {
  94. POINT point;
  95. point.x = x;
  96. point.y = y;
  97. ClientToScreen(hWnd, &point);
  98. SetCursorPos(point.x,point.y);
  99. lastMouseX = x;
  100. lastMouseY = y;
  101. }
  102. unsigned int Win32Core::getTicks() {
  103. return GetTickCount();
  104. }
  105. bool Win32Core::Update() {
  106. if(!running)
  107. return false;
  108. checkEvents();
  109. Gamepad_processEvents();
  110. renderer->BeginRender();
  111. updateCore();
  112. renderer->EndRender();
  113. SwapBuffers(hDC);
  114. doSleep();
  115. return running;
  116. }
  117. void Win32Core::setVSync(bool vSyncVal) {
  118. if(wglSwapIntervalEXT) {
  119. if(vSyncVal) {
  120. wglSwapIntervalEXT(1);
  121. } else {
  122. wglSwapIntervalEXT(0);
  123. }
  124. }
  125. }
  126. void Win32Core::setVideoMode(int xRes, int yRes, bool fullScreen, bool vSync, int aaLevel, int anisotropyLevel) {
  127. if(fullScreen) {
  128. SetWindowLong(hWnd, GWL_STYLE, WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_POPUP);
  129. ShowWindow(hWnd, SW_SHOW);
  130. DEVMODE dmScreenSettings; // Device Mode
  131. memset(&dmScreenSettings,0,sizeof(dmScreenSettings)); // Makes Sure Memory's Cleared
  132. dmScreenSettings.dmSize=sizeof(dmScreenSettings); // Size Of The Devmode Structure
  133. dmScreenSettings.dmPelsWidth = xRes; // Selected Screen Width
  134. dmScreenSettings.dmPelsHeight = yRes; // Selected Screen Height
  135. dmScreenSettings.dmBitsPerPel = 32; // Selected Bits Per Pixel
  136. dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
  137. ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN);
  138. SetWindowPos(hWnd, NULL, 0, 0, xRes, yRes, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
  139. } else {
  140. if(isFullScreen) {
  141. ChangeDisplaySettings(NULL,0);
  142. }
  143. // SetWindowLong(hWnd, GWL_STYLE, WS_OVERLAPPED|WS_SYSMENU);
  144. // ShowWindow(hWnd, SW_SHOW);
  145. ClientResize(hWnd, xRes, yRes);
  146. }
  147. isFullScreen = fullScreen;
  148. initContext(false, 0);
  149. if(aaLevel > 0) {
  150. initMultisample(aaLevel);
  151. }
  152. setVSync(vSync);
  153. renderer->Resize(xRes, yRes);
  154. }
  155. void Win32Core::initContext(bool usePixelFormat, unsigned int pixelFormat) {
  156. destroyContext();
  157. memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR)) ;
  158. pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
  159. pfd.nVersion = 1 ;
  160. pfd.dwFlags = PFD_DOUBLEBUFFER |
  161. PFD_SUPPORT_OPENGL |
  162. PFD_DRAW_TO_WINDOW ;
  163. pfd.iPixelType = PFD_TYPE_RGBA ;
  164. pfd.cColorBits = 24;
  165. pfd.cDepthBits = 16;
  166. pfd.cAccumBlueBits = 8;
  167. pfd.cAccumRedBits = 8;
  168. pfd.cAccumGreenBits = 8;
  169. pfd.cAccumAlphaBits = 8;
  170. pfd.cAccumBits = 24;
  171. pfd.iLayerType = PFD_MAIN_PLANE ;
  172. if (!(hDC=GetDC(hWnd))) // Did We Get A Device Context?
  173. {
  174. Logger::log("Can't Create A GL Device Context.\n");
  175. return; // Return FALSE
  176. }
  177. if(usePixelFormat) {
  178. PixelFormat = pixelFormat;
  179. } else {
  180. if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd))) // Did Windows Find A Matching Pixel Format?
  181. {
  182. Logger::log("Can't Find A Suitable PixelFormat.\n");
  183. return; // Return FALSE
  184. }
  185. }
  186. Logger::log("Setting format: %d\n", PixelFormat);
  187. if(!SetPixelFormat(hDC,PixelFormat,&pfd)) // Are We Able To Set The Pixel Format?
  188. {
  189. Logger::log("Can't Set The PixelFormat: %d.\n", PixelFormat);
  190. return; // Return FALSE
  191. }
  192. if (!(hRC=wglCreateContext(hDC))) // Are We Able To Get A Rendering Context?
  193. {
  194. Logger::log("Can't Create A GL Rendering Context.\n");
  195. return; // Return FALSE
  196. }
  197. if(!wglMakeCurrent(hDC,hRC)) // Try To Activate The Rendering Context
  198. {
  199. Logger::log("Can't Activate The GL Rendering Context.\n");
  200. return; // Return FALSE
  201. }
  202. }
  203. void Win32Core::destroyContext() {
  204. if(hDC == NULL)
  205. return;
  206. wglMakeCurrent (hDC, 0);
  207. wglDeleteContext(hRC);
  208. hRC = 0;
  209. ReleaseDC (hWnd, hDC);
  210. hDC = 0;
  211. if (isFullScreen)
  212. ChangeDisplaySettings (NULL,0);
  213. }
  214. void Win32Core::initMultisample(int numSamples) {
  215. PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB =
  216. (PFNWGLCHOOSEPIXELFORMATARBPROC)wglGetProcAddress("wglChoosePixelFormatARB");
  217. if (!wglChoosePixelFormatARB) {
  218. Logger::log("Multisampling not supported!\n");
  219. return;
  220. }
  221. int pixelFormat;
  222. UINT numFormats;
  223. float fAttributes[] = {0,0};
  224. int iAttributes[] = { WGL_DRAW_TO_WINDOW_ARB,GL_TRUE,
  225. WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
  226. WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB,
  227. WGL_COLOR_BITS_ARB,24,
  228. WGL_DEPTH_BITS_ARB,24,
  229. WGL_DOUBLE_BUFFER_ARB,GL_TRUE,
  230. WGL_ACCUM_GREEN_BITS_ARB, 8,
  231. WGL_ACCUM_RED_BITS_ARB, 8,
  232. WGL_ACCUM_BLUE_BITS_ARB, 8,
  233. WGL_ACCUM_ALPHA_BITS_ARB, 8,
  234. WGL_SAMPLE_BUFFERS_ARB,GL_TRUE,
  235. WGL_SAMPLES_ARB, numSamples ,
  236. 0,0};
  237. if(!wglChoosePixelFormatARB(hDC,iAttributes,fAttributes,1,&pixelFormat,&numFormats)) {
  238. Logger::log("Invalid pixel format chosen\n");
  239. return;
  240. }
  241. // initContext(true, pixelFormat);
  242. glEnable(GL_MULTISAMPLE_ARB);
  243. }
  244. void Win32Core::initKeymap() {
  245. for (int i=0; i<1024; ++i )
  246. keyMap[i] = KEY_UNKNOWN;
  247. keyMap[VK_BACK] = KEY_BACKSPACE;
  248. keyMap[VK_TAB] = KEY_TAB;
  249. keyMap[VK_CLEAR] = KEY_CLEAR;
  250. keyMap[VK_RETURN] = KEY_RETURN;
  251. keyMap[VK_PAUSE] = KEY_PAUSE;
  252. keyMap[VK_ESCAPE] = KEY_ESCAPE;
  253. keyMap[VK_SPACE] = KEY_SPACE;
  254. keyMap[VK_APOSTROPHE] = KEY_QUOTE;
  255. keyMap[VK_COMMA] = KEY_COMMA;
  256. keyMap[VK_MINUS] = KEY_MINUS;
  257. keyMap[VK_PERIOD] = KEY_PERIOD;
  258. keyMap[VK_SLASH] = KEY_SLASH;
  259. keyMap[VK_0] = KEY_0;
  260. keyMap[VK_1] = KEY_1;
  261. keyMap[VK_2] = KEY_2;
  262. keyMap[VK_3] = KEY_3;
  263. keyMap[VK_4] = KEY_4;
  264. keyMap[VK_5] = KEY_5;
  265. keyMap[VK_6] = KEY_6;
  266. keyMap[VK_7] = KEY_7;
  267. keyMap[VK_8] = KEY_8;
  268. keyMap[VK_9] = KEY_9;
  269. keyMap[VK_SEMICOLON] = KEY_SEMICOLON;
  270. keyMap[VK_EQUALS] = KEY_EQUALS;
  271. keyMap[VK_LBRACKET] = KEY_LEFTBRACKET;
  272. keyMap[VK_BACKSLASH] = KEY_BACKSLASH;
  273. keyMap[VK_OEM_102] = KEY_LESS;
  274. keyMap[VK_RBRACKET] = KEY_RIGHTBRACKET;
  275. keyMap[VK_GRAVE] = KEY_BACKQUOTE;
  276. keyMap[VK_BACKTICK] = KEY_BACKQUOTE;
  277. keyMap[VK_A] = KEY_a;
  278. keyMap[VK_B] = KEY_b;
  279. keyMap[VK_C] = KEY_c;
  280. keyMap[VK_D] = KEY_d;
  281. keyMap[VK_E] = KEY_e;
  282. keyMap[VK_F] = KEY_f;
  283. keyMap[VK_G] = KEY_g;
  284. keyMap[VK_H] = KEY_h;
  285. keyMap[VK_I] = KEY_i;
  286. keyMap[VK_J] = KEY_j;
  287. keyMap[VK_K] = KEY_k;
  288. keyMap[VK_L] = KEY_l;
  289. keyMap[VK_M] = KEY_m;
  290. keyMap[VK_N] = KEY_n;
  291. keyMap[VK_O] = KEY_o;
  292. keyMap[VK_P] = KEY_p;
  293. keyMap[VK_Q] = KEY_q;
  294. keyMap[VK_R] = KEY_r;
  295. keyMap[VK_S] = KEY_s;
  296. keyMap[VK_T] = KEY_t;
  297. keyMap[VK_U] = KEY_u;
  298. keyMap[VK_V] = KEY_v;
  299. keyMap[VK_W] = KEY_w;
  300. keyMap[VK_X] = KEY_x;
  301. keyMap[VK_Y] = KEY_y;
  302. keyMap[VK_Z] = KEY_z;
  303. keyMap[VK_DELETE] = KEY_DELETE;
  304. keyMap[VK_NUMPAD0] = KEY_KP0;
  305. keyMap[VK_NUMPAD1] = KEY_KP1;
  306. keyMap[VK_NUMPAD2] = KEY_KP2;
  307. keyMap[VK_NUMPAD3] = KEY_KP3;
  308. keyMap[VK_NUMPAD4] = KEY_KP4;
  309. keyMap[VK_NUMPAD5] = KEY_KP5;
  310. keyMap[VK_NUMPAD6] = KEY_KP6;
  311. keyMap[VK_NUMPAD7] = KEY_KP7;
  312. keyMap[VK_NUMPAD8] = KEY_KP8;
  313. keyMap[VK_NUMPAD9] = KEY_KP9;
  314. keyMap[VK_DECIMAL] = KEY_KP_PERIOD;
  315. keyMap[VK_DIVIDE] = KEY_KP_DIVIDE;
  316. keyMap[VK_MULTIPLY] = KEY_KP_MULTIPLY;
  317. keyMap[VK_SUBTRACT] = KEY_KP_MINUS;
  318. keyMap[VK_ADD] = KEY_KP_PLUS;
  319. keyMap[VK_UP] = KEY_UP;
  320. keyMap[VK_DOWN] = KEY_DOWN;
  321. keyMap[VK_RIGHT] = KEY_RIGHT;
  322. keyMap[VK_LEFT] = KEY_LEFT;
  323. keyMap[VK_INSERT] = KEY_INSERT;
  324. keyMap[VK_HOME] = KEY_HOME;
  325. keyMap[VK_END] = KEY_END;
  326. keyMap[VK_PRIOR] = KEY_PAGEUP;
  327. keyMap[VK_NEXT] = KEY_PAGEDOWN;
  328. keyMap[VK_F1] = KEY_F1;
  329. keyMap[VK_F2] = KEY_F2;
  330. keyMap[VK_F3] = KEY_F3;
  331. keyMap[VK_F4] = KEY_F4;
  332. keyMap[VK_F5] = KEY_F5;
  333. keyMap[VK_F6] = KEY_F6;
  334. keyMap[VK_F7] = KEY_F7;
  335. keyMap[VK_F8] = KEY_F8;
  336. keyMap[VK_F9] = KEY_F9;
  337. keyMap[VK_F10] = KEY_F10;
  338. keyMap[VK_F11] = KEY_F11;
  339. keyMap[VK_F12] = KEY_F12;
  340. keyMap[VK_F13] = KEY_F13;
  341. keyMap[VK_F14] = KEY_F14;
  342. keyMap[VK_F15] = KEY_F15;
  343. keyMap[VK_NUMLOCK] = KEY_NUMLOCK;
  344. keyMap[VK_CAPITAL] = KEY_CAPSLOCK;
  345. keyMap[VK_SCROLL] = KEY_SCROLLOCK;
  346. keyMap[VK_RSHIFT] = KEY_RSHIFT;
  347. keyMap[VK_LSHIFT] = KEY_LSHIFT;
  348. keyMap[VK_RCONTROL] = KEY_RCTRL;
  349. keyMap[VK_LCONTROL] = KEY_LCTRL;
  350. keyMap[VK_RMENU] = KEY_RALT;
  351. keyMap[VK_LMENU] = KEY_LALT;
  352. keyMap[VK_RWIN] = KEY_RSUPER;
  353. keyMap[VK_LWIN] = KEY_LSUPER;
  354. keyMap[VK_HELP] = KEY_HELP;
  355. keyMap[VK_SNAPSHOT] = KEY_PRINT;
  356. keyMap[VK_CANCEL] = KEY_BREAK;
  357. keyMap[VK_APPS] = KEY_MENU;
  358. }
  359. PolyKEY Win32Core::mapKey(LPARAM lParam, WPARAM wParam) {
  360. switch (wParam) {
  361. case VK_CONTROL:
  362. if ( lParam&EXTENDED_KEYMASK )
  363. wParam = VK_RCONTROL;
  364. else
  365. wParam = VK_LCONTROL;
  366. break;
  367. case 33:
  368. if ( lParam&EXTENDED_KEYMASK )
  369. wParam = VK_RMENU;
  370. else
  371. wParam = VK_LMENU;
  372. break;
  373. }
  374. return keyMap[(unsigned int)wParam];
  375. }
  376. void Win32Core::handleKeyDown(LPARAM lParam, WPARAM wParam, wchar_t unicodeChar) {
  377. lockMutex(eventMutex);
  378. Win32Event newEvent;
  379. newEvent.eventGroup = Win32Event::INPUT_EVENT;
  380. newEvent.eventCode = InputEvent::EVENT_KEYDOWN;
  381. newEvent.keyCode = mapKey(lParam, wParam);
  382. newEvent.unicodeChar = unicodeChar;
  383. win32Events.push_back(newEvent);
  384. unlockMutex(eventMutex);
  385. }
  386. void Win32Core::handleKeyUp(LPARAM lParam, WPARAM wParam) {
  387. lockMutex(eventMutex);
  388. Win32Event newEvent;
  389. newEvent.eventGroup = Win32Event::INPUT_EVENT;
  390. newEvent.eventCode = InputEvent::EVENT_KEYUP;
  391. newEvent.keyCode = mapKey(lParam, wParam);
  392. newEvent.unicodeChar = 0;
  393. win32Events.push_back(newEvent);
  394. unlockMutex(eventMutex);
  395. }
  396. #ifndef NO_TOUCH_API
  397. void Win32Core::handleTouchEvent(LPARAM lParam, WPARAM wParam) {
  398. // Bail out now if multitouch is not available on this system
  399. if ( hasMultiTouch == false )
  400. {
  401. return;
  402. }
  403. lockMutex(eventMutex);
  404. int iNumContacts = LOWORD(wParam);
  405. HTOUCHINPUT hInput = (HTOUCHINPUT)lParam;
  406. TOUCHINPUT *pInputs = new TOUCHINPUT[iNumContacts];
  407. if(pInputs != NULL) {
  408. if(GetTouchInputInfoFunc(hInput, iNumContacts, pInputs, sizeof(TOUCHINPUT))) {
  409. std::vector<TouchInfo> touches;
  410. for(int i = 0; i < iNumContacts; i++) {
  411. TOUCHINPUT ti = pInputs[i];
  412. TouchInfo touchInfo;
  413. touchInfo.id = (int) ti.dwID;
  414. POINT pt;
  415. pt.x = TOUCH_COORD_TO_PIXEL(ti.x);
  416. pt.y = TOUCH_COORD_TO_PIXEL(ti.y);
  417. ScreenToClient(hWnd, &pt);
  418. touchInfo.position.x = pt.x;
  419. touchInfo.position.y = pt.y;
  420. touches.push_back(touchInfo);
  421. }
  422. for(int i = 0; i < iNumContacts; i++) {
  423. TOUCHINPUT ti = pInputs[i];
  424. if (ti.dwFlags & TOUCHEVENTF_UP) {
  425. Win32Event newEvent;
  426. newEvent.eventGroup = Win32Event::INPUT_EVENT;
  427. newEvent.eventCode = InputEvent::EVENT_TOUCHES_ENDED;
  428. newEvent.touches = touches;
  429. newEvent.touch = touches[i];
  430. win32Events.push_back(newEvent);
  431. } else if(ti.dwFlags & TOUCHEVENTF_MOVE) {
  432. Win32Event newEvent;
  433. newEvent.eventGroup = Win32Event::INPUT_EVENT;
  434. newEvent.eventCode = InputEvent::EVENT_TOUCHES_MOVED;
  435. newEvent.touches = touches;
  436. newEvent.touch = touches[i];
  437. win32Events.push_back(newEvent);
  438. } else if(ti.dwFlags & TOUCHEVENTF_DOWN) {
  439. Win32Event newEvent;
  440. newEvent.eventGroup = Win32Event::INPUT_EVENT;
  441. newEvent.eventCode = InputEvent::EVENT_TOUCHES_BEGAN;
  442. newEvent.touches = touches;
  443. newEvent.touch = touches[i];
  444. win32Events.push_back(newEvent);
  445. }
  446. }
  447. }
  448. }
  449. unlockMutex(eventMutex);
  450. }
  451. #endif
  452. void Win32Core::handleMouseMove(LPARAM lParam, WPARAM wParam) {
  453. lockMutex(eventMutex);
  454. Win32Event newEvent;
  455. newEvent.eventGroup = Win32Event::INPUT_EVENT;
  456. newEvent.eventCode = InputEvent::EVENT_MOUSEMOVE;
  457. newEvent.mouseX = GET_X_LPARAM(lParam);
  458. newEvent.mouseY = GET_Y_LPARAM(lParam);
  459. win32Events.push_back(newEvent);
  460. unlockMutex(eventMutex);
  461. }
  462. void Win32Core::handleMouseWheel(LPARAM lParam, WPARAM wParam) {
  463. lockMutex(eventMutex);
  464. Win32Event newEvent;
  465. newEvent.eventGroup = Win32Event::INPUT_EVENT;
  466. newEvent.mouseX = GET_X_LPARAM(lParam);
  467. newEvent.mouseY = GET_Y_LPARAM(lParam);
  468. int zDelta = GET_WHEEL_DELTA_WPARAM(wParam);
  469. if(zDelta < 0)
  470. newEvent.eventCode = InputEvent::EVENT_MOUSEWHEEL_DOWN;
  471. else
  472. newEvent.eventCode = InputEvent::EVENT_MOUSEWHEEL_UP;
  473. win32Events.push_back(newEvent);
  474. unlockMutex(eventMutex);
  475. }
  476. void Win32Core::handleMouseDown(int mouseCode,LPARAM lParam, WPARAM wParam) {
  477. lockMutex(eventMutex);
  478. Win32Event newEvent;
  479. newEvent.eventGroup = Win32Event::INPUT_EVENT;
  480. newEvent.mouseX = GET_X_LPARAM(lParam);
  481. newEvent.mouseY = GET_Y_LPARAM(lParam);
  482. newEvent.eventCode = InputEvent::EVENT_MOUSEDOWN;
  483. newEvent.mouseButton = mouseCode;
  484. win32Events.push_back(newEvent);
  485. unlockMutex(eventMutex);
  486. }
  487. void Win32Core::handleMouseUp(int mouseCode,LPARAM lParam, WPARAM wParam) {
  488. lockMutex(eventMutex);
  489. Win32Event newEvent;
  490. newEvent.eventGroup = Win32Event::INPUT_EVENT;
  491. newEvent.mouseX = GET_X_LPARAM(lParam);
  492. newEvent.mouseY = GET_Y_LPARAM(lParam);
  493. newEvent.eventCode = InputEvent::EVENT_MOUSEUP;
  494. newEvent.mouseButton = mouseCode;
  495. win32Events.push_back(newEvent);
  496. unlockMutex(eventMutex);
  497. }
  498. void Win32Core::checkEvents() {
  499. lockMutex(eventMutex);
  500. Win32Event event;
  501. for(int i=0; i < win32Events.size(); i++) {
  502. event = win32Events[i];
  503. switch(event.eventGroup) {
  504. case Win32Event::INPUT_EVENT:
  505. switch(event.eventCode) {
  506. case InputEvent::EVENT_TOUCHES_BEGAN:
  507. input->touchesBegan(event.touch, event.touches, getTicks());
  508. break;
  509. case InputEvent::EVENT_TOUCHES_ENDED:
  510. input->touchesEnded(event.touch, event.touches, getTicks());
  511. break;
  512. case InputEvent::EVENT_TOUCHES_MOVED:
  513. input->touchesMoved(event.touch, event.touches, getTicks());
  514. break;
  515. case InputEvent::EVENT_MOUSEMOVE:
  516. input->setDeltaPosition(event.mouseX - lastMouseX , event.mouseY - lastMouseY);
  517. lastMouseX = event.mouseX;
  518. lastMouseY = event.mouseY;
  519. input->setMousePosition(event.mouseX, event.mouseY, getTicks());
  520. break;
  521. case InputEvent::EVENT_MOUSEDOWN:
  522. input->setMouseButtonState(event.mouseButton, true, getTicks());
  523. break;
  524. case InputEvent::EVENT_MOUSEUP:
  525. input->setMouseButtonState(event.mouseButton, false, getTicks());
  526. break;
  527. case InputEvent::EVENT_KEYDOWN:
  528. input->setKeyState(event.keyCode, (char)event.unicodeChar, true, getTicks());
  529. break;
  530. case InputEvent::EVENT_KEYUP:
  531. input->setKeyState(event.keyCode, (char)event.unicodeChar, false, getTicks());
  532. break;
  533. }
  534. break;
  535. }
  536. }
  537. win32Events.clear();
  538. unlockMutex(eventMutex);
  539. }
  540. void Win32Core::handleAxisChange(GamepadDeviceEntry * device, int axisIndex, DWORD value) {
  541. if (axisIndex < 0 || axisIndex >= (int) device->numAxes) {
  542. return;
  543. }
  544. Gamepad_devicePrivate *devicePrivate = device->privateData;
  545. float floatVal = (value - devicePrivate->axisRanges[axisIndex][0]) / (float) (devicePrivate->axisRanges[axisIndex][1] - devicePrivate->axisRanges[axisIndex][0]) * 2.0f - 1.0f;
  546. input->joystickAxisMoved(axisIndex, floatVal, device->deviceID);
  547. }
  548. void Win32Core::handleButtonChange(GamepadDeviceEntry * device, DWORD lastValue, DWORD value) {
  549. Gamepad_devicePrivate *devicePrivate = device->privateData;
  550. unsigned int buttonIndex;
  551. for (buttonIndex = 0; buttonIndex < device->numButtons; buttonIndex++) {
  552. if ((lastValue ^ value) & (1 << buttonIndex)) {
  553. if(!!(value & (1 << buttonIndex))) {
  554. input->joystickButtonDown(buttonIndex, device->deviceID);
  555. } else {
  556. input->joystickButtonUp(buttonIndex, device->deviceID);
  557. }
  558. }
  559. }
  560. }
  561. static void povToXY(DWORD pov, int * outX, int * outY) {
  562. if (pov == JOY_POVCENTERED) {
  563. *outX = *outY = 0;
  564. } else {
  565. if (pov > JOY_POVFORWARD && pov < JOY_POVBACKWARD) {
  566. *outX = 1;
  567. } else if (pov > JOY_POVBACKWARD) {
  568. *outX = -1;
  569. } else {
  570. *outX = 0;
  571. }
  572. if (pov > JOY_POVLEFT || pov < JOY_POVRIGHT) {
  573. *outY = -1;
  574. } else if (pov > JOY_POVRIGHT && pov < JOY_POVLEFT) {
  575. *outY = 1;
  576. } else {
  577. *outY = 0;
  578. }
  579. }
  580. }
  581. void Win32Core::handlePOVChange(GamepadDeviceEntry * device, DWORD lastValue, DWORD value) {
  582. Gamepad_devicePrivate *devicePrivate = device->privateData;
  583. int lastX, lastY, newX, newY;
  584. if (devicePrivate->povXAxisIndex == -1 || devicePrivate->povYAxisIndex == -1) {
  585. return;
  586. }
  587. povToXY(lastValue, &lastX, &lastY);
  588. povToXY(value, &newX, &newY);
  589. if (newX != lastX) {
  590. input->joystickAxisMoved(devicePrivate->povXAxisIndex, newX, device->deviceID);
  591. }
  592. if (newY != lastY) {
  593. input->joystickAxisMoved(devicePrivate->povYAxisIndex, newY, device->deviceID);
  594. }
  595. }
  596. void Win32Core::Gamepad_processEvents() {
  597. if(getTicks() > lastGamepadDetect + 3000) {
  598. detectGamepads();
  599. }
  600. unsigned int deviceIndex;
  601. JOYINFOEX info;
  602. MMRESULT result;
  603. GamepadDeviceEntry * device;
  604. Gamepad_devicePrivate * devicePrivate;
  605. for (deviceIndex = 0; deviceIndex < gamepads.size(); deviceIndex++) {
  606. device = gamepads[deviceIndex];
  607. devicePrivate = device->privateData;
  608. info.dwSize = sizeof(info);
  609. info.dwFlags = JOY_RETURNALL;
  610. result = joyGetPosEx(devicePrivate->joystickID, &info);
  611. if (result == JOYERR_UNPLUGGED) {
  612. input->removeJoystick(device->deviceID);
  613. gamepads.erase(gamepads.begin() + deviceIndex);
  614. } else if (result == JOYERR_NOERROR) {
  615. if (info.dwXpos != devicePrivate->lastState.dwXpos) {
  616. handleAxisChange(device, devicePrivate->xAxisIndex, info.dwXpos);
  617. }
  618. if (info.dwYpos != devicePrivate->lastState.dwYpos) {
  619. handleAxisChange(device, devicePrivate->yAxisIndex, info.dwYpos);
  620. }
  621. if (info.dwZpos != devicePrivate->lastState.dwZpos) {
  622. handleAxisChange(device, devicePrivate->zAxisIndex, info.dwZpos);
  623. }
  624. if (info.dwRpos != devicePrivate->lastState.dwRpos) {
  625. handleAxisChange(device, devicePrivate->rAxisIndex, info.dwRpos);
  626. }
  627. if (info.dwUpos != devicePrivate->lastState.dwUpos) {
  628. handleAxisChange(device, devicePrivate->uAxisIndex, info.dwUpos);
  629. }
  630. if (info.dwVpos != devicePrivate->lastState.dwVpos) {
  631. handleAxisChange(device, devicePrivate->vAxisIndex, info.dwVpos);
  632. }
  633. if (info.dwPOV != devicePrivate->lastState.dwPOV) {
  634. handlePOVChange(device, devicePrivate->lastState.dwPOV, info.dwPOV);
  635. }
  636. if (info.dwButtons != devicePrivate->lastState.dwButtons) {
  637. handleButtonChange(device, devicePrivate->lastState.dwButtons, info.dwButtons);
  638. }
  639. devicePrivate->lastState = info;
  640. }
  641. }
  642. }
  643. void Win32Core::detectGamepads() {
  644. lastGamepadDetect = getTicks();
  645. unsigned int numPadsSupported;
  646. unsigned int deviceIndex, deviceIndex2;
  647. JOYINFOEX info;
  648. JOYCAPS caps;
  649. bool duplicate;
  650. Gamepad_devicePrivate * deviceRecordPrivate;
  651. UINT joystickID;
  652. int axisIndex;
  653. numPadsSupported = joyGetNumDevs();
  654. for (deviceIndex = 0; deviceIndex < numPadsSupported; deviceIndex++) {
  655. info.dwSize = sizeof(info);
  656. info.dwFlags = JOY_RETURNALL;
  657. joystickID = JOYSTICKID1 + deviceIndex;
  658. if (joyGetPosEx(joystickID, &info) == JOYERR_NOERROR &&
  659. joyGetDevCaps(joystickID, &caps, sizeof(JOYCAPS)) == JOYERR_NOERROR) {
  660. duplicate = false;
  661. for (deviceIndex2 = 0; deviceIndex2 < gamepads.size(); deviceIndex2++) {
  662. if (((Gamepad_devicePrivate *) gamepads[deviceIndex2]->privateData)->joystickID == joystickID) {
  663. duplicate = true;
  664. break;
  665. }
  666. }
  667. if (duplicate) {
  668. continue;
  669. }
  670. GamepadDeviceEntry *deviceRecord = new GamepadDeviceEntry();
  671. deviceRecord->deviceID = nextDeviceID++;
  672. // deviceRecord->description = getDeviceDescription(joystickID, caps);
  673. // deviceRecord->vendorID = caps.wMid;
  674. // deviceRecord->productID = caps.wPid;
  675. deviceRecord->numAxes = caps.wNumAxes + ((caps.wCaps & JOYCAPS_HASPOV) ? 2 : 0);
  676. deviceRecord->numButtons = caps.wNumButtons;
  677. // deviceRecord->axisStates = calloc(sizeof(float), deviceRecord->numAxes);
  678. // deviceRecord->buttonStates = calloc(sizeof(bool), deviceRecord->numButtons);
  679. // deviceRecord->eventDispatcher = EventDispatcher_create(deviceRecord);
  680. // devices = realloc(devices, sizeof(struct Gamepad_device *) * (numDevices + 1));
  681. gamepads.push_back(deviceRecord);
  682. deviceRecordPrivate = new Gamepad_devicePrivate();
  683. deviceRecordPrivate->joystickID = joystickID;
  684. deviceRecordPrivate->lastState = info;
  685. deviceRecordPrivate->xAxisIndex = 0;
  686. deviceRecordPrivate->yAxisIndex = 1;
  687. axisIndex = 2;
  688. deviceRecordPrivate->zAxisIndex = (caps.wCaps & JOYCAPS_HASZ) ? axisIndex++ : -1;
  689. deviceRecordPrivate->rAxisIndex = (caps.wCaps & JOYCAPS_HASR) ? axisIndex++ : -1;
  690. deviceRecordPrivate->uAxisIndex = (caps.wCaps & JOYCAPS_HASU) ? axisIndex++ : -1;
  691. deviceRecordPrivate->vAxisIndex = (caps.wCaps & JOYCAPS_HASV) ? axisIndex++ : -1;
  692. deviceRecordPrivate->axisRanges = (UINT (*)[2]) malloc(sizeof(UINT[2]) * axisIndex);
  693. deviceRecordPrivate->axisRanges[0][0] = caps.wXmin;
  694. deviceRecordPrivate->axisRanges[0][1] = caps.wXmax;
  695. deviceRecordPrivate->axisRanges[1][0] = caps.wYmin;
  696. deviceRecordPrivate->axisRanges[1][1] = caps.wYmax;
  697. if (deviceRecordPrivate->zAxisIndex != -1) {
  698. deviceRecordPrivate->axisRanges[deviceRecordPrivate->zAxisIndex][0] = caps.wZmin;
  699. deviceRecordPrivate->axisRanges[deviceRecordPrivate->zAxisIndex][1] = caps.wZmax;
  700. }
  701. if (deviceRecordPrivate->rAxisIndex != -1) {
  702. deviceRecordPrivate->axisRanges[deviceRecordPrivate->rAxisIndex][0] = caps.wRmin;
  703. deviceRecordPrivate->axisRanges[deviceRecordPrivate->rAxisIndex][1] = caps.wRmax;
  704. }
  705. if (deviceRecordPrivate->uAxisIndex != -1) {
  706. deviceRecordPrivate->axisRanges[deviceRecordPrivate->uAxisIndex][0] = caps.wUmin;
  707. deviceRecordPrivate->axisRanges[deviceRecordPrivate->uAxisIndex][1] = caps.wUmax;
  708. }
  709. if (deviceRecordPrivate->vAxisIndex != -1) {
  710. deviceRecordPrivate->axisRanges[deviceRecordPrivate->vAxisIndex][0] = caps.wVmin;
  711. deviceRecordPrivate->axisRanges[deviceRecordPrivate->vAxisIndex][1] = caps.wVmax;
  712. }
  713. deviceRecordPrivate->povXAxisIndex = (caps.wCaps & JOYCAPS_HASPOV) ? axisIndex++ : -1;
  714. deviceRecordPrivate->povYAxisIndex = (caps.wCaps & JOYCAPS_HASPOV) ? axisIndex++ : -1;
  715. deviceRecord->privateData = deviceRecordPrivate;
  716. input->addJoystick(deviceRecord->deviceID);
  717. }
  718. }
  719. }
  720. void Win32Core::initGamepad() {
  721. nextDeviceID = 0;
  722. detectGamepads();
  723. }
  724. void Win32Core::shutdownGamepad() {
  725. }
  726. void Win32Core::initTouch() {
  727. #ifdef NO_TOUCH_API
  728. hasMultiTouch = false;
  729. #else
  730. // Check for windows multitouch support at runtime
  731. // This could be done easily during preprocessing but would require building
  732. // multiple releases of polycode for both winxp/vista and win7
  733. GetTouchInputInfoFunc = (GetTouchInputInfoType) GetProcAddress(GetModuleHandle(TEXT("user32")), "GetTouchInputInfo");
  734. // If the above multitouch functions were found, then set a flag so we don't
  735. // have to check again later
  736. hasMultiTouch = ( GetTouchInputInfoFunc == NULL ) ? false : true;
  737. if(hasMultiTouch) {
  738. RegisterTouchWindow(hWnd, 0);
  739. }
  740. #endif
  741. }
  742. DWORD WINAPI Win32LaunchThread(LPVOID data) {
  743. Threaded *threaded = (Threaded*)data;
  744. threaded->runThread();
  745. return 1;
  746. }
  747. void Win32Core::createThread(Threaded *target) {
  748. DWORD dwGenericThread;
  749. HANDLE handle = CreateThread(NULL,0,Win32LaunchThread,target,0,&dwGenericThread);
  750. }
  751. void Win32Core::lockMutex(CoreMutex *mutex) {
  752. WaitForSingleObject(((Win32Mutex*)mutex)->winMutex,INFINITE);
  753. }
  754. void Win32Core::unlockMutex(CoreMutex *mutex) {
  755. ReleaseMutex(((Win32Mutex*)mutex)->winMutex);
  756. }
  757. void Win32Core::platformSleep(int msecs) {
  758. Sleep(msecs);
  759. }
  760. CoreMutex *Win32Core::createMutex() {
  761. Win32Mutex *newMutex = new Win32Mutex();
  762. newMutex->winMutex = CreateMutex( NULL, FALSE, NULL);
  763. return newMutex;
  764. }
  765. std::vector<Polycode::Rectangle> Win32Core::getVideoModes() {
  766. std::vector<Polycode::Rectangle> retVector;
  767. return retVector;
  768. }