Sign in to follow this  

DirectInput troubles (losing the mouse and huge values)

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have two problems with DirectInput. First, on every poll I make, I lose the mouse so have to aquire it again. What could be causing this? I have the coop mode set to foreground and exclusive. On top of this, I get strange numbers of my mouse values, like 84215040, and -1684300928. How do I make sense of these values? Also, the X and Y values are always the same. Thanks. -Nick

Share this post


Link to post
Share on other sites
Here's Mouse.h
[source="C++"]
#include <dinput.h>

#include "Exception.h"
#include "NLogFile.h"
#include "Keys.h"
#include "Vector3f.h"

#pragma once

class Mouse
{

private:
LPDIRECTINPUT8 g_lpDI;
LPDIRECTINPUTDEVICE8 g_lpDIDevice;
DIMOUSESTATE2 mouse_state;
Vector3f mousePosition;


public:
Mouse(void);
~Mouse(void);
void Initialize(HINSTANCE hInstance, HWND hWnd);
void CleanUp();
bool ButtonDown(int button);
void SetCursorPosition(Vector3f positon);
Vector3f GetCursorPosition();
void PollMouse();
};




Mouse.cpp
[source="C++"]
#include ".\mouse.h"

Mouse::Mouse(void)
{

}

Mouse::~Mouse(void)
{
}

void Mouse::Initialize(HINSTANCE hInstance, HWND hWnd)
{
HRESULT hr;

hr = DirectInput8Create(hInstance, DIRECTINPUT_VERSION,
IID_IDirectInput8, (void**)&g_lpDI, NULL);

if FAILED(hr)
{
throw Exception("Could not initiate DirectInput");
}

hr = g_lpDI->CreateDevice(GUID_SysMouse, &g_lpDIDevice, NULL);
if FAILED(hr)
{
CleanUp();
throw Exception("Could not create Mouse");
}

hr = g_lpDIDevice->SetDataFormat(&c_dfDIMouse);

if FAILED(hr)
{
CleanUp();
throw Exception("Could not set data format");
}

hr = g_lpDIDevice->SetCooperativeLevel(hWnd,
DISCL_FOREGROUND | DISCL_EXCLUSIVE);


if FAILED(hr)
{
CleanUp();
throw Exception("Could not set cooperation level");
}

if (g_lpDIDevice)
{
g_lpDIDevice->Acquire();
}
}

void Mouse::CleanUp(void)
{
if(g_lpDI)
{
g_lpDI->Release();
}
if(g_lpDIDevice)
{
g_lpDIDevice->Unacquire();
g_lpDIDevice->Release();
}
}

void Mouse::PollMouse()
{
HRESULT hr;

hr = g_lpDIDevice->GetDeviceState(sizeof(DIMOUSESTATE2), (LPVOID)&mouse_state);
if FAILED(hr)
{
NLogFile * lf=NLogFile::getInstance();
lf->write("Lost the Mouse");
g_lpDIDevice->Acquire();
//throw Exception("Mouse Lost");
}
mousePosition.x+=mouse_state.lX;
mousePosition.y+=mouse_state.lY;
mousePosition.z+=mouse_state.lZ;

}

bool Mouse::ButtonDown(int button)
{
PollMouse();
return (mouse_state.rgbButtons[button] & 0x80)==0;

}

Vector3f Mouse::GetCursorPosition()
{
return mousePosition;
}

void Mouse::SetCursorPosition(Vector3f position)
{
mousePosition=position;
}




Poll is called at the beginning of every update (50 calls/second).

Share this post


Link to post
Share on other sites
I don't know if it is the problem, but since you're using DIMOUSESTATE2

g_lpDIDevice->GetDeviceState(sizeof(DIMOUSESTATE2), (LPVOID)&mouse_state);

you should change

hr = g_lpDIDevice->SetDataFormat(&c_dfDIMouse);

to

hr = g_lpDIDevice->SetDataFormat(&c_dfDIMouse2);

I hope that solves your problem

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this