{******************************************************************************}
{* Иллюстрация к книге "DirectX. Графика в проектах Delphi".                  *}
{* Издательство BHV. www.bhv.ru                                               *}
{* Краснов М.В. softgl@chat.ru                                                *}
{******************************************************************************}
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  AppEvnts, ExtCtrls,
  DirectDraw, DDUtil;

type
  TfrmDD = class(TForm)
    ApplicationEvents1: TApplicationEvents;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure ApplicationEvents1Deactivate(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure ApplicationEvents1Restore(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure ApplicationEvents1Idle(Sender: TObject; var Done: Boolean);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
    FDD            : IDirectDraw7;
    FDDSBack       : IDirectDrawSurface7;
    FDDSPrimary    : IDirectDrawSurface7;
    FActive        : BOOL;
    function UpdateFrame : HRESULT;
    function FlipPages : HRESULT;
    function RestoreAll : HRESULT;
    function Circle (const X, Y, R : Integer;
                     const Color : Byte) : HRESULT;
    procedure ErrorOut(hRet : HRESULT; FuncName : String);
    function Clear : HRESULT;
  public
    { Public declarations }
  protected
    procedure FormSetCursor(var aMsg : TMessage); message WM_SETCURSOR;
  end;

const
  ScreenWidth    = 640;
  ScreenHeight   = 480;
  ScreenBitDepth = 8;

var
  frmDD : TfrmDD;

implementation

{$R *.DFM}

procedure TfrmDD.ErrorOut(hRet : HRESULT; FuncName : String);
var
  t : TextFile;
begin
  AssignFile (t, 'Debug.txt');
  Rewrite (t);
  WriteLn (t, FuncName + ': ' + DDErrorString (hRet));
  CloseFile (t);
  Destroy;
end;

procedure TfrmDD.FormCreate(Sender: TObject);
var
  hRet : HRESULT;
  ddsd : TDDSurfaceDesc2;
  ddscaps : TDDSCaps2;
begin
  FDDSBack := nil;
  FDDSPrimary := nil;
  FDD := nil;

  hRet := DirectDrawCreateEx (nil, FDD, IDirectDraw7, nil);
  if Failed (hRet) then ErrorOut(hRet, 'DirectDrawCreateEx');

  hRet := FDD.SetCooperativeLevel(Handle, DDSCL_FULLSCREEN or DDSCL_EXCLUSIVE);
  if Failed (hRet) then ErrorOut(hRet, 'SetCooperativeLevel');

  hRet := FDD.SetDisplayMode (ScreenWidth, ScreenHeight, ScreenBitDepth, 0, 0);
  if Failed (hRet) then ErrorOut(hRet, 'SetDisplayMode');

  ZeroMemory (@ddsd, SizeOf(ddsd));
  with ddsd do begin
       dwSize := SizeOf(ddsd);
       dwFlags := DDSD_CAPS or DDSD_BACKBUFFERCOUNT;
       ddsCaps.dwCaps := DDSCAPS_PRIMARYSURFACE or DDSCAPS_FLIP or DDSCAPS_COMPLEX;
       dwBackBufferCount := 1;
  end;
  hRet := FDD.CreateSurface(ddsd, FDDSPrimary, nil);
  if Failed (hRet) then ErrorOut(hRet, 'Create Primary Surface');

  ZeroMemory (@ddscaps, SizeOf(ddscaps));
  ddscaps.dwCaps := DDSCAPS_BACKBUFFER;
  hRet := FDDSPrimary.GetAttachedSurface(ddscaps, FDDSBack);
  if Failed (hRet) then ErrorOut(hRet, 'GetAttachedSurface');

  if Failed (Clear) then Close;
  if Failed (FlipPages) then Close;
  if Failed (Clear) then Close;
end;

procedure TfrmDD.FormDestroy(Sender: TObject);
begin
  if Assigned(FDD) then begin
      if Assigned(FDDSBack) then FDDSBack := nil;
      if Assigned(FDDSPrimary) then FDDSPrimary := nil;
      FDD := nil;
  end;
end;

procedure TfrmDD.ApplicationEvents1Deactivate(Sender: TObject);
begin
  FActive := False;
  Application.Minimize;
end;

procedure TfrmDD.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (Key = VK_ESCAPE) or (Key = VK_F12) then Close;
end;

procedure TfrmDD.FormSetCursor(var aMsg : TMessage);
begin
  SetCursor(0);
end;

function TfrmDD.RestoreAll : HRESULT;
var
  hRet : HRESULT;
begin
  hRet := FDDSPrimary._Restore;
  if Succeeded (hRet) then begin
     if Failed (Clear) then Close;
     if Failed (FlipPages) then Close;
     if Failed (Clear) then Close;
     Result := DD_OK
  end else Result := hRet;
end;

procedure TfrmDD.ApplicationEvents1Restore(Sender: TObject);
begin
  WindowState := wsMaximized;
  FActive := True;
end;

procedure TfrmDD.FormActivate(Sender: TObject);
begin
  FActive := True;
end;

procedure TfrmDD.ApplicationEvents1Idle(Sender: TObject;
  var Done: Boolean);
begin
  if FActive then begin
     if Succeeded (UpdateFrame)
        then FlipPages
        else RestoreAll
  end;
  Done := False;
end;

function TfrmDD.Clear : HRESULT;
var
   desc : TDDSURFACEDESC2;
   hRet : HRESULT;
begin
   ZeroMemory (@desc, SizeOf(desc));
   desc.dwSize := SizeOf(desc);

   hRet := FDDSBack.Lock (nil, desc, DDLOCK_WAIT, 0);
   if Failed (hRet) then begin
      Result := hRet;
      Exit;
   end;

   FillChar (desc.lpSurface^, 307200, 0);

   Result := FDDSBack.Unlock (nil);
end;

function TfrmDD.Circle (const X, Y, R : Integer;
                        const Color : Byte) : HRESULT;
procedure PutPixel (const Surf, lPitch, X, Y : Integer; Value : Byte);
begin
   PByte (Surf + Y * lPitch + X)^ := Value;
end;
var
   desc : TDDSURFACEDESC2;
   a : 0..359;
   hRet : HRESULT;
begin
   ZeroMemory (@desc, SizeOf(desc));
   desc.dwSize := SizeOf(desc);

   hRet := FDDSBack.Lock (nil, desc, DDLOCK_WAIT, 0);
   if Failed (hRet) then begin
      Result := hRet;
      Exit;
   end;
   for a := 0 to 359 do
       PutPixel (Integer(desc.lpSurface),  desc.lPitch,
                 X + trunc (cos (a) * R),
                 Y + trunc (sin (a) * R),
                 Color);
   Result := FDDSBack.Unlock (nil);
end;

function TfrmDD.UpdateFrame : HRESULT;
var
  X, Y, R : Integer; Color : Byte;
  hRet : HRESULT;
begin
  X := random (ScreenWidth - 30) + 15;
  Y := random (ScreenHeight - 30) + 15;
  R := random (10) + 5;
  Color := random (256);

  hRet := Circle (X, Y, R, Color);
  if Failed (hRet) then begin
     Result := hRet;
     Exit;
  end;

  hRet := FDDSPrimary.Flip(nil, DDFLIP_WAIT);
  if Failed (hRet) then begin
     Result := hRet;
     Exit;
  end;

  hRet := Circle (X, Y, R, Color);
  if Failed (hRet) then begin
     Result := hRet;
     Exit;
  end;

  Result := DD_OK;
end;

function TfrmDD.FlipPages : HRESULT;
begin
  Result := FDDSPrimary.Flip(nil, DDFLIP_WAIT);
end;

procedure TfrmDD.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Hide
end;

end.

