- var
- CreatorRenderer : TCreatorRenderer;
- Form1: TForm1;
窗体代码:
- {$R *.dfm}
-
- procedure TForm1.FormCreate(Sender: TObject);
- begin
-
- CreatorRenderer := TCreatorRenderer.Create();
- CreatorRenderer.CreateDevice(self.Handle,self.Width,Self.Height ,true);
-
-
- Timer1.Enabled := True;
- end;
-
- procedure TForm1.FormDestroy(Sender: TObject);
- begin
- CreatorRenderer.Free;
- end;
-
- procedure TForm1.Timer1Timer(Sender: TObject);
- begin
- CreatorRenderer.BeginRender();
- CreatorRenderer.EndRender();
- end;
// 下面是封装的渲染器类
- unit xCreatorRenderer;
-
- interface
- uses Direct3D9, D3DX9, Windows;
- type
- TCreatorRenderer = class(TObject)
- private
-
- 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
-
- 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;
- m_d3dpp.Flags := 0;
- m_d3dpp.FullScreen_RefreshRateInHz := D3DPRESENT_RATE_DEFAULT;
- m_d3dpp.PresentationInterval := D3DPRESENT_INTERVAL_IMMEDIATE;
-
- myhResult := pD3d9.CreateDevice( D3DADAPTER_DEFAULT,
- D3DDEVTYPE_HAL,
- hWnd,
- D3DCREATE_HARDWARE_VERTEXPROCESSING,
- @m_d3dpp,
- m_pDevice);
-
- if (FAILED(myhResult)) then
- begin
- OutputDebugString('D3D device create failed.');
- Result := false;
- Exit;
- end;
-
- myhResult := D3DXCreateSprite(m_pDevice, m_pSprite);
- if (FAILED(myhResult)) then
- begin
- OutputDebugString('D3D sprite create failed.');
- Result := false;
- Exit;
- end;
-
- OutputDebugString('Renderer create succeed.');
- Result := True;
-
-
- end;
-
- destructor TCreatorRenderer.Destroy;
- begin
-
- inherited;
- end;
-
- procedure TCreatorRenderer.EndRender;
- begin
- m_pDevice.EndScene();
- m_pDevice.Present(nil, nil, 0, nil);
- end;
-
- procedure TCreatorRenderer.EndRenderSprite;
- begin
- m_pSprite._End();
- end;
-
- class function TCreatorRenderer.GetDevice: IDirect3DDevice9;
- begin
- Result := m_pDevice;
- end;
-
- class function TCreatorRenderer.GetSprite: ID3DXSprite;
- begin
- Result := m_pSprite;
- end;
-
-
- end.
-
http://blog.csdn.net/warrially/article/details/7938672
|
请发表评论