<?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; Code</title>
	<atom:link href="http://wam-games.com/category/code/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>Curing the Clicks &#8211; Release Curves in XACT</title>
		<link>http://wam-games.com/2010/04/release-curves-xact/</link>
		<comments>http://wam-games.com/2010/04/release-curves-xact/#comments</comments>
		<pubDate>Thu, 22 Apr 2010 18:23:21 +0000</pubDate>
		<dc:creator>Martin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Retrofit series]]></category>
		<category><![CDATA[Xbox]]></category>
		<category><![CDATA[overload]]></category>
		<category><![CDATA[Sound]]></category>
		<category><![CDATA[xact]]></category>

		<guid isPermaLink="false">http://wam-games.com/?p=433</guid>
		<description><![CDATA[A step by step guide to adding a release curve to a sound in XACT.]]></description>
			<content:encoded><![CDATA[<p>Once I&#8217;d worked out that the clicking noises I was getting in <a title="Retrofit posts" href="/category/retrofit-series/">Retrofit:Overload</a> were not clipping but were caused by stopping the sounds, I expected it to be easy to fix in <a title="XACT on MSDN" href="http://msdn.microsoft.com/en-us/library/ee415964%28VS.85%29.aspx" onclick="pageTracker._trackPageview('/outgoing/msdn.microsoft.com/en-us/library/ee415964_28VS.85_29.aspx?referer=');">XACT</a>. It was, really, but only after some puzzling over the MSDN docs and a false start or two.</p>
<p>To save others similar confusion, here&#8217;s a step by step guide to adding a release curve to a sound in XACT&#8230;<br />
<span id="more-433"></span></p>
<ul>
<li>In XACT, create a new RPC Preset. This brings up the RPC editor.</li>
<li>Set <em>Variable</em> to <em>ReleaseTime.</em></li>
<li>Set <em>Object</em> to <em>Track 1.</em></li>
<li>Set <em>Parameter</em> to <em>Volume</em>.</li>
<li>Now draw your release curve. To just prevent clicks, rather than create an audible fade out, it can be really short. I used half a second but it could probably be shorter. So at time 0 Volume is 0dB, and at 0.5 seconds Volume is -96dB.</li>
<li>Give your RPC an informative name, E.g. &#8220;ReleaseRPC&#8221;.</li>
<li>Now find the relevant sounds (not cues) in your Sound Bank and attach the RPC. (For single sounds you can do this on the sound&#8217;s right-click menu. Alternatively you can drag sounds onto your RPC entry in the project tree and this works with multiple sounds at once.)</li>
<li>Thats it for XACT. Save your project <img src='http://wam-games.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </li>
</ul>
<p><a href="http://wam-games.com/wp-content/uploads/2010/04/release-curves-xact.png"><img class="alignnone size-full wp-image-440" title="release-curves-xact" src="http://wam-games.com/wp-content/uploads/2010/04/release-curves-xact.png" alt="" width="550" height="377" /></a></p>
<p>To use the release when the sound is stopped, it must be stopped  &#8220;as authored&#8221;.</p>
<ul>
<li>With XNA: <code>cue.Stop(AudioStopOptions.AsAuthored);</code></li>
<li>With DirectX: <code>cue-&gt;Stop(0);</code></li>
</ul>
<p>There&#8217;s no need to create a new XACT Variable to drive the RPC. The ReleaseTime variable is automatically available on each playing cue.</p>
<p>The <a title="MSDN" href="http://msdn.microsoft.com/en-us/library/ee416028%28VS.85%29.aspx" onclick="pageTracker._trackPageview('/outgoing/msdn.microsoft.com/en-us/library/ee416028_28VS.85_29.aspx?referer=');">MSDN  entry on XACT envelope parameters</a> has much more to say about this. It does actually make sense if you don&#8217;t let the jargon distract you.</p>
]]></content:encoded>
			<wfw:commentRss>http://wam-games.com/2010/04/release-curves-xact/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<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>
		<item>
		<title>Easy loading bar for XNA</title>
		<link>http://wam-games.com/2009/03/easy-loading-bar-for-xna/</link>
		<comments>http://wam-games.com/2009/03/easy-loading-bar-for-xna/#comments</comments>
		<pubDate>Thu, 26 Mar 2009 09:19:20 +0000</pubDate>
		<dc:creator>Martin</dc:creator>
				<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://wam-games.com/?p=449</guid>
		<description><![CDATA[Here&#8217;s the loading-bar system that I wrote for Pegzo (also used now in Retrofit:Overload, although that loads so fast it doesn&#8217;t really need it).
Loading bars seem to be rare in community games and I think the approach  I took is simpler than is often advised. This system is single-threaded and revolves around an enumerator [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s the loading-bar system that I wrote for <a title="Pegzo game information" href="/games/pegzo/">Pegzo</a> (also used now in <a title="Retrofit posts" href="/category/retrofit-series/">Retrofit:Overload</a>, although that loads so fast it doesn&#8217;t really need it).</p>
<p>Loading bars seem to be rare in community games and I think the approach  I took is simpler than is often advised. This system is single-threaded and revolves around an enumerator function  that looks something like this:</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">    thing = content.Load&lt;Thing&gt;(<span class="str">"thing"</span>);
    <span class="kwrd">yield</span> <span class="kwrd">return</span> progress();  

    levelNames = content.Load&lt;LevelNames&gt;(<span class="str">"level-names"</span>);
    <span class="kwrd">yield</span> <span class="kwrd">return</span> progress();  

    <span class="kwrd">foreach</span> (<span class="kwrd">string</span> name <span class="kwrd">in</span> levelNames) {
        levels.Add(content.Load&lt;Level&gt;(levelName));
        <span class="kwrd">yield</span> <span class="kwrd">return</span> progress();
    }  

    // etc.</pre>
<p>You can download the source from here: <a title="EasyLoadingBar.zip" href="/downloads/EasyLoadingBar.zip">Download EasyLoadingBar</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://wam-games.com/2009/03/easy-loading-bar-for-xna/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
