{******************************************************************************}
{* Иллюстрация к книге "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)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormPaint(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
  private
    { Private declarations }
    FDD            : IDirectDraw7;
    FDDSPrimary    : IDirectDrawSurface7;
    FDDSBackGround : IDirectDrawSurface7;
    FDDSBack       : IDirectDrawSurface7;
    FDDSImage      : IDirectDrawSurface7;    
    FDDClipper     : IDirectDrawClipper; 
    function RestoreAll : HRESULT;
    procedure ErrorOut(hRet : HRESULT; FuncName : String);
  public
    { Public declarations }
  end;

const
  groundBmp      = '..\LAKE.BMP';
  imageBmp      = '..\HAND.BMP';

var
  frmDD : TfrmDD;
  kr : Integer = 16;
  mouseX : Integer = 0;
  mouseY : Integer = 0;    

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;
begin
  FDDSImage := nil;
  FDDSBackGround := nil;
  FDDClipper := nil;
  FDDSPrimary := nil;
  FDD := nil;

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

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

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

  hRet := FDD.CreateClipper(0, FDDClipper, nil);
  if Failed (hRet) then ErrorOut(hRet, 'CreateClipper FAILED');

  hRet := FDDClipper.SetHWnd(0, Handle);
  if Failed (hRet) then ErrorOut(hRet, 'SetHWnd FAILED');

  hRet := FDDSPrimary.SetClipper(FDDClipper);
  if Failed (hRet) then ErrorOut(hRet, 'SetClipper FAILED');

  FDDSBackGround := DDLoadBitmap(FDD, groundBmp, 0, 0);
  if FDDSBackGround = nil then ErrorOut(hRet, 'DDLoadBitmap');

  FDDSImage := DDLoadBitmap(FDD, imageBmp, 0, 0);
  if FDDSImage = nil then ErrorOut(hRet, 'DDLoadBitmap');

  hRet := DDSetColorKey (FDDSImage, RGB (0, 0, 0));
  if Failed (hRet) then ErrorOut(hRet, 'DDSetColorKey');

  ShowCursor (False);
end;

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

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

function TfrmDD.RestoreAll : HRESULT;
var
  hRet : HRESULT;
begin
  hRet := FDDSPrimary._Restore;
  if Succeeded (hRet) then begin
      hRet := FDDSBack._Restore;
      if Failed (hRet) then begin
         Result := hRet;
         Exit;
      end;
      hRet := FDDSImage._Restore;
      if Failed (hRet) then begin
         Result := hRet;
         Exit;
      end;
      hRet := DDReLoadBitmap(FDDSImage, imageBmp);
      if Failed (hRet) then ErrorOut(hRet, 'DDReLoadBitmap');
      hRet := FDDSBackGround._Restore;
      if Failed (hRet) then begin
         Result := hRet;
         Exit;
      end;
      hRet := DDReLoadBitmap(FDDSBackGround, groundBmp);
      if Failed (hRet)
         then ErrorOut(hRet, 'DDReLoadBitmap');
      Result := DD_OK;
  end else Result := hRet;
end;

var
  rcMouse : TRECT;

procedure TfrmDD.FormPaint(Sender: TObject);
var
  rcDest, wrkRect : TRECT;
  p : TPOINT;
begin
  p.X := 0;
  p.Y := 0;
  Windows.ClientToScreen(Handle, p);
  Windows.GetClientRect(Handle, rcDest);
  OffsetRect(rcDest, p.X, p.Y);
  SetRect (wrkRect, 0, 0, ClientWidth, ClientHeight);
  if Failed (FDDSBack.Blt (@wrkRect, FDDSBackGround, nil, DDBLT_WAIT, nil))
      then if Failed (RestoreAll) then Exit;
  if Failed (FDDSBack.BltFast (mouseX, mouseY, FDDSImage, @rcMouse, DDBLTFAST_WAIT or
      DDBLTFAST_SRCCOLORKEY))
      then if Failed (RestoreAll) then Exit;
  if Failed (FDDSPrimary.Blt (@rcDest, FDDSBack, nil, DDBLT_WAIT, nil))
      then if Failed (RestoreAll) then Exit;
end;

procedure TfrmDD.FormResize(Sender: TObject);
var
  hRet : HRESULT;
  ddsd : TDDSurfaceDesc2;
begin
  if Assigned(FDDSBack) then FDDSBack := nil;
  ZeroMemory(@ddsd, SizeOf(ddsd));
  with ddsd do begin
     dwSize := SizeOf(ddsd);
     dwFlags := DDSD_CAPS or DDSD_HEIGHT or DDSD_WIDTH;
     ddsCaps.dwCaps := DDSCAPS_OFFSCREENPLAIN;
     dwWidth  := ClientWidth;
     dwHeight := ClientHeight;
  end;
  hRet := FDD.CreateSurface(ddsd, FDDSBack, nil);
  if Failed(hRet) then ErrorOut(hRet, 'Create Back Surface');
  FormPaint (nil);
end;

procedure TfrmDD.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var
  wrkI, wrkJ : Integer;
begin
  mouseX := X;
  if X < ClientWidth - 32
     then wrkI := 32
     else wrkI := ClientWidth - X;
  mouseY := Y;
  if Y < ClientHeight - 32
     then wrkJ := 32
     else wrkJ := ClientHeight - Y;
  SetRect (rcMouse, 0, 0, wrkI, wrkJ);
  FormPaint (nil);
end;

end.

