J'utilisais des méthodes pour accéder aux entrées des joueurs directement dans mon jeux et j'ai décidé qu'il serait plus efficace de sortir ses méthodes dans une autre classe, de cette facon, je pourrais simplement l'inclure dans mes projets.
Pour pouvoir accéder à mes méthodes, je les ai tous déclaré static, et je pense que c'est cela qui me cause problème (car elles fonctionnaient quand elles était locale et non-static).
Les erreurs que je recoit sont à chaque appels des méthodes. Par exemple :
"error LNK2019: unresolved external symbol "public: static void __cdecl InputEngine::initializeDInput"
Voici le code des ma classe ainsi que le code du projet test pour la tester.
InputEngine.h
InputEngine.cpp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 #ifndef INPUTENGINE_H #define INPUTENGINE_H #include <dinput.h> #pragma comment (lib, "dinput.lib") #pragma comment (lib, "dinput8.lib") #pragma comment (lib, "dxguid.lib") class InputEngine { public : /*returns true if there is a keyboard*/ static bool isKeyboardAvailable(); /*returns true if there is a mouse*/ static bool isMouseAvailable(); /*returns true if there is a controller*/ static bool isControllerAvailable(); /*returns an array of bytes [256] containing the state of the keyboard keys Access values with (detect_test()[DIK_A] & 0x80)*/ static BYTE * getKeyPressed(); /*returns the mouse struct from Direct Input Uses relative coordinates typedef struct DIMOUSESTATE { LONG lX; LONG lY; LONG lZ; BYTE rgbButtons[4]; } DIMOUSESTATE, *LPDIMOUSESTATE; */ static DIMOUSESTATE getRelativeMouseState(); /*returns the mouse struct from Direct Input Uses absolute coordinates typedef struct DIMOUSESTATE { LONG lX; LONG lY; LONG lZ; BYTE rgbButtons[4]; } DIMOUSESTATE, *LPDIMOUSESTATE; */ static DIMOUSESTATE getAbsoluteMouseState(); /*Returns the absolute X axis of the mouse*/ static long getAbsoluteMouseX(); /*Returns the absolute Y axis of the mouse*/ static long getAbsoluteMouseY(); /*Returns the absolute Z axis of the mouse*/ static long getAbsoluteMouseZ(); /*Returns the relative X axis of the mouse*/ static long getRelativeMouseX(); /*Returns the relative Y axis of the mouse*/ static long getRelativeMouseY(); /*Returns the relative Z axis of the mouse*/ static long getRelativeMouseZ(); /*Returns the absolute coordinates of the mouse 0 = X 1 = Y 2 = Z */ static long * getAbsoluteMouseCoordinates(); /*Returns the relative coordinates of the mouse 0 = X 1 = Y 2 = Z */ static long * getRelativeMouseCoordinates(); /*Returns an array of bytes containing the states of the mouse buttons. Up to 5 buttons 0 = Left button 1 = Right button 2 = Center button 3 = Back button 4 = Foward button */ static BYTE * getMouseButtons(); //Initializes Direct Input static void initializeDInput(HINSTANCE hInstance, HWND hWnd); //closes Direct Input static void closeDInput(void); }; #endif
main.cpp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269 // include the basic windows header files and the DirectX header files #include <windows.h> #include <windowsx.h> #include <dinput.h> #include "InputEngine.h" // include the DirectX Library files #pragma comment (lib, "dinput.lib") #pragma comment (lib, "dinput8.lib") #pragma comment (lib, "dxguid.lib") LPDIRECTINPUT8 directInputInterface; LPDIRECTINPUTDEVICE8 directInputKeyboard; LPDIRECTINPUTDEVICE8 directInputMouse; //containsKeyboardData static BYTE keyPressed[256]; //contains mouse data static DIMOUSESTATE mouseState; //contains only the mouse coordinates static long coordinates[3]; //contains only the mouse buttons static BYTE mouseButtons[5]; /* TODO list : Get controller input Initialize controllers Check for keyboard Check for mouse Check for controllers Absolute Z Release controller */ static bool isKeyboardAvailable() { //TODO Check for keyboard return true; } static bool isMouseAvailable() { //TODO Check for mouse return true; } static bool isControllerAvailable() { //TODO Check for controllers return false; } /*returns an array of bytes [256] containing the state of the keyboard keys Access values with (detect_test()[DIK_A] & 0x80)*/ static BYTE * getKeyPressed() { //static BYTE keyPressed[256]; directInputKeyboard->Acquire(); directInputKeyboard->GetDeviceState(256, (LPVOID)keyPressed); return keyPressed; } /*returns the mouse struct from Direct Input Uses relative coordinates typedef struct DIMOUSESTATE { LONG lX; LONG lY; LONG lZ; BYTE rgbButtons[4]; } DIMOUSESTATE, *LPDIMOUSESTATE; */ static DIMOUSESTATE getRelativeMouseState() { //static DIMOUSESTATE mouseState; directInputMouse->Acquire(); directInputMouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&mouseState); return mouseState; } /*returns the mouse struct from Direct Input Uses absolute coordinates typedef struct DIMOUSESTATE { LONG lX; LONG lY; LONG lZ; BYTE rgbButtons[4]; } DIMOUSESTATE, *LPDIMOUSESTATE; */ static DIMOUSESTATE getAbsoluteMouseState() { POINT absCursor; GetCursorPos(&absCursor); directInputMouse->Acquire(); directInputMouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&mouseState); mouseState.lX = absCursor.x; mouseState.lY = absCursor.y; //TODO absolute Z return mouseState; } /*Returns the absolute X axis of the mouse*/ static long getAbsoluteMouseX() { POINT absCursor; GetCursorPos(&absCursor); return absCursor.x; } /*Returns the absolute Y axis of the mouse*/ static long getAbsoluteMouseY() { POINT absCursor; GetCursorPos(&absCursor); return absCursor.y; } /*Returns the absolute Z axis of the mouse*/ static long getAbsoluteMouseZ() { //TODO absolute Z return 0; } /*Returns the relative X axis of the mouse*/ static long getRelativeMouseX() { //static DIMOUSESTATE mouseState; directInputMouse->Acquire(); directInputMouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&mouseState); return mouseState.lX; } /*Returns the relative Y axis of the mouse*/ static long getRelativeMouseY() { //static DIMOUSESTATE mouseState; directInputMouse->Acquire(); directInputMouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&mouseState); return mouseState.lY; } /*Returns the relative Z axis of the mouse*/ static long getRelativeMouseZ() { //static DIMOUSESTATE mouseState; directInputMouse->Acquire(); directInputMouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&mouseState); return mouseState.lZ; } /*Returns the absolute coordinates of the mouse 0 = X 1 = Y 2 = Z */ static long * getAbsoluteMouseCoordinates() { //long coordinates[3]; POINT absCursor; GetCursorPos(&absCursor); coordinates[0] = absCursor.x; coordinates[1] = absCursor.y; coordinates[2] = 0; //TODO absolute Z return coordinates; } /*Returns the relative coordinates of the mouse 0 = X 1 = Y 2 = Z */ static long * getRelativeMouseCoordinates() { //static DIMOUSESTATE mouseState; directInputMouse->Acquire(); directInputMouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&mouseState); coordinates[0] = mouseState.lX; coordinates[1] = mouseState.lY; coordinates[2] = mouseState.lZ; return coordinates; } /*Returns an array of bytes containing the states of the mouse buttons. Up to 5 buttons 0 = Left button 1 = Right button 2 = Center button 3 = Back button 4 = Foward button */ static BYTE * getMouseButtons() { directInputMouse->Acquire(); directInputMouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&mouseState); mouseButtons[0] = mouseState.rgbButtons[0]; mouseButtons[1] = mouseState.rgbButtons[1]; mouseButtons[2] = mouseState.rgbButtons[2]; mouseButtons[3] = mouseState.rgbButtons[3]; mouseButtons[4] = mouseState.rgbButtons[4]; return mouseButtons; } static void initializeDInput(HINSTANCE hInstance, HWND hWnd) { DirectInput8Create(hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&directInputInterface, NULL); directInputInterface->CreateDevice(GUID_SysKeyboard, &directInputKeyboard, NULL); directInputInterface->CreateDevice(GUID_SysMouse, &directInputMouse, NULL); directInputKeyboard->SetDataFormat(&c_dfDIKeyboard); directInputMouse->SetDataFormat(&c_dfDIMouse); directInputKeyboard->SetCooperativeLevel(hWnd, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND); directInputMouse->SetCooperativeLevel(hWnd, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND); directInputMouse->Acquire(); //return; } static void closeDInput(void) { directInputKeyboard->Unacquire(); directInputMouse->Unacquire(); //TODO : Release controller directInputInterface->Release(); // return; }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
148
149
150
151
152
153
154
155
156
157
158 // include the basic windows header file #include <windows.h> #include <windowsx.h> #include <dinput.h> #include "InputEngine.h" #pragma comment (lib, "dinput.lib") #pragma comment (lib, "dinput8.lib") #pragma comment (lib, "dxguid.lib") void detect_keys(void); // gets the current keys being pressed void detect_mousepos(void); // gets the mouse movement and updates the static variables // the WindowProc function prototype LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); // the entry point for any Windows program int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // the handle for the window, filled by a function HWND hWnd; // this struct holds information for the window class WNDCLASSEX wc; // clear out the window class for use ZeroMemory(&wc, sizeof(WNDCLASSEX)); // fill in the struct with the needed information wc.cbSize = sizeof(WNDCLASSEX); wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = (WNDPROC)WindowProc; wc.hInstance = hInstance; wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)COLOR_WINDOW; wc.lpszClassName = "WindowClass1"; // register the window class RegisterClassEx(&wc); // create the window and use the result as the handle hWnd = CreateWindowEx(NULL, "WindowClass1", // name of the window class "Our First Windowed Program", // title of the window WS_OVERLAPPEDWINDOW, // window style 300, // x-position of the window 300, // y-position of the window 500, // width of the window 400, // height of the window NULL, // we have no parent window, NULL NULL, // we aren't using menus, NULL hInstance, // application handle NULL); // used with multiple windows, NULL // display the window on the screen ShowWindow(hWnd, nCmdShow); // enter the main loop: InputEngine::initializeDInput(hInstance, hWnd); // this struct holds Windows event messages MSG msg; // wait for the next message in the queue, store the result in 'msg' while(TRUE) { // find out the starting time of each loop DWORD starting_point = GetTickCount(); // Check to see if any messages are waiting in the queue if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { // If the message is WM_QUIT, exit the while loop if (msg.message == WM_QUIT) break; // translate keystroke messages into the right format TranslateMessage(&msg); // send the message to the WindowProc function DispatchMessage(&msg); } detect_mousepos(); detect_keys(); // wait until 1/40th of a second has passed while ((GetTickCount() - starting_point) < 25); } // Clean up DirectInput InputEngine::closeDInput(); // return this part of the WM_QUIT message to Windows return msg.wParam; } // this is the main message handler for the program LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { // sort through and find what code to run for the message given switch(message) { // this message is read when the window is closed case WM_DESTROY: { // close the application entirely PostQuitMessage(0); return 0; } break; } // Handle any messages the switch statement didn't return DefWindowProc (hWnd, message, wParam, lParam); } // this is the function that detects keystrokes and displays them in a message box void detect_keys(void) { //static BYTE keystate[256]; // create a static storage for the key-states //dinkeyboard->Acquire(); // get access if we don't have it already //dinkeyboard->GetDeviceState(256, (LPVOID)keystate); // fill keystate with values if(InputEngine::getKeyPressed()[DIK_A] & 0x80) // if the 'A' key was pressed... // then inform the user of this very important message: MessageBox(NULL, "You pressed the 'A' key!", "IMPORTANT MESSAGE!", MB_OK); return; } // this is the function that detets mouse movements and mouse buttons void detect_mousepos(void) { //static DIMOUSESTATE mousestate; // create a static storage for the mouse-states //dinmouse->Acquire(); // get access if we don't have it already // fill the mousestate with values //dinmouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&mousestate); if(InputEngine::getRelativeMouseState().rgbButtons[0] & 0x80) // if the left mouse-button was clicked... // then inform the user of this very important message: MessageBox(NULL, "You clicked the mouse button", "IMPORTANT MESSAGE!", MB_OK); return; }
Partager