Navigation
Artikel
Stuff
RSS Feeds
|
Sourcecodes - DirectInput Input ManagerSprachenübersicht/C / C++/ C#/Spieleprogrammierung/DirectX Keywords: DirectInput Klasse, DirectX Der Folgende Quellcode beinhaltet eine DirectInput Klasse, die sich um die Maus-und Tastatur-eingabe kümmert.
Usage: int iMouseX,iMouseY; bn_MouseData MousePosition; bn_KeyboardData KeyboardPosition; bn_InputManager InputManager; InputManager.Initialise(hWnd, hInstance); InputManager.InitKeyboard(hWnd); InputManager.InitMouse(hWnd); [...] //Abfrageschleife InputManager.GetKeyboardInput(&KeyboardPosition); InputManager.GetMouseInput(&MousePosition); InputManager.GetCursorPosition(&iMouseX, &iMouseY); //Benutzung der Daten MeineFunktion(MousePosition.iMouseButton0):
direct_input.h: /**************************************************************************/ // /includes/input/input_manager.h // // This file manages the mouse/keyboard input of the project /**************************************************************************/ /**************************************************************************/ // Defines /**************************************************************************/ #ifndef FILE_9_INPUT_MANAGER #define FILE_9_INPUT_MANAGER #define DIRECTINPUT_VERSION 0x0800 /**************************************************************************/ // Header /**************************************************************************/ #include <dinput.h> #include <errorfile.h> namespace bn_Core { //Structure for the mouse struct DLLEXPORT bn_MouseData { int iMousePositionX; int iMousePositionY; int iMouseMoveX; int iMouseMoveY; int iMouseButton0; int iMouseButton1; }; //Structure for the keyboard struct DLLEXPORT bn_KeyboardData { bool bKeyboard[256]; }; //Class for DirectInput class DLLEXPORT bn_InputManager { public: //Constructor bn_InputManager(); //desctructor ~bn_InputManager(); //Initialise the class void Initialise(HWND hWnd,HINSTANCE hInstance); //Initialise the keyboard void InitKeyboard(HWND hWnd,DWORD dwFlags = DISCL_FOREGROUND | DISCL_NONEXCLUSIVE); //Initialise the keyboard void InitMouse(HWND hWnd,DWORD dwFlags = DISCL_FOREGROUND | DISCL_NONEXCLUSIVE); //Gets the mouse information back void GetMouseInput(bn_MouseData *Mouse); //Gets the mouse information back void GetKeyboardInput(bn_KeyboardData *Keyboard); //Gets the cursor position void GetCursorPosition(int *pX,int *pY); protected: //Pointer to the DirectInput 8 object LPDIRECTINPUT8 m_lpDirectInput; //The device objects LPDIRECTINPUTDEVICE8 m_lpDirectInputKeyboard; LPDIRECTINPUTDEVICE8 m_lpDirectInputMouse; }; } #endif
direct_input.cpp: //******************************* FOR MORE SEE THE .h file ********************************// #include "input_manager.h" //Initialise CDirectXGraphics bn_Core::bn_InputManager::bn_InputManager() { //Sets it NULL m_lpDirectInput = NULL; m_lpDirectInputKeyboard = NULL; } //Initialise CDirectXGraphics bn_Core::bn_InputManager::~bn_InputManager() { //clears the object if (NULL != m_lpDirectInput) { m_lpDirectInput->Release(); m_lpDirectInput = NULL; } //clears the object if (NULL != m_lpDirectInputMouse) { m_lpDirectInputMouse->Unacquire(); m_lpDirectInputMouse->Release(); m_lpDirectInputMouse = NULL; } //clears the object if (NULL != m_lpDirectInputKeyboard) { m_lpDirectInputKeyboard->Unacquire(); m_lpDirectInputKeyboard->Release(); m_lpDirectInputKeyboard = NULL; } } //Initialize the keyboard void bn_Core::bn_InputManager::InitKeyboard(HWND hWnd,DWORD dwFlags) { //Create the DirectInputKeyboard Object DXTestForError(m_lpDirectInput->CreateDevice(GUID_SysKeyboard,&m_lpDirectInputKeyboard,NULL),"Error, can't create the DirectInputKeyboard Object.",4,1); //Sets the Data Format DXTestForError(m_lpDirectInputKeyboard->SetDataFormat(&c_dfDIKeyboard),"Error, can't set the Data Format. (keyboard)",5,1); //Sets the CooperativeLevel DXTestForError(m_lpDirectInputKeyboard->SetCooperativeLevel(hWnd,dwFlags),"Error, can't set the cooperative mode.",6,1); //Activates the keyboard (gets the focus) m_lpDirectInputKeyboard->Acquire(); } //Initialise the class void bn_Core::bn_InputManager::Initialise(HWND hWnd,HINSTANCE hInstance) { //Creates the DirectInput Object DXTestForError(DirectInput8Create(hInstance,DIRECTINPUT_VERSION,IID_IDirectInput8,(LPVOID*)&m_lpDirectInput,NULL),"Error, can't create the DirectInput8 Object."$ } //Initialise the mouse void bn_Core::bn_InputManager::InitMouse(HWND hWnd,DWORD dwFlags) { //Create the DirectInputKeyboard Object DXTestForError(m_lpDirectInput->CreateDevice(GUID_SysMouse,&m_lpDirectInputMouse,NULL),"Error, can't create the DirectInputMouse Object.",7,1); //Sets the Data Format DXTestForError(m_lpDirectInputMouse->SetDataFormat(&c_dfDIMouse),"Error, can't set the Data Format (mouse).",8,1); //Sets the CooperativeLevel DXTestForError(m_lpDirectInputMouse->SetCooperativeLevel(hWnd,dwFlags),"Error, can't set the cooperative mode.",9,1); //Properties DIPROPDWORD DirectInputProperties; //Sets the settings ZeroMemory(&DirectInputProperties,sizeof(DirectInputProperties)); //Sets the size DirectInputProperties.diph.dwSize = sizeof(DIPROPDWORD); DirectInputProperties.diph.dwHeaderSize = sizeof(DIPROPHEADER); DirectInputProperties.diph.dwObj = 0; DirectInputProperties.diph.dwHow = DIPH_DEVICE; DirectInputProperties.dwData = 16; //Sets the proberties m_lpDirectInputMouse->SetProperty(DIPROP_BUFFERSIZE,&DirectInputProperties.diph); //Activates the mouse (gets the focus) m_lpDirectInputMouse->Acquire(); } //Returns the mouse information void bn_Core::bn_InputManager::GetMouseInput(bn_MouseData *Mouse) { //Gets the Result of the Acquire function HRESULT hrResult; //Sets the mousemove 0 Mouse->iMouseMoveX = 0; Mouse->iMouseMoveY = 0; //Reservates the elements DWORD dwNumberOfElements = 4; DIDEVICEOBJECTDATA Data[4]; ZeroMemory(&Data,sizeof(DIDEVICEOBJECTDATA)); //If the mouse focus is set by a other window take it back if (FAILED(m_lpDirectInputMouse->GetDeviceData(sizeof(DIDEVICEOBJECTDATA),Data,&dwNumberOfElements,0))) { //Takes the controll back hrResult = m_lpDirectInputMouse->Acquire(); while(hrResult == DIERR_INPUTLOST) hrResult = m_lpDirectInputMouse->Acquire(); } //Gets the Elements for(int i = 0;i < (int)dwNumberOfElements;i++) { switch(Data[i].dwOfs) { //If a button is pressed case DIMOFS_BUTTON0: { //Button is pressed if (Data[i].dwData) Mouse->iMouseButton0 = 1; //Button is down else Mouse->iMouseButton0 = 0; break; } //If a button is pressed case DIMOFS_BUTTON1: { //Button is pressed if (Data[i].dwData) Mouse->iMouseButton1 = 1; //Button is down else Mouse->iMouseButton1 = 0; break; } //If a button is pressed case DIMOFS_BUTTON1: { //Button is pressed if (Data[i].dwData) Mouse->iMouseButton1 = 1; //Button is down else Mouse->iMouseButton1 = 0; break; } //Gets the x koordinates case DIMOFS_X: { Mouse->iMouseMoveX = Data[i].dwData; Mouse->iMousePositionX += Mouse->iMouseMoveX; break; } //Gets the y koordinates case DIMOFS_Y: { Mouse->iMouseMoveY = Data[i].dwData; Mouse->iMousePositionY += Mouse->iMouseMoveY; break; } } } } //Gets the mouse information back void bn_Core::bn_InputManager::GetKeyboardInput(bn_KeyboardData *Keyboard) { //Gets the Result of the Acquire function HRESULT hrResult; //The keys bool bKeyboard[256]; //Copies the array into the structure for(int z=0;z<256;z++) bKeyboard[z] = false; //If the mouse focus is set by a other window take it back if (FAILED(m_lpDirectInputKeyboard->GetDeviceState(sizeof(bKeyboard),(LPVOID)&bKeyboard))) { //Takes the controll back hrResult = m_lpDirectInputKeyboard->Acquire(); while(hrResult == DIERR_INPUTLOST) hrResult = m_lpDirectInputKeyboard->Acquire(); } //Copies the array into the structure for(int i=0;i<256;i++) Keyboard->bKeyboard[i] = bKeyboard[i]; } //Gets the cursor position void bn_Core::bn_InputManager::GetCursorPosition(int *pX,int *pY) { //Point POINT lpPoint; //Gets the cursor position GetCursorPos(&lpPoint); //Sets x,y *pX = lpPoint.x; *pY = lpPoint.y; } Gibt es noch irgendwelche Fragen, oder wollen Sie über den Artikel diskutieren? Sprachenübersicht/C / C++/ C#/Spieleprogrammierung/DirectX/DirectInput Input Manager |