DirectInput troubles

Ok, simple. You look at my code, tell me what I did wrong, I learn something new, you gain the satisfaction of knowing that I will be back asking for more help very soon... I'm new to this so I don't even know if i've done all the setting up etc. correctly

Here is the code for setting up direct input etc...



LPDIRECTINPUT8 lpdi;
LPDIRECTINPUTDEVICE8 lpdiKeyboard;
LPDIRECTINPUTDEVICE8 lpdiMouse;

DIMOUSESTATE MouseState;

///////////////////////////////
// DirectInput Init()
//
////////////////////////////////

bool DIInit(HWND hWnd)
{
if (FAILED(DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION,
IID_IDirectInput8, (void**)&lpdi, NULL)))
return false;

// initialize the keyboard
if (FAILED(lpdi->CreateDevice(GUID_SysKeyboard, &lpdiKeyboard, NULL)))
return false;
if (FAILED(lpdiKeyboard->SetDataFormat(&c_dfDIKeyboard)))
return false;
if (FAILED(lpdiKeyboard->SetCooperativeLevel(hWnd, DISCL_BACKGROUND |
DISCL_NONEXCLUSIVE)))
return false;
if (FAILED(lpdiKeyboard->Acquire()))
return false;

// initialize the mouse
if (FAILED(lpdi->CreateDevice(GUID_SysMouse, &lpdiMouse, NULL)))
return false;
if (FAILED(lpdiMouse->SetCooperativeLevel(hWnd, DISCL_BACKGROUND |
DISCL_NONEXCLUSIVE)))
return false;
if (FAILED(lpdiMouse->SetDataFormat(&c_dfDIMouse)))
return false;
if (FAILED(lpdiMouse->Acquire()))
return false;

return true;
}

///////////////////////////
// Update DI devices
/////////////////////////////
UCHAR keystate[256];
DIMOUSESTATE mouse_state;

bool UpdateDeviceState(void)
{
if (FAILED(lpdiKeyboard->GetDeviceState(sizeof(UCHAR[256]), (LPVOID)keystate)))
return false;
if (FAILED(lpdiMouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&mouse_state)))
return false;

return true;
}


Now. Whenever I run the following function in the main program loop:



bool LMBDown()
{
if (MouseState.rgbButtons[0] & 0x80)
return TRUE;
else
return FALSE;
}


It appears to be returning false no matter wether I hold down LMB or not. Why?

Comments

  • You're declaring 2 DIMOUSESTATE structs.

    [code]
    [green]/* DIMOUSESTATE 1 (MouseState) */[/green]
    LPDIRECTINPUT8 lpdi;
    LPDIRECTINPUTDEVICE8 lpdiKeyboard;
    LPDIRECTINPUTDEVICE8 lpdiMouse;

    DIMOUSESTATE MouseState;

    [green]/* DIMOUSESTATE 2 (mouse_state) */
    ///////////////////////////
    // Update DI devices
    ///////////////////////////[/green]
    UCHAR keystate[256];
    DIMOUSESTATE mouse_state;
    [/code]

    Then when you call UpdateDeviceState() you use [b]mouse_state[/b] to retrieve the mouse state. But when you call LMBDown() you check [b]MouseState[/b] to see if the button is down.

    So either copy mouse_state into MouseState using memcpy() after you retrieve the mouse state or retrieve the mouse state with MouseState rather than mouse_state. OR check the left button state with mouse_state rather than MouseState.

    Simple :)
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion