-
Notifications
You must be signed in to change notification settings - Fork 0
/
InputManager.cpp
89 lines (73 loc) · 2.85 KB
/
InputManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//------------------------------------------------------------------------------
// File: InputManager.cpp
// Desc: Wrapper for directinput
//
// Created: 15 September 2005 14:52:26
//
// (c)2005 Neil Wakefield, Lionhead Studios Ltd
//------------------------------------------------------------------------------
#pragma warning( disable : 4995 )
//------------------------------------------------------------------------------
// Included files:
//------------------------------------------------------------------------------
#include "dxstdafx.h"
#include "InputManager.h"
//------------------------------------------------------------------------------
// Statics:
//------------------------------------------------------------------------------
LPDIRECTINPUT8 GInputManager::DirectInput = NULL;
LPDIRECTINPUTDEVICE8 GInputManager::KeyboardDevice = NULL;
char GInputManager::KeyState[256] = {0};
char GInputManager::PreviousKeyState[256] = {0};
//------------------------------------------------------------------------------
// Definitions:
//------------------------------------------------------------------------------
bool GInputManager::Initialise()
{
if( FAILED( DirectInput8Create( DXUTGetHINSTANCE(), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&DirectInput, NULL ) ) )
{
return false;
}
if( FAILED( DirectInput->CreateDevice( GUID_SysKeyboard, &KeyboardDevice, NULL ) ) )
{
SAFE_RELEASE( DirectInput );
return false;
}
if( FAILED( KeyboardDevice->SetDataFormat( &c_dfDIKeyboard ) ) ||
FAILED( KeyboardDevice->SetCooperativeLevel( DXUTGetHWND(), DISCL_FOREGROUND | DISCL_NONEXCLUSIVE ) ) )
{
SAFE_RELEASE( KeyboardDevice );
SAFE_RELEASE( DirectInput );
return false;
}
return true;
}
//------------------------------------------------------------------------------------------------------------
bool GInputManager::Shutdown()
{
SAFE_RELEASE( KeyboardDevice );
SAFE_RELEASE( DirectInput );
return true;
}
//------------------------------------------------------------------------------------------------------------
bool GInputManager::Process()
{
assert( KeyboardDevice );
memcpy( PreviousKeyState, KeyState, sizeof( PreviousKeyState ) );
memset( KeyState, 0, sizeof( KeyState ) );
if( FAILED( KeyboardDevice->GetDeviceState( sizeof( KeyState ), KeyState ) ) )
{
KeyboardDevice->Acquire();
}
return true;
}
//------------------------------------------------------------------------------------------------------------
bool GInputManager::IsKeyDown( unsigned char key )
{
return( (KeyState[ key ] & 0x80) != 0 );
}
//------------------------------------------------------------------------------------------------------------
bool GInputManager::WasKeyDown( unsigned char key )
{
return( (PreviousKeyState[ key ] & 0x80) != 0 );
}