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

interface

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

type
  TfrmDD = class(TForm)
    ApplicationEvents1: TApplicationEvents;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure ApplicationEvents1Idle(Sender: TObject; var Done: Boolean);
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure ApplicationEvents1Restore(Sender: TObject);
    procedure ApplicationEvents1Deactivate(Sender: TObject);
  private
    { Private declarations }
    FDD            : IDirectDraw7;
    FDDSPrimary    : IDirectDrawSurface7;
    FDDSBack       : IDirectDrawSurface7;
    FDDClipper     : IDirectDrawClipper;
    FDDSBackGround : IDirectDrawSurface7;
    FDDSImage      : IDirectDrawSurface7;
    FActive        : BOOL;
    procedure ErrorOut(hRet : HRESULT; FuncName : String);
    function RestoreAll : HRESULT;
    function UpdateFrame : HRESULT;
  public
    { Public declarations }
  end;

const
  ScreenWidth    = 800;
  ScreenHeight   = 600;
  ScreenBitDepth = 16;
  GroundBmp      = '..\beardo_pond1.bmp';
  ImageBmp       = '..\directx.bmp';

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;
  rgn : TRgnData;
  wrk : TRECT;
begin
  FDDSBack := nil;
  FDDSImage := nil;
  FDDSBackGround := nil;
  FDDSPrimary := nil;
  FDD := nil;

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

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

  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 begin
     ErrorOut(hRet, 'Create Primary Surface');
     Exit;
  end;

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

  SetRect (wrk, 0, 0, 800, 600);
  with rgn.rdh do begin
       dwSize := SizeOf (RGNDATAHEADER);
       iType := RDH_RECTANGLES;
       nCount := 1;
       nRgnSize := Sizeof(TRECT);
  end;
  PRECT(@rgn.Buffer)^ := wrk;
  if FDD.CreateClipper(0, FDDClipper, nil) = DD_OK then begin
     FDDClipper.SetClipList (@rgn, 0);
     FDDSBack.SetClipper(FDDClipper);
  end;

  FDDSBackGround := DDLoadBitmap(FDD, GroundBmp, 0, 0);
  if FDDSBackGround = nil then ErrorOut(DD_FALSE, 'DDLoadBitmap');

  FDDSImage := DDLoadBitmap(FDD, ImageBmp, 0, 0);
  if FDDSImage = nil then ErrorOut(DD_FALSE, '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 FDDSImage := nil;
      if Assigned(FDDSBackGround) then FDDSBackGround := nil;
      if Assigned(FDDClipper) then FDDClipper := nil;
      if Assigned(FDDSBack) then FDDSBack := nil;
      if Assigned(FDDSPrimary) then FDDSPrimary := nil;
      FDD := nil;
  end;
end;

var
  Scale : Integer = 32;
  Step : Integer = 2;

  mouseX : Integer = 0;
  mouseY : Integer = 0;

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

procedure TfrmDD.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Hide
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.ApplicationEvents1Idle(Sender: TObject;
  var Done: Boolean);
begin
  if FActive then begin
     if Failed (UpdateFrame) then RestoreAll;
     Scale := Scale + Step;
     if (Scale > 128) or (Scale < 32) then Step := - Step;
  end;

  Done := False;
end;

procedure TfrmDD.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  mouseX := X - Scale div 2;
  mouseY := Y - Scale div 2;
end;

function TfrmDD.RestoreAll : HRESULT;
var
  hRet : HRESULT;
begin
  hRet := FDDSPrimary._Restore;
  if Succeeded (hRet) then begin
      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;

function TfrmDD.UpdateFrame : HRESULT;
var
  hRet : HRESULT;
  wrkRect : TRECT;
begin
  hRet := FDDSBack.Blt (nil, FDDSBackGround, nil, DDBLT_WAIT, nil);
  if Failed (hRet) then begin
     Result := hRet;
     Exit;
  end;

  SetRect (wrkRect, mouseX, mouseY, mouseX + Scale, mouseY + Scale);

  hRet := FDDSBack.Blt (@wrkRect, FDDSImage, nil, DDBLT_WAIT or DDBLT_KEYSRC, nil);
  if Failed (hRet) then begin
     Result := hRet;
     Exit;
  end;

  Result := FDDSPrimary.Flip(nil, DDFLIP_WAIT)
end;

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

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

end.


