首页 > 代码库 > 简单封装 Delphi 的 DirectX类
简单封装 Delphi 的 DirectX类
[delphi] view plain copy
- var
- CreatorRenderer : TCreatorRenderer;
- Form1: TForm1;
窗体代码:
[delphi] view plain copy
- {$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;
// 下面是封装的渲染器类
[delphi] view plain copy
- // 渲染器类
- // 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;
- 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
简单封装 Delphi 的 DirectX类
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。