unit Unit5;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, ComCtrls, Grids;
type
TTerrainCell = record
Index : integer;
end;
THeader = record
WIDTH : integer;
HEIGHT : integer;
end;
type
TForm2 = class(TForm)
Panel1: TPanel;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
StringGrid1: TStringGrid;
CheckBox1: TCheckBox;
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure CheckBox1Click(Sender: TObject);
private
{ Private declarations }
Header: THeader;
MyTerrain : array of array of TTerrainCell;
outfile : file; // of byte; (*glenn*)
procedure ReadData;
procedure FillData;
procedure WriteData;
procedure Truncate;
procedure WriteMemo;
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
const
filename = 'MyTerrainCell.dat';
procedure TForm2.FillData;
var x, y: integer;
begin
MyTerrain:= nil;
Header.WIDTH:= 8;
Header.HEIGHT:= 8;
setlength(MyTerrain, Header.WIDTH);
for x:= Low(MyTerrain) to High(MyTerrain) do
setlength(MyTerrain[x], Header.HEIGHT);
for x:= Low(MyTerrain) to High(MyTerrain) do
for y:= Low(MyTerrain[x]) to High(MyTerrain[x]) do
MyTerrain[x, y].Index:= Integer(@MyTerrain[x, y].Index); //x + y; //random(32)
end;
procedure TForm2.ReadData;
var x, y: integer;
begin
assignfile(outfile, filename);
reset(outfile, 1); // ADDED ,1 (*glen*)
// read header
blockread(outfile, Header, sizeof(Header));
// create dynamic array dimensions
setlength(MyTerrain, Header.WIDTH);
for x:= Low(MyTerrain) to High(MyTerrain) do
setlength(MyTerrain[x], Header.HEIGHT);
// read data
(* new code - not working????
for x := Low(MyTerrain) to High(MyTerrain) do
blockread(outfile, MyTerrain[x, Low(MyTerrain)], Sizeof([x, Low(MyTerrain)])*Header.Height);
*)
for x:= Low(MyTerrain) to High(MyTerrain) do
for y:= Low(MyTerrain[x]) to High(MyTerrain[x]) do {*glen*}
blockread(outfile, MyTerrain[x, y], Sizeof(MyTerrain[x, y])); {*glen*}
closefile(outfile);
end;
procedure TForm2.WriteData;
var x, y: integer;
begin
assignfile(outfile, filename);
rewrite(outfile, 1);
// write header
blockwrite(outfile, Header, sizeof(THeader));
// write to disk
(* new code - not working????
for x := Low(MyTerrain) to High(MyTerrain) do
blockwrite(outfile, MyTerrain[x, Low(MyTerrain)], Sizeof([x, Low(MyTerrain)]) * Header.Height);
*)
for x := Low(MyTerrain) to High(MyTerrain) do {*glenn*}
for y := Low(MyTerrain[x]) to High(MyTerrain[x]) do {*glenn*}
blockwrite(outfile, MyTerrain[x, y], sizeof(MyTerrain[x, y])); {*glenn*}
closefile(outfile);
end;
procedure TForm2.Truncate;
var n: integer;
begin
n:= 5;
Header.Height:= Header.Height - n;
Move(MyTerrain[n, Low(MyTerrain)],
MyTerrain[0, Low(MyTerrain)],
Sizeof([0, Low(MyTerrain)]) * Header.Height);
end;
procedure TForm2.WriteMemo;
var x, y: integer;
begin
StringGrid1.FixedCols:= 1;
StringGrid1.FixedRows:= 1;
StringGrid1.ColCount:= Header.WIDTH + 1;
StringGrid1.RowCount:= Header.HEIGHT + 1;
for x:= 1 to StringGrid1.ColCount - 1 do
StringGrid1.Cells[x, 0]:= format('x[%d]', [x-1]);
for y:= 1 to StringGrid1.RowCount - 1 do
StringGrid1.Cells[0, y]:= format('y[%d]', [y-1]);
for x:= Low(MyTerrain) to High(MyTerrain) do
for y:= Low(MyTerrain[x]) to High(MyTerrain[x]) do
if CheckBox1.Checked then
StringGrid1.Cells[x+1, y+1]:= format('%s', [IntToHex(MyTerrain[x, y].Index, 8)])
else
StringGrid1.Cells[x+1, y+1]:= format('%8d', [MyTerrain[x, y].Index])
end;
procedure TForm2.Button1Click(Sender: TObject);
begin
ReadData;
WriteMemo
end;
procedure TForm2.Button3Click(Sender: TObject);
begin
FillData;
WriteMemo
end;
procedure TForm2.Button2Click(Sender: TObject);
begin
WriteData;
WriteMemo
end;
procedure TForm2.Button4Click(Sender: TObject);
begin
Truncate;
WriteMemo
end;
procedure TForm2.CheckBox1Click(Sender: TObject);
begin
WriteMemo
end;
end.