<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>David Seah / Code</title>
	
	<link>http://davidseah.com/code</link>
	<description>Process Journal: Computer Programming Explorations</description>
	<lastBuildDate>Wed, 03 Apr 2013 02:11:10 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/DavidSeah/Code" /><feedburner:info uri="davidseah/code" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>HTML5 Crash Course: Digging into Javascript’s Weirdness</title>
		<link>http://feedproxy.google.com/~r/DavidSeah/Code/~3/N39MDD33-08/446</link>
		<comments>http://davidseah.com/code/archives/446#comments</comments>
		<pubDate>Wed, 13 Feb 2013 18:33:46 +0000</pubDate>
		<dc:creator>Dave Seah</dc:creator>
				<category><![CDATA[Project]]></category>
		<category><![CDATA[h5cc]]></category>

		<guid isPermaLink="false">http://davidseah.com/code/?p=446</guid>
		<description><![CDATA[Today I looked at the HTML5 Boilerplate and KnockoutJS. HTML5 Boilerplate is an application framework, meaning it&#8217;s a bare-bones way of structuring the way your HTML5 app loads resources and starts up. The alternative is to come up with your own organization. KnockoutJS is a pure Javascript library that implements a &#8220;Model &#8211; View &#8211; [...]
]]></description>
				<content:encoded><![CDATA[<p><em>This is a series of posts where I'm familiarizing myself with HTML5, which is new to me, presented as a series of notes to myself. The articles are tagged <a href="/code/archives/tag/h5cc" target="_blank">h5cc</a></em></p>

<hr/>


<p>Today I looked at the <a href="http://html5boilerplate.com/" target="_blank">HTML5 Boilerplate</a> and <a href="http://knockoutjs.com/" target="_blank">KnockoutJS</a>.</p>

<ul>
<li><p>HTML5 Boilerplate is an application framework, meaning it&#8217;s a bare-bones way of structuring the way your HTML5 app loads resources and starts up. The alternative is to come up with your own organization.</p></li>
<li><p>KnockoutJS is a pure Javascript library that implements a &#8220;Model &#8211; View &#8211; ViewModel&#8221; pattern in a clean, declarative way. I don&#8217;t yet know exactly what the ramifications are, but there&#8217;s an awesome <a href="http://learn.knockoutjs.com/" target="_blank">interactive tutorial</a> that walks you through it.</p></li>
</ul>

<p>Exploring these two environments today gave me additional insight into Javascript Weirdness related to its super-flexible object-oriented underpinnings. Basically everything is an object, even the ones that look like they might be functions or expressions. Writing Javascript is like living on the frontier, where STREET JUSTICE prevails over centralized language specification. There are no police. It&#8217;s up to you to enforce good programming habits, because Javascript doesn&#8217;t have any built-in to save you from shooting yourself in the foot.</p>

<p>As a result, you need to explore the Javascript community for a bit to understand what the law of the land is. Here&#8217;s some of the ones that I collected yesterday:</p>

<h4>Functions and Parenthesis</h4>

<p>The jQuery library is used everywhere, as it is one of those libraries that makes cross-browser DOM manipulation more &#8220;write-once&#8221; than &#8220;hope and pray it works in every browser&#8221;, which was the reason I avoided Javascript for so many years. To start executing your code that uses jQuery, though, you need to make sure it&#8217;s loaded. Since browsers load resources asynchronously, and also have slightly different ways they do it, jQuery itself provides a means to call your important startup function: <code>$(document).ready()</code>. The <code>ready()</code> call takes a function as an argument, which is executed when it&#8217;s time to run.</p>

<p>Unfortunately, in the non-trivial code examples, the ready() sometimes looks like this:</p>

<pre><code>(function($) { 
    $(function() {
        // code using $ as alias to jQuery
    });
})(jQuery);
</code></pre>

<p>Note the enclosing parenthesis. WHAT IS THIS? The short answer is that it converts the function() definition into an object expression which is then executed immediately, with the jQuery object passed to it as a parameter. WHY IN THE WORLD would you want to do something as convoluted as this? It&#8217;s to make the variables in your code private through the implicit creation of a Javascript &#8220;Closure&#8221;, which is the variable scope/state within a function. Since scripts in a browser all run in the same global namespace, they can overwrite each other. The code here creates an anonymous function, which in turn has its own closure with the $ variable assigned to the jQuery object. Everything inside has its own private space to declare whatever variables it wishes. Furthermore, the variable state exists after the code finishes running (another aspect of closures which is a bit different from other compiled languages), so you can safely assign event handler functions that refer to variables inside; they remain persistent.</p>

<p>The enclosing parenthesis is not necessary, but is added so you can tell right away that this is the case. Technically, Javascript will do the closure thing because of the <code>(jQuery)</code> tacked on the end, but it is unclear that it&#8217;s not a function definition until the very end. Adding the explicit () around it turns it into an explicit EXPRESSION instead of a function declaration; this is pure coding style because the () at the end <em>also</em> turns the function declaration syntax into an expression, but you see it all the way at the end and then you&#8217;re like, &#8220;damn, this isn&#8217;t a definition that&#8217;s called, it&#8217;s something that runs right now&#8221;.</p>

<p>You also see stuff like:</p>

<pre><code>!function($) { 
    $(function() {
        // code using $ as alias to jQuery
    });
}(jQuery);
</code></pre>

<p>I KNOW&#8230;WHAT? The ! is the unary negation operator, and it has the side effect of telling Javascript that an expression follows it. This is the same effect as using (), which is the arithmetic precedence operator. Both, being operators, turn the function into an expression, which is OK because expressions can be executed when a () is after them to turn it into a function invocation. That executes immediately. You could also use + or &#8211; as well and get the same effects&#8230;it&#8217;s purely for code readability.</p>

<p>It seems stupid, but it&#8217;s necessary to establish a private namespace (via closures) so you can write code without side effects like name collisions. Other languages have explicit ways of establishing a namespace, but Javascript does not.</p>

<p>Well, there is stuff like this:</p>

<pre><code>var myNameSpace = myNameSpace || {}
myNameSpace.myProperty = 1;
myNameSpace.myFunction = function(x) {};
</code></pre>

<p>This creates an object with objects assigned on-the-fly. Although since I used <code>var</code>, it&#8217;s local to the closure that the namespace is declared in. If I didn&#8217;t use <code>var</code>, I think that means it would be in the global namespace. Argh.</p>

<p>Anyway, that is enough of this for the day. Here&#8217;s the links that helped me understand this:</p>

<ul>
<li>http://michaux.ca/articles/an-important-pair-of-parens</li>
<li>http://blog.themeforest.net/tutorials/ask-jw-decoding-self-invoking-anonymous-functions/</li>
<li>http://www.adequatelygood.com/2010/3/JavaScript-Module-Pattern-In-Depth</li>
</ul>
<img src="http://feeds.feedburner.com/~r/DavidSeah/Code/~4/N39MDD33-08" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davidseah.com/code/archives/446/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://davidseah.com/code/archives/446</feedburner:origLink></item>
		<item>
		<title>HTML5 Crash Course: Some Starting Specification</title>
		<link>http://feedproxy.google.com/~r/DavidSeah/Code/~3/FQpuxl6Ss1k/443</link>
		<comments>http://davidseah.com/code/archives/443#comments</comments>
		<pubDate>Tue, 12 Feb 2013 17:12:21 +0000</pubDate>
		<dc:creator>Dave Seah</dc:creator>
				<category><![CDATA[Project]]></category>
		<category><![CDATA[h5cc]]></category>

		<guid isPermaLink="false">http://davidseah.com/code/?p=443</guid>
		<description><![CDATA[Yesterday I&#8217;d started to gather materials on HTML5 online before remembering that I had a book. I septn a little bit of time before going to sleep skimming the HTML5 and Javascript references I have, and came to the conclusion that HTML5 itself is just a bunch of new features. The ones I&#8217;m interested in [...]
]]></description>
				<content:encoded><![CDATA[<p><em>This is a series of posts where I'm familiarizing myself with HTML5, which is new to me, presented as a series of notes to myself. The articles are tagged <a href="/code/archives/tag/h5cc" target="_blank">h5cc</a></em></p>

<hr/>


<p>Yesterday I&#8217;d started to gather materials on HTML5 online before remembering that I had a book. I septn a little bit of time before going to sleep skimming the HTML5 and Javascript references I have, and came to the conclusion that HTML5 itself is just a bunch of new features. The ones I&#8217;m interested in are the Canvas, Sound, and Event models.</p>

<p>I need a very basic app to get focused. It might be instructive to write a simple GUI that gets something done. That would familiarize myself with a few things</p>

<ol>
<li>Basic HTML5 Layout of Elements</li>
<li>Using jQuery to bind events to the Elements</li>
<li>Writing some Javascript core code to send events and data requests to my own server using JSON or something</li>
</ol>

<p>I think I&#8217;d like to make a basic two-column text editor. This is something that&#8217;s been on my mind for a while.</p>

<p>I&#8217;m kind of tired, so I think I&#8217;ll do the setup of a framework later today or tomorrow. My testbed will be the davidseah.net server, where my other experiments live.</p>

<p>Here&#8217;s what the HTML5 page needs:</p>

<ul>
<li>HTML5 markup!</li>
<li>A place to LOAD script libraries!</li>
<li>A way to detect no javascript and gracefully degrade</li>
<li>A way to debug!</li>
</ul>

<p>That&#8217;s a good enough starting list.</p>
<img src="http://feeds.feedburner.com/~r/DavidSeah/Code/~4/FQpuxl6Ss1k" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davidseah.com/code/archives/443/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://davidseah.com/code/archives/443</feedburner:origLink></item>
		<item>
		<title>HTML5 Crash Course: Kickoff</title>
		<link>http://feedproxy.google.com/~r/DavidSeah/Code/~3/WL6JN_fB8c8/432</link>
		<comments>http://davidseah.com/code/archives/432#comments</comments>
		<pubDate>Mon, 11 Feb 2013 17:15:10 +0000</pubDate>
		<dc:creator>Dave Seah</dc:creator>
				<category><![CDATA[Kickoff]]></category>
		<category><![CDATA[h5cc]]></category>
		<category><![CDATA[HTML5]]></category>

		<guid isPermaLink="false">http://davidseah.com/code/?p=432</guid>
		<description><![CDATA[I&#8217;m starting an HTML5 crash course for myself. I&#8217;m applying the 15 minute ritual to this. This is the first 15 minutes. What I know about HTML5 is this: There are a bunch of new semantic elements, like &#60;section&#62;. I&#8217;ve read that this should be ignored because they&#8217;re really not the point. More interestingly, there [...]
]]></description>
				<content:encoded><![CDATA[<p><em>This is a series of posts where I'm familiarizing myself with HTML5, which is new to me, presented as a series of notes to myself. The articles are tagged <a href="/code/archives/tag/h5cc" target="_blank">h5cc</a></em></p>

<hr/>


<p>I&#8217;m starting an HTML5 crash course for myself. I&#8217;m applying the <a href="http://davidseah.com/blog/2013/01/maintaining-momentum-15-minutes-a-day/" target="_blank">15 minute ritual</a> to this. This is the first 15 minutes.</p>

<p>What I know about HTML5 is this:</p>

<ul>
<li>There are a bunch of new semantic elements, like <code>&lt;section&gt;</code>. I&#8217;ve read that this should be ignored because they&#8217;re really not the point.</li>
<li>More interestingly, there is a refined DOM and new Javascript features related to new objects like <code>&lt;canvas&gt;</code> and <code>&lt;audio&gt;</code>. </li>
</ul>

<p>To get my head wrapped around HTML5, I need to do the following:</p>

<ul>
<li>Find a good reference for HTML5 features, in a nutshell. I know I&#8217;ve seen them.</li>
<li>Consolidate my Javascript knowledge and workflow for editing and debugging.</li>
<li>Set up a development environment where I can play and keep track of samples, maybe in a VM</li>
<li>Maybe pick a framework to start with?</li>
<li>Or a project to build?</li>
</ul>

<p>There are some issues with HTML5 that prevent adoption by a broader base of users, I suspect.</p>

<h4>Getting Familiar</h4>

<ul>
<li><strong><a href="http://www.html5rocks.com/en/" target="_blank">HTML5 Rocks</a></strong> &#8211; some kind of aggregator</li>
<li><strong><a href="http://html5test.com/" target="_blank">HTML5 Test</a></strong> &#8211; for browser compliance, rated.</li>
<li><strong><a href="http://html5demos.com/" target="_blank">HTML5 Demos</a></strong> &#8211; a questionable demo site&#8230;have to review</li>
<li><strong><a href="http://coding.smashingmagazine.com/tag/html5" target="_blank">Smashing Magazine HTML5 tags</a></strong> &#8211; worth browsing?</li>
<li><strong><a href="http://html5.org/" target="_blank">html5.org</a></strong> &#8211; might be useful?</li>
</ul>

<p>These references, so far, kind of really don&#8217;t give me a starting place. Let&#8217;s look for Javascript HTML5 next. Oh, I forgot I had this book:</p>

<ul>
<li><strong>HTML5 Up and Running</strong> &#8211; by Mark Pilgrim. My cousin Ben recommended this to me.</li>
</ul>

<h4>Development</h4>

<ul>
<li><strong><a href="http://code.google.com/p/html5shiv/" target="_blank">HTML5 Shiv</a></strong> &#8211; some kind of IE9 enabling script</li>
<li><strong><a href="http://creativejs.com/" target="_blank">CreativeJS</a></strong> &#8211; neat javascript examples.</li>
<li><strong><a href="http://craftyjs.com/" target="_blank">CraftyJS</a></strong> &#8211; some kind of framework, it looks like.</li>
<li><strong>Maintainable Javascript</strong> &#8211; by Nicholas C. Zakas, also recommended by Ben.</li>
</ul>

<p>OOOPS, out of time!</p>

<hr />

<h4>Post Notes</h4>

<ul>
<li><strong><a href="http://diveintohtml5.info/" target="_blank">Dive Into HTML5</a></strong> &#8211; Apparently this is the source for the HTML5 book, maintained by the community.</li>
<li>Ben mentioned &#8220;Phonegap, HTML5 Boilerplate, jQuery Mobile&#8221;. What are those? Hm.</li>
<li><strong><a href="http://html5boilerplate.com/" target="_blank">HTML Boiler Plate</a></strong> &#8211; Ben said this was worth looking into.</li>
<li>The question: Do I want to make HTML5 my new interactive platform for development? That&#8217;s what I&#8217;m trying to find out.</li>
<li>Can I alternatively just have other people do my interactive work? My thoughts: no. I want control over this so I can wield it as an expressive medium for demonstrating my ideas. It would take a pretty exceptional developer to meet my needs. Perhaps if I could afford to pay someone, it&#8217;s worthwhile.</li>
</ul>
<img src="http://feeds.feedburner.com/~r/DavidSeah/Code/~4/WL6JN_fB8c8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davidseah.com/code/archives/432/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://davidseah.com/code/archives/432</feedburner:origLink></item>
		<item>
		<title>AS3 and Flash Document Class</title>
		<link>http://feedproxy.google.com/~r/DavidSeah/Code/~3/njdL0baa-wY/419</link>
		<comments>http://davidseah.com/code/archives/419#comments</comments>
		<pubDate>Thu, 01 Nov 2012 00:03:43 +0000</pubDate>
		<dc:creator>Dave Seah</dc:creator>
				<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://davidseah.com/code/?p=419</guid>
		<description><![CDATA[I&#8217;ve been thinking about porting some old ActionScript 2 code to ActionScript 3 and building an AIR app. The big advantage of ActionScript 3 is that it&#8217;s faster AND has a new package hierarchy that makes way more sense than the old one. For example, you can now instantiate a MovieClip or TextField with var [...]
]]></description>
				<content:encoded><![CDATA[<p>I&#8217;ve been thinking about porting some old ActionScript 2 code to ActionScript 3 and building an AIR app. The big advantage of ActionScript 3 is that it&#8217;s faster AND has a new package hierarchy that makes way more sense than the old one. For example, you can now instantiate a MovieClip or TextField with <code>var mc:MovieClip = new MovieClip()</code>, which looks totally normal. In ActionScript 2, you&#8217;d actually have to call DuplicateMovieClip() on an existing MovieClip instance.</p>

<p>Anyway, there&#8217;s always a little bit of magic involved in setting up a new &#8220;Hello World&#8221; style program. As is the norm, the official &#8220;Getting Started&#8221; material that I could find is simplistic to the point of uselessness&#8230;who writes these things? I don&#8217;t want to print &#8220;hello world&#8221;; I want to set up the basis of a rich flourishing application. Here&#8217;s the basic steps I followed with Flash Professional CS 5.5.</p>

<h4>1. Create a New Project</h4>

<p>Switched to the &#8220;Developer&#8221; Workspace Layout, which exposes the Project Panel. Created a new project, which creates a Flash .FLA stub file. We&#8217;ll be setting properties here later.</p>

<p>On a side note, the Project Manager is as terrible as I remember it from 2007, but at least it is just a mirror of an underlying directory structure. Nothing fancy here!</p>

<h4>2. Create the Startup Class</h4>

<p>The Flash file will end up holding any static library assets I create such as animations and graphics, but I will be using code attached to instances I create dynamically with my own scripting. Rather than stuff this all in Frame 1 of the timeline, there&#8217;s a way of binding a particular class to the Flash file. This is a class that is instantiated on startup. It&#8217;s known officially as the <strong>document class</strong>, and you set it under Properties for the .FLA file.</p>

<p>Class files in Actionscript 3 have the .as extension, and the filename must match the class name. There is also the notion of packages. I created a package path of <code>dseah</code>, which is implemented as a folder relative to the .FLA file.</p>

<pre><code>project_folder/
    dseah/
        MainClass.as
    DaveTest01.fla
    DaveTest01.swf
</code></pre>

<p>My <code>DaveTest01.fla</code> file is at the same level as the <code>dseah</code> folder. Inside this folder I created <code>MainClass.as</code>, which is declared as follows:</p>

<div><pre class="brush: as3; title: ; notranslate">
package dseah { 
    
    import flash.display.MovieClip; 

    public class MainClass extends MovieClip { 
        public function MainClass() { 
            trace ('Hello World'); 
        }
    } 
}
</pre></div>

<p>Two Important Notes:</p>

<ul>
<li>See how MainClass extends MovieClip? That&#8217;s because the class we&#8217;re setting as the document class is a representation of the .FLA file, which is a MovieClip. </li>
<li>When we go to properties to set the document class, we also have to use the entire package path, which here is <code>dseah.MainClass</code>.</li>
</ul>

<h4>3.0 Compile and Test!</h4>

<p>I clicked the &#8220;Test Project&#8221; button and did see that the trace worked, outputing &#8220;Hello World&#8221; to the console. Whee! So that&#8217;s it. Took me 90 minutes to dig up these basic setup facts while I was at Starbucks. I suppose that if I&#8217;d had a copy of Colin Moock&#8217;s <em>Actionscript 3: The Complete Reference</em> I would have saved myself some time. Grumble grumble.</p>
<img src="http://feeds.feedburner.com/~r/DavidSeah/Code/~4/njdL0baa-wY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davidseah.com/code/archives/419/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://davidseah.com/code/archives/419</feedburner:origLink></item>
		<item>
		<title>XNABS2: XML Read Routines</title>
		<link>http://feedproxy.google.com/~r/DavidSeah/Code/~3/M_Kn1SW3FHI/415</link>
		<comments>http://davidseah.com/code/archives/415#comments</comments>
		<pubDate>Thu, 13 Sep 2012 07:31:02 +0000</pubDate>
		<dc:creator>Dave Seah</dc:creator>
				<category><![CDATA[Project]]></category>
		<category><![CDATA[XNABS2]]></category>

		<guid isPermaLink="false">http://davidseah.com/code/?p=415</guid>
		<description><![CDATA[It&#8217;s been a while since I worked on this project. I want to get the XMLReader code to work right, then I&#8217;m going to see how the 3d model import workflow works. Which means I need to be able to make 3D models to import in the first place. First things first: let&#8217;s get this [...]
]]></description>
				<content:encoded><![CDATA[<p>It&#8217;s been a while since I worked on this project. I want to get the XMLReader code to work right, then I&#8217;m going to see how the 3d model import workflow works. Which means I need to be able to make 3D models to import in the first place. First things first: let&#8217;s get this XMLReader to work.</p>

<p>&#8230;</p>

<p>There were a few bugs leftover from the last attempt. I needed to specify the data directory, get rid of some overzealous try/catch exceptions which masked some important errors, and also add a specific enclosing XML tag in the data files. Updated the GameData.XML reader routines in BaseGameData, as well. Its XML reading is set up differently that in SettingsManager, which bugs me a bit, but I&#8217;m going to leave it alone.</p>

<p>And that about does it for the XML reading. I now have the following capabilities:</p>

<ul>
<li>Read key/value pairs from an XML file for common settings</li>
<li>Read Game setup data from an XML file</li>
</ul>

<p>Next up: Exporting a textured 3D model from Modo, Importing into XNA, Refactoring the View and Camera Classes.</p>
<img src="http://feeds.feedburner.com/~r/DavidSeah/Code/~4/M_Kn1SW3FHI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davidseah.com/code/archives/415/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://davidseah.com/code/archives/415</feedburner:origLink></item>
		<item>
		<title>XNABS2: Pondering the Next Stage, Loader Code</title>
		<link>http://feedproxy.google.com/~r/DavidSeah/Code/~3/ZuiMgUntSyg/406</link>
		<comments>http://davidseah.com/code/archives/406#comments</comments>
		<pubDate>Sat, 25 Aug 2012 06:34:39 +0000</pubDate>
		<dc:creator>Dave Seah</dc:creator>
				<category><![CDATA[Project]]></category>
		<category><![CDATA[XNABS2]]></category>

		<guid isPermaLink="false">http://davidseah.com/code/?p=406</guid>
		<description><![CDATA[Yesterday I finished cobbling together the first rudimentary bits of plumbing, clarifying some architectural issues that would make this version of the codebase a little easier to work with. There are a few interesting rabbit holes to explore along the way too, because .NET is a gigantic framework that encompasses all manner of programming tomfoolery. [...]
]]></description>
				<content:encoded><![CDATA[<p>Yesterday I finished cobbling together the first rudimentary bits of plumbing, clarifying some architectural issues that would make this version of the codebase a little easier to work with. There are a few interesting rabbit holes to explore along the way too, because .NET is a gigantic framework that encompasses all manner of programming tomfoolery.</p>

<p>Anyway, what&#8217;s in the current codebase is this:</p>

<ul>
<li>A startup sequence that works within the confines of XNA&#8217;s notion of a Game Loop + Initialiation.</li>
<li>A pretty well-defined procedure for handling the creation of my own main game objects of various type and vintage within the context of the XNA Game Loop + Initialization.</li>
<li>A rudimentary Debugger and Console, though I haven&#8217;t rewritten the command stuff yet</li>
<li>An Input Manager</li>
<li>A Settings Manager</li>
<li>A package hierarchy</li>
<li>A Mercurial repository and central Bitbucket.org repository</li>
<li>A rudimentary event listener structure and inter-object communication setup for main game objects</li>
<li>Graphics Initialization and Debug messages written to the console</li>
</ul>

<p>What was most important to me was to get the procedures, settings, and debugger setup so I could start building the rest and have some way to debug it. There are a lot of different things I could implement next, and I&#8217;d love to just dive write in and port the graphics routines. However, this routines are already written; what I should work on first is a new object creation language.</p>

<p>Currently, game engine 1.0 uses XML files to define everything that appears in the game at the Piece, Visual, and Player level. However, all this does is declare and name pieces and their associations visuals, along with players and their associated pieces. Also, the XML definition is a bit cumbersome, requiring multiple definitions to set up even a simple test. What I&#8217;d like for it to do is be a lot less redundant, and scale from very simple declarations of a handful of pieces in a space to complex hierarchies with behaviors attached to them. This GameXML language should be strive to minimally describe any game startup sequence. Each GameXML file also should serve as the defining datastructure for any level, presuming there are multiple levels in the game.</p>

<p>I&#8217;m thinking of something like this:</p>

<ul>
<li><p><code>[piece name="uniquePieceName" [attributes]]</code> is the way to declare any piece. This automatically adds the piece to a global piece dictionary if the name doesn&#8217;t exist, or updates its attributes if it already does. If the <code>name</code> attribute doesn&#8217;t exist, the piece is created with a unique name.</p></li>
<li><p>If the piece declaration occurs inside a <code>[player]</code> declaration, then the piece is created as described above AND it is added to the player&#8217;s piece array.</p></li>
<li><p>A piece can appear in multiple contexts like <code>[player]</code> and possibly other groups that are of interest to the game.</p></li>
</ul>

<p>Pieces have the following attributes:</p>

<ul>
<li>name = unique identifier of this piece</li>
<li>template = a piece name to copy attributes from</li>
</ul>

<p>Initialization that may execute a script somewhere to complete the initialization as well as tagging the pieces with this role</p>

<ul>
<li>type = type of piece, as defined in the game</li>
<li>visual = the type of visual to associate with this piece</li>
<li>role = logical role(s), comma separated</li>
<li>tag = logical tag(s), comma separated</li>
<li>behavior = an AI behavior</li>
<li>init = an initializing script to use to initialize any other parameters</li>
</ul>

<p>3D world representations</p>

<ul>
<li>position = vector3 coordinate of where this piece exists in space</li>
<li>scale = the size of the piece, defaults to unit 1 meter</li>
<li>direction = vector3 pointing </li>
<li>speed, maxspeed = how fast this can go up to maximum</li>
<li>turn, maxturn = how many degrees per second this object is allowed to turn</li>
</ul>

<p>So, the next step would be to write the parser that reads this information and calls a myriad of functions and factories to set all this stuff up. Not the most fun, but it will make creation much easier. After that, we&#8217;ll port the visual classes to verify that it&#8217;s working correctly. Then, we can port the physics engine and AI behaviors. Then, we can write game logic for the referee classes.</p>

<p>&#8230;</p>

<p>AFTER SLEEPING ON IT</p>

<p>The very minimal piece would be to do the piece reading, and maybe read player and visual data structures. So let me start by porting the GameScreen.Prologue() routines XML reader, and see what I can do to encapsulate it.</p>

<p>Where to put this routine? GameScreen.Prologue() is executed as soon as the screen is created and pushed on the stack, which is handled in the LoadContent() part of the call. So that&#8217;s where I&#8217;m going to put it in the new architecture, except we don&#8217;t have a GameScreen to hold this data. So where does this live now?</p>

<p>For now, I think it makes sense to store this information in BaseGame somewhere. Creating a BaseGame Data.cs partial class and a Game.XML file. I also need to create a rudimentary piece class now.</p>

<p>PORTING</p>

<p>BaseGame Data has a new InitalizeDataStructures() and LoadDataStructures() methods that are called from the corresponding BaseGame.Initalize() and BaseGame.LoadContent() master entry points.</p>

<p>The piece-building logic is built around XmlReader, and there&#8217;s the option of having a validating reader that using an XmlSchema. I&#8217;m going to disable this for now. Related to this, though, is a class called StringUtility that converts the strings in the XML file. The current implementation uses default values in case the attribute being read doesn&#8217;t exist. This makes the code not only harder to read, but it also will mask problems with the game initialization XML file. I&#8217;m going to remove this feature.</p>
<img src="http://feeds.feedburner.com/~r/DavidSeah/Code/~4/ZuiMgUntSyg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davidseah.com/code/archives/406/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://davidseah.com/code/archives/406</feedburner:origLink></item>
		<item>
		<title>IOS Session 01: Installing the environment</title>
		<link>http://feedproxy.google.com/~r/DavidSeah/Code/~3/Lv4FEvsZDIs/403</link>
		<comments>http://davidseah.com/code/archives/403#comments</comments>
		<pubDate>Fri, 24 Aug 2012 20:00:43 +0000</pubDate>
		<dc:creator>Dave Seah</dc:creator>
				<category><![CDATA[Kickoff]]></category>
		<category><![CDATA[Project]]></category>

		<guid isPermaLink="false">http://davidseah.com/code/?p=403</guid>
		<description><![CDATA[As part of the ETP on iPad initiative, Al Briggs has opened the nascent source code to me so I can see what a real app looks like in the dev environment! It&#8217;s also my hope to be able to implement some of the drawing code directly. First thing first&#8230;setting up! Installed Mountain Lion, Clean, [...]
]]></description>
				<content:encoded><![CDATA[<p>As part of the ETP on iPad initiative, Al Briggs has opened the nascent source code to me so I can see what a real app looks like in the dev environment! It&#8217;s also my hope to be able to implement some of the drawing code directly.</p>

<p>First thing first&#8230;setting up!</p>

<ol>
<li><p>Installed Mountain Lion, Clean, on my MacBook Pro 17&#8243; from late 2007. It&#8217;s old, but it works.</p></li>
<li><p>Installed Xcode via the App Store.</p></li>
<li><p>Installed <a href="http://mac.github.com" target="_blank">GitHub for Mac</a>.</p></li>
<li><p>Downloaded the project from Github.</p></li>
<li><p>Tried to open the project file and run it. BOOM. Missing header files. I wondered if I had to install the TestFlight SDK, so I emailed Al.</p></li>
<li><p>He emailed back that he was using <a href="http://cocoapods.org" target="_blank">CocoaPods</a>, which is a &#8220;library dependency manager&#8221; for Objective-C. You can specify the library you want to use, and CocoaPods takes care of grabbing everything that this library needs to work properly so you don&#8217;t have to do it manually. It relies on something called <a href="http://rubygems" target="_blank">/ruby</a>, which is a package manager for programs written in the ruby language, which I needed to install.</p></li>
</ol>

<p>After some farting around, I found I had to go to XCode&#8217;s Preferences and under Components select &#8220;Install Command Line Components&#8221; so I could run the <code>sudo gem install cocoapods</code> command (gem was already installed as part of the dev tools, I believe), followed by <code>pod setup</code>. I also had to run <code>sudo gem update --system</code> to get the most up-to-date version of whatever gem is.</p>

<p>After that, I went to the directory where I downloaded Al&#8217;s source, and typed <code>pod install</code> per Al&#8217;s instructions. It started doing this:</p>

<pre><code>    Installing Objection (0.13.0)
    Installing TestFlightSDK (1.0
    Installing OCMock (2.0.1)
    Using Objection (0.13.0)
    Using TestFlightSDK (1.0)
    Generating support files
</code></pre>

<p>Al then said to look at the &#8220;workspace file&#8221; that has been created, which I&#8217;m guessing is the file that ends with .xcworkspace. I double-clicked it to see what would happen. I was able to build the project and run it in a simulator. Very exciting!</p>

<p>I poked around the file explorer in xcode, and was reminded of just how old-school Objective-C is compared to C#. I&#8217;ll have to read up a bit on this.</p>
<img src="http://feeds.feedburner.com/~r/DavidSeah/Code/~4/Lv4FEvsZDIs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davidseah.com/code/archives/403/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://davidseah.com/code/archives/403</feedburner:origLink></item>
		<item>
		<title>Project: XNABS2 Session 02</title>
		<link>http://feedproxy.google.com/~r/DavidSeah/Code/~3/6c8tEK0uhMo/395</link>
		<comments>http://davidseah.com/code/archives/395#comments</comments>
		<pubDate>Thu, 23 Aug 2012 04:49:48 +0000</pubDate>
		<dc:creator>Dave Seah</dc:creator>
				<category><![CDATA[Project]]></category>
		<category><![CDATA[XNA]]></category>
		<category><![CDATA[XNABS2]]></category>

		<guid isPermaLink="false">http://davidseah.com/code/?p=395</guid>
		<description><![CDATA[SUMMARY: Last time, I got as far as creating the beginning of a Screen architecture and supporting concepts. The short-term goal is to get enough of this hooked in so I can just write debug messages to a console. That&#8217;s really all I want to shoot for tonight. ROUGH NOTE CAPTURE MODE Ok, I have [...]
]]></description>
				<content:encoded><![CDATA[<p>SUMMARY: Last time, I got as far as creating the beginning of a Screen architecture and supporting concepts. The short-term goal is to get enough of this hooked in so I can just write debug messages to a console. That&#8217;s really all I want to shoot for tonight. <hr id="more-395" class="more-separator" /></p>

<p>ROUGH NOTE CAPTURE MODE</p>

<p>Ok, I have a ScreenDisplayList<T> and that maintains a list of things for it to draw. Subclassers of the base Screen class can use that to handle the Load(), Unload(), and Paint() virtual methods to handle a display list. However, for our console screen, I think it will work a bit different and hold lines of text.</p>

<p>I just realized that the DisplayList<T> class I made is really just a List<T>. DUH.</p>

<p>DELEGATES?</p>

<p>Anyway, let&#8217;s grab the old DebugManager implementation for the console. It has a HandleKey to manage the text structures. We need to register DebugManager for Key events. <strong>Do I need to use a  delegate?</strong> What&#8217;s the way to do this in C#? A delegate is a TYPE that can hold a reference to any function that matches its signature.</p>

<pre><code>public **delegate** *function signature*
</code></pre>

<p>where an example would look like this:</p>

<pre><code>public delegate int MyDelegateFunction(int x, int y);
</code></pre>

<p>In this case, <code>MyDelegateFunction</code> is the delegate&#8217;s name. I&#8217;d declare it like this:</p>

<pre><code>MyDelegateFunction myFunction;
</code></pre>

<p>and I can assign it to any other function declaration that matches the original declaration&#8217;s function signature. So if I had a pair of functions like this&#8230;</p>

<p>public static int PlotPoint (int x, int y) { blah blah blah }
   public static int PlotPoint2 (intx, int y) { more blah blah }</p>

<p>&#8230;I could do something like this, if I&#8217;m understanding this right.</p>

<pre><code>MyDelegateFunction myFunction;
myFunction = PlotPoint;
myFunction(1,2);
myFunction = PlotPoint2;
myFunction(1,2);
</code></pre>

<p>You can pass a delegate as an object! So you can use it for asynchronous callbacks to handle events. A delegate object can also hold MULTIPLE methods to invoke. You can also assign a Delegate object a method within an object instance, and that works too. Pretty swanky. It&#8217;s <a href="http://msdn.microsoft.com/en-us/library/ms173172(v=vs.80).aspx" target="_blank">described here</a>.</p>

<p>So what do I know now that helps me write my thing? I need to add some delegates support to the InputManager class. However, if there are multiple listeners, I need to check the return value of EACH listener, and abort if the key event is handled. After doing some digging, I used the Delegate.GetInvocationList() and just called them one after the other. This seems to be what you&#8217;re supposed to do.</p>

<p>So now, the DebugManager needs to hook itself up to the InputManager and do some wiring in BaseGameINIT and BaseGameRun. Now using Console.WriteLine() to see if I&#8217;m capturing anything&#8230;cool! It works!</p>

<ul>
<li><p>There&#8217;s some interesting stuff here with Asynchronous callbacks&#8230;hmm. Any delegate type has a method called BeginInvoke added, which takes your parameters PLUS an AsyncCallback delegate and an object that &#8220;passes information into the callback method. It&#8217;s of type IASyncResult, and has some data in it (and probably other interesting things). More of this is described on the <a href="http://msdn.microsoft.com/en-us/library/2e08f6yc(v=vs.80).aspx" target="_blank">Calling Synchronous Methods Asynchronously</a> page.</p></li>
<li><p>On a side note, you can also create a delegate by calling new() on the type, initializing it with an existing function or method.</p>

<p>delegate void MyDelegateType ( int index );
MyDelagateType delegate = new MyDelegateType( someobject.somefunction );
delegate ( 10 );</p></li>
</ul>

<p>Mysterious and cool! I&#8217;m capturing text. Now I need to DRAW it.</p>

<p>PRINTING OUTPUT TO OUR OWN CONSOLE</p>

<p>Another source of text in our DebugConsole window will be a regular DebugManager. I just looked at the built-in System.Diagnostics.Debug and Trace classes, which seem to do a lot of the same thing. However, I&#8217;m not seeing a way to actually redirect the output back into our system so I can write it to the screen as an overlay.</p>

<p>I&#8217;ll sleep on this a bit and tackle it again tomorrow. I might as well just port the old code and use that, as it does work.</p>

<p>AND WE&#8217;RE BACK</p>

<p>Each type of Screen (ConsoleScreen, for example) is designed to integrate with a certain kind of visual data. They are what keep track of all the visual-related classes that draw things. I&#8217;m thinking that a GameScreen probably maintains the list of Visuals as associated with Pieces, and Pieces no longer maintain them. Each Screen-derived subclass would be initialized with a list of pieces it was interested in, and it would then create the visuals that go with each of those pieces. The pieces may have hints as to its appearance, name, and other unique qualities.</p>

<p>A slight detour: The GameXML file that defines all the elements of a working game will be split into multiple files. Pieces can be defined within various context like Player, etc. They have at minimum a unique Name. If a piece is declared for the first time, it is added to the global piece list, and also added to whatever context (e.g. Player)&#8217;s list.</p>

<p>Screens also declare what pieces it is interested in, though it can refer to any containing context (e.g. Player) also to read in all the pieces as needed.</p>

<p>Handling RenderPasses: A Screen may have several renderpasses. I&#8217;m not very clear on the architecture of HLSL shaders and renderpasses, so for now I&#8217;m going to just assume every screen has one renderpass, during which all visuals are drawn.</p>

<p>Ok, let&#8217;s get back to <strong>consolescreen</strong>&#8230;</p>

<p>BaseGame.Draw() is where the magic would be happening. It needs to tell the ScreenManager to draw. Let&#8217;s let&#8217;s add that.</p>

<p>&#8230; hours pass &#8230;</p>

<p>Ok, I have implemented the following:</p>

<ul>
<li><p>Diagrammed the correct startup sequence for the XNA Game Class. Initialize() is followed by LoadContent() which is then followed by Update() and Draw() in a loop, until the game quits.</p></li>
<li><p>In Initialize(), called the constructors of all &#8220;main game objects&#8221;, which create local data structures only.</p></li>
<li>After all main game objects are constructed, each of them gets their Initialize() function called.</li>
<li>After all main game objects are Initialized(), they are allowed to MakeConnections() with other game objects</li>
</ul>

<p>Then, XNA Game called LoadContent(), which is when other major game objects get their LoadContent() called as well.</p>

<p>This is a bit messy and confusing, so let me think about this a bit:</p>

<ul>
<li><p>What is a Main Game Object (MGO)? Generally, they&#8217;re singletons that perform some function, and there&#8217;s only one of them. They&#8217;re usually a manager class of some kind.</p></li>
<li><p>There are different categories of MGO. Some are System-level, like GraphicsDeviceManager, ContentManager, which are responsible for implementing the XNA subsystem and loading content that it can use. Related are MGOs like a SoundManager and InputManager. Let&#8217;s call these the HARDWARE LEVEL MGOs that encapsulate what the hardware and operating system can provide in a game-friendly package.</p></li>
<li><p>There are some high level MGOs that aren&#8217;t related to hardware/OS, such as DebugManager and SettingsManager. They are foundational and are used by everyone else.</p></li>
<li><p>The other MGOs implement systems that are used for the game portion. ScreenManager manages Screens, which uses the A/V MGOs to draw graphics. PhysicsManager imposes physics on pieces in the game. Players also manage pieces. Referee monitors the state of the game.</p></li>
<li><p>Game Loaders, Persistent Storage, Level Loading and State Preservation are high-level MGOs that manipulate the state of the game-related MGOs above.</p></li>
</ul>

<p>There&#8217;s some fishiness with the way I&#8217;m creating the ConsoleScreen. Currently, DebugManager is the data holder, and it maintains a &#8220;DebugBuffer&#8221; class with all the string tables and offsets that it needs to keep track of. It&#8217;s a class because they can be passed by reference to ConsoleScreen, which keeps its own reference to that datastructure so it can draw from it.</p>

<p>ConsoleScreen is created by a call to ScreenManager by DebugManager sometimes after DebugManager is fully initialized. This means:</p>

<ul>
<li>all MGOs constructed!</li>
<li>All MGOs initialized!</li>
<li>All MGOs have loaded content</li>
</ul>

<p>At this point, any MGO can request information from any other MGO and be confident that it&#8217;s ready. Right now, that is handled in the Start() method, which is called at the very end of BaseGame.LoadContent()</p>

<p>And that closes out this phase of development. We have a clean, ready-to-use base that can print text to the screen.</p>
<img src="http://feeds.feedburner.com/~r/DavidSeah/Code/~4/6c8tEK0uhMo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davidseah.com/code/archives/395/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://davidseah.com/code/archives/395</feedburner:origLink></item>
		<item>
		<title>Project: XNABS2</title>
		<link>http://feedproxy.google.com/~r/DavidSeah/Code/~3/r9ayKg4QUtk/390</link>
		<comments>http://davidseah.com/code/archives/390#comments</comments>
		<pubDate>Tue, 21 Aug 2012 05:14:03 +0000</pubDate>
		<dc:creator>Dave Seah</dc:creator>
				<category><![CDATA[Project]]></category>
		<category><![CDATA[XNABS2]]></category>

		<guid isPermaLink="false">http://davidseah.com/code/?p=390</guid>
		<description><![CDATA[SUMMARY: One of my long-term projects is to create a simple reusable 3D engine for playing with interactivity. I have a bunch of code from an earlier project that I&#8217;m refactoring into something that will serve as an example of a more complex XNA application. These notes are pretty rough, as they reflect speculative thinking [...]
]]></description>
				<content:encoded><![CDATA[<p><strong>SUMMARY:</strong> One of my long-term projects is to create a simple reusable 3D engine for playing with interactivity. I have a bunch of code from an earlier project that I&#8217;m refactoring into something that will serve as an example of a more complex XNA application. These notes are pretty rough, as they reflect speculative thinking about work in progress. These notes are also actively edited, so the contents may change unexpectedly. <hr id="more-390" class="more-separator" /></p>

<p>SO FAR:</p>

<p>I&#8217;ve ported some really basic stuff, refactoring along the way:</p>

<ul>
<li>BaseGame.cs split into partial classes <code>BaseGame Init.cs</code> and <code>BaseGame Run.cs</code></li>
<li>Singleton Pattern, made less twiddly to use, the basis for all Manager classes</li>
<li>SettingsManager.cs, cleaned up</li>
</ul>

<p>I&#8217;m now at the point that I want to port over something that actually draws on the screen. This is a complex subject, because we need to have a system for drawing, with a well-defined order of operations coordinated between multiple system objects.</p>

<p>THE OLD SYSTEM</p>

<p>Just about every modern video game uses multiple framebuffers, showing a completed frame while drawing on a hidden one before switching. Drawing a frame usually consists of multiple drawing passes, such as a 3D view with a 2D user interface superimposed on top of it. The drawing process itself may require multiple passes as well for special effects.</p>

<p>In our old game, we had the notion of &#8220;Screens&#8221; managed by a &#8220;ScreenManager&#8221;. Screens shared a common subclass that defined typical game loop behaviors, namely:</p>

<ul>
<li>Update() &#8211; processing time to do calculations</li>
<li>Paint() &#8211; the command to draw to the current framebuffer</li>
<li>HandleButtons() &#8211; event handling for gamepad button presses</li>
<li>HandleKey() &#8211; event handling for key presses</li>
<li>HandleRepeatedKey() &#8211; event handling for repeating key presses</li>
<li>Prologue() &#8211; pre-run initialization of data structures</li>
<li>Epilogue() &#8211; post-run cleanup and release of data structures</li>
</ul>

<p>There were three types of Screens defined: GameScreen, GUIScreen, and DebugScreen. ScreenManager managed them as a stack, drawn in the order listed.</p>

<p>What I didn&#8217;t like about this arrangement is that GameScreen is essentially the game program. It&#8217;s buried in the class hierarchy under Classes/Layout/GameScreen. I would never have thought to look there for game initialization and running, expecting it to be more top-level in the hierarchy. The second issue I had was that a Screen seems more like a display device to me, not something that would even have high-level logic in it. I can accept the notion of a Screen being smart enough to draw a display list, but I don&#8217;t like it having player AI and level logic stored in it. I&#8217;d prefer that to be somewhere else.</p>

<p>The other main screen is DebugScreen, which is what draws the debug console. It works in concert with the DebugManager, which is what keeps track of all the debug messages and processes commands. DebugScreen knows how to display that data using drawing commands, which I have no problem with. It is a cleaner separation between display code and data management. GUIScreen is a hacky screen that draws between GameScreen and DebugScreen, and was used for display giant bitmaps as signage when no one was playing the game in the museum space. It&#8217;s used by WorldPlayer, which is what implements the game logic through its various stages. This is a little screwed up too, so I&#8217;d want to clean it up.</p>

<p>THE NEW SYSTEM</p>

<p>I&#8217;m moving the game logic out of GameScreen. Screens and the ScreenManager will be implemented differently, as sets of display lists that are bound with data from other system players and objects. Instead of having game-functional names, they will have be organized by displaytype. For example:</p>

<ul>
<li>ThreeDeeScreen &#8211; draws a 3D world from its displaylist consisting of Visuals</li>
<li>ConsoleScreen &#8211; draws 2D text from its displaylist consisting of lines of text and other properties</li>
<li>SpriteScreen &#8211; draws 2D sprites from a displaylist consisting of sprite elements</li>
</ul>

<p>The ScreenManager will maintain a stack of these screens, and will also be called directly by the Game Loop. ScreenManager will know how to construct its screen stack based on XML files. Screens can be active or not, toggled by external controllers.</p>

<p>To handle actual text input, there&#8217;s a new class called InputManager that consolidates the gamepad button and keyboard state into one place. It&#8217;s called once during Game Loop so that all interested subscribers can receive notification. The DebugManager will be one of these, as will any Players that may need control input. There are some additional complexities that may be added, but for now we can just implement this pretty simply.</p>

<p>I&#8217;ve just written InputManager, and at a minimum I need to rewrite the Screen and ScreenManager so I can write ConsoleScreen. While the ScreenManager will eventually be initialized from a config file, for now I can just hard-code the creation chain.</p>

<p>SCREEN CLASS</p>

<p>Has notions of visibility, event consumption, viewport, and name. Virtual methods are Load(), Unload(), and Paint().</p>

<p>A Screen needs to have a source of data to draw, the &#8220;display list&#8221;. Should the base class have the notion of a DisplayList bound to it at creation time? Maybe something like ScreenDisplayList<Sprite>, ScreenDisplayList<Visual> generics. ScreenDisplayLists would accept an ADD command. The subclassers of Screen are responsible for implementing storage of a ScreenDisplayList<T>.</p>

<p>This might be overly fancy for what I need, but we&#8217;ll see how it goes.</p>
<img src="http://feeds.feedburner.com/~r/DavidSeah/Code/~4/r9ayKg4QUtk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davidseah.com/code/archives/390/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://davidseah.com/code/archives/390</feedburner:origLink></item>
		<item>
		<title>001 BaseSystem2 Kickoff</title>
		<link>http://feedproxy.google.com/~r/DavidSeah/Code/~3/8g1RdMCrBlc/370</link>
		<comments>http://davidseah.com/code/archives/370#comments</comments>
		<pubDate>Tue, 07 Aug 2012 06:10:53 +0000</pubDate>
		<dc:creator>Dave Seah</dc:creator>
				<category><![CDATA[Project]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Game]]></category>
		<category><![CDATA[XNA]]></category>

		<guid isPermaLink="false">http://davidseah.com/code/?p=370</guid>
		<description><![CDATA[I&#8217;ve been intending to update the XNA 2.0 game engine we made for Take a Stand into something cleaner running under XNA 4.0. Code notes follow on setting up the project and porting the first bit of code over: error logging classes. Intentions The name of our Visual Studio 2005 project, started in 2009, was [...]
]]></description>
				<content:encoded><![CDATA[<p>I&#8217;ve been intending to update the XNA 2.0 game engine we made for <em>Take a Stand</em> into something cleaner running under XNA 4.0. Code notes follow on setting up the project and porting the first bit of code over: error logging classes. <hr id="more-370" class="more-separator" /></p>

<h2>Intentions</h2>

<p>The name of our Visual Studio 2005 project, started in 2009, was &#8220;BaseSystem&#8221; out of no other reason than it was the base. I&#8217;ve named the new version BaseSystem2.</p>

<p>The strategy will be to port function from the old into the new, cleaning up along the way. There are some basic 2D graphic routines I want to port just so we can write text on the screen and support our console functions. Then, I would like to add some basic 2D GUI functions that make more sense than the old widgets. Toward the end of the project, we came up with better ways to organize 2D screen content on top of the 3D.</p>

<p>There is also a matter of just revisiting the game initialization and setting of XML-based runtime parameters. There&#8217;s plenty to do. I&#8217;ll document this step-by-step, and clean it up later.</p>

<h3>Create the Project</h3>

<p>I installed Visual Studio 2010 Professional, and then XNA Game Studio 4.0 Refresh on it. Created a new C# Windows Game solution called BaseSystem2. This created a few default program files:</p>

<ul>
<li>Program.cs</li>
<li>Game1.cs</li>
</ul>

<p>I also opened BaseSystem under Visual Studio 2005, and did a side-by-side comparison of Program.cs. This is the stub program that contains the Main() entry point in the Program class. It&#8217;s a static entry point, and I think all C# programs start like this.</p>

<p>The only thing it really does is create a new Game instance, and then calls its Run() method. This is where all the XNA magic happens, so that should be far more interesting.</p>

<p>Comparing this to the BaseSystem (hereafter referred to OldBase), there&#8217;s some additional things we&#8217;ve crammed in here that I&#8217;d forgotten about. There is a global exception handler adopted from <a href="http://www.doogal.co.uk/exception.php" target="_blank">doogal.co.uk</a>. This writes our error logs to disk for us. It&#8217;s part of our Utility classes. I think we can create a place for it in a Debugger namespace. I might as well move that code now.</p>

<p>Ooops, have to download the OldBase code, so I can copy it to NewBase. Ugh, I don&#8217;t like the way that VisualStudio named the various components&#8230;too many folder named BaseSystem2! I renamed the files and directories, then edited the .sln and .prj files to refer to the new directories after double-clicking the .sln file showed that there were reference errors. Double-clicking the solution file didn&#8217;t open Visual Studio 2010 the first time, though, after making this edit. Somehow the &#8220;default program&#8221; was set to use &#8220;Visual Studio 2010 Version Selector&#8221;, which didn&#8217;t do anything, as a result of that error. I just reset it to use VS2010.</p>

<p>Ok, now I can copy/paste some classes in! Creating &#8220;Debug.Logger&#8221; for these logger classes. I first tried to &#8220;add existing item&#8221;, but this didn&#8217;t create the folder hierarchy. I had to create the folder structure first, then add the files to that folder. I guess that&#8217;s OK&#8230;the folder hierarchy in the Solution view doesn&#8217;t have to match the disk layout. That&#8217;s probably more flexible.</p>

<p>I probably have to rewrite the namespace declarations for these files too. I&#8217;m also noting that TextFileLogger and LoggerImplementation can&#8217;t see the System.Windows.Forms package. VS2010 suggests I&#8217;m missing a reference. Let me see what references are here in the Solution Explorer:</p>

<ul>
<li>Microsoft.XNA.Framework.*</li>
<li>mscorlib</li>
<li>System</li>
<li>System.Core</li>
<li>System.Net</li>
<li>System.Xml</li>
<li>System.Xml.Linq</li>
</ul>

<p>It&#8217;ll be interesting to look through this later and see what&#8217;s in those references. A reference is how one links to a library, I guess, in Visual Studio 2010. I wonder how they are implemented or exist as files? Looking at the properties for the &#8220;System&#8221; reference:</p>

<ul>
<li>It&#8217;s an &#8220;Assembly&#8221;</li>
<li>C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework&#46;NETFramework\v4.0\Profile\Client\System.dll</li>
</ul>

<p>A C# program maybe isn&#8217;t statically linked as in the old days. Perhaps this library is a hardcoded path and the system loads it on demand. Will have to read up on that. ANYWAY, let me see if there are difference references in OldBase:</p>

<ul>
<li><strong>FarseerPhysics</strong></li>
<li>Microsoft.XNA.Framework.*</li>
<li>mscorlib</li>
<li><strong>Softimage.XWImporter</strong></li>
<li>System</li>
<li><strong>System.Data</strong></li>
<li><strong>System.Web</strong></li>
<li><del>System.Core</del></li>
<li><del>System.Net</del></li>
<li><strong>System.Web</strong></li>
<li><strong>System.Windows.Forms</strong></li>
<li>System.Xml</li>
<li><del>System.Xml.Linq</del></li>
<li><strong>XSIXNARuntime</strong></li>
</ul>

<p>So there are some differences. Three of these are game-specific (Farseer, Softimage, XSIXNA). The others might be new to Net 4.0 or renamed. I see I have to add System.Windows.Forms here. But how? Let me try right-clicking.</p>

<p>Add Reference -> .NET -> System.Windows.Forms&#8230;. that was easy. Let&#8217;s see if the errors have gone away in the loggers&#8230;yup.</p>

<p>At this point, we should be able to build&#8230;oops a few missing references. A function called HttpUtility.UrlEncode() doesn&#8217;t exist. Doing a google, I find it&#8217;s part of System.Web. I add that reference&#8230;but wait, System.Web isn&#8217;t listed. STUCK! A quick google, and apparently System.Web is part of the .NET 4 framework, but <a href="http://msdn.microsoft.com/en-us/library/cc656912.aspx" target="_blank">Visual Studio defaults to something called the .NET CLIENT PROFILE</a>, which is a subset of the complete .NET framework. The idea is that it&#8217;s smaller, and therfore deploys smaller. Changing the &#8220;Target Framework&#8221; can be done by going to the project properties (Project Menu) and selecting the Application tab. I changed it from &#8220;.NET 4 Client Profile&#8221; to &#8220;NET Profile&#8221;. Now I see a lot more System.Web references available, including the one I want. Success!</p>

<p>The two remaining errors refer to missing stuff from OldBase. One is a reference to BaseGame, which is called Game1 in NewBase. I need to move this, actually, into my namespace hierarchy and clean it up:</p>

<ul>
<li>My &#8216;Engine&#8217; solution has a classes folder, which might not be good practice. I probably should just let the folders live outside of this. </li>
<li>However, there are other folders in this same level, the bin and obj folders. I&#8217;m going to rename them to _bin and _obj so they sort toward the top by modifying project&#8217;s Build properties&#8230;however, i don&#8217;t see a place to set the obj output. Hm. Google shows that you can <a href="http://stackoverflow.com/questions/4735534/how-can-i-redirect-the-bin-and-obj-directories-to-a-different-location" target="_blank">add two properties</a> to change this by editing the .csproj file. I did this, and it works.</li>
<li>I&#8217;m just trying to make a clean directory here&#8230;sigh. I&#8217;ve made a directory manually called _src, and will try adding an existing item. Argh, this is annoying. The folder hierarchy in the solution explorer is what matters, apparently. </li>
</ul>

<p>Oh well. I reverted all those changes and will live with &#8220;source&#8221; being in my namespace. It seems retarded&#8230;I must be missing something. I supposed I could manually rename all the namespaces as I create new files, but I would rather not.</p>

<p>&#8230;</p>

<p>I&#8217;ve added the stuff in, and commented it. I added a Singleton class based on Bretton&#8217;s original, except I added an &#8220;S&#8221; accessor variable in addition to the verbose &#8220;GetSingleton()&#8221; call. Also documented some of the interesting features he&#8217;s using with the Generic class.</p>

<p>So what do I have now? The beginnings of BaseSystem with a skeleton outline.</p>
<img src="http://feeds.feedburner.com/~r/DavidSeah/Code/~4/8g1RdMCrBlc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davidseah.com/code/archives/370/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://davidseah.com/code/archives/370</feedburner:origLink></item>
	</channel>
</rss>
