-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgamepad.cpp
More file actions
executable file
·147 lines (125 loc) · 2.95 KB
/
gamepad.cpp
File metadata and controls
executable file
·147 lines (125 loc) · 2.95 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "gamepad.h"
std::string Gamepad::getLastError()
{
return error;
}
// === Constructeur : Invocation de DI
Gamepad::Gamepad()
{
error="";
id_count=0;
id_pad=0;
di=NULL;
pad=NULL;
id_exclu=0;
nom_pad="";
HRESULT result;
HINSTANCE hinst = (HINSTANCE)GetModuleHandle(NULL);
result = DirectInput8Create(hinst,
DIRECTINPUT_VERSION,
IID_IDirectInput8,
(void **)&di,
NULL);
if (FAILED(result))
{
std::cout << "Erreur dans l'invocation de Direct Input.";
}
}
// === Déstructeur : Désinvocation de DI
Gamepad::~Gamepad()
{
di->Release();
}
// === Connexion au PAD
short Gamepad::open(std::string nom, short n)
{
id_exclu=n;
id_count=0;
nom_pad=nom;
HRESULT result;
result = di->EnumDevices(DI8DEVCLASS_GAMECTRL,
::enumCallback,
(LPVOID)this,
DIEDFL_ATTACHEDONLY);
if(FAILED(result))
{
error="Impossible de se connecter aux peripheriques USB.";
return 0;
}
if (!pad)
{
error="Auncun peripherique trouve";
return 0;
}
result=pad->SetDataFormat(&c_dfDIJoystick2);
if(FAILED(result))
{
error="Impossible de definir le format.";
return 0;
}
pad->Acquire();
return id_pad;
}
void Gamepad::close()
{
if(pad)
{
pad->Unacquire();
pad->Release();
}
}
std::string Gamepad::getName()
{
DIDEVICEINSTANCE device;
ZeroMemory(&device, sizeof(device));
device.dwSize = sizeof(device);
pad->GetDeviceInfo(&device);
return device.tszProductName;
}
bool Gamepad::synch()
{
if(SUCCEEDED(pad->GetDeviceState(sizeof(state), (LPVOID)&state)))
return true;
else
return false;
}
int Gamepad::getX()
{
return state.lX;
}
int Gamepad::getY()
{
return state.lY;
}
int Gamepad::getZ()
{
return state.lZ;
}
BYTE* Gamepad::getButtons()
{
return state.rgbButtons;
}
// Fonction de valdation du périphérique trouvé
BOOL CALLBACK enumCallback(const DIDEVICEINSTANCE* instance, VOID* context)
{
if (context != NULL) {
return ((Gamepad *)context)->enumCallback(instance, context);
} else {
return DIENUM_STOP;
}
}
BOOL CALLBACK Gamepad::enumCallback(const DIDEVICEINSTANCE* instance, VOID* context)
{
id_count++;
if(id_exclu != 0 && id_count == id_exclu)
{
return DIENUM_CONTINUE;
}
if (SUCCEEDED(di->CreateDevice(instance->guidInstance, &pad, NULL)))
if((getName()).substr(0,nom_pad.length()) == nom_pad)
{
id_pad=id_count;
return DIENUM_STOP;
}
return DIENUM_CONTINUE;
}