This commit is contained in:
Glenn Maynard
2003-02-15 06:44:53 +00:00
parent a39bf03926
commit 8447976237
2 changed files with 63 additions and 53 deletions
+50 -43
View File
@@ -43,8 +43,12 @@ struct RageInput::pump_t
pump_t(); pump_t();
~pump_t(); ~pump_t();
bool Active() const { return h != INVALID_HANDLE_VALUE; }
void Update();
bool init(int devno); bool init(int devno);
int GetPadEvent(); int GetPadEvent();
bool current_state[NUM_PUMP_PAD_BUTTONS];
}; };
int DeviceInput::NumButtons(InputDevice device) int DeviceInput::NumButtons(InputDevice device)
@@ -222,12 +226,7 @@ RageInput::RageInput()
SDL_InitSubSystem( SDL_INIT_JOYSTICK ); SDL_InitSubSystem( SDL_INIT_JOYSTICK );
// init state info // init state info
memset( m_keys, 0, sizeof(m_keys) ); memset( state, 0, sizeof(state) );
memset( m_oldKeys, 0, sizeof(m_oldKeys) );
memset( m_joyState, 0, sizeof(m_joyState) );
memset( m_oldJoyState, 0, sizeof(m_oldJoyState) );
memset( m_pumpState, 0, sizeof(m_pumpState) );
memset( m_oldPumpState, 0, sizeof(m_oldPumpState) );
// //
@@ -302,9 +301,7 @@ void RageInput::Update( float fDeltaTime )
// //
// Move last current state to old state // Move last current state to old state
// //
memcpy( m_oldKeys, m_keys, sizeof(m_oldKeys) ); memcpy( &state[LAST], &state[CURRENT], sizeof(state[LAST]) );
memcpy( m_oldJoyState, m_joyState, sizeof(m_joyState) );
memcpy( m_oldPumpState, m_pumpState, sizeof(m_pumpState) );
// //
@@ -312,14 +309,14 @@ void RageInput::Update( float fDeltaTime )
// //
SDL_PumpEvents(); SDL_PumpEvents();
Uint8* keystate = SDL_GetKeyState(NULL); Uint8* keystate = SDL_GetKeyState(NULL);
memcpy( m_keys, keystate, sizeof(m_keys) ); memcpy( state[CURRENT].m_keys, keystate, sizeof(state[CURRENT].m_keys) );
// //
// Update Joystick // Update Joystick
// //
SDL_JoystickUpdate(); SDL_JoystickUpdate();
memset( m_joyState, 0, sizeof(m_joyState) ); // clear current state memset( state[CURRENT].m_joyState, 0, sizeof(state[CURRENT].m_joyState) ); // clear current state
for( int joy=0; joy<NUM_JOYSTICKS; joy++ ) // foreach joystick for( int joy=0; joy<NUM_JOYSTICKS; joy++ ) // foreach joystick
{ {
@@ -335,12 +332,12 @@ void RageInput::Update( float fDeltaTime )
if( val < -16000 ) if( val < -16000 )
{ {
JoystickButton b = (JoystickButton)(JOY_LEFT+2*axis); JoystickButton b = (JoystickButton)(JOY_LEFT+2*axis);
m_joyState[joy].button[b] = true; state[CURRENT].m_joyState[joy].button[b] = true;
} }
else if( val > +16000 ) else if( val > +16000 )
{ {
JoystickButton b = (JoystickButton)(JOY_RIGHT+2*axis); JoystickButton b = (JoystickButton)(JOY_RIGHT+2*axis);
m_joyState[joy].button[b] = true; state[CURRENT].m_joyState[joy].button[b] = true;
} }
} }
@@ -348,17 +345,17 @@ void RageInput::Update( float fDeltaTime )
for( int hat=0; hat<iNumJoyHats; hat++ ) for( int hat=0; hat<iNumJoyHats; hat++ )
{ {
Uint8 val = SDL_JoystickGetHat(pJoy,hat); Uint8 val = SDL_JoystickGetHat(pJoy,hat);
m_joyState[joy].button[JOY_HAT_UP] = (val & SDL_HAT_UP) != 0; state[CURRENT].m_joyState[joy].button[JOY_HAT_UP] = (val & SDL_HAT_UP) != 0;
m_joyState[joy].button[JOY_HAT_RIGHT] = (val & SDL_HAT_RIGHT) != 0; state[CURRENT].m_joyState[joy].button[JOY_HAT_RIGHT] = (val & SDL_HAT_RIGHT) != 0;
m_joyState[joy].button[JOY_HAT_DOWN] = (val & SDL_HAT_DOWN) != 0; state[CURRENT].m_joyState[joy].button[JOY_HAT_DOWN] = (val & SDL_HAT_DOWN) != 0;
m_joyState[joy].button[JOY_HAT_LEFT] = (val & SDL_HAT_LEFT) != 0; state[CURRENT].m_joyState[joy].button[JOY_HAT_LEFT] = (val & SDL_HAT_LEFT) != 0;
} }
int iNumJoyButtons = MIN(NUM_JOYSTICK_BUTTONS,SDL_JoystickNumButtons(pJoy)); int iNumJoyButtons = MIN(NUM_JOYSTICK_BUTTONS,SDL_JoystickNumButtons(pJoy));
for( int button=0; button<iNumJoyButtons; button++ ) for( int button=0; button<iNumJoyButtons; button++ )
{ {
JoystickButton b = (JoystickButton)(JOY_1 + button); JoystickButton b = (JoystickButton)(JOY_1 + button);
m_joyState[joy].button[b] = SDL_JoystickGetButton(pJoy,button) != 0; state[CURRENT].m_joyState[joy].button[b] = SDL_JoystickGetButton(pJoy,button) != 0;
} }
} }
@@ -368,42 +365,25 @@ void RageInput::Update( float fDeltaTime )
// //
for( int i=0; i<NUM_PUMPS; i++ ) for( int i=0; i<NUM_PUMPS; i++ )
{ {
if(m_Pumps[i].h == INVALID_HANDLE_VALUE) if(!m_Pumps[i].Active()) continue;
continue; /* no pad */
int ret = m_Pumps[i].GetPadEvent(); m_Pumps[i].Update();
if(ret == -1) memcpy(state[CURRENT].m_pumpState[i].button, m_Pumps[i].current_state, sizeof(m_Pumps[i].current_state));
continue; /* no event */
/* Since we're checking for messages, and not polling,
* only zero this out when we actually *have* a new
* message. */
ZeroMemory( &m_pumpState[i], sizeof(m_pumpState[i]) );
int bits[] = {
/* P1 */ (1<<9), (1<<12), (1<<13), (1<<11), (1<<10),
/* ESC */ (1<<16),
/* P1 */ (1<<17), (1<<20), (1<<21), (1<<19), (1<<18),
};
for (int butno = 0 ; butno < NUM_PUMP_PAD_BUTTONS ; butno++)
{
if(!(ret & bits[butno]))
m_pumpState[i].button[butno] = true;
}
} }
} }
bool RageInput::BeingPressed( DeviceInput di, bool bPrevState ) bool RageInput::BeingPressed( DeviceInput di, bool bPrevState )
{ {
State s = bPrevState? LAST:CURRENT;
switch( di.device ) switch( di.device )
{ {
case DEVICE_KEYBOARD: case DEVICE_KEYBOARD:
{ {
ASSERT(di.button < NUM_KEYBOARD_BUTTONS); ASSERT(di.button < NUM_KEYBOARD_BUTTONS);
return (bPrevState?m_oldKeys:m_keys) [ di.button ]; return state[s].m_keys[di.button];
} }
case DEVICE_JOY1: case DEVICE_JOY1:
case DEVICE_JOY2: case DEVICE_JOY2:
@@ -411,13 +391,13 @@ bool RageInput::BeingPressed( DeviceInput di, bool bPrevState )
case DEVICE_JOY4: case DEVICE_JOY4:
{ {
ASSERT(di.button < NUM_JOYSTICK_BUTTONS); ASSERT(di.button < NUM_JOYSTICK_BUTTONS);
return (bPrevState?m_oldJoyState:m_joyState) [di.device - DEVICE_JOY1].button[ di.button ]; return state[s].m_joyState[di.device - DEVICE_JOY1].button[ di.button ];
} }
case DEVICE_PUMP1: case DEVICE_PUMP1:
case DEVICE_PUMP2: case DEVICE_PUMP2:
{ {
ASSERT(di.button < NUM_PUMP_PAD_BUTTONS); ASSERT(di.button < NUM_PUMP_PAD_BUTTONS);
return (bPrevState?m_oldPumpState:m_pumpState) [di.device - DEVICE_PUMP1].button[di.button]; return state[s].m_pumpState[di.device - DEVICE_PUMP1].button[di.button];
} }
default: default:
ASSERT( 0 ); // bad device ASSERT( 0 ); // bad device
@@ -511,6 +491,7 @@ HANDLE USB::OpenUSB (int VID, int PID, int num)
RageInput::pump_t::pump_t() RageInput::pump_t::pump_t()
{ {
ZeroMemory( &ov, sizeof(ov) ); ZeroMemory( &ov, sizeof(ov) );
memset(current_state, 0, sizeof(current_state));
pending=false; pending=false;
h = INVALID_HANDLE_VALUE; h = INVALID_HANDLE_VALUE;
} }
@@ -563,3 +544,29 @@ int RageInput::pump_t::GetPadEvent()
return buf; return buf;
} }
void RageInput::pump_t::Update()
{
int ret = GetPadEvent();
if(ret == -1)
return; /* no event */
/* Since we're checking for messages, and not polling,
* only zero this out when we actually *have* a new
* message. */
memset( &current_state, 0, sizeof(current_state) );
int bits[] = {
/* P1 */ (1<<9), (1<<12), (1<<13), (1<<11), (1<<10),
/* ESC */ (1<<16),
/* P1 */ (1<<17), (1<<20), (1<<21), (1<<19), (1<<18),
};
for (int butno = 0 ; butno < NUM_PUMP_PAD_BUTTONS ; butno++)
{
if(!(ret & bits[butno]))
current_state[butno] = true;
}
}
+13 -10
View File
@@ -83,19 +83,22 @@ class RageInput
{ {
SDL_Joystick* m_pJoystick[NUM_JOYSTICKS]; SDL_Joystick* m_pJoystick[NUM_JOYSTICKS];
// Keyboard state data enum State { CURRENT = 0, LAST, NUM_STATES };
bool m_keys[NUM_KEYBOARD_BUTTONS];
bool m_oldKeys[NUM_KEYBOARD_BUTTONS];
// Joystick state data
struct { struct {
bool button[NUM_JOYSTICK_BUTTONS]; // Keyboard state data
} m_joyState[NUM_JOYSTICKS], m_oldJoyState[NUM_JOYSTICKS]; bool m_keys[NUM_KEYBOARD_BUTTONS];
// Pump state data // Joystick state data
struct { struct {
bool button[NUM_PUMP_PAD_BUTTONS]; bool button[NUM_JOYSTICK_BUTTONS];
} m_pumpState[NUM_PUMPS], m_oldPumpState[NUM_PUMPS]; } m_joyState[NUM_JOYSTICKS];
// Pump state data
struct {
bool button[NUM_PUMP_PAD_BUTTONS];
} m_pumpState[NUM_PUMPS];
} state[NUM_STATES];
/* Structure for reading Pump pads: */ /* Structure for reading Pump pads: */
struct pump_t; struct pump_t;