センサーの値をポーリングにより取得するサンプルコード
#include <windows.h>
#include <atlbase.h>
#import "progid:TDxInput.Device" no_namespace
#define WND_CLS_NAME "SpaceNavigator"
#define TIMER_ELAPSE 100
#define WINDOW_WIDTH 500
#define WINDOW_HEIGHT 100
CComPtr<ISensor> gSensor;
UINT_PTR gTimerId = NULL;
ポーリングを行うためのコールバック関数
VOID CALLBACK _3DxTimerProc( HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime )
{
if( gSensor )
{
CComPtr<IAngleAxis> pRotation = gSensor->Rotation;
char rotationText[ 0xFF ];
sprintf(
rotationText,
"Rotation x:%+5.3f y:%+5.3f z:%+5.3f angle:%5.2f ",
pRotation->X,
pRotation->Y,
pRotation->Z,
pRotation->Angle
);
CComPtr<IVector3D> pTranslation = gSensor->Translation;
char translationText[ 0xFF ];
sprintf(
translationText,
"Translation x:%+5.2f y:%+5.2f z:%+5.2f length:%5.2f ",
pTranslation->X,
pTranslation->Y,
pTranslation->Z,
pTranslation->Length
);
HDC hdc = ::GetDC( hwnd );
::TextOut( hdc, 0, 0, rotationText, ::lstrlen( rotationText ) );
::TextOut( hdc, 0, 30, translationText, ::lstrlen( rotationText ) );
::ReleaseDC( hwnd, hdc );
pRotation.Release();
pTranslation.Release();
}
}
初期化処理
bool _3DxCreate()
{
HRESULT hResult;
hResult = ::CoInitializeEx( NULL, COINIT_APARTMENTTHREADED );
if( SUCCEEDED( hResult ) )
{
CComPtr<IUnknown> _3DxDevice;
hResult = _3DxDevice.CoCreateInstance( __uuidof( Device ) );
if( SUCCEEDED( hResult ) )
{
CComPtr<ISimpleDevice> _3DxSimpleDevice;
hResult = _3DxDevice.QueryInterface( &_3DxSimpleDevice );
if( SUCCEEDED( hResult ) )
{
gSensor = _3DxSimpleDevice->Sensor;
_3DxSimpleDevice->LoadPreferences( "Sample" );
_3DxSimpleDevice->Connect();
return true;
}
}
}
return false;
}
終了処理
bool _3DxDestroy()
{
CComPtr<ISimpleDevice> _3DxDevice;
if( gSensor )
{
gSensor->get_Device( ( IDispatch** )&_3DxDevice );
gSensor.Release();
}
if( _3DxDevice )
{
_3DxDevice->Disconnect();
_3DxDevice.Release();
}
return true;
}
ウィンドウ プロシージャ
LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
switch( uMsg )
{
case WM_CREATE:
if( _3DxCreate() )
{
gTimerId = ::SetTimer( hWnd, 0, TIMER_ELAPSE, _3DxTimerProc );
}
break;
case WM_DESTROY:
_3DxDestroy();
if( gTimerId )
{
::KillTimer( hWnd, gTimerId);
gTimerId = NULL;
}
::PostQuitMessage( 0 );
break;
default:
return ::DefWindowProc( hWnd, uMsg, wParam, lParam );
}
return 0;
}
初期エントリポイント
int WINAPI WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow )
{
WNDCLASS wndcls;
::ZeroMemory( &wndcls, sizeof( wndcls ) );
wndcls.lpfnWndProc = WndProc;
wndcls.hInstance = hInst;
wndcls.hbrBackground = ( HBRUSH )COLOR_WINDOW;
wndcls.lpszClassName = WND_CLS_NAME;
if( ::RegisterClass( &wndcls ) == 0 )
{
return 0;
}
HWND hWnd = ::CreateWindow(
WND_CLS_NAME,
"Space Navigator - Polling",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
WINDOW_WIDTH,
WINDOW_HEIGHT,
0,
0,
hInst,
NULL
);
if( hWnd == NULL )
{
return 0;
}
::ShowWindow( hWnd, nCmdShow );
::UpdateWindow( hWnd );
MSG msg;
while( ::GetMessage( &msg, NULL, 0, 0 ) )
{
::DispatchMessage( &msg );
}
return msg.wParam;
}