// VC,DirectX8开发实例(坐标和行列(平行移动))
// http://wisdom.sakura.ne.jp/system/directx/dxg7.html
#include <windows.h>
#include <d3d8.h>
#include <D3dx8math.h>
#define TITLE TEXT("Kitty on your lap")
IDirect3D8 * pDirect3D;
IDirect3DDevice8 * pD3Device;
D3DPRESENT_PARAMETERS d3dpp;
typedef struct {
float x , y , z , rhw;
DWORD diff;
} D3DVERTEX;
LRESULT CALLBACK WndProc(HWND hWnd , UINT msg , WPARAM wp , LPARAM lp) {
D3DXMATRIX d3dm;
static D3DVERTEX pt[3] = {
{200 , 10 , 1 , 1 , 0xFFFF0000} ,
{400 , 200 , 1 , 1 , 0xFF0000FF} ,
{10 , 200 , 1 , 1 , 0xFF0000FF}
};
switch (msg) {
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_CREATE:
SetTimer(hWnd , 1 , 100 , NULL);
return 0;
case WM_PAINT:
if (!pD3Device) break;
pD3Device->Clear(0 , NULL , D3DCLEAR_TARGET ,
D3DCOLOR_XRGB(0xFF , 0xFF , 0xFF) , 1.0 , 0);
pD3Device->BeginScene();
pD3Device->SetVertexShader(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
pD3Device->DrawPrimitiveUP(
D3DPT_TRIANGLELIST, 1 , pt , sizeof (D3DVERTEX)
);
pD3Device->EndScene();
pD3Device->Present(NULL,NULL,NULL,NULL);
ValidateRect(hWnd , NULL);
return 0;
case WM_TIMER:
//进行平行移动用
D3DXMatrixTranslation(&d3dm , //指定对构造体的指针
1.0f , //X
0 ,//Y
0);//Z
pt[0].x = pt[0].x * d3dm.m[0][0] +
pt[0].x * d3dm.m[1][0] +
pt[0].x * d3dm.m[2][0] +
d3dm.m[3][0];
InvalidateRect(hWnd , NULL , TRUE);
return 0;
case WM_SIZE:
if (!pD3Device) return 0;
d3dpp.BackBufferWidth = LOWORD(lp);
d3dpp.BackBufferHeight = HIWORD(lp);
pD3Device->Reset(&d3dpp);
return 0;
}
return DefWindowProc(hWnd , msg , wp , lp);
}
int WINAPI WinMain(HINSTANCE hInstance , HINSTANCE hPrevInstance ,
PSTR lpCmdLine , int nCmdShow) {
MSG msg;
HWND hWnd;
WNDCLASS winc;
D3DDISPLAYMODE d3ddm;
pDirect3D = Direct3DCreate8(D3D_SDK_VERSION);
pDirect3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT , &d3ddm);
ZeroMemory(&d3dpp , sizeof (D3DPRESENT_PARAMETERS));
d3dpp.BackBufferFormat = d3ddm.Format;
d3dpp.BackBufferCount = 1;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.Windowed = TRUE;
winc.style = CS_HREDRAW | CS_VREDRAW;
winc.lpfnWndProc = WndProc;
winc.cbClsExtra = winc.cbWndExtra = 0;
winc.hInstance = hInstance;
winc.hIcon = LoadIcon(NULL , IDI_APPLICATION);
winc.hCursor = LoadCursor(NULL , IDC_ARROW);
winc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
winc.lpszMenuName = NULL;
winc.lpszClassName = TEXT("KITTY");
if (!RegisterClass(&winc)) return 0;
hWnd = CreateWindow(
TEXT("KITTY") , TITLE , WS_OVERLAPPEDWINDOW | WS_VISIBLE ,
CW_USEDEFAULT , CW_USEDEFAULT , CW_USEDEFAULT , CW_USEDEFAULT ,
NULL , NULL , hInstance , NULL
);
if (!hWnd) return 0;
pDirect3D->CreateDevice(
D3DADAPTER_DEFAULT , D3DDEVTYPE_HAL , hWnd ,
D3DCREATE_SOFTWARE_VERTEXPROCESSING , &d3dpp , &pD3Device
);
while (GetMessage(&msg , NULL , 0 , 0 )) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
pDirect3D->Release();
pD3Device->Release();
return msg.wParam;
}