Advertisement
Guest User

Untitled

a guest
Jul 30th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.79 KB | None | 0 0
  1. #include "input.h"
  2.  
  3. input::input()
  4. {
  5.  
  6. debug = 1;
  7.  
  8. ininit = 1;
  9. deadzone = 5000;
  10. }
  11.  
  12. bool input::init()
  13. {
  14. SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER);
  15.  
  16. SDL_GameControllerAddMappingsFromFile("gamecontrollerdb.txt");
  17.  
  18. numDevices = SDL_NumJoysticks();
  19. std::cout << std::endl << numDevices <<" controller(s) found" <<std::endl;
  20.  
  21. SDL_JoystickEventState(SDL_ENABLE);
  22. SDL_GameControllerEventState(SDL_ENABLE);
  23.  
  24. for(int i=0; i<numDevices; ++i)
  25. {
  26.  
  27. controller[i].js = SDL_JoystickOpen(i);
  28. if(!controller[i].js) {
  29. std::cout << "failed to open joystick: " << SDL_GetError() << "\n";
  30. return 113;
  31. }
  32.  
  33.  
  34. if(SDL_IsGameController(i)) {
  35. controller[i].gc = SDL_GameControllerOpen(i);
  36. if(!controller[i].gc) {
  37. std::cout << "failed to open game controller: " << SDL_GetError() << "\n";
  38. return 114;
  39. }
  40. }
  41. std:: cout << "Controller: " << i+1 << " on ID:" << SDL_JoystickInstanceID(controller[i].js) << std::endl;
  42.  
  43.  
  44. deviceindex[SDL_JoystickInstanceID(controller[i].js)] = i;
  45. }
  46.  
  47. std::cout << "--------------------------" << std::endl;
  48.  
  49. if (debug == 1)
  50. Details();
  51.  
  52. ininit = 0;
  53. }
  54.  
  55. void input::Details()
  56. {
  57. std::cout << numDevices <<" controllers found" <<std::endl;
  58.  
  59. for(int i=0; i<numDevices; i++)
  60. {
  61.  
  62. char sguid[1024];
  63. SDL_JoystickGUID guid = SDL_JoystickGetGUID(controller[i].js);
  64. SDL_JoystickGetGUIDString(guid, sguid, sizeof(sguid));
  65.  
  66. std::cout << std::endl << "-------------------------------------------------------"<<std::endl;
  67. std::cout << "Controller " << i+1 << "\n";
  68. std::cout << " guid = " << sguid << "\n";
  69. std::cout << " name = '" << SDL_JoystickName(controller[i].js) << "'\n";
  70. std::cout << " axes = " << SDL_JoystickNumAxes(controller[i].js) << "\n";
  71. std::cout << " balls = " << SDL_JoystickNumBalls(controller[i].js) << "\n";
  72. std::cout << " hats = " << SDL_JoystickNumHats(controller[i].js) << "\n";
  73. std::cout << " buttons = " << SDL_JoystickNumButtons(controller[i].js) << "\n";
  74. if(controller[i].gc) {
  75. std::cout << "\n --detected as game controller--\n";
  76. std::cout << " Controller Name = '" << SDL_GameControllerName(controller[i].gc) ;
  77. }
  78. else {
  79. std::cout << "\n --detected as game controller--\n";
  80. std::cout << " Controller Name = '" << SDL_JoystickName(controller[i].js) ;
  81. }
  82. std::cout << std::endl << "-------------------------------------------------------"<<std::endl;
  83. }
  84. }
  85.  
  86. int input::stringtoint(const char* value)
  87. {
  88. std::stringstream convert;
  89. convert << value;
  90.  
  91. unsigned int intValue;
  92. convert >> intValue;
  93.  
  94. return intValue;
  95. }
  96.  
  97. void input::handleEvents(SDL_Event event)
  98. {
  99.  
  100. //while( SDL_PollEvent( &event ))
  101. switch(event.type)
  102. {
  103.  
  104. case SDL_JOYDEVICEADDED: // LOOK INTO: event.cdevice.which
  105. //case SDL_CONTROLLERDEVICEADDED: // the cdevice part may be off, think might only supposed to be during controller events
  106. // not for button or axis events. maybe caxis / cbutton.
  107. {
  108. if (ininit == 1)
  109. break;
  110.  
  111. numDevices = SDL_NumJoysticks();
  112. if (debug == 1)
  113. {
  114. std::cout << "Now there is "<< numDevices << " device(s)." << std::endl;
  115. }
  116. bool yesbreak = 0;
  117. for(int i=0; i<numDevices; ++i)
  118. {
  119. if (controller[i].js == 0)
  120. {
  121. controller[i].gc = SDL_GameControllerOpen(i);
  122. controller[i].js = SDL_JoystickOpen(i);
  123. SDL_Delay(10);
  124. std::cout << "Plugged in AAAcontroller " << (int)event.jdevice.which+1 << " now on ID :" <<SDL_JoystickInstanceID(controller[(int)event.jdevice.which].js)<<std::endl;
  125.  
  126. deviceindex[SDL_JoystickInstanceID(controller[(int)event.jdevice.which].js)] = (int)event.jdevice.which;
  127.  
  128.  
  129. yesbreak = 1;
  130.  
  131. break;
  132. }
  133.  
  134. }
  135. if (yesbreak == 1)
  136. break;
  137.  
  138. controller[numDevices-1].gc = SDL_GameControllerOpen((int)event.cdevice.which);
  139. controller[numDevices-1].js = SDL_JoystickOpen((int)event.cdevice.which);
  140.  
  141. if (debug == 1)
  142. {
  143. std::cout << "Plugged in controller " << (int)event.jdevice.which+1 << " now on ID :" <<SDL_JoystickInstanceID(controller[(int)event.jdevice.which].js)<<std::endl;
  144. }
  145.  
  146. deviceindex[SDL_JoystickInstanceID(controller[(int)event.jdevice.which].js)] = (int)event.jdevice.which;
  147.  
  148. if (SDL_JoystickInstanceID(controller[(int)event.jdevice.which].js) < 0)
  149. std::cout << SDL_GetError() << std::endl;
  150.  
  151. break;
  152.  
  153. }
  154.  
  155.  
  156. case SDL_JOYDEVICEREMOVED:
  157. // case SDL_CONTROLLERDEVICEREMOVED:
  158. {
  159.  
  160.  
  161. SDL_GameControllerClose(controller[deviceindex[(int)event.cdevice.which]].gc);
  162. SDL_JoystickClose(controller[deviceindex[(int)event.cdevice.which]].js);
  163. controller[deviceindex[(int)event.cdevice.which]].gc = NULL;
  164. controller[deviceindex[(int)event.cdevice.which]].js = NULL;
  165.  
  166. numDevices = SDL_NumJoysticks();
  167.  
  168. if (debug == 1)
  169. {
  170. std::cout << "removed controller " << deviceindex[(int)event.cdevice.which]+1 << " on ID "<< (int)event.cdevice.which << std::endl;
  171. std::cout << numDevices << " devices still plugged in." << std::endl;
  172. }
  173. break;
  174. }
  175.  
  176.  
  177. case SDL_CONTROLLERAXISMOTION:
  178. {
  179. const char* axis = SDL_GameControllerGetStringForAxis((SDL_GameControllerAxis)event.caxis.axis);
  180.  
  181.  
  182.  
  183. switch((SDL_GameControllerAxis)event.caxis.axis)
  184. {
  185. case 0:
  186. {
  187. controller[deviceindex[(int)event.cdevice.which]].x1 = (int)event.caxis.value;
  188. break;
  189. }
  190. case 1:
  191. {
  192. controller[deviceindex[(int)event.cdevice.which]].y1 = (int)event.caxis.value;
  193. break;
  194. }
  195. case 2:
  196. {
  197. controller[deviceindex[(int)event.cdevice.which]].x2 = (int)event.caxis.value;
  198. break;
  199. }
  200. case 3:
  201. {
  202. controller[deviceindex[(int)event.cdevice.which]].y2 = (int)event.caxis.value;
  203. break;
  204. }
  205. case 4:
  206. {
  207. controller[deviceindex[(int)event.cdevice.which]].L2 = (int)event.caxis.value;
  208. break;
  209. }
  210. case 5:
  211. {
  212. controller[deviceindex[(int)event.cdevice.which]].R2 = (int)event.caxis.value;
  213. break;
  214. }
  215. }
  216.  
  217. std::cout << SDL_GameControllerGetStringForAxis((SDL_GameControllerAxis)event.caxis.axis) << " is " << (SDL_GameControllerAxis)event.caxis.axis;
  218.  
  219.  
  220. if ( debug == 1)
  221. {
  222. std::cout << "controller axis motion: "
  223. << "Controller " << deviceindex[(int)event.cdevice.which]+1
  224. << " on ID " << (int)event.caxis.which
  225. << " axis = " << axis << " AKA: " << (SDL_GameControllerAxis)event.caxis.axis
  226. << " value = " << (int)event.caxis.value
  227. << std::endl;
  228. }
  229. break;
  230. }
  231.  
  232.  
  233. case SDL_CONTROLLERBUTTONDOWN:
  234. {
  235. const char* button = NULL;
  236.  
  237. button = SDL_GameControllerGetStringForButton(
  238. (SDL_GameControllerButton)event.cbutton.button);
  239.  
  240. switch((SDL_GameControllerButton)event.cbutton.button)
  241. {
  242. case 0:
  243. {
  244. controller[deviceindex[(int)event.cdevice.which]].a = true;
  245. break;
  246. }
  247. case 1:
  248. {
  249. controller[deviceindex[(int)event.cdevice.which]].b = true;
  250. break;
  251. }
  252. case 2:
  253. {
  254. controller[deviceindex[(int)event.cdevice.which]].x = true;
  255. break;
  256. }
  257. case 3:
  258. {
  259. controller[deviceindex[(int)event.cdevice.which]].y = true;
  260. break;
  261. }
  262. case 4:
  263. {
  264. controller[deviceindex[(int)event.cdevice.which]].back = true;
  265. break;
  266. }
  267. case 5:
  268. {
  269. controller[deviceindex[(int)event.cdevice.which]].guide = true;
  270. break;
  271. }
  272. case 6:
  273. {
  274. controller[deviceindex[(int)event.cdevice.which]].start = true;
  275. break;
  276. }
  277. case 7:
  278. {
  279. controller[deviceindex[(int)event.cdevice.which]].L3 = true;
  280. break;
  281. }
  282. case 8:
  283. {
  284. controller[deviceindex[(int)event.cdevice.which]].R3 = true;
  285. break;
  286. }
  287. case 9:
  288. {
  289. controller[deviceindex[(int)event.cdevice.which]].L1 = true;
  290. break;
  291. }
  292. case 10:
  293. {
  294. controller[deviceindex[(int)event.cdevice.which]].R1 = true;
  295. break;
  296. }
  297. case 11:
  298. {
  299. controller[deviceindex[(int)event.cdevice.which]].dup = true;
  300. break;
  301. }
  302. case 12:
  303. {
  304. controller[deviceindex[(int)event.cdevice.which]].ddown = true;
  305. break;
  306. }
  307. case 13:
  308. {
  309. controller[deviceindex[(int)event.cdevice.which]].dleft = true;
  310. break;
  311. }
  312. case 14:
  313. {
  314. controller[deviceindex[(int)event.cdevice.which]].dright = true;
  315. break;
  316. }
  317. }
  318.  
  319.  
  320. if (debug == 1)
  321. {
  322. std::cout << "controller button down:"
  323. << "Controller " << deviceindex[(int)event.cdevice.which]+1
  324. << " on ID " << (int)event.caxis.which
  325. << " button = " << button
  326. << std::endl;
  327. }
  328.  
  329. break;
  330. }
  331.  
  332. case SDL_CONTROLLERBUTTONUP:
  333. {
  334. const char* button = NULL;
  335.  
  336. button = SDL_GameControllerGetStringForButton(
  337. (SDL_GameControllerButton)event.cbutton.button);
  338.  
  339. switch((SDL_GameControllerButton)event.cbutton.button)
  340. {
  341. case 0:
  342. {
  343. controller[deviceindex[(int)event.cdevice.which]].a = false;
  344. break;
  345. }
  346. case 1:
  347. {
  348. controller[deviceindex[(int)event.cdevice.which]].b = false;
  349. break;
  350. }
  351. case 2:
  352. {
  353. controller[deviceindex[(int)event.cdevice.which]].x = false;
  354. break;
  355. }
  356. case 3:
  357. {
  358. controller[deviceindex[(int)event.cdevice.which]].y = false;
  359. break;
  360. }
  361. case 4:
  362. {
  363. controller[deviceindex[(int)event.cdevice.which]].back = false;
  364. break;
  365. }
  366. case 5:
  367. {
  368. controller[deviceindex[(int)event.cdevice.which]].guide = false;
  369. break;
  370. }
  371. case 6:
  372. {
  373. controller[deviceindex[(int)event.cdevice.which]].start = false;
  374. break;
  375. }
  376. case 7:
  377. {
  378. controller[deviceindex[(int)event.cdevice.which]].L3 = false;
  379. break;
  380. }
  381. case 8:
  382. {
  383. controller[deviceindex[(int)event.cdevice.which]].R3 = false;
  384. break;
  385. }
  386. case 9:
  387. {
  388. controller[deviceindex[(int)event.cdevice.which]].L1 = false;
  389. break;
  390. }
  391. case 10:
  392. {
  393. controller[deviceindex[(int)event.cdevice.which]].R1 = false;
  394. break;
  395. }
  396. case 11:
  397. {controller[deviceindex[(int)event.cdevice.which]].dup = false;
  398. break;
  399. }
  400. case 12:
  401. {
  402. controller[deviceindex[(int)event.cdevice.which]].ddown = false;
  403. break;
  404. }
  405. case 13:
  406. {
  407. controller[deviceindex[(int)event.cdevice.which]].dleft = false;
  408. break;
  409. }
  410. case 14:
  411. {
  412. controller[deviceindex[(int)event.cdevice.which]].dright = false;
  413. break;
  414. }
  415. }
  416.  
  417.  
  418. if (debug == 1)
  419. {
  420.  
  421. std::cout << "controller button up: "
  422. << "Controller " << deviceindex[(int)event.cdevice.which]+1
  423. << " on ID " << (int)event.caxis.which
  424. << " button = " << button
  425. << std::endl;
  426. }
  427.  
  428. break;
  429. }
  430.  
  431.  
  432. }
  433.  
  434.  
  435. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement