PolyWinCore.cpp 28 KB

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