Ejemplo n.º 1
0
Archivo: Input.cpp Proyecto: 456z/gosu
 void pollGameController(SDL_GameController *gameController, GamepadBuffer& gamepad)
 {
     gamepad[gpLeft - gpRangeBegin] =
         SDL_GameControllerGetButton(gameController, SDL_CONTROLLER_BUTTON_DPAD_LEFT) ||
         SDL_GameControllerGetAxis(gameController, SDL_CONTROLLER_AXIS_LEFTX) < -DEAD_ZONE ||
         SDL_GameControllerGetAxis(gameController, SDL_CONTROLLER_AXIS_RIGHTX) < -DEAD_ZONE;
     
     gamepad[gpRight - gpRangeBegin] =
         SDL_GameControllerGetButton(gameController, SDL_CONTROLLER_BUTTON_DPAD_RIGHT) ||
         SDL_GameControllerGetAxis(gameController, SDL_CONTROLLER_AXIS_LEFTX) > +DEAD_ZONE ||
         SDL_GameControllerGetAxis(gameController, SDL_CONTROLLER_AXIS_RIGHTX) > +DEAD_ZONE;
     
     gamepad[gpUp - gpRangeBegin] =
         SDL_GameControllerGetButton(gameController, SDL_CONTROLLER_BUTTON_DPAD_UP) ||
         SDL_GameControllerGetAxis(gameController, SDL_CONTROLLER_AXIS_LEFTY) < -DEAD_ZONE ||
         SDL_GameControllerGetAxis(gameController, SDL_CONTROLLER_AXIS_RIGHTY) < -DEAD_ZONE;
     
     gamepad[gpDown - gpRangeBegin] =
         SDL_GameControllerGetButton(gameController, SDL_CONTROLLER_BUTTON_DPAD_DOWN) ||
         SDL_GameControllerGetAxis(gameController, SDL_CONTROLLER_AXIS_LEFTY) > +DEAD_ZONE ||
         SDL_GameControllerGetAxis(gameController, SDL_CONTROLLER_AXIS_RIGHTY) > +DEAD_ZONE;
     
     for (int button = SDL_CONTROLLER_BUTTON_A; button < SDL_CONTROLLER_BUTTON_DPAD_UP; ++button) {
         gamepad[gpButton0 - gpRangeBegin + button - SDL_CONTROLLER_BUTTON_A] =
             SDL_GameControllerGetButton(gameController, (SDL_GameControllerButton)button);
     }
 }
Ejemplo n.º 2
0
void Game::processEvents(){
    SDL_Event event;
    
    while(SDL_PollEvent(&event) != 0){
        if(event.type == SDL_QUIT){
            this->running = false;
        } else if(event.type == SDL_KEYDOWN){
            switch (event.key.keysym.sym) {
                case SDLK_UP:
                    this->vKeys.up = true;
                    break;
                case SDLK_DOWN:
                    this->vKeys.down = true;
                    break;
                case SDLK_LEFT:
                    this->vKeys.left = true;
                    break;
                case SDLK_RIGHT:
                    this->vKeys.right = true;
                    break;
                    
                default:
                    break;
            }
        } else if(event.type == SDL_KEYUP){
            switch (event.key.keysym.sym) {
                case SDLK_UP:
                    this->vKeys.up = false;
                    break;
                case SDLK_DOWN:
                    this->vKeys.down = false;
                    break;
                case SDLK_LEFT:
                    this->vKeys.left = false;
                    break;
                case SDLK_RIGHT:
                    this->vKeys.right = false;
                    break;
                default:
                    break;
            }
        }
    }
    
    if (this->gamePad) {
        this->vKeys.up = SDL_GameControllerGetButton(this->gamePad, SDL_CONTROLLER_BUTTON_DPAD_UP);
        this->vKeys.down = SDL_GameControllerGetButton(this->gamePad, SDL_CONTROLLER_BUTTON_DPAD_DOWN);
        this->vKeys.left = SDL_GameControllerGetButton(this->gamePad, SDL_CONTROLLER_BUTTON_DPAD_LEFT);
        this->vKeys.right = SDL_GameControllerGetButton(this->gamePad, SDL_CONTROLLER_BUTTON_DPAD_RIGHT);
        if (SDL_GameControllerGetButton(this->gamePad, SDL_CONTROLLER_BUTTON_BACK)) {
            this->running = false;
        }
    }
}
Ejemplo n.º 3
0
static void sdl_process_controller_input(SDL_GameController* controller, input_state_t* input_state)
{
		controller = grab_controller(controller);
		if (controller) {
			process_button(&input_state->move_up, SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_DPAD_UP));
			process_button(&input_state->move_down, SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_DPAD_DOWN));
			process_button(&input_state->move_left, SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_DPAD_LEFT));
			process_button(&input_state->move_right, SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_DPAD_RIGHT));
		}
		else {
			memset(input_state, 0, sizeof(input_state_t));
		}
	return;
}
Ejemplo n.º 4
0
bool isControllerButtonPressed(u32 controllerIndex, ControllerButton button)
{
	SDL_GameController* gc = g_controllerHandles[controllerIndex];
	if (gc && SDL_GameControllerGetAttached(gc))
		return SDL_GameControllerGetButton(gc, (SDL_GameControllerButton)button) != 0;

	return false;
}
Ejemplo n.º 5
0
static uint8_t sdl_pad_get_button(sdl_joypad_t *pad, unsigned button)
{
#ifdef HAVE_SDL2
   /* TODO: see if a LUT like xinput_joypad.c's button_index_to_bitmap_code is needed. */
   if (pad->controller)
      return SDL_GameControllerGetButton(pad->controller, (SDL_GameControllerButton)button);
#endif
   return SDL_JoystickGetButton(pad->joypad, button);
}
Ejemplo n.º 6
0
internal void process_button(const struct game_button_state *old_state,
			     struct game_button_state *new_state,
			     SDL_GameController * controller,
			     SDL_GameControllerButton button)
{
	new_state->ended_down = SDL_GameControllerGetButton(controller, button);
	if (new_state->ended_down == old_state->ended_down) {
		++new_state->half_transition_count;
	}
}
Ejemplo n.º 7
0
bool DEV_Joystick::aButtonReleaseIsPositive(int button)
{
#ifdef WITH_SDL
	if (!(SDL_CHECK(SDL_GameControllerGetButton) &&
		SDL_GameControllerGetButton(m_private->m_gamecontroller, (SDL_GameControllerButton)button)))
	{
		return true;
	}
#endif
	return false;
}
void GameEngine::CheckJoystick(){
  SDL_GameController *controller = this->GetController();
  int jsJoystickState = JOY_NONE;
  Sint16 axis = 0;
  
  // Check horizontal movement
  axis = SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_LEFTX);
  if(axis){
    if(axis < m_rcJoystickTrip.x)
      jsJoystickState |= JOY_LEFT;
    else if(axis > m_rcJoystickTrip.w){
      jsJoystickState |= JOY_RIGHT;
    }
  }

  // Check vertical movement
  axis = SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_LEFTY);
  if(axis){
    if(axis < m_rcJoystickTrip.h)
      jsJoystickState |= JOY_UP;
    else if(axis > m_rcJoystickTrip.y){
      jsJoystickState |= JOY_DOWN;
    }
  }

  //check joystick buttons
  Uint8 is_pressed_a = SDL_GameControllerGetButton(controller,
						   SDL_CONTROLLER_BUTTON_A);
  Uint8 is_pressed_b = SDL_GameControllerGetButton(controller,
						   SDL_CONTROLLER_BUTTON_B);  
  if(is_pressed_a)
    jsJoystickState |= JOY_FIRE1;
  if(is_pressed_b)
    jsJoystickState |= JOY_FIRE2;
  
  // Allow the game to handle the joystick
  m_game->HandleJoystick((JOYSTATE)jsJoystickState);
}
Ejemplo n.º 9
0
Controller::Button	Controller::Peripheral::retJoyBut()
{
  register SDL_GameController *getJoy = getControl();

  if (!getJoy)
    return (retJoyButton());
  for (register int i = static_cast<int>(BUTTON_MAX); i >= 0 ; i--)
    {
      if (SDL_GameControllerGetButton(getJoy, static_cast<SDL_GameControllerButton>(i)))
	return (static_cast<Controller::Button>(getValToBut
						(static_cast<Controller::Button>(i))));
    }
  return (BUTTON_INVALID);
}
Ejemplo n.º 10
0
/*
================
IN_Commands

Emit key events for game controller buttons, including emulated buttons for analog sticks/triggers
================
*/
void IN_Commands (void)
{
	joyaxisstate_t newaxisstate;
	int i;
	const float stickthreshold = 0.9;
	const float triggerthreshold = joy_deadzone_trigger.value;
	
	if (!joy_enable.value)
		return;
	
	if (!joy_active_controller)
		return;

	// emit key events for controller buttons
	for (i = 0; i < SDL_CONTROLLER_BUTTON_MAX; i++)
	{
		qboolean newstate = SDL_GameControllerGetButton(joy_active_controller, (SDL_GameControllerButton)i);
		qboolean oldstate = joy_buttonstate.buttondown[i];
		
		joy_buttonstate.buttondown[i] = newstate;
		
		// NOTE: This can cause a reentrant call of IN_Commands, via SCR_ModalMessage when confirming a new game.
		IN_JoyKeyEvent(oldstate, newstate, IN_KeyForControllerButton((SDL_GameControllerButton)i), &joy_buttontimer[i]);
	}
	
	for (i = 0; i < SDL_CONTROLLER_AXIS_MAX; i++)
	{
		newaxisstate.axisvalue[i] = SDL_GameControllerGetAxis(joy_active_controller, (SDL_GameControllerAxis)i) / 32768.0f;
	}
	
	// emit emulated arrow keys so the analog sticks can be used in the menu
	if (key_dest != key_game)
	{
		IN_JoyKeyEvent(joy_axisstate.axisvalue[SDL_CONTROLLER_AXIS_LEFTX] < -stickthreshold, newaxisstate.axisvalue[SDL_CONTROLLER_AXIS_LEFTX] < -stickthreshold, K_LEFTARROW, &joy_emulatedkeytimer[0]);
		IN_JoyKeyEvent(joy_axisstate.axisvalue[SDL_CONTROLLER_AXIS_LEFTX] > stickthreshold,  newaxisstate.axisvalue[SDL_CONTROLLER_AXIS_LEFTX] > stickthreshold, K_RIGHTARROW, &joy_emulatedkeytimer[1]);
		IN_JoyKeyEvent(joy_axisstate.axisvalue[SDL_CONTROLLER_AXIS_LEFTY] < -stickthreshold, newaxisstate.axisvalue[SDL_CONTROLLER_AXIS_LEFTY] < -stickthreshold, K_UPARROW, &joy_emulatedkeytimer[2]);
		IN_JoyKeyEvent(joy_axisstate.axisvalue[SDL_CONTROLLER_AXIS_LEFTY] > stickthreshold,  newaxisstate.axisvalue[SDL_CONTROLLER_AXIS_LEFTY] > stickthreshold, K_DOWNARROW, &joy_emulatedkeytimer[3]);
		IN_JoyKeyEvent(joy_axisstate.axisvalue[SDL_CONTROLLER_AXIS_RIGHTX] < -stickthreshold,newaxisstate.axisvalue[SDL_CONTROLLER_AXIS_RIGHTX] < -stickthreshold, K_LEFTARROW, &joy_emulatedkeytimer[4]);
		IN_JoyKeyEvent(joy_axisstate.axisvalue[SDL_CONTROLLER_AXIS_RIGHTX] > stickthreshold, newaxisstate.axisvalue[SDL_CONTROLLER_AXIS_RIGHTX] > stickthreshold, K_RIGHTARROW, &joy_emulatedkeytimer[5]);
		IN_JoyKeyEvent(joy_axisstate.axisvalue[SDL_CONTROLLER_AXIS_RIGHTY] < -stickthreshold,newaxisstate.axisvalue[SDL_CONTROLLER_AXIS_RIGHTY] < -stickthreshold, K_UPARROW, &joy_emulatedkeytimer[6]);
		IN_JoyKeyEvent(joy_axisstate.axisvalue[SDL_CONTROLLER_AXIS_RIGHTY] > stickthreshold, newaxisstate.axisvalue[SDL_CONTROLLER_AXIS_RIGHTY] > stickthreshold, K_DOWNARROW, &joy_emulatedkeytimer[7]);
	}
	
	// emit emulated keys for the analog triggers
	IN_JoyKeyEvent(joy_axisstate.axisvalue[SDL_CONTROLLER_AXIS_TRIGGERLEFT] > triggerthreshold,  newaxisstate.axisvalue[SDL_CONTROLLER_AXIS_TRIGGERLEFT] > triggerthreshold, K_LTRIGGER, &joy_emulatedkeytimer[8]);
	IN_JoyKeyEvent(joy_axisstate.axisvalue[SDL_CONTROLLER_AXIS_TRIGGERRIGHT] > triggerthreshold, newaxisstate.axisvalue[SDL_CONTROLLER_AXIS_TRIGGERRIGHT] > triggerthreshold, K_RTRIGGER, &joy_emulatedkeytimer[9]);
	
	joy_axisstate = newaxisstate;
}
Ejemplo n.º 11
0
	bool Joystick::isButtonPressed(int8_t button) const {
		if (button < 0 || !isConnected()) {
			return false;
		}
		if (!isController()) {
			if (SDL_JoystickGetButton(m_joystickHandle, button) == 1) {
				return true;
			}
			return false;
		}

		SDL_GameControllerButton sdlButton = static_cast<SDL_GameControllerButton>(button);
		if (SDL_GameControllerGetButton(m_controllerHandle, sdlButton) == 1) {
			return true;
		}
		return false;
	}
Ejemplo n.º 12
0
int	Controller::Peripheral::checkButtons()
{
  register SDL_GameController *getJoy = getControl();

  _but = Controller::BUTTON_INVALID;
  if (!getJoy)
    return (checkJoyButton());
  for (register int i = static_cast<int>(BUTTON_MAX); i >= 0 ; i--)
    {
      if (SDL_GameControllerGetButton(getJoy, static_cast<SDL_GameControllerButton>(i)))
	{
	  _but = static_cast<Controller::Button>(getValToBut(static_cast<Controller::Button>(i)));
	  break ;
	}
    }
  return (-1);
}
Ejemplo n.º 13
0
bool DEV_Joystick::aAnyButtonPressIsPositive(void)
{
#ifdef WITH_SDL
	if (!(SDL_CHECK(SDL_GameControllerGetButton))) {
		return false;
	}

	/* this is needed for the "all events" option
	 * so we know if there are no buttons pressed */
	for (int i = 0; i < m_buttonmax; i++) {
		if (SDL_GameControllerGetButton(m_private->m_gamecontroller, (SDL_GameControllerButton)i)) {
			return true;
		}
	}
#endif
	return false;
}
Ejemplo n.º 14
0
bool Joystick::isGamepadDown(const std::vector<GamepadButton> &blist) const
{
	if (!isConnected() || !isGamepad())
		return false;

	SDL_GameControllerButton sdlbutton;

	for (GamepadButton button : blist)
	{
		if (!getConstant(button, sdlbutton))
			continue;

		if (SDL_GameControllerGetButton(controller, sdlbutton) == 1)
			return true;
	}

	return false;
}
Ejemplo n.º 15
0
void DoInputSingle(photon_input_state &state, photon_input &input){
    state.last_state = state.current_state;
    switch(state.type){
    case photon_input_state::keyboard:{
        const Uint8* keyboard = SDL_GetKeyboardState(nullptr);
        SDL_Keymod modifiers = SDL_GetModState();
        state.current_state = keyboard[state.key] && (modifiers == state.modifiers || modifiers & state.modifiers);
        break;
    }
    case photon_input_state::joystick_axis:{
        if(state.joystick_input_index > -1){
            state.current_state = SDL_JoystickGetAxis(input.joystick, state.joystick_input_index) / 32768.0f;
            if(state.axis_input_negate){
                state.current_state = -state.current_state;
            }
        }
        if(state.current_state < input.deadzone){
            state.current_state = 0.0f;
        }
        break;
    }
    case photon_input_state::joystick_button:{
        state.current_state = SDL_JoystickGetButton(input.joystick, state.joystick_input_index);
        break;
    }
    case photon_input_state::gamecontroller_axis:{
        state.current_state = SDL_GameControllerGetAxis(input.controller, state.controller_axis) / 32768.0f;
        if(state.axis_input_negate){
            state.current_state = -state.current_state;
        }
        if(state.current_state < input.deadzone){
            state.current_state = 0.0f;
        }
        break;
    }
    case photon_input_state::gamecontroller_button:{
        state.current_state = SDL_GameControllerGetButton(input.controller, state.controller_button);
        break;
    }
    case photon_input_state::none:
        break;
    }
}
Ejemplo n.º 16
0
int GetKeyValue(PIN_GameControllerEntry* ctrl,PIN_GameControllerKeyInfo key)
{
    if(key.KeyType == PIN_GKT_BUTTON)
    {
        return SDL_GameControllerGetButton(ctrl->Controller,key.Button);
    }
    else if(key.KeyType == PIN_GKT_AXIS)
    {
        if(key.Axis == SDL_CONTROLLER_AXIS_INVALID)
        {
            printf("Should NOT come this far!\n");
        }
        else
        {
            Sint16 v = SDL_GameControllerGetAxis(ctrl->Controller,key.Axis);
            return v;
        }
    }

    return 0;
}
Ejemplo n.º 17
0
static void processControllerButtonInput(ButtonState* newState, const ButtonState* oldState, bool* isAnalog, SDL_GameController* controller, SDL_GameControllerButton sdlButton) {
    newState->isEndedDown = SDL_GameControllerGetButton(controller, sdlButton);
    if(newState->isEndedDown) {

        switch(sdlButton) {
            case SDL_CONTROLLER_BUTTON_DPAD_LEFT:
            case SDL_CONTROLLER_BUTTON_DPAD_RIGHT:
            case SDL_CONTROLLER_BUTTON_DPAD_DOWN:
            case SDL_CONTROLLER_BUTTON_DPAD_UP:
                *isAnalog = false;
                break;
            default:
                break;
        }
    }

    if(newState->isEndedDown != oldState->isEndedDown) {
        newState->halfTransitionCount +=  1;
    }



}
Ejemplo n.º 18
0
void SDLController::Tick()
{
    float x[3];
    float y[3];

    // we'll accept input from left stick, right stick or dpad...

    // X axis
    x[0] = ApplyDeadZone(SDL_GameControllerGetAxis(m_Ctrl, SDL_CONTROLLER_AXIS_LEFTX));
    x[1] = ApplyDeadZone(SDL_GameControllerGetAxis(m_Ctrl, SDL_CONTROLLER_AXIS_RIGHTX));
    if (SDL_GameControllerGetButton(m_Ctrl, SDL_CONTROLLER_BUTTON_DPAD_LEFT))
        x[2] = -1.0f;
    else if (SDL_GameControllerGetButton(m_Ctrl, SDL_CONTROLLER_BUTTON_DPAD_RIGHT))
        x[2] = 1.0f;
    else
        x[2] = 0.0f;

    m_X = AvgNonZero(x);

    // Y axis
    y[0] = ApplyDeadZone(SDL_GameControllerGetAxis(m_Ctrl, SDL_CONTROLLER_AXIS_LEFTY));
    y[1] = ApplyDeadZone(SDL_GameControllerGetAxis(m_Ctrl, SDL_CONTROLLER_AXIS_RIGHTY));
    if (SDL_GameControllerGetButton(m_Ctrl, SDL_CONTROLLER_BUTTON_DPAD_UP))
        y[2] = -1.0f;
    else if (SDL_GameControllerGetButton(m_Ctrl, SDL_CONTROLLER_BUTTON_DPAD_DOWN))
        y[2] = 1.0f;
    else
        y[2] = 0.0f;
    m_Y = AvgNonZero(y);



    // buttons
    int buttons =  0;
    if (SDL_GameControllerGetButton(m_Ctrl,SDL_CONTROLLER_BUTTON_A)==1)
        buttons |= CTRL_BTN_FIRE;
    if (SDL_GameControllerGetButton(m_Ctrl,SDL_CONTROLLER_BUTTON_START)==1)
        buttons |= CTRL_BTN_START;

    m_PrevBtnState = m_BtnState;
    m_BtnState = buttons;
}
Ejemplo n.º 19
0
static bool
getJoystickButtonState(const config::input::InputDevice *device,
                       SDL_GameController *controller,
                       vpad::Channel channel,
                       vpad::Core button)
{
   decaf_check(device);
   decaf_check(controller);

   auto joystick = SDL_GameControllerGetJoystick(controller);
   decaf_check(joystick);

   auto index = -1;
   auto name = SDL_CONTROLLER_BUTTON_INVALID;
   // SDL has no concept of ZR/ZL "buttons" (only axes) so we have to
   //  kludge around...
   auto axisName = SDL_CONTROLLER_AXIS_INVALID;

   switch (button) {
   case vpad::Core::Up:
      index = device->button_up;
      name = SDL_CONTROLLER_BUTTON_DPAD_UP;
      break;
   case vpad::Core::Down:
      index = device->button_down;
      name = SDL_CONTROLLER_BUTTON_DPAD_DOWN;
      break;
   case vpad::Core::Left:
      index = device->button_left;
      name = SDL_CONTROLLER_BUTTON_DPAD_LEFT;
      break;
   case vpad::Core::Right:
      index = device->button_right;
      name = SDL_CONTROLLER_BUTTON_DPAD_RIGHT;
      break;
   case vpad::Core::A:
      index = device->button_a;
      name = SDL_CONTROLLER_BUTTON_B;  // SDL uses stupid Microsoft mapping :P
      break;
   case vpad::Core::B:
      index = device->button_b;
      name = SDL_CONTROLLER_BUTTON_A;
      break;
   case vpad::Core::X:
      index = device->button_x;
      name = SDL_CONTROLLER_BUTTON_Y;
      break;
   case vpad::Core::Y:
      index = device->button_y;
      name = SDL_CONTROLLER_BUTTON_X;
      break;
   case vpad::Core::TriggerR:
      index = device->button_trigger_r;
      name = SDL_CONTROLLER_BUTTON_RIGHTSHOULDER;
      break;
   case vpad::Core::TriggerL:
      index = device->button_trigger_l;
      name = SDL_CONTROLLER_BUTTON_LEFTSHOULDER;
      break;
   case vpad::Core::TriggerZR:
      index = device->button_trigger_zr;
      axisName = SDL_CONTROLLER_AXIS_TRIGGERRIGHT;
      break;
   case vpad::Core::TriggerZL:
      index = device->button_trigger_zl;
      axisName = SDL_CONTROLLER_AXIS_TRIGGERLEFT;
      break;
   case vpad::Core::LeftStick:
      index = device->button_stick_l;
      name = SDL_CONTROLLER_BUTTON_LEFTSTICK;
      break;
   case vpad::Core::RightStick:
      index = device->button_stick_r;
      name = SDL_CONTROLLER_BUTTON_RIGHTSTICK;
      break;
   case vpad::Core::Plus:
      index = device->button_plus;
      name = SDL_CONTROLLER_BUTTON_START;
      break;
   case vpad::Core::Minus:
      index = device->button_minus;
      name = SDL_CONTROLLER_BUTTON_BACK;
      break;
   case vpad::Core::Home:
      index = device->button_home;
      name = SDL_CONTROLLER_BUTTON_GUIDE;
      break;
   case vpad::Core::Sync:
      index = device->button_sync;
      break;
   }

   if (index >= 0) {
      return !!SDL_JoystickGetButton(joystick, index);
   } else if (index == -2) {
      if (name != SDL_CONTROLLER_BUTTON_INVALID) {
         return !!SDL_GameControllerGetButton(controller, name);
      } else if (axisName != SDL_CONTROLLER_AXIS_INVALID) {  // ZL/ZR kludge
         int value = SDL_GameControllerGetAxis(controller, axisName);
         return value >= 16384;
      }
   }

   return false;
}
Ejemplo n.º 20
0
	s32 proc()
	{
#if _WIN32
		SetProcessDPIAware();
#endif

#if defined(__APPLE__)
		SDL_SetHint(SDL_HINT_MOUSE_RELATIVE_MODE_WARP, "1");
#endif

		// Initialize SDL
		if (SDL_Init(
			SDL_INIT_VIDEO
			| SDL_INIT_EVENTS
			| SDL_INIT_GAMECONTROLLER
			| SDL_INIT_HAPTIC
			| SDL_INIT_TIMER
			| SDL_INIT_JOYSTICK
		) < 0)
		{
			fprintf(stderr, "Failed to initialize SDL: %s\n", SDL_GetError());
			return -1;
		}

		Loader::data_directory = SDL_GetPrefPath("HelveticaScenario", "Yearning");

		SDL_GameControllerAddMappingsFromFile("gamecontrollerdb.txt");

		SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
		SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
		SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
		SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);

		{
			SDL_DisplayMode display;
			SDL_GetDesktopDisplayMode(0, &display);
			Loader::settings_load(display.w, display.h);
		}

		if (SDL_SetRelativeMouseMode(SDL_TRUE) != 0)
		{
			fprintf(stderr, "Failed to set relative mouse mode: %s\n", SDL_GetError());
			return -1;
		}

		window = SDL_CreateWindow
		(
			"The Yearning",
			0,
			0,
			Settings::width, Settings::height,
			SDL_WINDOW_OPENGL
			| SDL_WINDOW_SHOWN
			| SDL_WINDOW_INPUT_GRABBED
			| SDL_WINDOW_INPUT_FOCUS
			| SDL_WINDOW_MOUSE_FOCUS
			| SDL_WINDOW_MOUSE_CAPTURE
			| SDL_WINDOW_ALLOW_HIGHDPI
			| (Settings::fullscreen ? SDL_WINDOW_FULLSCREEN : SDL_WINDOW_BORDERLESS)
		);

#if defined(__APPLE__)
		SDL_SetWindowGrab(window, SDL_TRUE);
#endif

		// Open a window and create its OpenGL context
		if (!window)
		{
			fprintf(stderr, "Failed to open SDL window. Most likely your GPU is out of date! %s\n", SDL_GetError());
			SDL_Quit();
			return -1;
		}

		SDL_GLContext context = SDL_GL_CreateContext(window);
		if (!context)
		{
			fprintf(stderr, "Failed to create GL context: %s\n", SDL_GetError());
			return -1;
		}

		if (SDL_GL_SetSwapInterval(Settings::vsync ? 1 : 0) != 0)
		{
			fprintf(stderr, "Failed to set OpenGL swap interval: %s\n", SDL_GetError());
			return -1;
		}

		{
			glewExperimental = true; // Needed for core profile

			GLenum glew_result = glewInit();
			if (glew_result != GLEW_OK)
			{
				fprintf(stderr, "Failed to initialize GLEW: %s\n", glewGetErrorString(glew_result));
				return -1;
			}
		}

		glGetError(); // Clear initial error caused by GLEW

		render_init();

		// Launch threads

		Sync<LoopSync> render_sync;

		LoopSwapper swapper_render_update = render_sync.swapper(0);
		LoopSwapper swapper_render = render_sync.swapper(1);

		Sync<PhysicsSync, 1> physics_sync;

		PhysicsSwapper swapper_physics = physics_sync.swapper();
		PhysicsSwapper swapper_physics_update = physics_sync.swapper();

		std::thread thread_physics(Physics::loop, &swapper_physics);

		std::thread thread_update(Loop::loop, &swapper_render_update, &swapper_physics_update);

		std::thread thread_ai(AI::loop);

		LoopSync* sync = swapper_render.get();

		r64 last_time = SDL_GetTicks() / 1000.0;

		b8 has_focus = true;

		SDL_PumpEvents();

		const u8* sdl_keys = SDL_GetKeyboardState(0);

		refresh_controllers();

		while (true)
		{
			render(sync);

			// Swap buffers
			SDL_GL_SwapWindow(window);

			SDL_PumpEvents();

			memcpy(sync->input.keys, sdl_keys, sizeof(sync->input.keys));

			sync->input.keys[(s32)KeyCode::MouseWheelDown] = false;
			sync->input.keys[(s32)KeyCode::MouseWheelUp] = false;

			SDL_Event sdl_event;
			while (SDL_PollEvent(&sdl_event))
			{
				if (sdl_event.type == SDL_QUIT)
					sync->quit = true;
				else if (sdl_event.type == SDL_MOUSEWHEEL)
				{ 
					b8 up = sdl_event.wheel.y > 0;
					if (sdl_event.wheel.direction == SDL_MOUSEWHEEL_FLIPPED)
						up = !up;
					if (up)
						sync->input.keys[(s32)KeyCode::MouseWheelUp] = true;
					else
						sync->input.keys[(s32)KeyCode::MouseWheelDown] = true;
				} 
				else if (sdl_event.type == SDL_JOYDEVICEADDED
					|| sdl_event.type == SDL_JOYDEVICEREMOVED)
					refresh_controllers();
				else if (sdl_event.type == SDL_WINDOWEVENT)
				{
					if (sdl_event.window.event == SDL_WINDOWEVENT_FOCUS_GAINED)
						has_focus = true;
					else if (sdl_event.window.event == SDL_WINDOWEVENT_FOCUS_LOST)
						has_focus = false;
				}
			}

			sync->input.focus = has_focus;

			s32 mouse_buttons = SDL_GetRelativeMouseState(&sync->input.cursor_x, &sync->input.cursor_y);

			sync->input.keys[(s32)KeyCode::MouseLeft] = mouse_buttons & (1 << 0);
			sync->input.keys[(s32)KeyCode::MouseMiddle] = mouse_buttons & (1 << 1);
			sync->input.keys[(s32)KeyCode::MouseRight] = mouse_buttons & (1 << 2);

			s32 active_gamepads = 0;
			for (s32 i = 0; i < MAX_GAMEPADS; i++)
			{
				SDL_GameController* controller = controllers[i];
				Gamepad* gamepad = &sync->input.gamepads[i];
				gamepad->active = controller != 0;
				gamepad->btns = 0;
				if (gamepad->active)
				{
					gamepad->left_x = (r32)SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_LEFTX) / 32767.0f;
					gamepad->left_y = (r32)SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_LEFTY) / 32767.0f;
					gamepad->right_x = (r32)SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_RIGHTX) / 32767.0f;
					gamepad->right_y = (r32)SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_RIGHTY) / 32767.0f;
					gamepad->left_trigger = (r32)SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_TRIGGERLEFT) / 32767.0f;
					gamepad->right_trigger = (r32)SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_TRIGGERRIGHT) / 32767.0f;
					if (gamepad->left_trigger > 0.5f)
						gamepad->btns |= Gamepad::Btn::LeftTrigger;
					if (gamepad->right_trigger > 0.5f)
						gamepad->btns |= Gamepad::Btn::RightTrigger;
					if (SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_LEFTSHOULDER))
						gamepad->btns |= Gamepad::Btn::LeftShoulder;
					if (SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER))
						gamepad->btns |= Gamepad::Btn::RightShoulder;
					if (SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_LEFTSTICK))
						gamepad->btns |= Gamepad::Btn::LeftClick;
					if (SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_RIGHTSTICK))
						gamepad->btns |= Gamepad::Btn::RightClick;
					if (SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_A))
						gamepad->btns |= Gamepad::Btn::A;
					if (SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_B))
						gamepad->btns |= Gamepad::Btn::B;
					if (SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_X))
						gamepad->btns |= Gamepad::Btn::X;
					if (SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_Y))
						gamepad->btns |= Gamepad::Btn::Y;
					if (SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_BACK))
						gamepad->btns |= Gamepad::Btn::Back;
					if (SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_START))
						gamepad->btns |= Gamepad::Btn::Start;
					if (SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_DPAD_UP))
						gamepad->btns |= Gamepad::Btn::DUp;
					if (SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_DPAD_DOWN))
						gamepad->btns |= Gamepad::Btn::DDown;
					if (SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_DPAD_LEFT))
						gamepad->btns |= Gamepad::Btn::DLeft;
					if (SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_DPAD_RIGHT))
						gamepad->btns |= Gamepad::Btn::DRight;
					if (gamepad->rumble > 0.0f)
						SDL_HapticRumblePlay(haptics[i], gamepad->rumble, 33);
					gamepad->rumble = 0.0f;
					active_gamepads++;
				}
				else
				{
					gamepad->left_x = 0.0f;
					gamepad->left_y = 0.0f;
					gamepad->right_x = 0.0f;
					gamepad->right_y = 0.0f;
					gamepad->left_trigger = 0.0f;
					gamepad->right_trigger = 0.0f;
					gamepad->rumble = 0.0f;
				}
			}

			SDL_GetWindowSize(window, &sync->input.width, &sync->input.height);

			r64 time = (SDL_GetTicks() / 1000.0);
			sync->time.total = (r32)time;
			sync->time.delta = vi_min((r32)(time - last_time), 0.25f);
			last_time = time;

			b8 quit = sync->quit;

			sync = swapper_render.swap<SwapType_Read>();

			if (quit || sync->quit)
				break;
		}

		AI::quit();

		thread_update.join();
		thread_physics.join();
		thread_ai.join();

		SDL_GL_DeleteContext(context);
		SDL_DestroyWindow(window);

		// SDL sucks
		for (s32 i = 0; i < MAX_GAMEPADS; i++)
		{
			if (haptics[i])
				SDL_HapticClose(haptics[i]);
		}

		SDL_Quit();

		return 0;
	}
Ejemplo n.º 21
0
/*
* IN_SDL_JoyCommands
*
* SDL game controller code called in IN_Commands.
*/
void IN_SDL_JoyCommands( void )
{
	int i, buttons = 0, buttonsDiff;
	static int buttonsOld;
	const int keys[] =
	{
		K_A_BUTTON, K_B_BUTTON, K_X_BUTTON, K_Y_BUTTON, K_ESCAPE, 0, 0,
		K_LSTICK, K_RSTICK, K_LSHOULDER, K_RSHOULDER,
		K_DPAD_UP, K_DPAD_DOWN, K_DPAD_LEFT, K_DPAD_RIGHT,
		K_LTRIGGER, K_RTRIGGER
	};

	if( in_sdl_joyInitialized )
	{
		SDL_GameControllerUpdate();

		if( in_sdl_joyController && !SDL_GameControllerGetAttached( in_sdl_joyController ) )
		{
			SDL_GameControllerClose( in_sdl_joyController );
			in_sdl_joyController = NULL;
		}

		if( !in_sdl_joyController )
		{
			int num = SDL_NumJoysticks();

			for( i = 0; i < num; i++ )
			{
				in_sdl_joyController = SDL_GameControllerOpen( i );
				if( in_sdl_joyController )	
					break;
			}
		}
	}

	if( in_sdl_joyActive )
	{
		SDL_GameController *controller = in_sdl_joyController;
		if( controller )
		{
			for( i = 0; i < SDL_CONTROLLER_BUTTON_MAX; i++ )
			{
				if( keys[i] && SDL_GameControllerGetButton( controller, i ) )
					buttons |= 1 << i;
			}

			if( SDL_GameControllerGetButton( controller, SDL_CONTROLLER_BUTTON_START ) )
				buttons |= 1 << SDL_CONTROLLER_BUTTON_BACK;
			if( SDL_GameControllerGetAxis( controller, SDL_CONTROLLER_AXIS_TRIGGERLEFT ) > ( 30 * 128 ) )
				buttons |= 1 << SDL_CONTROLLER_BUTTON_MAX;
			if( SDL_GameControllerGetAxis( controller, SDL_CONTROLLER_AXIS_TRIGGERRIGHT ) > ( 30 * 128 ) )
				buttons |= 1 << ( SDL_CONTROLLER_BUTTON_MAX + 1 );
		}
	}

	buttonsDiff = buttons ^ buttonsOld;
	if( buttonsDiff )
	{
		unsigned int time = Sys_Milliseconds();

		for( i = 0; i < ( sizeof( keys ) / sizeof( keys[0] ) ); i++ )
		{
			if( buttonsDiff & ( 1 << i ) )
				Key_Event( keys[i], ( buttons & ( 1 << i ) ) ? true : false, time );
		}

		buttonsOld = buttons;
	}
}
Ejemplo n.º 22
0
CIControllerState SDL2EventHandler::getControllerState(uint16 index)
{
    if(!getSDL2Context())
        return {};

    SDL_GameController* gc = getSDL2Context()->controllers[index];
    if(!gc)
        return {};

    CIControllerState state;

    state.axes.e.l_x = SDL_GameControllerGetAxis(gc, SDL_CONTROLLER_AXIS_LEFTX);
    state.axes.e.l_y = SDL_GameControllerGetAxis(gc, SDL_CONTROLLER_AXIS_LEFTY);

    state.axes.e.r_x =
        SDL_GameControllerGetAxis(gc, SDL_CONTROLLER_AXIS_RIGHTX);
    state.axes.e.r_y =
        SDL_GameControllerGetAxis(gc, SDL_CONTROLLER_AXIS_RIGHTY);

    state.axes.e.t_l =
        SDL_GameControllerGetAxis(gc, SDL_CONTROLLER_AXIS_TRIGGERLEFT);
    state.axes.e.t_r =
        SDL_GameControllerGetAxis(gc, SDL_CONTROLLER_AXIS_TRIGGERRIGHT);

    state.buttons.e.a =
        SDL_GameControllerGetButton(gc, SDL_CONTROLLER_BUTTON_A);
    state.buttons.e.b =
        SDL_GameControllerGetButton(gc, SDL_CONTROLLER_BUTTON_B);
    state.buttons.e.x =
        SDL_GameControllerGetButton(gc, SDL_CONTROLLER_BUTTON_X);
    state.buttons.e.y =
        SDL_GameControllerGetButton(gc, SDL_CONTROLLER_BUTTON_Y);

    state.buttons.e.back =
        SDL_GameControllerGetButton(gc, SDL_CONTROLLER_BUTTON_BACK);
    state.buttons.e.guide =
        SDL_GameControllerGetButton(gc, SDL_CONTROLLER_BUTTON_GUIDE);
    state.buttons.e.start =
        SDL_GameControllerGetButton(gc, SDL_CONTROLLER_BUTTON_START);

    state.buttons.e.s_l =
        SDL_GameControllerGetButton(gc, SDL_CONTROLLER_BUTTON_LEFTSTICK);
    state.buttons.e.s_r =
        SDL_GameControllerGetButton(gc, SDL_CONTROLLER_BUTTON_RIGHTSTICK);

    state.buttons.e.b_l =
        SDL_GameControllerGetButton(gc, SDL_CONTROLLER_BUTTON_LEFTSHOULDER);
    state.buttons.e.b_r =
        SDL_GameControllerGetButton(gc, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER);

    state.buttons.e.p_up =
        SDL_GameControllerGetButton(gc, SDL_CONTROLLER_BUTTON_DPAD_UP);
    state.buttons.e.p_down =
        SDL_GameControllerGetButton(gc, SDL_CONTROLLER_BUTTON_DPAD_DOWN);
    state.buttons.e.p_left =
        SDL_GameControllerGetButton(gc, SDL_CONTROLLER_BUTTON_DPAD_LEFT);
    state.buttons.e.p_right =
        SDL_GameControllerGetButton(gc, SDL_CONTROLLER_BUTTON_DPAD_RIGHT);

    return state;
}
Ejemplo n.º 23
0
 bool is_pressed(button b) const noexcept {
     return SDL_GameControllerGetButton(ptr.get(), static_cast<SDL_GameControllerButton>(b));
 }
void loop(void *arg) {
  SDL_Event event;
  int i;
  SDL_GameController *gamecontroller = (SDL_GameController *)arg;

  /* blank screen, set up for drawing this frame. */
  SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE);
  SDL_RenderClear(screen);
  SDL_RenderCopy(screen, background, NULL, NULL);

  while (SDL_PollEvent(&event)) {
    switch (event.type) {
    case SDL_KEYDOWN:
      if (event.key.keysym.sym != SDLK_ESCAPE) {
        break;
      }
    /* Fall through to signal quit */
    case SDL_QUIT:
      done = SDL_TRUE;
      break;
    default:
      break;
    }
  }

  /* Update visual controller state */
  for (i = 0; i < SDL_CONTROLLER_BUTTON_MAX; ++i) {
    if (SDL_GameControllerGetButton(
            gamecontroller, (SDL_GameControllerButton)i) == SDL_PRESSED) {
      const SDL_Rect dst = {button_positions[i].x, button_positions[i].y, 50,
                            50};
      SDL_RenderCopyEx(screen, button, NULL, &dst, 0, NULL, 0);
    }
  }

  for (i = 0; i < SDL_CONTROLLER_AXIS_MAX; ++i) {
    const Sint16 deadzone = 8000; /* !!! FIXME: real deadzone */
    const Sint16 value =
        SDL_GameControllerGetAxis(gamecontroller, (SDL_GameControllerAxis)(i));
    if (value < -deadzone) {
      const SDL_Rect dst = {axis_positions[i].x, axis_positions[i].y, 50, 50};
      const double angle = axis_positions[i].angle;
      SDL_RenderCopyEx(screen, axis, NULL, &dst, angle, NULL, 0);
    } else if (value > deadzone) {
      const SDL_Rect dst = {axis_positions[i].x, axis_positions[i].y, 50, 50};
      const double angle = axis_positions[i].angle + 180.0;
      SDL_RenderCopyEx(screen, axis, NULL, &dst, angle, NULL, 0);
    }
  }

  SDL_RenderPresent(screen);

  if (!SDL_GameControllerGetAttached(gamecontroller)) {
    done = SDL_TRUE;
    retval = SDL_TRUE; /* keep going, wait for reattach. */
  }

#ifdef __EMSCRIPTEN__
  if (done) {
    emscripten_cancel_main_loop();
  }
#endif
}
/**
 * @brief Renderer::process_keys called each frame to process key inputs
 * @param keystate
 */
void Renderer::process_keys(const Uint8 *keystate){
    if(keystate[SDL_SCANCODE_A]){
        camera_position = camera_position - 0.05f*glm::vec3(glm::row(world_to_camera_matrix, 0));
    }
    if(keystate[SDL_SCANCODE_D]){
        camera_position = camera_position + 0.05f*glm::vec3(glm::row(world_to_camera_matrix, 0));
    }
    if(keystate[SDL_SCANCODE_W]){
        camera_position = camera_position - 0.05f*glm::vec3(glm::row(world_to_camera_matrix, 2));
    }
    if(keystate[SDL_SCANCODE_S]){
        camera_position = camera_position + 0.05f*glm::vec3(glm::row(world_to_camera_matrix, 2));
    }

    if(keystate[SDL_SCANCODE_LSHIFT]){
        camera_position[1] -= 0.05;
    }
    if(keystate[SDL_SCANCODE_SPACE]){
        camera_position[1] += 0.05;
    }
    if(keystate[SDL_SCANCODE_Q]){
        quit_flag = true;
    }

    if(gamepad != NULL){
        float left_axis_x = SDL_GameControllerGetAxis(gamepad, (SDL_GameControllerAxis)0)/32768.0;
        float left_axis_y = SDL_GameControllerGetAxis(gamepad, (SDL_GameControllerAxis)1)/32768.0;
        float right_axis_x = SDL_GameControllerGetAxis(gamepad, (SDL_GameControllerAxis)2)/32768.0;
        float right_axis_y = SDL_GameControllerGetAxis(gamepad, (SDL_GameControllerAxis)3)/32768.0;

        // xbox360 controllers need a surprisingly large deadzone. These values are
        // lifted from microsofts xinput.h.
        float XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE = 7849.0/32768.0;
        float XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE = 8689.0/32768.0;

        if(fabs(left_axis_x) < XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE){
            left_axis_x = 0.0f;
        }
        else {
            left_axis_x -= sgn(left_axis_x)*XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE;
        }
        if(fabs(left_axis_y) < XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE){
            left_axis_y = 0.0f;
        }
        else {
            left_axis_y -= sgn(left_axis_y)*XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE;
        }

        if(fabs(right_axis_x) < XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE){
            right_axis_x = 0.0f;
        }
        else {
            right_axis_x -= sgn(right_axis_x)*XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE;
        }
        if(fabs(right_axis_y) < XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE){
            right_axis_y = 0.0f;
        }
        else {
            right_axis_y -= sgn(right_axis_y)*XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE;
        }

        camera_horizontal -= 0.06*right_axis_x;
        camera_vertical -= 0.06*right_axis_y;
        if(camera_vertical < -M_PI/2.0)
            camera_vertical = -M_PI/2.0;
        if(camera_vertical > M_PI/2.0)
            camera_vertical = M_PI/2.0;
        camera_position = camera_position + left_axis_y*0.07f*glm::vec3(glm::row(world_to_camera_matrix, 2));
        camera_position = camera_position + left_axis_x*0.07f*glm::vec3(glm::row(world_to_camera_matrix, 0));

        Uint8 left_shoulder_button = SDL_GameControllerGetButton(gamepad, (SDL_GameControllerButton)9); // left shoulder button
        Uint8 right_shoulder_button = SDL_GameControllerGetButton(gamepad, (SDL_GameControllerButton)10); // right shoulder button
        Uint8 start_button = SDL_GameControllerGetButton(gamepad, (SDL_GameControllerButton)6); // start button
        if(left_shoulder_button == 1){
            camera_position[1] -= 0.04;
        }
        if(right_shoulder_button == 1){
            camera_position[1] += 0.04;
        }
        if(start_button == 1){
            quit_flag = true;
        }
    }
}
Ejemplo n.º 26
0
SDL_bool
WatchGameController(SDL_GameController * gamecontroller)
{
    /* This is indexed by SDL_GameControllerButton. */
    static const struct { int x; int y; } button_positions[] = {
        {387, 167},  /* A */
        {431, 132},  /* B */
        {342, 132},  /* X */
        {389, 101},  /* Y */
        {174, 132},  /* BACK */
        {233, 132},  /* GUIDE */
        {289, 132},  /* START */
        {75,  154},  /* LEFTSTICK */
        {305, 230},  /* RIGHTSTICK */
        {77,  40},   /* LEFTSHOULDER */
        {396, 36},   /* RIGHTSHOULDER */
        {154, 188},  /* DPAD_UP */
        {154, 249},  /* DPAD_DOWN */
        {116, 217},  /* DPAD_LEFT */
        {186, 217},  /* DPAD_RIGHT */
    };

    /* This is indexed by SDL_GameControllerAxis. */
    static const struct { int x; int y; double angle; } axis_positions[] = {
        {75,  154, 0.0},  /* LEFTX */
        {75,  154, 90.0},  /* LEFTY */
        {305, 230, 0.0},  /* RIGHTX */
        {305, 230, 90.0},  /* RIGHTY */
        {91, 0, 90.0},     /* TRIGGERLEFT */
        {375, 0, 90.0},    /* TRIGGERRIGHT */
    };

    const char *name = SDL_GameControllerName(gamecontroller);
    const char *basetitle = "Game Controller Test: ";
    const size_t titlelen = SDL_strlen(basetitle) + SDL_strlen(name) + 1;
    char *title = (char *)SDL_malloc(titlelen);
    SDL_Texture *background, *button, *axis;
    SDL_Window *window = NULL;
    SDL_Renderer *screen = NULL;
    SDL_bool retval = SDL_FALSE;
    SDL_bool done = SDL_FALSE;
    SDL_Event event;
    int i;

    if (title) {
        SDL_snprintf(title, titlelen, "%s%s", basetitle, name);
    }

    /* Create a window to display controller state */
    window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED,
                              SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
                              SCREEN_HEIGHT, 0);
    if (window == NULL) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
        return SDL_FALSE;
    }

    screen = SDL_CreateRenderer(window, -1, 0);
    if (screen == NULL) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
        SDL_DestroyWindow(window);
        return SDL_FALSE;
    }

    SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
    SDL_RenderClear(screen);
    SDL_RenderPresent(screen);
    SDL_RaiseWindow(window);

    /* scale for platforms that don't give you the window size you asked for. */
    SDL_RenderSetLogicalSize(screen, SCREEN_WIDTH, SCREEN_HEIGHT);

    background = LoadTexture(screen, "controllermap.bmp", SDL_FALSE);
    button = LoadTexture(screen, "button.bmp", SDL_TRUE);
    axis = LoadTexture(screen, "axis.bmp", SDL_TRUE);

    if (!background || !button || !axis) {
        SDL_DestroyRenderer(screen);
        SDL_DestroyWindow(window);
        return SDL_FALSE;
    }
    SDL_SetTextureColorMod(button, 10, 255, 21);
    SDL_SetTextureColorMod(axis, 10, 255, 21);

    /* !!! FIXME: */
    /*SDL_RenderSetLogicalSize(screen, background->w, background->h);*/

    /* Print info about the controller we are watching */
    SDL_Log("Watching controller %s\n",  name ? name : "Unknown Controller");

    /* Loop, getting controller events! */
    while (!done) {
        /* blank screen, set up for drawing this frame. */
        SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE);
        SDL_RenderClear(screen);
        SDL_RenderCopy(screen, background, NULL, NULL);

        while (SDL_PollEvent(&event)) {
            switch (event.type) {
            case SDL_KEYDOWN:
                if (event.key.keysym.sym != SDLK_ESCAPE) {
                    break;
                }
                /* Fall through to signal quit */
            case SDL_QUIT:
                done = SDL_TRUE;
                break;
            default:
                break;
            }
        }

        /* Update visual controller state */
        for (i = 0; i < SDL_CONTROLLER_BUTTON_MAX; ++i) {
            if (SDL_GameControllerGetButton(gamecontroller, (SDL_GameControllerButton)i) == SDL_PRESSED) {
                const SDL_Rect dst = { button_positions[i].x, button_positions[i].y, 50, 50 };
                SDL_RenderCopyEx(screen, button, NULL, &dst, 0, NULL, 0);
            }
        }

        for (i = 0; i < SDL_CONTROLLER_AXIS_MAX; ++i) {
            const Sint16 deadzone = 8000;  /* !!! FIXME: real deadzone */
            const Sint16 value = SDL_GameControllerGetAxis(gamecontroller, (SDL_GameControllerAxis)(i));
            if (value < -deadzone) {
                const SDL_Rect dst = { axis_positions[i].x, axis_positions[i].y, 50, 50 };
                const double angle = axis_positions[i].angle;
                SDL_RenderCopyEx(screen, axis, NULL, &dst, angle, NULL, 0);
            } else if (value > deadzone) {
                const SDL_Rect dst = { axis_positions[i].x, axis_positions[i].y, 50, 50 };
                const double angle = axis_positions[i].angle + 180.0;
                SDL_RenderCopyEx(screen, axis, NULL, &dst, angle, NULL, 0);
            }
        }

        SDL_RenderPresent(screen);

        if (!SDL_GameControllerGetAttached(gamecontroller)) {
            done = SDL_TRUE;
            retval = SDL_TRUE;  /* keep going, wait for reattach. */
        }
    }

    SDL_DestroyRenderer(screen);
    SDL_DestroyWindow(window);
    return retval;
}
Ejemplo n.º 27
0
/*
===========
IN_Commands
===========
*/
void IN_Commands (void)
{
	int		i, key_index;

	if (!joy_avail)
	{
		return;
	}

	DWORD	buttonstate, povstate;
	
	// loop through the joystick buttons
	// key a joystick event or auxillary event for higher number buttons for each state change
	buttonstate = 0;
	for ( i = 0; i < SDL_CONTROLLER_BUTTON_MAX; i++ )
	{
		if ( SDL_GameControllerGetButton( s_pJoystick, (SDL_GameControllerButton)i ) )
		{
			buttonstate |= 1<<i;
		}
	}
	
	for (i = 0; i < JOY_MAX_AXES; i++)
	{
		pdwRawValue[i] = RawValuePointer(i);
	}

	for (i=0 ; i < (int)joy_numbuttons ; i++)
	{
		if ( (buttonstate & (1<<i)) && !(joy_oldbuttonstate & (1<<i)) )
		{
			key_index = (i < 4) ? K_JOY1 : K_AUX1;
			gEngfuncs.Key_Event (key_index + i, 1);
		}

		if ( !(buttonstate & (1<<i)) && (joy_oldbuttonstate & (1<<i)) )
		{
			key_index = (i < 4) ? K_JOY1 : K_AUX1;
			gEngfuncs.Key_Event (key_index + i, 0);
		}
	}
	joy_oldbuttonstate = buttonstate;

	if (joy_haspov)
	{
		// convert POV information into 4 bits of state information
		// this avoids any potential problems related to moving from one
		// direction to another without going through the center position
		povstate = 0;
		// determine which bits have changed and key an auxillary event for each change
		for (i=0 ; i < 4 ; i++)
		{
			if ( (povstate & (1<<i)) && !(joy_oldpovstate & (1<<i)) )
			{
				gEngfuncs.Key_Event (K_AUX29 + i, 1);
			}

			if ( !(povstate & (1<<i)) && (joy_oldpovstate & (1<<i)) )
			{
				gEngfuncs.Key_Event (K_AUX29 + i, 0);
			}
		}
		joy_oldpovstate = povstate;
	}
}
Ejemplo n.º 28
0
	int GameController::getButton(State & state, SDL_GameController * gamecontroller){
		Stack * stack = state.stack;
		stack->push<int>(SDL_GameControllerGetButton(gamecontroller, static_cast<SDL_GameControllerButton>(stack->to<int>(1))));
		return 1;
	}
Ejemplo n.º 29
0
void
WatchGameController(SDL_GameController * gamecontroller)
{
    SDL_Window *window = NULL;
    SDL_Renderer *screen = NULL;
    const char *name = NULL;
    int done = 0;
    SDL_Event event;
    int i;

    /* Create a window to display controller axis position */
    window = SDL_CreateWindow("Game Controller Test", SDL_WINDOWPOS_CENTERED,
                              SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
                              SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
    if (window == NULL) {
        fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
        return;
    }

    screen = SDL_CreateRenderer(window, -1, 0);
    if (screen == NULL) {
        fprintf(stderr, "Couldn't create renderer: %s\n", SDL_GetError());
        SDL_DestroyWindow(window);
        return;
    }

    SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
    SDL_RenderClear(screen);
    SDL_RenderPresent(screen);
	SDL_RaiseWindow(window);

    /* Print info about the controller we are watching */
    name = SDL_GameControllerName(gamecontroller);
    printf("Watching controller %s\n",  name ? name : "Unknown Controller");
    
    /* Loop, getting controller events! */
    while (!done) {
        /* blank screen, set up for drawing this frame. */
        SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
        SDL_RenderClear(screen);

        while (SDL_PollEvent(&event)) {
            switch (event.type) {
            case SDL_CONTROLLERAXISMOTION:
                printf("Controller %d axis %d value: %d\n",
                       event.caxis.which,
                       event.caxis.axis, event.caxis.value);
                break;
            case SDL_CONTROLLERBUTTONDOWN:
                printf("Controller %d button %d down\n",
                       event.cbutton.which, event.cbutton.button);
                break;
            case SDL_CONTROLLERBUTTONUP:
                printf("Controller %d button %d up\n",
                       event.cbutton.which, event.cbutton.button);
                break;
            case SDL_KEYDOWN:
                if (event.key.keysym.sym != SDLK_ESCAPE) {
                    break;
                }
                /* Fall through to signal quit */
            case SDL_QUIT:
                done = 1;
				s_ForceQuit = SDL_TRUE;
                break;
            default:
                break;
            }
        }
        /* Update visual controller state */
        SDL_SetRenderDrawColor(screen, 0x00, 0xFF, 0x00, SDL_ALPHA_OPAQUE);
        for (i = 0; i <SDL_CONTROLLER_BUTTON_MAX; ++i) {
            if (SDL_GameControllerGetButton(gamecontroller, i) == SDL_PRESSED) {
                DrawRect(screen, i * 34, SCREEN_HEIGHT - 34, 32, 32);
            }
        }

        SDL_SetRenderDrawColor(screen, 0xFF, 0x00, 0x00, SDL_ALPHA_OPAQUE);
        for (i = 0; i < SDL_CONTROLLER_AXIS_MAX / 2; ++i) {
            /* Draw the X/Y axis */
            int x, y;
            x = (((int) SDL_GameControllerGetAxis(gamecontroller, i * 2 + 0)) + 32768);
            x *= SCREEN_WIDTH;
            x /= 65535;
            if (x < 0) {
                x = 0;
            } else if (x > (SCREEN_WIDTH - 16)) {
                x = SCREEN_WIDTH - 16;
            }
            y = (((int) SDL_GameControllerGetAxis(gamecontroller, i * 2 + 1)) + 32768);
            y *= SCREEN_HEIGHT;
            y /= 65535;
            if (y < 0) {
                y = 0;
            } else if (y > (SCREEN_HEIGHT - 16)) {
                y = SCREEN_HEIGHT - 16;
            }

            DrawRect(screen, x, y, 16, 16);
        }

        SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0xFF, SDL_ALPHA_OPAQUE);

        SDL_RenderPresent(screen);
		
		if ( !done )
			done = SDL_GameControllerGetAttached( gamecontroller ) == 0;
    }

    SDL_DestroyRenderer(screen);
    SDL_DestroyWindow(window);
}
Ejemplo n.º 30
0
void GeneralManager::processPlayerInputs(){
	for (int i = 0; i < _players.size(); i++) {	
		if(SDL_GameControllerGetButton(_players[i].getController(),SDL_CONTROLLER_BUTTON_A) == 1) {
			_players[i].pressKey(SDL_CONTROLLER_BUTTON_A);
		} else {
			_players[i].releaseKey(SDL_CONTROLLER_BUTTON_A);
		}

		if(SDL_GameControllerGetButton(_players[i].getController(),SDL_CONTROLLER_BUTTON_B) == 1) {
			_players[i].pressKey(SDL_CONTROLLER_BUTTON_B);
		} else {
			_players[i].releaseKey(SDL_CONTROLLER_BUTTON_B);
		}

		if(SDL_GameControllerGetButton(_players[i].getController(),SDL_CONTROLLER_BUTTON_X) == 1) {
			_players[i].pressKey(SDL_CONTROLLER_BUTTON_X);
		} else {
			_players[i].releaseKey(SDL_CONTROLLER_BUTTON_X);
		}

		if(SDL_GameControllerGetButton(_players[i].getController(),SDL_CONTROLLER_BUTTON_Y) == 1) {
			_players[i].pressKey(SDL_CONTROLLER_BUTTON_Y);
		} else {
			_players[i].releaseKey(SDL_CONTROLLER_BUTTON_Y);
		}

		if(SDL_GameControllerGetButton(_players[i].getController(),SDL_CONTROLLER_BUTTON_BACK) == 1) {
			_players[i].pressKey(SDL_CONTROLLER_BUTTON_BACK);
		} else {
			_players[i].releaseKey(SDL_CONTROLLER_BUTTON_BACK);
		}

		if(SDL_GameControllerGetButton(_players[i].getController(),SDL_CONTROLLER_BUTTON_GUIDE) == 1) {
			_players[i].pressKey(SDL_CONTROLLER_BUTTON_GUIDE);
		} else {
			_players[i].releaseKey(SDL_CONTROLLER_BUTTON_GUIDE);
		}

		if(SDL_GameControllerGetButton(_players[i].getController(),SDL_CONTROLLER_BUTTON_START) == 1) {
			_players[i].pressKey(SDL_CONTROLLER_BUTTON_START);
		} else {
			_players[i].releaseKey(SDL_CONTROLLER_BUTTON_START);
		}

		if(SDL_GameControllerGetButton(_players[i].getController(),SDL_CONTROLLER_BUTTON_LEFTSTICK) == 1) {
			_players[i].pressKey(SDL_CONTROLLER_BUTTON_LEFTSTICK);
		} else {
			_players[i].releaseKey(SDL_CONTROLLER_BUTTON_LEFTSTICK);
		}

		if(SDL_GameControllerGetButton(_players[i].getController(),SDL_CONTROLLER_BUTTON_RIGHTSTICK) == 1) {
			_players[i].pressKey(SDL_CONTROLLER_BUTTON_RIGHTSTICK);
		} else {
			_players[i].releaseKey(SDL_CONTROLLER_BUTTON_RIGHTSTICK);
		}

		if(SDL_GameControllerGetButton(_players[i].getController(),SDL_CONTROLLER_BUTTON_LEFTSHOULDER) == 1) {
			_players[i].pressKey(SDL_CONTROLLER_BUTTON_LEFTSHOULDER);
		} else {
			_players[i].releaseKey(SDL_CONTROLLER_BUTTON_LEFTSHOULDER);
		}

		if(SDL_GameControllerGetButton(_players[i].getController(),SDL_CONTROLLER_BUTTON_RIGHTSHOULDER) == 1) {
			_players[i].pressKey(SDL_CONTROLLER_BUTTON_RIGHTSHOULDER);
		} else {
			_players[i].releaseKey(SDL_CONTROLLER_BUTTON_RIGHTSHOULDER);
		}

		if(SDL_GameControllerGetButton(_players[i].getController(),SDL_CONTROLLER_BUTTON_DPAD_UP) == 1) {
			_players[i].pressKey(SDL_CONTROLLER_BUTTON_DPAD_UP);
		} else {
			_players[i].releaseKey(SDL_CONTROLLER_BUTTON_DPAD_UP);
		}

		if(SDL_GameControllerGetButton(_players[i].getController(),SDL_CONTROLLER_BUTTON_DPAD_DOWN) == 1) {
			_players[i].pressKey(SDL_CONTROLLER_BUTTON_DPAD_DOWN);
		} else {
			_players[i].releaseKey(SDL_CONTROLLER_BUTTON_DPAD_DOWN);
		}

		if(SDL_GameControllerGetButton(_players[i].getController(),SDL_CONTROLLER_BUTTON_DPAD_LEFT) == 1) {
			_players[i].pressKey(SDL_CONTROLLER_BUTTON_DPAD_LEFT);
		} else {
			_players[i].releaseKey(SDL_CONTROLLER_BUTTON_DPAD_LEFT);
		}


		if(SDL_GameControllerGetButton(_players[i].getController(),SDL_CONTROLLER_BUTTON_DPAD_RIGHT) == 1) {
			_players[i].pressKey(SDL_CONTROLLER_BUTTON_DPAD_RIGHT);
		} else {
			_players[i].releaseKey(SDL_CONTROLLER_BUTTON_DPAD_RIGHT);
		}
	}
}