-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added filters and renamed InputHandler to DIInputHandler to signify t…
…hat it uses DirectInput
- Loading branch information
Mattias Liljeson
committed
Nov 16, 2012
1 parent
5397ed7
commit 8aaae7f
Showing
5 changed files
with
228 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
#include "DIInputHandler.h" | ||
|
||
DIInputHandler::DIInputHandler(HINSTANCE* _hInstance, HWND* _hWnd) | ||
{ | ||
hInstance = _hInstance; | ||
hWnd = _hWnd; | ||
|
||
// create the DirectInput interface | ||
DirectInput8Create(*hInstance, // the handle to the application | ||
DIRECTINPUT_VERSION, // the compatible version | ||
IID_IDirectInput8, // the DirectInput interface version | ||
(void**)&din, // the pointer to the interface | ||
NULL); // COM stuff, so we'll set it to NULL | ||
|
||
// create the keyboard device | ||
din->CreateDevice(GUID_SysKeyboard, // the default keyboard ID being used | ||
&dinkeyboard, // the pointer to the device interface | ||
NULL); // COM stuff, so we'll set it to NULL | ||
din->CreateDevice(GUID_SysMouse, | ||
&dinmouse, | ||
NULL); | ||
|
||
// set the data format to keyboard format | ||
dinkeyboard->SetDataFormat(&c_dfDIKeyboard); | ||
dinmouse->SetDataFormat(&c_dfDIMouse); | ||
|
||
// set the control we will have over the keyboard | ||
dinkeyboard->SetCooperativeLevel(*hWnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND); | ||
dinmouse->SetCooperativeLevel(*hWnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND); | ||
|
||
reset(); | ||
|
||
// Debug Vars | ||
mouseSpeedX = 1.0f; | ||
|
||
for ( int i=0; i<NUM_MOUSE_KEYS; i++) | ||
{ | ||
m_mouseKeys[i] = KEY_UP; | ||
} | ||
|
||
} | ||
|
||
DIInputHandler::~DIInputHandler() | ||
{ | ||
dinkeyboard->Unacquire(); // make sure the keyboard is unacquired | ||
dinmouse->Unacquire(); // make sure the mouse in unacquired | ||
din->Release(); // close DirectInput before exiting | ||
} | ||
|
||
void DIInputHandler::reset() | ||
{ | ||
keys[A] = 0; | ||
keys[S] = 0; | ||
keys[D] = 0; | ||
keys[W] = 0; | ||
keys[SPACE] = 0; | ||
keys[LCTRL] = 0; | ||
keys[F1] = 0; | ||
keys[F2] = 0; | ||
keys[F3] = 0; | ||
keys[F4] = 0; | ||
|
||
mouse[X] = 0; | ||
mouse[Y] = 0; | ||
} | ||
|
||
void DIInputHandler::detectInput(void) | ||
{ | ||
// get access if we don't have it already | ||
dinkeyboard->Acquire(); | ||
dinmouse->Acquire(); | ||
|
||
// get the input data | ||
dinkeyboard->GetDeviceState(256, (LPVOID)keystate); | ||
dinmouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&mousestate); | ||
} | ||
|
||
void DIInputHandler::update() | ||
{ | ||
reset(); | ||
detectInput(); | ||
|
||
if(keystate[DIK_W] & 0x80) | ||
keys[W] = true; | ||
if(keystate[DIK_A] & 0x80) | ||
keys[A] = true; | ||
if(keystate[DIK_S] & 0x80) | ||
keys[S] = true; | ||
if(keystate[DIK_D] & 0x80) | ||
keys[D] = true; | ||
if(keystate[DIK_SPACE] & 0x80) | ||
keys[SPACE] = true; | ||
if(keystate[DIK_LSHIFT] & 0x80) //DEBUG: should be LCONTROL | ||
keys[LCTRL] = true; | ||
|
||
if(keystate[DIK_F1] & 0x80) | ||
keys[F1] = true; | ||
if(keystate[DIK_F2] & 0x80) | ||
keys[F2] = true; | ||
if(keystate[DIK_F3] & 0x80) | ||
keys[F3] = true; | ||
if(keystate[DIK_F4] & 0x80) | ||
keys[F4] = true; | ||
|
||
if(keystate[DIK_ESCAPE] & 0x80) | ||
PostMessage(*hWnd, WM_DESTROY, 0, 0); | ||
|
||
for( int i=0; i<NUM_MOUSE_KEYS; i++) | ||
{ | ||
if( m_mouseKeys[i] == KEY_UP ) | ||
{ | ||
if( mousestate.rgbButtons[i] ) | ||
m_mouseKeys[i] = KEY_PRESSED; | ||
else | ||
m_mouseKeys[i] = KEY_UP; | ||
} | ||
else if( m_mouseKeys[i] == KEY_PRESSED ) | ||
{ | ||
if( mousestate.rgbButtons[i] ) | ||
m_mouseKeys[i] = KEY_DOWN; | ||
else | ||
m_mouseKeys[i] = KEY_RELEASED; | ||
} | ||
else if( m_mouseKeys[i] == KEY_DOWN ) | ||
{ | ||
if( mousestate.rgbButtons[i] ) | ||
m_mouseKeys[i] = KEY_DOWN; | ||
else | ||
m_mouseKeys[i] = KEY_RELEASED; | ||
} | ||
else if( m_mouseKeys[i] == KEY_RELEASED ) | ||
{ | ||
if( mousestate.rgbButtons[i] ) | ||
m_mouseKeys[i] = KEY_PRESSED; | ||
else | ||
m_mouseKeys[i] = KEY_UP; | ||
} | ||
else | ||
{ | ||
// should NEVER happen! | ||
m_mouseKeys[i] = KEY_UP; | ||
} | ||
} | ||
} | ||
|
||
bool DIInputHandler::getKey(int key) | ||
{ | ||
return keys[key]; | ||
} | ||
|
||
int DIInputHandler::getMouseKeyState( int p_key ) | ||
{ | ||
return m_mouseKeys[p_key]; | ||
} | ||
|
||
long DIInputHandler::getMouse(int axis) | ||
{ | ||
//DEBUG: return std values | ||
if(axis == X) | ||
{ | ||
return mousestate.lX; | ||
} | ||
else if (axis == Y) | ||
{ | ||
return mousestate.lY; | ||
} | ||
else | ||
{ | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#ifndef DIINPUTHANDLER_H | ||
#define DIINPUTHANDLER_H | ||
|
||
#include <dinput.h> | ||
|
||
#pragma comment (lib, "dinput8.lib") | ||
#pragma comment (lib, "dxguid.lib") | ||
|
||
class DIInputHandler | ||
{ | ||
//Enums | ||
public: | ||
enum KEY_STATE{ KEY_DOWN, KEY_PRESSED, KEY_UP, KEY_RELEASED }; | ||
enum KEYBOARD{ W, A, S, D, SPACE, LCTRL, F1, F2, F3, F4, NUM_KEYB_KEYS }; | ||
enum MOUSE_KEY{ M_LBTN, M_MBTN, M_RBTN, NUM_MOUSE_KEYS }; | ||
enum MOUSE{ X, Y, NUM_MOUSE_AXIS }; | ||
|
||
private: | ||
HINSTANCE* hInstance; | ||
HWND* hWnd; | ||
LPDIRECTINPUT8 din; // the pointer to our DirectInput interface | ||
LPDIRECTINPUTDEVICE8 dinkeyboard; // the pointer to the keyboard device | ||
LPDIRECTINPUTDEVICE8 dinmouse; // the pointer to the mouse device | ||
BYTE keystate[256]; // the storage for the key-information | ||
DIMOUSESTATE mousestate; // the storage for the mouse-information | ||
|
||
bool keys[NUM_KEYB_KEYS]; | ||
|
||
long mouse[NUM_MOUSE_AXIS]; | ||
int m_mouseKeys[NUM_MOUSE_KEYS]; | ||
|
||
// For the debug GUI | ||
bool autoRotate; | ||
float mouseSpeedX; | ||
|
||
public: | ||
DIInputHandler(HINSTANCE* hInstance, HWND* hWnd); // sets up and initializes DirectInput | ||
~DIInputHandler(); //closes DirectInput and releases memory | ||
void reset(); | ||
void detectInput(void); // gets the current input state | ||
void update(); | ||
bool getKey(int key); | ||
int getMouseKeyState( int p_key); | ||
long getMouse(int axis); | ||
}; | ||
|
||
#endif //INPUTHANDLER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters