Function: get controller state()
Posted: Tue Oct 25, 2011 6:31 pm
Hello!
So, some of you may know that GE has GetKeyState(); function, which returns the state of the keyboard.
And well I thought that it's simpler to create same kind of for controller than call getJoystick1Button() million times.. xD
So, here's what I got:
Usage (just the same as using GetKeyState()):
And another version with sticks treated as buttons, simply on/off.
Usage:
Sorry for not putting that much comments to the code, I just can't think of anything to put there..
And please tell me if there is something that I could/should optimize.
-lcl-
So, some of you may know that GE has GetKeyState(); function, which returns the state of the keyboard.
And well I thought that it's simpler to create same kind of for controller than call getJoystick1Button() million times.. xD
So, here's what I got:
- Code: Select all
#define BUTTON_1 1
#define BUTTON_2 2
#define BUTTON_3 3
#define BUTTON_4 4
#define BUTTON_5 5
#define BUTTON_6 6
#define BUTTON_7 7
#define BUTTON_8 8
#define BUTTON_9 9
#define BUTTON_10 10
#define BUTTON_11 11
#define BUTTON_12 12
#define BUTTON_13 13
#define BUTTON_14 14
char joystickKey[15];
char * getControllerState()
{
int i = 0;
for (i = 0; i < 14; i ++)
{
joystickKey[i + 1] = GetJoystick1Button(i); //+ 1 is because for some reason GE doesn't return pointers to array cell 0 correctly.. :P
}
return joystickKey;
}
Usage (just the same as using GetKeyState()):
- Code: Select all
char * key = getControllerState();
if (key[BUTTON_1])//do some stuff ... xD
And another version with sticks treated as buttons, simply on/off.
- Code: Select all
#define BUTTON_1 1
#define BUTTON_2 2
#define BUTTON_3 3
#define BUTTON_4 4
#define BUTTON_5 5
#define BUTTON_6 6
#define BUTTON_7 7
#define BUTTON_8 8
#define BUTTON_9 9
#define BUTTON_10 10
#define BUTTON_11 11
#define BUTTON_12 12
#define BUTTON_13 13
#define BUTTON_14 14
#define STICK_L_R 16 //stick left, pushed right :D
#define STICK_L_L 17
#define STICK_L_U 18
#define STICK_L_D 19
#define STICK_R_R 20
#define STICK_R_L 21
#define STICK_R_U 22
#define STICK_R_D 23
char joystickKey[24];
char * getControllerState()
{
int i = 0;
for (i = 0; i < 14; i ++)
{
joystickKey[i + 1] = GetJoystick1Button(i);
}
joystickKey[16] = (abs(GetJoystick1Axis(0))>3000) * (sign(GetJoystick1Axis(0)) == -1);
joystickKey[17] = (abs(GetJoystick1Axis(0))>3000) * (sign(GetJoystick1Axis(0)) == 1);
joystickKey[18] = (abs(GetJoystick1Axis(1))>3000) * (sign(GetJoystick1Axis(1)) == -1);
joystickKey[19] = (abs(GetJoystick1Axis(1))>3000) * (sign(GetJoystick1Axis(1)) == 1);
joystickKey[20] = (abs(GetJoystick1Axis(2))>3000) * (sign(GetJoystick1Axis(2)) == -1);
joystickKey[21] = (abs(GetJoystick1Axis(2))>3000) * (sign(GetJoystick1Axis(2)) == 1);
joystickKey[22] = (abs(GetJoystick1Axis(3))>3000) * (sign(GetJoystick1Axis(3)) == -1);
joystickKey[23] = (abs(GetJoystick1Axis(3))>3000) * (sign(GetJoystick1Axis(3)) == 1);
return joystickKey;
}
Usage:
- Code: Select all
char * key = getControllerState();
if (key[STICK_L_R])xvelocity = 5; //If left stick is moved to right, set xvelocity to 5 :D
Sorry for not putting that much comments to the code, I just can't think of anything to put there..
And please tell me if there is something that I could/should optimize.
-lcl-