DirectX游戏编程(一):创建一个Direct3D程序

一、环境

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,复制以下代码:

  1 #include <d3dx9.h>
  2 IDirect3DDevice9* Device=0;
  3 LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
  4 {
  5     switch(msg)
  6     {
  7     case WM_DESTROY:PostQuitMessage(0);break;
  8     case WM_KEYDOWN:
  9         if(wParam==VK_ESCAPE)DestroyWindow(hwnd);break;
 10     }
 11     return DefWindowProcW(hwnd,msg,wParam,lParam);
 12 }
 13 bool Init(HINSTANCE hInstance,int width,int height,bool windowed,D3DDEVTYPE devType,IDirect3DDevice9** device)
 14 {
 15     WNDCLASSW wc;
 16     wc.cbClsExtra=0;
 17     wc.cbWndExtra=0;
 18     wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
 19     wc.hCursor=LoadCursorW(0,IDC_ARROW);
 20     wc.hIcon=LoadIconW(0,IDI_APPLICATION);
 21     wc.hInstance=hInstance;
 22     wc.lpfnWndProc=(WNDPROC)WndProc;
 23     wc.lpszClassName=L"Direct3D";
 24     wc.lpszMenuName=0;
 25     wc.style=CS_HREDRAW|CS_VREDRAW;
 26     if(!RegisterClassW(&wc))
 27     {
 28         MessageBoxW(0,L"Register Failed",0,0);
 29         return false;
 30     }
 31 
 32     HWND hwnd=0;
 33     hwnd=CreateWindowW(L"Direct3D",L"Direct3D",WS_OVERLAPPEDWINDOW,0,0,width,height,0,0,hInstance,0);
 34     if(!hwnd)
 35     {
 36         MessageBoxW(0,L"CreateWindow Failed",0,0);
 37         return false;
 38     }
 39     ShowWindow(hwnd,SW_SHOW);
 40     UpdateWindow(hwnd);
 41 
 42     HRESULT hr;
 43     IDirect3D9* d3d9;
 44     d3d9=Direct3DCreate9(D3D_SDK_VERSION);
 45     
 46     int vp=0;
 47     D3DCAPS9 caps;
 48     d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT,devType,&caps);
 49     if(caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
 50         vp=D3DCREATE_HARDWARE_VERTEXPROCESSING;
 51     else vp=D3DCREATE_SOFTWARE_VERTEXPROCESSING;
 52 
 53     D3DPRESENT_PARAMETERS d3dapp;
 54     d3dapp.AutoDepthStencilFormat=D3DFMT_D24S8;
 55     d3dapp.BackBufferCount=1;
 56     d3dapp.BackBufferFormat=D3DFMT_A8R8G8B8;
 57     d3dapp.BackBufferHeight=height;
 58     d3dapp.BackBufferWidth=width;
 59     d3dapp.EnableAutoDepthStencil=true;
 60     d3dapp.Flags=0;
 61     d3dapp.FullScreen_RefreshRateInHz=D3DPRESENT_RATE_DEFAULT;
 62     d3dapp.hDeviceWindow=hwnd;
 63     d3dapp.MultiSampleQuality=0;
 64     d3dapp.MultiSampleType=D3DMULTISAMPLE_NONE;
 65     d3dapp.PresentationInterval=D3DPRESENT_INTERVAL_IMMEDIATE;
 66     d3dapp.SwapEffect=D3DSWAPEFFECT_DISCARD;
 67     d3dapp.Windowed=windowed;
 68     hr=d3d9->CreateDevice(D3DADAPTER_DEFAULT,devType,hwnd,vp,&d3dapp,device);
 69     if(FAILED(hr))
 70     {
 71         MessageBoxW(0,L"CreateDevice Failed",0,0);
 72         d3d9->Release();
 73         return false;
 74     }
 75     d3d9->Release();
 76     return true;
 77 }
 78 int EnterMsgLoop(bool (*ptr_display)(float TimeDelta))
 79 {
 80     MSG msg;
 81     ZeroMemory(&msg,sizeof(MSG));
 82     float LastTime=(float)GetCurrentTime();
 83     while(msg.message!=WM_QUIT)
 84     {
 85         if(PeekMessageW(&msg,0,0,0,PM_REMOVE))
 86         {
 87             TranslateMessage(&msg);
 88             DispatchMessageW(&msg);
 89         }
 90         else 
 91         {
 92             float CurTime=(float)GetCurrentTime();
 93             float TimeDelta=(float)(CurTime-LastTime)*0.001f;
 94             ptr_display(TimeDelta);
 95             LastTime=CurTime;
 96         }
 97     }
 98     return msg.wParam;
 99 }
100 bool Setup()
101 {
102     return true;
103 }
104 void Cleanup()
105 {
106 
107 }
108 bool Display(float TimeDelta)
109 {
110     if(Device)
111     {
112         Device->Clear(0,0,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,0x00000000,1.0f,0);
113         Device->Present(0,0,0,0);
114     }
115     return true;
116 }
117 int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE prevInstance,PSTR cmdLine,int showCmd)
118 {
119     if(!Init(hInstance,640,480,true,D3DDEVTYPE_HAL,&Device))
120         MessageBoxW(0,L"Init Failed",0,0);
121     if(!Setup())MessageBoxW(0,L"Setup Failed",0,0);
122     EnterMsgLoop(Display);
123     Cleanup();
124     Device->Release();
125     return 0;
126 }

 

点击Local Windows Debugger编译项目,运行结果如下:

至此,第一个Direct3D程序已经完工。

 

CSDN博客地址:http://blog.csdn.net/lfeng1420

posted @ 2014-03-06 22:24  LFENG  阅读(1035)  评论(0编辑  收藏  举报