The entrance (WinMain) to the application is in NetWars.cpp. It is currently utilizing DirectX, D3DX, XInput (360 controller), Box2D and eventually Enet for networking.
Last Updated 5/30/08
//-----------------------------------------------------------------------------
// UserInput implementation.
//-----------------------------------------------------------------------------
#include "NetWars.h"
//-----------------------------------------------------------------------------
// The UserInput class constructor.
//-----------------------------------------------------------------------------
UserInput::UserInput( HWND window )
{
m_WindowHWND = window;
// Create a DirectInput interface.
DirectInput8Create( GetModuleHandle( NULL ), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&m_DirectInput, NULL );
// Create, prepare, and aquire the keyboard device.
m_DirectInput->CreateDevice( GUID_SysKeyboard, &m_Keyboard, NULL );
m_Keyboard->SetDataFormat( &c_dfDIKeyboard );
m_Keyboard->SetCooperativeLevel( m_WindowHWND, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE );
m_Keyboard->Acquire();
// Create, prepare, and aquire the mouse device.
m_DirectInput->CreateDevice( GUID_SysMouse, &m_Mouse, NULL );
m_Mouse->SetDataFormat( &c_dfDIMouse );
m_Mouse->SetCooperativeLevel( m_WindowHWND, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE );
m_Mouse->Acquire();
ZeroMemory( &m_gamepadState, sizeof(XINPUT_STATE) );
m_gamepadConnected = false;
// Initialize the Deadzone values.
m_thumbDeadzone = 7849;
m_triggerThreshhold = 30;
// Start the press stamp.
m_pressStamp = 0;
}
//-----------------------------------------------------------------------------
// The UserInput class destructor.
//-----------------------------------------------------------------------------
UserInput::~UserInput( )
{
if( m_Keyboard )
m_Keyboard->Unacquire();
SAFE_RELEASE( m_Keyboard );
if( m_Mouse )
m_Mouse->Unacquire();
SAFE_RELEASE( m_Mouse );
SAFE_RELEASE( m_DirectInput );
}
//-----------------------------------------------------------------------------
// The UserInput update method.
//
//-----------------------------------------------------------------------------
void UserInput::Update( )
{
static HRESULT result;
// Poll the keyboard until it succeeds or returns an unknown error.
while( true )
{
m_Keyboard->Poll();
if( SUCCEEDED( result = m_Keyboard->GetDeviceState( 256, (LPVOID)&m_keyState ) ) )
break;
if( result != DIERR_INPUTLOST && result != DIERR_NOTACQUIRED )
return;
// Reacquire the device if the focus was lost.
if( FAILED( m_Keyboard->Acquire() ) )
return;
}
// Poll the mouse until it succeeds or returns an unknown error.
while( true )
{
m_Mouse->Poll();
if( SUCCEEDED( result = m_Mouse->GetDeviceState( sizeof( DIMOUSESTATE ), &m_mouseState ) ) )
break;
if( result != DIERR_INPUTLOST && result != DIERR_NOTACQUIRED )
return;
// Reacquire the device if the focus was lost.
if( FAILED( m_Mouse->Acquire() ) )
return;
}
// Get Gamepad #1
ZeroMemory( &m_gamepadState, sizeof(XINPUT_STATE) );
// Simply get the state of the controller from XInput.
DWORD dwResult;
dwResult = XInputGetState( 0, &m_gamepadState );
if( dwResult == ERROR_SUCCESS )
m_gamepadConnected = true;
else
m_gamepadConnected = false;
if( m_gamepadConnected )
{
ApplyDeadzones();
}
// Get the relative position of the mouse.
GetCursorPos( &m_position );
ScreenToClient( m_WindowHWND, &m_position );
processInput();
// Increment the press stamp.
m_pressStamp++;
}
//-----------------------------------------------------------------------------
// Returns true if the given key is pressed.
// Note: Consistent presses will return false when using the press stamp.
//-----------------------------------------------------------------------------
bool UserInput::GetKeyPress( char key, bool ignorePressStamp )
{
if( ( m_keyState[key] & 0x80 ) == false )
return false;
bool pressed = true;
if( ignorePressStamp == false )
if( m_keyPressStamp[key] == m_pressStamp - 1 || m_keyPressStamp[key] == m_pressStamp )
pressed = false;
m_keyPressStamp[key] = m_pressStamp;
return pressed;
}
//-----------------------------------------------------------------------------
// Returns true if the given button is pressed.
// Note: Consistent presses will return false when using the press stamp.
//-----------------------------------------------------------------------------
bool UserInput::GetButtonPress( char button, bool ignorePressStamp )
{
if( ( m_mouseState.rgbButtons[button] & 0x80 ) == false )
return false;
bool pressed = true;
if( ignorePressStamp == false )
if( m_buttonPressStamp[button] == m_pressStamp - 1 || m_buttonPressStamp[button] == m_pressStamp )
pressed = false;
m_buttonPressStamp[button] = m_pressStamp;
return pressed;
}
//-----------------------------------------------------------------------------
// Returns the x position of the mouse.
//-----------------------------------------------------------------------------
long UserInput::GetMouseX()
{
return m_position.x;
}
//-----------------------------------------------------------------------------
// Returns the y position of the mouse.
//-----------------------------------------------------------------------------
long UserInput::GetMouseY()
{
return m_position.y;
}
//-----------------------------------------------------------------------------
// Returns the change in the mouse's x coordinate.
//-----------------------------------------------------------------------------
long UserInput::GetDeltaX()
{
return m_mouseState.lX;
}
//-----------------------------------------------------------------------------
// Returns the change in the mouse's y coordinate.
//-----------------------------------------------------------------------------
long UserInput::GetDeltaY()
{
return m_mouseState.lY;
}
//-----------------------------------------------------------------------------
// Returns the change in the mouse's scroll wheel.
//-----------------------------------------------------------------------------
long UserInput::GetDeltaWheel()
{
return m_mouseState.lZ;
}
//-----------------------------------------------------------------------------
// Returns the angle of the Right Thumbstick
//-----------------------------------------------------------------------------
float UserInput::GetAngleRightThumb()
{
if( m_gamepadConnected )
return atan2( (float) m_gamepadState.Gamepad.sThumbRY, (float) m_gamepadState.Gamepad.sThumbRX );
else
return 0.0f;
}
//-----------------------------------------------------------------------------
// Returns the angle of the Left Thumbstick
//-----------------------------------------------------------------------------
float UserInput::GetAngleLeftThumb()
{
if( m_gamepadConnected )
return atan2( (float) m_gamepadState.Gamepad.sThumbLY, (float) m_gamepadState.Gamepad.sThumbLX );
else
return 0.0f;
}
//-----------------------------------------------------------------------------
// Returns the angle of the Right Thumbstick
//-----------------------------------------------------------------------------
float UserInput::GetRadiusRightThumb()
{
if( m_gamepadConnected )
{
float Y = m_gamepadState.Gamepad.sThumbRY;
float X = m_gamepadState.Gamepad.sThumbRX;
return sqrt( X*X + Y*Y );
} else {
return 0.0f;
}
}
//-----------------------------------------------------------------------------
// Returns the angle of the Left Thumbstick
//-----------------------------------------------------------------------------
float UserInput::GetRadiusLeftThumb()
{
if( m_gamepadConnected )
{
float Y = m_gamepadState.Gamepad.sThumbLY;
float X = m_gamepadState.Gamepad.sThumbLX;
return sqrt( X*X + Y*Y );
} else {
return 0.0f;
}
}
//-----------------------------------------------------------------------------
// Returns the unit vector of the Left Thumbstick
//-----------------------------------------------------------------------------
b2Vec2 UserInput::GetDirLeftThumb()
{
b2Vec2 dir;
dir.x = cos( GetAngleLeftThumb() );
dir.y = sin( GetAngleLeftThumb() );
return dir;
}
//-----------------------------------------------------------------------------
// Returns the unit vector of the Right Thumbstick
//-----------------------------------------------------------------------------
b2Vec2 UserInput::GetDirRightThumb()
{
b2Vec2 dir;
dir.x = cos( GetAngleRightThumb() );
dir.y = sin( GetAngleRightThumb() );
return dir;
}
//-----------------------------------------------------------------------------
// Applies the Deadzone to the Gamepad
//-----------------------------------------------------------------------------
void UserInput::ApplyDeadzones()
{
// Zero value if thumbsticks are within the dead zone
if( (m_gamepadState.Gamepad.sThumbLX < m_thumbDeadzone &&
m_gamepadState.Gamepad.sThumbLX > -m_thumbDeadzone) &&
(m_gamepadState.Gamepad.sThumbLY < m_thumbDeadzone &&
m_gamepadState.Gamepad.sThumbLY > -m_thumbDeadzone) )
{
m_gamepadState.Gamepad.sThumbLX = 0;
m_gamepadState.Gamepad.sThumbLY = 0;
}
if( (m_gamepadState.Gamepad.sThumbRX < m_thumbDeadzone &&
m_gamepadState.Gamepad.sThumbRX > -m_thumbDeadzone) &&
(m_gamepadState.Gamepad.sThumbRY < m_thumbDeadzone &&
m_gamepadState.Gamepad.sThumbRY > -m_thumbDeadzone) )
{
m_gamepadState.Gamepad.sThumbRX = 0;
m_gamepadState.Gamepad.sThumbRY = 0;
}
if( m_gamepadState.Gamepad.bRightTrigger < m_triggerThreshhold )
{
m_gamepadState.Gamepad.bRightTrigger = 0;
}
if( m_gamepadState.Gamepad.bLeftTrigger < m_triggerThreshhold )
{
m_gamepadState.Gamepad.bLeftTrigger = 0;
}
}
//-----------------------------------------------------------------------------
// Returns the DirectInput Interface.
//-----------------------------------------------------------------------------
IDirectInput8 * UserInput::GetDirectInput()
{
return m_DirectInput;
}
//-----------------------------------------------------------------------------
// Returns the value of the Right Trigger
//-----------------------------------------------------------------------------
int UserInput::GetRightTrigger()
{
if( m_gamepadConnected )
{
return m_gamepadState.Gamepad.bRightTrigger;
} else {
return 0;
}
}
//-----------------------------------------------------------------------------
// Returns the value of the Left Trigger
//-----------------------------------------------------------------------------
int UserInput::GetLeftTrigger()
{
if( m_gamepadConnected )
{
return m_gamepadState.Gamepad.bLeftTrigger;
} else {
return 0;
}
}
//-----------------------------------------------------------------------------
// Processes the UserInput And convert to commands
//-----------------------------------------------------------------------------
void UserInput::processInput()
{
//SpaceShip
SpaceShip * ship = (SpaceShip *) g_Engine->GetPlayerShip();
if( GetRadiusLeftThumb() )
ship->AddForce( GetDirLeftThumb(), GetRadiusLeftThumb()/1.0f );
if( GetRadiusRightThumb() )
ship->Rotate( -GetAngleRightThumb() );
if( GetRightTrigger() )
ship->FireShield();
if( GetButtonPress( 0, true ) )
ship->FireShield();
if( GetButtonPress( 1, true ) )
ship->AttachShield();
if( GetLeftTrigger() > 100 )
ship->AttachShield();
float wasdForce = 10000.0f;
if( GetKeyPress( DIK_A, true ) )
ship->AddForce( g_Engine->m_rightVector, wasdForce );
if( GetKeyPress( DIK_D, true ) )
ship->AddForce( g_Engine->m_leftVector, wasdForce );
if( GetKeyPress( DIK_W, true ) )
ship->AddForce( g_Engine->m_upVector, wasdForce );
if( GetKeyPress( DIK_S, true ) )
ship->AddForce( g_Engine->m_downVector, wasdForce );
// All of this is to calculate the angle from the player ship
// to the mouse - this is should probably be simplified and moved to
// a method.
if( GetDeltaX() || GetDeltaY() )
{
float mouseX = GetMouseX();
float mouseY = GetMouseY();
RECT windowRect = g_Engine->GetWindow()->GetRect();
float adjustedY = -GetMouseY() + windowRect.top + 300.0f - 40.0f;
float adjustedX = GetMouseX() - windowRect.left - 400.0f + 25.0f;
float xdist = adjustedX + (ship->GetX()/GAMEBORDER_WIDTH * 400.0f) ;
float ydist = adjustedY - (ship->GetY()/GAMEBORDER_HEIGHT * 300.0f);
float angle = atan2( (float) xdist, (float) ydist ) - 1.5;
ship->Rotate( angle );
}
/*
if( GetKeyPress( DIK_NUMPAD4, true ) )
ship->Move( 0.0f, 0.0f );
*/
}