转自 http://blog.csdn.net/lfeng1420/article/details/20639665
一、环境
Visual Studio 2012,DirectX SDK (June 2010)
二、准备
1.环境变量(如没有配置请添加)
变量名:DXSDK_DIR
变量值:D:\Software\Program\Microsoft DirectX SDK (June 2010)
这里D:\Software\Program\Microsoft DirectX SDK (June 2010)是我的DirectX安装目录,视情况修改即可
三、步骤
1.打开VS2012,创建一个空的Win32 Project
这里的项目名称(Name)可以自定义,项目位置(Location)可能不同,不过没关系,可以不用修改
点击OK,然后点击Next,勾选Empty Project
点击Finish,完成项目创建。
2.打开项目属性(Properites),配置项目的DirectX环境
主要需修改的项:Configuration Properites->VC++ Directories,Configuration Properites->Linker->General,Configuration Properites->Linker->Input
VC++ Directories:(注意后面的分号)
Include Directories项中添加$(DXSDK_DIR)\Include;
Library Directories项中添加$(DXSDK_DIR)\Lib\x86;
Linker->Input:添加以下三个lib文件d3d9.lib d3dx9.lib Winmm.lib,添加时注意用分号隔开,如下:
d3d9.lib;d3dx9.lib;Winmm.lib;
Linker->General:AdditionalLibrary Directories项中添加$(DXSDK_DIR)\Lib\x86 (可以不加分号)
点击确定应用以上更改。
3.打开Source.cpp,复制以下代码:
- #include <d3dx9.h>
- IDirect3DDevice9* Device=0;
- LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
- {
- switch(msg)
- {
- case WM_DESTROY:PostQuitMessage(0);break;
- case WM_KEYDOWN:
- if(wParam==VK_ESCAPE)DestroyWindow(hwnd);break;
- }
- return DefWindowProcW(hwnd,msg,wParam,lParam);
- }
- bool Init(HINSTANCE hInstance,int width,int height,bool windowed,D3DDEVTYPE devType,IDirect3DDevice9** device)
- {
- WNDCLASSW wc;
- wc.cbClsExtra=0;
- wc.cbWndExtra=0;
- wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
- wc.hCursor=LoadCursorW(0,IDC_ARROW);
- wc.hIcon=LoadIconW(0,IDI_APPLICATION);
- wc.hInstance=hInstance;
- wc.lpfnWndProc=(WNDPROC)WndProc;
- wc.lpszClassName=L"Direct3D";
- wc.lpszMenuName=0;
- wc.style=CS_HREDRAW|CS_VREDRAW;
- if(!RegisterClassW(&wc))
- {
- MessageBoxW(0,L"Register Failed",0,0);
- return false;
- }
-
- HWND hwnd=0;
- hwnd=CreateWindowW(L"Direct3D",L"Direct3D",WS_OVERLAPPEDWINDOW,0,0,width,height,0,0,hInstance,0);
- if(!hwnd)
- {
- MessageBoxW(0,L"CreateWindow Failed",0,0);
- return false;
- }
- ShowWindow(hwnd,SW_SHOW);
- UpdateWindow(hwnd);
-
- HRESULT hr;
- IDirect3D9* d3d9;
- d3d9=Direct3DCreate9(D3D_SDK_VERSION);
-
- int vp=0;
- D3DCAPS9 caps;
- d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT,devType,&caps);
- if(caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
- vp=D3DCREATE_HARDWARE_VERTEXPROCESSING;
- else vp=D3DCREATE_SOFTWARE_VERTEXPROCESSING;
-
- D3DPRESENT_PARAMETERS d3dapp;
- d3dapp.AutoDepthStencilFormat=D3DFMT_D24S8;
- d3dapp.BackBufferCount=1;
- d3dapp.BackBufferFormat=D3DFMT_A8R8G8B8;
- d3dapp.BackBufferHeight=height;
- d3dapp.BackBufferWidth=width;
- d3dapp.EnableAutoDepthStencil=true;
- d3dapp.Flags=0;
- d3dapp.FullScreen_RefreshRateInHz=D3DPRESENT_RATE_DEFAULT;
- d3dapp.hDeviceWindow=hwnd;
- d3dapp.MultiSampleQuality=0;
- d3dapp.MultiSampleType=D3DMULTISAMPLE_NONE;
- d3dapp.PresentationInterval=D3DPRESENT_INTERVAL_IMMEDIATE;
- d3dapp.SwapEffect=D3DSWAPEFFECT_DISCARD;
- d3dapp.Windowed=windowed;
- hr=d3d9->CreateDevice(D3DADAPTER_DEFAULT,devType,hwnd,vp,&d3dapp,device);
- if(FAILED(hr))
- {
- MessageBoxW(0,L"CreateDevice Failed",0,0);
- d3d9->Release();
- return false;
- }
- d3d9->Release();
- return true;
- }
- int EnterMsgLoop(bool (*ptr_display)(float TimeDelta))
- {
- MSG msg;
- ZeroMemory(&msg,sizeof(MSG));
- float LastTime=(float)GetCurrentTime();
- while(msg.message!=WM_QUIT)
- {
- if(PeekMessageW(&msg,0,0,0,PM_REMOVE))
- {
- TranslateMessage(&msg);
- DispatchMessageW(&msg);
- }
- else
- {
- float CurTime=(float)GetCurrentTime();
- float TimeDelta=(float)(CurTime-LastTime)*0.001f;
- ptr_display(TimeDelta);
- LastTime=CurTime;
- }
- }
- return msg.wParam;
- }
- bool Setup()
- {
- return true;
- }
- void Cleanup()
- {
-
- }
- bool Display(float TimeDelta)
- {
- if(Device)
- {
- Device->Clear(0,0,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,0x00000000,1.0f,0);
- Device->Present(0,0,0,0);
- }
- return true;
- }
- int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE prevInstance,PSTR cmdLine,int showCmd)
- {
- if(!Init(hInstance,640,480,true,D3DDEVTYPE_HAL,&Device))
- MessageBoxW(0,L"Init Failed",0,0);
- if(!Setup())MessageBoxW(0,L"Setup Failed",0,0);
- EnterMsgLoop(Display);
- Cleanup();
- Device->Release();
- return 0;
- }
点击Local Windows Debugger编译项目,运行结果如下:
至此,第一个Direct3D程序已经完工。