VS2008 direct9.0 开发平台搭建————第一个D3D程序

来源:互联网 发布:乐视2用不了移动数据 编辑:程序博客网 时间:2024/05/22 12:27

                                          VS2008 direct9.0 开发平台搭建————第一个D3D程序

一.VS2008中DirectX 9.0的配置方法

    1.  在VS2008里面选择: ”工具”-->”选项”-->”项目和解决方案”-->”vc++目录”

   2. 在”显示以下内容的目录”下的下拉框中选择"包含文件"中插入新行输入

       选择“E:/Include”

   3.在”显示以下内容的目录”下的下拉框中选择”库文件”插入新行并输入
      ”E:/Lib/x86”
    (  没有双引号,具体看你的DirectX安装目录)

二.测试程序:

1.  平台:windows XP  direct9.0  VS2008
2.   打开 vs2008 选择新建项目 ->win32项目 ,输入项目名,选择空项目。
向“源文件”添加文件main.cpp
输入以下代码:

Code:
  1. #include<d3d9.h>   
  2.   
  3. #include <tchar.h>   
  4.   
  5. #pragma comment(lib, "d3d9.lib")   
  6. #pragma comment(lib, "d3dx9.lib")   
  7.   
  8. #define WINDOW_CLASS _T("UGPDX")   
  9. #define WINDOW_NAME  _T("Blank D3D Window")   
  10.   
  11.   
  12. // Function Prototypes...   
  13. bool InitializeD3D(HWND hWnd, bool fullscreen);   
  14. void RenderScene();   
  15. void Shutdown();   
  16.   
  17.   
  18. // Direct3D object and device.   
  19. LPDIRECT3D9 g_D3D = NULL;   
  20. LPDIRECT3DDEVICE9 g_D3DDevice = NULL;   
  21.   
  22.   
  23. LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)   
  24. {   
  25.     switch(msg)   
  26.     {   
  27.     case WM_DESTROY:   
  28.         PostQuitMessage(0);   
  29.         return 0;   
  30.         break;   
  31.   
  32.     case WM_KEYUP:   
  33.         if(wParam == VK_ESCAPE) PostQuitMessage(0);   
  34.         break;   
  35.     }   
  36.   
  37.     return DefWindowProc(hWnd, msg, wParam, lParam);   
  38. }   
  39.   
  40.   
  41. int WINAPI WinMain(HINSTANCE hInst, HINSTANCE prevhInst, LPSTR cmdLine, int show)   
  42. {   
  43.     // Register the window class   
  44.     WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,   
  45.         GetModuleHandle(NULL), NULL, NULL, NULL, NULL,   
  46.         WINDOW_CLASS, NULL };   
  47.     RegisterClassEx(&wc);   
  48.   
  49.     // Create the application's window   
  50.     HWND hWnd = CreateWindow(WINDOW_CLASS, WINDOW_NAME, WS_OVERLAPPEDWINDOW,   
  51.         100, 100, 640, 480, GetDesktopWindow(), NULL,   
  52.         wc.hInstance, NULL);   
  53.   
  54.     // Initialize Direct3D   
  55.     if(InitializeD3D(hWnd, false))   
  56.     {   
  57.         // Show the window   
  58.         ShowWindow(hWnd, SW_SHOWDEFAULT);   
  59.         UpdateWindow(hWnd);   
  60.   
  61.         // Enter the message loop   
  62.         MSG msg;   
  63.         ZeroMemory(&msg, sizeof(msg));   
  64.   
  65.         while(msg.message != WM_QUIT)   
  66.         {   
  67.             if(PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))   
  68.             {   
  69.                 TranslateMessage(&msg);   
  70.                 DispatchMessage(&msg);   
  71.             }   
  72.             else  
  73.                 RenderScene();   
  74.         }   
  75.     }   
  76.   
  77.     // Release any and all resources.   
  78.     Shutdown();   
  79.   
  80.     // Unregister our window.   
  81.     UnregisterClass(WINDOW_CLASS, wc.hInstance);   
  82.     return 0;   
  83. }   
  84.   
  85.   
  86. bool InitializeD3D(HWND hWnd, bool fullscreen)   
  87. {   
  88.     D3DDISPLAYMODE displayMode;   
  89.   
  90.     // Create the D3D object.   
  91.     g_D3D = Direct3DCreate9(D3D_SDK_VERSION);   
  92.     if(g_D3D == NULL) return false;   
  93.   
  94.     // Get the desktop display mode.   
  95.     if(FAILED(g_D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &displayMode)))   
  96.         return false;   
  97.   
  98.     // Set up the structure used to create the D3DDevice   
  99.     D3DPRESENT_PARAMETERS d3dpp;   
  100.     ZeroMemory(&d3dpp, sizeof(d3dpp));   
  101.   
  102.     if(fullscreen)   
  103.     {   
  104.         d3dpp.Windowed = FALSE;   
  105.         d3dpp.BackBufferWidth = 640;   
  106.         d3dpp.BackBufferHeight = 480;   
  107.     }   
  108.     else  
  109.         d3dpp.Windowed = TRUE;   
  110.     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;   
  111.     d3dpp.BackBufferFormat = displayMode.Format;   
  112.   
  113.     // Create the D3DDevice   
  114.     if(FAILED(g_D3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,   
  115.         D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_D3DDevice)))   
  116.     {   
  117.         return false;   
  118.     }   
  119.   
  120.     return true;   
  121. }   
  122.   
  123.   
  124. void RenderScene()   
  125. {   
  126.     // Clear the backbuffer.   
  127.     g_D3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);   
  128.   
  129.     // Begin the scene.  Start rendering.   
  130.     g_D3DDevice->BeginScene();   
  131.   
  132.     // End the scene.  Stop rendering.   
  133.     g_D3DDevice->EndScene();   
  134.   
  135.     // Display the scene.   
  136.     g_D3DDevice->Present(NULL, NULL, NULL, NULL);   
  137. }   
  138.   
  139.   
  140. void Shutdown()   
  141. {   
  142.     if(g_D3DDevice != NULL) g_D3DDevice->Release();   
  143.     if(g_D3D != NULL) g_D3D->Release();   
  144.   
  145.     g_D3DDevice = NULL;   
  146.     g_D3D = NULL;   
  147. }   


安装错误解决:

问题1..
 d3d002/d3d002/main.cpp(46) : error C3861: “_T”: 找不到标识符
解决方法:加载 头文件:#include <tchar.h>

原创粉丝点击