本文示例源代碼或素材下載
DirectX, 微軟很久的技術了; 從 Windows Vista 開始, DirectX 已經是微軟操作系統的界面基礎了.
在 Delphi 下使用 DirectX 比較流行的有下面四種方式:
DelphiX
DSPack
Asphyre (?)
Delphi DirectX
DelphiX 是最早的(十年了), 也是最簡單的, 也是和 Delphi 結合最密切的;
但為了入手簡單, 准備從 DelphiX 開始, 先有個宏觀概念, 同時也學習一下 DelphiX 構造手法;
不過, 我想最終使用的應該是: Delphi DirectX.
DelphiX 從 2000.07.17 就沒在更新過, 不過另有熱心的支持者一直維護著, 甚至讓它支持了 Delphi 2009.
我現在使用的版本是從這裡下載的: http://www.micrel.cz/Dx/
使用了它的自動安裝文件: http://www.micrel.cz/Dx/download/install.exe
盡管介紹是支持 Delphi 2009, 我還是發現了很多問題; 經過修正最後在 2009 下能用了.
但很多例子並不支持 2009, 因為在 2007 下要穩定得多, 所以選擇在 Delphi 2007 下學習 DelphiX; 同時爭取讓代碼兼容 2009.
為了保證運行所有的例子, 還需要把 DelphiXcfg.inc 的倒數第二行的 {.$Define D3DRM} 改為 {$Define D3DRM}
另外, 因為有些日文的注釋亂碼, 也會讓有些例子運行不了, 修改下亂碼的地方即可.
目前我抽樣測試了一些, 都沒問題了.
總感覺學晚了, 爭取盡快超越它!
先從網上學了個例子, 作為開盤測試吧:
本例效果圖:
代碼文件:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DXDraws, StdCtrls, DXClass;
type
TForm1 = class(TDXForm)
DXDraw1: TDXDraw;
DXTimer1: TDXTimer;
DXImageList1: TDXImageList;
procedure FormCreate(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure DXDraw1Initialize(Sender: TObject);
procedure DXDraw1Finalize(Sender: TObject);
procedure DXTimer1Timer(Sender: TObject; LagCount: Integer);
private
procedure CalculateTables;
procedure PlotPoint(XCenter, YCenter, Radius, Angle: Word);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
var
SineMove: array[0..255] of integer;
CosineMove: array[0..255] of integer;
SineTable: array[0..449] of integer;
CenterX, CenterY: Integer;
x: Word = 0;
y: Word = 0;
procedure TForm1.CalculateTables;
var
wCount: Word;
begin
for wCount := 0 to 255 do
begin
SineMove[wCount] := round( sin( pi*wCount/128 ) * 45 );
CosineMove[wCount] := round( cos( pi*wCount/128 ) * 60 );
end;
for wCount := 0 to 449 do
begin
SineTable[wCount] := round( sin( pi*wCount/180 ) * 128);
end;
end;
procedure TForm1.PlotPoint(XCenter, YCenter, Radius, Angle: Word);
var
a, b : Word;
begin
a := ( Radius * SineTable[90 + Angle]);
asm
sar a,7
end;
a := CenterX + XCenter + a;
b := ( Radius * SineTable[Angle] );
asm
sar b,7
end;
b := CenterY + YCenter + b;
if (a < Width ) and ( b < Height ) then
begin
DXImageList1.Items[0].Draw(DXDraw1.Surface, a, b, 0 );
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
DXDraw1.Align := alClIEnt;
CenterX := Width div 2;
CenterY := Height div 2;
CalculateTables;
end;
procedure TForm1.DXDraw1Finalize(Sender: TObject);
begin
DXTimer1.Enabled := False;
end;
procedure TForm1.DXDraw1Initialize(Sender: TObject);
begin
DXTimer1.Enabled := True;
end;
procedure TForm1.DXTimer1Timer(Sender: TObject; LagCount: Integer);
const
IncAngle = 12;
XMove = 7;
YMove = 8;
var
CountAngle : Word;
CountLong : Word;
IncLong :Word;
begin
if not DXDraw1.CanDraw then exit;
IncLong := 2;
CountLong := 20;
DXDraw1.Surface.Fill(0);
repeat
CountAngle := 0;
repeat
PlotPoint(CosineMove[(x + (200 - CountLong)) mod 255],
SineMove[(y + (200 - CountLong)) mod 255], CountLong, CountAngle);
Inc(CountAngle, IncAngle);
until CountAngle >= 360;
inc(CountLong, IncLong);
if (CountLong mod 3) = 0 then
begin
inc(IncLong);
end;
until CountLong >= 270;
x := XMove + x mod 255;
y := YMove + y mod 255;
with DXDraw1.Surface.Canvas do
begin
Brush.Style := bsClear;
Font.Color := clWhite;
Font.Size := 12;
Textout(0, 0, 'FPS: '+inttostr( DXTimer1.FrameRate)); { 錄制動畫是丟了這句 }
Release;
end;
DXDraw1.Flip;
end;
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key = VK_ESCAPE then Close;
end;
end.
窗體文件:
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClIEntHeight = 206
ClIEntWidth = 339
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
OnKeyDown = FormKeyDown
PixelsPerInch = 96
TextHeight = 13
object DXDraw1: TDXDraw
Left = 8
Top = 8
Width = 249
Height = 177
AutoInitialize = True
AutoSize = True
Color = clBlack
Display.FixedBitCount = False
Display.FixedRatio = True
Display.FixedSize = True
Options = [doAllowReboot, doWaitVBlank, doCenter, do3D, doDirectX7Mode, doHardware, DOSelectDriver]
SurfaceHeight = 177
SurfaceWidth = 249
OnFinalize = DXDraw1Finalize
OnInitialize = DXDraw1Initialize
TabOrder = 0
Traces = <
item
Name = 'aaa'
Active = True
Tag = 0
Blit.Active = True
Blit.Z = 0
Blit.Width = 0
Blit.Height = 0
Blit.Paths = {
200000000080A8430000FE420000000019000000000000000000000000000000
0000000000000000000000000000000000B0D7E2000080A74300000C43000000
0019000000000000000000000000000000000000000000000000000000000000
000030BD77050080A44300001843000000001900000000000000000000000000
00000000000000000000000000000000000000F0BE770500809F430000234300
0000001900000000000000000000000000000000000000000000000000000000
00000000B0C077050080984300002D4300000000190000000000000000000000
00000000000000000000000000000000000000000070C2770500809043000035
4300000000190000000000000000000000000000000000000000000000000000
00000000000030C477050000874300003B430000000019000000000000000000
0000000000000000000000000000000000000000000000F0C577050000794300
003F430000000019000000000000000000000000000000000000000000000000
0000000000000000B0C777050000644300004043000000001900000000000000
0000000000000000000000000000000000000000000000000070C9770500004F
4300003F43000000001900000000000000000000000000000000000000000000
0000000000000000000030CB770500003A4300003B4300000000190000000000
000000000000000000000000000000000000000000000000000000C099400700
0027430000354300000000190000000000000000000000000000000000000000
000000000000000000000000809B40070000174300002D430000000019000000
0000000000000000000000000000000000000000000000000000000000409D40
0700000943000023430000000019000000000000000000000000000000000000
0000000000000000000000000000009F40070000FE4200001843000000001900
00000000000000000000000000000000000000000000000000000000000000C0
A040070000F24200000C43000000001900000000000000000000000000000000
0000000000000000000000000000000080A240070000EE420000FE4200000000
1900000000000000000000000000000000000000000000000000000000000000
0040A440070000F2420000E44200000000190000000000000000000000000000
00000000000000000000000000000000000000A640070000FE420000CC420000
0000190000000000000000000000000000000000000000000000000000000000
000000C0A74007000009430000B6420000000019000000000000000000000000
000000000000000000000000000000000000000080A94007000017430000A242
0000000019000000000000000000000000000000000000000000000000000000
000000000040AB40070000274300009242000000001900000000000000000000
0000000000000000000000000000000000000000000000AD400700003A430000
8642000000001900000000000000000000000000000000000000000000000000
00000000000000C0AE400700004F4300007C4200000000190000000000000000
00000000000000000000000000000000000000000000000080B0400700006443
0000784200000000190000000000000000000000000000000000000000000000
00000000000000000040B240070000794300007C420000000019000000000000
000000000000000000000000000000000000000000000000000000B440070000
8743000086420000000019000000000000000000000000000000000000000000
0000000000000000000000C0B540070080904300009242000000001900000000
0000000000000000000000000000000000000000000000000000000080B74007
008098430000A242000000001900000000000000000000000000000000000000
0000000000000000000000000040B9400700809F430000B64200000000190000
00000000000000000000000000000000000000000000000000000000000000BB
40070080A4430000CC4200000000190000000000000000000000000000000000
000000000000000000000000000000C0BC40070080A7430000E4420000000019
0000000000000000000000000000000000000000000000000000000000000000
80BE4007}
end>
end
object DXTimer1: TDXTimer
ActiveOnly = True
Enabled = False
Interval = 10
OnTimer = DXTimer1Timer
Left = 160
Top = 104
end
object DXImageList1: TDXImageList
DXDraw = DXDraw1
Items.ColorTable = {
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000}
Items = <
item
PatternHeight = 0
PatternWidth = 0
Picture.Data = {
0454444942280000000200000001000000010008000000000004000000000000
0000000000000000000000000099FFFF00FFFFFF000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000001000000}
SystemMemory = False
Transparent = True
TransparentColor = clWhite
end>
Left = 192
Top = 96
end
end