2020-10-15 12:55:27 +01:00
|
|
|
#include "StdAfx.h"
|
2020-12-23 19:25:25 +00:00
|
|
|
#include "frontends/sdl/gamepad.h"
|
2020-10-15 12:55:27 +01:00
|
|
|
|
|
|
|
#include "Log.h"
|
|
|
|
|
|
|
|
#define AXIS_MIN -32768
|
|
|
|
#define AXIS_MAX 32767
|
|
|
|
|
|
|
|
|
2021-02-25 16:31:24 +00:00
|
|
|
namespace sa2
|
2020-10-15 12:55:27 +01:00
|
|
|
{
|
|
|
|
|
2021-02-25 16:31:24 +00:00
|
|
|
Gamepad::Gamepad(const int index)
|
|
|
|
: myButtonCodes(2), myAxisCodes(2)
|
2020-10-15 12:55:27 +01:00
|
|
|
{
|
2021-02-25 16:31:24 +00:00
|
|
|
myController.reset(SDL_GameControllerOpen(index), SDL_GameControllerClose);
|
|
|
|
myButtonCodes[0] = SDL_CONTROLLER_BUTTON_A;
|
|
|
|
myButtonCodes[1] = SDL_CONTROLLER_BUTTON_B;
|
|
|
|
myAxisCodes[0] = SDL_CONTROLLER_AXIS_LEFTX;
|
|
|
|
myAxisCodes[1] = SDL_CONTROLLER_AXIS_LEFTY;
|
2020-10-15 12:55:27 +01:00
|
|
|
}
|
|
|
|
|
2021-02-25 16:31:24 +00:00
|
|
|
bool Gamepad::getButton(int i) const
|
2020-10-15 12:55:27 +01:00
|
|
|
{
|
2021-02-25 16:31:24 +00:00
|
|
|
int value = 0;
|
|
|
|
if (myController)
|
|
|
|
{
|
|
|
|
value = SDL_GameControllerGetButton(myController.get(), myButtonCodes[i]);
|
|
|
|
}
|
|
|
|
return value != 0;
|
2020-10-15 12:55:27 +01:00
|
|
|
}
|
2021-02-25 16:31:24 +00:00
|
|
|
|
|
|
|
double Gamepad::getAxis(int i) const
|
2020-10-15 12:55:27 +01:00
|
|
|
{
|
2021-02-25 16:31:24 +00:00
|
|
|
if (myController)
|
|
|
|
{
|
|
|
|
const int value = SDL_GameControllerGetAxis(myController.get(), myAxisCodes[i]);
|
|
|
|
const double axis = 2.0 * double(value - AXIS_MIN) / double(AXIS_MAX - AXIS_MIN) - 1.0;
|
|
|
|
return axis;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0.0;
|
|
|
|
}
|
2020-10-15 12:55:27 +01:00
|
|
|
}
|
2021-02-25 16:31:24 +00:00
|
|
|
|
2020-10-15 12:55:27 +01:00
|
|
|
}
|