Sign in to follow this  
KaKaroTo

steering wheel

Recommended Posts

Hi i'm building a Racing Game In C++ using OpenGL... i have a steering wheel that is plugged into a USB port. How can i access the usb port and get information from the steering wheel to my program?

Share this post


Link to post
Share on other sites
Steering wheels are just like any other input devices in the eyes of DirectInput (Joystick / mouse / keyboards). There are probably wrapper libraries that makes using DirectInput easier (It's not that hard really), but that's the basics. Look on the net for plenty of tutorials on the subject. MS has a very decent doc shipped with the DirectX SDK regarding input devices and how to access them. It's probably available online too. If you use OpenGL instead of DirectX, it doesn't matter.

Here's my basic DirectInput Wrapper I use with OpenGL.

header


#include <dinput.h>
extern bool DXInputs_Initialise(const char* windowName);
extern void DXInputs_Shutdown();
extern void DXInputs_Update();

extern bool DXInputs_DebouncedKeyPressed(int k);
extern bool DXInputs_DebouncedKeyRepeat(int k);
extern bool DXInputs_DebouncedKeyReleased(int k);
extern bool DXInputs_KeyReleased(int k);
extern bool DXInputs_KeyPressed(int k);

extern void DXInputs_GetMouseVelocity(int& vx, int& vy, int& vz);
extern void DXInputs_GetMouseAccel(int& ax, int& ay, int& az);
extern unsigned int DXInputs_GetMouseButtons();
extern int DXInputs_GetMouseWheelVelocity();



code

//===========================================================================
//
// INCLUDES
//
//===========================================================================
#include <tchar.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <math.h>

#include <windows.h>

#include "DXInputs.h"

//===========================================================================
//
// DX INPUTS
//
//===========================================================================

#pragma comment(lib, "dxguid.lib")
#pragma comment(lib, "dxerr8.lib")
#pragma comment(lib, "dinput8.lib")

int mousevx[2], mousevy[2], mousevz[2];
int mouseb [2];
int mousepage = 0;

unsigned char keyb[2][256];
int keypage = 0;

IDirectInput8* m_pDI=0;
IDirectInputDevice8* m_pKeyboard=0;
IDirectInputDevice8* m_pMouse=0;
HWND hWnd=0;
HINSTANCE hInstance=0;
HRESULT hr=0;

HRESULT DXInputs_CreateDevice();
HRESULT DXInputs_CreateKeyboard(BOOL bExclusive, BOOL bForeground, BOOL bDisableWindowsKey);
HRESULT DXInputs_CreateMouse(BOOL bExclusive, BOOL bForeground, BOOL bDisableWindowsKey);

HRESULT DXInputs_DestroyDevice();
HRESULT DXInputs_DestroyKeyboard();
HRESULT DXInputs_DestroyMouse();

HRESULT DXInputs_UpdateKeyboard();
HRESULT DXInputs_UpdateMouse();


//-----------------------------------------------------------------------------
// Defines, constants, and global variables
//-----------------------------------------------------------------------------
#define SAFE_DELETE(p) { if(p) { delete (p); (p)=NULL; } }
#define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=NULL; } }
#define SAMPLE_BUFFER_SIZE 8 // arbitrary number of buffer elements

bool DXInputs_DebouncedKeyRepeat(int k)
{
if (k < 0 || k > 255) return false;

if ((keyb[keypage][k] & 0x80) == 0)
return false;

return (keyb[keypage][k] & 0x03) == 0x03;
}

bool DXInputs_DebouncedKeyPressed(int k)
{
if (k < 0 || k > 255) return false;

if ((keyb[keypage][k] & 0x80) == 0)
return false;

return ((keyb[keypage][k] & 0x80) != 0) && ((keyb[1-keypage][k] & 0x80) == 0);
}

bool DXInputs_DebouncedKeyReleased(int k)
{
if (k < 0 || k > 255) return false;

return ((keyb[keypage][k] & 0x80) == 0) && ((keyb[1-keypage][k] & 0x80) != 0);
}

bool DXInputs_KeyReleased(int k)
{
if (k < 0 || k > 255) return false;

return ((keyb[keypage][k] & 0x80) == 0);
}

bool DXInputs_KeyPressed(int k)
{
if (k < 0 || k > 255) return false;

return ((keyb[keypage][k] & 0x80) != 0);
}

void DXInputs_GetMouseVelocity(int& vx, int& vy, int& vz)
{
vx = mousevx[mousepage];
vy = mousevy[mousepage];
vz = mousevz[mousepage];
}

void DXInputs_GetMouseAccel(int& ax, int& ay, int& az)
{
ax = mousevx[mousepage] - mousevx[1 - mousepage];
ay = mousevy[mousepage] - mousevy[1 - mousepage];
az = mousevz[mousepage] - mousevz[1 - mousepage];
}

unsigned int DXInputs_GetMouseButtons()
{
return mouseb[mousepage];
}
int DXInputs_GetMouseWheelVelocity()
{
return mousevz[mousepage];
}

bool DXInputs_Initialise(const char* windowName)
{
hWnd = FindWindow(NULL, windowName);
hInstance = (HINSTANCE) GetWindowLong(hWnd, GWL_HINSTANCE);

RECT rect;
GetClientRect(hWnd, &rect);

DXInputs_Shutdown();

BOOL bExcl = true;
BOOL bFore = true;
BOOL bKey = true;

if (FAILED(DXInputs_CreateDevice()) || FAILED(DXInputs_CreateKeyboard(bExcl, bFore, TRUE)) || FAILED(DXInputs_CreateMouse(bExcl, bFore, TRUE)))
{
MessageBox( hWnd, _T("CreateDevice() failed. ")
_T("The application will now exit."),
_T("Keyboard"), MB_ICONERROR | MB_OK );

DXInputs_Shutdown();
return false;
}

return true;
}
void DXInputs_Shutdown()
{
DXInputs_DestroyDevice();
}

void DXInputs_Update()
{
DXInputs_UpdateKeyboard();
DXInputs_UpdateMouse();
}

HRESULT DXInputs_CreateDevice()
{
// Create a DInput object
if( FAILED( hr = DirectInput8Create( hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (VOID**)&m_pDI, NULL ) ) )
return hr;

return S_OK;
}

HRESULT DXInputs_CreateKeyboard(BOOL bExclusive, BOOL bForeground, BOOL bDisableWindowsKey)
{
DWORD dwCoopFlags;

if( bExclusive )
dwCoopFlags = DISCL_EXCLUSIVE;
else
dwCoopFlags = DISCL_NONEXCLUSIVE;

if( bForeground )
dwCoopFlags |= DISCL_FOREGROUND;
else
dwCoopFlags |= DISCL_BACKGROUND;

// Disabling the windows key is only allowed only if we are in foreground nonexclusive
if( bDisableWindowsKey && !bExclusive && bForeground )
dwCoopFlags |= DISCL_NOWINKEY;

// Obtain an interface to the system keyboard device.
if( FAILED( hr = m_pDI->CreateDevice( GUID_SysKeyboard, &m_pKeyboard, NULL ) ) )
return hr;

// Set the data format to "keyboard format" - a predefined data format
//
// A data format specifies which controls on a device we
// are interested in, and how they should be reported.
//
// This tells DirectInput that we will be passing an array
// of 256 bytes to IDirectInputDevice::GetDeviceState.
if( FAILED( hr = m_pKeyboard->SetDataFormat( &c_dfDIKeyboard ) ) )
return hr;

// Set the cooperativity level to let DirectInput know how
// this device should interact with the system and with other
// DirectInput applications.
hr = m_pKeyboard->SetCooperativeLevel( hWnd, dwCoopFlags );
if( hr == DIERR_UNSUPPORTED && !bForeground && bExclusive )
{
DXInputs_DestroyDevice();
MessageBox( hWnd, _T("SetCooperativeLevel() returned DIERR_UNSUPPORTED.\n")
_T("For security reasons, background exclusive keyboard\n")
_T("access is not allowed."), _T("Keyboard"), MB_OK );
return S_OK;
}

if( FAILED(hr) )
return hr;

// Acquire the newly created device
m_pKeyboard->Acquire();

return S_OK;
}

HRESULT DXInputs_CreateMouse(BOOL bExclusive, BOOL bForeground, BOOL bDisableWindowsKey)
{
DWORD dwCoopFlags;

if( bExclusive )
dwCoopFlags = DISCL_EXCLUSIVE;
else
dwCoopFlags = DISCL_NONEXCLUSIVE;

if( bForeground )
dwCoopFlags |= DISCL_FOREGROUND;
else
dwCoopFlags |= DISCL_BACKGROUND;

// Obtain an interface to the system mouse device.
if( FAILED( hr = m_pDI->CreateDevice( GUID_SysMouse, &m_pMouse, NULL ) ) )
return hr;

// Set the data format to "mouse format" - a predefined data format
//
// A data format specifies which controls on a device we
// are interested in, and how they should be reported.
//
// This tells DirectInput that we will be passing a
// DIMOUSESTATE2 structure to IDirectInputDevice::GetDeviceState.
if( FAILED( hr = m_pMouse->SetDataFormat( &c_dfDIMouse2 ) ) )
return hr;

// Set the cooperativity level to let DirectInput know how
// this device should interact with the system and with other
// DirectInput applications.
hr = m_pMouse->SetCooperativeLevel( hWnd, dwCoopFlags );
if( hr == DIERR_UNSUPPORTED && !bForeground && bExclusive )
{
DXInputs_DestroyDevice();
MessageBox( hWnd, _T("SetCooperativeLevel() returned DIERR_UNSUPPORTED.\n")
_T("For security reasons, background exclusive mouse\n")
_T("access is not allowed."),
_T("Mouse"), MB_OK );
return S_OK;
}

if( FAILED(hr) )
return hr;

hr = m_pMouse->Acquire();

return S_OK;
}

HRESULT DXInputs_DestroyDevice()
{
SAFE_RELEASE( m_pDI );
DXInputs_DestroyKeyboard();
DXInputs_DestroyMouse();

return S_OK;
}

HRESULT DXInputs_DestroyKeyboard()
{
// Unacquire the device one last time just in case
// the app tried to exit while the device is still acquired.
if( m_pKeyboard )
m_pKeyboard->Unacquire();

// Release any DirectInput objects.
SAFE_RELEASE( m_pKeyboard );

return S_OK;
}

HRESULT DXInputs_DestroyMouse()
{
// Unacquire the device one last time just in case
// the app tried to exit while the device is still acquired.
if( m_pMouse )
m_pMouse->Unacquire();

// Release any DirectInput objects.
SAFE_RELEASE( m_pMouse );

return S_OK;
}


HRESULT DXInputs_UpdateKeyboard()
{
if (m_pKeyboard && GetForegroundWindow() == hWnd)
{
hr = m_pKeyboard->Acquire();
}
else
{
if (m_pKeyboard) m_pKeyboard->Unacquire();
return S_OK;
}

// Get the input's device state, and put the state in dims
keypage = 1-keypage;
ZeroMemory( &keyb[keypage], sizeof(keyb[keypage]) );
hr = m_pKeyboard->GetDeviceState( sizeof(keyb[keypage]), keyb[keypage] );

for(int i = 0; i < 256; i ++)
{
if (keyb[keypage][i] && keyb[1-keypage][i])
{
keyb[keypage][i] |= keyb[1-keypage][i] + 1;
}
}

if( FAILED(hr) )
{
// DirectInput may be telling us that the input stream has been
// interrupted. We aren't tracking any state between polls, so
// we don't have any special reset that needs to be done.
// We just re-acquire and try again.

// If input is lost then acquire and keep trying
hr = m_pKeyboard->Acquire();
while( hr == DIERR_INPUTLOST )
{
hr = m_pKeyboard->Acquire();
}

// hr may be DIERR_OTHERAPPHASPRIO or other errors. This
// may occur when the app is minimized or in the process of
// switching, so just try again later
return S_OK;
}
return S_OK;
}

HRESULT DXInputs_UpdateMouse()
{
if (m_pMouse && GetForegroundWindow() == hWnd)
{
m_pMouse->Acquire();
}
else
{
if (m_pMouse) m_pMouse->Unacquire();
return S_OK;
}

DIMOUSESTATE2 dims2; // DirectInput mouse state structure

// Get the input's device state, and put the state in dims
mousepage = 1-mousepage;
ZeroMemory( &dims2, sizeof(dims2) );
hr = m_pMouse->GetDeviceState( sizeof(DIMOUSESTATE2), &dims2 );
if( FAILED(hr) )
{
// DirectInput may be telling us that the input stream has been
// interrupted. We aren't tracking any state between polls, so
// we don't have any special reset that needs to be done.
// We just re-acquire and try again.

// If input is lost then acquire and keep trying
hr = m_pMouse->Acquire();
while( hr == DIERR_INPUTLOST )
hr = m_pMouse->Acquire();

// hr may be DIERR_OTHERAPPHASPRIO or other errors. This
// may occur when the app is minimized or in the process of
// switching, so just try again later
return S_OK;
}

mousevx[mousepage] = dims2.lX;
mousevy[mousepage] = dims2.lY;
mousevz[mousepage] = dims2.lZ;
mouseb [mousepage] = 0;

for(int i = 0; i < 8; i ++)
{
if (dims2.rgbButtons[i] & 0x80)
mouseb[mousepage] |= (1 << i);
}
return S_OK;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this