Original Post
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; } }