Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "input.h"
- input::input()
- {
- debug = 1;
- ininit = 1;
- deadzone = 5000;
- }
- bool input::init()
- {
- SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER);
- SDL_GameControllerAddMappingsFromFile("gamecontrollerdb.txt");
- numDevices = SDL_NumJoysticks();
- std::cout << std::endl << numDevices <<" controller(s) found" <<std::endl;
- SDL_JoystickEventState(SDL_ENABLE);
- SDL_GameControllerEventState(SDL_ENABLE);
- for(int i=0; i<numDevices; ++i)
- {
- controller[i].js = SDL_JoystickOpen(i);
- if(!controller[i].js) {
- std::cout << "failed to open joystick: " << SDL_GetError() << "\n";
- return 113;
- }
- if(SDL_IsGameController(i)) {
- controller[i].gc = SDL_GameControllerOpen(i);
- if(!controller[i].gc) {
- std::cout << "failed to open game controller: " << SDL_GetError() << "\n";
- return 114;
- }
- }
- std:: cout << "Controller: " << i+1 << " on ID:" << SDL_JoystickInstanceID(controller[i].js) << std::endl;
- deviceindex[SDL_JoystickInstanceID(controller[i].js)] = i;
- }
- std::cout << "--------------------------" << std::endl;
- if (debug == 1)
- Details();
- ininit = 0;
- }
- void input::Details()
- {
- std::cout << numDevices <<" controllers found" <<std::endl;
- for(int i=0; i<numDevices; i++)
- {
- char sguid[1024];
- SDL_JoystickGUID guid = SDL_JoystickGetGUID(controller[i].js);
- SDL_JoystickGetGUIDString(guid, sguid, sizeof(sguid));
- std::cout << std::endl << "-------------------------------------------------------"<<std::endl;
- std::cout << "Controller " << i+1 << "\n";
- std::cout << " guid = " << sguid << "\n";
- std::cout << " name = '" << SDL_JoystickName(controller[i].js) << "'\n";
- std::cout << " axes = " << SDL_JoystickNumAxes(controller[i].js) << "\n";
- std::cout << " balls = " << SDL_JoystickNumBalls(controller[i].js) << "\n";
- std::cout << " hats = " << SDL_JoystickNumHats(controller[i].js) << "\n";
- std::cout << " buttons = " << SDL_JoystickNumButtons(controller[i].js) << "\n";
- if(controller[i].gc) {
- std::cout << "\n --detected as game controller--\n";
- std::cout << " Controller Name = '" << SDL_GameControllerName(controller[i].gc) ;
- }
- else {
- std::cout << "\n --detected as game controller--\n";
- std::cout << " Controller Name = '" << SDL_JoystickName(controller[i].js) ;
- }
- std::cout << std::endl << "-------------------------------------------------------"<<std::endl;
- }
- }
- int input::stringtoint(const char* value)
- {
- std::stringstream convert;
- convert << value;
- unsigned int intValue;
- convert >> intValue;
- return intValue;
- }
- void input::handleEvents(SDL_Event event)
- {
- //while( SDL_PollEvent( &event ))
- switch(event.type)
- {
- case SDL_JOYDEVICEADDED: // LOOK INTO: event.cdevice.which
- //case SDL_CONTROLLERDEVICEADDED: // the cdevice part may be off, think might only supposed to be during controller events
- // not for button or axis events. maybe caxis / cbutton.
- {
- if (ininit == 1)
- break;
- numDevices = SDL_NumJoysticks();
- if (debug == 1)
- {
- std::cout << "Now there is "<< numDevices << " device(s)." << std::endl;
- }
- bool yesbreak = 0;
- for(int i=0; i<numDevices; ++i)
- {
- if (controller[i].js == 0)
- {
- controller[i].gc = SDL_GameControllerOpen(i);
- controller[i].js = SDL_JoystickOpen(i);
- SDL_Delay(10);
- std::cout << "Plugged in AAAcontroller " << (int)event.jdevice.which+1 << " now on ID :" <<SDL_JoystickInstanceID(controller[(int)event.jdevice.which].js)<<std::endl;
- deviceindex[SDL_JoystickInstanceID(controller[(int)event.jdevice.which].js)] = (int)event.jdevice.which;
- yesbreak = 1;
- break;
- }
- }
- if (yesbreak == 1)
- break;
- controller[numDevices-1].gc = SDL_GameControllerOpen((int)event.cdevice.which);
- controller[numDevices-1].js = SDL_JoystickOpen((int)event.cdevice.which);
- if (debug == 1)
- {
- std::cout << "Plugged in controller " << (int)event.jdevice.which+1 << " now on ID :" <<SDL_JoystickInstanceID(controller[(int)event.jdevice.which].js)<<std::endl;
- }
- deviceindex[SDL_JoystickInstanceID(controller[(int)event.jdevice.which].js)] = (int)event.jdevice.which;
- if (SDL_JoystickInstanceID(controller[(int)event.jdevice.which].js) < 0)
- std::cout << SDL_GetError() << std::endl;
- break;
- }
- case SDL_JOYDEVICEREMOVED:
- // case SDL_CONTROLLERDEVICEREMOVED:
- {
- SDL_GameControllerClose(controller[deviceindex[(int)event.cdevice.which]].gc);
- SDL_JoystickClose(controller[deviceindex[(int)event.cdevice.which]].js);
- controller[deviceindex[(int)event.cdevice.which]].gc = NULL;
- controller[deviceindex[(int)event.cdevice.which]].js = NULL;
- numDevices = SDL_NumJoysticks();
- if (debug == 1)
- {
- std::cout << "removed controller " << deviceindex[(int)event.cdevice.which]+1 << " on ID "<< (int)event.cdevice.which << std::endl;
- std::cout << numDevices << " devices still plugged in." << std::endl;
- }
- break;
- }
- case SDL_CONTROLLERAXISMOTION:
- {
- const char* axis = SDL_GameControllerGetStringForAxis((SDL_GameControllerAxis)event.caxis.axis);
- switch((SDL_GameControllerAxis)event.caxis.axis)
- {
- case 0:
- {
- controller[deviceindex[(int)event.cdevice.which]].x1 = (int)event.caxis.value;
- break;
- }
- case 1:
- {
- controller[deviceindex[(int)event.cdevice.which]].y1 = (int)event.caxis.value;
- break;
- }
- case 2:
- {
- controller[deviceindex[(int)event.cdevice.which]].x2 = (int)event.caxis.value;
- break;
- }
- case 3:
- {
- controller[deviceindex[(int)event.cdevice.which]].y2 = (int)event.caxis.value;
- break;
- }
- case 4:
- {
- controller[deviceindex[(int)event.cdevice.which]].L2 = (int)event.caxis.value;
- break;
- }
- case 5:
- {
- controller[deviceindex[(int)event.cdevice.which]].R2 = (int)event.caxis.value;
- break;
- }
- }
- std::cout << SDL_GameControllerGetStringForAxis((SDL_GameControllerAxis)event.caxis.axis) << " is " << (SDL_GameControllerAxis)event.caxis.axis;
- if ( debug == 1)
- {
- std::cout << "controller axis motion: "
- << "Controller " << deviceindex[(int)event.cdevice.which]+1
- << " on ID " << (int)event.caxis.which
- << " axis = " << axis << " AKA: " << (SDL_GameControllerAxis)event.caxis.axis
- << " value = " << (int)event.caxis.value
- << std::endl;
- }
- break;
- }
- case SDL_CONTROLLERBUTTONDOWN:
- {
- const char* button = NULL;
- button = SDL_GameControllerGetStringForButton(
- (SDL_GameControllerButton)event.cbutton.button);
- switch((SDL_GameControllerButton)event.cbutton.button)
- {
- case 0:
- {
- controller[deviceindex[(int)event.cdevice.which]].a = true;
- break;
- }
- case 1:
- {
- controller[deviceindex[(int)event.cdevice.which]].b = true;
- break;
- }
- case 2:
- {
- controller[deviceindex[(int)event.cdevice.which]].x = true;
- break;
- }
- case 3:
- {
- controller[deviceindex[(int)event.cdevice.which]].y = true;
- break;
- }
- case 4:
- {
- controller[deviceindex[(int)event.cdevice.which]].back = true;
- break;
- }
- case 5:
- {
- controller[deviceindex[(int)event.cdevice.which]].guide = true;
- break;
- }
- case 6:
- {
- controller[deviceindex[(int)event.cdevice.which]].start = true;
- break;
- }
- case 7:
- {
- controller[deviceindex[(int)event.cdevice.which]].L3 = true;
- break;
- }
- case 8:
- {
- controller[deviceindex[(int)event.cdevice.which]].R3 = true;
- break;
- }
- case 9:
- {
- controller[deviceindex[(int)event.cdevice.which]].L1 = true;
- break;
- }
- case 10:
- {
- controller[deviceindex[(int)event.cdevice.which]].R1 = true;
- break;
- }
- case 11:
- {
- controller[deviceindex[(int)event.cdevice.which]].dup = true;
- break;
- }
- case 12:
- {
- controller[deviceindex[(int)event.cdevice.which]].ddown = true;
- break;
- }
- case 13:
- {
- controller[deviceindex[(int)event.cdevice.which]].dleft = true;
- break;
- }
- case 14:
- {
- controller[deviceindex[(int)event.cdevice.which]].dright = true;
- break;
- }
- }
- if (debug == 1)
- {
- std::cout << "controller button down:"
- << "Controller " << deviceindex[(int)event.cdevice.which]+1
- << " on ID " << (int)event.caxis.which
- << " button = " << button
- << std::endl;
- }
- break;
- }
- case SDL_CONTROLLERBUTTONUP:
- {
- const char* button = NULL;
- button = SDL_GameControllerGetStringForButton(
- (SDL_GameControllerButton)event.cbutton.button);
- switch((SDL_GameControllerButton)event.cbutton.button)
- {
- case 0:
- {
- controller[deviceindex[(int)event.cdevice.which]].a = false;
- break;
- }
- case 1:
- {
- controller[deviceindex[(int)event.cdevice.which]].b = false;
- break;
- }
- case 2:
- {
- controller[deviceindex[(int)event.cdevice.which]].x = false;
- break;
- }
- case 3:
- {
- controller[deviceindex[(int)event.cdevice.which]].y = false;
- break;
- }
- case 4:
- {
- controller[deviceindex[(int)event.cdevice.which]].back = false;
- break;
- }
- case 5:
- {
- controller[deviceindex[(int)event.cdevice.which]].guide = false;
- break;
- }
- case 6:
- {
- controller[deviceindex[(int)event.cdevice.which]].start = false;
- break;
- }
- case 7:
- {
- controller[deviceindex[(int)event.cdevice.which]].L3 = false;
- break;
- }
- case 8:
- {
- controller[deviceindex[(int)event.cdevice.which]].R3 = false;
- break;
- }
- case 9:
- {
- controller[deviceindex[(int)event.cdevice.which]].L1 = false;
- break;
- }
- case 10:
- {
- controller[deviceindex[(int)event.cdevice.which]].R1 = false;
- break;
- }
- case 11:
- {controller[deviceindex[(int)event.cdevice.which]].dup = false;
- break;
- }
- case 12:
- {
- controller[deviceindex[(int)event.cdevice.which]].ddown = false;
- break;
- }
- case 13:
- {
- controller[deviceindex[(int)event.cdevice.which]].dleft = false;
- break;
- }
- case 14:
- {
- controller[deviceindex[(int)event.cdevice.which]].dright = false;
- break;
- }
- }
- if (debug == 1)
- {
- std::cout << "controller button up: "
- << "Controller " << deviceindex[(int)event.cdevice.which]+1
- << " on ID " << (int)event.caxis.which
- << " button = " << button
- << std::endl;
- }
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement