Skip to content

Instantly share code, notes, and snippets.

@kimsama
Last active November 5, 2020 09:54
Show Gist options
  • Save kimsama/25415fd4cf055235493cef80d550d84f to your computer and use it in GitHub Desktop.
Save kimsama/25415fd4cf055235493cef80d550d84f to your computer and use it in GitHub Desktop.
struct packet
{
char name[16];
unsigned short id;
char state_01; // 0: safety / 1: fire / 2: reload :==> safety / reload는 무시.
char state_02; // reserved.
unsigned short buttonState; // buttons
float thumb_x; // joystick x-axis
float thumb_y; // joystick y-axis
};
#define WEAPON_FIRE 0x0001 // weapon trigger
#define BUTTON_IK 0x0001 // lock button
const int ButtonCount = 14;
const unsinged short controller::buttonInputID[buttonCount] =
{
Button_A, // 0x0001
Button_B, // 0x0002
Button_C, // 0x0004
Button_D, // 0x0008
Button_E, // 0x0010
Button_F, // 0x0020
Button_G, // 0x0040
Button_H, // 0x0080
Button_I, // 0x0100
Button_J, // 0x0200
Button_K, // 0x1000
Button_L, // 0x2000
Button_M, // 0x4000
Button_N // 0x8000
};
void controller::update(float deltaTime)
{
...
// received packet from the gun controller.
struct packet recv_data;
unsigned int size = sizeof(packet);
memcpy(&recv_data, data, size);
switch (recv_data.state_01)
{
case '0': // turn on the power button
_powerOn = true;
_enableIK = (XCONTROLLER_BUTTON_IK & recv_data.buttonState) ? true : false;
break;
case '1': // fire action
_weaponFire = (XCONTROLLER_WEAPON_FIRE & recv_data.buttonState) ? true : false;
break;
case '2': // lock button
_enableIK = (XCONTROLLER_BUTTON_IK & recv_data.buttonState) ? true : false;
break;
case '3': // button
{
unsigned short buttonStates = recv_data.buttonStaste;
for (int i=0; i<buttonCount; ++i)
{
// !!check whether the button is pressed or not!!
bool pressed = (butonStates & buttonInputID[i]) != 0;
setButtonPressed(i, pressed);
}
// thumbstick
_thumbstick_posx = recv_data.thumb_x;
_thumbstick_posy = recv_data.thumb_y;
}
break;
}
}
void controller::setButtonPressed( ButtonID buttonID, bool pressed )
{
if ( buttonID >= ButtonCount )
return;
if ( !hasButton(buttonID) )
return;
if ( _iButtonPressed[buttonID] == pressed )
return;
_isButtonPressed[buttonID] = pressed;
// Notify
for ( Listeners::iterator itr=mListeners.begin(); itr!=mListeners.end(); ++itr )
(*itr)->onComponentChanged( this, ComponentType_Button, buttonID );
}
bool controller::isButtonPressed(ButtonID buttonID)
{
return _isButtonPressed[buttonID];
}
@kimsama
Copy link
Author

kimsama commented Oct 14, 2020

  • 버튼의 개수가 많기 때문에 xbox controller 와 같이 & 연산으로 버튼의 press 유무를 검사.
  • packet 구조는 이전 버전의 packet 구조에서 buttonState, thumbStick (x, y) 를 추가.
  • safety / reload는 무시.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment