If U’re using the mouse capture routine and want to use a fast interface
just use DirectX by the following steps:
1.- Include the <dinput.h> header file
2.- Declare DirectInput variables
Including a nice struct of data that will keep up to date
the current mouse position and Button status
3.- Declaring DirectInput functions that do the job
or transform the appropiate behavior of your program vars
4.- Take into account the logical order of call to these functions
1.- Call CreateDInput upon Window creation prior to initialize
OpenGL.
2.- Call UpdateDirectInputState before rendering the scene, so
when you draw the mouse pointer every frame it’ll be already
in the right place (see below example ***)
3.- Call DestroyDInput upon Window destruction
5.- Accomodate the UpdateDirectInputState function to change
6.- Enjoy the fastest way of acquire mouse status!
(***)
UpdateDirectInputState(); // Take a look at the mouse status
if (DrawGLScene())
SwapBuffers(hDC); // Swap Buffers (Double Buffering)
else
done=TRUE;
NOTE: acquiring the mouse status via Windows Message Queue is slower than
acquiring it directly from the input device every time you redraw
the scene!
/////////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include <dinput.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <stdlib.h>
#include <stdio.h>
////////////////////////////////////////////////////
// Global variables related to the app window //
/////////////////////////////////////////////////////
HDC hDC=NULL; // Private GDI Device Context
HGLRC hRC=NULL; // Permanent Rendering Context
HWND hWnd=NULL; // Holds Our Window Handle
HINSTANCE hInstance; // Holds The Instance Of The Application
LPDIRECTINPUT g_pDI; // DirectInput Object
LPDIRECTINPUTDEVICE g_pdidMouse; // DirectInput Device interface
int winH, winW; // Screen or window width and height
struct GL_MOUSE_STATUS
{
int x, y, z;
bool button1, button2, button3;
}MyMouseStatus;
//////////////////////////////// Function Declarations ////////////////////////////////
BOOL CreateDInput(HWND hWnd);
VOID DestroyDInput(void);
void UpdateDirectInputState(void);
//////////////////////////////// Function Definitions ////////////////////////////////
//-----------------------------------------------------------------------------
// Name: CreateDInput()
// Desc: Initialize the DirectInput variables using:
// DirectInputCreate
// IDirectInput::CreateDevice
// IDirectInputDevice::SetDataFormat
// IDirectInputDevice::SetCooperativeLevel
//-----------------------------------------------------------------------------
BOOL CreateDInput( HWND hWnd )
{
HINSTANCE hInst = (HINSTANCE)GetWindowLong( hWnd, GWL_HINSTANCE );
HRESULT hr;
// Register with the DirectInput subsystem and get a pointer
// to a IDirectInput interface we can use
if( FAILED( hr = DirectInputCreate( hInst, DIRECTINPUT_VERSION, &g_pDI, NULL) ) )
return hr;
// Obtain an interface to the system mouse device
if( FAILED( hr = g_pDI->CreateDevice( GUID_SysMouse, &g_pdidMouse, NULL ) ) )
return hr;
// Set the data format to “mouse 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
// DIMOUSESTATE structure to IDirectInputDevice::GetDeviceState.
if( FAILED( hr = g_pdidMouse->SetDataFormat( &c_dfDIMouse ) ) )
return hr;
// Set the cooperativity level to let DirectInput know how this device
// should interact with the system and with other DirectInput applications.
// Use DISCL_NONEXCLUSIVE to Retrieve mouse data when acquired, not
// interfering with any other applications which are reading mouse data.
// Use DISCL_FOREGROUND so that if the user switches away from our app,
// automatically release the mouse back to the system.
if( FAILED( hr = g_pdidMouse->SetCooperativeLevel( hWnd, DISCL_EXCLUSIVE | DISCL_FOREGROUND ) ) )
return hr;
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: DestroyDInput()
// Desc: Terminate our usage of DirectInput
//-----------------------------------------------------------------------------
VOID DestroyDInput(void)
{
// Destroy the mouse object
if( g_pdidMouse )
{
g_pdidMouse->Unacquire(); // Unacquire the device (just in case) before exitting.
g_pdidMouse->Release();
g_pdidMouse = NULL;
}
// Destroy the DInput object
if( g_pDI )
{
g_pDI->Release();
g_pDI = NULL;
}
}
//-----------------------------------------------------------------------------
// Name: UpdateDirectInputState()
// Desc: The game plays here. Read mouse data and displaying it.
//-----------------------------------------------------------------------------
VOID UpdateDirectInputState(void)
{
if( g_pdidMouse )
{
DIMOUSESTATE dims; /* DirectInput mouse state structure */
HRESULT hr;
hr = g_pdidMouse->GetDeviceState( sizeof(DIMOUSESTATE), &dims );
if( hr == DIERR_INPUTLOST )
{
// DInput is telling us that the input stream has been interrupted.
// We aren't tracking any state between polls, so just re-acquire
// and try again.
hr = g_pdidMouse->Acquire();
if( SUCCEEDED(hr) )
UpdateDirectInputState();
}
if( SUCCEEDED(hr) )
{
// Get x
MyMouseStatus.x += dims.lX;
// force mouse x to be inside 0 and winW
if (MyMouseStatus.x>winW)
MyMouseStatus.x=winW;
if (MyMouseStatus.x<0)
MyMouseStatus.x=0;
// Get y
MyMouseStatus.y -= dims.lY;
// force mouse y to be inside 0 and winH
if (MyMouseStatus.y>winH)
MyMouseStatus.y=winH;
if (MyMouseStatus.y<0)
MyMouseStatus.y=0;
if (dims.rgbButtons[0] & 0x80) // Button1 pressed????
MyMouseStatus.button1 = true;
else // Button1 released is (!dims.rgbButtons[0] & 0x80)
MyMouseStatus.button1 = false;
if (dims.rgbButtons[1] & 0x80) // Button2 pressed????
MyMouseStatus.button2 = true;
else // Button2 released is (!dims.rgbButtons[1] & 0x80)
MyMouseStatus.button2 = false;
if (dims.rgbButtons[2] & 0x80) // Button3 pressed????
MyMouseStatus.button3 = true;
else // Button3 released is (!dims.rgbButtons[2] & 0x80)
MyMouseStatus.button3 = false;
// In this case i change what i want by pressing Button1 and Button2
// ***** YOU CHANGE WHAT YOU WANT *****
if (MyMouseStatus.button1)
eye[2]+=dims.lY;
if (MyMouseStatus.button2)
{
eye[0]+=dims.lX;
eye[1]-=dims.lY;
}
}
}
}
Have nice programming!