纠结了两天了 dx环境配置及运行一个小例子 终于解决了
新建一个 win32项目 选择 “空项目”(如果给建成win32控制台的项目了 那就要更改一些其他的属性 最后说)
点击项目->属性->VC++目录 添加包含目录和库目录
$(DXSDK_DIR)Include
$(DXSDK_DIR)Lib\x86
(添加的时候 不要忘了加英文标点符号;
例如 原来是
$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSdkDir)include;$(FrameworkSDKDir)\include
在这个的最后添加 ;$(DXSDK_DIR)Include )
然后再 点击连接器->输入 在附加依赖项上添加 需要的.lib
d3dx9.lib;d3d9.lib;dinput8.lib;dxguid.lib;winmm.lib;
到这就配置完了
下面是在 http://www.mysjtu.com/page/M0/S655/655240.html 上找的一个dx 的例子。
1 #include <Windows.h> 2 #include <d3dx9.h> 3 #include <MMSystem.h> 4 LPDIRECT3D9 g_pD3D; 5 LPDIRECT3DDEVICE9 g_pd3dDevice; 6 LPDIRECT3DVERTEXBUFFER9 g_pVB; 7 struct CUSTOMVERTEX{ 8 FLOAT x, y, z; 9 DWORD color; 10 }; 11 #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE) 12 HRESULT InitObject() 13 { 14 CUSTOMVERTEX triangle[] = 15 { 16 { -1.0f,-1.0f, 0.0f, 0xffff0000, }, 17 { 1.0f,-1.0f, 0.0f, 0xff0000ff, }, 18 { 0.0f, 1.0f, 0.0f, 0xffffffff, } 19 }; 20 if (FAILED(g_pd3dDevice->CreateVertexBuffer(3*sizeof(CUSTOMVERTEX),0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVB, NULL))) 21 return E_FAIL; 22 VOID* pVertices; 23 if (FAILED(g_pVB->Lock(0,sizeof(triangle), &pVertices, 0))) 24 return E_FAIL; 25 memcpy(pVertices, triangle, sizeof(triangle)); 26 g_pVB->Unlock(); 27 return S_OK; 28 } 29 HRESULT InitD3D(HWND hWnd) 30 { 31 g_pD3D = Direct3DCreate9(D3D_SDK_VERSION); 32 if (NULL == g_pD3D) 33 return E_FAIL; 34 D3DPRESENT_PARAMETERS d3dpp; 35 ZeroMemory(&d3dpp, sizeof(d3dpp)); 36 d3dpp.Windowed = TRUE; 37 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; 38 d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; 39 if(FAILED( g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, 40 hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, 41 &d3dpp, &g_pd3dDevice))) 42 { 43 return E_FAIL; 44 } 45 g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); 46 // Turn off D3D lighting, since we are providing our own vertex colors 47 g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE ); 48 if (FAILED(InitObject())) 49 return E_FAIL; 50 return S_OK; 51 } 52 void SetupMatrices() 53 { 54 D3DXMATRIX matWorld; 55 UINT iTime = timeGetTime() % 1000; 56 FLOAT fAngle = iTime * ( 2.0f * D3DX_PI ) / 1000.0f; 57 D3DXMatrixRotationY(&matWorld, fAngle); 58 g_pd3dDevice->SetTransform(D3DTS_WORLD, &matWorld); 59 D3DXVECTOR3 vEyePt(0.0f, 3.0f, -5.0f); 60 D3DXVECTOR3 vLookAtPt(0.0f, 0.0f, 0.0f); 61 D3DXVECTOR3 vUp(0.0f, 1.0f, 0.0f); 62 D3DXMATRIXA16 matView; 63 D3DXMatrixLookAtLH(&matView, &vEyePt, &vLookAtPt, &vUp); 64 g_pd3dDevice->SetTransform(D3DTS_VIEW, &matView); 65 D3DXMATRIX matPoj; 66 D3DXMatrixPerspectiveFovLH(&matPoj, D3DX_PI/4, 1.0f, 1.0f, 100.0f); 67 g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &matPoj); 68 } 69 void Render() 70 { 71 g_pd3dDevice->Clear(0,NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 255), 1.0f, 0); 72 g_pd3dDevice->BeginScene(); 73 SetupMatrices(); 74 g_pd3dDevice->SetStreamSource(0, g_pVB, 0, sizeof(CUSTOMVERTEX)); 75 g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX); 76 g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1); 77 g_pd3dDevice->EndScene(); 78 g_pd3dDevice->Present(NULL, NULL, NULL, NULL); 79 } 80 void Cleanup() 81 { 82 if (g_pd3dDevice) 83 g_pd3dDevice->Release(); 84 if (g_pD3D) 85 g_pD3D->Release(); 86 if (g_pVB) 87 g_pVB->Release(); 88 } 89 LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) 90 { 91 switch (msg) 92 { 93 case WM_DESTROY: 94 PostQuitMessage(0); 95 return 0; 96 case WM_PAINT: 97 ValidateRect(hWnd, NULL); 98 return 0; 99 } 100 return DefWindowProc(hWnd, msg, wParam, lParam); 101 } 102 INT WINAPI WinMain(__in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in_opt LPSTR lpCmdLine, __in int nShowCmd ) 103 { 104 WNDCLASSEX wc = {sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0, 0, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, L"Direct3D", NULL}; 105 RegisterClassEx(&wc); 106 // Create the application window 107 HWND hWnd = CreateWindow(L"Direct3D", 108 L"Learn", WS_OVERLAPPEDWINDOW, 109 100, 100, 300, 300, 110 GetDesktopWindow(), 111 NULL, wc.hInstance, NULL); 112 // 113 ShowWindow(hWnd, SW_SHOW); 114 if (SUCCEEDED(InitD3D(hWnd))) 115 { 116 ShowWindow(hWnd, SW_SHOWDEFAULT); 117 UpdateWindow(hWnd); 118 MSG msg; 119 ZeroMemory(&msg, sizeof(msg)); 120 while (msg.message != WM_QUIT) 121 { 122 if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 123 { 124 TranslateMessage(&msg); 125 DispatchMessage(&msg); 126 } 127 else 128 Render(); 129 } 130 } 131 UnregisterClass( L"Direct3D", wc.hInstance ); 132 Cleanup(); 133 return nShowCmd; 134 }
然后点击调试(F5)就OK了 下面是例子的截图
/**********************************************************/
如果给建成win32控制台的项目了 那就要更改一些其他的属性
这样 代码就可正常运行了 而不会报错:
error LNK2019: 无法解析的外部符号 _main,该符号在函数 ___tmainCRTStartup 中被引用
fatal error LNK1120: 1 个无法解析的外部命令
终于运行出来啦,希望可以帮到和我一样困惑的