unit UMemMap;
interface
uses SysUtils, Graphics;
type PFType = ^TFType;
TFType = (
ftFree,
ftWater,
ftUndestroyable,
ftDestroyable
);
TMapData = record
Name : String;
Fieldsize : Word;
BgColor : TColor;
Images : array [ftWater..ftDestroyable] of String;
end;
TMemMap = class
private
Data : Pointer;
Rows,
Cols : Word;
public
destructor Destroy; override;
procedure AddRow(PrevRow : Word);
procedure ChangeSize(NewRows, NewCols : Word);
procedure UpdateField(Row, Col : Word; FType : TFType);
procedure Echo;
end;
implementation
destructor TMemMap.Destroy;
begin
FreeMem(Data);
inherited
end;
procedure TMemMap.AddRow(PrevRow : Word);
var NewData : Pointer;
Row,
Col : Integer;
begin
GetMem(NewData, (Rows + 1) * Cols * SizeOf(TFType));
for Row := 0 to PrevRow - 1 do
for Col := 0 to Cols do
PFType(Integer(NewData) + (Row * Cols + Col) * SizeOf(TFType))^ :=
PFType(Integer(Data) + (Row * Cols + Col) * SizeOf(TFType))^;
for Col := 0 to Cols do
PFType(Integer(NewData) + (PrevRow * Cols + Col) * SizeOf(TFType))^ := ftFree;
for Row := PrevRow + 1 to Rows do
for Col := 0 to Cols do
PFType(Integer(NewData) + (Row * Cols + Col) * SizeOf(TFType))^ :=
PFType(Integer(Data) + ((Row - 1) * Cols + Col) * SizeOf(TFType))^;
FreeMem(Data);
Inc(Rows);
Data := NewData
end;
procedure TMemMap.ChangeSize(NewRows, NewCols : Word);
var NewData : Pointer;
Row,
Col : Integer;
begin
if (NewRows = 0) or (NewCols = 0) then
NewData := nil
else
begin
GetMem(NewData, NewRows * NewCols * SizeOf(TFType));
for Row := 0 to NewRows - 1 do
begin
for Col := 0 to NewCols - 1 do
if (Col > Cols - 1) or (Row > Rows - 1) then
PFType(Integer(NewData) + (Row * NewCols + Col) * SizeOf(TFType))^ := ftFree
else
PFType(Integer(NewData) + (Row * NewCols + Col) * SizeOf(TFType))^ :=
PFType(Integer(data) + (Row * Cols + Col) * SizeOf(TFtype))^
end
end;
FreeMem(Data);
Rows := NewRows;
Cols := NewCols;
Data := NewData
end;
procedure TMemMap.UpdateField(Row, Col : Word; FType : TFType);
begin
PFType(Integer(Data) + ((Row - 1) * Cols + (Col - 1)) * SizeOf(TFType))^ := FType
end;
procedure TMemMap.Echo;
var Row, Col : Integer;
begin
writeln('Ausagbe: ', Cols, 'x', Rows);
for Row := 0 to Rows - 1 do
begin
for Col := 0 to Cols - 1 do
write(Word(PFType(Integer(Data) + (Row * Cols + Col) * SizeOf(TFType))^), ' ');
writeln
end;
writeln
end;
end.