{******************************************************************************}
{* Иллюстрация к книге "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,
  CDXScreenX, CDXTileX, CDXMapX, CDXInputX, DirectInput;

type
  TfrmDD = class(TForm)
    ApplicationEvents1: TApplicationEvents;
    procedure FormCreate(Sender: TObject);
    procedure ApplicationEvents1Deactivate(Sender: TObject);
    procedure ApplicationEvents1Restore(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure ApplicationEvents1Idle(Sender: TObject; var Done: Boolean);
    procedure FormActivate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    FActive : BOOL;
    function UpdateFrame : HRESULT;
  public
    { Public declarations }
  protected
    procedure FormSetCursor(var aMsg : TMessage); message WM_SETCURSOR;
  end;

const
  ScreenWidth    = 800;
  ScreenHeight   = 600;
  ScreenBitDepth = 8;
  MapSizeX       = 64;
  MapSizeY       = 64;

var
  frmDD : TfrmDD;
  GameInput : CDXInput;
  GameScreen : CDXScreen;
  LandScape : CDXTile;
  GameMap : CDXMap;
  MapScrollSpeed : Integer = 4;

implementation

{$R *.DFM}

procedure TfrmDD.FormCreate(Sender: TObject);
var
  i, j : Integer;
  Tile : Integer;
begin
  GameInput := CDXInput.CreateCDXInput;
  GameInput.Create(HInstance, Handle);

  GameScreen := CDXScreen.CreateCDXScreenCustomBPP(Handle, ScreenWidth,
                ScreenHeight, ScreenBitDepth);

  LandScape := CDXTile.CDXTileCustom(GameScreen,'Anim.bmp', MapSizeX, MapSizeY, 4);

  GameMap := CDXMap.CDXMap(LandScape, GameScreen);
  GameMap.CreateMap(MapSizeX, MapSizeY, 1);
  GameMap.MoveTo(0, 0);

  Tile := 1;

  for i := 0 to 63 do
      for j := 0 to 62 do begin
	GameMap.SetTile(i, j, Tile);
        Tile := Tile + 1;
	if Tile > 4 then Tile := 1;
  end;
end;

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

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

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

function KeyDown (Key : Byte): BOOL;
begin
  Result := GameInput.Keys[Key] = 128;
end;

procedure UpdateKeys;
begin
  if KeyDown(DIK_RIGHT) then GameMap.WrapScrollRight(MapScrollSpeed);
  if KeyDown(DIK_LEFT) then GameMap.WrapScrollLeft(MapScrollSpeed);
  if KeyDown(DIK_UP) then GameMap.WrapScrollUp(MapScrollSpeed);
  if KeyDown(DIK_DOWN) then GameMap.WrapScrollDown(MapScrollSpeed);
  if KeyDown(DIK_ESCAPE) then frmDD.Close;
end;

var
  Delay : Integer = 0;

function TfrmDD.UpdateFrame : HRESULT;
var
  wrk : TRECT;
  i, j, Tile : Integer;
begin
  GameInput.Update;
  UpdateKeys;

  SetRect (wrk, 0, 0, ScreenWidth, ScreenHeight);
  GameMap.DrawClipped (GameScreen.GetAppBackBuffer, wrk);

  Inc (Delay);

  if Delay > 40 then begin
     for i := 0 to 62 do
	 for j := 0 to 62 do begin
    	     Tile := GameMap.GetTile(i, j);
	     Inc (Tile);
             if Tile > 4 then Tile := 1;
	     GameMap.SetTile(i, j, Tile);
             end;
     Delay := 0;
  end;

  Result := GameScreen.Flip;
end;

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

procedure TfrmDD.ApplicationEvents1Idle(Sender: TObject;
  var Done: Boolean);
begin
  if FActive then if Failed (UpdateFrame) then begin
     GameScreen.Restore;
     LandScape.Restore;
  end;
  Done := False;
end;

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

procedure TfrmDD.FormDestroy(Sender: TObject);
begin
  if Assigned (GameInput) then GameInput.Destroy;
  if Assigned (LandScape) then LandScape.Destroy;
  if Assigned (GameMap) then GameMap.Destroy;
  if Assigned (GameScreen) then GameScreen.Destroy;
end;

end.


