Delphi 与 DirectX 之 DelphiX(3): 初识 TDXDraw


在 DelphiX 中, 所能看到的一般都是画在 TDXDraw.

TDXDraw 默认两个绘图表面: TDXDraw.Surface 和 TDXDraw.Primary;
一般先在后台(Surface)绘图, 然后再用 TDXDraw.Flip 命令切换前后台, 从而看到绘制效果.

TDXDraw.Flip 会置换 Surface 和 Primary 两个对象, 所以我们尽可以只在 Surface 上绘图;
由于 DirectX 的工作机制, 让这个切换速度异常地快(只是切换一个指针), 从而可以完成非常流畅的动画、游戏.
从 TDXDraw 的绘图表面也可以获取 Canvas 对象, 因而在 TDXDraw 中也可以使用 GDI/GDI+ 的绘图命令;
不过把 GDI/GDI+ 和 DirectX 一起使用应该会让 DirectX 的性能缩水, 但有这种可能总是好的, 起码比单独使用 GDI/GDI+ 要好.

下面例子中就使用了 DXDraw1.Surface.Canvas 对象;
使用完 DXDraw1.Surface.Canvas 后, 一定要释放(DXDraw1.Surface.Canvas.Release);

另外, 绘图前经常要先用指定的颜色填充表面, 譬如: DXDraw1.Surface.Fill(颜色值);
这里的颜色格式和 Delphi 的不一样, 和 HTML 使用的颜色值的顺序是一样的, 譬如: $FF0000 表示红色.
本例效果图:

Delphi 与 DirectX 之 DelphiX(3): 初识 TDXDraw
代码文件:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    DXDraw1: TDXDraw;
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  str: string;
begin
  str := FormatDateTime('h:n:s:zz', Time); {获取当前时间, 精确到毫秒}
  DXDraw1.Surface.Fill($FF0000); {填充为红色, 注意这个颜色格式是和 HTML 的颜色顺序一样的}
  DXDraw1.Surface.Canvas.Brush.Style := bsClear;
  DXDraw1.Surface.Canvas.Font.Color := clYellow;
  DXDraw1.Surface.Canvas.Font.Size := 16;
  DXDraw1.Surface.Canvas.TextOut(10, 10, str);
  DXDraw1.Surface.Canvas.Release; {释放 Canvas 对象}
  DXDraw1.Flip;
end;

{这和上面的功能一样, 只是简化了写法}
procedure TForm1.Button2Click(Sender: TObject);
var
  str: string;
begin
  str := FormatDateTime('h:n:s:zz', Time);
  DXDraw1.Surface.Fill(0); {填充黑色}
  with DXDraw1.Surface.Canvas do begin
    Brush.Style := bsClear;
    Font.Color := clLime;
    Font.Size := 16;
    TextOut(10, 10, str);
    Release; {释放 Canvas 对象}
  end;
  DXDraw1.Flip;
end;

end.

 
 
 
 
 

 

 
  

窗体文件:
object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 127
  ClientWidth = 201
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object DXDraw1: TDXDraw
    Left = 8
    Top = 8
    Width = 185
    Height = 81
    AutoInitialize = True
    AutoSize = True
    Color = clBlack
    Display.FixedBitCount = False
    Display.FixedRatio = True
    Display.FixedSize = True
    Options = [doAllowReboot, doWaitVBlank, doCenter, do3D, doDirectX7Mode, doHardware, doSelectDriver]
    SurfaceHeight = 81
    SurfaceWidth = 185
    TabOrder = 0
    Traces = <>
  end
  object Button1: TButton
    Left = 30
    Top = 95
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 1
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 118
    Top = 95
    Width = 75
    Height = 25
    Caption = 'Button2'
    TabOrder = 2
    OnClick = Button2Click
  end
end

 
 
 
 
 

 

 
  

你可能感兴趣的:(Delphi 与 DirectX 之 DelphiX(3): 初识 TDXDraw)