<?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>JSEDLAK &#187; C#</title>
	<atom:link href="http://jsedlak.org/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://jsedlak.org</link>
	<description></description>
	<lastBuildDate>Wed, 01 Sep 2010 00:44:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Provider Model &#8211; Design Pattern</title>
		<link>http://jsedlak.org/2009/12/15/provider-model-design-pattern/</link>
		<comments>http://jsedlak.org/2009/12/15/provider-model-design-pattern/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 06:28:34 +0000</pubDate>
		<dc:creator>John Sedlak</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[XNA]]></category>

		<guid isPermaLink="false">http://jso.fg.com/?p=440</guid>
		<description><![CDATA[A design pattern made famous in the .NET community by Microsoft&#8217;s ASP.NET, the provider model explains is a pattern that supplies the end-developer with a plug-and-play architecture. The provider model is most often used when you have a consumer object that is dependent on specific functionality that can be supplied by one or more underlying [...]]]></description>
			<content:encoded><![CDATA[<p>A design pattern made famous in the .NET community by Microsoft&#8217;s ASP.NET, the provider model explains is a pattern that supplies the end-developer with a plug-and-play architecture. The provider model is most often used when you have a consumer object that is dependent on specific functionality that can be supplied by one or more underlying systems. The major benefit of which is an increase in manageability and reusability.</p>
<p><span id="more-440"></span></p>
<div class="Center"><img class="Bordered" src="/images/articles/design-patterns/provider-model-1.jpg" alt="Provider Model - UML"/></div>
<p>It is important to note that we identify, generalize and abstract key functionality from each specific implementation to a specification (an interface in C#) to guarantee the plug-and-play architecture. Consider a site that has one or more backend storage devices for user credentials. We want to iterate through them when a user attempts to login but hardcoding them creates a strong dependency on their specific implementations. This can cause maintenance issues later on as one or more devices are added or removed. To keep the site agile, we can use the provider model to abstract the credential verification process. Consider the following <i>provider specification</i>.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p440code6'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4406"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p440code6"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">interface</span> IAuthenticationProvider<span style="color: #008000;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">bool</span> CheckCredentials<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> username, <span style="color: #6666cc; font-weight: bold;">string</span> password<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>We can then implement this in many different ways, utilizing an SQL database, an Active Directory server or even a simple text check.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p440code7'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4407"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code" id="p440code7"><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> SqlAuthenticationProvider <span style="color: #008000;">:</span> IAuthenticationProvider<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> CheckCredentials<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> username, <span style="color: #6666cc; font-weight: bold;">string</span> password<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>IsValid<span style="color: #008000;">&#40;</span>username, password<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// Run some SQL statement - set result to a bool</span>
        <span style="color: #6666cc; font-weight: bold;">bool</span> result <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">return</span> result<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;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> TextAuthenticationProvider <span style="color: #008000;">:</span> IAuthenticationProvider<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> CheckCredentials<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> username, <span style="color: #6666cc; font-weight: bold;">string</span> password<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span>username <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;root&quot;</span> <span style="color: #008000;">&amp;&amp;</span> password <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;hello&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>The beauty of this pattern is that as business requirements or APIs change, the core code of your application can remain unchanged. Instead you can update a configuration file, removing the need for recompilation and possibly a full test.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p440code8'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4408"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p440code8"><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> LoginPage <span style="color: #008000;">:</span> Page<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #6666cc; font-weight: bold;">void</span> Submit_Click<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, EventArgs e<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>IAuthenticationProvider provider <span style="color: #0600FF; font-weight: bold;">in</span> Configuration<span style="color: #008000;">.</span><span style="color: #0000FF;">AuthenticationProviders</span><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>provider<span style="color: #008000;">.</span><span style="color: #0000FF;">CheckCredentials</span><span style="color: #008000;">&#40;</span>txtUsername<span style="color: #008000;">.</span><span style="color: #0000FF;">Text</span>, txtPassword<span style="color: #008000;">.</span><span style="color: #0000FF;">Text</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
                <span style="color: #008080; font-style: italic;">// set some session variables or a cookie</span>
                Response<span style="color: #008000;">.</span><span style="color: #0000FF;">Redirect</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;UserProfile.aspx&quot;</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>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>I can hear you asking how this applies to game development and specifically the XNA universe. If you take a closer look at the XNA Framework you will find that this pattern is frequently used because of how flexible it is. For instance, any instance of <i>DrawableGameComponent</i> or <i>GameComponent</i> is a provider that implements some base interfaces. Similarly, any component you add to the services collection acts as a provider for anyone that consumes it.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p440code9'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4409"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code" id="p440code9"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">namespace</span> Microsoft<span style="color: #008000;">.</span><span style="color: #0000FF;">Xna</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Framework</span><span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">interface</span> IGameComponent<span style="color: #008000;">&#123;</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;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">interface</span> IDrawable<span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">event</span> EventHandler DrawOrderChanged<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">event</span> EventHandler VisibleChanged<span style="color: #008000;">;</span>
        <span style="color: #6666cc; font-weight: bold;">void</span> Draw<span style="color: #008000;">&#40;</span>GameTime gameTime<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #6666cc; font-weight: bold;">int</span> DrawOrder <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #6666cc; font-weight: bold;">bool</span> Visible <span style="color: #008000;">&#123;</span> get<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;">public</span> <span style="color: #6666cc; font-weight: bold;">interface</span> IUpdateable<span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">event</span> EventHandler EnabledChanged<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">event</span> EventHandler UpdateOrderChanged<span style="color: #008000;">;</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;">;</span>
        <span style="color: #6666cc; font-weight: bold;">bool</span> Enabled <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #6666cc; font-weight: bold;">int</span> UpdateOrder <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>The above specifications are then implemented by child classes that you have probably run into and used before. Mainly GameComponent implements IUpdateable and IGameComponent while DrawableGameComponent inherits the former and implements the IDrawable interface. We can say here that the component classes provide functionality to the Game class through the IGameComponent, IUpdateable, and IDrawable interfaces.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p440code10'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p44010"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p440code10"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">namespace</span> Microsoft<span style="color: #008000;">.</span><span style="color: #0000FF;">Xna</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Framework</span><span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> GameComponent <span style="color: #008000;">:</span> IGameComponent, IUpdateable, IDisposable<span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// ...</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> DrawableGameComponent <span style="color: #008000;">:</span> GameComponent, IDrawable<span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// ...</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://jsedlak.org/2009/12/15/provider-model-design-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>FGF: Getting Started</title>
		<link>http://jsedlak.org/2009/11/05/fgf-getting-started/</link>
		<comments>http://jsedlak.org/2009/11/05/fgf-getting-started/#comments</comments>
		<pubDate>Thu, 05 Nov 2009 15:08:52 +0000</pubDate>
		<dc:creator>John Sedlak</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[FGF]]></category>
		<category><![CDATA[XNA]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Visual Studio 2008]]></category>

		<guid isPermaLink="false">http://jsedlak.com/?p=246</guid>
		<description><![CDATA[This series is devoted to the design and development of my framework, FGF (Focused Games Framework), and aims to cover topics like async content management, WCF services, and many more. In this first article, I cover how to setup Visual Studio 2008 for coding the framework. It is important to understand that I will be [...]]]></description>
			<content:encoded><![CDATA[<p>This series is devoted to the design and development of my framework, FGF (Focused Games Framework), and aims to cover topics like async content management, WCF services, and many more. In this first article, I cover how to setup Visual Studio 2008 for coding the framework. It is important to understand that I will be using Visual Studio Team System 2008 and thus may have features that are not available in the Express edition. It is possible to get around many of these or ignore them completely, however, so not having Team System does not mean developing FGF is impossible.</p>
<p>The first step is to open up the IDE and create our solution. When starting a large solution such as the one for FGF, I find it useful to create a <i>Blank Solution</i> so that the solution&#8217;s name can be different than that of the first project. Again, this can be worked around in Express as well as other versions of the editor.</p>
<div class="Center"><img class="Bordered" src="/wp-content/uploads/2009/11/screenshot_9-300x222.jpg" alt="Blank FGF Solution" title="Blank FGF Solution" width="300" height="222" class="size-medium wp-image-247" /></div>
<p><span id="more-246"></span></p>
<p>The next step is an obvious one: add the first project. The design of FGF is based on a hierarchy of libraries much like the .NET Framework. Whereas libraries depend on the System DLL and mscorlib, FGF libraries can all depend on the core project, FocusedGames. To enable some cross-platform awesomeness down the road, we make this project an XNA library and then remove the references to the XNA Framework. Remember that FGF is not just an XNA framework and should support situations where XNA is not installed. You can see the cleaned project in the below screenshot.</p>
<div class="Center"><img class="Bordered" src="/wp-content/uploads/2009/11/screenshot_11-279x300.jpg" alt="First project in FGF" title="First project in FGF" width="279" height="300" class="size-medium wp-image-248" /></div>
<p>After creating some copy projects for the Zune and the Xbox 360, repeat the process for a new project, FocusedGames.Xna. At this point you may find it nice to move the platform specific projects into platform specific Solution Folders. This is one of the features of Visual Studio I wish was present in the Express Edition, but unfortunately it was left out. The really nice part of Solution Folders is that it does not physically move the project files. Rather it simply organizes them in a virtual fashion.</p>
<p>Regardless, the last step is to setup the output directory. Personally, I like having the projects all push to the same bin folder so I can copy them with a Powershell script very easily. To do this, go into each project&#8217;s properties and change the output directory to something like &#8220;..\Bin\whatever was here>&#8221; and remember to change it for both Debug and Release.</p>
<div class="Center"><a href="/wp-content/uploads/2009/11/screenshot_12.jpg"><img src="http://jsedlak.com/wp-content/uploads/2009/11/screenshot_12-300x222.jpg" alt="Changing the output directory..." title="Changing the output directory..." width="300" height="222" class="Bordered" /></a></div>
<p>Finally we are ready to start coding!</p>
]]></content:encoded>
			<wfw:commentRss>http://jsedlak.org/2009/11/05/fgf-getting-started/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>2D Fog Of War</title>
		<link>http://jsedlak.org/2009/10/27/2d-fog-of-war/</link>
		<comments>http://jsedlak.org/2009/10/27/2d-fog-of-war/#comments</comments>
		<pubDate>Tue, 27 Oct 2009 13:44:35 +0000</pubDate>
		<dc:creator>John Sedlak</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[XNA]]></category>
		<category><![CDATA[2D]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Fog Of War]]></category>

		<guid isPermaLink="false">http://jsedlak.com/?p=231</guid>
		<description><![CDATA[This tutorial covers a basic way of implementing two dimensional fog of war for a game in XNA and assumes you (the reader) has basic knowledge of C# and the XNA Framework. To get things started, create an empty Windows game project. Two textures will be needed: one for the Light and one for the [...]]]></description>
			<content:encoded><![CDATA[<div class="Center"><a href="/wp-content/uploads/2009/10/screenshot_7.jpg"><img class="Bordered" src="/wp-content/uploads/2009/10/screenshot_7-300x233.jpg" alt="Fog Of War Sample" title="Fog Of War Sample" width="300" height="233" class="size-medium wp-image-233" /></a></div>
<p>This tutorial covers a basic way of implementing two dimensional fog of war for a game in XNA and assumes you (the reader) has basic knowledge of C# and the XNA Framework. To get things started, create an empty Windows game project. Two textures will be needed: one for the <a href="http://jsedlak.com/images/articles/fogofwar/Light.png">Light</a> and one for the <a href="http://jsedlak.com/images/articles/fogofwar/Background.jpg">Background</a>. After adding these to the Content Project, go ahead and add the following members to the game class.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p231code17'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p23117"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p231code17"><pre class="csharp" style="font-family:monospace;">Texture2D lightTexture<span style="color: #008000;">;</span>
Texture2D backgroundTexture<span style="color: #008000;">;</span>
&nbsp;
RenderTarget2D lightTarget<span style="color: #008000;">;</span>
RenderTarget2D mainTarget<span style="color: #008000;">;</span>
&nbsp;
Effect basicFogOfWarEffect<span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>While the use of the texture fields is obvious, the use of the render targets may not. The concept behind this fog of war implementation is to draw the light texture to the <i>lightTarget</i> render target and then use the produced texture as the alpha channel for the texture produced from the <i>mainTarget</i> render target.</p>
<p><span id="more-231"></span></p>
<p>The next task is to load up the textures and create the render targets. <b>Note:</b> If you have issues with the render targets, you may have to change the create statement for your GPU. A better render target creation method will be added to this article at a later date.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p231code18'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p23118"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code" id="p231code18"><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> LoadContent<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// Create a new SpriteBatch, which can be used to draw textures.</span>
    spriteBatch <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SpriteBatch<span style="color: #008000;">&#40;</span>GraphicsDevice<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    lightTexture <span style="color: #008000;">=</span> Content<span style="color: #008000;">.</span><span style="color: #0000FF;">Load</span><span style="color: #008000;">&lt;</span>Texture2D<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Light&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    backgroundTexture <span style="color: #008000;">=</span> Content<span style="color: #008000;">.</span><span style="color: #0000FF;">Load</span><span style="color: #008000;">&lt;</span>Texture2D<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Background&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    basicFogOfWarEffect <span style="color: #008000;">=</span> Content<span style="color: #008000;">.</span><span style="color: #0000FF;">Load</span><span style="color: #008000;">&lt;</span>Effect<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Effects/BasicFogOfWar&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    mainTarget <span style="color: #008000;">=</span> CreateRenderTarget<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    lightTarget <span style="color: #008000;">=</span> CreateRenderTarget<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;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">LoadContent</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> RenderTarget2D CreateRenderTarget<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;">return</span> <span style="color: #008000;">new</span> RenderTarget2D<span style="color: #008000;">&#40;</span>GraphicsDevice, GraphicsDevice<span style="color: #008000;">.</span><span style="color: #0000FF;">PresentationParameters</span><span style="color: #008000;">.</span><span style="color: #0000FF;">BackBufferWidth</span>, GraphicsDevice<span style="color: #008000;">.</span><span style="color: #0000FF;">PresentationParameters</span><span style="color: #008000;">.</span><span style="color: #0000FF;">BackBufferHeight</span>, <span style="color: #FF0000;">1</span>, SurfaceFormat<span style="color: #008000;">.</span><span style="color: #0000FF;">Color</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Finally the drawing can begin. The process is incredibly simple and straight forward: draw the game, draw the lights, combine the two and draw to the screen. First the &quot;game&quot; is drawn to the <i>mainTarget</i> render target.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p231code19'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p23119"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code" id="p231code19"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> DrawMain<span style="color: #008000;">&#40;</span>GameTime gameTime<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    GraphicsDevice<span style="color: #008000;">.</span><span style="color: #0000FF;">SetRenderTarget</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span>, mainTarget<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    GraphicsDevice<span style="color: #008000;">.</span><span style="color: #0000FF;">Clear</span><span style="color: #008000;">&#40;</span>Color<span style="color: #008000;">.</span><span style="color: #0000FF;">Black</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// Draw the background</span>
    spriteBatch<span style="color: #008000;">.</span><span style="color: #0000FF;">Begin</span><span style="color: #008000;">&#40;</span>SpriteBlendMode<span style="color: #008000;">.</span><span style="color: #0000FF;">AlphaBlend</span>, SpriteSortMode<span style="color: #008000;">.</span><span style="color: #0000FF;">Immediate</span>, SaveStateMode<span style="color: #008000;">.</span><span style="color: #0000FF;">SaveState</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    spriteBatch<span style="color: #008000;">.</span><span style="color: #0000FF;">Draw</span><span style="color: #008000;">&#40;</span>
        backgroundTexture,
        <span style="color: #008000;">new</span> Rectangle<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">0</span>, GraphicsDevice<span style="color: #008000;">.</span><span style="color: #0000FF;">PresentationParameters</span><span style="color: #008000;">.</span><span style="color: #0000FF;">BackBufferWidth</span>, GraphicsDevice<span style="color: #008000;">.</span><span style="color: #0000FF;">PresentationParameters</span><span style="color: #008000;">.</span><span style="color: #0000FF;">BackBufferHeight</span><span style="color: #008000;">&#41;</span>,
        Color<span style="color: #008000;">.</span><span style="color: #0000FF;">White</span>
    <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    spriteBatch<span style="color: #008000;">.</span><span style="color: #0000FF;">End</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;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Draw</span><span style="color: #008000;">&#40;</span>gameTime<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    GraphicsDevice<span style="color: #008000;">.</span><span style="color: #0000FF;">SetRenderTarget</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span>, <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Step two is to draw the lights. For now drawing based on where the mouse will do nicely for a test.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p231code20'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p23120"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code" id="p231code20"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> DrawLights<span style="color: #008000;">&#40;</span>GameTime gameTime<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    GraphicsDevice<span style="color: #008000;">.</span><span style="color: #0000FF;">SetRenderTarget</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span>, lightTarget<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    GraphicsDevice<span style="color: #008000;">.</span><span style="color: #0000FF;">Clear</span><span style="color: #008000;">&#40;</span>Color<span style="color: #008000;">.</span><span style="color: #0000FF;">Black</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    MouseState mouseState <span style="color: #008000;">=</span> Mouse<span style="color: #008000;">.</span><span style="color: #0000FF;">GetState</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    spriteBatch<span style="color: #008000;">.</span><span style="color: #0000FF;">Begin</span><span style="color: #008000;">&#40;</span>SpriteBlendMode<span style="color: #008000;">.</span><span style="color: #0000FF;">AlphaBlend</span>, SpriteSortMode<span style="color: #008000;">.</span><span style="color: #0000FF;">Immediate</span>, SaveStateMode<span style="color: #008000;">.</span><span style="color: #0000FF;">SaveState</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    spriteBatch<span style="color: #008000;">.</span><span style="color: #0000FF;">Draw</span><span style="color: #008000;">&#40;</span>
        lightTexture,
        <span style="color: #008000;">new</span> Vector2<span style="color: #008000;">&#40;</span>mouseState<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span>, mouseState<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span><span style="color: #008000;">&#41;</span>,
        <span style="color: #0600FF; font-weight: bold;">null</span>,
        Color<span style="color: #008000;">.</span><span style="color: #0000FF;">White</span>,
        0f,
        <span style="color: #008000;">new</span> Vector2<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">16</span>, <span style="color: #FF0000;">16</span><span style="color: #008000;">&#41;</span>,
        Vector2<span style="color: #008000;">.</span><span style="color: #0000FF;">One</span> <span style="color: #008000;">*</span> <span style="color: #FF0000;">16</span>,
        SpriteEffects<span style="color: #008000;">.</span><span style="color: #0000FF;">None</span>,
        1<span style="color: #008000;">.</span>0f
    <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    spriteBatch<span style="color: #008000;">.</span><span style="color: #0000FF;">End</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    GraphicsDevice<span style="color: #008000;">.</span><span style="color: #0000FF;">SetRenderTarget</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span>, <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>And finally these methods are put together and the final is drawn with the effect.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p231code21'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p23121"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
</pre></td><td class="code" id="p231code21"><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> Draw<span style="color: #008000;">&#40;</span>GameTime gameTime<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    DrawMain<span style="color: #008000;">&#40;</span>gameTime<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    DrawLights<span style="color: #008000;">&#40;</span>gameTime<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    GraphicsDevice<span style="color: #008000;">.</span><span style="color: #0000FF;">Clear</span><span style="color: #008000;">&#40;</span>Color<span style="color: #008000;">.</span><span style="color: #0000FF;">Black</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    Texture2D mainTex <span style="color: #008000;">=</span> mainTarget<span style="color: #008000;">.</span><span style="color: #0000FF;">GetTexture</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    Texture2D lightTex <span style="color: #008000;">=</span> lightTarget<span style="color: #008000;">.</span><span style="color: #0000FF;">GetTexture</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    basicFogOfWarEffect<span style="color: #008000;">.</span><span style="color: #0000FF;">Parameters</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;LightsTexture&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SetValue</span><span style="color: #008000;">&#40;</span>lightTex<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    spriteBatch<span style="color: #008000;">.</span><span style="color: #0000FF;">Begin</span><span style="color: #008000;">&#40;</span>SpriteBlendMode<span style="color: #008000;">.</span><span style="color: #0000FF;">AlphaBlend</span>, SpriteSortMode<span style="color: #008000;">.</span><span style="color: #0000FF;">Immediate</span>, SaveStateMode<span style="color: #008000;">.</span><span style="color: #0000FF;">SaveState</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    basicFogOfWarEffect<span style="color: #008000;">.</span><span style="color: #0000FF;">Begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    basicFogOfWarEffect<span style="color: #008000;">.</span><span style="color: #0000FF;">CurrentTechnique</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Passes</span><span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    spriteBatch<span style="color: #008000;">.</span><span style="color: #0000FF;">Draw</span><span style="color: #008000;">&#40;</span>
        mainTex,
        <span style="color: #008000;">new</span> Rectangle<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">0</span>, GraphicsDevice<span style="color: #008000;">.</span><span style="color: #0000FF;">PresentationParameters</span><span style="color: #008000;">.</span><span style="color: #0000FF;">BackBufferWidth</span>, GraphicsDevice<span style="color: #008000;">.</span><span style="color: #0000FF;">PresentationParameters</span><span style="color: #008000;">.</span><span style="color: #0000FF;">BackBufferHeight</span><span style="color: #008000;">&#41;</span>,
        Color<span style="color: #008000;">.</span><span style="color: #0000FF;">White</span>
    <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    spriteBatch<span style="color: #008000;">.</span><span style="color: #0000FF;">End</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    basicFogOfWarEffect<span style="color: #008000;">.</span><span style="color: #0000FF;">CurrentTechnique</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Passes</span><span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">End</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    basicFogOfWarEffect<span style="color: #008000;">.</span><span style="color: #0000FF;">End</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>The effect itself is incredibly simple. Combine the two textures but use a channel from the &#8220;light&#8221; texture as the alpha for the color (screen facing) texture.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p231code22'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p23122"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code" id="p231code22"><pre class="csharp" style="font-family:monospace;">texture LightsTexture<span style="color: #008000;">;</span>
&nbsp;
sampler  ColorSampler  <span style="color: #008000;">:</span> register<span style="color: #008000;">&#40;</span>s0<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
sampler LightsSampler <span style="color: #008000;">=</span> sampler_state<span style="color: #008000;">&#123;</span>
	Texture <span style="color: #008000;">=</span> <span style="color: #008000;">&lt;</span>LightsTexture<span style="color: #008000;">&gt;;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #6666cc; font-weight: bold;">struct</span> VertexShaderOutput
<span style="color: #008000;">&#123;</span>
    float4 Position <span style="color: #008000;">:</span> POSITION0<span style="color: #008000;">;</span>
    float2 TexCoord <span style="color: #008000;">:</span> TEXCOORD0<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
float4 PixelShaderFunction<span style="color: #008000;">&#40;</span>VertexShaderOutput input<span style="color: #008000;">&#41;</span> <span style="color: #008000;">:</span> COLOR0
<span style="color: #008000;">&#123;</span>
	float2 tex <span style="color: #008000;">=</span> input<span style="color: #008000;">.</span><span style="color: #0000FF;">TexCoord</span><span style="color: #008000;">;</span>
&nbsp;
    float4 color <span style="color: #008000;">=</span> tex2D<span style="color: #008000;">&#40;</span>ColorSampler, tex<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    float4 alpha <span style="color: #008000;">=</span> tex2D<span style="color: #008000;">&#40;</span>LightsSampler, tex<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">return</span> float4<span style="color: #008000;">&#40;</span>color<span style="color: #008000;">.</span><span style="color: #0000FF;">r</span>, color<span style="color: #008000;">.</span><span style="color: #0000FF;">g</span>, color<span style="color: #008000;">.</span><span style="color: #0000FF;">b</span>, alpha<span style="color: #008000;">.</span><span style="color: #0000FF;">r</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
technique Technique1
<span style="color: #008000;">&#123;</span>
    pass Pass1
    <span style="color: #008000;">&#123;</span>
        PixelShader <span style="color: #008000;">=</span> compile ps_2_0 PixelShaderFunction<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></pre></td></tr></table></div>

<p>Download: <a class="downloadlink" href="http://jsedlak.org/wp-content/plugins/download-monitor/download.php?id=2" title="Version1.0 downloaded 278 times" >Fog Of War Sample (278)</a></p>
]]></content:encoded>
			<wfw:commentRss>http://jsedlak.org/2009/10/27/2d-fog-of-war/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Important .NET 4.0 News</title>
		<link>http://jsedlak.org/2009/08/27/important-net-40-news/</link>
		<comments>http://jsedlak.org/2009/08/27/important-net-40-news/#comments</comments>
		<pubDate>Thu, 27 Aug 2009 14:26:47 +0000</pubDate>
		<dc:creator>John Sedlak</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[.NET 4.0]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://jsedlak.com/?p=196</guid>
		<description><![CDATA[Scott &#8220;Gu&#8221; (Guthrie) has started a series of posts about VS2010 and (more importantly) .NET 4.0, touting its new features that so far have been incredibly important for developers. It seems that Microsoft is finally &#8220;getting it&#8221; and listening to us about certain things. While most of you have already heard about the ability to [...]]]></description>
			<content:encoded><![CDATA[<p>Scott &#8220;Gu&#8221; (Guthrie) has started a <a href="http://weblogs.asp.net/scottgu/archive/2009/08/25/vs-2010-and-net-4-series.aspx">series of posts</a> about VS2010 and (more importantly) .NET 4.0, touting its new features that so far have been incredibly important for developers. It seems that Microsoft is finally &#8220;getting it&#8221; and listening to us about certain things.</p>
<p>While most of you have already heard about the ability to define Debug and Release web.config files, these two new features may put you over the edge in waiting for .NET 4.0 to release.</p>
<ul>
<li><a href="http://weblogs.asp.net/scottgu/archive/2009/08/25/clean-web-config-files-vs-2010-and-net-4-0-series.aspx">Clean Web.Config Files</a>
<p>Finally Microsoft has realized that while having configuration files that are verbose enough to support wild customizations, most of us do not need to change the default sections in the <i>web.config</i> file. So it should come as no surprise that clean configuration files will now be a thing of the (recent) past. It is just a shame it took so long to get.</p>
</li>
<li><a href="http://weblogs.asp.net/scottgu/archive/2009/08/26/starter-project-templates-vs-2010-and-net-4-0-series.aspx">Control over Client Ids</a>
<p>Hidden in this post about new project templates and what looks to be the ASP.NET WebForms version of the ASP.NET MVC template is a bit about client ids for controls.</p>
<blockquote><p>All of the styles and content within the site are configured using CSS, and take advantage of some of the new features with Web Forms in ASP.NET 4 – including clean client-side “id” names (no more ctrl_ mangled names – ASP.NET 4 gives you complete control over the client id), and CSS based rendering instead of table based rendering for the built-in server controls.</p></blockquote>
<p>This, again, should come as no surprise as the mangled names can present problems for developers looking to write some quick javascript as well as those looking to simply clean up their HTML. Here&#8217;s to hoping that Microsoft cleans up its HTML in other places as well (SharePoint anyone?).</p>
</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://jsedlak.org/2009/08/27/important-net-40-news/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tackling The Problem</title>
		<link>http://jsedlak.org/2008/12/03/tackling-the-problem/</link>
		<comments>http://jsedlak.org/2008/12/03/tackling-the-problem/#comments</comments>
		<pubDate>Wed, 03 Dec 2008 13:08:31 +0000</pubDate>
		<dc:creator>John Sedlak</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[FGF]]></category>
		<category><![CDATA[Web CMS]]></category>

		<guid isPermaLink="false">http://jsedlak.com/?p=146</guid>
		<description><![CDATA[Tackling the problem in the previous post I have decided to rely on providing more options than necessary. I have to remember that because Vodka is not client software, I have to write it as if the client software will be as simple as possible. The goal of Vodka is to give developers a way [...]]]></description>
			<content:encoded><![CDATA[<p>Tackling the problem in the <a href="http://jsedlak.com/2008/12/01/problems-with-vodka/">previous post</a> I have decided to rely on providing more options than necessary. I have to remember that because Vodka is not client software, I have to write it as if the client software will be as simple as possible. The goal of Vodka is to give developers a way of setting up a software based CMS with great ease.</p>
<p>To do this, I have built in services that can be implemented and exposed via WCF as well as built in implementations of these services. If you had the binaries for Vodka, you could reference FocusedGames.Vodka.Services, create a class in a Service project and inherit from ContentService or MembershipService. After deploying the project, you can treat it like any other WCF service reference.</p>
<p>The original question dealt with translation services however, is it a client or server responsibility? You would generally make it client side because you may wish to get a different template for the content or change how translation is done. This is on par with most CMS software installs out there but there is a problem. Templates in the Vodka backend are treated as standard items of content. It then would require two calls to the service: one for the template and one for the content. You would also have to know how the template is extracted from the content item. This is a bad idea as it increases server load unecessarily.</p>
<p>The answer is simple: abstract it and keep it server side. The idea here is that the developers can plugin their own template provider into the service when setting it up. This will allow them to point to another service, a local directory or hardcoded templates. If no plugin is used, the service will continue on its standard course, grabbing the template from the standard content data store.</p>
<p>You may be asking why you would ever want to use a custom provider to point to a second service. Let&#8217;s say you have a ton of websites that you all want to look the same. For instance Microsoft may have microsoft.com, msdn.microsoft.com, creators.xna.com, xbox.com, et cetera and may want them to all use the same templates for items. They could use a provider to point to a single template service removing the need to change 5+ templates on 5+ servers.</p>
]]></content:encoded>
			<wfw:commentRss>http://jsedlak.org/2008/12/03/tackling-the-problem/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Difference Between New and Override</title>
		<link>http://jsedlak.org/2008/11/03/the-difference-between-new-and-override/</link>
		<comments>http://jsedlak.org/2008/11/03/the-difference-between-new-and-override/#comments</comments>
		<pubDate>Mon, 03 Nov 2008 06:38:13 +0000</pubDate>
		<dc:creator>John Sedlak</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Coding]]></category>

		<guid isPermaLink="false">http://jsedlak.com/?p=132</guid>
		<description><![CDATA[This is a fundamental topic in C# and if you plan to do any development at all with the language you need to know this. Override is used for changing the behavior of the original type&#8217;s method or property. The new keyword, in this case, is used for scratching the existing behavior but only in [...]]]></description>
			<content:encoded><![CDATA[<p>This is a fundamental topic in C# and if you plan to do any development at all with the language you need to know this. Override is used for changing the behavior of the original type&#8217;s method or property. The new keyword, in this case, is used for scratching the existing behavior but only in the current type. To better explain this, consider the following code.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p132code25'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p13225"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p132code25"><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> Foo<span style="color: #008000;">&#123;</span>
     <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">virtual</span> <span style="color: #6666cc; font-weight: bold;">void</span> Print<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
          Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;I am a Foo!&quot;</span><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;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Bar <span style="color: #008000;">:</span> Foo<span style="color: #008000;">&#123;</span>
     <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> Print<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
          Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;I am a Bar!&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
     <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>If we declare a Bar and cast it to a Foo in the above code, we will still get &#8220;I am a Bar!&#8221; when we invoke the Print method. However, if we change the code to the following things will get messed up a bit.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p132code26'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p13226"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p132code26"><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> Foo<span style="color: #008000;">&#123;</span>
     <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Print<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
          Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;I am a Foo!&quot;</span><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;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Bar <span style="color: #008000;">:</span> Foo<span style="color: #008000;">&#123;</span>
     <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">void</span> Print<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
          Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;I am a Bar!&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
     <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>If we instantiate a Bar, we will get the new functionality. However, if we cast that Bar object into a Foo, we will get the implementation provided by the Foo. So be sure of what you are using and why!</p>
]]></content:encoded>
			<wfw:commentRss>http://jsedlak.org/2008/11/03/the-difference-between-new-and-override/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Provider Model is Win</title>
		<link>http://jsedlak.org/2008/10/07/provider-model-is-win/</link>
		<comments>http://jsedlak.org/2008/10/07/provider-model-is-win/#comments</comments>
		<pubDate>Tue, 07 Oct 2008 06:28:40 +0000</pubDate>
		<dc:creator>John Sedlak</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Provider Model]]></category>
		<category><![CDATA[Web CMS]]></category>

		<guid isPermaLink="false">http://jsedlak.com/?p=105</guid>
		<description><![CDATA[The provider model’s goal is to allow for the abstraction of implementation to the nth degree by allowing for the choice between separate implementations of the same functionality. This design pattern is incredibly common in Microsoft’s ASP.NET, especially in the realm of security. Developers can choose between different providers for membership, roles and authentication for [...]]]></description>
			<content:encoded><![CDATA[<p>The provider model’s goal is to allow for the abstraction of implementation to the n<sup>th</sup> degree by allowing for the choice between separate implementations of the same functionality. This design pattern is incredibly common in Microsoft’s ASP.NET, especially in the realm of security. Developers can choose between different providers for membership, roles and authentication for a single site. The point is that the core functionality of the site does not change, only the implementation. Below is a quick snapshot of how such a model is used in Vodka.</p>
<p>The driving force behind the provider model is the abstraction of the implementation. This is done via an interface.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p105code30'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p10530"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p105code30"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">interface</span> IAuthenticationProvider<span style="color: #008000;">&#123;</span>
	<span style="color: #6666cc; font-weight: bold;">bool</span> Authenticate<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> username, <span style="color: #6666cc; font-weight: bold;">string</span> password<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>What gives the provider model its meat and bones is the different implementations of the above interface. The two implementations below represent two authentication backends: an ActiveDirectory installation and an Sql database. Although the meat has been stripped from these two classes, it is clear that their implementations would differ enough to have the two separate classes.<span id="more-105"></span></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p105code31'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p10531"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p105code31"><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> ActiveDirectoryAuthenticationProvider <span style="color: #008000;">:</span> IAuthenticationProvider<span style="color: #008000;">&#123;</span>
	<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> Authenticate<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> username, <span style="color: #6666cc; font-weight: bold;">string</span> password<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
		<span style="color: #008080; font-style: italic;">// Attempt to log user into AD.</span>
		<span style="color: #0600FF; font-weight: bold;">return</span> authTest<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;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> SqlAuthenticationProvider <span style="color: #008000;">:</span> IAuthenticationProvider<span style="color: #008000;">&#123;</span>
	<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> Authenticate<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> username, <span style="color: #6666cc; font-weight: bold;">string</span> password<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
		<span style="color: #008080; font-style: italic;">// Validate credentials against the Sql DB.</span>
		<span style="color: #0600FF; font-weight: bold;">return</span> authTest<span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Now that the implementation is abstracted from the functionality requirements, the provider model requires only a driver to use differing implementations. Below is a sample driver that simply uses each provider to authenticate a user. It is important to note that there is no code to be changed based on what backend (provider) is being used due to the use of the common interface.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p105code32'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p10532"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p105code32"><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> Driver <span style="color: #008000;">&#123;</span>
	<span style="color: #0600FF; font-weight: bold;">public</span> Driver<span style="color: #008000;">&#40;</span>IAuthenticationProvider provider<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>provider<span style="color: #008000;">.</span><span style="color: #0000FF;">Authenticate</span><span style="color: #008000;">&#40;</span>“jsedlak”, “hello”<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
			Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>“You are authenticated<span style="color: #008000;">!</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: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
		Driver d1 <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Driver<span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> SqlAuthenticationProvider<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		Driver d2 <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Driver<span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> ActiveDirectoryAuthenticationProvider<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>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>The above example is a simplified version of the provider models used in Vodka where Membership, Authentication and Roles have all been squeezed into one provider. The reason for this is to remove an unnecessary code complexity requirement of three different security providers. More often than not, the three are commonly found together in the backend anyways.</p>
]]></content:encoded>
			<wfw:commentRss>http://jsedlak.org/2008/10/07/provider-model-is-win/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Garbage Collection Nightmares&#8230;</title>
		<link>http://jsedlak.org/2008/09/21/garbage-collection-nightmares/</link>
		<comments>http://jsedlak.org/2008/09/21/garbage-collection-nightmares/#comments</comments>
		<pubDate>Sun, 21 Sep 2008 19:54:39 +0000</pubDate>
		<dc:creator>John Sedlak</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[XNA]]></category>
		<category><![CDATA[Garbage Collection]]></category>
		<category><![CDATA[GC]]></category>
		<category><![CDATA[Memory]]></category>
		<category><![CDATA[Zune]]></category>

		<guid isPermaLink="false">http://jsedlak.com/?p=95</guid>
		<description><![CDATA[I was pushing Galactic Defense to the Zune today to do some minor version testing (I develop on the PC and then push to Zune for platform specific tests) and ran into some major problems. The GC was collecting about once an update which was slowing the game down tremendously. So I dug in and [...]]]></description>
			<content:encoded><![CDATA[<p>I was pushing Galactic Defense to the Zune today to do some minor version testing (I develop on the PC and then push to Zune for platform specific tests) and ran into some major problems. The GC was collecting about once an update which was slowing the game down tremendously. So I dug in and started searching my code.</p>
<p>One of the best ways to figure out where you are creating garbage (when no tool can do it for you) is just to start commenting things out. For starters, I commented out the draw code for my map. This worked, but not enough. I ended up adding a line that removed the InputManager from the component collection (effectively commenting out its update code) and I had it: 0 allocations per update.</p>
<p>So I looked at the draw code again and realized (with the help from guys in #xna) that I was using an enumeration in a Dictionary. A big no no since the framework allocates every time you do a lookup. A quick switch to ints (cast all lookups to integers) fixed that problem. Now onto the InputManager.</p>
<p>To make a long story short, I followed the trail and cornered this line of code:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p95code35'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p9535"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p95code35"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>repeatKeys<span style="color: #008000;">.</span><span style="color: #0000FF;">Contains</span><span style="color: #008000;">&#40;</span>button<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span></pre></td></tr></table></div>

<p>It is this line of code that actively makes sure that I have all the buttons in the repeatKey list so I can check and update the repeated keys later on. I removed this line of code and added the following:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p95code36'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p9536"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p95code36"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">bool</span> found <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</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> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> repeatKeys<span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><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>repeatKeys<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #008000;">==</span> button<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        found <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">break</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;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>found<span style="color: #008000;">&#41;</span>
    repeatKeys<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>button<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>And now I am back to a normal amount of allocations (some for strings) per update. I wonder why Contains is causing an allocation. I am too lazy to figure that one out right now&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://jsedlak.org/2008/09/21/garbage-collection-nightmares/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Restoring LocalUserAppDataPath in WPF</title>
		<link>http://jsedlak.org/2008/08/29/restoring-localuserappdatapath-in-wpf/</link>
		<comments>http://jsedlak.org/2008/08/29/restoring-localuserappdatapath-in-wpf/#comments</comments>
		<pubDate>Fri, 29 Aug 2008 21:54:16 +0000</pubDate>
		<dc:creator>John Sedlak</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[.NET Framework]]></category>

		<guid isPermaLink="false">http://jso.fg.com/?p=376</guid>
		<description><![CDATA[With the arrival of .NET 3.5 and consequently WPF applications, Microsoft has done away with the old Application WinForms class. No longer can you call Application.LocalUserAppDataPath to get where the user&#8217;s files are stored. To restore this functionality as well as add a bit more, I have tapped into an uncommon resource: extension methods. Let&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>With the arrival of .NET 3.5 and consequently WPF applications, Microsoft has done away with the old Application WinForms class. No longer can you call Application.LocalUserAppDataPath to get where the user&#8217;s files are stored. To restore this functionality as well as add a bit more, I have tapped into an uncommon resource: extension methods. Let&#8217;s dig a little deeper, shall we?</p>
<p><span id="more-376"></span></p>
<p><b>Note:</b> You should not rely on this for accessing local data stores. Microsoft has done away with this method for a reason and relying on it in the future can cause problems.</p>
<p>The first thing we need to do is be able to get the Company Name from the assembly. Inside of a blank static class, add the following functions. I have named my class <i>ApplicationExtensions</i>.</p>
<p>Fortunately the company name is kept in an attribute:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p376code40'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p37640"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code" id="p376code40"><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;">string</span> GetCompanyName<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> Assembly assembly<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">string</span> name <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Empty</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> attributes <span style="color: #008000;">=</span> assembly<span style="color: #008000;">.</span><span style="color: #0000FF;">GetCustomAttributes</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>AssemblyCompanyAttribute<span style="color: #008000;">&#41;</span>, <span style="color: #0600FF; font-weight: bold;">true</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>attributes <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">&amp;&amp;</span> attributes<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span> <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        AssemblyCompanyAttribute aca <span style="color: #008000;">=</span> attributes<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #0600FF; font-weight: bold;">as</span> AssemblyCompanyAttribute<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>aca <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>
            name <span style="color: #008000;">=</span> aca<span style="color: #008000;">.</span><span style="color: #0000FF;">Company</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;">return</span> name<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Note my lack of any formal error handling. I will leave this up to you to handle. For now, returning an empty string will do. Next up, we need to be able to format the version number:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p376code41'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p37641"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code" id="p376code41"><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;">string</span> GetFormattedVersion<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> Assembly assembly<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">string</span> version <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Empty</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">try</span><span style="color: #008000;">&#123;</span>
        Version assemblyVersion <span style="color: #008000;">=</span> assembly<span style="color: #008000;">.</span><span style="color: #0000FF;">GetName</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Version</span><span style="color: #008000;">;</span>
&nbsp;
        version <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;{0}.{1}.{2}.{3}&quot;</span>,
            assemblyVersion<span style="color: #008000;">.</span><span style="color: #0000FF;">Major</span>,
            assemblyVersion<span style="color: #008000;">.</span><span style="color: #0000FF;">Minor</span>,
            assemblyVersion<span style="color: #008000;">.</span><span style="color: #0000FF;">Build</span>,
            assemblyVersion<span style="color: #008000;">.</span><span style="color: #0000FF;">Revision</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">catch</span><span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">return</span> version<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>And finally, the LocalUserDataPath:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p376code42'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p37642"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code" id="p376code42"><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;">string</span> GetLocalUserAppDataPath<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> Application application<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">string</span> path <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Empty</span><span style="color: #008000;">;</span>
&nbsp;
    Assembly assembly <span style="color: #008000;">=</span> Assembly<span style="color: #008000;">.</span><span style="color: #0000FF;">GetEntryAssembly</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>assembly <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>
        path <span style="color: #008000;">=</span> Path<span style="color: #008000;">.</span><span style="color: #0000FF;">Combine</span><span style="color: #008000;">&#40;</span>
            Environment<span style="color: #008000;">.</span><span style="color: #0000FF;">GetFolderPath</span><span style="color: #008000;">&#40;</span>Environment<span style="color: #008000;">.</span><span style="color: #0000FF;">SpecialFolder</span><span style="color: #008000;">.</span><span style="color: #0000FF;">LocalApplicationData</span><span style="color: #008000;">&#41;</span>,
            Path<span style="color: #008000;">.</span><span style="color: #0000FF;">Combine</span><span style="color: #008000;">&#40;</span>
                Path<span style="color: #008000;">.</span><span style="color: #0000FF;">Combine</span><span style="color: #008000;">&#40;</span>
                    assembly<span style="color: #008000;">.</span><span style="color: #0000FF;">GetCompanyName</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,
                    assembly<span style="color: #008000;">.</span><span style="color: #0000FF;">GetName</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">&#41;</span>,
                assembly<span style="color: #008000;">.</span><span style="color: #0000FF;">GetFormattedVersion</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</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;
    <span style="color: #0600FF; font-weight: bold;">return</span> path<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://jsedlak.org/2008/08/29/restoring-localuserappdatapath-in-wpf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MGS: Pong (Pages 4-6)</title>
		<link>http://jsedlak.org/2008/07/04/mgs-pong-pages-4-6/</link>
		<comments>http://jsedlak.org/2008/07/04/mgs-pong-pages-4-6/#comments</comments>
		<pubDate>Fri, 04 Jul 2008 18:32:19 +0000</pubDate>
		<dc:creator>John Sedlak</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[XNA]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://jso.fg.com/?p=388</guid>
		<description><![CDATA[In this fourth [page, second post] installment of the MGS: Pong article series, I am going to cover how to draw a background for the game. Before I start, I should tell you that there will be two more articles: one for creating the menu system and one for pulling all the classes together. Okay, [...]]]></description>
			<content:encoded><![CDATA[<p>In this fourth [page, second post] installment of the MGS: Pong article series, I am going to cover how to draw a background for the game. Before I start, I should tell you that there will be two more articles: one for creating the menu system and one for pulling all the classes together. Okay, let&#8217;s begin! The background class (Background.cs) is incredibly easy to create. First we need some private members to draw the background.</p>
<p><span id="more-388"></span></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p388code47'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p38847"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p388code47"><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> Background <span style="color: #008000;">:</span> Microsoft<span style="color: #008000;">.</span><span style="color: #0000FF;">Xna</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Framework</span><span style="color: #008000;">.</span><span style="color: #0000FF;">DrawableGameComponent</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #008080;">#region Private Members</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> SpriteBatch m_spriteBatch<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> ContentManager m_conManager<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">private</span> Texture2D m_bgTexture<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> m_texSource<span style="color: #008000;">;</span>
    <span style="color: #008080;">#endregion</span></pre></td></tr></table></div>

<p>The next thing we need to do is handling how to load and unload content. This is nothing new, so no explanation is required.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p388code48'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p38848"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
</pre></td><td class="code" id="p388code48"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080;">#region Constructor</span>
<span style="color: #0600FF; font-weight: bold;">public</span> Background <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>
    <span style="color: #008080; font-style: italic;">// Instantiates a new Content Manager. The CM class</span>
    <span style="color: #008080; font-style: italic;">// is a lightweight object that allows us to load and</span>
    <span style="color: #008080; font-style: italic;">// process XNA content.</span>
    m_conManager <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ContentManager <span style="color: #008000;">&#40;</span> game<span style="color: #008000;">.</span><span style="color: #0000FF;">Services</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #008080;">#endregion</span>
&nbsp;
<span style="color: #008080;">#region Load / Unload Content</span>
<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> LoadGraphicsContent <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">bool</span> loadAllContent<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">LoadGraphicsContent</span> <span style="color: #008000;">&#40;</span> loadAllContent <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// Load All Content is true when the Device is created</span>
    <span style="color: #008080; font-style: italic;">// or goes through a hard reset. This happens when the</span>
    <span style="color: #008080; font-style: italic;">// window is created and sometimes when moved to </span>
    <span style="color: #008080; font-style: italic;">// another monitor.</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span> loadAllContent <span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// Instantiates a new SpriteBatch object.</span>
        m_spriteBatch <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SpriteBatch <span style="color: #008000;">&#40;</span> GraphicsDevice <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// Checks if the texture file exists, and if so</span>
        <span style="color: #008080; font-style: italic;">// loads the texture.</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span> File<span style="color: #008000;">.</span><span style="color: #0000FF;">Exists</span> <span style="color: #008000;">&#40;</span> m_texSource <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;.xnb&quot;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span>
            m_bgTexture <span style="color: #008000;">=</span> m_conManager<span style="color: #008000;">.</span><span style="color: #0000FF;">Load</span><span style="color: #008000;">&lt;</span>Texture2D<span style="color: #008000;">&gt;</span> <span style="color: #008000;">&#40;</span> m_texSource <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;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> UnloadGraphicsContent <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">bool</span> unloadAllContent<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">UnloadGraphicsContent</span> <span style="color: #008000;">&#40;</span> unloadAllContent <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// Much like loadAllContent, the value of unloadAllContent</span>
    <span style="color: #008080; font-style: italic;">// is true when the device is created or goes through a hard</span>
    <span style="color: #008080; font-style: italic;">// reset. This occurs before a LoadGraphicsContent(...) in</span>
    <span style="color: #008080; font-style: italic;">// the latter cases.</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span> unloadAllContent <span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// Unloads all the data.</span>
        m_conManager<span style="color: #008000;">.</span><span style="color: #0000FF;">Unload</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// Dispose of unmanaged memory and</span>
        <span style="color: #008080; font-style: italic;">// set the paddle texture to null</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span> m_bgTexture <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>
            m_bgTexture<span style="color: #008000;">.</span><span style="color: #0000FF;">Dispose</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            m_bgTexture <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// Dispose of the spritebatch</span>
        <span style="color: #008080; font-style: italic;">// and set it to null.</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span> m_spriteBatch <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>
            m_spriteBatch<span style="color: #008000;">.</span><span style="color: #0000FF;">Dispose</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            m_spriteBatch <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #008080;">#endregion</span></pre></td></tr></table></div>

<p>Next we handle drawing! Again, this is nothing new so I will just let you see the code.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p388code49'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p38849"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code" id="p388code49"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080;">#region Drawing</span>
<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> Draw <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;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Draw</span> <span style="color: #008000;">&#40;</span> gameTime <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// If the texture doesn't exist, we can't draw!</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span> m_bgTexture <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;">return</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// Start drawing.</span>
    m_spriteBatch<span style="color: #008000;">.</span><span style="color: #0000FF;">Begin</span> <span style="color: #008000;">&#40;</span> SpriteBlendMode<span style="color: #008000;">.</span><span style="color: #0000FF;">AlphaBlend</span>, SpriteSortMode<span style="color: #008000;">.</span><span style="color: #0000FF;">Immediate</span>, SaveStateMode<span style="color: #008000;">.</span><span style="color: #0000FF;">SaveState</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// Draw the paddle.</span>
    m_spriteBatch<span style="color: #008000;">.</span><span style="color: #0000FF;">Draw</span> <span style="color: #008000;">&#40;</span> m_bgTexture, Vector2<span style="color: #008000;">.</span><span style="color: #0000FF;">Zero</span>, Color<span style="color: #008000;">.</span><span style="color: #0000FF;">White</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// End drawing.</span>
    m_spriteBatch<span style="color: #008000;">.</span><span style="color: #0000FF;">End</span> <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: #008080;">#endregion</span></pre></td></tr></table></div>

<p>Finally we have a property so that we can set the texture source from outside of the class. As said before, the next article will cover the menu system.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p388code50'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p38850"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p388code50"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080;">#region Properties</span>
<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Gets or Sets the source of the paddle's texture.</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> TextureSource
<span style="color: #008000;">&#123;</span>
    get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> m_texSource<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    set <span style="color: #008000;">&#123;</span> m_texSource <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #008080;">#endregion</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://jsedlak.org/2008/07/04/mgs-pong-pages-4-6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
