This topic is 4301 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am starting directX still. I have managed to setup a graphics core, but it is mostly a copy of code from a book. I am customizing more of it, but I want to be able to get input first. I can read the keyboard, but the mouse is more complex. This is what I have:
bool eInput::InitMouse()
{

// Create the device object
if(FAILED(iDI->CreateDevice(GUID_SysMouse, &iDIMouse, NULL)))
{
Error(6);
return false;
}

mdatastruct.diph.dwSize       = sizeof(DIPROPDWORD);
mdatastruct.diph.dwObj        = 0;
mdatastruct.diph.dwHow        = DIPH_DEVICE;
// the data
mdatastruct.dwData            = 32;

if(FAILED(iDIMouse->SetProperty(DIPROP_BUFFERSIZE, &mdatastruct.diph)))
{

// Set the data format
if(FAILED(iDIMouse->SetDataFormat(&c_dfDIMouse))) {
iDIMouse->Release();
Error(6);
return false;
}


This basically sets the mouse, correct? I need to read from mdatastruct. I have a function mpros for that, but it is empty... I have read the DXSDK docs and a book that I have, but they both are confusing, and the source doesn't make sense for my context. I would be most greatful (grateful?) if someone could explain how to read from my nice little buffer :) Oh, here is the eInput class:
class eInput
{
protected:
HWND hwnd;
IDirectInput8 *iDI;
IDirectInputDevice8 *iDIKeyboard;
IDirectInputDevice8 *iDIMouse;
HANDLE mevent;
// IDirectInputDevice8 *iDIJoy;
char kdatabuffer[256];
DIPROPDWORD mdatastruct;
DIMOUSESTATE mstate;
long mY, mX, mZ;

public:
eInput();
~eInput();
bool Kpros();
bool Mpros();
bool InitKeyboard();
bool InitMouse();
// bool InitJoy();
bool Init(HWND hWnd, HINSTANCE hinst);
bool Shutdown();
};



##### Share on other sites
here is an example of how to do it written by LaMothe:
(error testing removed for clarity)

iDIMouse->Acquire();

//Make a global variable to read the mouse:
DIMOUSESTATE mouse_state;
...
// read the current mouse state like this:
iDIMouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)mouse_state); // (the last argument should probably be (LPVOID)&mouse_state. Just a typo in the book I guess)

// move the object (or what ever...)
object.x += mouse_state.lX;
...
if(mouse_state.rgbButtons[0] & 0x80)
// left button pressed...
...

I have seen other ways to do this but this is the simplest way to do it that I know of.
(Also note that right button = 1 and middle button = 2)

edit:
If you do it this way, there is no need for either the SetProperty function or the mdatastruct at all.
Just replace the call to SetProperty with
iDIMouse->SetCooperativeLevel(hwnd, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE);// or some other flags appropriate for your application

[Edited by - pulpfist on May 17, 2006 2:15:53 PM]