2.教大家怎么用DirectX来画线段
#include <d3d9.h>
#pragma comment (lib,"d3d9.lib")
#pragma comment (lib,"d3dx9.lib")
#define Window_Class "Draw Line DirectX Window"
#define Window_Name "draw line directx window"
LPDIRECT3D9 g_D3D = NULL;
LPDIRECT3DDEVICE9 g_D3DDevice = NULL;
LPDIRECT3DVERTEXBUFFER9 g_VertxBuffer = NULL;
bool InitalizeD3D(HWND hWnd,bool FullScreen);
bool InitalizeObject();
void RenderScene();
void Shutdown();
struct STVertxBuffer
{
float x, y, z, rhw;
unsigned long Color;
};
#define D3DFVF_VertxBuffer (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)
LRESULT WINAPI MsgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
switch(msg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
break;
case WM_KEYUP:
if(wParam == VK_ESCAPE)
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd,msg,wParam,lParam);
}
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE prevhInst,LPSTR cmdLine,int show)
{
WNDCLASSEX wc = {sizeof(WNDCLASSEX),CS_CLASSDC,MsgProc,0L,0L,GetModuleHandle(NULL),NULL,NULL,NULL,NULL,Window_Class,NULL};
RegisterClassEx(&wc);
HWND hWnd = CreateWindow(Window_Class,Window_Name,WS_OVERLAPPEDWINDOW,100,100,640,480,GetDesktopWindow(),NULL,wc.hInstance,NULL);
if(InitalizeD3D(hWnd,false))
{
ShowWindow(hWnd,SW_SHOWDEFAULT);
UpdateWindow(hWnd);
MSG msg;
ZeroMemory(&msg,sizeof(msg));
while(msg.message != WM_QUIT)
{
if(PeekMessage(&msg,hWnd,0U,0U,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
RenderScene();
}
}
}
Shutdown();
UnregisterClass(Window_Class,wc.hInstance);
return 0;
}
bool InitalizeD3D(HWND hWnd,bool FullScreen)
{
D3DDISPLAYMODE displayMode;
g_D3D = Direct3DCreate9(D3D_SDK_VERSION);
if(g_D3D == NULL) return false;
if(FAILED(g_D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&displayMode))) return false;
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp,sizeof(d3dpp));
if(FullScreen)
{
d3dpp.Windowed = FALSE;
d3dpp.BackBufferWidth = 640;
d3dpp.BackBufferHeight = 480;
}
else
{
d3dpp.Windowed = TRUE;
}
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = displayMode.Format;
if(FAILED(g_D3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&g_D3DDevice))) return false;
if(!InitalizeObject()) return false;
return true;
}
bool InitalizeObject()
{
unsigned long col = D3DCOLOR_XRGB(255,255,255);
STVertxBuffer ObjectVertxBuffer[] =
{
{420.0f, 100.0f, 0.5f, 1.0f, col},
{420.0f, 300.0f, 0.5f, 1.0f, col},
{220.0f, 100.0f, 0.5f, 1.0f, col},
{220.0f, 300.0f, 0.5f, 1.0f, col}
};
if(FAILED(g_D3DDevice->CreateVertexBuffer(sizeof(ObjectVertxBuffer),0,D3DFVF_VertxBuffer,D3DPOOL_DEFAULT,&g_VertxBuffer,NULL))) return false;
void * Ptr;