DirectX 基础问题求教_百度知道下次自动登录
现在的位置:
& 综合 & 正文
DirectX游戏开发编程基础之Directx***与vs2010测试
C++一个重要的应用就是游戏开发, 而游戏开发就不得不谈directx,关于directx的众多知识本人还没有这个资格去谈论,只是刚刚起步开始学习,第一课也就是和大多数新东西一样的环境***配置。
学习DirectX游戏开发当然要***的就是DirectX SDK,一下是DX SDK的下载地址:没有钱最新的是June 2010版本了,我***的是February 2010版的,然后就是一个IDE了,一直都用VS,所以当然首选VS2010咯!
至于***就没有什么多数的了,***好后就是配置了。
1. 首先把DirectX SDK***目录中Lib和Include目录下的文件分别拷贝到VS2010***目录中的Lib和Include目录;
2. 打开vs2010新建一个Win32--& Win32项目,然后在应用设置中附加选项设置为 “空项目” 如图:
创建好项目后添加一个类粘贴一下:
#pragma comment (lib,"d3d9.lib")
#pragma comment (lib,"d3dx9.lib")
#pragma comment (lib,"winmm.lib")
#include &Windows.h&
#include &d3dx9.h&
#include &MMSystem.h&
LPDIRECT3D9 g_pD3D;
LPDIRECT3DDEVICE9 g_pd3dD
LPDIRECT3DVERTEXBUFFER9 g_pVB;
struct CUSTOMVERTEX{
FLOAT x, y,
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)
HRESULT InitObject()
CUSTOMVERTEX triangle[] =
{ -1.0f,-1.0f, 0.0f, 0xffff0000, },
1.0f,-1.0f, 0.0f, 0xff0000ff, },
0.0f, 1.0f, 0.0f, 0xffffffff, }
if (FAILED(g_pd3dDevice-&CreateVertexBuffer(3*sizeof(CUSTOMVERTEX),0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVB, NULL)))
return E_FAIL;
if (FAILED(g_pVB-&Lock(0,sizeof(triangle), &pVertices, 0)))
return E_FAIL;
memcpy(pVertices, triangle, sizeof(triangle));
g_pVB-&Unlock();
return S_OK;
HRESULT InitD3D(HWND hWnd)
g_pD3D = Direct3DCreate9(D3D_SDK_VERSION);
if (NULL == g_pD3D)
return E_FAIL;
D3DPRESENT_PARAMETERS d3
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
if(FAILED( g_pD3D-&CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice)))
return E_FAIL;
g_pd3dDevice-&SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
// Turn off D3D lighting, since we are providing our own vertex colors
g_pd3dDevice-&SetRenderState( D3DRS_LIGHTING, FALSE );
if (FAILED(InitObject()))
return E_FAIL;
return S_OK;
void SetupMatrices()
D3DXMATRIX matW
UINT iTime = timeGetTime() % 1000;
FLOAT fAngle = iTime * ( 2.0f * D3DX_PI ) / 1000.0f;
D3DXMatrixRotationY(&matWorld, fAngle);
g_pd3dDevice-&SetTransform(D3DTS_WORLD, &matWorld);
D3DXVECTOR3 vEyePt(0.0f, 3.0f, -5.0f);
D3DXVECTOR3 vLookAtPt(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 vUp(0.0f, 1.0f, 0.0f);
D3DXMATRIXA16 matV
D3DXMatrixLookAtLH(&matView, &vEyePt, &vLookAtPt, &vUp);
g_pd3dDevice-&SetTransform(D3DTS_VIEW, &matView);
D3DXMATRIX matP
D3DXMatrixPerspectiveFovLH(&matPoj, D3DX_PI/4, 1.0f, 1.0f, 100.0f);
g_pd3dDevice-&SetTransform(D3DTS_PROJECTION, &matPoj);
void Render()
g_pd3dDevice-&Clear(0,NULL,
D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 255), 1.0f, 0);
g_pd3dDevice-&BeginScene();
SetupMatrices();
g_pd3dDevice-&SetStreamSource(0, g_pVB, 0, sizeof(CUSTOMVERTEX));
g_pd3dDevice-&SetFVF(D3DFVF_CUSTOMVERTEX);
g_pd3dDevice-&DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
g_pd3dDevice-&EndScene();
g_pd3dDevice-&Present(NULL, NULL, NULL, NULL);
void Cleanup()
if (g_pd3dDevice)
g_pd3dDevice-&Release();
if (g_pD3D)
g_pD3D-&Release();
if (g_pVB)
g_pVB-&Release();
LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
switch (msg)
case WM_DESTROY:
PostQuitMessage(0);
case WM_PAINT:
ValidateRect(hWnd, NULL);
return DefWindowProc(hWnd, msg, wParam, lParam);
INT WINAPI WinMain(__in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in_opt LPSTR lpCmdLine, __in int nShowCmd )
WNDCLASSEX wc = {sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0, 0,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
L"Direct3D", NULL};
RegisterClassEx(&wc);
// Create the application window
HWND hWnd = CreateWindow(L"Direct3D",
L"Learn", WS_OVERLAPPEDWINDOW,
100, 100, 300, 300,
GetDesktopWindow(),
NULL, wc.hInstance, NULL);
ShowWindow(hWnd, SW_SHOW);
if (SUCCEEDED(InitD3D(hWnd)))
ShowWindow(hWnd, SW_SHOWDEFAULT);
UpdateWindow(hWnd);
ZeroMemory(&msg, sizeof(msg));
while (msg.message != WM_QUIT)
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
TranslateMessage(&msg);
DispatchMessage(&msg);
UnregisterClass( L"Direct3D", wc.hInstance );
Cleanup();
return nShowC
好了,运行下结果如下:
这样就成功了,代码以后在学习吧,这个方法省去了中提到的方法中的很多步骤,而且不用每次新建一个项目就要很多配置,在每次新建项目只需要加入以下代码:
#pragma comment (lib,"d3d9.lib")
#pragma comment (lib,"d3dx9.lib")
#pragma comment (lib,"winmm.lib")
即可完成配置,刚开始很简单,继续加油吧!
&&&&推荐文章:
【上篇】【下篇】