Reading XML SpriteSheet Maps

Here is a simple code to read XML Sprite sheet maps. It will extract the coordinates of all images in the Sprite sheet from a XML file generated with Sprite Vortex. We will use OmniXML to accomplish this task.
The following code will open a dialog to select an XML file extract the coordinates only, then list them in a memo component and save them to a text file.

This is how a XML Sprite sheet map looks like:

Spritemap screenshot

Sprite Map generated with Sprite Vortex

Under the Sprite tag you’ll find the Coordinates witch contain the info that we need to locate our animation frame or cell in the form of X, Y, Width and Height. The following code will extract those parameter and save them to a text file:


procedure TForm1.GoGetTheCoordinatesClick(Sender: TObject);
var
  descNode: IXMLNode;
  iNode   : integer;
  node    : IXMLNode;
  nodeList: IXMLNodeList;
  xml     : IXMLDocument;
  Lista: TStringList;
  s: String;
begin
if opendialog1.Execute then

Lista:= TStringList.Create;
s := OpenDialog1.FileName;
s := ChangeFileExt(s, '.txt'); {will return the myfile.txt}
 memo1.Clear;
  xml := CreateXMLDoc;
  if XMLLoadFromFile(xml, OpenDialog1.FileName) then begin
    nodeList := xml.SelectNodes('//Coordinates');

    for node in XMLEnumNodes(xml, '//Coordinates') do
    begin
    memo1.Lines.Add(GetNodeTextStr(node, 'X')+' '+GetNodeTextStr(node, 'Y')+' '+GetNodeTextStr(node, 'Width')+' '+GetNodeTextStr(node, 'Height'));
    Lista.Add(GetNodeTextStr(node, 'X')+' '+GetNodeTextStr(node, 'Y')+' '+GetNodeTextStr(node, 'Width')+' '+GetNodeTextStr(node, 'Height'));
    end;
     end;
  Lista.SaveToFile(s);
  Lista.Free;
end;

Some alternatives to OmniXML, are OXml and NativeXML

In the future We probably will explore integrating Box2d or the Chipmunk physics engines.

Have a nice day.
Gilbert