//
// VC,DirectX8开发实例(画面的清除和描画)
// IDirect3DDevice8::Clear()函数使用
#include <windows.h>
#include <d3d8.h>
#define TITLE TEXT("Kitty on your lap")
IDirect3D8 * pDirect3D;
IDirect3DDevice8 * pD3Device;
D3DPRESENT_PARAMETERS d3dpp;
LRESULT CALLBACK WndProc(HWND hWnd , UINT msg , WPARAM wp , LPARAM lp) {
switch (msg) {
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_PAINT:
if (!pD3Device) break;
pD3Device->Clear(0 ,
NULL , //Clear全体
D3DCLEAR_TARGET ,//指定Clear对象,blankの色にする
D3DCOLOR_XRGB(0 , 0 , 0) ,//清楚后显示的颜色
1.0 , //Z值,0.0 ~ 1.0 までの深度バッファに保存する
0); //指定为各StencilBuffer的报名保存的整数价值,
//0 ~ 2n - 1 までの
//IDirect3DDevice8::Present()
pD3Device->Present(NULL,// 全体が対象となります
NULL,//
NULL,//
NULL);//
ValidateRect(hWnd , NULL);
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;
}
本章我想在设备里(上)进行绘画。比起前几章要难,因为加入了Z坐标系。
//
//IDirect3DDevice8::Reset()方法使用
//
#include <windows.h>
#include <d3d8.h>
#define TITLE TEXT("Kitty on your lap")
IDirect3D8 * pDirect3D;
IDirect3DDevice8 * pD3Device;
D3DPRESENT_PARAMETERS d3dpp;
LRESULT CALLBACK WndProc(HWND hWnd , UINT msg , WPARAM wp , LPARAM lp) {
switch (msg) {
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_PAINT:
if (!pD3Device) break;
pD3Device->Clear(0 , NULL , D3DCLEAR_TARGET ,
D3DCOLOR_XRGB(0 , 0 , 0) , 1.0 , 0);
pD3Device->Present(NULL,NULL,NULL,NULL);
ValidateRect(hWnd , NULL);
return 0;
case WM_SIZE://如果窗体大小发生变化,执行此事件
if (!pD3Device) return 0;
d3dpp.BackBufferWidth = LOWORD(lp);
d3dpp.BackBufferHeight = HIWORD(lp);
pD3Device->Reset(&d3dpp);// 表示新的设备的状态
InvalidateRect(hWnd , NULL , TRUE);
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;
}