- [NEW] Afegit soport per a gamepad en JInput
This commit is contained in:
@@ -62,6 +62,12 @@ int main(int argc, char *argv[])
|
||||
{
|
||||
input::updateWheel(e.wheel.y);
|
||||
}
|
||||
if (e.type==SDL_CONTROLLERBUTTONDOWN) {
|
||||
input::updatePadBtn(e.cbutton.button);
|
||||
}
|
||||
if (e.type==SDL_CONTROLLERBUTTONUP) {
|
||||
input::updatePadBtnPressed(e.cbutton.button);
|
||||
}
|
||||
}
|
||||
|
||||
if (SDL_GetTicks()-current_ticks >= game::ticks_per_frame)
|
||||
@@ -71,6 +77,8 @@ int main(int argc, char *argv[])
|
||||
input::updateKeypressed(SDL_SCANCODE_UNKNOWN);
|
||||
input::updateClk(0);
|
||||
input::updateWheel(0);
|
||||
input::updatePadBtn(SDL_CONTROLLER_BUTTON_INVALID);
|
||||
input::updatePadBtnPressed(SDL_CONTROLLER_BUTTON_INVALID);
|
||||
current_ticks = SDL_GetTicks();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,10 +9,30 @@ namespace input
|
||||
static uint8_t keydown = 0;
|
||||
static uint8_t btnClicked = 0;
|
||||
static int wheel = 0;
|
||||
static SDL_GameController *gamepad = NULL;
|
||||
static int8_t pad_btn_pressed = SDL_CONTROLLER_BUTTON_INVALID;
|
||||
static int8_t pad_btn_down = SDL_CONTROLLER_BUTTON_INVALID;
|
||||
|
||||
void initGamePad()
|
||||
{
|
||||
const int num_joysticks = SDL_NumJoysticks();
|
||||
if (num_joysticks>=1) {
|
||||
for (int i=0; i<num_joysticks; ++i) {
|
||||
if (SDL_IsGameController(i)) {
|
||||
gamepad = SDL_GameControllerOpen(i);
|
||||
if (SDL_GameControllerGetAttached(gamepad) == SDL_TRUE) {
|
||||
SDL_GameControllerEventState(SDL_ENABLE);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void init()
|
||||
{
|
||||
keys = SDL_GetKeyboardState(NULL);
|
||||
initGamePad();
|
||||
}
|
||||
|
||||
// Determina si la tecla especificada està sent polsada ara mateix
|
||||
@@ -45,6 +65,40 @@ namespace input
|
||||
return keypressed;
|
||||
}
|
||||
|
||||
|
||||
// Determina si el botó del pad especificat està sent polsat ara mateix
|
||||
bool padBtnDown(const int8_t btn)
|
||||
{
|
||||
if (!gamepad) return false;
|
||||
return SDL_GameControllerGetButton(gamepad, SDL_GameControllerButton(btn)) == 1;
|
||||
}
|
||||
|
||||
// Determina si el botó del pad especificat ha sigut polsat, pero no tornarà a ser true fins
|
||||
bool padBtnPressed(const int8_t btn)
|
||||
{
|
||||
return btn == pad_btn_pressed;
|
||||
}
|
||||
|
||||
// Determina si hi ha algun botó del pad polsat ara mateix
|
||||
bool anyPadBtn()
|
||||
{
|
||||
return pad_btn_down != SDL_CONTROLLER_BUTTON_INVALID;
|
||||
}
|
||||
|
||||
// Torna el codi del botó del pad que està sent polsat ara mateix
|
||||
const int8_t whichPadBtn()
|
||||
{
|
||||
return pad_btn_down;
|
||||
}
|
||||
|
||||
// Torna el codi del botó del pad que està sent polsat ara mateix
|
||||
const int8_t getPadBtnPressed()
|
||||
{
|
||||
return pad_btn_pressed;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// (US INTERN) Actualitza la tecla actualment polsada (keydown) desde jgame
|
||||
void updateKey(const uint8_t key)
|
||||
{
|
||||
@@ -69,6 +123,18 @@ namespace input
|
||||
wheel = dy;
|
||||
}
|
||||
|
||||
// (US INTERN) Actualitza el botó actualment polsat (keydown) desde jgame
|
||||
void updatePadBtn(const int8_t btn)
|
||||
{
|
||||
pad_btn_down = btn;
|
||||
}
|
||||
|
||||
// (US INTERN) Actualitza el botó actualment polsat (keypress) desde jgame
|
||||
void updatePadBtnPressed(const int8_t btn)
|
||||
{
|
||||
pad_btn_pressed = btn;
|
||||
}
|
||||
|
||||
// Torna la posició X actual del ratolí
|
||||
const int mouseX()
|
||||
{
|
||||
|
||||
@@ -30,6 +30,32 @@ namespace input
|
||||
/// @return Quina tecla està sent polsada
|
||||
const uint8_t getKeyPressed();
|
||||
|
||||
|
||||
/// @brief Determina si el botó del pad especificat està sent polsat ara mateix
|
||||
/// @param btn botó del pad a consultar
|
||||
/// @return true si està polsat, false si no
|
||||
bool padBtnDown(const int8_t btn);
|
||||
|
||||
/// @brief Determina si el botó del pad especificat ha sigut polsat, pero no tornarà a ser true fins
|
||||
/// @brief que no se solte el botó i se torne a polsar.
|
||||
/// @param btn botó del pad a consultar
|
||||
/// @return true si està polsat, false si no
|
||||
bool padBtnPressed(const int8_t btn);
|
||||
|
||||
/// @brief Determina si hi ha algun botó del pad polsat ara mateix
|
||||
/// @return true si hi ha algun botó del pad polsat, false si no
|
||||
bool anyPadBtn();
|
||||
|
||||
/// @brief Torna el codi del botó del pad que està sent polsat ara mateix
|
||||
/// @return Quina botó del pad està sent polsat
|
||||
const int8_t whichPadBtn();
|
||||
|
||||
/// @brief Torna el codi del botó del pad que està sent polsat ara mateix
|
||||
/// @brief (nomes una vegada, com padBtnPressed)
|
||||
/// @return Quina botó del pad està sent polsat
|
||||
const int8_t getPadBtnPressed();
|
||||
|
||||
|
||||
/// @brief (US INTERN) Actualitza la tecla actualment polsada (keydown) desde jgame
|
||||
/// @param key tecla polsada
|
||||
void updateKey(const uint8_t key);
|
||||
@@ -46,6 +72,14 @@ namespace input
|
||||
/// @param dy desplaçament de la rodeta
|
||||
void updateWheel(const int dy);
|
||||
|
||||
/// @brief (US INTERN) Actualitza el botó actualment polsat (keydown) desde jgame
|
||||
/// @param btn botó polsat
|
||||
void updatePadBtn(const int8_t btn);
|
||||
|
||||
/// @brief (US INTERN) Actualitza el botó actualment polsat (keypress) desde jgame
|
||||
/// @param btn botó polsat
|
||||
void updatePadBtnPressed(const int8_t btn);
|
||||
|
||||
/// @brief Torna la posició X actual del ratolí
|
||||
/// @return valor de la coordenada X del ratolí
|
||||
const int mouseX();
|
||||
|
||||
Reference in New Issue
Block a user