<?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>The Blog of Zachary Snow &#187; XNA</title>
	<atom:link href="http://zacharysnow.net/category/net/xna/feed/" rel="self" type="application/rss+xml" />
	<link>http://zacharysnow.net</link>
	<description></description>
	<lastBuildDate>Tue, 20 Dec 2011 19:41:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Xna: Load Texture2D from Embedded Resource</title>
		<link>http://zacharysnow.net/2010/07/03/xna-load-texture2d-from-embedded-resource/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=xna-load-texture2d-from-embedded-resource</link>
		<comments>http://zacharysnow.net/2010/07/03/xna-load-texture2d-from-embedded-resource/#comments</comments>
		<pubDate>Sat, 03 Jul 2010 10:46:07 +0000</pubDate>
		<dc:creator>Zachary</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[XNA]]></category>

		<guid isPermaLink="false">http://zacharysnow.net/?p=338</guid>
		<description><![CDATA[If you&#8217;re writing an app which uses Xna, you may need to load a texture from an embedded resource. Here&#8217;s how: First embed the resource in your app. Do so by choosing Embedded Resource as the Build Action in the &#8230; <a href="http://zacharysnow.net/2010/07/03/xna-load-texture2d-from-embedded-resource/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re writing an app which uses Xna, you may need to load a texture from an embedded resource. Here&#8217;s how:</p>
<p>First embed the resource in your app. Do so by choosing <strong>Embedded Resource</strong> as the <strong>Build Action</strong> in the properties of the resource.</p>
<p><a href="http://zacharysnow.net/wp-content/uploads/2010/07/embed-resource.png"><img src="http://zacharysnow.net/wp-content/uploads/2010/07/embed-resource.png" alt="Properties Dialog for a File" title="embed-resource" width="281" height="175" class="alignnone size-full wp-image-339" /></a></p>
<p>After that you can load the <strong>Texture2D</strong> using a stream handle to the embedded file.</p>
<pre name="code" class="c#">
Stream stream = Assembly.GetCallingAssembly().GetManifestResourceStream("AppNamespace.Folder.font.bmp");
return Texture2D.FromFile(graphicsDevice, stream);
</pre>
<p><strong>GetCallingAssembly()</strong> can be exchanged with <strong>GetExecutingAssembly()</strong> if needed. The name of the resource must be fully qualified with the app&#8217;s namespace and folders. I usually keep my resources in a folder <strong>Resources</strong> so I would have: AppNamespace.Resources.font.bmp.</p>
]]></content:encoded>
			<wfw:commentRss>http://zacharysnow.net/2010/07/03/xna-load-texture2d-from-embedded-resource/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Same Game Xna 2.0 Alpha</title>
		<link>http://zacharysnow.net/2010/06/29/same-game-xna-2-0-alpha/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=same-game-xna-2-0-alpha</link>
		<comments>http://zacharysnow.net/2010/06/29/same-game-xna-2-0-alpha/#comments</comments>
		<pubDate>Tue, 29 Jun 2010 16:31:29 +0000</pubDate>
		<dc:creator>Zachary</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[XNA]]></category>

		<guid isPermaLink="false">http://zacharysnow.net/?p=334</guid>
		<description><![CDATA[I&#8217;ve released what I&#8217;m calling the 2.0 alpha version of Same Game Xna. Codeplex Project Link Release Link]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve released what I&#8217;m calling the 2.0 alpha version of Same Game Xna.</p>
<p><a href="http://zacharysnow.net/wp-content/uploads/2010/06/SameGameXna-2.0-alpha.png"><img src="http://zacharysnow.net/wp-content/uploads/2010/06/SameGameXna-2.0-alpha-150x150.png" alt="" title="SameGameXna-2.0-alpha" width="150" height="150" class="alignnone size-thumbnail wp-image-335" /></a></p>
<p><a href="http://samegamexna.codeplex.com/">Codeplex Project Link</a><br />
<a href="http://samegamexna.codeplex.com/releases/view/47975">Release Link</a></p>
]]></content:encoded>
			<wfw:commentRss>http://zacharysnow.net/2010/06/29/same-game-xna-2-0-alpha/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>500 Downloads of the Same Game</title>
		<link>http://zacharysnow.net/2010/06/09/500-downloads-of-the-same-game/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=500-downloads-of-the-same-game</link>
		<comments>http://zacharysnow.net/2010/06/09/500-downloads-of-the-same-game/#comments</comments>
		<pubDate>Wed, 09 Jun 2010 20:17:28 +0000</pubDate>
		<dc:creator>Zachary</dc:creator>
				<category><![CDATA[XNA]]></category>
		<category><![CDATA[codeplex]]></category>
		<category><![CDATA[SameGameXna]]></category>
		<category><![CDATA[WinForms]]></category>

		<guid isPermaLink="false">http://zacharysnow.net/2010/06/09/500-downloads-of-the-same-game/</guid>
		<description><![CDATA[My little Xna game that I wrote nearly 2 years ago reached the 500 downloads mark (binaries and source) the other day. With that said, I&#8217;d like to say that I&#8217;m working on version 2.0. In version 2.0 I&#8217;m going &#8230; <a href="http://zacharysnow.net/2010/06/09/500-downloads-of-the-same-game/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>My little Xna game that I wrote nearly 2 years ago reached the 500 downloads mark (binaries and source) the other day. With that said, I&#8217;d like to say that I&#8217;m working on version 2.0.</p>
<p>In version 2.0 I&#8217;m going to make the code more event driven. The old code uses the Xna Game class and in the new version I&#8217;ll be making it WinForms based. Almost a complete rewrite.</p>
<p>My work so far is available through SVN on the project&#8217;s <a href="http://samegamexna.codeplex.com/">Codeplex page</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://zacharysnow.net/2010/06/09/500-downloads-of-the-same-game/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Drawing Rectangles with SpriteBatch</title>
		<link>http://zacharysnow.net/2010/03/29/drawing-rectangles-with-spritebatch/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=drawing-rectangles-with-spritebatch</link>
		<comments>http://zacharysnow.net/2010/03/29/drawing-rectangles-with-spritebatch/#comments</comments>
		<pubDate>Mon, 29 Mar 2010 17:14:25 +0000</pubDate>
		<dc:creator>Zachary</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[XNA]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://zacharysnow.net/2010/03/29/drawing-rectangles-with-spritebatch/</guid>
		<description><![CDATA[Just a quick code snippet which adds an extension method for drawing Rectangles to SpriteBatch: public static class SpriteBatchHelper { static Texture2D pixel; private static void LoadPixel(GraphicsDevice graphicsDevice) { if(pixel == null) { pixel = new Texture2D(graphicsDevice, 1, 1); pixel.SetData&#60;Color&#62;(new &#8230; <a href="http://zacharysnow.net/2010/03/29/drawing-rectangles-with-spritebatch/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Just a quick code snippet which adds an extension method for drawing Rectangles to SpriteBatch:</p>
<pre name="code" class="c#">
public static class SpriteBatchHelper
{
	static Texture2D pixel;

	private static void LoadPixel(GraphicsDevice graphicsDevice)
	{
		if(pixel == null)
		{
			pixel = new Texture2D(graphicsDevice, 1, 1);
			pixel.SetData&lt;Color&gt;(new Color[] { Color.White });
		}
	}

	public static void DrawRectangle(this SpriteBatch spriteBatch, Rectangle rectangle, Color color)
	{
		LoadPixel(spriteBatch.GraphicsDevice);
		spriteBatch.Draw(pixel, rectangle, color);
	}
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://zacharysnow.net/2010/03/29/drawing-rectangles-with-spritebatch/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating and consuming services in your XNA Game</title>
		<link>http://zacharysnow.net/2010/02/18/creating-and-consuming-services-in-your-xna-game/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=creating-and-consuming-services-in-your-xna-game</link>
		<comments>http://zacharysnow.net/2010/02/18/creating-and-consuming-services-in-your-xna-game/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 20:26:13 +0000</pubDate>
		<dc:creator>Zachary</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[XNA]]></category>

		<guid isPermaLink="false">http://zacharysnow.net/?p=290</guid>
		<description><![CDATA[The GameServiceContainer implements the IServiceProvider interface and the MSDN documentation says about the IServiceProvider interface: Defines a mechanism for retrieving a service object; that is, an object that provides custom support to other objects. This article will &#8220;attempt&#8221; to describe &#8230; <a href="http://zacharysnow.net/2010/02/18/creating-and-consuming-services-in-your-xna-game/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.gameservicecontainer.aspx">GameServiceContainer</a> implements the <a href="http://msdn.microsoft.com/en-us/library/system.iserviceprovider.aspx">IServiceProvider</a> interface and the MSDN documentation says about the IServiceProvider interface:</p>
<blockquote><p>Defines a mechanism for retrieving a service object; that is, an object that provides custom support to other objects.</p></blockquote>
<p>This article will &#8220;<em>attempt</em>&#8221; to describe how can you use the GameServiceContainer in your XNA game, in both your GameComponent(s) and your game&#8217;s entity objects.<br />
<span id="more-290"></span><br />
The most obvious place to use the GameServiceContainer is in your GameComponent(s). But first, lets talk about &#8220;<a href="http://en.wikipedia.org/wiki/Coupling_(computer_science)">Coupling</a>&#8220;. Let&#8217;s assume you have the following components:</p>
<pre name="code" class="c#">
class FooComponent : GameComponent
{
	public FooComponent(Game game)
		: base(game)
	{
	}

	public int DoFoo()
	{
		// Do something and return an int.
	}
}

class BarComponent : GameComponent
{
	FooComponent foo;

	public BarComponent(Game game)
		: base(game)
	{
		this.foo = new FooComponent(game);
	}

	public void DoBar()
	{
		int result = this.foo.DoFoo();
		// Do something based on result.
	}
}
</pre>
<p>There&#8217;s nothing wrong with the code, but BarComponent has a dependency on FooComponent. BarComponent directly interacts with FooComponent and therefore any change made to FooComponent indirectly affects BarComponent. For instance, let&#8217;s assume the constructor for FooComponent needs to be modified. That means we now have to update not only the FooComponent class but as well the BarComponent class. Throw in a few more components with dependencies on FooComponent and you could start to get headache really fast. This design is highly coupled.</p>
<p>Let&#8217;s try a slight redesign:</p>
<pre name="code" class="c#">
class FooComponent : GameComponent
{
	public FooComponent(Game game)
		: base(game)
	{
	}

	public int DoFoo()
	{
		// Do something and return an int.
	}
}

class BarComponent : GameComponent
{
	FooComponent foo;

	public BarComponent(Game game, FooComponent foo)
		: base(game)
	{
		this.foo = foo;
	}

	public void DoBar()
	{
		int result = this.foo.DoFoo();
		// Do something based on result.
	}
}
</pre>
<p>We&#8217;ve now eliminated the construction of the FooComponent from within the BarComponent. The design is better but still not that great. BarComponent is still directly relying on and communicating with FooComponent. We want to change BarComponent so that it has no direct dependency on a concrete implementation of FooComponent. We&#8217;ll create an interface:</p>
<pre name="code" class="c#">
interface IFooService
{
	int DoFoo();
}

class FooComponent : GameComponent, IFooService
{
	public FooComponent(Game game)
		: base(game)
	{
	}

	public int DoFoo()
	{
		// Do something and return an int.
	}
}

class BarComponent : GameComponent
{
	IFooService foo;

	public BarComponent(Game game, IFooService foo)
		: base(game)
	{
		this.foo = foo;
	}

	public void DoBar()
	{
		int result = this.foo.DoFoo();
		// Do something based on result.
	}
}
</pre>
<p>We can now change FooComponent as much as we want and BarComponent will be unaffected. BarComponent now communicates with FooComponent through the IFooService interface. This also allows us to have multiple implementations of DoFoo():</p>
<pre name="code" class="c#">
class SimpleFooComponent : GameComponent, IFooService
{
	public SimpleFooComponent(Game game)
		: base(game)
	{
	}

	public int DoFoo()
	{
		return 5; // The class says "Simple"
	}
}

class ComplexFooComponent : GameComponent, IFooService
{
	public ComplexFooComponent(Game game)
		: base(game)
	{
	}

	public int DoFoo()
	{
		int result = 0;
		// Do some very complex calculation
		return result;
	}
}
</pre>
<p>We can pass BarComponent an instance of SimpleFooComponent or ComplexFooComponent. Whatever the situation may call for.</p>
<p>Where does GameServiceContainer fit into all of this? You can use the GameServiceContainer to hold all your &#8220;Services&#8221;. Add whatever class will implement the IFooService and then from within your BarComponent you can query for it:</p>
<pre name="code" class="c#">
class BarComponent : GameComponent
{
	IFooService foo;

	public BarComponent(Game game)
		: base(game)
	{
	}

	public override void Initialize()
	{
		this.foo = this.Game.Services.GetService(typeof(IFooService)) as IFooService;

		if(this.foo == null)
			throw new InvalidOperationException("IFooService not found.");
	}

	public void DoBar()
	{
		int result = this.foo.DoFoo();
		// Do something based on result.
	}
}

// In your Game's constructor.
this.Services.AddService(typeof(IFooService), new SimpleFooComponent(this));
</pre>
<p>Not only does BarComponent no longer require an instance of IFooService in its constructor, it also no longer matters if the instance of IFooService is constructed before or after the BarComponent. So long as all the services BarComponent requires are in the GameServiceContainer before Initialize() is called, it doesn&#8217;t matter what order your components are constructed in. Now, suppose that BarComponent didn&#8217;t necessarily depend on IFooService and instead the behavior of DoBar() is changed based on whether or not IFooService is available:</p>
<pre name="code" class="c#">
class BarComponent : GameComponent
{
	IFooService foo;

	public BarComponent(Game game)
		: base(game)
	{
	}

	public override void Initialize()
	{
		this.foo = this.Game.Services.GetService(typeof(IFooService)) as IFooService;
	}

	public intDoBar()
	{
		// If the IFooService is available, delegate to the DoFoo() method.
		if(this.foo != null)
			return this.foo.DoFoo();

		int result = 0;
		// Otherwise do some other calculation.
		return result;
	}
}
</pre>
<p>Service providers don&#8217;t always have to be GameComponent(s). Our BarComponent needs a Camera class now:</p>
<pre name="code" class="c#">
interface ICamera
{
	Matrix Transform { get; }
}

class IdentityCamera : ICamera
{
	public Matrix Transform
	{
		get { return Matrix.Identity; }
	}
}

class MovingCamera : ICamera
{
	public Matrix Transform
	{
		get;
		set;
	}
}

class BarComponent : DrawableGameComponent
{
	ICamera camera;

	public BarComponent(Game game)
		: base(game)
	{
	}

	public override void Initialize()
	{
		this.camera = this.Game.Services.GetService(typeof(ICamera)) as ICamera;
	}

	public override void Draw(GameTime gameTime)
	{
		Matrix transform = this.camera.Transform;
		// Draw based on the transform matrix
	}
}

// In your Game's constructor.
this.Services.AddService(typeof(ICamera), new MovingCamera());
</pre>
<p>BarComponent uses the camera&#8217;s Transform matrix and doesn&#8217;t care how it is calculated. It&#8217;s completely decoupled from the camera&#8217;s implementation.</p>
<p>In closing, using the GameServiceContainer and interfaces makes your classes more loosely coupled. This makes it easier to make changes to the way your game works. Your classes also become more reusable as you can now mix and match service providers and consumers as needed. If you need a specific implementation of a camera for your game, you can still use the BarComponent so long as your camera class implements the ICamera interface.</p>
<p>Loosely coupling your classes has the added benefit of making them more testable. That&#8217;s another blog post though.</p>
]]></content:encoded>
			<wfw:commentRss>http://zacharysnow.net/2010/02/18/creating-and-consuming-services-in-your-xna-game/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>First Look at Map Editor</title>
		<link>http://zacharysnow.net/2009/07/13/first-look-at-map-editor/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=first-look-at-map-editor</link>
		<comments>http://zacharysnow.net/2009/07/13/first-look-at-map-editor/#comments</comments>
		<pubDate>Mon, 13 Jul 2009 19:59:37 +0000</pubDate>
		<dc:creator>Zachary</dc:creator>
				<category><![CDATA[WinForms]]></category>
		<category><![CDATA[XNA]]></category>

		<guid isPermaLink="false">http://zacharysnow.wordpress.com/?p=251</guid>
		<description><![CDATA[I&#8217;ve been working on a map editor for what seem likes forever now. I&#8217;ve gotten so much done and then I would just feel like the thing was written poorly and so I would completely reorganize it. I&#8217;ve done that &#8230; <a href="http://zacharysnow.net/2009/07/13/first-look-at-map-editor/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://zacharysnow.files.wordpress.com/2009/07/mapeditor.jpg"><img src="http://zacharysnow.files.wordpress.com/2009/07/mapeditor.jpg?w=300" alt="MapEditor" title="MapEditor" width="300" height="182" class="alignleft size-medium wp-image-253" /></a></p>
<p>I&#8217;ve been working on a map editor for what seem likes forever now. I&#8217;ve gotten so much done and then I would just feel like the thing was written poorly and so I would completely reorganize it. I&#8217;ve done that about 3 times now.</p>
<p>Finally though I have an MVC pattern going in the code and I&#8217;m very comfortable with what I&#8217;ve written. I&#8217;m not sure when I&#8217;ll be finished but I should have something workable within the next weeks or so.</p>
]]></content:encoded>
			<wfw:commentRss>http://zacharysnow.net/2009/07/13/first-look-at-map-editor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

