unit MainUnit;
{$mode objfpc}{$H+}
interface
uses
Types, Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, Direct3D9;
const
{$IFDEF DXDEBUG}
Local_D3D_SDK_VERSION = (32 or $80000000);
{$ELSE}
Local_D3D_SDK_VERSION = 32;
{$ENDIF}
type
{ TForm1 }
TForm1 = class(TForm)
Memo1: TMemo;
procedure FormClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormKeyPress(Sender: TObject; var Key: char);
procedure Log(const S : string; N : Int64);
procedure Init3D;
private
fD3D : IDirect3D9;
fDevice : IDirect3DDevice9;
public
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
Caption:='Fpc'+IntToStr(SizeOf(Pointer)*8);
Init3D;
end;
procedure TForm1.FormKeyPress(Sender: TObject; var Key: char);
begin
if Key=#13 then Self.Click;
end;
procedure TForm1.Init3D;
var
R : TRect;
W, H : Integer;
PresParams : TD3DPresentParameters;
res : HResult;
begin
R:=Self.BoundsRect;
W:=(R.Right-R.Left);
H:=(R.Bottom-R.Top);
fD3D:=Direct3DCreate9(Local_D3D_SDK_Version);
Log('IDirect3D ',UIntPtr(fD3D));
if not Assigned(fD3D) then Exit;
FillChar(PresParams, SizeOf(PresParams), 0);
PresParams.Windowed:=True;
PresParams.PresentationInterval:=D3DPRESENT_INTERVAL_DEFAULT;
PresParams.SwapEffect:=D3DSWAPEFFECT_COPY;
PresParams.BackBufferCount:=1;
PresParams.BackBufferWidth:=W;
PresParams.BackBufferHeight:=H;
PresParams.BackBufferFormat:=D3DFMT_X8R8G8B8; //_D3DFORMAT(Params.DisplayMode.PixelFormat);
Log('Self.Handle ', Self.Handle);
res := fD3D.CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, Self.Handle,
D3DCREATE_MULTITHREADED or D3DCREATE_FPU_PRESERVE or D3DCREATE_SOFTWARE_VERTEXPROCESSING,
@PresParams, fDevice );
Log('Create Device, HResult ',res );
if res=D3D_OK then Log('IDirect3DDevice9 ', UIntPtr(fDevice) );
end;
procedure TForm1.FormClick(Sender: TObject);
var
R : TRect;
W, H : Integer;
begin
if fDevice=nil then Exit;
if Memo1.Visible then begin
Memo1.Visible:=False;
Self.Update;
end;
R:=Self.BoundsRect;
W:=(R.Right-R.Left);
H:=(R.Bottom-R.Top);
fDevice.BeginScene;
R:=Rect(0,0,W,H div 3);
fDevice.Clear(1, @R, D3DCLEAR_TARGET, $00FFFFFF, 0, 0);
R.Top:=R.Bottom; R.Bottom:=H*2 div 3;
fDevice.Clear(1, @R, D3DCLEAR_TARGET, $000000FF, 0, 0);
R.Top:=R.Bottom; R.Bottom:=H;
fDevice.Clear(1, @R, D3DCLEAR_TARGET, $00FF0000, 0, 0);
fDevice.EndScene;
R:=Rect(0,0,W,H);
fDevice.Present( @R, @R, 0, nil );
end;
procedure TForm1.Log(const S : string; N : Int64);
begin
Form1.Memo1.Lines.Add(S+IntToStr(N));;
end;
end.