<?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>Jason Mitchell &#187; XNA</title>
	<atom:link href="http://jason-mitchell.com/index.php/category/programming/csharp/xna/feed/" rel="self" type="application/rss+xml" />
	<link>http://jason-mitchell.com</link>
	<description></description>
	<lastBuildDate>Thu, 26 Jan 2012 22:31:00 +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>Quick Tip: Fixing Incorrectly Sized Bounding Sphere for 3DS Model in XNA</title>
		<link>http://jason-mitchell.com/xna/quick-tip-fixing-incorrectly-sized-bounding-sphere-for-3ds-model-in-xna/</link>
		<comments>http://jason-mitchell.com/xna/quick-tip-fixing-incorrectly-sized-bounding-sphere-for-3ds-model-in-xna/#comments</comments>
		<pubDate>Mon, 23 Jan 2012 12:00:47 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[Quick Tip]]></category>
		<category><![CDATA[XNA]]></category>
		<category><![CDATA[3D Graphics]]></category>
		<category><![CDATA[quick tip]]></category>

		<guid isPermaLink="false">http://jason-mitchell.com/?p=478</guid>
		<description><![CDATA[Recently I&#8217;ve been spending a lot of time working on a 3D game engine using XNA and wanted to implement some model measuring functionality.  I noticed that my measurements weren&#8217;t coming out quite right so with the help of the App Hub&#8217;s shape renderer sample I was able to visualise the bounding sphere for the model and [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Recently I&#8217;ve been spending a lot of time working on a 3D game engine using XNA and wanted to implement some model measuring functionality.  I noticed that my measurements weren&#8217;t coming out quite right so with the help of the App Hub&#8217;s <a href="http://create.msdn.com/en-US/education/catalog/sample/shape_rendering" target="_blank">shape renderer sample</a> I was able to visualise the bounding sphere for the model and noticed it was significantly smaller than my model.  After some poking around and googling I discovered that <strong>I needed to set the units in my install of 3DS Max to be centimeters</strong> &#8211; once this was done and I exported my model again the bounding spheres were correctly sized and my model measurements were returning the correct results!</p>
]]></content:encoded>
			<wfw:commentRss>http://jason-mitchell.com/xna/quick-tip-fixing-incorrectly-sized-bounding-sphere-for-3ds-model-in-xna/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>XNA and C#: Calling DateTime.Now is Expensive</title>
		<link>http://jason-mitchell.com/xna/xna-and-c-calling-datetime-now-is-expensive/</link>
		<comments>http://jason-mitchell.com/xna/xna-and-c-calling-datetime-now-is-expensive/#comments</comments>
		<pubDate>Sat, 07 Jan 2012 20:39:29 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[XNA]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://jason-mitchell.com/?p=429</guid>
		<description><![CDATA[While working on a new game engine using XNA I decided to run Visual Studios profiling tools against a basic demo game to measure CPU usage.  The results showed that the update process used approximately 65% of the CPU time while the render process used roughly 25%.  This ratio was not what I had expected [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">While working on a new game engine using XNA I decided to run Visual Studios profiling tools against a basic demo game to measure CPU usage.  The results showed that the update process used approximately 65% of the CPU time while the render process used roughly 25%.  This ratio was not what I had expected at all because the updating was fairly simple so I drilled down into report to find the culprit.  It turns out the code using up the most of the CPU time in the update was a call to DateTime.Now to use as a time stamp.</p>
<p><span id="more-429"></span></p>
<p style="text-align: justify;">First I want to provide a bit of context on how DateTime.Now is being called.  My game engine includes a messaging system so that objects can create and publish messages to the game so they can be consumed by subscribed objects.  Each message generates a time stamp for itself using DateTime.Now.  The demo game has been purposefully set up to create 800 objects each of which send out new messages every frame.</p>
<p style="text-align: justify;">The following screenshots are from the profiler run with the DateTime.Now call still in the code.  This first image shows the CPU usage ratio between the full update and draw processes:</p>
<p><a href="http://jason-mitchell.com/wp-content/uploads/2012/01/Game.Run_Poor.png"><img class="size-full wp-image-437  alignnone" title="Game.Run_Poor" src="http://jason-mitchell.com/wp-content/uploads/2012/01/Game.Run_Poor.png" alt="" width="715" height="351" /></a></p>
<p>The following image shows the call to the constructor containing the usage of DateTime.Now.  Note that the usage for the function calling this code (the blue box on the left) is 35.8%.</p>
<p><a href="http://jason-mitchell.com/wp-content/uploads/2012/01/AttributeChangedMessage.Ctor_Poor1.png"><img class="alignnone size-full wp-image-436" title="AttributeChangedMessage.Ctor_Poor" src="http://jason-mitchell.com/wp-content/uploads/2012/01/AttributeChangedMessage.Ctor_Poor1.png" alt="" width="716" height="348" /></a></p>
<p>Lastly, this image shows the CPU percentage used for the DateTime.Now call.</p>
<p><a href="http://jason-mitchell.com/wp-content/uploads/2012/01/Message.Ctor_Poor.png"><img class="alignnone size-full wp-image-438" title="Message.Ctor_Poor" src="http://jason-mitchell.com/wp-content/uploads/2012/01/Message.Ctor_Poor.png" alt="" width="709" height="337" /></a></p>
<p style="text-align: justify;">I was really quite surprised to see how much CPU time the DateTime.Now call was using.  I then removed the call and ran the CPU profiler to see how the results changed.  The first image is of the Game.Run() call again showing the ratio between the update and draw methods.  Already it&#8217;s clear that removing the DateTime.Now call has improved things since the CPU usage is split more evenly.</p>
<p style="text-align: justify;"><a href="http://jason-mitchell.com/wp-content/uploads/2012/01/Game.Run_.png"><img class="alignnone size-full wp-image-433" title="Game.Run_Improved" src="http://jason-mitchell.com/wp-content/uploads/2012/01/Game.Run_.png" alt="" width="710" height="340" /></a></p>
<p style="text-align: justify;">The following image shows the call to the constructor which <strong>used to</strong> contain the call to DateTime.Now.  Notice that the usage for the code calling this function has dropped from 35.8% to 0.9% which is a massive change!</p>
<p style="text-align: justify;"><a href="http://jason-mitchell.com/wp-content/uploads/2012/01/AttributeChangedMessage.Ctor_Poor.png"><img class="alignnone size-full wp-image-434" title="AttributeChangedMessage.Ctor_Improved" src="http://jason-mitchell.com/wp-content/uploads/2012/01/AttributeChangedMessage.Ctor_Poor.png" alt="" width="717" height="342" /></a></p>
<p style="text-align: justify;">Because the results of this performance test were so surprising I decided to do a bit of googling around the performance of DateTime.Now and it didn&#8217;t take very long to find an article confirming that this call is expensive.  The article also states that using DateTime.UtcNow is much faster however I plan on staying away from using DateTime in my game engine.  You can read the article at <a href="http://bit.ly/zTJaaT" target="_blank">http://bit.ly/zTJaaT</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://jason-mitchell.com/xna/xna-and-c-calling-datetime-now-is-expensive/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using WCF for database access in XNA: Sample Project</title>
		<link>http://jason-mitchell.com/xna/using-wcf-for-database-access-in-xna-sample-project/</link>
		<comments>http://jason-mitchell.com/xna/using-wcf-for-database-access-in-xna-sample-project/#comments</comments>
		<pubDate>Tue, 20 Sep 2011 20:38:20 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[WCF]]></category>
		<category><![CDATA[XNA]]></category>
		<category><![CDATA[data access]]></category>

		<guid isPermaLink="false">http://jason-mitchell.com/programming/using-wcf-for-database-access-in-xna-sample-project/</guid>
		<description><![CDATA[I spent a bit of time this evening on the phone talking to a friend about life, the universe and work.  We got around to talking a bit about accessing a database from within an XNA application so I thought I would make a quick sample and post it on here. This project uses a [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">I spent a bit of time this evening on the phone talking to a friend about life, the universe and work.  We got around to talking a bit about accessing a database from within an XNA application so I thought I would make a quick sample and post it on here.</p>
<p><span id="more-378"></span></p>
<p align="justify">This project uses a simple WCF service to get some text from the database on the server and to store details of clicks within the game in the database.  On the server-side it uses LINQ to SQL to interact with the database for retrieving and adding records.</p>
<p align="justify">Before running this sample you should probably do a full rebuild of the solution and then update the service reference in the WindowsGame1 project.  Get it from the link below:</p>
<p align="justify"><strong><em>Download: <a title="http://www.jason-mitchell.com/Uploads/XNAWCFSample-20_09_2011.zip" href="http://www.jason-mitchell.com/Uploads/XNAWCFSample-20_09_2011.zip">http://www.jason-mitchell.com/Uploads/XNAWCFSample-20_09_2011.zip</a></em></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://jason-mitchell.com/xna/using-wcf-for-database-access-in-xna-sample-project/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>XNA Texture2D Creation: byte[] vs. Color[]</title>
		<link>http://jason-mitchell.com/xna/xna-texture2d-creation-byte-vs-color/</link>
		<comments>http://jason-mitchell.com/xna/xna-texture2d-creation-byte-vs-color/#comments</comments>
		<pubDate>Wed, 31 Aug 2011 19:22:21 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[XNA]]></category>
		<category><![CDATA[2D Graphics]]></category>

		<guid isPermaLink="false">http://jason-mitchell.com/programming/xna-texture2d-creation-byte-vs-color/</guid>
		<description><![CDATA[This is just a quick article to share this interesting article by Jonny Hughes about the difference in performance between using a byte array and a Color array to generate data for a new Texture2D object in XNA.  The results surprised me a bit; I didn’t expect the use of the Color object to use [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">This is just a quick article to share <a href="http://jonny-hughes.co.uk/2011/08/25/creating-a-texture2dbyte-vs-color/" target="_blank">this interesting article</a> by Jonny Hughes about the difference in performance between using a byte array and a Color array to generate data for a new Texture2D object in XNA.  The results surprised me a bit; I didn’t expect the use of the Color object to use so much more processor time.</p>
]]></content:encoded>
			<wfw:commentRss>http://jason-mitchell.com/xna/xna-texture2d-creation-byte-vs-color/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>XNA Twitter Powered Tic Tac Toe</title>
		<link>http://jason-mitchell.com/xna/xna-twitter-powered-tic-tac-toe/</link>
		<comments>http://jason-mitchell.com/xna/xna-twitter-powered-tic-tac-toe/#comments</comments>
		<pubDate>Wed, 24 Aug 2011 20:57:04 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[XNA]]></category>

		<guid isPermaLink="false">http://jason-mitchell.com/2011/08/24/xna-twitter-powered-tic-tac-toe/</guid>
		<description><![CDATA[This was a project that was not mostly pointless and impractical but is as bizarre as this article’s title makes it sound.  One day about two years ago I was bored, very bored.  I was at a total loss for small projects to work on in the evenings after work.  Somehow I came up with [...]]]></description>
			<content:encoded><![CDATA[<p>This was a project that was not mostly pointless and impractical but is as bizarre as this article’s title makes it sound.  One day about two years ago I was bored, very bored.  I was at a total loss for small projects to work on in the evenings after work.  Somehow I came up with the idea to make a multiplayer XNA Tic Tac Toe game which used Twitter mentions for sending messages between players (wtf….I know).  Basically the game constructs simple text “commands” and posts them to a Twitter account and the other player’s game would poll Twitter looking for messages from the other player.</p>
<p>I decided to post this project because I’ve been at a loss for what to write about recently and I thought this project was so strange that I thought I should share it.  The project was made in XNA 3.0 or 3.1 and may not work anymore depending on what has changed with Twitter’s API; I really don’t know and I haven’t run the game.</p>
<p><strong><em>Download: <a title="http://www.jason-mitchell.com/Uploads/TwitterTicTacToe_24-08-2011.zip" href="http://www.jason-mitchell.com/Uploads/TwitterTicTacToe_24-08-2011.zip">http://www.jason-mitchell.com/Uploads/TwitterTicTacToe_24-08-2011.zip</a></em></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://jason-mitchell.com/xna/xna-twitter-powered-tic-tac-toe/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Basic XNA and Kinect SDK Sample</title>
		<link>http://jason-mitchell.com/xna/basic-xna-and-kinect-sdk-sample/</link>
		<comments>http://jason-mitchell.com/xna/basic-xna-and-kinect-sdk-sample/#comments</comments>
		<pubDate>Tue, 28 Jun 2011 15:10:55 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[Kinect]]></category>
		<category><![CDATA[XNA]]></category>
		<category><![CDATA[sample]]></category>

		<guid isPermaLink="false">http://jason-mitchell.com/2011/06/28/basic-xna-and-kinect-sdk-sample/</guid>
		<description><![CDATA[This past week I have spent a little bit of time playing with the Kinect SDK and trying to get to grips with how it all works.  My biggest interest is to use the Kinect SDK along with Microsoft’s XNA Framework for games development so I made a sample project using these technologies.  I’ve posted [...]]]></description>
			<content:encoded><![CDATA[<p>This past week I have spent a little bit of time playing with the Kinect SDK and trying to get to grips with how it all works.  My biggest interest is to use the Kinect SDK along with Microsoft’s XNA Framework for games development so I made a sample project using these technologies.  I’ve posted a link to this project at the end of the article.</p>
<p><span id="more-357"></span></p>
<p>The idea of this project was pretty simple.  I wanted to get the RGB camera stream of me goofing around and use it as the background in an XNA application.  I also wanted to try out some skeleton tracking functionality to test when my hands are intersecting with hot spots (represented as semi-transparent squares) on the screen.  As it turns out, this project was pretty simple and quick to make and overall I’m really quite impressed with how the SDK is to use.</p>
<p>This project makes use of a couple of <a href="http://jason-mitchell.com/2011/06/27/kinect-sdk-extension-methods/" target="_blank">simple extension methods I created</a> and is fairly heavily based on the samples which get installed with the Kinect SDK.</p>
<p><strong><em>Download: <a title="http://jason-mitchell.com/Uploads/XNA_And_Kinect-28_06_11.zip" href="http://jason-mitchell.com/Uploads/XNA_And_Kinect-28_06_11.zip">http://jason-mitchell.com/Uploads/XNA_And_Kinect-28_06_11.zip</a></em></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://jason-mitchell.com/xna/basic-xna-and-kinect-sdk-sample/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Kinect SDK Extension Methods</title>
		<link>http://jason-mitchell.com/xna/kinect-sdk-extension-methods/</link>
		<comments>http://jason-mitchell.com/xna/kinect-sdk-extension-methods/#comments</comments>
		<pubDate>Mon, 27 Jun 2011 17:02:33 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[Kinect]]></category>
		<category><![CDATA[XNA]]></category>

		<guid isPermaLink="false">http://jason-mitchell.com/?p=340</guid>
		<description><![CDATA[In this article I’m sharing a couple of quick extension methods I made for working with the Kinect SDK Beta.  I was experimenting with using the Kinect sensor in an XNA project and found myself wanting to do two things; convert the output from the RGB stream to a Texture2D and to get the position [...]]]></description>
			<content:encoded><![CDATA[<p>In this article I’m sharing a couple of quick extension methods I made for working with the Kinect SDK Beta.  I was experimenting with using the Kinect sensor in an XNA project and found myself wanting to do two things; convert the output from the RGB stream to a Texture2D and to get the position of a joint from the sensors skeleton tracking functionality relative to the game screen dimensions.</p>
<p><span id="more-340"></span></p>
<p>The following code shows the implementation of these extension methods.  I must add that this code is pretty much taken straight out of the samples and that I have just reorganised it to make it a bit easier to use.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">class</span> KinectExtensions
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> Texture2D texture <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> Color<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> colorData <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> Texture2D ToTexture2D<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> PlanarImage image, GraphicsDevice graphicsDevice<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>texture <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">||</span> colorData <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            texture <span style="color: #008000;">=</span> 
                <span style="color: #008000;">new</span> Texture2D<span style="color: #008000;">&#40;</span>graphicsDevice, image<span style="color: #008000;">.</span><span style="color: #0000FF;">Width</span>, image<span style="color: #008000;">.</span><span style="color: #0000FF;">Height</span>, <span style="color: #0600FF; font-weight: bold;">false</span>, SurfaceFormat<span style="color: #008000;">.</span><span style="color: #0000FF;">Color</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            colorData <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Color<span style="color: #008000;">&#91;</span>image<span style="color: #008000;">.</span><span style="color: #0000FF;">Width</span> <span style="color: #008000;">*</span> image<span style="color: #008000;">.</span><span style="color: #0000FF;">Height</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #6666cc; font-weight: bold;">int</span> index <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> y <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> y <span style="color: #008000;">&lt;</span> image<span style="color: #008000;">.</span><span style="color: #0000FF;">Height</span><span style="color: #008000;">;</span> y<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> x <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> x <span style="color: #008000;">&lt;</span> image<span style="color: #008000;">.</span><span style="color: #0000FF;">Width</span><span style="color: #008000;">;</span> x<span style="color: #008000;">++</span>, index <span style="color: #008000;">+=</span> image<span style="color: #008000;">.</span><span style="color: #0000FF;">BytesPerPixel</span><span style="color: #008000;">&#41;</span>
                colorData<span style="color: #008000;">&#91;</span>y <span style="color: #008000;">*</span> image<span style="color: #008000;">.</span><span style="color: #0000FF;">Width</span> <span style="color: #008000;">+</span> x<span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> 
                    <span style="color: #008000;">new</span> Color<span style="color: #008000;">&#40;</span>image<span style="color: #008000;">.</span><span style="color: #0000FF;">Bits</span><span style="color: #008000;">&#91;</span>index <span style="color: #008000;">+</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">&#93;</span>, image<span style="color: #008000;">.</span><span style="color: #0000FF;">Bits</span><span style="color: #008000;">&#91;</span>index <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span>, image<span style="color: #008000;">.</span><span style="color: #0000FF;">Bits</span><span style="color: #008000;">&#91;</span>index <span style="color: #008000;">+</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        texture<span style="color: #008000;">.</span><span style="color: #0000FF;">SetData</span><span style="color: #008000;">&#40;</span>colorData<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> texture<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> Vector2 GetScreenPosition<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> Joint joint, Runtime kinectRuntime, <span style="color: #6666cc; font-weight: bold;">int</span> screenWidth, <span style="color: #6666cc; font-weight: bold;">int</span> screenHeight<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #6666cc; font-weight: bold;">float</span> depthX<span style="color: #008000;">;</span>
        <span style="color: #6666cc; font-weight: bold;">float</span> depthY<span style="color: #008000;">;</span>
&nbsp;
        kinectRuntime<span style="color: #008000;">.</span><span style="color: #0000FF;">SkeletonEngine</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SkeletonToDepthImage</span><span style="color: #008000;">&#40;</span>joint<span style="color: #008000;">.</span><span style="color: #0000FF;">Position</span>, <span style="color: #0600FF; font-weight: bold;">out</span> depthX, <span style="color: #0600FF; font-weight: bold;">out</span> depthY<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        depthX <span style="color: #008000;">=</span> Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Max</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span>, Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Min</span><span style="color: #008000;">&#40;</span>depthX <span style="color: #008000;">*</span> <span style="color: #FF0000;">320</span>, <span style="color: #FF0000;">320</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>  <span style="color: #008080; font-style: italic;">//convert to 320, 240 space</span>
        depthY <span style="color: #008000;">=</span> Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Max</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span>, Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Min</span><span style="color: #008000;">&#40;</span>depthY <span style="color: #008000;">*</span> <span style="color: #FF0000;">240</span>, <span style="color: #FF0000;">240</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>  <span style="color: #008080; font-style: italic;">//convert to 320, 240 space</span>
&nbsp;
        <span style="color: #6666cc; font-weight: bold;">int</span> colorX<span style="color: #008000;">;</span>
        <span style="color: #6666cc; font-weight: bold;">int</span> colorY<span style="color: #008000;">;</span>
        <span style="color: #008080; font-style: italic;">// only ImageResolution.Resolution640x480 is supported at this point</span>
        kinectRuntime<span style="color: #008000;">.</span><span style="color: #0000FF;">NuiCamera</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetColorPixelCoordinatesFromDepthPixel</span><span style="color: #008000;">&#40;</span>ImageResolution<span style="color: #008000;">.</span><span style="color: #0000FF;">Resolution640x480</span>, <span style="color: #008000;">new</span> ImageViewArea<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#41;</span>depthX, <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#41;</span>depthY, <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">short</span><span style="color: #008000;">&#41;</span><span style="color: #FF0000;">0</span>, <span style="color: #0600FF; font-weight: bold;">out</span> colorX, <span style="color: #0600FF; font-weight: bold;">out</span> colorY<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// map back to skeleton.Width &amp; skeleton.Height</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">new</span> Vector2<span style="color: #008000;">&#40;</span>screenWidth <span style="color: #008000;">*</span> colorX <span style="color: #008000;">/</span> 640<span style="color: #008000;">.</span>0f, screenHeight <span style="color: #008000;">*</span> colorY <span style="color: #008000;">/</span> 480f<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://jason-mitchell.com/xna/kinect-sdk-extension-methods/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Observable Properties in XNA</title>
		<link>http://jason-mitchell.com/xna/observable-properties-in-xna/</link>
		<comments>http://jason-mitchell.com/xna/observable-properties-in-xna/#comments</comments>
		<pubDate>Sat, 30 Apr 2011 10:57:54 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[XNA]]></category>

		<guid isPermaLink="false">http://jason-mitchell.com/?p=304</guid>
		<description><![CDATA[I like to experiment a lot with XNA to try and find ways of doing things which I haven’t tried before.  Recently I have been doing some reading around the .NET Reactive Extensions and thought that it might be pretty useful to use something like that in an XNA game.  I played around with it [...]]]></description>
			<content:encoded><![CDATA[<p>I like to experiment a lot with XNA to try and find ways of doing things which I haven’t tried before.  Recently I have been doing some reading around the <a href="http://msdn.microsoft.com/en-us/data/gg577609" target="_blank">.NET Reactive Extensions</a> and thought that it might be pretty useful to use something like that in an XNA game.  I played around with it a little bit and thought it was a little bit too “heavy” for what I wanted to do since it seems like I would need to define my observers as well as my observables. I was really looking for something that only required me to define my observable properties and subscribe to them using delegates without my other game engine classes needing to know anything about them.  By removing the knowledge of observers and observables from the other game engine classes it leaves the choice of whether or not to use a subscription-based approach open.</p>
<p><span id="more-304"></span></p>
<p>The implementation of this seemed pretty simple.  I create a new class called Observable which provides methods for (un)subscribing and a property to access the actual value held by the class.  It uses a generic type to define the the value property so it should work in most cases.  So here is the class:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Observable<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> List<span style="color: #008000;">&lt;</span>Action<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;&gt;</span> subscriptions <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span>Action<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> T observableValue<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> Action<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> Subscribe<span style="color: #008000;">&#40;</span>Action<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> callback<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        subscriptions<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>callback<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> callback<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Unsubscribe<span style="color: #008000;">&#40;</span>Action<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> callback<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        subscriptions<span style="color: #008000;">.</span><span style="color: #0000FF;">Remove</span><span style="color: #008000;">&#40;</span>callback<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> UnsubscribeAll<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        subscriptions<span style="color: #008000;">.</span><span style="color: #0000FF;">Clear</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> NotifyValueChanged<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>Action<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> callback <span style="color: #0600FF; font-weight: bold;">in</span> subscriptions<span style="color: #008000;">&#41;</span>
            callback<span style="color: #008000;">&#40;</span>observableValue<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> T Value
    <span style="color: #008000;">&#123;</span>
        get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> observableValue<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        set
        <span style="color: #008000;">&#123;</span>
            observableValue <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
            NotifyValueChanged<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The class is really simple so I don’t think its worth stepping through the code and explaining it all.  The only thing I do want to point out is the Subscribe method.  It returns the exact same delegate as was passed into the method.  This is really just for convenience if you use a lambda expression to subscribe and need to hold on to it to unsubscribe from the Observable instance later.</p>
<p>To use this class all you need to do is define a new instance of the Observable type (you can see this in the attached demo project) and then use the Value property to set the data in the class.  For example if you wanted an observable Vector2 instance you would define it as “Observable&lt;Vector2&gt; position = new Observable&lt;Vector2&gt;()” and then set its value by using “position.Value = new Vector2(……)”.</p>
<p>I can see using an approach like this would be useful in a few situations.  First of all I think it would be great for separating a game’s logic from its graphics by allowing the graphics in the game to subscribe to the positions of logical entities however there is a bit of redundant data in this approach.  I have also thought that it could be very useful in developing an artificial intelligence framework.  Elements in the game could subscribe to changes in the state of an AI agent and run some code when it changes.  I’m really just throwing around some ideas here.</p>
<p><strong>Update:</strong><br />
So I had a bit of a thought about the class in this post. It&#8217;s all well and good when you want to be notified of <em>every</em> change to a property but at the minute I can&#8217;t think of a scenario in XNA when I would want that to happen.  Perhaps maybe a position gets modified multiple times in one update (e.g. moving player and finding a collision then &#8220;fixing&#8221; the players position) which in my sample would result in loads of code being executed for no real reason.  I&#8217;m currently playing with a different approach to implement this kind of process which makes use of a GameComponent to notify all interested objects once per update if something has changed.  I will probably post that when I am happy with it &#8211; so far I have removed the need for the Observable class in this article but the objects containing observable values must implement an interface for use with the game component.  At the minute I&#8217;m trying INotifyPropertyChanged to avoid adding a custom interface but there seems to be a couple of issues that I need to work out here.</p>
<p><strong><em>Sample Project: <a title="http://www.jason-mitchell.com/uploads/XNAObservableDemo-30_04_2011.zip" href="http://www.jason-mitchell.com/uploads/XNAObservableDemo-30_04_2011.zip">http://www.jason-mitchell.com/uploads/XNAObservableDemo-30_04_2011.zip</a></em></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://jason-mitchell.com/xna/observable-properties-in-xna/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>XNA Gesture Helper for Windows Phone 7</title>
		<link>http://jason-mitchell.com/xna/xna-gesture-helper-for-windows-phone-7/</link>
		<comments>http://jason-mitchell.com/xna/xna-gesture-helper-for-windows-phone-7/#comments</comments>
		<pubDate>Fri, 08 Apr 2011 13:15:41 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[XNA]]></category>

		<guid isPermaLink="false">http://www.jason-mitchell.com/?p=285</guid>
		<description><![CDATA[For my final year project at university I’ve been doing a bit of work with Windows Phone 7 and naturally that has involved working with gestures a bit.  I think the gesture support in XNA 4 is great but I found myself writing the same code over and over again to handle them.  So I [...]]]></description>
			<content:encoded><![CDATA[<p>For my final year project at university I’ve been doing a bit of work with Windows Phone 7 and naturally that has involved working with gestures a bit.  I think the gesture support in <a href="http://create.msdn.com" target="_blank">XNA</a> 4 is great but I found myself writing the same code over and over again to handle them.  So I decided to make a GestureHelper class which would take care of a lot of the repetitive work for me.  I’ve attached a sample project containing my gesture helper and a couple of examples of using it.</p>
<p><span id="more-285"></span></p>
<p>The class I created is basically an XNA game component which maintains a dictionary of gestures and an associated list of delegates and provides some obvious methods to add and remove gesture callbacks.  The Update method will take care of reading in the gestures from the touch screen and then calling the delegates for any gesture detected.  The code is listed below:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Collections.Generic</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">Microsoft.Xna.Framework</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">Microsoft.Xna.Framework.Input.Touch</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">namespace</span> GestureHelperTest
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> GestureHelper <span style="color: #008000;">:</span> GameComponent
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> GestureHelper Instance <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">private</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Initialize<span style="color: #008000;">&#40;</span>Game game<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>Instance <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
                <span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> InvalidOperationException<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Only one instance of GestureHelper can be created.&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            Instance <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> GestureHelper<span style="color: #008000;">&#40;</span>game<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            game<span style="color: #008000;">.</span><span style="color: #0000FF;">Components</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>Instance<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">private</span> Dictionary<span style="color: #008000;">&lt;</span>GestureType, List<span style="color: #008000;">&lt;</span>Action<span style="color: #008000;">&lt;</span>GestureSample<span style="color: #008000;">&gt;&gt;&gt;</span> gestureCallbacks<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">private</span> GestureHelper<span style="color: #008000;">&#40;</span>Game game<span style="color: #008000;">&#41;</span> <span style="color: #008000;">:</span> <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">&#40;</span>game<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            TouchPanel<span style="color: #008000;">.</span><span style="color: #0000FF;">EnabledGestures</span> <span style="color: #008000;">=</span> GestureType<span style="color: #008000;">.</span><span style="color: #0000FF;">None</span><span style="color: #008000;">;</span>
            gestureCallbacks <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Dictionary<span style="color: #008000;">&lt;</span>GestureType, List<span style="color: #008000;">&lt;</span>Action<span style="color: #008000;">&lt;</span>GestureSample<span style="color: #008000;">&gt;&gt;&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> AddCallback<span style="color: #008000;">&#40;</span>GestureType gestureType, Action<span style="color: #008000;">&lt;</span>GestureSample<span style="color: #008000;">&gt;</span> callback<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>gestureCallbacks<span style="color: #008000;">.</span><span style="color: #0000FF;">ContainsKey</span><span style="color: #008000;">&#40;</span>gestureType<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                TouchPanel<span style="color: #008000;">.</span><span style="color: #0000FF;">EnabledGestures</span> <span style="color: #008000;">|=</span> gestureType<span style="color: #008000;">;</span>
                gestureCallbacks<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>gestureType, <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span>Action<span style="color: #008000;">&lt;</span>GestureSample<span style="color: #008000;">&gt;&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
&nbsp;
            gestureCallbacks<span style="color: #008000;">&#91;</span>gestureType<span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>callback<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Clear<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            TouchPanel<span style="color: #008000;">.</span><span style="color: #0000FF;">EnabledGestures</span> <span style="color: #008000;">=</span> GestureType<span style="color: #008000;">.</span><span style="color: #0000FF;">None</span><span style="color: #008000;">;</span>
            gestureCallbacks<span style="color: #008000;">.</span><span style="color: #0000FF;">Clear</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> ClearGesture<span style="color: #008000;">&#40;</span>GestureType gestureType<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            TouchPanel<span style="color: #008000;">.</span><span style="color: #0000FF;">EnabledGestures</span> <span style="color: #008000;">-=</span> gestureType<span style="color: #008000;">;</span>
            gestureCallbacks<span style="color: #008000;">.</span><span style="color: #0000FF;">Remove</span><span style="color: #008000;">&#40;</span>gestureType<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> Update<span style="color: #008000;">&#40;</span>GameTime gameTime<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">while</span> <span style="color: #008000;">&#40;</span>TouchPanel<span style="color: #008000;">.</span><span style="color: #0000FF;">IsGestureAvailable</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                GestureSample gestureSample <span style="color: #008000;">=</span> TouchPanel<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadGesture</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>gestureCallbacks<span style="color: #008000;">.</span><span style="color: #0000FF;">ContainsKey</span><span style="color: #008000;">&#40;</span>gestureSample<span style="color: #008000;">.</span><span style="color: #0000FF;">GestureType</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">&#123;</span>
                    <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>Action<span style="color: #008000;">&lt;</span>GestureSample<span style="color: #008000;">&gt;</span> callback <span style="color: #0600FF; font-weight: bold;">in</span> gestureCallbacks<span style="color: #008000;">&#91;</span>gestureSample<span style="color: #008000;">.</span><span style="color: #0000FF;">GestureType</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span>
                        callback<span style="color: #008000;">&#40;</span>gestureSample<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
            <span style="color: #008000;">&#125;</span>
&nbsp;
            <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Update</span><span style="color: #008000;">&#40;</span>gameTime<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The class is basically implemented as a singleton but with a little bit of a deviation.  I’ve opted to not allow lazy initialization of the class in favour of requiring the user to explicitly initialize it to keep it nice and clean when creating the game component.  Adding the GestureHelper to the components list could be done in the game class but it will basically amount to the same thing (ie requiring the user to perform some initialization step).</p>
<p>In the sample project I’ve created, I provide the following examples of how to handle a gesture with my GestureHelper class:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> Initialize<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    GestureHelper<span style="color: #008000;">.</span><span style="color: #0000FF;">Initialize</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    GestureHelper<span style="color: #008000;">.</span><span style="color: #0000FF;">Instance</span><span style="color: #008000;">.</span><span style="color: #0000FF;">AddCallback</span><span style="color: #008000;">&#40;</span>GestureType<span style="color: #008000;">.</span><span style="color: #0000FF;">FreeDrag</span>, gestureSample <span style="color: #008000;">=&gt;</span> position <span style="color: #008000;">=</span> gestureSample<span style="color: #008000;">.</span><span style="color: #0000FF;">Position</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    GestureHelper<span style="color: #008000;">.</span><span style="color: #0000FF;">Instance</span><span style="color: #008000;">.</span><span style="color: #0000FF;">AddCallback</span><span style="color: #008000;">&#40;</span>GestureType<span style="color: #008000;">.</span><span style="color: #0000FF;">Hold</span>, gestureSample <span style="color: #008000;">=&gt;</span> scale <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    GestureHelper<span style="color: #008000;">.</span><span style="color: #0000FF;">Instance</span><span style="color: #008000;">.</span><span style="color: #0000FF;">AddCallback</span><span style="color: #008000;">&#40;</span>GestureType<span style="color: #008000;">.</span><span style="color: #0000FF;">Tap</span>, ScaleSquare<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Initialize</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> ScaleSquare<span style="color: #008000;">&#40;</span>GestureSample gestureSample<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    scale <span style="color: #008000;">+=</span> 0<span style="color: #008000;">.</span>1f<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The first two examples pass lambda expressions to the AddCallback method and the third passes a method.  Once these delegates have been added that’s it!  No more code is needed to handle these gestures!</p>
<p>This class is the result of a bit of experimentation that has been working out well for me.  I’d really appreciate any opinions that others might have on this approach to handling gestures.</p>
<p><em><strong>Sample Project: <a title="http://www.jason-mitchell.com/Uploads/GestureHelperTest-08_04_11.zip" href="http://www.jason-mitchell.com/Uploads/GestureHelperTest-08_04_11.zip">http://www.jason-mitchell.com/Uploads/GestureHelperTest-08_04_11.zip</a></strong></em></p>
]]></content:encoded>
			<wfw:commentRss>http://jason-mitchell.com/xna/xna-gesture-helper-for-windows-phone-7/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>XNA IntermediateSerializer</title>
		<link>http://jason-mitchell.com/xna/xna-intermediateserializer/</link>
		<comments>http://jason-mitchell.com/xna/xna-intermediateserializer/#comments</comments>
		<pubDate>Wed, 06 Apr 2011 12:50:31 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[XNA]]></category>

		<guid isPermaLink="false">http://www.jason-mitchell.com/index.php/2011/04/06/xna-intermediateserializer/</guid>
		<description><![CDATA[Recently I’ve been doing a lot of XNA work which involves loading XML files via the content pipeline and I frequently found myself wanting to do more with the IntermediateSerializer than the basics.  I did a bit of a search and came up with the following blog article by Shawn Hargreaves that has helped me [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I’ve been doing a lot of <a href="http://create.msdn.com" target="_blank">XNA</a> work which involves loading XML files via the content pipeline and I frequently found myself wanting to do more with the IntermediateSerializer than the basics.  I did a bit of a search and came up with the following blog article by <a href="http://blogs.msdn.com/b/shawnhar/" target="_blank">Shawn Hargreaves</a> that has helped me out loads:</p>
<p><a title="http://blogs.msdn.com/b/shawnhar/archive/2008/08/12/everything-you-ever-wanted-to-know-about-intermediateserializer.aspx" href="http://blogs.msdn.com/b/shawnhar/archive/2008/08/12/everything-you-ever-wanted-to-know-about-intermediateserializer.aspx">http://blogs.msdn.com/b/shawnhar/archive/2008/08/12/everything-you-ever-wanted-to-know-about-intermediateserializer.aspx</a></p>
<p>For anyone working with XML and the content pipeline in XNA I strongly recommend taking a read through it!</p>
]]></content:encoded>
			<wfw:commentRss>http://jason-mitchell.com/xna/xna-intermediateserializer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

