Question

I have a Map that is a TGraphicControl with the properties

HexRow : Integer;
HexColumns : integer;
TempMap :TBitmap;

I would like to save all this data into one file. And when I read in the file I can get all this data. Like so...

Procedure Map.Load(name :String);
begin
  Something.LoadfromFile(string);
  TempMap := Something.Bitmap;
  HexRow  := Something.hexRow;
  HexColumn := Something.HexColumn;
end;

but no idea what "something" is. Also same for saving it should save all these propertys

Était-ce utile?

La solution

You need to write methods that write and read the information. You could do it using binary writer and reader objects.

procedure TMap.SaveToStream(Stream: TStream);
var
  Writer: TBinaryWriter;
begin
  Writer := TBinaryWriter.Create(Stream);
  try
    Writer.Write(HexRow);
    Writer.Write(HexColumn);
    Bitmap.SaveToStream(Stream);
  finally
    Writer.Free;
  end;
end;

procedure TMap.LoadFromStream(Stream: TStream);
var
  Reader: TBinaryReader;
begin
  Reader := TBinaryReader.Create(Stream);
  try
    HexRow := Reader.ReadInteger;
    HexColumn := Reader.ReadInteger;
    Bitmap.LoadFromStream(Stream);
  finally
    Reader.Free;
  end;
end;

Or if you wanted to be cute then you could write the bitmap first, and stuff the extra information at the end of the file. I think then that most bitmap reading code would just ignore the extra numbers you stored at the end. Of course, those values would not survive a round trip using any software other than your own.

To save to and load from a file use TFileStream.

Of course, such a file format doesn't give you much room for future enhancement and modification. Personally I think I'd be inclined to use PNG format. This gives you compression and I believe that you can add custom metadata.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top