Thema: Delphi [DelphiX] Sprites

Einzelnen Beitrag anzeigen

Benutzerbild von 3_of_8
3_of_8

Registriert seit: 22. Mär 2005
Ort: Dingolfing
4.129 Beiträge
 
Turbo Delphi für Win32
 
#34

Re: [DelphiX] Sprites

  Alt 6. Jul 2006, 15:12
Delphi-Quellcode:
unit Map;

interface

uses SysUtils, Classes, DXDraws, Tiles, Dialogs;

type
  TMapField=Byte;

  TMapFields=array of array of TMapField;

  TMap=class
  protected
    FFields: TMapFields;
    FWidth, FHeight: Cardinal;
    function GetField(I, J: Cardinal): TMapField;
    procedure SetField(I, J: Cardinal; Value: TMapField);
    procedure SetWidth(Value: Cardinal);
    procedure SetHeight(Value: Cardinal);
  public
    procedure Clear;
    procedure Draw(Surface: TDirectDrawSurface; Tiles: TTiles);
    procedure LoadFromFile(FileName: string);
    procedure SaveToFile(FileName: string);
    property Width: Cardinal read FWidth write SetWidth default 0;
    property Height: Cardinal read FHeight write SetHeight default 0;
    property Fields[I, J: Cardinal]: TMapField
    read GetField write SetField; default;
  end;

implementation

procedure TMap.Clear;
var I, J: Cardinal;
begin
  for I:=0 to FWidth-1 do
    for J:=0 to FHeight-1 do
      FFields[I, J]:=0;
end;

function TMap.GetField(I, J: Cardinal): TMapField;
begin
  Result:=FFields[I, J];
end;

procedure TMap.SetField(I, J: Cardinal; Value: TMapField);
begin
  FFields[I, J]:=Value;
end;

procedure TMap.SetWidth(Value: Cardinal);
begin
  FWidth:=Value;
  setlength(FFields, FWidth, FHeight);
end;

procedure TMap.SetHeight(Value: Cardinal);
begin
  FHeight:=Value;
  setlength(FFields, FWidth, FHeight);
end;

procedure TMap.Draw(Surface: TDirectDrawSurface; Tiles: TTiles);
var I, J: Cardinal;
begin
  for I:=0 to FWidth-1 do
    for J:=0 to FHeight-1 do
    begin
      Tiles[FFields[I, J]].
        Draw(Surface, I*Tiles.tilewidth, J*Tiles.tileheight, 0);
    end;
end;

procedure TMap.LoadFromFile(FileName: string);
var fs: TFileStream;
begin
  fs:=TFileStream.Create(FileName, fmOpenRead);
  fs.Read(FWidth, SizeOf(Cardinal));
  fs.Read(FHeight, SizeOf(Cardinal));
  SetLength(FFields, FWidth, FHeight);
  fs.Read(FFields[0, 0], FWidth*FHeight*sizeof(TMapField));
  fs.free;
end;

procedure TMap.SaveToFile(FileName: string);
var fs: TFileStream;
begin
  fs:=TFileStream.Create(FileName, fmOpenWrite);
  fs.Write(FWidth, SizeOf(Cardinal));
  fs.Write(FHeight, SizeOf(Cardinal));
  fs.Write(FFields[0, 0], FWidth*FHeight*sizeof(TMapField));
  fs.free;
end;

end.
Manuel Eberl
„The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.“
- Terry Pratchett
  Mit Zitat antworten Zitat