1 VS2010+DirectX9.环境安装
Win7下下载DirectX9 SDK包,解压。
将需要的.lib文件添加到:C:\Program Files\Microsoft SDKs\Windows\v7.0A\Lib
将需要的.h头文件添加到:C:\Program Files\MicrosoftSDKs\Windows\v7.0A\Include
将DX包里面解压出来的需要的版本的.DLL防止到:C:\Windows\System32
用VS2010,建立一个空白的win32 Windows应用程序。如果是win32 控制台应用程序,需要到工程属性的连接-系统-子系统修改为windows就OK了。
主线:
(1)创建Direct3D接口
IDirect3D9
(2)由显卡显示模式,窗口显示属性参数来创建设备接口
D3DDISPLAYMODE
D3DPRESENT_PARAMETERS
IDirect3DDevice9 CreateDevice
(3)坐标系,图形数据的指定,创建顶点缓存(并将数据memcpy到顶点缓存)
D3DXMATRIX
D3DXMatrixOrthoLHh或D3DXMatrixPerspectiveFovLH
SetTransform/SetRenderState
stVertexData设置
CreateVertexBuffer
(4)坐标系的变换,设备接口对图形的渲染
BeginScene
/*//可选
D3DXMatrixTranslation
D3DXMatrixRotationY
SetTransform
*/
SetStreamSource
SetFVF
DrawPrimitive
EndScene
Present
(5)COM对象清理,Release减少内存引用计数。
3.注意
(1)MSG结构体和D3DPRESENT_PARAMETERS记得用ZeroMemory将其初始化,否则后续访问会出错。
(2)每个函数要多查阅MSDN,多用才能在能力上面提高。
4.代码例子
//common.h
#include <d3d9.h>
#include <d3dx9.h>LPDIRECT3DDEVICE9 g_d3dDevice=NULL;
LPDIRECT3DVERTEXBUFFER9 g_vertexBuff=NULL;
//common.cpp
#include "common.h"
LRESULT WINAPI MsgProc(HWND wnd,UINT msg,WPARAM wparam,LPARAM lparam)
{
int virtual_code;
switch(msg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
//break;
case WM_KEYUP:
virtual_code=(int)wparam;
if(virtual_code==VK_ESCAPE)
PostQuitMessage(0);
return 0;
//break;
}
return DefWindowProc(wnd,msg,wparam,lparam);
}
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE prevhInst, LPSTR cmdLine, int show)
{
HWND hwd;
MSG msg;
WNDCLASSEX wc={sizeof(WNDCLASSEX),CS_CLASSDC,MsgProc,NULL,NULL,
GetModuleHandle(NULL),NULL,NULL,NULL,NULL,WINDOWCLASS,NULL};//GetModuleHandle(NULL)
RegisterClassEx(&wc);
HWND hWnd=CreateWindow(WINDOWCLASS,TITLENAME,WS_OVERLAPPEDWINDOW|WS_VISIBLE,100,100,WNDWIDTH,WNDHIGHT,
GetDesktopWindow(),NULL,wc.hInstance,NULL);
if(InitializeD3D(hWnd,false))
{
ShowWindow(hWnd,SW_SHOWDEFAULT);
UpdateWindow(hWnd);
ZeroMemory(&msg,sizeof(msg));
while(msg.message!=WM_QUIT)
{
if(PeekMessage(&msg,NULL,0U,0U,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
RenderScene();//渲染
}
}//end while
}//end if
//清理
ShutDown();
UnregisterClass(WINDOWCLASS,hInst);
return 0;
}
bool InitializeD3D(HWND hwnd,bool fullscreen)
{
D3DDISPLAYMODE displayMode;
g_d3dInterface=Direct3DCreate9(D3D_SDK_VERSION);
if(g_d3dInterface==NULL)
return false;
if(FAILED(g_d3dInterface->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&displayMode)))
return false;
D3DPRESENT_PARAMETERS presentPram;
ZeroMemory(&presentPram,sizeof(presentPram));
if(fullscreen)
{
presentPram.Windowed=FALSE;//不是一个小窗口
presentPram.BackBufferWidth=WNDWIDTH;//全屏是需要设置的窗口的宽度和长度
presentPram.BackBufferHeight=WNDHIGHT;
}
else
{
presentPram.Windowed=TRUE;
}
presentPram.BackBufferFormat=displayMode.Format;
presentPram.SwapEffect=D3DSWAPEFFECT_DISCARD;
if(FAILED(g_d3dInterface->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hwnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&presentPram,&g_d3dDevice)))
return false;
if(!InitializeObjects())
return false;
return true;
}
bool InitializeObjects()
{
D3DXMatrixPerspectiveFovLH(&g_projectionMatrix,D3DX_PI/4,WNDWIDTH/WNDHIGHT,0.1f,1000.0f);
g_d3dDevice->SetTransform(D3DTS_PROJECTION,&g_projectionMatrix);
g_d3dDevice->SetRenderState(D3DRS_LIGHTING,FALSE);
g_d3dDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
tagD3DVertextData vertexData[]=
{
{-0.3f, -0.3f, 1.0f, D3DCOLOR_XRGB(255,255,0)},
{0.3f, -0.3f, 1.0f, D3DCOLOR_XRGB(255,0,0)},
{0.0f, 0.3f, 1.0f, D3DCOLOR_XRGB(0,0,255)}
};
if(FAILED(g_d3dDevice->CreateVertexBuffer(sizeof(tagD3DVertextData)*3,0,D3DFVF_XYZ|D3DFVF_DIFFUSE,D3DPOOL_DEFAULT,&g_vertexBuff,NULL)))
return false;
void *ptr=NULL;
if(FAILED(g_vertexBuff->Lock(0,sizeof(vertexData),(void**)(&ptr),0)))
return false;
memcpy(ptr,vertexData,sizeof(vertexData));
g_vertexBuff->Unlock();
return true;
}
void RenderScene()
{
g_d3dDevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,0);
g_d3dDevice->BeginScene();
D3DXMatrixTranslation(&g_translationMatrix,0.0f,0.0f,7.0f);
D3DXMatrixRotationY(&g_rotationMatrix,g_angle);
g_worldMatrix=g_translationMatrix*g_rotationMatrix;
g_angle+=0.02f;
if(g_angle>360)g_angle=0.0f;
g_d3dDevice->SetTransform(/*D3DTS_PROJECTION*/D3DTS_WORLD,&g_worldMatrix);
g_d3dDevice->SetStreamSource(0,g_vertexBuff,0,sizeof(tagD3DVertextData));
g_d3dDevice->SetFVF(D3DFVF);
g_d3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,1);
g_d3dDevice->EndScene();
g_d3dDevice->Present(NULL,NULL,NULL,NULL);
}
void ShutDown()
{
if(g_vertexBuff!=NULL)
g_vertexBuff->Release();
if(g_d3dDevice!=NULL)
g_d3dDevice->Release();
if(g_d3dInterface!=NULL)
g_d3dInterface->Release();
g_vertexBuff=NULL;
g_d3dDevice=NULL;
g_d3dInterface=NULL;
}