<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>WAM Games &#187; svg</title>
	<atom:link href="http://wam-games.com/tag/svg/feed/" rel="self" type="application/rss+xml" />
	<link>http://wam-games.com</link>
	<description></description>
	<lastBuildDate>Wed, 21 Jul 2010 14:03:06 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>SVG in XNA</title>
		<link>http://wam-games.com/2010/04/svg-in-xna/</link>
		<comments>http://wam-games.com/2010/04/svg-in-xna/#comments</comments>
		<pubDate>Wed, 14 Apr 2010 10:41:31 +0000</pubDate>
		<dc:creator>Martin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Retrofit series]]></category>
		<category><![CDATA[Xbox]]></category>
		<category><![CDATA[content pipeline]]></category>
		<category><![CDATA[overload]]></category>
		<category><![CDATA[retrofit]]></category>
		<category><![CDATA[svg]]></category>
		<category><![CDATA[xblig]]></category>

		<guid isPermaLink="false">http://www.wam-games.com/?p=328</guid>
		<description><![CDATA[Here's how we use the open-source SVG Rendering Engine to read SVG files in our content pipeline...]]></description>
			<content:encoded><![CDATA[<p><img src="/wp-content/uploads/2010/04/svg-in-xna.png" alt="Authoring enemy flight paths with SVG Beziers." /></p>
<p>Scalable Vector Graphics (<a title="SVG on Wikipedia" href="http://en.wikipedia.org/wiki/Scalable_Vector_Graphics" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Scalable_Vector_Graphics?referer=');">SVG</a>) seemed an ideal format for authoring the enemy paths in <em>Retrofit: Overload</em> but it turned out to be much harder to parse than it appeared at first sight. Fortunately the open-source <a title="SVG Rendering Engine home page" href="http://svg.codeplex.com/" onclick="pageTracker._trackPageview('/outgoing/svg.codeplex.com/?referer=');">SVG Rendering Engine</a> works very nicely for reading SVG files in our content pipeline. Here&#8217;s how we use it:</p>
<p><span id="more-328"></span></p>
<p>Although library source code is available, it&#8217;s sufficient to add the <a title="download page" href="http://svg.codeplex.com/releases/view/18884" onclick="pageTracker._trackPageview('/outgoing/svg.codeplex.com/releases/view/18884?referer=');">Svg.dll</a> to the content importer project&#8217;s References. This makes the Svg namespace available. We also need references to System.Drawing and System.Xml.</p>
<p>Here&#8217;s some fairly standard preamble:</p>
<pre class="csharpcode"><span class="kwrd">using</span> System;
<span class="kwrd">using</span> System.Collections.Generic;
<span class="kwrd">using</span> System.Linq;

<span class="kwrd">using</span> Microsoft.Xna.Framework;
<span class="kwrd">using</span> Microsoft.Xna.Framework.Content.Pipeline;

<span class="kwrd">using</span> Svg;
<span class="kwrd">using</span> System.Drawing.Drawing2D;

<span class="rem">// Alias our output type -- A dictionary mapping a path name to a list of </span>
<span class="rem">// control points.</span>
<span class="kwrd">using</span> PathDict = System.Collections.Generic.Dictionary&lt;<span class="kwrd">string</span>,
    System.Collections.Generic.List&lt;Microsoft.Xna.Framework.Vector2&gt;&gt;;

<span class="rem">// The Svg library uses this matrix type not the XNA one:</span>
<span class="kwrd">using</span> Matrix = System.Drawing.Drawing2D.Matrix;

<span class="kwrd">namespace</span> ContentPipeline {

    [ContentImporter(<span class="str">".svg"</span>, DisplayName = <span class="str">"SVG Path Importer"</span>)]
    <span class="kwrd">public</span> <span class="kwrd">class</span> SvgPathImporter : ContentImporter&lt;PathDict&gt; {

        <span class="rem">// (I like to stash the filename at the start of my content
        // importers to make it easier to generate useful error messages in
        // nested calls.)</span>
        <span class="kwrd">string</span> filename;

        <span class="kwrd">public</span> <span class="kwrd">override</span> PathDict Import(
                <span class="kwrd">string</span> filename,
                ContentImporterContext context) {

            <span class="kwrd">this</span>.filename = filename;
</pre>
<p>Now for the interesting stuff. Opening and parsing the SVG file is as simple as a single function call:</p>
<pre class="csharpcode">            SvgDocument doc = SvgDocument.Open(filename);
</pre>
<p>Now we scan for path nodes and pull out the data. The collectPaths() function is defined later on.</p>
<pre class="csharpcode">            <span class="kwrd">var</span> paths = <span class="kwrd">new</span> PathDict();
            collectPaths(<span class="kwrd">ref</span> paths, doc);
            <span class="kwrd">return</span> paths;
        }
</pre>
<p>We&#8217;re only going to consider Bézier curves. Here&#8217;s how we test for them:</p>
<pre class="csharpcode">        <span class="kwrd">static</span> <span class="kwrd">bool</span> isBezier(SvgPath path) {
            <span class="rem">// All but the first point must be type '3'</span>
            <span class="kwrd">const</span> <span class="kwrd">int</span> bezierPointId = 3;
            <span class="kwrd">return</span> path.Path.PathTypes.Skip(1).All(t =&gt; t == bezierPointId);
        }
</pre>
<p>SVG nodes may have multiple associated transforms. (This is one of the things that makes it impractical to throw together a quick parser for this stuff &#8212; There are <a title="SVG spec" href="http://www.w3.org/TR/SVG11/coords.html#TransformAttribute" onclick="pageTracker._trackPageview('/outgoing/www.w3.org/TR/SVG11/coords.html_TransformAttribute?referer=');">many ways</a> of representing transformations in SVG, and unless you know that your editor won&#8217;t use them, even the simplest SVG data is affected by them.) Fortunately SVGRE parses these for us and stores them with each node. The parsed points are stored untransformed though, so we need to accumulate the node&#8217;s transforms and apply them ourselves. Here&#8217;s a function to calculate the overall transform that applies to a node.</p>
<pre class="csharpcode">        <span class="kwrd">static</span> Matrix getTransform(SvgElement node) {
            <span class="kwrd">var</span> m = <span class="kwrd">new</span> Matrix();
            <span class="kwrd">while</span> (node != <span class="kwrd">null</span>) {
                <span class="kwrd">if</span> (node.Transforms != <span class="kwrd">null</span> &amp;&amp; node.Transforms.Count &gt; 0) {
                    <span class="kwrd">var</span> temp = <span class="kwrd">new</span> Matrix();
                    <span class="kwrd">foreach</span> (<span class="kwrd">var</span> trans <span class="kwrd">in</span> node.Transforms) {
                        temp.Multiply(trans.Matrix, MatrixOrder.Prepend);
                    }
                    m.Multiply(temp, MatrixOrder.Append);
                }
                node = node.Parent;
            }
            <span class="kwrd">return</span> m;
        }
</pre>
<p>We traverse the tree of SvgElement nodes recursively. If a node is of type SvgPath, and is a Bézier curve, then we process it and add it to our dictionary.</p>
<pre class="csharpcode">        <span class="kwrd">void</span> collectPaths(<span class="kwrd">ref</span> PathDict paths, SvgElement e) {
            SvgPath path = e <span class="kwrd">as</span> SvgPath;
            <span class="kwrd">if</span> (path != <span class="kwrd">null</span>) {
                <span class="kwrd">if</span> (!isBezier(path)) {
                    <span class="rem">// Complain about any non-Bezier curves</span>
                    <span class="rem">// so we can fix them in the editor.</span>
                    <span class="kwrd">throw</span> <span class="kwrd">new</span> InvalidContentException(
                        String.Format(<span class="str">"Cubic Beziers only: {0}"</span>, path.ID),
                        <span class="kwrd">new</span> ContentIdentity(filename));
                }
                <span class="rem">// Transform points in-place.</span>
                <span class="kwrd">var</span> pts = path.Path.PathPoints;
                getTransform(e).TransformPoints(pts);
                <span class="rem">// Convert to Vector2.</span>
                <span class="kwrd">var</span> vpts = pts.Select(p =&gt; <span class="kwrd">new</span> Vector2(p.X, p.Y)).ToList();
                paths.Add(path.ID, vpts);
            }

            <span class="kwrd">foreach</span> (<span class="kwrd">var</span> child <span class="kwrd">in</span> e.Children) {
                 collectPaths(<span class="kwrd">ref</span> paths, child);
            }
        }

    }
}
</pre>
<p>That&#8217;s it. To load the path data in-game we use:</p>
<pre class="csharpcode"><span class="kwrd">var</span> paths = Content.Load&lt;Dictionary&lt;<span class="kwrd">string</span>, List&lt;Vector2&gt;&gt;&gt;(<span class="str">"test"</span>);
</pre>
<p>Here&#8217;s <a title="SvgImporter.cs" href="/wp-content/uploads/2010/04/SvgImporter.cs">the code in one handy chunk</a>.</p>
<p>The same approach can be used to extract other SVG data. In fact in the Retrofit:Overload importer we look for a screen rectangle too and offset the paths relative to that.</p>
<p>The main disadvantage of SVG Rendering Engine is the lack of documentation, however it is possible to work out quite a lot by loading a simple file and browsing the object structure in the debugger.</p>
<p>You can find the game on the Xbox Dashboard (Game Marketplace -&gt; Indie Games) or <a title="Retrofit: Overload on Xbox.com" href="http://marketplace.xbox.com/games/media/66acd000-77fe-1000-9115-d802585504e5/" onclick="pageTracker._trackPageview('/outgoing/marketplace.xbox.com/games/media/66acd000-77fe-1000-9115-d802585504e5/?referer=');">schedule a download via the web</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://wam-games.com/2010/04/svg-in-xna/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>

