Delphi,在DelphiXE下使用DirectX的初步摸索

昨天刚安装了 DelphiXE , Delphi2010以上的版本自带了 DirectX 的SDK。

不多说了,先上代码

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Direct3D8, StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Device : IDirect3DDevice8;
  pd3d   : IDirect3D8;
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  parms : D3DPRESENT_PARAMETERS ;
  hr : HRESULT ;
begin
  memo1.Clear;
  pd3d := Direct3DCreate8 ( D3D_SDK_VERSION ) ;

  parms.BackBufferWidth := 0 ;
  parms.BackBufferHeight := 0 ;
  parms.BackBufferFormat := D3DFMT_A8R8G8B8 ;
  parms.BackBufferCount := 1 ;
  parms.MultiSampleType := D3DMULTISAMPLE_NONE ;
  parms.SwapEffect := D3DSWAPEFFECT_COPY ;
  parms.Windowed := TRUE ;
  parms.hDeviceWindow := Self.Handle ;
  parms.EnableAutoDepthStencil := FALSE ;
  parms.AutoDepthStencilFormat := D3DFMT_UNKNOWN ;
  parms.Flags := 0 ;
  parms.FullScreen_RefreshRateInHz := 0 ;
  parms.FullScreen_PresentationInterval := D3DPRESENT_INTERVAL_DEFAULT ;

  hr := pd3d.CreateDevice( D3DADAPTER_DEFAULT , D3DDEVTYPE_REF , Self.Handle , D3DCREATE_SOFTWARE_VERTEXPROCESSING , parms , Device ) ;
  if ( FAILED ( hr ) )  then
  begin
    Memo1.Lines.Add('不能创建 HAL 设备!') ;
    hr := pd3d.CreateDevice( D3DADAPTER_DEFAULT , D3DDEVTYPE_REF , Self.Handle , D3DCREATE_SOFTWARE_VERTEXPROCESSING , parms , Device ) ;
    if ( FAILED ( hr ) ) then
    begin
      Memo1.Lines.Add('不能创建 REF 设备!') ;
      Self.Close ;
    end
    else
    begin
      Memo1.Lines.Add('成功创建 REF 设备') ;
    end;
  end
  else
  begin
    Memo1.Lines.Add ('成功创建 HAL 设备') ;
  end;
end;



procedure TForm1.FormDestroy(Sender: TObject);
begin
  if Device <> nil then      //用完要记得放掉
    Device := nil;
  if pd3d <> nil then
    pd3d := nil;
end;

end.
//窗体文件
object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 282
  ClientWidth = 418
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  OnDestroy = FormDestroy
  PixelsPerInch = 96
  TextHeight = 13
  object Memo1: TMemo
    Left = 16
    Top = 24
    Width = 377
    Height = 250
    Lines.Strings = (
     '')
    TabOrder = 0
  end
end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值