delphi 之directX学习开篇(一)

  • A+
所属分类:[开发技巧]

今天跟着万一老师的blog 开始学习 direcXt技术,接触delphi这么久~现在才开始学习这个,可能是晚了点,自己的记忆力一直在衰退,所以把学到的都写下来~以后方便才找

测试 Demo 下载: http://files.cnblogs.com/del/DelphiX_1.rar (在 Delphi 2007 和 2009 下均编译通过)

其实按照 这里 的介绍, 比下载来的快, 也不需要下载.

, 微软很久的技术了; 从 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}
另外, 因为有些日文的注释乱码, 也会让有些例子运行不了, 修改下乱码的地方即可.
目前万一老师抽样测试了一些, 都没问题了.我日语反正是不会~directX这类的文章基本上会全部来源于万一老师的博客~请大家支持他.支持原创

本例效果图:

 

 

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
&
nbsp;     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
&
nbsp;    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

  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin
广告也精彩
avatar
广告也精彩

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: