Directx3d9——CreateDevice(原)

1.DirectX SDK 下载地址:http://www.microsoft.com/downloads/en/details.aspx?displaylang=en&FamilyID=3021d52b-514e-41d3-ad02-438a3ba730ba

2.安装SDK

3.以vs2010为例子,配置开发环境。

首先,创建win32工程,然后修改工程的属性。以下是工程的属性页。修改包含目录和库目录。

Directx3d9——CreateDevice(原)_第1张图片

  在链接->输入:

d3d9.lib
d3dx10d.lib
d3dx9d.lib
dxerr.lib
dxguid.lib
winmm.lib
comctl32.lib

Directx3d9——CreateDevice(原)_第2张图片

4.主要代码CreateDevice.cpp:

   1:  //-----------------------------------------------------------------------------
   2:  // File: CreateDevice.cpp
   3:  //
   4:  // Desc: This is the first tutorial for using Direct3D. In this tutorial, all
   5:  //       we are doing is creating a Direct3D device and using it to clear the
   6:  //       window.
   7:  //
   8:  // Copyright (c) Microsoft Corporation. All rights reserved.
   9:  //-----------------------------------------------------------------------------
  10:  #include <d3d9.h>
  11:  #pragma warning( disable : 4996 ) // 禁用过时警告
  12:  #include <strsafe.h>
  13:  #pragma warning( default : 4996 )
  14:   
  15:   
  16:   
  17:   
  18:  //-----------------------------------------------------------------------------
  19:  // 全局变量
  20:  //-----------------------------------------------------------------------------
  21:  LPDIRECT3D9         g_pD3D = NULL; // 用于创建D3D设备
  22:  LPDIRECT3DDEVICE9   g_pd3dDevice = NULL; // 我们的渲染设备
  23:   
  24:   
  25:   
  26:   
  27:  //-----------------------------------------------------------------------------
  28:  // Name: InitD3D()
  29:  // Desc: 初始化 Direct3D
  30:  //-----------------------------------------------------------------------------
  31:  HRESULT InitD3D( HWND hWnd )
  32:  {
  33:      // 创建D3D的对象,这是需要创建D3DDevice。
  34:      if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
  35:          return E_FAIL;
  36:   
  37:      // Set up the structure used to create the D3DDevice. Most parameters are
  38:      // zeroed out. We set Windowed to TRUE, since we want to do D3D in a
  39:      // window, and then set the SwapEffect to "discard", which is the most
  40:      // efficient method of presenting the back buffer to the display.  And 
  41:      // we request a back buffer format that matches the current desktop display 
  42:      // format.
  43:      /*ZeroMemory() ZeroMemory宏用0来填充一块内存区域。
  44:        为了避免优化编译器的意外的影响,请使用SecureZeroMemory函数。
  45:              void ZeroMemory(
  46:                PVOID Destination,
  47:                  SIZE_T Length
  48:                    );
  49:          参数:
  50:    Destination :指向一块准备用0来填充的内存区域的开始地址。
  51:    Length :准备用0来填充的内存区域的大小,按字节来计算。
  52:              返回值:无
  53:                ZeroMemory 只是将指定的内存块清零.
  54:                  使用结构前清零, 而不让结构的成员数值具有不确定性, 是一个好的编程习惯 
  55:      */
  56:   
  57:      D3DPRESENT_PARAMETERS d3dpp;
  58:      ZeroMemory( &d3dpp, sizeof( d3dpp ) );
  59:      d3dpp.Windowed = TRUE;    //不是全屏
  60:      d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  61:      d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
  62:   
  63:      //创建Direct3D 设备. 在这里,我们使用默认的适配器 (大多数的
  64:      // 系统只有一块显卡, 除非我们有多卡互联)
  65:      //  和需要硬件抽象层 (就像我们希望的是硬件设备而非软件
  66:      // 当我们想知道它在所有的卡上都可以使用的时,我们
  67:      //会使用软件顶点处理 。对于支持硬件顶点处理的显卡,会有性能上的较大提升
  68:   
  69:      if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
  70:                                        D3DCREATE_SOFTWARE_VERTEXPROCESSING,
  71:                                        &d3dpp, &g_pd3dDevice ) ) )
  72:      {
  73:          return E_FAIL;
  74:      }
  75:   
  76:      // 设备状态通常会设定在这里
  77:   
  78:      return S_OK;
  79:  }
  80:   
  81:   
  82:   
  83:   
  84:  //-----------------------------------------------------------------------------
  85:  // Name: Cleanup()
  86:  // Desc: 释放预先初始化的对象
  87:  //-----------------------------------------------------------------------------
  88:  VOID Cleanup()
  89:  {
  90:      if( g_pd3dDevice != NULL )
  91:          g_pd3dDevice->Release();
  92:   
  93:      if( g_pD3D != NULL )
  94:          g_pD3D->Release();
  95:  }
  96:   
  97:   
  98:   
  99:   
 100:  //-----------------------------------------------------------------------------
 101:  // Name: Render()
 102:  // Desc: 绘制场景
 103:  //-----------------------------------------------------------------------------
 104:  VOID Render()
 105:  {
 106:      if( NULL == g_pd3dDevice )
 107:          return;
 108:   
 109:      // 清除后台缓冲区为蓝色
 110:      g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 0, 0, 255 ), 1.0f, 0 );
 111:   
 112:      // 场景开始
 113:      if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
 114:      {
 115:          // Rendering of scene objects can happen here
 116:   
 117:          // 场景结束
 118:          g_pd3dDevice->EndScene();
 119:      }
 120:   
 121:      // Present the backbuffer contents to the display
 122:      g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
 123:  }
 124:   
 125:   
 126:   
 127:   
 128:  //-----------------------------------------------------------------------------
 129:  // Name: MsgProc()
 130:  // Desc: 窗体消息句柄
 131:  //-----------------------------------------------------------------------------
 132:  LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
 133:  {
 134:      switch( msg )
 135:      {
 136:          case WM_DESTROY:
 137:              Cleanup();
 138:              PostQuitMessage( 0 );
 139:              return 0;
 140:   
 141:          case WM_PAINT:
 142:              Render();
 143:              ValidateRect( hWnd, NULL );
 144:              return 0;
 145:      }
 146:   
 147:      return DefWindowProc( hWnd, msg, wParam, lParam );
 148:  }
 149:   
 150:   
 151:   
 152:   
 153:  //-----------------------------------------------------------------------------
 154:  // Name: wWinMain()
 155:  // Desc: 应用程序的入口
 156:  //-----------------------------------------------------------------------------
 157:  INT WINAPI wWinMain( HINSTANCE hInst, HINSTANCE, LPWSTR, INT )
 158:  {
 159:      UNREFERENCED_PARAMETER( hInst );
 160:   
 161:      // 注册窗体类
 162:      WNDCLASSEX wc =
 163:      {
 164:          sizeof( WNDCLASSEX ), CS_CLASSDC, MsgProc, 0L, 0L,
 165:          GetModuleHandle( NULL ), NULL, NULL, NULL, NULL,
 166:          L"D3D Tutorial", NULL
 167:      };
 168:      RegisterClassEx( &wc );
 169:   
 170:      // 创建应用程序窗口
 171:      HWND hWnd = CreateWindow( L"D3D Tutorial", L"D3D Tutorial 01: CreateDevice",
 172:                                WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,
 173:                                NULL, NULL, wc.hInstance, NULL );
 174:   
 175:      // 初始化 Direct3D
 176:      if( SUCCEEDED( InitD3D( hWnd ) ) )
 177:      {
 178:          // 显示窗体
 179:          ShowWindow( hWnd, SW_SHOWDEFAULT );
 180:          UpdateWindow( hWnd );
 181:   
 182:          // 进入消息循环
 183:          MSG msg;
 184:          while( GetMessage( &msg, NULL, 0, 0 ) )
 185:          {
 186:              TranslateMessage( &msg );
 187:              DispatchMessage( &msg );
 188:          }
 189:      }
 190:   
 191:      UnregisterClass( L"D3D Tutorial", wc.hInstance );
 192:      return 0;
 193:  }

然后,创建一个资源图标资源文件。

以上是完整的directx的helloworld!

你可能感兴趣的:(create)