<?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>Milkstone Studios &#187; XNA Programming</title>
	<atom:link href="http://www.milkstonestudios.com/category/3_development/xna-programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.milkstonestudios.com</link>
	<description>Independent Game Developers</description>
	<lastBuildDate>Sat, 12 Jun 2010 11:34:26 +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>Creating 2D geometry from a line strip</title>
		<link>http://www.milkstonestudios.com/2009/03/creating-2d-geometry-from-a-line-strip/</link>
		<comments>http://www.milkstonestudios.com/2009/03/creating-2d-geometry-from-a-line-strip/#comments</comments>
		<pubDate>Tue, 24 Mar 2009 21:45:30 +0000</pubDate>
		<dc:creator>WaaghMan</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[XNA Programming]]></category>
		<category><![CDATA[3d]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[xna]]></category>

		<guid isPermaLink="false">http://www.milkstonestudios.com/?p=119</guid>
		<description><![CDATA[During development of an old prototype that never saw the light, I found the need to create some 2D geometry from a set of points that form a line.
After searching for any algorithm that could make what I needed, with no success, I decided to develop my own.
The algorithm is very simple, it just takes [...]]]></description>
			<content:encoded><![CDATA[<p>During development of an old prototype that never saw the light, I found the need to create some 2D geometry from a set of points that form a line.</p>
<p>After searching for any algorithm that could make what I needed, with no success, I decided to develop my own.</p>
<p>The algorithm is very simple, it just takes a rope and a width and creates a set of vertices tha form a TriangleStrip to be rendered on screen.</p>
<pre class="brush: csharp">

        /// &lt;summary&gt;
        /// Triangulates a line, returning a list of vertices that form a triangle strip
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;contour&quot;&gt;List of points that form the line&lt;/param&gt;
        /// &lt;param name=&quot;width&quot;&gt;Desired with of the geometry&lt;/param&gt;
        /// &lt;returns&gt;list of vertices that form a triangle strip&lt;/returns&gt;
        public static List&lt;Vector2&gt; Process(ReadOnlyCollection&lt;Vector2&gt; contour,float width)
        {
            List&lt;Vector2&gt; result = new List&lt;Vector2&gt;();
            Vector2 normal = Vector2.Zero ;
            // Generate vertices for each point in the line
            for (int i = 0; i &lt; contour.Count; i++)
            {
                // First and last are special cases, the normal is calculated right away
                if (i == contour.Count - 1)
                    normal = GameMath.Normal(contour[i] - contour[i - 1]);
                else if (i == 0)
                    normal = GameMath.Normal(contour[1] - contour[0]);
                else
                {
                    // For the rest of points, determine the normal as the middle angle between the direction of the previous and next segments.
                    Vector2 delta1 = contour[i] - contour[i - 1];
                    Vector2 delta2 = contour[i + 1] - contour[i];
                    if ((delta1.Length()!=0)&amp;amp;&amp;amp;(delta2.Length()!=0))
                    {
                        delta1.Normalize();
                        delta2.Normalize();
                        normal = GameMath.Normal(delta1 + delta2);
                    }

                }
                // Add two vertices to the vertex list
                result.Add(contour[i] - normal * width / 2f);
                result.Add(contour[i] + normal * width / 2f);
            }
            return result;

        }
        /// &lt;summary&gt;
        /// Returns one normal of the 2D vector
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;vector&quot;&gt;&lt;/param&gt;
        /// &lt;returns&gt;&lt;/returns&gt;
        public static Vector2 Normal(Vector2 line)
        {
            float x = line.X; float y = line.Y;
            Vector2 normal = new Vector2();
            if (x != 0)
            {
                normal.X = -y / x;
                normal.Y = 1 / (float)Math.Sqrt(normal.X * normal.X + 1);
                normal.Normalize();
            }
            else if (y != 0)
            {
                normal.Y = 0;
                normal.X = (line.Y&lt;0) ? 1 : -1;
            }
            else
            {
                normal.X = 1;
                normal.Y = 0;
            }
            if (x &lt; 0)
            {
                normal *= -1;
            }
            return normal;
        }
</pre>
<p>That code is well enough to generate a credible line in almost all situations. As for UVs, what I did was assign the even vertices the value (X,0) and odd vertices (X,1), where X is the distance elapsed since the line start.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.milkstonestudios.com/2009/03/creating-2d-geometry-from-a-line-strip/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Problems with System.Random</title>
		<link>http://www.milkstonestudios.com/2009/03/problems-with-systemrandom/</link>
		<comments>http://www.milkstonestudios.com/2009/03/problems-with-systemrandom/#comments</comments>
		<pubDate>Wed, 11 Mar 2009 08:05:39 +0000</pubDate>
		<dc:creator>WaaghMan</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[XNA Programming]]></category>
		<category><![CDATA[csharp]]></category>

		<guid isPermaLink="false">http://www.milkstonestudios.com/?p=69</guid>
		<description><![CDATA[Yesterday I ran a profiling tool (CLR Profiler) to see how Little Racers does with heap memory allocation, and found something strange: The log showed that class System.Random allocated around 88Mbytes of RAM during the whole execution.
The reason of this is the way the particle system works: Each particle stores only a seed to generate [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday I ran a profiling tool (CLR Profiler) to see how Little Racers does with heap memory allocation, and found something strange: The log showed that class System.Random allocated around 88Mbytes of RAM during the whole execution.</p>
<p>The reason of this is the way the particle system works: Each particle stores only a seed to generate its random values, and a new Random is generated with that seed each time it needs to be updated (that is, on every frame). So, if we&#8217;re showing 50 particles on a frame, we&#8217;re calling new Random(seed) 50 times.</p>
<p>With System.Random, there was no possible workaround since it doesn&#8217;t have a method available to set the seed without recreating everything. Also, it seems to do some hard work on creation, maybe allocating the N next random numbers, I don&#8217;t know.</p>
<p>Anyway, I found a nice replacement called <a href="http://www.codeproject.com/KB/cs/fastrandom.aspx" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.codeproject.com');">FastRandom</a>:  This class has the same interface that System.Random, so they can be easily interchanged. Also, it has a method to set the seed without having to recreate the class.</p>
<p>Anyway, I found a bug in the provided code, related with the methods Next(int) and Next(int,int) , it seems to store a double with the minimum value of an int and then multiply it by the desired range. It didn&#8217;t work for me, so I just replaced it with a modulus. Maybe it&#8217;s a little slower, but it works perfectly.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.milkstonestudios.com/2009/03/problems-with-systemrandom/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Plain XML content in XNA Content Pipeline</title>
		<link>http://www.milkstonestudios.com/2009/03/plain-xml-content-in-xna-content-pipeline/</link>
		<comments>http://www.milkstonestudios.com/2009/03/plain-xml-content-in-xna-content-pipeline/#comments</comments>
		<pubDate>Mon, 02 Mar 2009 22:26:35 +0000</pubDate>
		<dc:creator>WaaghMan</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[XNA Programming]]></category>
		<category><![CDATA[content]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[xna]]></category>

		<guid isPermaLink="false">http://ns355097.ovh.net/gamelicious/?p=45</guid>
		<description><![CDATA[During the first stages of Little Racers development, I found the need of parsing XML data during the game load. The XNA Content Pipeline system includes XML Parsing, but it uses it in a very particular way. I won&#8217;t go much further in the way XNA parses XML because, to be sincere, I&#8217;ve not tried [...]]]></description>
			<content:encoded><![CDATA[<p>During the first stages of Little Racers development, I found the need of parsing XML data during the game load. The XNA Content Pipeline system includes XML Parsing, but it uses it in a very particular way. I won&#8217;t go much further in the way XNA parses XML because, to be sincere, I&#8217;ve not tried it enough to test wether it&#8217;s good or not.<br />
My first impression was that it was too strict (probably due to optimization and type safe issues), moreover during parse I would need access to some data structures that only were available at runtime, so I preferred to store plain XML and parse it during game load.<br />
This can be done by extending the content pipeline and adding a new type. To do this we&#8217;ll need to add two new projects to our solution.</p>
<h1>XmlSource project</h1>
<p>The first project we&#8217;ll create is the one which has the class that stores our xml plain text. I&#8217;ve called it XmlSource.It will have two classes in total.</p>
<h2>XmlSource</h2>
<p>This class is a very simple one. It simply has a string with the xml plain text:</p>
<pre class="brush: csharp">

public class XmlSource
{
public XmlSource(string xmlCode)
{
this.xmlCode = xmlCode;
}

private string xmlCode;
public string XmlCode { get { return xmlCode; } }
}
</pre>
<h2>XmlSourceReader</h2>
<p>This class extends ContentTypeReader and simply creates an XmlSource instance from its binary representation. In our case, the binary representation wil be a simple string, so the read is straightforward.</p>
<pre class="brush: csharp">

public class XmlSourceReader : ContentTypeReader&lt;XmlSource&gt;
{
/// &lt;summary&gt;
/// Loads an imported shader.
/// &lt;/summary&gt;
protected override XmlSource Read(ContentReader input, XmlSource existingInstance)
{
string xmlData = input.ReadString();

return new XmlSource(xmlData);
}
}
</pre>
<p>With this, our first project is finished. This project is platform-independant (The Xbox needs to read the binary data) and will have to be referenced from every project that uses the XmlSource class, including the second project we&#8217;re making:</p>
<h1>XmlSourceImporter</h1>
<p>The second project(I called it XmlDocumentImporter) is a project of type &#8220;Content Pipeline Extension Library&#8221;. This project is referenced from the Content projects, and converts the source text file to binary data that will be saved in the .xnb files.</p>
<p>This project has 2 files:</p>
<h2>XmlSourceImporter.cs</h2>
<p>This file converts the XML source file into an XmlSource instance, that will be further converted into binary data</p>
<pre class="brush: csharp">

[ContentImporter(&quot;.xml&quot;, DisplayName = &quot;Xml Source Importer&quot;)]
class XmlSourceImporter : ContentImporter&lt;XmlSource&gt;
{
public override XmlSource Import(string filename, ContentImporterContext context)
{
string sourceCode = System.IO.File.ReadAllText(filename);
return new XmlSource(sourceCode);
}
}
</pre>
<h2>XmlSourceWriter.cs</h2>
<p>This file converts an XmlSource into binary data, as opposed to XmlSourceReader</p>
<pre class="brush: csharp">

[ContentTypeWriter]
class XmlSourceWriter : ContentTypeWriter&lt;XmlSource&gt;
{
protected override void Write(ContentWriter output, XmlSource value)
{
/*StringWriter sw=new StringWriter();
value.Save(sw);
string content = sw.ToString();*/
output.Write(value.XmlCode);
}
public override string GetRuntimeType(TargetPlatform targetPlatform)
{
return typeof(XmlDocument).AssemblyQualifiedName;
}
public override string GetRuntimeReader(TargetPlatform targetPlatform)
{
return typeof(XmlDocumentReader).AssemblyQualifiedName;
}
}
</pre>
<h1>Sample of use</h1>
<p>Once both projects set up (remember to add reference to XmlSourceImporter from the Content project), we can add .xml files to the content project. Under &#8220;Content importer&#8221;, we&#8217;ll need to select &#8220;Xml Source importer&#8221;, instead of the default XNA one.</p>
<p>Once done this, the program sould compile ok. Loading XML from the program now is very easy:</p>
<pre class="brush: csharp">

XmlSource xs = Content.Load&lt;XmlSource&gt;(AssetName);
XmlDocument xd = new XmlDocument();
xd.LoadXml(xs.XmlCode);
</pre>
<p>I know this can be a little confusing, i&#8217;ll upload some binaries if someone asks for them.</p>
<p>PS: Here they are: <a href="http://www.milkstonestudios.com/wp-content/uploads/2009/03/xmlsource.zip" onclick="javascript:pageTracker._trackPageview('/downloads/wp-content/uploads/2009/03/xmlsource.zip');">Plain Xml Content Importer</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.milkstonestudios.com/2009/03/plain-xml-content-in-xna-content-pipeline/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
