Friday, 25 April 2014

Writing a StrPix-like program with Lazarus – 6

Our program copies the *.wad file into memory and then reads the file in memory and extracts the data into a variable named Wad which is declared in the TombRaiderWAD unit.

Since the implementation section of unit1 includes TombRaiderWAD in the uses clause, the variable Wad can be used in any of the functions or procedures in unit1’s implementation section.

The variable Wad is declared as type TWAD and we can inspect this type in the TombRaiderWAD unit to see what information is available in the Wad variable.
We can see that there is a variable (also known as a field) named "version" declared in the TWAD type .

TWADType

Its type is DWord. To find out what a DWord is, click on the word “DWord” so the cursor is positioned inside “DWord” and press the F1 key.

In CodeTyphon, F1 brings up the help. If there are several different references for the topic you may be presented with a list to choose from. If there are errors in your code the help function may not work. Also, standard Lazarus does not have this feature unless you install the help package.

Help

We see from help that a DWord is an unsigned 32bit integer.

We will add a control to display the version number of a *.wad file.

From the Window menu select Form1 to display our program’s main form.
Click on the TEdit control in the Standard tab of the component panel and then click and drag on the form to create the Edit1 control on the form.
Edit1 is the same size as the rectangle you draw on the form when dragging.

TEdit

Edit1

We will display the version number in the Edit1 control.

In the Window menu select Source Editor to display the code and make sure unit1 is displayed by selecting the unit1 tab.
In the Search menu click on Procedure List and then double click on TForm1.FileOpen1Accept to jump to that procedure in the Source Editor.

ProcedureJump

We will add the following lines to the procedure.

Edit1.Clear;
Edit1.Text := IntToStr(Wad.version);

Edit1.Clear erases the text that is showing.

In the next line we assign the new text to show.

Wad.version gets the version variable from the Wad variable.

The Wad.version variable is a Dword, that is, an integer number.

The text of Edit1 is a string (string means text) variable so we have to change a number to text and we do that by using the IntToStr function. Int(eger)To Str(ing).

Now where to put these lines in the procedure?

The variable Wad doesn’t contain the *.wad information until the information is extracted by WADParser.ExtractAll so our lines must be written after that.

The procedure code should look like this.
procedure TForm1.FileOpen1Accept(Sender: TObject);
var
  err: string;
  valid: integer;
begin
  // Processing all windows messages before and after using the
  // dialog box keeps the user interface clean.
  Application.ProcessMessages;
  ////////////// if not Main.OpenDialogWad.Execute then
  //////////////  exit;
  Application.ProcessMessages;

  // Make sure that wads will not accumulate in memory.
  // Free is successful even if the memory stream was not yet created.
  streamWAD.Free;
  streamWAD := TMemoryStream.Create;
  streamWAD.LoadFromFile(FileOpen1.Dialog.FileName);
  ////////////// Main.TextBox.Caption := Main.OpenDialogWad.FileName;

  // Crawl through the file for validity and find the main sections.
  valid := WADCrawler.IsValidWad(streamWAD, err);
  if valid < 0 then
  begin
    MessageDlg('File is not a valid WAD.' + #10 + err,
      mtWarning,
      [mbOK],
      0);
    exit;
  end
  else if valid > 0 then
    MessageDlg('File is not a regular WAD.' + #10 + err,
      mtWarning,
      [mbOK],
      0);

  // The main sections were already detected by the crawler.
  // Now parse the WAD file and extract the data tables.
  WADParser.ExtractAll(streamWAD);

  Edit1.Clear; //NEW
  Edit1.Text:= IntToStr(wad.version); //NEW

  // Build the Wad Tree.
  //////////////Main.MakeWadTree;
  //////////////Main.TreeViewWad.FullExpand;

  // Prepare the user data interface.
  //////////////Main.PrepareWadDataForDisplay;

  // Enable the other file options in the popup.
  //Main.SaveWad.Enabled   := TRUE;
  //Main.SaveAsWad.Enabled := TRUE;
end;

Build and run the program and open a *.wad file and the version number should be displayed in Edit1.

Output


Next time we will use a TTreeView control to display the information the same as WadExplorer, PixStr and TRViewer.

If you want to get a head start, study the MakeWadTree method in WadExplorer’s MainUnit.



prev | next

No comments:

Post a Comment