我的第一个DirectX程序界面及三角形的绘制

一下是我的第一个DirecX界面程序,

有点像MFC、和Win32的程序。

可以新建一个Win32 Application工程文件,命名为test

再新建一个cpp文件test.cpp

 

代码如下:#include <d3d9.h> #pragma comment(lib,"d3d9.lib") #pragma comment(lib,"d3dx9.lib") #pragma comment(lib,"winmm.lib") //D3D Object and D3D Device Object LPDIRECT3D9 g_pD3D = NULL; LPDIRECT3DDEVICE9 g_pD3DDevice = NULL; LRESULT CALLBACK MsgProc( HWND hwnd, // handle to window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM lParam // second message parameter ); HRESULT InitD3D(HWND hwnd); VOID Cleanup(); VOID Render(); // the entrance of program int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd ) { // register window class WNDCLASS wndclass; wndclass.style = 0; wndclass.lpfnWndProc = MsgProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; wndclass.hIcon = NULL; wndclass.hCursor = NULL; wndclass.hbrBackground = NULL; wndclass.lpszMenuName = NULL; wndclass.lpszClassName = "MyD3D"; RegisterClass(&wndclass); //Create the application's window HWND hwnd = CreateWindow("MyD3D","My first Direct3D Program",WS_OVERLAPPEDWINDOW,100,100,600,400, GetDesktopWindow(),NULL,wndclass.hInstance,NULL); if (SUCCEEDED(InitD3D(hwnd))) { //Show Window ShowWindow(hwnd,SW_SHOWDEFAULT); UpdateWindow(hwnd); MSG msg; while (GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } } // Cleanup(); UnregisterClass("MyD3D",wndclass.hInstance); return 0; } LRESULT CALLBACK MsgProc( HWND hwnd, // handle to window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM lParam // second message parameter ) { switch (uMsg) { case WM_DESTROY: PostQuitMessage(0); return 0; case WM_PAINT: Render(); ValidateRect(hwnd,NULL); return 0; } return DefWindowProc(hwnd,uMsg,wParam,lParam); } HRESULT InitD3D(HWND hwnd) { if (NULL == (g_pD3D = Direct3DCreate9(D3D_SDK_VERSION))) { return E_FAIL; } //display Mode D3DDISPLAYMODE d3ddm; if (FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&d3ddm))) { return E_FAIL; } // parameters of D3D D3DPRESENT_PARAMETERS d3dpp; ZeroMemory(&d3dpp,sizeof(d3dpp)); d3dpp.Windowed = true; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = d3ddm.Format; if (FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hwnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&g_pD3DDevice))) { return E_FAIL; } return S_OK; } VOID Cleanup() { if(g_pD3DDevice != NULL) g_pD3DDevice->Release(); if(g_pD3D != NULL) g_pD3D->Release(); } VOID Render() { if(NULL == g_pD3DDevice) return ; g_pD3DDevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,255),1.0f,0); g_pD3DDevice->BeginScene(); g_pD3DDevice->EndScene(); g_pD3DDevice->Present(NULL,NULL,NULL,NULL); }

 运行之后会弹出一个深蓝色的窗口。

 

 

下面是在上面代码的基础上,在界面绘制的三角形。

步骤如下:

1)创建顶点缓存

2)访问顶点缓存

3)绘制三角形

 

代码如下:

#include <d3d9.h> #pragma comment(lib,"d3d9.lib") #pragma comment(lib,"d3dx9.lib") #pragma comment(lib,"winmm.lib") //D3D Object and D3D Device Object LPDIRECT3D9 g_pD3D = NULL; LPDIRECT3DDEVICE9 g_pD3DDevice = NULL; /*******************************************/ LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL; struct CUSTOMVERTEX { float x,y,z,thw; DWORD color; }; #define D3DFVF_CUSTOMVERTEX D3DFVF_XYZRHW | D3DFVF_DIFFUSE /*******************************************/ LRESULT CALLBACK MsgProc( HWND hwnd, // handle to window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM lParam // second message parameter ); HRESULT InitD3D(HWND hwnd); VOID Cleanup(); VOID Render(); ////////////////////////////////// HRESULT InitVB(); //////////////////////////////// // the entrance of program int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd ) { // register window class WNDCLASS wndclass; wndclass.style = 0; wndclass.lpfnWndProc = MsgProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; wndclass.hIcon = NULL; wndclass.hCursor = NULL; wndclass.hbrBackground = NULL; wndclass.lpszMenuName = NULL; wndclass.lpszClassName = "MyD3D"; RegisterClass(&wndclass); //Create the application's window HWND hwnd = CreateWindow("MyD3D","My first Direct3D Program",WS_OVERLAPPEDWINDOW,100,100,600,400, GetDesktopWindow(),NULL,wndclass.hInstance,NULL); if (SUCCEEDED(InitD3D(hwnd))) { if(SUCCEEDED(InitVB())) { //Show Window ShowWindow(hwnd,SW_SHOWDEFAULT); UpdateWindow(hwnd); MSG msg; while (GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } } } // Cleanup(); UnregisterClass("MyD3D",wndclass.hInstance); return 0; } LRESULT CALLBACK MsgProc( HWND hwnd, // handle to window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM lParam // second message parameter ) { switch (uMsg) { case WM_DESTROY: PostQuitMessage(0); return 0; case WM_PAINT: Render(); ValidateRect(hwnd,NULL); return 0; } return DefWindowProc(hwnd,uMsg,wParam,lParam); } HRESULT InitD3D(HWND hwnd) { if (NULL == (g_pD3D = Direct3DCreate9(D3D_SDK_VERSION))) { return E_FAIL; } //display Mode D3DDISPLAYMODE d3ddm; if (FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&d3ddm))) { return E_FAIL; } // parameters of D3D D3DPRESENT_PARAMETERS d3dpp; ZeroMemory(&d3dpp,sizeof(d3dpp)); d3dpp.Windowed = true; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = d3ddm.Format; if (FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hwnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&g_pD3DDevice))) { return E_FAIL; } return S_OK; } VOID Cleanup() { if(g_pVB !=NULL) g_pVB->Release(); if(g_pD3DDevice != NULL) g_pD3DDevice->Release(); if(g_pD3D != NULL) g_pD3D->Release(); } VOID Render() { if(NULL == g_pD3DDevice) return ; g_pD3DDevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,255),1.0f,0); g_pD3DDevice->BeginScene(); //////////////////////// g_pD3DDevice->SetStreamSource(0,g_pVB,0,sizeof(CUSTOMVERTEX)); g_pD3DDevice->SetFVF(D3DFVF_CUSTOMVERTEX); g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,1); ////////////////////////// g_pD3DDevice->EndScene(); g_pD3DDevice->Present(NULL,NULL,NULL,NULL); } HRESULT InitVB() { CUSTOMVERTEX g_vertex[]={ {150.0f,50.0f,0.5f,1.0f,0xffff0000}, {250.0f,250.0f,0.5f,1.0f,0xff00ff00}, {50.0f,250.0f,0.5f,1.0f,0xff0000ff}}; if(FAILED(g_pD3DDevice->CreateVertexBuffer(3*sizeof(CUSTOMVERTEX),0, D3DFVF_CUSTOMVERTEX,D3DPOOL_DEFAULT,&g_pVB,NULL))) { return E_FAIL; } VOID *pvertex; if(FAILED(g_pVB->Lock(0,sizeof(g_vertex),(void **)&pvertex,0))) { return E_FAIL; } memcpy(pvertex,g_vertex,sizeof(g_vertex)); g_pVB->Unlock(); return S_OK; }

你可能感兴趣的:(null,application,Parameters,callback,Direct3D,winapi)