PolyWinCore.cpp 26 KB

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