最简单的DX窗口程序
一、下载DX装起来:http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=6812
二、创建一个Win32窗口程序,空的,创建一个cpp文件,把项目属性配置一下,主要就是连接的静态库设置一下,要连接的静态库如下
d3d9.lib
d3dcompiler.lib --HLSL编译器
DxErr.lib --错误库
dxguid.lib --定义了Direct3D中所需要的GUID,即COM需要引用的
其它的,像DX的动态库、运行库、运行目录,在安装DX SDK的时候就已经设置好了,可以在选项里面看到
三、把下面的代码贴上去,编译运行,OK
#include <Windows.h> #include <d3d9.h> #include <d3dx9.h> IDirect3D9 *g_pD3D; // D3D对象 IDirect3DDevice9 *g_pd3dDevice; // D3D设备 LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); void Render(); void Cleanup(); INT WINAPI WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in_opt LPSTR lpCmdLine, __in int nShowCmd ) { // step 1. WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, "Direct3D Frame", NULL }; RegisterClassEx(&wc); HWND hWnd = CreateWindow( "Direct3D Frame", "Direct3D BaseFrame", WS_OVERLAPPEDWINDOW, 100, 100, 300, 300, GetDesktopWindow(), NULL, wc.hInstance, NULL); ShowWindow(hWnd, nShowCmd); // step 2. if (NULL == (g_pD3D = Direct3DCreate9(D3D_SDK_VERSION))) { return E_FAIL; } D3DPRESENT_PARAMETERS d3dpp; ZeroMemory(&d3dpp, sizeof(d3dpp)); d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; if (FAILED(g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice))); // step 3. MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } } LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_DESTROY: Cleanup(); PostQuitMessage(0); return 0; case WM_PAINT: Render(); ValidateRect(hWnd, NULL); return 0; } return DefWindowProc(hWnd, msg, wParam, lParam); } void Render() { g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0); g_pd3dDevice->BeginScene(); // rendering of scene objects happens here g_pd3dDevice->EndScene(); g_pd3dDevice->Present(NULL, NULL, NULL, NULL); } void Cleanup() { if (g_pd3dDevice != NULL) g_pd3dDevice ->Release(); if (g_pD3D != NULL) g_pD3D->Release(); }
熟悉Win32程序开发的一看就知道,在响应WM_PAINT消息的时候换成了调用Render()这个函数,在这个函数里面你就可以用DX"画"自己的东东了<^^>