TNB Library
TnbDxInput.h
[詳解]
1#pragma once
13#include "TnbDef.h"
14
15
16
17#ifndef _TnbDOXYGEN //Document作成用シンボル
18 #define DIRECTINPUT_VERSION 0x0800
19#endif
20#include <atlbase.h>
21#include <dinput.h>
22#pragma comment(lib,"dinput8.lib")
23#pragma comment(lib,"dxguid.lib")
24
25
26
27//TNB Library
28namespace TNB{
29namespace DX
30{
31
32
33
49{
50public:
51
53 CDirectInputDevice(void) : m_deviceSize(0), m_hResult(E_FAIL)
54 {
55 }
56
59 {
60 Destroy();
61 }
62
67 void Destroy(void)
68 {
69 m_pDiObject = NULL;
70 loop ( i, m_deviceSize )
71 {
72 m_apDiDevice[i] = NULL;
73 }
74 m_deviceSize = 0;
75 m_hResult = E_FAIL;
76 }
77
90 bool Create(DWORD dwDevType, HINSTANCE hInst = NULL)
91 {
92 Destroy();
93 if ( hInst == NULL )
94 {
96 }
97 //DirectInputオブジェクトの生成
98 m_hResult = ::DirectInput8Create(hInst, DIRECTINPUT_VERSION,
99 IID_IDirectInput8, reinterpret_cast<LPVOID*>(&m_pDiObject), NULL);
100 if ( FAILED(m_hResult) )
101 {
102 return false;
103 }
104 //DirectInputデバイスオブジェクト達の生成
105 if ( m_pDiObject->EnumDevices(dwDevType, ms_Callback, this, DIEDFL_ATTACHEDONLY) != DI_OK )
106 {
107 Destroy();
108 return false;
109 }
110 return true;
111 }
112
122 bool Create(const GUID& guid, HINSTANCE hInst = NULL)
123 {
124 Destroy();
125 if ( hInst == NULL )
126 {
128 }
129 //DirectInputオブジェクトの生成
130 m_hResult = ::DirectInput8Create(hInst, DIRECTINPUT_VERSION,
131 IID_IDirectInput8, reinterpret_cast<LPVOID*>(&m_pDiObject), NULL);
132 if ( FAILED(m_hResult) )
133 {
134 return false;
135 }
136 // デバイス・オブジェクトを作成
137 m_hResult = m_pDiObject->CreateDevice(guid, &m_apDiDevice[0], NULL);
138 if ( FAILED(m_hResult) )
139 {
140 m_pDiObject = NULL;
141 return false;
142 }
143 m_deviceSize = 1;
144 return true;
145 }
146
151 const IDirectInput8* ReferObject(void) const
152 {
153 return m_pDiObject;
154 }
155
160 size_t GetDeviceNum(void) const
161 {
162 return m_deviceSize;
163 }
164
170 IDirectInputDevice8* operator[](INDEX index) const
171 {
172 if ( index < MAX )
173 {
174 return m_apDiDevice[index];
175 }
176 return NULL;
177 }
178
179private:
180 enum { MAX = 16 }; // 最大Device数
181 CComPtr<IDirectInput8> m_pDiObject;
182 size_t m_deviceSize;
183 CComPtr<IDirectInputDevice8> m_apDiDevice[MAX];
184 HRESULT m_hResult;
186 static BOOL CALLBACK ms_Callback(const DIDEVICEINSTANCE* pInst, VOID* pContext)
187 {
188 CDirectInputDevice* P = static_cast<CDirectInputDevice*>(pContext);
189 size_t l = P->m_deviceSize;
190 if ( l < MAX - 1 )
191 {
192 HRESULT hr = P->m_pDiObject->CreateDevice(pInst->guidInstance, &(P->m_apDiDevice[l]), NULL);
193 if ( SUCCEEDED(hr) )
194 {
195 (P->m_deviceSize)++;
196 return DIENUM_CONTINUE;
197 }
198 }
199 return DIENUM_STOP;
200 }
201};
202
203
204
217template<typename TYP>
219{
220public:
221
227 CDirectInputT(HINSTANCE hInst = NULL) : m_hInst(hInst), m_boIsInit(false)
228 {
229 Zero(m_state);
230 }
231
245 bool Initialize(HWND hWnd = NULL, DWORD dwMode = DISCL_NONEXCLUSIVE | DISCL_FOREGROUND)
246 {
247 if ( ! OnCreate(m_dx, m_hInst) )
248 {
249 return false;
250 }
251 loop ( i, m_dx.GetDeviceNum() )
252 {
253 // データ形式を設定
254 if ( ! OnSetDataFormat(m_dx[i]) )
255 {
256 return false;
257 }
258 //モードを設定
259 if ( hWnd != NULL && FAILED(m_dx[i]->SetCooperativeLevel(hWnd, dwMode)) )
260 {
261 return false;
262 }
263 //デバイスの設定
264 if ( ! OnSetProperty(m_dx[i]) )
265 {
266 return false;
267 }
268 }
269 m_boIsInit = true;
270 return true;
271 }
272
279 bool Acquire(void)
280 {
281 if ( ! m_boIsInit )
282 {
283 return false;
284 }
285 loop ( i, m_dx.GetDeviceNum() )
286 {
287 if ( FAILED(m_dx[i]->Acquire()) )
288 {
289 return false;
290 }
291 }
292 return true;
293 }
294
300 bool Unacquire(void)
301 {
302 if ( ! m_boIsInit )
303 {
304 return false;
305 }
306 loop ( i, m_dx.GetDeviceNum() )
307 {
308 if ( FAILED(m_dx[i]->Unacquire()) )
309 {
310 return false;
311 }
312 }
313 return true;
314 }
315
319 void Poll(void)
320 {
321 if ( m_boIsInit )
322 {
323 loop ( i, m_dx.GetDeviceNum() )
324 {
325 m_dx[i]->Poll();
326 }
327 }
328 }
329
334 size_t GetDeviceNum(void) const
335 {
336 return m_dx.GetDeviceNum();
337 }
338
346 const TYP* Get(INDEX index = 0) const
347 {
348 if ( m_boIsInit && IsInRange(index, GetDeviceNum()) )
349 {
350 IDirectInputDevice8* px = m_dx[index];
351 if ( px != NULL && SUCCEEDED(px->GetDeviceState(sizeof(TYP), &m_state)) )
352 {
353 return &m_state;
354 }
355 }
356 return NULL;
357 }
358
359protected:
360
369 virtual bool OnCreate(CDirectInputDevice& _dx, HINSTANCE hInst) = 0;
370
378 virtual bool OnSetDataFormat(IDirectInputDevice8* _px) = 0;
379
387 virtual bool OnSetProperty(IDirectInputDevice8* _px) = 0;
388
389private:
390 CDirectInputDevice m_dx;
391 bool m_boIsInit;
392 HINSTANCE m_hInst;
393 mutable TYP m_state;
394};
395
396
397
408class CDirectInputMouse : public CDirectInputT<DIMOUSESTATE2>
409{
411public:
412
426 const DIMOUSESTATE2* Get(INDEX index = 0) const
427 {
428 return _super::Get(index);
429 }
430
431protected:
432
441 virtual bool OnCreate(CDirectInputDevice& _dx, HINSTANCE hInst)
442 {
443 return _dx.Create(GUID_SysMouse, hInst);
444 }
445
453 virtual bool OnSetDataFormat(IDirectInputDevice8* _px)
454 {
455 return !! SUCCEEDED(_px->SetDataFormat(&c_dfDIMouse2));
456 }
457
465 virtual bool OnSetProperty(IDirectInputDevice8* _px)
466 {
467 // 軸モードを設定
468 DIPROPDWORD diprop = {sizeof(DIPROPDWORD)};
469 diprop.diph.dwHeaderSize = sizeof(diprop.diph);
470 diprop.diph.dwHow = DIPH_DEVICE;
471 diprop.dwData = DIPROPAXISMODE_REL; //相対値モード
472 return !! SUCCEEDED(_px->SetProperty(DIPROP_AXISMODE, &diprop.diph));
473 }
474};
475
476
477
488class CDirectInputJoystick : public CDirectInputT<DIJOYSTATE2>
489{
491public:
492
504 const DIJOYSTATE2* Get(INDEX index = 0) const
505 {
506 return _super::Get(index);
507 }
508
514 virtual LONG GetStickRange(void) const
515 {
516 return 1000;
517 }
518
519protected:
520
529 virtual bool OnCreate(CDirectInputDevice& _dx, HINSTANCE hInst)
530 {
531 return _dx.Create(DI8DEVCLASS_GAMECTRL, hInst);
532 }
533
541 virtual bool OnSetDataFormat(IDirectInputDevice8* _px)
542 {
543 return !! SUCCEEDED(_px->SetDataFormat(&c_dfDIJoystick2));
544 }
545
553 virtual bool OnSetProperty(IDirectInputDevice8* _px)
554 {
555 TParam p = { _px, this };
556 return (_px->EnumObjects(ms_EnumObjectsCallback, &p, DIDFT_ALL)) == DI_OK;
557 }
558
559private:
560
562 struct TParam
563 {
564 IDirectInputDevice8* pDev;
565 CDirectInputJoystick* pJoystick;
566 };
568 static BOOL CALLBACK ms_EnumObjectsCallback(const DIDEVICEOBJECTINSTANCE* pdidoi, LPVOID pContext)
569 {
570 TParam* P = static_cast<TParam*>(pContext);
571 if ( (pdidoi->dwType & DIDFT_AXIS) != 0 )
572 {
573 DIPROPRANGE diprg;
574 diprg.diph.dwSize = sizeof(DIPROPRANGE);
575 diprg.diph.dwHeaderSize = sizeof(DIPROPHEADER);
576 diprg.diph.dwHow = DIPH_BYID;
577 diprg.diph.dwObj = pdidoi->dwType; // Specify the enumerated axis
578 diprg.lMin = P->pJoystick->GetStickRange() * -1;
579 diprg.lMax = P->pJoystick->GetStickRange();
580 // Set the range for the axis
581 if ( FAILED(P->pDev->SetProperty(DIPROP_RANGE, &diprg.diph)) )
582 {
583 return false;
584 }
585 }
586 return true;
587 }
588};
589
590
591
592};//DX
593};//TNB
TNBライブラリの定義ヘッダ
#define loop(VAR, CNT)
loop構文.
Definition: TnbDef.h:343
[ETC] コピー不可能スーパークラス.
Definition: TnbDef.h:599
DirectInputデバイス管理クラス
Definition: TnbDxInput.h:49
~CDirectInputDevice(void)
デストラクタ
Definition: TnbDxInput.h:58
CDirectInputDevice(void)
コンストラクタ
Definition: TnbDxInput.h:53
size_t GetDeviceNum(void) const
[参照] デバイス数取得.
Definition: TnbDxInput.h:160
bool Create(DWORD dwDevType, HINSTANCE hInst=NULL)
[作成] デバイス作成.
Definition: TnbDxInput.h:90
void Destroy(void)
[破棄] デバイス破棄.
Definition: TnbDxInput.h:67
const IDirectInput8 * ReferObject(void) const
[参照] オブジェクト参照.
Definition: TnbDxInput.h:151
bool Create(const GUID &guid, HINSTANCE hInst=NULL)
[作成] デバイス作成.
Definition: TnbDxInput.h:122
IDirectInputDevice8 * operator[](INDEX index) const
[参照] デバイス参照.
Definition: TnbDxInput.h:170
DirectInputジョイスティック管理クラス
Definition: TnbDxInput.h:489
virtual LONG GetStickRange(void) const
[取得] Stickの傾き最大値.
Definition: TnbDxInput.h:514
const DIJOYSTATE2 * Get(INDEX index=0) const
[取得] 状態取得.
Definition: TnbDxInput.h:504
virtual bool OnSetDataFormat(IDirectInputDevice8 *_px)
[通知] フォーマット設定時.
Definition: TnbDxInput.h:541
virtual bool OnCreate(CDirectInputDevice &_dx, HINSTANCE hInst)
[通知] 作成時.
Definition: TnbDxInput.h:529
virtual bool OnSetProperty(IDirectInputDevice8 *_px)
[通知] プロパティ設定時.
Definition: TnbDxInput.h:553
DirectInputマウス管理クラス
Definition: TnbDxInput.h:409
const DIMOUSESTATE2 * Get(INDEX index=0) const
[取得] 状態取得.
Definition: TnbDxInput.h:426
virtual bool OnSetDataFormat(IDirectInputDevice8 *_px)
[通知] フォーマット設定時.
Definition: TnbDxInput.h:453
virtual bool OnCreate(CDirectInputDevice &_dx, HINSTANCE hInst)
[通知] 作成時.
Definition: TnbDxInput.h:441
virtual bool OnSetProperty(IDirectInputDevice8 *_px)
[通知] プロパティ設定時.
Definition: TnbDxInput.h:465
DirectInput管理ベーステンプレートクラス
Definition: TnbDxInput.h:219
virtual bool OnSetDataFormat(IDirectInputDevice8 *_px)=0
[通知] フォーマット設定時.
bool Initialize(HWND hWnd=NULL, DWORD dwMode=DISCL_NONEXCLUSIVE|DISCL_FOREGROUND)
[設定] 初期化.
Definition: TnbDxInput.h:245
virtual bool OnSetProperty(IDirectInputDevice8 *_px)=0
[通知] プロパティ設定時.
void Poll(void)
[設定] デバイス状態更新.
Definition: TnbDxInput.h:319
bool Unacquire(void)
[設定] 停止.
Definition: TnbDxInput.h:300
size_t GetDeviceNum(void) const
[参照] デバイス数取得.
Definition: TnbDxInput.h:334
bool Acquire(void)
[設定] 実行.
Definition: TnbDxInput.h:279
const TYP * Get(INDEX index=0) const
[取得] 状態取得.
Definition: TnbDxInput.h:346
CDirectInputT(HINSTANCE hInst=NULL)
コンストラクタ
Definition: TnbDxInput.h:227
virtual bool OnCreate(CDirectInputDevice &_dx, HINSTANCE hInst)=0
[通知] 作成時.
bool IsInRange(INDEX value, size_t size)
[確認] 範囲チェック.
Definition: TnbDef.h:421
void Zero(V &value)
[設定] ゼロクリア.
Definition: TnbDef.h:399
HINSTANCE GetInstanceHandleByTnb(EInstanceType type=EI_Process)
[取得] インスタンスハンドル取得.
Definition: TnbDef.h:1341
@ EI_Process
プロセス用
Definition: TnbDef.h:1259
TNB Library
Definition: TnbDoxyTitle.txt:2