-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.cpp
More file actions
696 lines (611 loc) · 16.1 KB
/
input.cpp
File metadata and controls
696 lines (611 loc) · 16.1 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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
//==========================================================================================
//
//入力プログラム[input.cpp]
//Author:石原颯馬
//
//==========================================================================================
//Ver.2.2.2(プロジェクト制作中のもの)
//使用可能な入力:キーボード・マウス(カーソル位置・移動量)・ゲームパッド(1~4台)
//==========================================================================================
#include "input.h"
#include "sound.h"
#include "debugproc.h"
//マクロ定義
#define NUM_KEY_MAX (256) //キーの最大数
#define REPEATE_TIME (150) //リピートの間隔
#define GAMEPAD_BUTTON_NUM (14) //ゲームパッドのボタン数
#define STICK_DEADZONE (655) //遊び
#define PAD_VIBE_FADE (200) //振動の減少量
//グローバル変数
//キーボード部
LPDIRECTINPUT8 g_pInputKeyboard = NULL;
LPDIRECTINPUTDEVICE8 g_pDevKeyboard = NULL;
Keyboard g_keyboard[NUM_KEY_MAX]; //キーボード構造体
//ゲームパッド(XInput使用)部
GamePad g_gamePad[MAX_USE_GAMEPAD]; //ゲームパッド情報
//マウス部
LPDIRECTINPUT8 g_pInputMouse = NULL;
LPDIRECTINPUTDEVICE8 g_pDevMouse = NULL;
Mouse g_mouse; //マウス情報
//==================================================
// キーボード
//==================================================
//========================
//キーボード初期化処理
//========================
HRESULT InitKeyboard(HINSTANCE hInstance, HWND hWnd)
{
//DireceInputオブジェクトの生成
if (FAILED(DirectInput8Create(hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void **)&g_pInputKeyboard, NULL)))
{
return E_FAIL;
}
//デバイス取得
if (FAILED(g_pInputKeyboard->CreateDevice(GUID_SysKeyboard, &g_pDevKeyboard, NULL)))
{
return E_FAIL;
}
//データフォーマット設定
if (FAILED(g_pDevKeyboard->SetDataFormat(&c_dfDIKeyboard)))
{
return E_FAIL;
}
//協調モード設定
if (FAILED(g_pDevKeyboard->SetCooperativeLevel(hWnd,
(DISCL_FOREGROUND | DISCL_NONEXCLUSIVE))))
{
return E_FAIL;
}
//キーボードへのアクセス権を獲得
g_pDevKeyboard->Acquire();
//時間初期化
for (int nCntInit = 0; nCntInit < NUM_KEY_MAX; nCntInit++)
{
g_keyboard[nCntInit].currentTime = 0;
g_keyboard[nCntInit].execLastTime = timeGetTime() - REPEATE_TIME;
}
return S_OK;
}
//========================
//キーボード終了処理
//========================
void UninitKeyboard(void)
{
//入力デバイスの破棄
if (g_pDevKeyboard != NULL)
{
g_pDevKeyboard->Unacquire();
g_pDevKeyboard->Release();
g_pDevKeyboard = NULL;
}
//DirectInputオブジェクトの破棄
if (g_pInputKeyboard != NULL)
{
g_pInputKeyboard->Release();
g_pInputKeyboard = NULL;
}
}
//========================
//キーボード更新処理
//========================
void UpdateKeyboard(void)
{
BYTE aKeyState[NUM_KEY_MAX];
//入力デバイスからデバイスを取得
if (SUCCEEDED(g_pDevKeyboard->GetDeviceState(sizeof(aKeyState), &aKeyState[0])))
{
for (int nCntKey = 0; nCntKey < NUM_KEY_MAX; nCntKey++)
{
g_keyboard[nCntKey].trigger = (g_keyboard[nCntKey].state ^ aKeyState[nCntKey]) & aKeyState[nCntKey];
g_keyboard[nCntKey].release = (g_keyboard[nCntKey].state ^ aKeyState[nCntKey]) & ~aKeyState[nCntKey];
//リピート
g_keyboard[nCntKey].currentTime = timeGetTime();
if (aKeyState[nCntKey] && (g_keyboard[nCntKey].currentTime - g_keyboard[nCntKey].execLastTime) > REPEATE_TIME)
{
g_keyboard[nCntKey].execLastTime = g_keyboard[nCntKey].currentTime;
g_keyboard[nCntKey].repeate = aKeyState[nCntKey];
}
else
{
g_keyboard[nCntKey].repeate = 0;
}
//プレス
g_keyboard[nCntKey].state = aKeyState[nCntKey]; //キーボードのプレス情報を保存
}
}
else
{
g_pDevKeyboard->Acquire();
}
}
//========================
//キーボードのプレス情報を返す処理
//========================
bool GetKeyboardPress(int nKey)
{
return (g_keyboard[nKey].state & 0x80) ? true : false; //?が条件式作ることになってるみたいよ
}
//========================
//キーボードのトリガー情報を返す処理
//========================
bool GetKeyboardTrigger(int nKey)
{
return (g_keyboard[nKey].trigger & 0x80) ? true : false;
}
//========================
//キーボードのリリース情報を返す処理
//========================
bool GetKeyboardRelease(int nKey)
{
return (g_keyboard[nKey].release & 0x80) ? true : false;
}
//========================
//キーボードのリピート情報を返す処理
//========================
bool GetKeyboardRepeate(int nKey)
{
return (g_keyboard[nKey].repeate & 0x80) ? true : false;
}
//==================================================
// マウス
//==================================================
//========================
//マウス初期化処理
//========================
HRESULT InitMouse(HINSTANCE hInstance, HWND hWnd)
{
//DireceInputオブジェクトの生成
if (FAILED(DirectInput8Create(hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void **)&g_pInputMouse, NULL)))
{
return E_FAIL;
}
//デバイス取得
if (FAILED(g_pInputMouse->CreateDevice(GUID_SysMouse, &g_pDevMouse, NULL)))
{
return E_FAIL;
}
//データフォーマット設定
if (FAILED(g_pDevMouse->SetDataFormat(&c_dfDIMouse)))
{
return E_FAIL;
}
//協調モード設定
if (FAILED(g_pDevMouse->SetCooperativeLevel(hWnd,
(DISCL_FOREGROUND | DISCL_NONEXCLUSIVE))))
{
return E_FAIL;
}
//マウスへのアクセス権を獲得
g_pDevMouse->Acquire();
//時間初期化
for (int nCntInit = 0; nCntInit < MOUSE_BUTTON_MAX; nCntInit++)
{
g_mouse.mb[nCntInit].currentTime = 0;
g_mouse.mb[nCntInit].execLastTime = timeGetTime() - REPEATE_TIME;
}
return S_OK;
}
//========================
//マウス終了処理
//========================
void UninitMouse(void)
{
//入力デバイスの破棄
if (g_pDevMouse != NULL)
{
g_pDevMouse->Unacquire();
g_pDevMouse->Release();
g_pDevMouse = NULL;
}
//DirectInputオブジェクトの破棄
if (g_pInputMouse != NULL)
{
g_pInputMouse->Release();
g_pInputMouse = NULL;
}
}
//========================
//マウス更新処理
//========================
void UpdateMouse(void)
{
DIMOUSESTATE MouseState;
POINT point;
//マウス取得
if (SUCCEEDED(g_pDevMouse->GetDeviceState(sizeof(DIMOUSESTATE), &MouseState)))
{
for (int nCntKey = 0; nCntKey < MOUSE_BUTTON_MAX; nCntKey++)
{
//トリガーとリリース
g_mouse.mb[nCntKey].trigger = (g_mouse.state.rgbButtons[nCntKey] ^ MouseState.rgbButtons[nCntKey]) & MouseState.rgbButtons[nCntKey];
g_mouse.mb[nCntKey].release = (g_mouse.state.rgbButtons[nCntKey] ^ MouseState.rgbButtons[nCntKey]) & ~MouseState.rgbButtons[nCntKey];
//リピート
g_mouse.mb[nCntKey].currentTime = timeGetTime();
if (MouseState.rgbButtons[nCntKey] && (g_mouse.mb[nCntKey].currentTime - g_mouse.mb[nCntKey].execLastTime) > REPEATE_TIME)
{
g_mouse.mb[nCntKey].execLastTime = g_mouse.mb[nCntKey].currentTime;
g_mouse.mb[nCntKey].repeate = MouseState.rgbButtons[nCntKey];
}
else
{
g_mouse.mb[nCntKey].repeate = 0;
}
}
//プレス
g_mouse.state = MouseState; //キーボードのプレス情報を保存
//移動量
g_mouse.move = D3DXVECTOR3((float)g_mouse.state.lX, (float)g_mouse.state.lY, 0.0f);
}
else
{
g_pDevMouse->Acquire();
}
//マウス座標取得
GetCursorPos(&point);
ScreenToClient(FindWindowA(CLASS_NAME, nullptr), &point);
g_mouse.pos.x = (float)point.x;
g_mouse.pos.y = (float)point.y;
}
//========================
//マウスのプレス情報を返す処理
//=======================
bool GetMouseClickPress(int nButton)
{
return g_mouse.state.rgbButtons[nButton] & 0x80 ? true : false;
}
//========================
//マウスのトリガー情報を返す処理
//=======================
bool GetMouseClickTrigger(int nButton)
{
return g_mouse.mb[nButton].trigger & 0x80 ? true : false;
}
//========================
//マウスのリピート情報を返す処理
//=======================
bool GetMouseClickRepeate(int nButton)
{
return g_mouse.mb[nButton].repeate & 0x80 ? true : false;
}
//========================
//マウスのカーソル位置を返す処理
//=======================
D3DXVECTOR3 GetMousePos(void)
{
return g_mouse.pos;
}
//========================
//マウスのカーソル移動量を返す処理
//=======================
D3DXVECTOR3 GetMouseMove(void)
{
return g_mouse.move;
}
//==================================================
// ゲームパッド
//==================================================
//========================
//ゲームパッド初期化処理
//========================
void InitGamePad(void)
{
//ゲームパッド類
for (int nCntGPad = 0; nCntGPad < MAX_USE_GAMEPAD; nCntGPad++)
{
g_gamePad[nCntGPad].currentTime = 0;
g_gamePad[nCntGPad].execLastTime = timeGetTime();
g_gamePad[nCntGPad].bUse = false;
g_gamePad[nCntGPad].Vibe_State = VIBE_STATE_00_STOP;
g_gamePad[nCntGPad].wVibePower = 0;
g_gamePad[nCntGPad].nVibeTime = 0;
}
}
//========================
//ゲームパッド終了処理
//========================
void UninitGamePad(void)
{
//全コントローラーの振動停止
StopVibration();
//XInput終了
XInputEnable(false);
}
//========================
//ゲームパッド更新処理
//========================
void UpdateGamePad(void)
{
XINPUT_STATE xInputState;
//ゲームパッドから情報取得
for (int nCntGPad = 0; nCntGPad < MAX_USE_GAMEPAD; nCntGPad++)
{
bool bOldUseGPad = g_gamePad[nCntGPad].bUse;
if (XInputGetState(nCntGPad, &xInputState) == ERROR_SUCCESS)
{
//使ってるよ
g_gamePad[nCntGPad].bUse = true;
//ボタントリガー情報取得
g_gamePad[nCntGPad].trigger = (g_gamePad[nCntGPad].state.Gamepad.wButtons ^ xInputState.Gamepad.wButtons) & xInputState.Gamepad.wButtons;
//ボタンリリース情報取得
g_gamePad[nCntGPad].release = (g_gamePad[nCntGPad].state.Gamepad.wButtons ^ xInputState.Gamepad.wButtons) & ~xInputState.Gamepad.wButtons;
//リピート情報生成
g_gamePad[nCntGPad].currentTime = timeGetTime();
for (int nCntKey = 0; nCntKey < GAMEPAD_BUTTON_NUM; nCntKey++)
{
if (g_gamePad[nCntGPad].state.Gamepad.wButtons & 1 << nCntKey && (g_gamePad[nCntGPad].currentTime - g_gamePad[nCntGPad].execLastTime) > REPEATE_TIME)
{
g_gamePad[nCntGPad].execLastTime = g_gamePad[nCntGPad].currentTime;
g_gamePad[nCntGPad].repeate += 1 << nCntKey;
}
}
//プレス情報その他もろもろ設定
g_gamePad[nCntGPad].state = xInputState;
//コントローラーの振動状態更新
UpdateVibeGamePad(nCntGPad);
}
else
{
//使ってないよ
g_gamePad[nCntGPad].bUse = false;
}
//コントローラー接続が変化したら音鳴らす
if (g_gamePad[nCntGPad].bUse != bOldUseGPad)
{
PlaySoundSE(SOUND_LABEL_SE_CONNECT, nCntGPad);
}
}
}
//========================
//ゲームパッド振動状態の更新
//========================
void UpdateVibeGamePad(int nPadNum)
{
//振動停止中
if (g_gamePad[nPadNum].nVibeTime <= 0)
{
g_gamePad[nPadNum].wVibePower = 0; // 振動量 0 に設定
g_gamePad[nPadNum].nVibeTime = 0; //継続時間 0 に設定
g_gamePad[nPadNum].Vibe_State = VIBE_STATE_00_STOP; // 状態 「停止」に設定
StopVibration(nPadNum); //対象のコントローラーの振動停止
}
//振動中
else
{
//継続時間減少
g_gamePad[nPadNum].nVibeTime--;
//バイブの状態が、徐々に弱まっていくものである
if (g_gamePad[nPadNum].Vibe_State == VIBE_STATE_02_FADE)
{
//振動量が最低値を下回ったら
if (g_gamePad[nPadNum].wVibePower <= VIBE_POWER_MIN)
{
//振動を止める
g_gamePad[nPadNum].wVibePower = 0;
g_gamePad[nPadNum].nVibeTime = 0;
g_gamePad[nPadNum].Vibe_State = VIBE_STATE_00_STOP;
StopVibration(nPadNum); //対象のコントローラーの振動停止
}
else
{
//徐々に減少させる
g_gamePad[nPadNum].wVibePower -= PAD_VIBE_FADE;
}
}
XINPUT_VIBRATION Vibration;
Vibration.wLeftMotorSpeed = g_gamePad[nPadNum].wVibePower; //左のモーターの回転速度を設定
Vibration.wRightMotorSpeed = g_gamePad[nPadNum].wVibePower; //右のモーターの回転速度を設定
XInputSetState(nPadNum, &Vibration); //対象のコントローラーに振動を設定
PrintDebugProc("振動量:%d 残り時間:%d\n", g_gamePad[nPadNum].wVibePower, g_gamePad[nPadNum].nVibeTime / MAX_FPS);
}
}
//========================
//全ゲームパッドの振動停止
//========================
void StopVibration(void)
{
XINPUT_VIBRATION Vibration;
//左右のモーターの回転速度を0に設定
Vibration.wLeftMotorSpeed = Vibration.wRightMotorSpeed = 0;
//全コントローラーの振動をOFFに
for (int nCntPad = 0; nCntPad < MAX_USE_GAMEPAD; nCntPad++)
{
//振動をOFF
XInputSetState(nCntPad, &Vibration);
}
}
//========================
//対象のコントローラーの振動停止
//========================
void StopVibration(int nPadNum)
{
XINPUT_VIBRATION Vibration = {0,0};
//対象のコントローラーの振動をOFF
XInputSetState(nPadNum, &Vibration);
}
//========================
//ゲームパッドのプレス情報を返す処理
//=======================
bool GetGamepadPress(int nPadNum, int nButton)
{
if (g_gamePad[nPadNum].bUse == true)
{
return g_gamePad[nPadNum].state.Gamepad.wButtons & nButton ? true : false;
}
else
{
return false;
}
}
//========================
//ゲームパッドのトリガー情報を返す処理
//=======================
bool GetGamepadTrigger(int nPadNum, int nButton)
{
if (g_gamePad[nPadNum].bUse == true)
{
return g_gamePad[nPadNum].trigger & nButton ? true : false;
}
else
{
return false;
}
}
//========================
//ゲームパッドのリリース情報を返す処理
//=======================
bool GetGamepadRelease(int nPadNum, int nButton)
{
if (g_gamePad[nPadNum].bUse == true)
{
return g_gamePad[nPadNum].release & nButton ? true : false;
}
else
{
return false;
}
}
//========================
//ゲームパッドのリピート情報を返す処理
//=======================
bool GetGamepadRepeate(int nPadNum, int nButton)
{
if (g_gamePad[nPadNum].bUse == true)
{
return g_gamePad[nPadNum].repeate & nButton ? true : false;
}
else
{
return false;
}
}
//========================
//左スティックの横軸を返す処理
//=======================
SHORT GetLStickX(int nPadNum)
{
if (g_gamePad[nPadNum].bUse == true)
{
//-値最大なら+値最大に合わせる
if (g_gamePad[nPadNum].state.Gamepad.sThumbLX < -STICK_MAX)
{
g_gamePad[nPadNum].state.Gamepad.sThumbLX = -STICK_MAX;
}
if (abs(g_gamePad[nPadNum].state.Gamepad.sThumbLX) > STICK_DEADZONE)
{
return g_gamePad[nPadNum].state.Gamepad.sThumbLX;
}
else
{
return 0;
}
}
else
{//使っていなかったら0返す
return 0;
}
}
//========================
//左スティックの縦軸を返す処理
//=======================
SHORT GetLStickY(int nPadNum)
{
if (g_gamePad[nPadNum].bUse == true)
{
//-値最大なら+値最大に合わせる
if (g_gamePad[nPadNum].state.Gamepad.sThumbLY < -STICK_MAX)
{
g_gamePad[nPadNum].state.Gamepad.sThumbLY = -STICK_MAX;
}
if (abs(g_gamePad[nPadNum].state.Gamepad.sThumbLY) > STICK_DEADZONE)
{
return g_gamePad[nPadNum].state.Gamepad.sThumbLY;
}
else
{
return 0;
}
}
else
{//使っていなかったら0返す
return 0;
}
}
//========================
//右スティックの横軸を返す処理
//=======================
SHORT GetRStickX(int nPadNum)
{
if (g_gamePad[nPadNum].bUse == true)
{
//-値最大なら+値最大に合わせる
if (g_gamePad[nPadNum].state.Gamepad.sThumbRX < -STICK_MAX)
{
g_gamePad[nPadNum].state.Gamepad.sThumbRX = -STICK_MAX;
}
if (abs(g_gamePad[nPadNum].state.Gamepad.sThumbRX) > STICK_DEADZONE)
{
return g_gamePad[nPadNum].state.Gamepad.sThumbRX;
}
else
{
return 0;
}
}
else
{//使っていなかったら0返す
return 0;
}
}
//========================
//右スティックの縦軸を返す処理
//=======================
SHORT GetRStickY(int nPadNum)
{
if (g_gamePad[nPadNum].bUse == true)
{
//-値最大なら+値最大に合わせる
if (g_gamePad[nPadNum].state.Gamepad.sThumbRY < -STICK_MAX)
{
g_gamePad[nPadNum].state.Gamepad.sThumbRY = -STICK_MAX;
}
if (abs(g_gamePad[nPadNum].state.Gamepad.sThumbRY) > STICK_DEADZONE)
{
return g_gamePad[nPadNum].state.Gamepad.sThumbRY;
}
else
{
return 0;
}
}
else
{//使っていなかったら0返す
return 0;
}
}
//========================
//ゲームパッド使用しているか返す処理
//=======================
bool GetUseGamepad(int nPadNum)
{
if (g_gamePad[nPadNum].bUse == true)
{
return g_gamePad[nPadNum].bUse;
}
else
{
return false;
}
}
//========================
//ゲームパッド振動設定
//========================
void SetPadVibration(int nPadNum, WORD wPower, int nVibeTime, VIBE_STATE State)
{
g_gamePad[nPadNum].wVibePower = wPower; //強さ設定
g_gamePad[nPadNum].nVibeTime = nVibeTime; //時間設定
g_gamePad[nPadNum].Vibe_State = State; //状態設定
XINPUT_VIBRATION Vibration;
Vibration.wLeftMotorSpeed = wPower; //左のモーターの回転速度を設定
Vibration.wRightMotorSpeed = wPower; //右のモーターの回転速度を設定
XInputSetState(nPadNum, &Vibration); //対象のコントローラーに振動を設定
}