Skip to main content
gamedev.net
🔒 Locked

Mouse Axis

Started by Loki Laughyjarson Aug 25, 2006 at 5:31 PM 6 replies 1.1k views
Original Post
Loki Laughyjarson
Loki Laughyjarson
Somebody please help. I am trying to get my mouse cordinates to read. I have read the help on here about Directx Input and the Mouse, but it doesn't seem to help me. I have no clue what to do, except that I know the mouse_state isn't equal to anything. I want both Mouse_X() and Mouse_Y() to return the mouse position on the screen. Here is my code:
Quote:
#include "dxinput.h" #define BUTTON_DOWN(obj, button) (obj.rgbButtons[button] & 0x80) LPDIRECTINPUT8 dinput; LPDIRECTINPUTDEVICE8 dimouse; LPDIRECTINPUTDEVICE8 dikeyboard; DIMOUSESTATE mouse_state; DIMOUSESTATE buffered_mouse; //keyboard input char keys[256]; int Init_DirectInput(HWND hwnd) { //initialize DirectInput object HRESULT result = DirectInput8Create( GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&dinput, NULL); if (result != DI_OK) return 0; //initialize the mouse result = dinput->CreateDevice(GUID_SysMouse, &dimouse, NULL); if (result != DI_OK) return 0; //initialize the keyboard result = dinput->CreateDevice(GUID_SysKeyboard, &dikeyboard, NULL); if (result != DI_OK) return 0; //clean return return 1; } int Init_Mouse(HWND hwnd) { //set the data format for mouse input HRESULT result = dimouse->SetDataFormat(&c_dfDIMouse); if (result != DI_OK) return 0; //set the cooperative level //this will also hide the mouse pointer result = dimouse->SetCooperativeLevel(hwnd, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND); if (result != DI_OK) return 0; //acquire the mouse result = dimouse->Acquire(); if (result != DI_OK) return 0; //give the go-ahead return 1; } //DIDEVICEOBJECTDATA rgdod[80]; //DWORD dwData = 80; int Mouse_X() { //mouse_state.lX = dimouse->GetDeviceData(sizeof(DIDEVICEOBJECTDATA), rgdod, &dwData, 0); return mouse_state.lX; } int Mouse_Y() { return mouse_state.lY; } int Mouse_Button(int button) { return BUTTON_DOWN(mouse_state, button); } void Poll_Mouse() { dimouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&mouse_state); } void Kill_Mouse() { if (dimouse != NULL) { dimouse->Unacquire(); dimouse->Release(); dimouse = NULL; } } int Init_Keyboard(HWND hwnd) { //set the data format for mouse input HRESULT result = dikeyboard->SetDataFormat(&c_dfDIKeyboard); if (result != DI_OK) return 0; //set the cooperative level result = dikeyboard->SetCooperativeLevel(hwnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND); if (result != DI_OK) return 0; //acquire the mouse result = dikeyboard->Acquire(); if (result != DI_OK) return 0; //give the go-ahead return 1; } void Poll_Keyboard() { dikeyboard->GetDeviceState(sizeof(keys), (LPVOID)&keys); } int Key_Down(int key) { return (keys[key] & 0x80); } void Kill_Keyboard() { if (dikeyboard != NULL) { dikeyboard->Unacquire(); dikeyboard->Release(); dikeyboard = NULL; } }
Loki Laughyjarson
Loki Laughyjarson
Is there anyway that I could change the mouse axis from relative to absolute? Then would it retreive it from the buffer with the correct position?
Evil Steve
Evil Steve
If you want screen coordinates, it's easier and more efficeint to just GetCursorPos(). Microsoft recommends against using DirectInput for mouse and keyboard input altogether.
Loki Laughyjarson
Loki Laughyjarson
Ok thank you. I've been looking at the help on it and can't seem to get it to work.I declare it.

PPOINT *mouse_axis;

Then in my Mouse_X() function I use it.

int Mouse_X()
{
Poll_Mouse();
GetCursorPos(*mouse_axis);
return mouse_state.lX;
}

It breaks at the GetCursorPos funtion. What code do I need to use so I can return mouse_state.lX my X cord. Or am I going about this all wrong?
Evil Steve
Evil Steve
GetCursorPos() expects you to allocate the memory for the POINT structure, and it fills the details in.
This should work:
POINT mouse_axis;int Mouse_X(){   // Poll_Mouse(); - No need to poll DInput, this is Win32 code   GetCursorPos(&mouse_axis);   return mouse_axis.x;}


GetCursorPos() has absolutely nothing to do with DirectInput. You don't need to even set up DirectInput to use GetCursorPos().
Loki Laughyjarson
Loki Laughyjarson
Thank you everyone that helped. I got it working. I really appreciate the help.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.