Skip to content

Commit

Permalink
Added filters and renamed InputHandler to DIInputHandler to signify t…
Browse files Browse the repository at this point in the history
…hat it uses DirectInput
  • Loading branch information
Mattias Liljeson committed Nov 16, 2012
1 parent 5397ed7 commit 8aaae7f
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 10 deletions.
171 changes: 171 additions & 0 deletions DIInputHandler.cpp
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;
}
}
47 changes: 47 additions & 0 deletions DIInputHandler.h
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
10 changes: 5 additions & 5 deletions Menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ MenuShit::~MenuShit()
delete systemInterface;
}

void MenuShit::init(InputHandler* _input, Timer* _timer, int _wndWidth, int _wndHeight,
void MenuShit::init(DIInputHandler* _input, Timer* _timer, int _wndWidth, int _wndHeight,
ID3D10Device* _device, ID3D10Effect* _effect, int _techNr, int _passNr)
{
input = _input;
Expand Down Expand Up @@ -111,14 +111,14 @@ void MenuShit::update(float _dt)

context->ProcessMouseMove(cursorPos.x, cursorPos.y,0);

if( input->getMouseKeyState( InputHandler::M_LBTN ) == InputHandler::KEY_PRESSED )
if( input->getMouseKeyState( DIInputHandler::M_LBTN ) == DIInputHandler::KEY_PRESSED )
context->ProcessMouseButtonDown( 0, 0 );
else if( input->getMouseKeyState( InputHandler::M_LBTN ) == InputHandler::KEY_RELEASED )
else if( input->getMouseKeyState( DIInputHandler::M_LBTN ) == DIInputHandler::KEY_RELEASED )
context->ProcessMouseButtonUp( 0, 0 );

if( input->getMouseKeyState( InputHandler::M_RBTN ) == InputHandler::KEY_PRESSED )
if( input->getMouseKeyState( DIInputHandler::M_RBTN ) == DIInputHandler::KEY_PRESSED )
context->ProcessMouseButtonDown( 0, 0 );
else if( input->getMouseKeyState( InputHandler::M_RBTN ) == InputHandler::KEY_RELEASED )
else if( input->getMouseKeyState( DIInputHandler::M_RBTN ) == DIInputHandler::KEY_RELEASED )
context->ProcessMouseButtonUp( 0, 0 );

context->Update();
Expand Down
6 changes: 3 additions & 3 deletions Menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <string>

#include "InputHandler.h"
#include "DIInputHandler.h"
#include "Timer.h"
#include "RenderInterfaceDx10.h"
#include "SystemInterfaceDx10.h"
Expand All @@ -16,7 +16,7 @@ using namespace std;
class MenuShit
{
private:
InputHandler* input;
DIInputHandler* input;
//Timer* timer;
RenderInterfaceDx10* renderInterface;
SystemInterfaceDx10* systemInterface;
Expand All @@ -31,7 +31,7 @@ class MenuShit
public:
MenuShit();
~MenuShit();
void init(InputHandler* _input, Timer* _timer, int _wndWidth, int _wndHeight,
void init(DIInputHandler* _input, Timer* _timer, int _wndWidth, int _wndHeight,
ID3D10Device* _device, ID3D10Effect* _effect, int _techNr, int _passNr);
bool setDocument(string filename);
void releaseContext();
Expand Down
4 changes: 2 additions & 2 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "Menu.h"
#include "DeviceHandler.h"
#include "Timer.h"
#include "InputHandler.h"
#include "DIInputHandler.h"

#include <iostream>

Expand All @@ -13,7 +13,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,

DeviceHandler* deviceHandler = new DeviceHandler( hInstance, wndWidth, wndHeight);
Timer* timer = new Timer();
InputHandler* inputHandler = new InputHandler( &hInstance, deviceHandler->getHWnd());
DIInputHandler* inputHandler = new DIInputHandler( &hInstance, deviceHandler->getHWnd());
//inputHandler->i
MenuShit* menu = new MenuShit();
menu->init(inputHandler, timer, wndWidth, wndHeight,
Expand Down

0 comments on commit 8aaae7f

Please sign in to comment.