|
From: w.y. <yok...@be...> - 2003-08-07 09:47:41
|
FerretallicA wrote:
>
> Still no luck. This is a f**king nightmare. Mayhaps a different approach...
> instead of asking who has a solution to getting it working, how about I ask
> this- put your hands up if you have DirectInput working in DevC++ 4.x... am
> I the only one having problems with it?
I copied and pasted from the examples from my book and got something
working. This uses Direct Input to read the mouse. It would take some
more tweaking to get something more useful, but this is something
at least. Assuming you have some basic windows app with menu choices,
pick a menu choice to use and call the 3 functions like this:
initDImouse(MainInstance, hwnd);
readDIMouse(hwnd);
shutDownDImouse();
"MainInstance" is a global I'm using obtained from the HINSTANCE
you get from WinMain. "hwnd" you probably know..
Merge the code below with your app, or put in a separate file (you'll
need to make a header file). The only library I included was
libdinput8.a (and libwinmm.a for something else) and I'm using
DevC++ 4.9.8.0
This all compiles with no problem for me.
////////////////////////////////////////////////////////////////
#define INITGUID
#include<windows.h>
#include<windowsx.h>
#include<dinput.h>
#include<cstdio>
#include"DImouse.h"
LPDIRECTINPUT8 lpdi = NULL; // dinput object
LPDIRECTINPUTDEVICE8 lpdimouse = NULL; // dinput mouse
const int winWidth = 640;
const int winHeight = 480;
int initDImouse(HINSTANCE, HWND);
int shutDownDImouse();
int readDIMouse(HWND);
int initDImouse(HINSTANCE hinstance,HWND hwnd)
{
DirectInput8Create(hinstance,DIRECTINPUT_VERSION,IID_IDirectInput8,
(void **)&lpdi,NULL);
// create a mouse device /////////////////////////////////////
lpdi->CreateDevice(GUID_SysMouse, &lpdimouse, NULL);
// set cooperation level
lpdimouse->SetCooperativeLevel(hwnd, DISCL_NONEXCLUSIVE |
DISCL_BACKGROUND);
// set data format
lpdimouse->SetDataFormat(&c_dfDIMouse);
// acquire the mouse
lpdimouse->Acquire();
return 1;
}
int shutDownDImouse()
{
// first unacquire the mouse
lpdimouse->Unacquire();
// now release the mouse
lpdimouse->Release();
// release directinput
lpdi->Release();
return 1;
}
int readDIMouse(HWND hwnd)
{
DIMOUSESTATE mouse_state;
int mouse_x = winWidth >> 1,
mouse_y = winHeight >> 1, prevX=mouse_x, prevY=mouse_y;
HDC hdc = GetDC(hwnd);
do
{ // get the input from the mouse
lpdimouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&mouse_state);
// move the mouse cursor
mouse_x += (mouse_state.lX); // relative movement
mouse_y += (mouse_state.lY); // returned....
// test bounds
// first x boundaries
if (mouse_x >= winWidth) mouse_x = winWidth-1;
else if (mouse_x < 0) mouse_x = 0;
// now the y boundaries
if (mouse_y >= winHeight) mouse_y= winHeight-1;
else if (mouse_y < 0) mouse_y = 0;
// Update output if changed
if ( (prevX != mouse_x) || (prevY != mouse_y) )
{
prevX = mouse_x; prevY = mouse_y;
char strbuf[250];
sprintf(strbuf,"Right click to exit. Direct Mouse: %3i, %3i
",mouse_x, mouse_y);
TextOut(hdc,10,20,strbuf,strlen(strbuf));
}
} while ( !mouse_state.rgbButtons[0] );
ReleaseDC(hwnd,hdc);
return 1;
}
|