C++ Exercises(十一)

1

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

None.gif #include < d3d9.h >
None.gif
#pragma warning(disable:4996) // disabledeprecatedwarning
None.gif#include
< strsafe.h >
None.gif
#pragma warning(default:4996)
None.gif
None.gif
// -----------------------------------------------------------------------------
None.gif
// Globalvariables
None.gif
// -----------------------------------------------------------------------------
None.gif
LPDIRECT3D9g_pD3D = NULL; // UsedtocreatetheD3DDevice
None.gif
LPDIRECT3DDEVICE9g_pd3dDevice = NULL; // Ourrenderingdevice
None.gif
None.gif
// -----------------------------------------------------------------------------
None.gif
// Name:InitD3D()
None.gif
// Desc:InitializesDirect3D
None.gif
// -----------------------------------------------------------------------------
None.gif
HRESULTInitD3D(HWNDhWnd)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
//CreatetheD3Dobject,whichisneededtocreatetheD3DDevice.
InBlock.gif
if(NULL==(g_pD3D=Direct3DCreate9(D3D_SDK_VERSION)))
InBlock.gif
returnE_FAIL;
InBlock.gif
InBlock.gif
//SetupthestructureusedtocreatetheD3DDevice.Mostparametersare
InBlock.gif
//zeroedout.WesetWindowedtoTRUE,sincewewanttodoD3Dina
InBlock.gif
//window,andthensettheSwapEffectto"discard",whichisthemost
InBlock.gif
//efficientmethodofpresentingthebackbuffertothedisplay.And
InBlock.gif
//werequestabackbufferformatthatmatchesthecurrentdesktopdisplay
InBlock.gif
//format.
InBlock.gif
D3DPRESENT_PARAMETERSd3dpp;
InBlock.gifZeroMemory(
&d3dpp,sizeof(d3dpp));
InBlock.gifd3dpp.Windowed
=TRUE;
InBlock.gifd3dpp.SwapEffect
=D3DSWAPEFFECT_DISCARD;
InBlock.gifd3dpp.BackBufferFormat
=D3DFMT_UNKNOWN;
InBlock.gif
InBlock.gif
//CreatetheDirect3Ddevice.Hereweareusingthedefaultadapter(most
InBlock.gif
//systemsonlyhaveone,unlesstheyhavemultiplegraphicshardwarecards
InBlock.gif
//installed)andrequestingtheHAL(whichissayingwewantthehardware
InBlock.gif
//deviceratherthanasoftwareone).Softwarevertexprocessingis
InBlock.gif
//specifiedsinceweknowitwillworkonallcards.Oncardsthatsupport
InBlock.gif
//hardwarevertexprocessing,though,wewouldseeabigperformancegain
InBlock.gif
//byspecifyinghardwarevertexprocessing.
InBlock.gif
if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,
InBlock.gifD3DCREATE_SOFTWARE_VERTEXPROCESSING,
InBlock.gif
&d3dpp,&g_pd3dDevice)))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
returnE_FAIL;
ExpandedSubBlockEnd.gif}

InBlock.gif
//Devicestatewouldnormallybesethere
InBlock.gif

InBlock.gif
returnS_OK;
ExpandedBlockEnd.gif}

None.gif
None.gif
// -----------------------------------------------------------------------------
None.gif
// Name:Cleanup()
None.gif
// Desc:Releasesallpreviouslyinitializedobjects
None.gif
// -----------------------------------------------------------------------------
None.gif
VOIDCleanup()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
//释放D3D设备对象
InBlock.gif
if(g_pd3dDevice!=NULL)
InBlock.gifg_pd3dDevice
->Release();
InBlock.gif
InBlock.gif
//释放D3D对象
InBlock.gif
if(g_pD3D!=NULL)
InBlock.gifg_pD3D
->Release();
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
// -----------------------------------------------------------------------------
None.gif
// Name:Render()
None.gif
// Desc:Drawsthescene
None.gif
// -----------------------------------------------------------------------------
None.gif
VOIDRender()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
if(NULL==g_pd3dDevice)
InBlock.gif
return;
InBlock.gif
//Clearthebackbuffertoabluecolor
InBlock.gif
g_pd3dDevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,255),1.0f,0);
InBlock.gif
//Beginthescene
InBlock.gif
if(SUCCEEDED(g_pd3dDevice->BeginScene()))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//Renderingofsceneobjectscanhappenhere
InBlock.gif
//Endthescene
InBlock.gif
g_pd3dDevice->EndScene();
ExpandedSubBlockEnd.gif}

InBlock.gif
//Presentthebackbuffercontentstothedisplay
InBlock.gif
g_pd3dDevice->Present(NULL,NULL,NULL,NULL);
ExpandedBlockEnd.gif}

None.gif
None.gif
// -----------------------------------------------------------------------------
None.gif
// Name:MsgProc()
None.gif
// Desc:Thewindow'smessagehandler
None.gif
// -----------------------------------------------------------------------------
None.gif
LRESULTWINAPIMsgProc(HWNDhWnd,UINTmsg,WPARAMwParam,LPARAMlParam)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
switch(msg)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
caseWM_DESTROY:
InBlock.gifCleanup();
InBlock.gifPostQuitMessage(
0);
InBlock.gif
return0;
InBlock.gif
caseWM_PAINT:
InBlock.gifRender();
InBlock.gifValidateRect(hWnd,NULL);
InBlock.gif
return0;
ExpandedSubBlockEnd.gif}

InBlock.gif
returnDefWindowProc(hWnd,msg,wParam,lParam);
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
// -----------------------------------------------------------------------------
None.gif
// Name:WinMain()
None.gif
// Desc:Theapplication'sentrypoint
None.gif
// -----------------------------------------------------------------------------
None.gif
INTWINAPIWinMain(HINSTANCEhInst,HINSTANCE,LPSTR,INT)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
//Registerthewindowclass
ExpandedSubBlockStart.gifContractedSubBlock.gif
WNDCLASSEXwc=dot.gif{sizeof(WNDCLASSEX),CS_CLASSDC,MsgProc,0L,0L,
InBlock.gifGetModuleHandle(NULL),NULL,NULL,NULL,NULL,
ExpandedSubBlockEnd.gif
"D3DTutorial",NULL}
;
InBlock.gifRegisterClassEx(
&wc);
InBlock.gif
//Createtheapplication'swindow
InBlock.gif
HWNDhWnd=CreateWindow("D3DTutorial","D3DTutorial01:CreateDevice",
InBlock.gifWS_OVERLAPPEDWINDOW,
100,100,300,300,
InBlock.gifNULL,NULL,wc.hInstance,NULL);
InBlock.gif
//InitializeDirect3D
InBlock.gif
if(SUCCEEDED(InitD3D(hWnd)))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{//初始化成功
InBlock.gif
//Showthewindow
InBlock.gif
ShowWindow(hWnd,SW_SHOWDEFAULT);
InBlock.gifUpdateWindow(hWnd);
InBlock.gif
//Enterthemessageloop
InBlock.gif
MSGmsg;
InBlock.gif
while(true)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{//消息处理
InBlock.gif
if(msg.message==WM_QUIT)
InBlock.gif
break;
InBlock.gifTranslateMessage(
&msg);
InBlock.gifDispatchMessage(
&msg);
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{//空闲时绘制
InBlock.gif
Render();
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gifUnregisterClass(
"D3DTutorial",wc.hInstance);
InBlock.gif
return0;
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
None.gif
None.gif

2

None.gif
None.gif#include
< d3d9.h >
None.gif
#pragma warning(disable:4996) // disabledeprecatedwarning
None.gif#include
< strsafe.h >
None.gif
#pragma warning(default:4996)
None.gif
None.gifLPDIRECT3D9g_pD3D
= NULL; // UsedtocreatetheD3DDevice
None.gif
LPDIRECT3DDEVICE9g_pd3dDevice = NULL; // Ourrenderingdevice
None.gif
LPDIRECT3DVERTEXBUFFER9g_pVB = NULL; // Buffertoholdvertices顶点缓冲区
None.gif
None.gif
// Astructureforourcustomvertextype
None.gif
struct CUSTOMVERTEX
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifFLOATx,y,z,rhw;
//Thetransformedpositionforthevertex
InBlock.gif
DWORDcolor;//Thevertexcolor
ExpandedBlockEnd.gif
}
;
None.gif
None.gif
// OurcustomFVF,whichdescribesourcustomvertexstructure
None.gif
#define D3DFVF_CUSTOMVERTEX(D3DFVF_XYZRHW|D3DFVF_DIFFUSE)
None.gif
None.gifHRESULTInitD3D(HWNDhWnd)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
//CreatetheD3Dobject.
InBlock.gif
if(NULL==(g_pD3D=Direct3DCreate9(D3D_SDK_VERSION)))
InBlock.gif
returnE_FAIL;
InBlock.gif
InBlock.gif
//SetupthestructureusedtocreatetheD3DDevice
InBlock.gif
D3DPRESENT_PARAMETERSd3dpp;
InBlock.gifZeroMemory(
&d3dpp,sizeof(d3dpp));
InBlock.gifd3dpp.Windowed
=TRUE;
InBlock.gifd3dpp.SwapEffect
=D3DSWAPEFFECT_DISCARD;
InBlock.gifd3dpp.BackBufferFormat
=D3DFMT_UNKNOWN;
InBlock.gif
InBlock.gif
//CreatetheD3DDevice
InBlock.gif
if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,
InBlock.gifD3DCREATE_SOFTWARE_VERTEXPROCESSING,
InBlock.gif
&d3dpp,&g_pd3dDevice)))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
returnE_FAIL;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
//Devicestatewouldnormallybesethere
InBlock.gif

InBlock.gif
returnS_OK;
ExpandedBlockEnd.gif}

None.gif
None.gifHRESULTInitVB()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
//Initializethreeverticesforrenderingatriangle
InBlock.gif
CUSTOMVERTEXvertices[]=
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{150.0f,50.0f,0.5f,1.0f,0xffff0000,},//x,y,z,rhw,color
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{250.0f,250.0f,0.5f,1.0f,0xff00ff00,},
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{50.0f,250.0f,0.5f,1.0f,0xff00ffff,},
ExpandedSubBlockEnd.gif}
;
InBlock.gif
InBlock.gif
//Createthevertexbuffer.Hereweareallocatingenoughmemory
InBlock.gif
//(fromthedefaultpool)toholdallour3customvertices.Wealso
InBlock.gif
//specifytheFVF,sothevertexbufferknowswhatdataitcontains.
InBlock.gif
if(FAILED(g_pd3dDevice->CreateVertexBuffer(3*sizeof(CUSTOMVERTEX),
InBlock.gif
0,D3DFVF_CUSTOMVERTEX,
InBlock.gifD3DPOOL_DEFAULT,
&g_pVB,NULL)))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
returnE_FAIL;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
//Nowwefillthevertexbuffer.Todothis,weneedtoLock()theVBto
InBlock.gif
//gainaccesstothevertices.Thismechanismisrequiredbecuasevertex
InBlock.gif
//buffersmaybeindevicememory.
InBlock.gif
VOID*pVertices;
InBlock.gif
if(FAILED(g_pVB->Lock(0,sizeof(vertices),(void**)&pVertices,0)))
InBlock.gif
returnE_FAIL;
InBlock.gifmemcpy(pVertices,vertices,
sizeof(vertices));
InBlock.gifg_pVB
->Unlock();
InBlock.gif
InBlock.gif
returnS_OK;
ExpandedBlockEnd.gif}

None.gifVOIDCleanup()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
if(g_pVB!=NULL)
InBlock.gifg_pVB
->Release();
InBlock.gif
InBlock.gif
if(g_pd3dDevice!=NULL)
InBlock.gifg_pd3dDevice
->Release();
InBlock.gif
InBlock.gif
if(g_pD3D!=NULL)
InBlock.gifg_pD3D
->Release();
ExpandedBlockEnd.gif}

None.gif
None.gifVOIDRender()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
//Clearthebackbuffertoabluecolor
InBlock.gif
g_pd3dDevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,255),1.0f,0);
InBlock.gif
InBlock.gif
//Beginthescene
InBlock.gif
if(SUCCEEDED(g_pd3dDevice->BeginScene()))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//Drawthetrianglesinthevertexbuffer.
InBlock.gif
g_pd3dDevice->SetStreamSource(0,g_pVB,0,sizeof(CUSTOMVERTEX));
InBlock.gifg_pd3dDevice
->SetFVF(D3DFVF_CUSTOMVERTEX);
InBlock.gifg_pd3dDevice
->DrawPrimitive(D3DPT_TRIANGLELIST,0,1);
InBlock.gif
InBlock.gif
//Endthescene
InBlock.gif
g_pd3dDevice->EndScene();
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
//Presentthebackbuffercontentstothedisplay
InBlock.gif
g_pd3dDevice->Present(NULL,NULL,NULL,NULL);
ExpandedBlockEnd.gif}

None.gif
None.gifLRESULTWINAPIMsgProc(HWNDhWnd,UINTmsg,WPARAMwParam,LPARAMlParam)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
switch(msg)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
caseWM_DESTROY:
InBlock.gifCleanup();
InBlock.gifPostQuitMessage(
0);
InBlock.gif
return0;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
returnDefWindowProc(hWnd,msg,wParam,lParam);
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gifINTWINAPIWinMain(HINSTANCEhInst,HINSTANCE,LPSTR,INT)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
//Registerthewindowclass
ExpandedSubBlockStart.gifContractedSubBlock.gif
WNDCLASSEXwc=dot.gif{sizeof(WNDCLASSEX),CS_CLASSDC,MsgProc,0L,0L,
InBlock.gifGetModuleHandle(NULL),NULL,NULL,NULL,NULL,
ExpandedSubBlockEnd.gif
"D3DTutorial",NULL}
;
InBlock.gifRegisterClassEx(
&wc);
InBlock.gif
//Createtheapplication'swindow
InBlock.gif
HWNDhWnd=CreateWindow("D3DTutorial","D3DTutorial01:CreateDevice",
InBlock.gifWS_OVERLAPPEDWINDOW,
100,100,300,300,
InBlock.gifNULL,NULL,wc.hInstance,NULL);
InBlock.gif
//InitializeDirect3D
InBlock.gif
if(SUCCEEDED(InitD3D(hWnd)))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{//初始化成功
InBlock.gif
//Createthevertexbuffer
InBlock.gif
if(SUCCEEDED(InitVB()))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//Showthewindow
InBlock.gif
ShowWindow(hWnd,SW_SHOWDEFAULT);
InBlock.gifUpdateWindow(hWnd);
InBlock.gif
//Enterthemessageloop
InBlock.gif
MSGmsg;
InBlock.gif
while(true)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{//消息处理
InBlock.gif
if(msg.message==WM_QUIT)
InBlock.gif
break;
InBlock.gifTranslateMessage(
&msg);
InBlock.gifDispatchMessage(
&msg);
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{//空闲时绘制
InBlock.gif
Render();
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gifUnregisterClass(
"D3DTutorial",wc.hInstance);
InBlock.gif
return0;
ExpandedBlockEnd.gif}

None.gif
None.gif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值