#include ".\d3dinit.h"
CD3DInit::CD3DInit(void)
{
g_pD3D = NULL;
g_pd3dDevice = NULL;
g_SrcHeight = 768;
g_SrcWeight = 1024;
}
CD3DInit::~CD3DInit(void)
{
Release(g_pd3dDevice);
Release(g_pD3D);
}
//--------------------------------------------------------------------------------------
// Name: Initialise
// Desc: 全部的初始化
//--------------------------------------------------------------------------------------
BOOL CD3DInit::GameInit(HWND hWnd,HINSTANCE hInstance )
{ //
LoadConfigureFiles(".\\configure.ini","screen");
if(SUCCEEDED(InitD3D(hWnd)))
{
//初始化输入输出设备(DInput)
g_pInput.InitInput(hWnd,hInstance);
g_pMouseCur = new CCursor();
g_pMouseCur->InitDraw(g_pd3dDevice);
g_pMouseCur->InitCursor(45.0f);
g_pMouseCur->SetTexture("pic\\mouse.bmp",0);
g_pSkyBox = new CSkyBox(g_pd3dDevice);
g_pSkyBox->InitSkyBox(50);
g_pSkyBox->SetTexture("pic\\skybox1_posx.jpg",0);
g_pSkyBox->SetTexture("pic\\skybox1_negx.jpg",1);
g_pSkyBox->SetTexture("pic\\skybox1_posy.jpg",2);
g_pSkyBox->SetTexture("pic\\skybox1_negy.jpg",3);
g_pSkyBox->SetTexture("pic\\skybox1_posz.jpg",4);
g_pSkyBox->SetTexture("pic\\skybox1_negz.jpg",5);
return true;
}
return true;
}
//--------------------------------------------------------------------------------------
// Name: InitD3D
// Desc: D3D的初始化
//--------------------------------------------------------------------------------------
HRESULT CD3DInit::InitD3D(HWND hWnd)
{
if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
return E_FAIL;
D3DDISPLAYMODE d3ddm;
if( FAILED( g_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm ) ) )
{
return E_FAIL;
}
//设置传递给LPDIRECT3D9::CreateDevice的参数
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = FALSE; // 指明是窗口模式,而非全局独占模式
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; // 设定换页效果为丢弃后台缓存
d3dpp.BackBufferFormat = d3ddm.Format; //色彩深度为桌面的色彩深度
d3dpp.BackBufferWidth = g_SrcWeight; //后缓冲宽度
d3dpp.BackBufferHeight = g_SrcHeight; //后缓冲高度
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT; //屏幕刷新率
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE;//翻转模式
//d3dpp.EnableAutoDepthStencil = true;
//d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT //使用当前显卡作注设备
, D3DDEVTYPE_HAL //硬件抽象层设备D3DDEVTYPE_REF 软件模拟
, hWnd
, D3DCREATE_SOFTWARE_VERTEXPROCESSING, //用软件的方法处理顶点数据
//参数若为D3DCREATE_HARDWARE_VERTEXPROCESSING则使用硬件
&d3dpp, &g_pd3dDevice ) ) )
{
return E_FAIL;
}
g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
return S_OK;
}
//--------------------------------------------------------------------------------------
// Name: CheckDevice
// Desc: D3D设备检测,如果丢失恢复设备
//--------------------------------------------------------------------------------------
bool CD3DInit::CheckDevice()
{
HRESULT flag;
flag = g_pd3dDevice->TestCooperativeLevel();
switch(flag)
{
case D3DERR_DEVICELOST:
{
return false;
}
case D3DERR_DEVICENOTRESET:
{
if(FAILED(g_pd3dDevice->Reset(&d3dpp)))
{
return false;
}
else
{
}
return true;
}
default: return true;
}
}
bool CD3DInit::LoadConfigureFiles(char *strFile,char *strSection)
{
GetPrivateProfileString(strSection,"height","0",
g_StrArray,FILESIZE,strFile);
g_SrcHeight=(int)atoi(g_StrArray);
GetPrivateProfileString(strSection,"weight","0",
g_StrArray,FILESIZE,strFile);
g_SrcWeight=(int)atoi(g_StrArray);
return true;
}
void CD3DInit::Set3DCamera()
{
D3DXMATRIX matWorld;
D3DXMATRIX matView;
D3DXMATRIX matProj;
D3DXMatrixLookAtLH(&matView, &g_pCamera.m_vPosition,
&g_pCamera.m_vViewAt,
&g_pCamera.m_vUpAxis);
g_pd3dDevice->SetTransform(D3DTS_VIEW, &matView);
D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4, 1024.0f/768.0f, 1.0f,5000.0f);
g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &matProj);
}
void CD3DInit::RenderInput()
{
g_pInput.ReadKeyboard();
g_pInput.ReadMouse();
g_pCamera.HandleInput(&g_pInput);
}
//--------------------------------------------------------------------------------------
// Name: Render
// Desc: 整个程序的渲染部分
//--------------------------------------------------------------------------------------
void CD3DInit::Render()
{
if( NULL == g_pd3dDevice )
return;
// 将填充后台缓冲区置为黑色
if(CheckDevice())
{
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );
g_pd3dDevice->BeginScene();
RenderInput();
Set3DCamera();
g_pMouseCur->MouseDraw();
g_pMouseCur->Render(0);
g_pSkyBox->Render(g_pCamera.m_vPosition);
g_pd3dDevice->EndScene();
g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}
}
- 1
- 2
- 3
- 4
前往页