[delphi]
// 渲染器类
// warrially
unit xCreatorRenderer;
interface
uses Direct3D9, D3DX9, Windows;
type
TCreatorRenderer = class(TObject)
private
// D3D基本参数
m_d3dpp : D3DPRESENT_PARAMETERS;
private class var
m_pDevice : IDirect3DDevice9; //设备
m_pSprite : ID3DXSprite; //精灵
public
constructor Create;
destructor Destroy; override;
public
// 创建设备 (容器句柄, 宽, 高, 是否窗口化)
function CreateDevice(hWnd:HWND ; nWidth : Integer ; nHeight : Integer ; bWindowed : Boolean):Boolean;
// 开始渲染
procedure BeginRender();
// 结束渲染
procedure EndRender();
// 开始精灵渲染
procedure BeginRenderSprite();
// 结束精灵渲染
procedure EndRenderSprite();
// 获取设备指针
class function GetDevice():IDirect3DDevice9;
// 获取精灵指针
class function GetSprite():ID3DXSprite;
end;
implementation
{ TCreatorRenderer }
// 开始渲染
procedure TCreatorRenderer.BeginRender;
begin
m_pDevice.Clear(0, nil, D3DCLEAR_TARGET or D3DCLEAR_ZBUFFER, 0, 1.0, 0);
m_pDevice.BeginScene();
end;
procedure TCreatorRenderer.BeginRenderSprite;
begin
m_pSprite._Begin(D3DXSPRITE_ALPHABLEND);
end;
constructor TCreatorRenderer.Create;
begin
end;
function TCreatorRenderer.CreateDevice(hWnd: HWND; nWidth, nHeight: Integer;
bWindowed: Boolean): Boolean;
var
pD3d9 : IDirect3D9;
myhResult : HResult ;
begin
pD3d9 := Direct3DCreate9(D3D_SDK_VERSION);
if (pD3d9 = nil) then
begin
OutputDebugString(‘Direct3D create failed.‘);
Result := False;
Exit;
end;
m_d3dpp.BackBufferWidth := nWidth;
m_d3dpp.BackBufferHeight := nHeight;
m_d3dpp.BackBufferFormat := D3DFMT_A8R8G8B8;
m_d3dpp.BackBufferCount := 1;
m_d3dpp.MultiSampleType := D3DMULTISAMPLE_NONMASKABLE;
m_d3dpp.MultiSampleQuality := 0;
m_d3dpp.SwapEffect := D3DSWAPEFFECT_DISCARD;
m_d3dpp.hDeviceWindow := hWnd;
m_d3dpp.Windowed := bWindowed;
m_d3dpp.EnableAutoDepthStencil := true;
m_d3dpp.AutoDepthStencilFormat := D3DFMT_D24S8;