<?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; .NET</title>
	<atom:link href="http://jsedlak.org/tag/net/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>Korkboard 1.0.0.4</title>
		<link>http://jsedlak.org/2010/07/08/korkboard-1-0-0-4/</link>
		<comments>http://jsedlak.org/2010/07/08/korkboard-1-0-0-4/#comments</comments>
		<pubDate>Thu, 08 Jul 2010 11:20:55 +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 4.0]]></category>
		<category><![CDATA[Applications]]></category>
		<category><![CDATA[Apps]]></category>
		<category><![CDATA[Korkboard]]></category>

		<guid isPermaLink="false">http://jsedlak.org/?p=655</guid>
		<description><![CDATA[Korkboard is a little tool I have been developing that enables a uniform method of storing multiple items on the clipboard. When Korkboard runs, it hooks into the clipboard chain and attempts to intercept messages as you use the Copy and Cut commands. It then stores the items on its own list so that they [...]]]></description>
			<content:encoded><![CDATA[<div style="float:right;margin-left:20px;"><a href="/apps#korkboard"><img src="http://jsedlak.org/images/screenshots/cb/cb4_small.jpg" alt="Korkboard Screenshot"></a></div>
<p><a href="/apps#korkboard">Korkboard</a> is a little tool I have been developing that enables a uniform method of storing multiple items on the clipboard. When Korkboard runs, it hooks into the clipboard chain and attempts to intercept messages as you use the Copy and Cut commands. It then stores the items on its own list so that they may be retrieved at a later time. It is important to note that Korkboard does <b>not</b> mess with the functionality of the clipboard. If you copy an item, the clipboard works as expected without any interruption or user required interception. Check it out, and let me know what you think!</p>
<ul>
<li><a href="http://jsedlak.org/wp-content/plugins/download-monitor/download.php?id=25" title="Downloaded 177 times.">Korkboard v1.1.0.0 (177)</a></li>
<li><a href="http://jsedlak.org/wp-content/plugins/download-monitor/download.php?id=26" title="Downloaded 22 times.">Korkboard (Zip) v1.0.0.4 (22)</a></li>
</ul>
<p>Korkboard is written in WPF/.NET4 and uses ClickOnce to manage the installation and update processes.</p>
<p>Update: Added the Zip file for those having trouble using the ClickOnce method.</p>
<div style="clear:both;"><!--clear--></div>
]]></content:encoded>
			<wfw:commentRss>http://jsedlak.org/2010/07/08/korkboard-1-0-0-4/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<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: A Helper For Creating Render Targets</title>
		<link>http://jsedlak.org/2009/11/25/a-helper-for-creating-render-targets/</link>
		<comments>http://jsedlak.org/2009/11/25/a-helper-for-creating-render-targets/#comments</comments>
		<pubDate>Wed, 25 Nov 2009 12:48:04 +0000</pubDate>
		<dc:creator>John Sedlak</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[FGF]]></category>
		<category><![CDATA[XNA]]></category>

		<guid isPermaLink="false">http://jsedlak.com/?p=259</guid>
		<description><![CDATA[Before I begin this post, big thanks to Eibx and David over at the Community Forums for helping me find these methods. I have modified the CheckTexture method a bit, but its purpose remains unchanged. As of the last FGF article, the Application class was implementing the IGame interface but was missing the ability to [...]]]></description>
			<content:encoded><![CDATA[<p>Before I begin this post, big thanks to Eibx and David over at the <a href="http://forums.xna.com/forums/p/43000/255276.aspx">Community Forums</a> for helping me find these methods. I have modified the CheckTexture method a bit, but its purpose remains unchanged.</p>
<p>As of the last FGF article, the Application class was implementing the IGame interface but was missing the ability to create a render target object on the PC and Xbox 360. For PC games this can be a troubling problem since different hardware can obviously require different formats and dimensions of render target. Rather than bake this functionality into the Application class itself, it is moved to a static helper class so that all developers can make good use of its functionality at any point in time.</p>
<p>To start off, a simple default creation method is included to give the basic functionality an easy access point.</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('p259code16'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p25916"><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="p259code16"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">Microsoft.Xna.Framework</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">Microsoft.Xna.Framework.Graphics</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">namespace</span> FocusedGames<span style="color: #008000;">.</span><span style="color: #0000FF;">Xna</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Graphics</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">class</span> GraphicsHelper
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> RenderTarget2D CreateRenderTarget<span style="color: #008000;">&#40;</span>GraphicsDevice device<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> CreateRenderTarget<span style="color: #008000;">&#40;</span>
                device, 
                device<span style="color: #008000;">.</span><span style="color: #0000FF;">PresentationParameters</span><span style="color: #008000;">.</span><span style="color: #0000FF;">BackBufferWidth</span>, 
                device<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>, 
                device<span style="color: #008000;">.</span><span style="color: #0000FF;">PresentationParameters</span><span style="color: #008000;">.</span><span style="color: #0000FF;">BackBufferFormat</span>
            <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p><span id="more-259"></span></p>
<p>Secondly the real CreateRenderTarget method is revealed. It checks the device format as well as the multisample type to ensure that they are in accordance with what the hardware device requires.</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('p259code17'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p25917"><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
</pre></td><td class="code" id="p259code17"><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> RenderTarget2D CreateRenderTarget<span style="color: #008000;">&#40;</span>GraphicsDevice device, <span style="color: #6666cc; font-weight: bold;">int</span> width, <span style="color: #6666cc; font-weight: bold;">int</span> height, <span style="color: #6666cc; font-weight: bold;">int</span> levels, SurfaceFormat format<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            MultiSampleType sampleType <span style="color: #008000;">=</span> device<span style="color: #008000;">.</span><span style="color: #0000FF;">PresentationParameters</span><span style="color: #008000;">.</span><span style="color: #0000FF;">MultiSampleType</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>
                <span style="color: #008000;">!</span>GraphicsAdapter<span style="color: #008000;">.</span><span style="color: #0000FF;">DefaultAdapter</span><span style="color: #008000;">.</span><span style="color: #0000FF;">CheckDeviceFormat</span><span style="color: #008000;">&#40;</span>
                    DeviceType<span style="color: #008000;">.</span><span style="color: #0000FF;">Hardware</span>, 
                    GraphicsAdapter<span style="color: #008000;">.</span><span style="color: #0000FF;">DefaultAdapter</span><span style="color: #008000;">.</span><span style="color: #0000FF;">CurrentDisplayMode</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span>, 
                    TextureUsage<span style="color: #008000;">.</span><span style="color: #0000FF;">None</span>, 
                    QueryUsages<span style="color: #008000;">.</span><span style="color: #0000FF;">None</span>, 
                    ResourceType<span style="color: #008000;">.</span><span style="color: #0000FF;">RenderTarget</span>, 
                    format
                <span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                format <span style="color: #008000;">=</span> device<span style="color: #008000;">.</span><span style="color: #0000FF;">DisplayMode</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
            <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>
                <span style="color: #008000;">!</span>GraphicsAdapter<span style="color: #008000;">.</span><span style="color: #0000FF;">DefaultAdapter</span><span style="color: #008000;">.</span><span style="color: #0000FF;">CheckDeviceMultiSampleType</span><span style="color: #008000;">&#40;</span>
                    DeviceType<span style="color: #008000;">.</span><span style="color: #0000FF;">Hardware</span>, 
                    format, 
                    device<span style="color: #008000;">.</span><span style="color: #0000FF;">PresentationParameters</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsFullScreen</span>, 
                    sampleType
                <span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                sampleType <span style="color: #008000;">=</span> MultiSampleType<span style="color: #008000;">.</span><span style="color: #0000FF;">None</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
&nbsp;
            CheckTextureSize<span style="color: #008000;">&#40;</span>width, height, <span style="color: #0600FF; font-weight: bold;">out</span> width, <span style="color: #0600FF; font-weight: bold;">out</span> height<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">new</span> RenderTarget2D<span style="color: #008000;">&#40;</span>device, width, height, levels, format, sampleType, <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>This method makes a call to the CheckTextureSize method which the <a href="http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.rendertarget2d.rendertarget2d.aspx">MSDN Documentation</a> is missing. Similar to the above method, the CheckTextureSize function checks the width and the height parameters against what the graphics adapter supports and, if necessary, changes each to comply with the requirements.</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('p259code18'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p25918"><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
</pre></td><td class="code" id="p259code18"><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;">bool</span> CheckTextureSize<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> width, <span style="color: #6666cc; font-weight: bold;">int</span> height, <span style="color: #0600FF; font-weight: bold;">out</span> <span style="color: #6666cc; font-weight: bold;">int</span> newWidth, <span style="color: #0600FF; font-weight: bold;">out</span> <span style="color: #6666cc; font-weight: bold;">int</span> newHeight<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #6666cc; font-weight: bold;">bool</span> returnValue <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
&nbsp;
            GraphicsDeviceCapabilities caps <span style="color: #008000;">=</span> GraphicsAdapter<span style="color: #008000;">.</span><span style="color: #0000FF;">DefaultAdapter</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetCapabilities</span><span style="color: #008000;">&#40;</span>DeviceType<span style="color: #008000;">.</span><span style="color: #0000FF;">Hardware</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>caps<span style="color: #008000;">.</span><span style="color: #0000FF;">TextureCapabilities</span><span style="color: #008000;">.</span><span style="color: #0000FF;">RequiresPower2</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                returnValue <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// Perform a simple check before doing (possibly) unecessary math</span>
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>IsPowerOfTwo<span style="color: #008000;">&#40;</span>width<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;">// Find the nearest base two log of the current width,</span>
                    <span style="color: #008080; font-style: italic;">// and go up to the next integer</span>
                    <span style="color: #6666cc; font-weight: bold;">double</span> exponent <span style="color: #008000;">=</span> Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Ceiling</span><span style="color: #008000;">&#40;</span>Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Log</span><span style="color: #008000;">&#40;</span>width<span style="color: #008000;">&#41;</span> <span style="color: #008000;">/</span> Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Log</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                    width <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#41;</span>Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Pow</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">2</span>, exponent<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// Perform a simple check before doing (possibly) unecessary math</span>
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>IsPowerOfTwo<span style="color: #008000;">&#40;</span>height<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;">// Find the nearest base two log of the current height,</span>
                    <span style="color: #008080; font-style: italic;">// and go up to the next integer</span>
                    <span style="color: #6666cc; font-weight: bold;">double</span> exponent <span style="color: #008000;">=</span> Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Ceiling</span><span style="color: #008000;">&#40;</span>Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Log</span><span style="color: #008000;">&#40;</span>height<span style="color: #008000;">&#41;</span> <span style="color: #008000;">/</span> Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Log</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                    height <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#41;</span>Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Pow</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">2</span>, exponent<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;">if</span> <span style="color: #008000;">&#40;</span>caps<span style="color: #008000;">.</span><span style="color: #0000FF;">TextureCapabilities</span><span style="color: #008000;">.</span><span style="color: #0000FF;">RequiresSquareOnly</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                returnValue <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
&nbsp;
                width <span style="color: #008000;">=</span> Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Max</span><span style="color: #008000;">&#40;</span>width, height<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                height <span style="color: #008000;">=</span> width<span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
&nbsp;
            newWidth <span style="color: #008000;">=</span> Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Min</span><span style="color: #008000;">&#40;</span>caps<span style="color: #008000;">.</span><span style="color: #0000FF;">MaxTextureWidth</span>, width<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            newHeight <span style="color: #008000;">=</span> Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Min</span><span style="color: #008000;">&#40;</span>caps<span style="color: #008000;">.</span><span style="color: #0000FF;">MaxTextureHeight</span>, height<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF; font-weight: bold;">return</span> returnValue<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>The only real change I have introduced here is a simple check to see if the width and height are already powers of two before doing some possibly unecessary math. I decided to keep the check because it is extremely basic in terms of mathematical computation.</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('p259code19'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p25919"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p259code19"><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;">bool</span> IsPowerOfTwo<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> number<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>number <span style="color: #008000;">&amp;</span> <span style="color: #008000;">&#40;</span>number <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>And there you have it! A robust way for creating a render target on all the platforms that XNA supports. Now that we have this, the last change needs to happen in the Application class. Remember that ResetRenderTarget method? Well now it can be fixed up properly:</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('p259code20'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p25920"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p259code20"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">virtual</span> <span style="color: #6666cc; font-weight: bold;">void</span> ResetRenderTarget<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;">if</span> <span style="color: #008000;">&#40;</span>renderTarget <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #008000;">!</span>renderTarget<span style="color: #008000;">.</span><span style="color: #0000FF;">IsDisposed</span><span style="color: #008000;">&#41;</span>
        renderTarget<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>
&nbsp;
    renderTarget <span style="color: #008000;">=</span> GraphicsHelper<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateRenderTarget</span><span style="color: #008000;">&#40;</span>GraphicsDevice, <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#41;</span>orientedDisplaySize<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span>, <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#41;</span>orientedDisplaySize<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</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>

]]></content:encoded>
			<wfw:commentRss>http://jsedlak.org/2009/11/25/a-helper-for-creating-render-targets/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('p231code27'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p23127"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p231code27"><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('p231code28'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p23128"><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="p231code28"><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('p231code29'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p23129"><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="p231code29"><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('p231code30'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p23130"><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="p231code30"><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('p231code31'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p23131"><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="p231code31"><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('p231code32'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p23132"><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="p231code32"><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>A Letter To Microsoft: On Framework Design</title>
		<link>http://jsedlak.org/2009/10/02/a-letter-to-microsoft-on-framework-design/</link>
		<comments>http://jsedlak.org/2009/10/02/a-letter-to-microsoft-on-framework-design/#comments</comments>
		<pubDate>Sat, 03 Oct 2009 01:23:06 +0000</pubDate>
		<dc:creator>John Sedlak</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[.NET 4.0]]></category>
		<category><![CDATA[.NET Framework]]></category>

		<guid isPermaLink="false">http://jsedlak.com/?p=220</guid>
		<description><![CDATA[Rather than writing an actual letter, which would do no good, I have chosen to write this post with the idea that as an MVP I should lead and teach by example. One of the most important design principles that factor into designing a framework is that of dependency hierarchies. In less fancy terms it [...]]]></description>
			<content:encoded><![CDATA[<p>Rather than writing an actual letter, which would do no good, I have chosen to write this post with the idea that as an MVP I should lead and teach by example. One of the most important design principles that factor into designing a framework is that of dependency hierarchies. In less fancy terms it simply means what classes implement what interfaces and where these things are maintained in the framework. Recently I placed a suggestion on <a href="http://connect.microsoft.com">Connect</a> to <a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=488607&#038;wa=wsignin1.0">Move INotifyCollectionChanged to System.dll</a> so that more projects that do not require WindowsBase.dll (WPF) could use the functionality. Fortunately for .NET developers everywhere the suggestion was taken (although I won&#8217;t claim sole responsibility) and the move is happening.</p>
<p>This post is about the reasons for such a move, and such reasons are incredibly basic to realize. Consider a platform other than Windows whether it be Linux, Windows Mobile, the Xbox 360 or even the Zune HD. Now think about how you would write a framework that supports some of (or all) these platforms and consider the problem of developing a class that implements something like INotifyCollectionChanged. The problem, of course, is that not all the platforms have access to WindowsBase.dll. Thus the developers come to a crossroads: either recreate the functionality for the platforms or don&#8217;t implement the functionality.</p>
<p>While that may not seem too unreasonable a dilemma, the problem is that Microsoft implemented functionality in a branched assembly that really has no dependency on any classes or interfaces within that assembly. Here the principle is simple: when possible, move functionality up the assembly hierarchy so as to reduce cross dependencies. In layman&#8217;s terms, by moving the INotifyCollectionChanged interface to System.dll, many more projects can take advantage of it without requiring any sort of platform dependent code. And here it is important to remember that one of the original goals of .NET was to enable true cross platform development.</p>
<p>To say that this case is the only one of its kind would be ignorant. The scary thing is that I don&#8217;t think anyone actually has a list of where these mistakes exist and what can be done about them. Even scarier than that is the fact that Microsoft is making these mistakes. Consider the following which further demonstrates this point.</p>
<p>While helping someone today with a few questions on the System.Reflection namespace, a point was made about how odd it is to compare generic types. When you have a generic class such as <i>Nullable&lt;T&gt;</i> you cannot do something as simple as <i>if(foo is Nullable)</i> because the generic parameter can make a difference. While this isn&#8217;t too much of a problem, Microsoft <span style="text-decoration: line-through;">later</span> also developed the System.Data assembly and included an interface called <i>INullable</i> within this assembly under the <a href="http://msdn.microsoft.com/en-us/library/system.data.sqltypes.aspx">System.Data.SqlTypes namespace</a>. This, like INotifyCollectionChanged, constricts the use of this interface to only assemblies that reference System.Data.dll. Furthermore, the interface contains a single boolean property and has not a single dependency on anything within the System.data assembly.</p>
<p>The problem and its solution should be obvious: move INullable to the System namespace in System.dll and make <i>Nullable&lt;T&gt;</i> implement it. This allows developers to easily check whether or not a type represents a nullable type while not creating a single problem in the entire framework&#8217;s design. Furthermore it would allow developers to treat System.Nullable and System.Data.SqlTypes.* as the same types which can come in handy.</p>
<p><b>Update:</b> I added a suggestion on Connect for <a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=495607">moving INullable to System.dll</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://jsedlak.org/2009/10/02/a-letter-to-microsoft-on-framework-design/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Cross Platform XNA Projects (X64 Content)</title>
		<link>http://jsedlak.org/2009/09/05/cross-platform-xna-projects-x64-content/</link>
		<comments>http://jsedlak.org/2009/09/05/cross-platform-xna-projects-x64-content/#comments</comments>
		<pubDate>Sat, 05 Sep 2009 17:42:24 +0000</pubDate>
		<dc:creator>John Sedlak</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[XNA]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://jsedlak.com/?p=197</guid>
		<description><![CDATA[One of the requirements of the framework I am building (FGF) is cross platform support. For my XNA games this means support for not only Windows but also the Zune and the Xbox 360. For my Windows based projects I often find that X64 can be used (and in the case of IIS in 2008 [...]]]></description>
			<content:encoded><![CDATA[<p>One of the requirements of the framework I am building (<a href="http://focusedgames.com/fg-framework">FGF</a>) is cross platform support. For my XNA games this means support for not only Windows but also the Zune and the Xbox 360. For my Windows based projects I often find that X64 can be used (and in the case of IIS in 2008 R2, encouraged) so I also support X64 versions.</p>
<p>The problem is that when using an XNA project template to build a library for the simple fact that XNA projects can automatically synchronized (across platforms), Visual Studio blocks the creation of an X64 build target. Rather you are stuck with X86, Zune or Xbox 360.</p>
<p>The good news is that you can get around this! Open up the Windows project file (csproj) in a suitable text editor and copy the sections for both &#8220;Debug|x86&#8243; and &#8220;Release|x86&#8243; and paste them right after.</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('p197code35'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p19735"><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
</pre></td><td class="code" id="p197code35"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;</span>PropertyGroup Condition<span style="color: #008000;">=</span><span style="color: #666666;">&quot; '$(Configuration)|$(Platform)' == 'Debug|x86' &quot;</span><span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>DebugSymbols<span style="color: #008000;">&gt;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&lt;/</span>DebugSymbols<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>DebugType<span style="color: #008000;">&gt;</span>full<span style="color: #008000;">&lt;/</span>DebugType<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>Optimize<span style="color: #008000;">&gt;</span><span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&lt;/</span>Optimize<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>OutputPath<span style="color: #008000;">&gt;..</span>\Bin\x86\Debug\<span style="color: #008000;">&lt;/</span>OutputPath<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>DefineConstants<span style="color: #008000;">&gt;</span>DEBUG<span style="color: #008000;">;</span>TRACE<span style="color: #008000;">;</span>WINDOWS<span style="color: #008000;">&lt;/</span>DefineConstants<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>ErrorReport<span style="color: #008000;">&gt;</span>prompt<span style="color: #008000;">&lt;/</span>ErrorReport<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>WarningLevel<span style="color: #008000;">&gt;</span><span style="color: #FF0000;">4</span><span style="color: #008000;">&lt;/</span>WarningLevel<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>NoStdLib<span style="color: #008000;">&gt;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&lt;/</span>NoStdLib<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>UseVSHostingProcess<span style="color: #008000;">&gt;</span><span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&lt;/</span>UseVSHostingProcess<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>PlatformTarget<span style="color: #008000;">&gt;</span>x86<span style="color: #008000;">&lt;/</span>PlatformTarget<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>XnaCompressContent<span style="color: #008000;">&gt;</span><span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&lt;/</span>XnaCompressContent<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;/</span>PropertyGroup<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;</span>PropertyGroup Condition<span style="color: #008000;">=</span><span style="color: #666666;">&quot; '$(Configuration)|$(Platform)' == 'Release|x86' &quot;</span><span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>DebugType<span style="color: #008000;">&gt;</span>pdbonly<span style="color: #008000;">&lt;/</span>DebugType<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>Optimize<span style="color: #008000;">&gt;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&lt;/</span>Optimize<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>OutputPath<span style="color: #008000;">&gt;..</span>\Bin\x86\Release\<span style="color: #008000;">&lt;/</span>OutputPath<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>DefineConstants<span style="color: #008000;">&gt;</span>TRACE<span style="color: #008000;">;</span>WINDOWS<span style="color: #008000;">&lt;/</span>DefineConstants<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>ErrorReport<span style="color: #008000;">&gt;</span>prompt<span style="color: #008000;">&lt;/</span>ErrorReport<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>WarningLevel<span style="color: #008000;">&gt;</span><span style="color: #FF0000;">4</span><span style="color: #008000;">&lt;/</span>WarningLevel<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>NoStdLib<span style="color: #008000;">&gt;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&lt;/</span>NoStdLib<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>UseVSHostingProcess<span style="color: #008000;">&gt;</span><span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&lt;/</span>UseVSHostingProcess<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>PlatformTarget<span style="color: #008000;">&gt;</span>x86<span style="color: #008000;">&lt;/</span>PlatformTarget<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>XnaCompressContent<span style="color: #008000;">&gt;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&lt;/</span>XnaCompressContent<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;/</span>PropertyGroup<span style="color: #008000;">&gt;</span></pre></td></tr></table></div>

<p>Next you simply replace the instances of x86 with x64 and change anything else you need.</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('p197code36'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p19736"><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
</pre></td><td class="code" id="p197code36"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;</span>PropertyGroup Condition<span style="color: #008000;">=</span><span style="color: #666666;">&quot; '$(Configuration)|$(Platform)' == 'Debug|x64' &quot;</span><span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>DebugSymbols<span style="color: #008000;">&gt;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&lt;/</span>DebugSymbols<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>DebugType<span style="color: #008000;">&gt;</span>full<span style="color: #008000;">&lt;/</span>DebugType<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>Optimize<span style="color: #008000;">&gt;</span><span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&lt;/</span>Optimize<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>OutputPath<span style="color: #008000;">&gt;..</span>\Bin\x64\Debug\<span style="color: #008000;">&lt;/</span>OutputPath<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>DefineConstants<span style="color: #008000;">&gt;</span>DEBUG<span style="color: #008000;">;</span>TRACE<span style="color: #008000;">;</span>WINDOWS<span style="color: #008000;">&lt;/</span>DefineConstants<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>ErrorReport<span style="color: #008000;">&gt;</span>prompt<span style="color: #008000;">&lt;/</span>ErrorReport<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>WarningLevel<span style="color: #008000;">&gt;</span><span style="color: #FF0000;">4</span><span style="color: #008000;">&lt;/</span>WarningLevel<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>NoStdLib<span style="color: #008000;">&gt;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&lt;/</span>NoStdLib<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>UseVSHostingProcess<span style="color: #008000;">&gt;</span><span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&lt;/</span>UseVSHostingProcess<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>PlatformTarget<span style="color: #008000;">&gt;</span>x64<span style="color: #008000;">&lt;/</span>PlatformTarget<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>XnaCompressContent<span style="color: #008000;">&gt;</span><span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&lt;/</span>XnaCompressContent<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;/</span>PropertyGroup<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;</span>PropertyGroup Condition<span style="color: #008000;">=</span><span style="color: #666666;">&quot; '$(Configuration)|$(Platform)' == 'Release|x64' &quot;</span><span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>DebugType<span style="color: #008000;">&gt;</span>pdbonly<span style="color: #008000;">&lt;/</span>DebugType<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>Optimize<span style="color: #008000;">&gt;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&lt;/</span>Optimize<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>OutputPath<span style="color: #008000;">&gt;..</span>\Bin\x64\Release\<span style="color: #008000;">&lt;/</span>OutputPath<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>DefineConstants<span style="color: #008000;">&gt;</span>TRACE<span style="color: #008000;">;</span>WINDOWS<span style="color: #008000;">&lt;/</span>DefineConstants<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>ErrorReport<span style="color: #008000;">&gt;</span>prompt<span style="color: #008000;">&lt;/</span>ErrorReport<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>WarningLevel<span style="color: #008000;">&gt;</span><span style="color: #FF0000;">4</span><span style="color: #008000;">&lt;/</span>WarningLevel<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>NoStdLib<span style="color: #008000;">&gt;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&lt;/</span>NoStdLib<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>UseVSHostingProcess<span style="color: #008000;">&gt;</span><span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&lt;/</span>UseVSHostingProcess<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>PlatformTarget<span style="color: #008000;">&gt;</span>x64<span style="color: #008000;">&lt;/</span>PlatformTarget<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>XnaCompressContent<span style="color: #008000;">&gt;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&lt;/</span>XnaCompressContent<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;/</span>PropertyGroup<span style="color: #008000;">&gt;</span></pre></td></tr></table></div>

<p>One warning &#8211; this should only be used for projects that don&#8217;t require references to the XNA framework. At this time Microsoft has no support for x64 XNA references.</p>
]]></content:encoded>
			<wfw:commentRss>http://jsedlak.org/2009/09/05/cross-platform-xna-projects-x64-content/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Debugging Unknown (IIS) Exceptions</title>
		<link>http://jsedlak.org/2009/07/31/debugging-unknown-iis-exceptions/</link>
		<comments>http://jsedlak.org/2009/07/31/debugging-unknown-iis-exceptions/#comments</comments>
		<pubDate>Fri, 31 Jul 2009 13:08:24 +0000</pubDate>
		<dc:creator>John Sedlak</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Sitecore]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://jsedlak.com/?p=191</guid>
		<description><![CDATA[One of the major gripes I have with the Windows operating system is its inability to build decent event logs when an exception occurs. This is particularly annoying on the server builds because as a developer, I need to know what is causing a crash or a hang. Recently I have been developing a site [...]]]></description>
			<content:encoded><![CDATA[<p>One of the major gripes I have with the Windows operating system is its inability to build decent event logs when an exception occurs. This is particularly annoying on the server builds because as a developer, I need to know what is causing a crash or a hang. Recently I have been developing a site using <a href="http://sitecore.net">Sitecore</a> and after a publish the site started crashing and bringing down the entire server. The following is an overview of how I solved the issue.</p>
<p>The first thing I did was check the Event Viewer on the server itself &#8211; notice the horrible information within the error. The one important code to write down is the exception code (<i>0xE053534F</i>).</p>
<div style="text-align:center;"><a href="http://jsedlak.com/wp-content/uploads/2009/07/event.png"><img src="http://jsedlak.com/wp-content/uploads/2009/07/events.png" alt="Event Viewer"/></a></div>
<p>After reading a tutorial on <a href="http://blogs.msdn.com/tess/archive/2009/03/20/debugging-a-net-crash-with-rules-in-debug-diag.aspx">how to catch a crash</a> I was able to catch the crash in a dump file. While the debug diag tool was useful for catching, I needed <a href="http://www.microsoft.com/whdc/devtools/debugging/default.mspx">WinDbg</a> to get to the root of the problem.</p>
<p><b>Note</b>: I set the symbols location to <i>SRV*c:\users\jsedlak\documents\websymbols*http://msdl.microsoft.com/download/symbols</i></p>
<div style="text-align:center;"><a href="http://jsedlak.com/wp-content/uploads/2009/07/dump.png"><img src="http://jsedlak.com/wp-content/uploads/2009/07/dumps.png" alt="Debug View"/></a></div>
<p>As you can see, the debug view is fairly useless. Run the following commands:</p>
<ul>
<li>.loadby sos mscorwks</li>
<li>!clrstack</li>
</ul>
<p>Now you can see what is causing the stack overflow. Turns out it was an XSLT file that was doing a recursive climb up the Sitecore tree without a base case. The cause of the problem however was that I had just implemented Workflow on all items which corrupted their state and seemed to have removed their published version from the web database. By fixing the XSLT to have a base case and submitting all content through their workflow I was able to get the site back.</p>
<div style="text-align:center;"><a href="http://jsedlak.com/wp-content/uploads/2009/07/dump2.png"><img src="http://jsedlak.com/wp-content/uploads/2009/07/dump2s.png" alt="Stack View"/></a></div>
]]></content:encoded>
			<wfw:commentRss>http://jsedlak.org/2009/07/31/debugging-unknown-iis-exceptions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Managing Focus Across Different Platforms</title>
		<link>http://jsedlak.org/2008/12/11/managing-focus-across-different-platforms/</link>
		<comments>http://jsedlak.org/2008/12/11/managing-focus-across-different-platforms/#comments</comments>
		<pubDate>Thu, 11 Dec 2008 14:25:13 +0000</pubDate>
		<dc:creator>John Sedlak</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[XNA]]></category>
		<category><![CDATA[FGF]]></category>
		<category><![CDATA[Input]]></category>
		<category><![CDATA[Thrust]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[Xbox 360]]></category>
		<category><![CDATA[Zune]]></category>

		<guid isPermaLink="false">http://jsedlak.com/?p=151</guid>
		<description><![CDATA[With the upcoming release of FGF/Thrust comes the return of a more traditional GUI. One of the most problematic requirements of a large GUI system is the notion of focus. The question remains how do you efficiently determine who has focus and how do you pass focus between controls? On Windows this is incredibly easy [...]]]></description>
			<content:encoded><![CDATA[<p>With the upcoming release of FGF/Thrust comes the return of a more traditional GUI. One of the most problematic requirements of a large GUI system is the notion of focus. The question remains how do you efficiently determine who has focus and how do you pass focus between controls? On Windows this is incredibly easy because we have the mouse pointer. Focus is changed whenever your mouse acts on a control. What about on the Zune or the Xbox 360 though? On both of these systems their is no mouse (although Thrust supports a virtual mouse).</p>
<p>The answer is to look at what Thrust currently supports. What built in system supports all three systems seamlessly? (~ means some support, X means full support)</p>
<table>
<tr>
<td>Event</td>
<td>Zune</td>
<td>Windows</td>
<td>Xbox 360</td>
</tr>
<tr>
<td>Mouse</td>
<td>&nbsp;</td>
<td>X</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>Keyboard</td>
<td>&nbsp;</td>
<td>X</td>
<td>X</td>
</tr>
<tr>
<td>GamePad</td>
<td>~</td>
<td>X</td>
<td>X</td>
</tr>
<tr>
<td>UniversalButton</td>
<td>X</td>
<td>X</td>
<td>X</td>
</tr>
</table>
<p>The problem is we can&#8217;t rely on each individual hardware controller being present and useable. For example we can&#8217;t rely on a keyboard being a focusing mechanism on the Xbox 360 because it isn&#8217;t a guarantee it exists. Likewise, we cannot rely on the GamePad on the Zune because of the lack of buttons.</p>
<p>To get to the point, the UniversalButton system was meant as a virtualization of the various hardware supported for each platform. It turns GamePad, Mouse, and Keyboard events into simple events like Up, Left, Down, Right, Select and Cancel. Because of this we can rely on it and implement a few more events.</p>
<ul>
<li>TabPreviousLocal</li>
<li>TabNextLocal</li>
<li>TabPreviousGlobal</li>
<li>TabNextGlobal</li>
</ul>
<p>You can consider local tabbing to be much like you would on a Windows form: hitting the tab key (or shift-tab) will move you from one control to another based on some order and only in the context of the global focus point (you never tab to a control in another window). The global tabbing can be considered like an Alt-Tab (or shift-alt-tab) where you can switch between windows.</p>
<p>So how do we implement this? We need a managing class (sorry Bjoern) to produce a bottleneck for the input events. As events are channeled through this class, it massages the data and figures out what to do. For instance if a global tab next event is received it has to switch focus to the next window in the system on the same level as the current window. If it receives a local tab event it will pass a message to the current focal point to tab to the next control.</p>
<p>What does this do for us? For one, it unifies the approach to focusing across all the UI subsystems. This means that a Window/Form implementation will focus in much the same way a simple screen will. Unfortunately it also means a complexity requirement for implementation developers. The age of the simple StateManager class is coming to an end. Elements on the screen now need to have a basic state for animation as well as a state for focus (or lack thereof). While it may still be possible to simply unload an element through the StateManager, the reality is that UI elements will have to do a little more management under the hood. Whether this is exposed to the user / developer is still to be decided.</p>
]]></content:encoded>
			<wfw:commentRss>http://jsedlak.org/2008/12/11/managing-focus-across-different-platforms/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Spark of Genius?</title>
		<link>http://jsedlak.org/2008/12/09/a-spark-of-genius/</link>
		<comments>http://jsedlak.org/2008/12/09/a-spark-of-genius/#comments</comments>
		<pubDate>Wed, 10 Dec 2008 01:44:26 +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[Code]]></category>
		<category><![CDATA[Versionator]]></category>

		<guid isPermaLink="false">http://jsedlak.com/?p=150</guid>
		<description><![CDATA[Knowing how frameworks are designed and developed definately has its benefits. Awhile ago I spent some time looking into how DependencyObject and DependencyProperty work in WPF. Essentially, many of the properties are not simple properties but rather facades for method invocations. Fast forward to today when I was working on Versionator, an application I am [...]]]></description>
			<content:encoded><![CDATA[<p>Knowing how frameworks are designed and developed definately has its benefits. Awhile ago I spent some time looking into how DependencyObject and DependencyProperty work in WPF. Essentially, many of the properties are not simple properties but rather facades for method invocations.</p>
<p>Fast forward to today when I was working on Versionator, an application I am writing to help me manage my many projects&#8217; versions. The problem is that project information files (AssemblyInfo.cs) are not straight forward in terms of parsing. They are merely text files, awkward and malformed code files in fact. To be able to support many properties so easily, I came up with a scheme to parse, manage and save the projects&#8217; properties in the file.</p>
<p>What I do is parse the properties, extracting the data and storing it in a dictionary as &#8220;old values.&#8221; At the same time I create a copy of the values in a &#8220;current values&#8221; dictionary. As new values come in, I channel them through properties, then Get and Set methods which access the &#8220;current values&#8221; dictionary. When it comes time to save I have to do some management of the data based on whether or not the property was empty, non-existant or is being changed to empty. It is simple to say that it would have taken me a lot longer if I had not taken a look at WPF. So was it a spark of genius? Or just plain old learnedness?</p>
<p>The Code (Beware of its massive length):</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('p150code38'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15038"><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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
</pre></td><td class="code" id="p150code38"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Collections.Generic</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.IO</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">namespace</span> FocusedGames<span style="color: #008000;">.</span><span style="color: #0000FF;">Versionator</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Data</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> ProjectInfo
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">enum</span> Properties <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">ushort</span>
        <span style="color: #008000;">&#123;</span>
            Title <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span>,
            Description <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span>,
            Company <span style="color: #008000;">=</span> <span style="color: #FF0000;">2</span>,
            Product <span style="color: #008000;">=</span> <span style="color: #FF0000;">3</span>,
            Copyright <span style="color: #008000;">=</span> <span style="color: #FF0000;">4</span>,
            Trademark <span style="color: #008000;">=</span> <span style="color: #FF0000;">5</span>,
            AssemblyVersion <span style="color: #008000;">=</span> <span style="color: #FF0000;">6</span>,
            FileVersion <span style="color: #008000;">=</span> <span style="color: #FF0000;">7</span>,
            Guid <span style="color: #008000;">=</span> <span style="color: #FF0000;">8</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> lookups <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span> <span style="color: #666666;">&quot;AssemblyTitle&quot;</span>, <span style="color: #666666;">&quot;AssemblyDescription&quot;</span>, <span style="color: #666666;">&quot;AssemblyCompany&quot;</span>, <span style="color: #666666;">&quot;AssemblyProduct&quot;</span>, <span style="color: #666666;">&quot;AssemblyCopyright&quot;</span>, <span style="color: #666666;">&quot;AssemblyTrademark&quot;</span>, <span style="color: #666666;">&quot;AssemblyVersion&quot;</span>, <span style="color: #666666;">&quot;AssemblyFileVersion&quot;</span>, <span style="color: #666666;">&quot;Guid&quot;</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> Dictionary<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">ushort</span>, <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;</span> propertyValues <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Dictionary<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">ushort</span>, <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> Dictionary<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">ushort</span>, <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;</span> oldValues <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Dictionary<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">ushort</span>, <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;</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;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> ProjectInfo FromFile<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> filename<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> ProjectInfo<span style="color: #008000;">&#40;</span>filename<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> ProjectInfo<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            Array enumValues <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">Enum</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetValues</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span> <span style="color: #008000;">&#40;</span>Properties<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF; font-weight: bold;">foreach</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">ushort</span> prop <span style="color: #0600FF; font-weight: bold;">in</span> enumValues<span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                propertyValues<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>prop, <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Empty</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                oldValues<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>prop, <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Empty</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> ProjectInfo<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> filename<span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">:</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            Load<span style="color: #008000;">&#40;</span>filename<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #008080;">#region Getting and Setting Project Properties</span>
        <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #6666cc; font-weight: bold;">void</span> SetProperty<span style="color: #008000;">&#40;</span>Properties prop, <span style="color: #6666cc; font-weight: bold;">string</span> value<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            propertyValues<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">ushort</span><span style="color: #008000;">&#41;</span> prop<span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #6666cc; font-weight: bold;">string</span> GetProperty<span style="color: #008000;">&#40;</span>Properties prop<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #6666cc; font-weight: bold;">string</span> newValue <span style="color: #008000;">=</span> propertyValues<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">ushort</span><span style="color: #008000;">&#41;</span> prop<span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #008000;">&#40;</span>newValue<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
                <span style="color: #0600FF; font-weight: bold;">return</span> oldValues<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">ushort</span><span style="color: #008000;">&#41;</span> prop<span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF; font-weight: bold;">return</span> newValue<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #008080;">#endregion</span>
&nbsp;
        <span style="color: #008080;">#region Loading and Saving</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Load<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> filename<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            Filename <span style="color: #008000;">=</span> filename<span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> lines <span style="color: #008000;">=</span> File<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadAllLines</span><span style="color: #008000;">&#40;</span>Filename<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            Array enumValues <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">Enum</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetValues</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span> <span style="color: #008000;">&#40;</span>Properties<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF; font-weight: bold;">foreach</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">ushort</span> prop <span style="color: #0600FF; font-weight: bold;">in</span> enumValues<span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                Load<span style="color: #008000;">&#40;</span>lines, prop<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;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> Load<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> lines, <span style="color: #6666cc; font-weight: bold;">ushort</span> lookup<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">for</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> lines<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</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: #6666cc; font-weight: bold;">string</span> currentLine <span style="color: #008000;">=</span> lines<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
                <span style="color: #6666cc; font-weight: bold;">string</span> searchTerm <span style="color: #008000;">=</span> lookups<span style="color: #008000;">&#91;</span>lookup<span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// Get the first index of the search term</span>
                <span style="color: #6666cc; font-weight: bold;">int</span> firstIndex <span style="color: #008000;">=</span> currentLine<span style="color: #008000;">.</span><span style="color: #0000FF;">IndexOf</span><span style="color: #008000;">&#40;</span>searchTerm<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// The property wasn't found, continue</span>
                <span style="color: #008080; font-style: italic;">// through the file.</span>
                <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>firstIndex <span style="color: #008000;">==</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span>
                    <span style="color: #0600FF; font-weight: bold;">continue</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// Now we need to extract the data out of the string!</span>
                <span style="color: #6666cc; font-weight: bold;">string</span> data <span style="color: #008000;">=</span> currentLine<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span>firstIndex <span style="color: #008000;">+</span> searchTerm<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">TrimFront</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">TrimBack</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                oldValues<span style="color: #008000;">&#91;</span>lookup<span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> data<span style="color: #008000;">;</span>
                propertyValues<span style="color: #008000;">&#91;</span>lookup<span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> data<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;">void</span> Save<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> lines <span style="color: #008000;">=</span> File<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadAllLines</span><span style="color: #008000;">&#40;</span>Filename<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            Array enumValues <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">Enum</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetValues</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>Properties<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">ushort</span> prop <span style="color: #0600FF; font-weight: bold;">in</span> enumValues<span style="color: #008000;">&#41;</span>
                Save<span style="color: #008000;">&#40;</span>lines, prop<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            StreamWriter sw <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StreamWriter<span style="color: #008000;">&#40;</span>Filename, <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <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> lines<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
                sw<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>lines<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            sw<span style="color: #008000;">.</span><span style="color: #0000FF;">Close</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> Save<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> lines, <span style="color: #6666cc; font-weight: bold;">ushort</span> prop<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> lines<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</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: #6666cc; font-weight: bold;">string</span> currentLine <span style="color: #008000;">=</span> lines<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
                <span style="color: #6666cc; font-weight: bold;">string</span> searchTerm <span style="color: #008000;">=</span> lookups<span style="color: #008000;">&#91;</span>prop<span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// Get the first index of the search term</span>
                <span style="color: #6666cc; font-weight: bold;">int</span> firstIndex <span style="color: #008000;">=</span> currentLine<span style="color: #008000;">.</span><span style="color: #0000FF;">IndexOf</span><span style="color: #008000;">&#40;</span>searchTerm<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// The property wasn't found, continue</span>
                <span style="color: #008080; font-style: italic;">// through the file.</span>
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>firstIndex <span style="color: #008000;">==</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span>
                    <span style="color: #0600FF; font-weight: bold;">continue</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #6666cc; font-weight: bold;">string</span> oldValueLocal <span style="color: #008000;">=</span> oldValues<span style="color: #008000;">&#91;</span>prop<span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
                <span style="color: #6666cc; font-weight: bold;">string</span> newValueLocal <span style="color: #008000;">=</span> propertyValues<span style="color: #008000;">&#91;</span>prop<span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// If the old value didn't exist, we can adjust the </span>
                <span style="color: #008080; font-style: italic;">// replacement mechanism to remove the &quot;&quot; and add</span>
                <span style="color: #008080; font-style: italic;">// them back in.</span>
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #008000;">&#40;</span>oldValueLocal<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">&#123;</span>
                    oldValueLocal <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\&quot;</span><span style="color: #008080; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #008000;">;</span>
                    newValueLocal <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;<span style="color: #008080; font-weight: bold;">\&quot;</span>{0}<span style="color: #008080; font-weight: bold;">\&quot;</span>&quot;</span>, newValueLocal<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// We also need to catch people emptying strings out.</span>
                <span style="color: #008080; font-style: italic;">// To do this we adjust the saving mechanism the same way.</span>
                <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #008000;">&#40;</span>newValueLocal<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">&#123;</span>
                    oldValueLocal <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;<span style="color: #008080; font-weight: bold;">\&quot;</span>{0}<span style="color: #008080; font-weight: bold;">\&quot;</span>&quot;</span>, oldValueLocal<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                    newValueLocal <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\&quot;</span><span style="color: #008080; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// Replace the old value with the new value</span>
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #008000;">&#40;</span>newValueLocal<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
                        lines<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> lines<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Replace</span><span style="color: #008000;">&#40;</span>oldValueLocal, newValueLocal<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: #008080;">#endregion</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Filename <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">private</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> AssemblyVersion
        <span style="color: #008000;">&#123;</span>
            get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> GetProperty<span style="color: #008000;">&#40;</span>Properties<span style="color: #008000;">.</span><span style="color: #0000FF;">AssemblyVersion</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
            set <span style="color: #008000;">&#123;</span> SetProperty<span style="color: #008000;">&#40;</span>Properties<span style="color: #008000;">.</span><span style="color: #0000FF;">AssemblyVersion</span>, value<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;">string</span> FileVersion
        <span style="color: #008000;">&#123;</span>
            get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> GetProperty<span style="color: #008000;">&#40;</span>Properties<span style="color: #008000;">.</span><span style="color: #0000FF;">FileVersion</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
            set <span style="color: #008000;">&#123;</span> SetProperty<span style="color: #008000;">&#40;</span>Properties<span style="color: #008000;">.</span><span style="color: #0000FF;">FileVersion</span>, value<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;">string</span> Title 
        <span style="color: #008000;">&#123;</span>
            get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> GetProperty<span style="color: #008000;">&#40;</span>Properties<span style="color: #008000;">.</span><span style="color: #0000FF;">Title</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
            set <span style="color: #008000;">&#123;</span> SetProperty<span style="color: #008000;">&#40;</span>Properties<span style="color: #008000;">.</span><span style="color: #0000FF;">Title</span>, value<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;">string</span> Description
        <span style="color: #008000;">&#123;</span>
            get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> GetProperty<span style="color: #008000;">&#40;</span>Properties<span style="color: #008000;">.</span><span style="color: #0000FF;">Description</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
            set <span style="color: #008000;">&#123;</span> SetProperty<span style="color: #008000;">&#40;</span>Properties<span style="color: #008000;">.</span><span style="color: #0000FF;">Description</span>, value<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;">string</span> Company
        <span style="color: #008000;">&#123;</span>
            get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> GetProperty<span style="color: #008000;">&#40;</span>Properties<span style="color: #008000;">.</span><span style="color: #0000FF;">Company</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
            set <span style="color: #008000;">&#123;</span> SetProperty<span style="color: #008000;">&#40;</span>Properties<span style="color: #008000;">.</span><span style="color: #0000FF;">Company</span>, value<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;">string</span> Product 
        <span style="color: #008000;">&#123;</span>
            get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> GetProperty<span style="color: #008000;">&#40;</span>Properties<span style="color: #008000;">.</span><span style="color: #0000FF;">Product</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
            set <span style="color: #008000;">&#123;</span> SetProperty<span style="color: #008000;">&#40;</span>Properties<span style="color: #008000;">.</span><span style="color: #0000FF;">Product</span>, value<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;">string</span> Copyright
        <span style="color: #008000;">&#123;</span>
            get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> GetProperty<span style="color: #008000;">&#40;</span>Properties<span style="color: #008000;">.</span><span style="color: #0000FF;">Copyright</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
            set <span style="color: #008000;">&#123;</span> SetProperty<span style="color: #008000;">&#40;</span>Properties<span style="color: #008000;">.</span><span style="color: #0000FF;">Copyright</span>, value<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;">string</span> Trademark
        <span style="color: #008000;">&#123;</span>
            get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> GetProperty<span style="color: #008000;">&#40;</span>Properties<span style="color: #008000;">.</span><span style="color: #0000FF;">Trademark</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
            set <span style="color: #008000;">&#123;</span> SetProperty<span style="color: #008000;">&#40;</span>Properties<span style="color: #008000;">.</span><span style="color: #0000FF;">Trademark</span>, value<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;">string</span> Guid
        <span style="color: #008000;">&#123;</span>
            get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> GetProperty<span style="color: #008000;">&#40;</span>Properties<span style="color: #008000;">.</span><span style="color: #0000FF;">Guid</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
            set <span style="color: #008000;">&#123;</span> SetProperty<span style="color: #008000;">&#40;</span>Properties<span style="color: #008000;">.</span><span style="color: #0000FF;">Guid</span>, value<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>P.S. The code is not done, is not even close to being efficient or functionally complete. But it works!</p>
]]></content:encoded>
			<wfw:commentRss>http://jsedlak.org/2008/12/09/a-spark-of-genius/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>
	</channel>
</rss>
