This repository has been archived by the owner on Jan 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DirectInputSource.cpp
executable file
·133 lines (116 loc) · 3.59 KB
/
DirectInputSource.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*!\file DirectInputSource.cpp
* \brief Functions to handle input using DirectInput.
* \author Dean N. Butcher
* \version 2.1
* \date August 2004
*/
// These are the routines that support DirectInput that
// offers the alternative to Win32 messaging.
#define DIRECTINPUT_VERSION 0x0700 // create definitions for DirectX 7.
#include <windows.h>
#include <dinput.h>
#include "DirectInputIncludes.h"
const char DIERROR[] = "DirectInput Error...";
const int KEYBUFCOUNT = 256;
LPDIRECTINPUT lpDI;
LPDIRECTINPUTDEVICE pKeyboard;
char keyboardBuffer[KEYBUFCOUNT];
HRESULT directInputErrorDesc; // Handle used to store Direct Input Error.
extern HWND ghwnd;
bool DirectInputDevicesInitialisation(HINSTANCE &hInst)
{
// Create DI object.
if(DirectInputCreate(hInst, DIRECTINPUT_VERSION, &lpDI, NULL) != DI_OK)
{
MessageBox(NULL, "Failed to create DirectInput Object", DIERROR, MB_OK | MB_ICONEXCLAMATION);
return false;
}
// Create device
if(lpDI->CreateDevice(GUID_SysKeyboard, &pKeyboard, NULL) != DI_OK)
{
MessageBox(NULL, "Failed to create system keyboard", DIERROR, MB_OK | MB_ICONEXCLAMATION);
return false;
}
// Set data format for keyboard
if(pKeyboard->SetDataFormat(&c_dfDIKeyboard) != DI_OK)
{
MessageBox(NULL, "Failed to set data format for system keyboard", DIERROR, MB_OK | MB_ICONEXCLAMATION);
pKeyboard->Release();
pKeyboard = NULL;
return false;
}
// Set keyboard's priority
if(pKeyboard->SetCooperativeLevel(ghwnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE) != DI_OK)
{
MessageBox(NULL, "Failed to set cooperative level for system keyboard", DIERROR, MB_OK | MB_ICONEXCLAMATION);
pKeyboard->Release();
pKeyboard = NULL;
return false;
}
if(!DirectInputCheckForKeyboard())
return false; //The keyboard was not grabbed!
// All keyboard initialisation attempts successful.
return true;
}
bool DirectInputCheckForKeyboard()
{
// Acquire the keyboard device
if((directInputErrorDesc = pKeyboard->Acquire()) != DI_OK)
{
switch(directInputErrorDesc)
{
case DIERR_INVALIDPARAM:
{
MessageBox(NULL, "Failed to aquire system keyboard - INVALID PARAMETER", DIERROR, MB_OK | MB_ICONEXCLAMATION);
break;
}
case DIERR_NOTINITIALIZED:
{
MessageBox(NULL, "Failed to aquire system keyboard - NOT INITIALISED", DIERROR, MB_OK | MB_ICONEXCLAMATION);
break;
}
case DIERR_OTHERAPPHASPRIO:
{
MessageBox(NULL, "Failed to aquire system keyboard - OTHER APPLICATION HAS PRIORITY", DIERROR, MB_OK | MB_ICONEXCLAMATION);
break;
}
default:
{
MessageBox(NULL, "Failed to aquire system keyboard - ERROR UNKNOWN", DIERROR, MB_OK | MB_ICONEXCLAMATION);
break;
}
}
pKeyboard->Release();
pKeyboard = NULL;
return false;
}
return true;
}
bool DeleteDirectInputResources()
{
if(pKeyboard)
{
pKeyboard->Unacquire();
pKeyboard->Release();
pKeyboard = NULL;
}
// Just incase something should go wrong.
if(pKeyboard) // If the keyboard should still exist.
{
MessageBox(NULL, "Keyboard device freed without having been acquired.", DIERROR, MB_OK | MB_ICONEXCLAMATION);
}
if(lpDI)
{
lpDI->Release();
lpDI = NULL;
}
// Just incase something should go wrong.
if(lpDI) // If the keyboard object should still exist.
{
MessageBox(NULL, "Keyboard object freed without having been acquired.", DIERROR, MB_OK | MB_ICONEXCLAMATION);
}
if(!pKeyboard && !lpDI) // If releases were successful
return true;
else
return false;
}