maruti borker wrote:
>
> Yes , even i wanted to have quake-type controls ... can u please copy
> paste some example code so that i can have an idea.
>
Ok, our code is part of a Java peer since we actually grab the mouse
values from Java. So I'll cut and paste the relevant stuff but don't be
surprised if it it a little disjointed... oh, and ignore my funky
bracing style... (and errorf() and warnf() are our own calls into a
logging API on the Java side.)
In the header, we include:
#define DIRECTINPUT_VERSION 0x0800
#include <dinput.h>
And add a field to the class:
LPDIRECTINPUTDEVICE8 g_pMouse;
In the class implementation we have an initialize method that acquires
access to the mouse device:
HRESULT hr;
LPDIRECTINPUT8 g_pDI = NULL;
g_pMouse = NULL;
// Create a DInput object
hr = DirectInput8Create( GetModuleHandle(NULL), DIRECTINPUT_VERSION,
IID_IDirectInput8, (VOID**)&g_pDI, NULL );
if( FAILED(hr) )
{
errorf( "Failed to initialize DirectInput: %i", hr );
return( -1 );
}
// Obtain an interface to the system mouse device.
hr = g_pDI->CreateDevice( GUID_SysMouse, &g_pMouse, NULL );
if( FAILED(hr) )
{
errorf( "Failed to create mouse device: %i", hr );
return( -1 );
}
// 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.
hr = g_pMouse->SetDataFormat( &c_dfDIMouse2 );
if( FAILED(hr) )
{
errorf( "Failed to set data format: %i", hr );
return( -1 );
}
// By default we'll set the device to be in non-exclusive
// mode until acquired. Which actually is still pretty
// much what we want other than the fact that regular events
// still work.
DWORD dwCoopFlags = DISCL_NONEXCLUSIVE | DISCL_FOREGROUND;
HWND hWnd = GetForegroundWindow();
// Set the cooperativity level to let DirectInput know how
// this device should interact with the system and with other
// DirectInput applications.
hr = g_pMouse->SetCooperativeLevel( hWnd, dwCoopFlags );
if( FAILED(hr) )
{
errorf( "Failed to set mouse cooperation mode: %i", hr );
return( -1 );
}
// Acquire the non-exclusive device
hr = g_pMouse->Acquire();
if( FAILED(hr) )
{
errorf( "Mouse acquisition failed:%i", hr );
return( -1 );
}
return( (int)0 );
Then to read the mouse state we have a method for reading the device
values that looks like:
HRESULT hr;
DIMOUSESTATE2 dims2; // DirectInput mouse state structure
// Get the input's device state, and put the state in dims
ZeroMemory( &dims2, sizeof(dims2) );
hr = g_pMouse->GetDeviceState( sizeof(DIMOUSESTATE2), &dims2 );
if( FAILED(hr) )
{
// We may have lost acquisition of the device because another
// app went to the foreground. Since we don't busy-wait for
// it to come back we can't rely on the return code to tell us...
// so we'll keep trying to reacquire for every error.
if( hr == DIERR_INPUTLOST )
warnf( "Mouse access lost." );
// Try to reacquire it once
hr = g_pMouse->Acquire();
if( FAILED(hr) )
{
// Just return an empty state
return( createMouseState( 0, 0, 0, 0, 0, 0 ) );
}
else
{
warnf( "Mouse access reacquired." );
}
}
return createMouseState( dims2.lX, dims2.lY, dims2.lZ,
dims2.rgbButtons[0],
dims2.rgbButtons[1],
dims2.rgbButtons[2] );
The create mouse state method call above is creating our own Java holder
for the data. I left it in because it takes all of the data members
that are interesting.
We have another method for acquiring exclusive and non-exclusive access
to the mouse:
void MousePeer::setAcquire(bool flag)
{
// Unacquire the mouse if we've grabbed it previously
if( g_pMouse )
{
g_pMouse->Unacquire();
}
if( flag )
{
// Acquire exclusively
// Right now we always do exclusive foreground mode
// when we acquire. This means that our app should have
// full access to mouse deltas as long as they are the
// foreground app
DWORD dwCoopFlags = DISCL_EXCLUSIVE | DISCL_FOREGROUND;
HRESULT hr;
HWND hWnd = GetForegroundWindow();
// Set the cooperativity level to let DirectInput know how
// this device should interact with the system and with other
// DirectInput applications.
hr = g_pMouse->SetCooperativeLevel( hWnd, dwCoopFlags );
if( FAILED(hr) )
{
errorf( "Failed to set exclusive mouse mode: %i", hr );
return;
}
// Acquire the device
hr = g_pMouse->Acquire();
if( FAILED(hr) )
{
errorf( "Mouse acquisition failed: %i", hr );
return;
}
}
else
{
// Acquire non-exclusively
// By default we'll set the device to be in non-exclusive
// mode until acquired. Which actually is still pretty
// much what we want other than the fact that regular events
// still work.
DWORD dwCoopFlags = DISCL_NONEXCLUSIVE | DISCL_FOREGROUND;
HRESULT hr;
HWND hWnd = GetForegroundWindow();
// Set the cooperativity level to let DirectInput know how
// this device should interact with the system and with other
// DirectInput applications.
hr = g_pMouse->SetCooperativeLevel( hWnd, dwCoopFlags );
if( FAILED(hr) )
{
errorf( "Failed to set mouse cooperation mode: %i", hr );
return;
}
// Acquire the non-exclusive device
hr = g_pMouse->Acquire();
if( FAILED(hr) )
{
errorf( "Mouse acquisition failed: %i", hr );
return;
}
}
}
In our case, I'm not sure we ever call that method since in our
particular application it is ok for the mouse pointer to still be
visible (even if it's a little confusing).
Hopefully the above code is helpful to you.
-Paul
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org