/*
-------------------------------------
代码清单:D3DGame.cpp
来自:
http://www.cnblogs.com/kenkao
-------------------------------------
*/
#include
"
StdAfx.h
"
#include
"
D3DGame.h
"
#include
"
D3DCamera.h
"
#include
"
CoordCross.h
"
#include
<
stdio.h
>
HINSTANCE g_hInst;
HWND g_hWnd;
IDirect3D9
*
g_pD3D
=
NULL;
IDirect3DDevice9
*
g_pD3DDevice
=
NULL;
CMouseInput
*
g_pMouseInput
=
NULL;
CKeyboardInput
*
g_pKeyboardInput
=
NULL;
CD3DCamera
*
g_pD3DCamera
=
NULL;
CCoordCross
*
g_pCoordCross
=
NULL;
//
鼠标输入单元测试函数
void
TestMouseInput();
void
TestKeyboardInput();
void
Initialize(HINSTANCE hInst, HWND hWnd)
{
g_hInst
=
hInst;
g_hWnd
=
hWnd;
InitD3D(
&
g_pD3D,
&
g_pD3DDevice, hWnd);
g_pMouseInput
=
new
CMouseInput;
g_pMouseInput
->
Initialize(hInst,hWnd);
g_pKeyboardInput
=
new
CKeyboardInput;
g_pKeyboardInput
->
Initialize(hInst,hWnd);
g_pD3DCamera
=
new
CD3DCamera;
}
void
LoadContent()
{
g_pCoordCross
=
new
CCoordCross;
g_pD3DCamera
->
SetCameraPos(D3DXVECTOR3(
3.0f
,
2.0f
,
-
8.0f
));
}
void
Update()
{
g_pMouseInput
->
GetState();
g_pKeyboardInput
->
GetState();
g_pD3DCamera
->
Update();
}
void
Draw()
{
g_pD3DDevice
->
SetTransform(D3DTS_VIEW,
&
g_pD3DCamera
->
GetViewMatrix());
g_pD3DDevice
->
Clear(
0
, NULL, D3DCLEAR_TARGET
|
D3DCLEAR_ZBUFFER, D3DCOLOR_RGBA(
100
,
149
,
237
,
255
),
1.0f
,
0
);
if
(SUCCEEDED(g_pD3DDevice
->
BeginScene()))
{
g_pCoordCross
->
Draw();
g_pD3DDevice
->
EndScene();
}
g_pD3DDevice
->
Present(NULL, NULL, NULL, NULL);
}
void
UnloadContent()
{
ReleaseCOM(g_pCoordCross);
}
void
Dispose()
{
ReleaseCOM(g_pD3DCamera);
ReleaseCOM(g_pKeyboardInput);
ReleaseCOM(g_pMouseInput);
ReleaseCOM(g_pD3DDevice);
ReleaseCOM(g_pD3D);
}
void
TestMouseInput()
{
POINT point;
g_pMouseInput
->
GetState();
g_pMouseInput
->
GetPosition(point);
TCHAR tmpText[
50
];
if
(g_pMouseInput
->
LeftButton()
==
BUTTONSTATE_PRESSED)
{
sprintf(tmpText,
"
鼠标左键已按下,X-Y坐标为(%d,%d)
"
,point.x,point.y);
MessageBox(NULL,tmpText,
"
提示
"
,MB_OK
|
MB_ICONINFORMATION);
}
else
if
(g_pMouseInput
->
MiddleButton()
==
BUTTONSTATE_PRESSED)
{
sprintf(tmpText,
"
鼠标滚轮已按下,X-Y坐标为(%d,%d)
"
,point.x,point.y);
MessageBox(NULL,tmpText,
"
提示
"
,MB_OK
|
MB_ICONINFORMATION);
}
else
if
(g_pMouseInput
->
RightButton()
==
BUTTONSTATE_PRESSED)
{
sprintf(tmpText,
"
鼠标右键已按下,X-Y坐标为(%d,%d)
"
,point.x,point.y);
MessageBox(NULL,tmpText,
"
提示
"
,MB_OK
|
MB_ICONINFORMATION);
}
}
void
TestKeyboardInput()
{
TCHAR tmpText[
50
];
//
获得键盘输入设备状态
g_pKeyboardInput
->
GetState();
//
单键检测
if
(g_pKeyboardInput
->
IsKeyDown(DIK_D))
{
sprintf(tmpText,
"
D键被按下
"
);
MessageBox(NULL,tmpText,
"
提示
"
,MB_OK
|
MB_ICONINFORMATION);
}
//
组合键检测
else
if
(g_pKeyboardInput
->
IsKeyDown(DIK_A)
&
g_pKeyboardInput
->
IsKeyDown(DIK_S))
{
sprintf(tmpText,
"
A&S组合键被按下
"
);
MessageBox(NULL,tmpText,
"
提示
"
,MB_OK
|
MB_ICONINFORMATION);
}
}