#include "gameengine.h"
GameEngine* g_GameEngine;
IDirect3D9* g_pd3d=NULL;
IDirect3DDevice9* g_pd3ddev=NULL;
ID3DXMesh *g_pMesh=NULL;
IDirectInput8* g_pidi=NULL;
IDirectInputDevice8* g_pididk=NULL;
IDirectInputDevice8* g_pididm=NULL;
char KeyBuffer[256];
DIDEVICEOBJECTDATA dod[10];
std::vector<D3DMATERIAL9> m(0);
std::vector<IDirect3DTexture9*> tex(0);
BOOL GameInitial(HINSTANCE hInstance)
{
g_GameEngine=new GameEngine(hInstance,L"WndClass",L"Game Box",800,600,20);
if(g_GameEngine==NULL)
return FALSE;
return TRUE;
}
void GameStart()
{
g_pd3d=::Direct3DCreate9(D3D_SDK_VERSION);
if(g_pd3d==NULL)
return ;
D3DDISPLAYMODE d3ddm;
g_pd3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&d3ddm);
D3DPRESENT_PARAMETERS d3dpp;
::ZeroMemory(&d3dpp,sizeof(d3dpp));
d3dpp.BackBufferFormat=d3ddm.Format;
d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD;
d3dpp.Windowed=TRUE;
g_pd3d->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,g_GameEngine->GetWnd(),D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&g_pd3ddev);
if(g_pd3ddev==NULL)
return ;
InitMesh();
}
void GameEnd()
{
}
void GamePaint()
{
SetLight(D3DXVECTOR3(0,0,-30));
g_pd3ddev->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,0);
g_pd3ddev->BeginScene();
g_pd3ddev->SetRenderState(::D3DRS_LIGHTING,true);
for(int i=0;i<m.size();i++)
{
g_pd3ddev->SetMaterial(&m[i]);
::SetTexture(tex[i]);
g_pMesh->DrawSubset(i);
}
g_pd3ddev->EndScene();
g_pd3ddev->Present(0,0,0,0);
}
void GameCycle()
{
}
void SetLight(const D3DXVECTOR3& pos)
{
D3DLIGHT9 light_spot;
::ZeroMemory(&light_spot,sizeof(light_spot));
light_spot.Type=::D3DLIGHT_POINT;
light_spot.Diffuse=D3DXCOLOR(1.0f,1.0f,1.0f,1.0f);
light_spot.Ambient=D3DXCOLOR(1.0f,1.0f,1.0f,1.0f);
light_spot.Specular=D3DXCOLOR(0.0f,0.0f,0.0f,1.0f);
light_spot.Position=pos;
light_spot.Attenuation0=1.0f;
light_spot.Attenuation1=0.0f;
light_spot.Attenuation2=0.0f;
light_spot.Range=1000.0f;;
g_pd3ddev->SetLight(0,&light_spot);
g_pd3ddev->LightEnable(0,true);
}
void SetMaterial(D3DXCOLOR am,D3DXCOLOR di,D3DXCOLOR sp)
{
D3DMATERIAL9 m;
::ZeroMemory(&m,sizeof(m));
m.Ambient=am;
m.Diffuse=di;
m.Specular=sp;
m.Emissive=D3DXCOLOR(0.0f,0.0f,0.0f,1.0f);
m.Power=5.0f;
g_pd3ddev->SetMaterial(&m);
}
void InitMesh()
{ ID3DXBuffer* pa,*pm;
DWORD num;
::D3DXLoadMeshFromX(L"airplane.X",::D3DXMESH_MANAGED,g_pd3ddev,&pa,&pm,0,&num,&g_pMesh);
D3DXMATERIAL *p=(D3DXMATERIAL*)pm->GetBufferPointer();
for(int i=0;i<num;i++)
{
p[i].MatD3D.Ambient=p[i].MatD3D.Diffuse;
m.push_back(p[i].MatD3D);
IDirect3DTexture9* t=0;
::D3DXCreateTextureFromFileA(g_pd3ddev,p[i].pTextureFilename,&t);
tex.push_back(t);
}
g_pMesh->OptimizeInplace(::D3DXMESHOPT_COMPACT|::D3DXMESHOPT_ATTRSORT|::D3DXMESHOPT_VERTEXCACHE,(DWORD*)pa->GetBufferPointer(),0,0,0);
::D3DXComputeNormals(g_pMesh,0);
}
void SetCamera(D3DXVECTOR3 eye,D3DXVECTOR3 at)
{
D3DXVECTOR3 up(0,1,0);
D3DXMATRIX view;
::D3DXMatrixLookAtLH(&view,&eye,&at,&up);
g_pd3ddev->SetTransform(::D3DTS_VIEW,&view);
}
void SetProjection()
{
D3DXMATRIX pro;
::D3DXMatrixPerspectiveFovLH(&pro,D3DX_PI/3,800/600,1.0f,1000.0f);
g_pd3ddev->SetTransform(::D3DTS_PROJECTION,&pro);
}
void SetTexture(IDirect3DTexture9* g_pTexture)
{
g_pd3ddev->SetTexture(0,g_pTexture);
g_pd3ddev->SetSamplerState(0,::D3DSAMP_MIPFILTER,D3DTEXF_POINT);
g_pd3ddev->SetSamplerState(0,::D3DSAMP_MAGFILTER,D3DTEXF_LINEAR);
g_pd3ddev->SetSamplerState(0,::D3DSAMP_MINFILTER,D3DTEXF_LINEAR);
}
BOOL InitInput()
{
HRESULT hr=::DirectInput8Create(g_GameEngine->GetInstance(),DIRECTINPUT_VERSION,IID_IDirectInput8,(void**)&g_pidi,0);
if(FAILED(hr))
{
::MessageBox(0,L"建立IDrectInput8接口失败!",L"警告",0);
return FALSE;
}
return TRUE;
}
BOOL InitKeyboard()
{
HRESULT hr=g_pidi->CreateDevice(::GUID_SysKeyboard,&g_pididk,0);
if(FAILED(hr))
{
::MessageBox(0,L"建立IDrectInputDevice8接口失败!",L"警告",0);
return FALSE;
}
hr=g_pididk->SetDataFormat(&c_dfDIKeyboard);
if(FAILED(hr))
{
::MessageBox(0,L"设置键盘格式失败!",L"警告",0);
return FALSE;
}
hr=g_pididk->SetCooperativeLevel(g_GameEngine->GetWnd(),DISCL_EXCLUSIVE|DISCL_FOREGROUND);
if(FAILED(hr))
{
::MessageBox(0,L"键盘协调级别设置失败!",L"警告",0);
return FALSE;
}
hr=g_pididk->Acquire();
if(FAILED(hr))
{
::MessageBox(0,L"取得键盘访问权失败!",L"警告",0);
return FALSE;
}
return TRUE;
}
BOOL IsPress(int key)
{
::ZeroMemory(KeyBuffer,sizeof(KeyBuffer));
HRESULT hr=g_pididk->GetDeviceState(sizeof(KeyBuffer),KeyBuffer);
if(FAILED(hr))
{
g_pididk->Acquire();
hr=g_pididk->GetDeviceState(sizeof(KeyBuffer),KeyBuffer);
if(FAILED(hr))
{
::MessageBox(0,L"取得键盘状态失败!",L"警告",0);
return FALSE;
}
}
if(KeyBuffer[key]&0x80)
return TRUE;
else
return FALSE;
}
BOOL InitMouse()
{
HRESULT hr;
hr=g_pidi->CreateDevice(GUID_SysMouse,&g_pididm,0);
if(FAILED(hr))
{
::MessageBox(0,L"创建鼠标设备失败!",L"警告",0);
return FALSE;
}
hr=g_pididm->SetDataFormat(&c_dfDIMouse);
if(FAILED(hr))
{
::MessageBox(0,L"设置鼠标数据格式失败!",L"警告",0);
return FALSE;
}
hr=g_pididm->SetCooperativeLevel(g_GameEngine->GetWnd(),DISCL_EXCLUSIVE|DISCL_FOREGROUND);
if(FAILED(hr))
{
::MessageBox(0,L"创建鼠标设备的协作等级失败!",L"警告",0);
return FALSE;
}
DIPROPDWORD prop;
::ZeroMemory(&prop,sizeof(prop));
prop.diph.dwSize=sizeof(DIPROPDWORD);
prop.diph.dwHeaderSize=sizeof(DIPROPHEADER);
prop.diph.dwObj=0;
prop.diph.dwHow=DIPH_DEVICE;
prop.dwData=10;
hr=g_pididm->SetProperty(DIPROP_BUFFERSIZE,&prop.diph);
if(FAILED(hr))
{
::MessageBox(0,L"设置鼠标属性失败!",L"警告",0);
return FALSE;
}
hr=g_pididm->Acquire();
if(FAILED(hr))
{
::MessageBox(0,L"获取鼠标访问权限失败!",L"警告",0);
return FALSE;
}
return TRUE;
}
void CaptureMouse(DWORD& mousex,DWORD& mousey)
{
::ZeroMemory(dod,sizeof(dod));
DWORD num=1;
HRESULT hr;
for(int i=0;i<10;i++)
{
hr=g_pididm->GetDeviceData(sizeof(DIDEVICEOBJECTDATA),&dod[i],&num,0);
if(FAILED(hr))
{
g_pididm->Acquire();
hr=g_pididm->GetDeviceData(sizeof(DIDEVICEOBJECTDATA),&dod[i],&num,0);
}
if(dod[i].dwOfs==DIMOFS_X)
{
mousex+=dod[i].dwData;
}
if(dod[i].dwOfs==DIMOFS_Y)
{
mousey+=dod[i].dwData;
}
}
}
BOOL IsLButtonPress()
{
for(int i=0;i<10;i++)
{
if(dod[i].dwOfs==DIMOFS_BUTTON0)
{
return TRUE;
}
}
return FALSE;
}
BOOL IsRButtonPress()
{
for(int i=0;i<10;i++)
{
if(dod[i].dwOfs==DIMOFS_BUTTON1)
{
return TRUE;
}
}
return FALSE;
}