[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[adonthell-commits] master fd8c543 3/4: ADDED support for gamecontroller
From: |
Kai Sterker |
Subject: |
[adonthell-commits] master fd8c543 3/4: ADDED support for gamecontrollers (recognized by SDL) |
Date: |
Sat, 30 Sep 2017 18:27:32 -0400 (EDT) |
branch: master
commit fd8c5435e4d8328bbe5d0d045d1f253323fe958d
Author: Kai Sterker <address@hidden>
Commit: Kai Sterker <address@hidden>
ADDED support for gamecontrollers (recognized by SDL)
---
src/input.cc | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
src/input.h | 4 +++
2 files changed, 117 insertions(+), 1 deletion(-)
diff --git a/src/input.cc b/src/input.cc
index 7dc44fa..1b319f0 100644
--- a/src/input.cc
+++ b/src/input.cc
@@ -45,6 +45,8 @@ u_int16 input::mouse_posx, input::mouse_posy;
bool input::mouse_button[3];
std::queue<std::string> input::input_queue;
+SDL_GameController *input::controller = NULL;
+
void input::init()
{
const u_int8 *state = SDL_GetKeyboardState(&keystatelength);
@@ -54,11 +56,37 @@ void input::init()
// set_keyboard_mode(MODE_STATE);
p_keystate=new u_int8[keystatelength];
memset(p_keystate, 0, keystatelength);
+
+ controller = init_controller();
+}
+
+SDL_GameController *input::init_controller()
+{
+ for (int i = 0; i < SDL_NumJoysticks(); ++i)
+ {
+ if (SDL_IsGameController(i))
+ {
+ SDL_GameController *controller =
SDL_GameControllerOpen(i);
+ if (controller)
+ {
+ return controller;
+ }
+ else
+ {
+ std::cerr << "Failed to open controller " <<
SDL_GameControllerNameForIndex(i) << ": " << SDL_GetError() << std::endl;
+ }
+ }
+ }
+ return NULL;
}
void input::shutdown()
{
- delete[] p_keystate;
+ if(controller != NULL)
+ {
+ SDL_GameControllerClose(controller);
+ }
+ delete[] p_keystate;
}
void input::update()
@@ -103,6 +131,90 @@ void input::update()
}
break;
}
+
+ case SDL_CONTROLLERBUTTONDOWN:
+ {
+ SDL_Scancode idx =
map_controller_button((SDL_GameControllerButton) event.cbutton.button);
+ if (idx != SDL_SCANCODE_UNKNOWN)
+ {
+ keystate[idx] = 1;
+ p_keystate[idx]++;
+ }
+ break;
+ }
+ case SDL_CONTROLLERBUTTONUP:
+ {
+ SDL_Scancode idx =
map_controller_button((SDL_GameControllerButton) event.cbutton.button);
+ if (idx != SDL_SCANCODE_UNKNOWN)
+ {
+ keystate[idx] = 0;
+ }
+ break;
+ }
+ case SDL_CONTROLLERDEVICEADDED:
+ {
+ if (controller == NULL)
+ {
+ controller = init_controller();
+ }
+ break;
+ }
+ case SDL_CONTROLLERDEVICEREMOVED:
+ {
+ if (controller != NULL)
+ {
+ SDL_Joystick *stick =
SDL_GameControllerGetJoystick(controller);
+ if (event.cdevice.which ==
SDL_JoystickInstanceID(stick))
+ {
+
SDL_GameControllerClose(controller);
+ controller = NULL;
+ }
+ }
+ break;
+ }
+ }
+ }
+}
+
+SDL_Scancode input::map_controller_button(const SDL_GameControllerButton &
button)
+{
+ switch(button)
+ {
+ case SDL_CONTROLLER_BUTTON_DPAD_UP:
+ {
+ return SDL_SCANCODE_UP;
+ }
+ case SDL_CONTROLLER_BUTTON_DPAD_DOWN:
+ {
+ return SDL_SCANCODE_DOWN;
+ }
+ case SDL_CONTROLLER_BUTTON_DPAD_LEFT:
+ {
+ return SDL_SCANCODE_LEFT;
+ }
+ case SDL_CONTROLLER_BUTTON_DPAD_RIGHT:
+ {
+ return SDL_SCANCODE_RIGHT;
+ }
+ case SDL_CONTROLLER_BUTTON_A:
+ {
+ return SDL_SCANCODE_RETURN;
+ }
+ case SDL_CONTROLLER_BUTTON_B:
+ {
+ return SDL_SCANCODE_ESCAPE;
+ }
+ case SDL_CONTROLLER_BUTTON_LEFTSHOULDER:
+ {
+ return SDL_SCANCODE_PAGEUP;
+ }
+ case SDL_CONTROLLER_BUTTON_RIGHTSHOULDER:
+ {
+ return SDL_SCANCODE_PAGEDOWN;
+ }
+ default:
+ {
+ return SDL_SCANCODE_UNKNOWN;
}
}
}
diff --git a/src/input.h b/src/input.h
index fa51420..ec91eb5 100644
--- a/src/input.h
+++ b/src/input.h
@@ -123,6 +123,9 @@ public:
static void clear_keys_queue();
private:
+ static SDL_GameController *init_controller();
+ static SDL_Scancode map_controller_button(const SDL_GameControllerButton &
button);
+
static bool text_input;
static u_int16 mouse_posx, mouse_posy;
@@ -131,6 +134,7 @@ private:
static s_int32 keystatelength;
#ifndef SWIG
static bool mouse_button[3];
+ static SDL_GameController *controller;
#endif
static std::queue<std::string> input_queue;
};