MainFm.pas
上传用户:ctlcnc
上传日期:2021-12-10
资源大小:4933k
文件大小:5k
源码类别:

2D图形编程

开发平台:

Delphi

  1. unit MainFm;
  2. //---------------------------------------------------------------------------
  3. interface
  4. //---------------------------------------------------------------------------
  5. uses
  6.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7.   Dialogs, ComCtrls, XPMan, ExtCtrls, ToolWin, StdCtrls, Direct3D9,
  8.   AsphyreSystemFonts, AsphyreDevices;
  9. //---------------------------------------------------------------------------
  10. type
  11.   TMainForm = class(TForm)
  12.     XPManifest1: TXPManifest;
  13.     StatusBar1: TStatusBar;
  14.     procedure FormResize(Sender: TObject);
  15.     procedure FormDestroy(Sender: TObject);
  16.     procedure FormCreate(Sender: TObject);
  17.   private
  18.     { Private declarations }
  19.     Ticks: Integer;
  20.     procedure ConfigureDevice(Sender: TAsphyreDevice; Tag: TObject;
  21.      var Config: TScreenConfig);
  22.     procedure DeviceReset(Sender: TAsphyreDevice; Tag: TObject;
  23.      var Params: TD3DPresentParameters);
  24.     procedure TimerEvent(Sender: TObject);
  25.     procedure ProcessEvent(Sender: TObject);
  26.     procedure DrawView(Sender: TAsphyreDevice; Tag: TObject);
  27.     procedure Button1Click(Sender: TObject);
  28.     procedure Button2Click(Sender: TObject);
  29.   public
  30.     { Public declarations }
  31.   end;
  32. //---------------------------------------------------------------------------
  33. var
  34.   MainForm: TMainForm;
  35. //---------------------------------------------------------------------------
  36. implementation
  37. uses
  38.  Vectors2px, MediaImages, MediaFonts, AsphyreTimer, GuiHelpers,
  39.  GuiTypes, GuiShapeRep, GuiEdit, GuiButton, GuiForms;
  40. {$R *.dfm}
  41. //---------------------------------------------------------------------------
  42. procedure TMainForm.FormCreate(Sender: TObject);
  43. begin
  44.  ImageGroups.ParseLink('/images.xml');
  45.  FontGroups.ParseLink('/fonts.xml');
  46.  GuiShapes.ParseLink('/guidesc.xml');
  47.  if (not Devices.Initialize(ConfigureDevice, Self)) then
  48.   begin
  49.    MessageDlg('Failed to initialize Asphyre device!', mtError, [mbOk], 0);
  50.    Close();
  51.    Exit;
  52.   end;
  53.  Timer.Enabled  := True;
  54.  Timer.OnTimer  := TimerEvent;
  55.  Timer.OnProcess:= ProcessEvent;
  56.  Timer.MaxFPS   := 100;
  57.  GuiUseDevice(DefDevice);
  58.  GuiHelper.EventForm:= Self;
  59.  DefDevice.SysFonts.CreateFont('sys:tahoma', 'tahoma', 9, False, fwtHeavy,
  60.   fqtClearType, fctAnsi);
  61.  GuiHelper.Workspace.ParseLink('/guidesc.xml');
  62.  TGuiButton(GuiHelper.Ctrl['Button1']).OnClick:= Button1Click;
  63.  TGuiButton(GuiHelper.Ctrl['Button2']).OnClick:= Button2Click;
  64.  TGuiForm(GuiHelper.Ctrl['Form1']).Origin:= Point2px(40, 40);
  65. end;
  66. //---------------------------------------------------------------------------
  67. procedure TMainForm.FormDestroy(Sender: TObject);
  68. begin
  69.  Devices.Finalize();
  70. end;
  71. //---------------------------------------------------------------------------
  72. procedure TMainForm.ConfigureDevice(Sender: TAsphyreDevice; Tag: TObject;
  73.  var Config: TScreenConfig);
  74. begin
  75.  Config.WindowHandle:= Self.Handle;
  76.  Config.HardwareTL  := False;
  77.  Config.DepthStencil:= dsNone;
  78.  Config.Width   := ClientWidth;
  79.  Config.Height  := ClientHeight;
  80.  Config.Windowed:= True;
  81.  Config.VSync   := False;
  82.  Config.BitDepth:= bd24bit;
  83. end;
  84. //---------------------------------------------------------------------------
  85. procedure TMainForm.FormResize(Sender: TObject);
  86. begin
  87.  DefDevice.Reset(DeviceReset, Self);
  88. end;
  89. //---------------------------------------------------------------------------
  90. procedure TMainForm.DeviceReset(Sender: TAsphyreDevice; Tag: TObject;
  91.  var Params: TD3DPresentParameters);
  92. begin
  93.  Params.BackBufferWidth := ClientWidth;
  94.  Params.BackBufferHeight:= ClientHeight;
  95. end;
  96. //---------------------------------------------------------------------------
  97. procedure TMainForm.TimerEvent(Sender: TObject);
  98. begin
  99.  DefDevice.Render(DrawView, Self, $2680BA);
  100.  Timer.Process();
  101. end;
  102. //---------------------------------------------------------------------------
  103. procedure TMainForm.ProcessEvent(Sender: TObject);
  104. begin
  105.  Inc(Ticks);
  106.  GuiHelper.Update();
  107. end;
  108. //---------------------------------------------------------------------------
  109. procedure TMainForm.DrawView(Sender: TAsphyreDevice; Tag: TObject);
  110. begin
  111.  GuiHelper.Draw();
  112.  Sender.SysFonts.Font['sys:tahoma'].TextOut('FPS: ' + IntToStr(Timer.FrameRate),
  113.   4, ClientHeight - 40, $FF3F0BA2);
  114. end;
  115. //---------------------------------------------------------------------------
  116. procedure TMainForm.Button1Click(Sender: TObject);
  117. begin
  118.  // Copy text from Edit1 to Edit2
  119.  TGuiEdit(GuiHelper.Ctrl['Edit2']).Text:= TGuiEdit(GuiHelper.Ctrl['Edit1']).Text;
  120.  // Change the string in Edit1
  121.  TGuiEdit(GuiHelper.Ctrl['Edit1']).Text:= 'Type text here';
  122. end;
  123. //---------------------------------------------------------------------------
  124. procedure TMainForm.Button2Click(Sender: TObject);
  125. begin
  126.  TGuiEdit(GuiHelper.Ctrl['Edit1']).Text:= 'Cancelled!';
  127.  TGuiEdit(GuiHelper.Ctrl['Edit2']).Text:= 'Cancelled!';
  128. end;
  129. //---------------------------------------------------------------------------
  130. end.