<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Michael Larouche - The Day Dreamer</title>
	<atom:link href="http://michaellarouche.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://michaellarouche.wordpress.com</link>
	<description>Random thoughts and ramblings of a computer/music geek</description>
	<lastBuildDate>Wed, 28 Dec 2011 16:56:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='michaellarouche.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Michael Larouche - The Day Dreamer</title>
		<link>http://michaellarouche.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://michaellarouche.wordpress.com/osd.xml" title="Michael Larouche - The Day Dreamer" />
	<atom:link rel='hub' href='http://michaellarouche.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Create your own scripting language in C++ with Qt (Part 1)</title>
		<link>http://michaellarouche.wordpress.com/2011/12/28/create-your-own-scripting-language-in-cpp-with-qt-part-1/</link>
		<comments>http://michaellarouche.wordpress.com/2011/12/28/create-your-own-scripting-language-in-cpp-with-qt-part-1/#comments</comments>
		<pubDate>Wed, 28 Dec 2011 16:54:16 +0000</pubDate>
		<dc:creator>michaellarouche</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Qt]]></category>
		<category><![CDATA[Writing Compilers]]></category>
		<category><![CDATA[abstract syntax tree]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[compilation]]></category>
		<category><![CDATA[grammar analysis]]></category>
		<category><![CDATA[lexical analysis]]></category>
		<category><![CDATA[scripting language]]></category>

		<guid isPermaLink="false">http://michaellarouche.wordpress.com/?p=146</guid>
		<description><![CDATA[I&#8217;m being writing my own scripting language in Qt for the past month or so. My original goal was to have immediate feedback when trying various drawing methods using Qt drawing class QPainter. First I thought I could use QtScript and make it a 2 week programming task, but I decided to spice it a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=michaellarouche.wordpress.com&amp;blog=6504667&amp;post=146&amp;subd=michaellarouche&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m being writing my own scripting language in Qt for the past month or so. My original goal was to have immediate feedback when trying various drawing methods using Qt drawing class QPainter. First I thought I could use QtScript and make it a 2 week programming task, but I decided to spice it a bit and write the script language from scratch.</p>
<p>In this series of blog posts, I&#8217;m doing to teach you how I did it. I assume you know a bit about regular expressions, C++ and Qt. I will try to simplify the theory behind the best I can.</p>
<p>Bear in mind that my method is not the most efficient way to implement a scripting language. My goal with this project was to learn and have fun, not to worry about performance too much.</p>
<h1>Basics of compilation</h1>
<h2>Overview of the process</h2>
<p>In its simplest form, the compilation process is the act of translating an input into another format. Your C++ compiler takes a text file with instructions in it and translate it into machine code.</p>
<p><a href="http://michaellarouche.files.wordpress.com/2011/12/basic_compilation1.png"><img class="alignnone  wp-image-155" title="basic_compilation" src="http://michaellarouche.files.wordpress.com/2011/12/basic_compilation1.png?w=408&#038;h=131" alt="" width="408" height="131" /></a></p>
<p>A compiler needs several building blocks to understand a language and create another output. There are the <strong>lexical analysis (Lexer),</strong> the <strong>grammar analysis (Parser)</strong>,  an <strong>abstract syntax tree (AST)</strong> and an <strong>Interpreter</strong> or a <strong>CodeGenerator </strong>in a compiled language. I left out the semantic analysis because in our case, it will be done in the interpreter.</p>
<p><a href="http://michaellarouche.files.wordpress.com/2011/12/building_blocks.png"><img class="alignnone size-full wp-image-148" title="building_blocks" src="http://michaellarouche.files.wordpress.com/2011/12/building_blocks.png?w=510&#038;h=218" alt="" width="510" height="218" /></a></p>
<p>What&#8217;s lexical analysis and grammar analysis ? We going to find out in the following sections.</p>
<h2>Lexical analysis</h2>
<p>First let&#8217;s take a English sentence and identify the various lexical class.</p>
<pre>My father gave me a nice gift.</pre>
<p>If you were paying any attention in our English or French class. You will identify these as</p>
<table border="1">
<tbody>
<tr>
<td><strong>Fragment</strong></td>
<td><strong>Lexical class</strong></td>
</tr>
<tr>
<td>My</td>
<td>article</td>
</tr>
<tr>
<td>father</td>
<td>noun</td>
</tr>
<tr>
<td>gave</td>
<td>verb</td>
</tr>
<tr>
<td>me</td>
<td>pronoun</td>
</tr>
<tr>
<td>a</td>
<td>article</td>
</tr>
<tr>
<td>nice</td>
<td>adjective</td>
</tr>
<tr>
<td>gift</td>
<td>noun</td>
</tr>
</tbody>
</table>
<p>Identifying the various components of a sentence helps us in the grammar analysis by giving us a simplified representation of the sentence. If we put the sentence like this:</p>
<pre>article noun verb pronoun article adjective noun</pre>
<p>It is now easier to tell that our sentence is grammatically correct. If we shuffle the words around a bit like this:</p>
<pre>father my me gave nice a gift -&gt;
noun article pronoun verb adjective article noun</pre>
<p>We&#8217;re still able to identify the various components but the sentence make no sense from a grammar point of view. A noun is not supposed to be followed by a article for instance.</p>
<p>In a programming language, we can apply the same process</p>
<p><pre class="brush: cpp;">
void PrintDebugInformation(const char *format, int arg1)
{
    printf(format,arg1);
}
</pre></p>
<p>Here&#8217;s the various components</p>
<table border="1">
<tbody>
<tr>
<td><strong>Fragment</strong></td>
<td><strong>Lexical class</strong></td>
</tr>
<tr>
<td>void</td>
<td>KEYWORD_VOID</td>
</tr>
<tr>
<td>PrintDebugInformation</td>
<td>IDENTIFIER</td>
</tr>
<tr>
<td>(</td>
<td>LEFT_PARENTHESIS</td>
</tr>
<tr>
<td>const</td>
<td>KEYWORD_CONST</td>
</tr>
<tr>
<td>char</td>
<td>KEYWORD_CHAR</td>
</tr>
<tr>
<td>*</td>
<td>POINTER_SIGN</td>
</tr>
<tr>
<td>format</td>
<td>IDENTIFIER</td>
</tr>
<tr>
<td>,</td>
<td>COMMA</td>
</tr>
<tr>
<td>int</td>
<td>KEYWORD_INT</td>
</tr>
<tr>
<td>arg1</td>
<td>IDENTIFIER</td>
</tr>
<tr>
<td>)</td>
<td>RIGHT_PARENTHESIS</td>
</tr>
<tr>
<td>{</td>
<td>LEFT_BLOCK_SIGN</td>
</tr>
<tr>
<td>printf</td>
<td>IDENTIFIER</td>
</tr>
<tr>
<td>(</td>
<td>LEFT_PARENTHESIS</td>
</tr>
<tr>
<td>format</td>
<td>IDENTIFIER</td>
</tr>
<tr>
<td>,</td>
<td>COMMA</td>
</tr>
<tr>
<td>arg1</td>
<td>IDENTIFIER</td>
</tr>
<tr>
<td>)</td>
<td>RIGHT_PARENTHESIS</td>
</tr>
<tr>
<td>;</td>
<td>STATEMENT_END</td>
</tr>
<tr>
<td>}</td>
<td>RIGHT_BLOCK_SIGN</td>
</tr>
</tbody>
</table>
<p>Thus the job of a <strong>Lexer</strong> is to identify lexical classes, called tokens, from the input text. Like the English grammar analysis, it will simplify the grammar analysis. <strong>Note that I named the lexical class all in caps. It helps to separate them from a grammar rule.</strong></p>
<h2>Grammar analysis</h2>
<p>A grammar is a set of rules to describe a syntax, A syntax is how the lexical classes are structured together to create a cohesive and logical form.  Let&#8217;s take a C function call as an example.</p>
<p><pre class="brush: cpp;">
printf(format,arg1);
</pre></p>
<p>The lexical analysis give us this:</p>
<pre>IDENTIFIER LEFT_PARENTHESIS IDENTIFIER COMMA IDENTIFIER RIGHT_PARENTHESIS STATEMENT_END</pre>
<p>We could formulate a rule to identify a function call like:</p>
<pre>functionCall ::= IDENTIFIER LEFT_PARENTHESIS IDENTIFIER COMMA IDENTIFIER RIGHT_PARENTHESIS STATEMENT_END</pre>
<p>But it assume we pass only 2 arguments to our <em>functionCall</em>. What if we want to support 0, 1 or more arguments ? What if we could use the <strong>zero-or-more operator(*)</strong> from regular expression ? Using the <strong>*</strong> operator, our functionCall rule looks like this</p>
<pre>functionCall ::= IDENTIFIER LEFT_PARENTHESIS IDENTIFIER (COMMA IDENTIFIER)* RIGHT_PARENTHESIS STATEMENT_END</pre>
<p>Okay, now we can express various function arguments, but what if our <em>functionCall</em> does not have arguments ? We can use another regular expression operator, the <strong>zero-or-one operator(?) </strong>on the first <em>IDENTIFIER</em> after the parenthesis to parse the <em>functionCall</em> correctly.</p>
<pre>functionCall ::= IDENTIFIER LEFT_PARENTHESIS IDENTIFIER? (COMMA IDENTIFIER)* RIGHT_PARENTHESIS STATEMENT_END</pre>
<p>We should be fine with our functionCall rule right ?  What if we want to pass a number to a function ? We could use the <strong>alternative operator(|) </strong>right ?</p>
<pre>functionCall ::= IDENTIFIER LEFT_PARENTHESIS (IDENTIFIER|NUMBER)? (COMMA (IDENTIFIER|NUMBER))* RIGHT_PARENTHESIS STATEMENT_END</pre>
<p>While it could work if we only want to support variables and numbers as function arguments, it is not flexible enough if we want to support different types of arguments like a math expression, a function call, etc. We will use another grammar rule to express the term of a <em>functionCall</em></p>
<pre>term ::= IDENTIFIER | NUMBER
functionCall ::= IDENTIFIER LEFT_PARENTHESIS term? (COMMA term)* RIGHT_PARENTHESIS STATEMENT_END</pre>
<p>During the parsing process, the <em>term</em> rule in the <em>functionCall </em>rule will be expanded into the right lexical class. Here&#8217;s an example of a concrete parse tree of the function call defined above. As you can see, our lexical classes are all leaves in the tree. The goal of a rule is to create leaf nodes in a parse tree (conceptually of course).</p>
<p><a href="http://michaellarouche.files.wordpress.com/2011/12/concrete_parse_tree.png"><img class="alignnone size-full wp-image-152" title="concrete_parse_tree" src="http://michaellarouche.files.wordpress.com/2011/12/concrete_parse_tree.png?w=510&#038;h=166" alt="" width="510" height="166" /></a></p>
<p>The <strong>Parser</strong> job is to take a text input and validate the syntax of a language. It uses the grammar rules to define the order of parsing. But it can do more than just validate the syntax of a language. For each rule, we can add code to be executed each time the parser encounter that rule. It can be used to create the nodes in the Abtract Syntax Tree for instance.</p>
<p>In a next part, we&#8217;ll go deeper into defining grammar rules and how it translate to C++ code.</p>
<h2>Abstract Syntax Tree</h2>
<p>An abstract syntax tree is a tree thats represent operations in your language. It is called abstract because it doesn&#8217;t store the whole source code, it only store the data needed for each operation. Why we don&#8217;t store the whole information ? Because most information is redundant or only used to identify a language construct. Let&#8217;s take a variable declaration statement.</p>
<p><pre class="brush: cpp;">
int myVariable = 3*5*x;
</pre></p>
<p>It generates this Abtract Syntax Tree:</p>
<p><a href="http://michaellarouche.files.wordpress.com/2011/12/abstract_syntax_tree.png"><img class="alignnone size-full wp-image-159" title="abstract_syntax_tree" src="http://michaellarouche.files.wordpress.com/2011/12/abstract_syntax_tree.png?w=510&#038;h=266" alt="" width="510" height="266" /></a></p>
<p>As you can see, the int type, equal sign and the semicolon are not expressed in the tree. We just don&#8217;t need them. I also put the data stored in each node to show that an abstract syntax is not a dumb representation of the source code syntax, it is used for interpretation or code generation.</p>
<h2>Interpreter</h2>
<p>An <strong>Interpreter</strong> takes an AST, traverse it and execute an operation on each node. In a nutshell, it&#8217;s the <a href="http://en.wikipedia.org/wiki/Interpreter_pattern" target="_blank">Interpreter pattern</a> in the GoF Design Patterns book. We could also use the <a href="http://en.wikipedia.org/wiki/Visitor_pattern" target="_blank">Visitor pattern</a> to visit each node and make the AST independent of the code execution mechanism (ex: interpreted, JIT, compiled)</p>
<h2>Conclusion</h2>
<p>In this part, I introduced you to various concepts related to compilation: lexing, parsing, abstract tree and interpretation. In the next part, we will start coding the scripting language by implementing mathematic expression in our scripting language. Don&#8217;t worry, I won&#8217;t fall in the trap of others compilations tutorial, I will go further than math expressions <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/michaellarouche.wordpress.com/146/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/michaellarouche.wordpress.com/146/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/michaellarouche.wordpress.com/146/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/michaellarouche.wordpress.com/146/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/michaellarouche.wordpress.com/146/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/michaellarouche.wordpress.com/146/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/michaellarouche.wordpress.com/146/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/michaellarouche.wordpress.com/146/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/michaellarouche.wordpress.com/146/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/michaellarouche.wordpress.com/146/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/michaellarouche.wordpress.com/146/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/michaellarouche.wordpress.com/146/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/michaellarouche.wordpress.com/146/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/michaellarouche.wordpress.com/146/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=michaellarouche.wordpress.com&amp;blog=6504667&amp;post=146&amp;subd=michaellarouche&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://michaellarouche.wordpress.com/2011/12/28/create-your-own-scripting-language-in-cpp-with-qt-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/aeadb8b29fc572b2497dff871d9406bf?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">michaellarouche</media:title>
		</media:content>

		<media:content url="http://michaellarouche.files.wordpress.com/2011/12/basic_compilation1.png" medium="image">
			<media:title type="html">basic_compilation</media:title>
		</media:content>

		<media:content url="http://michaellarouche.files.wordpress.com/2011/12/building_blocks.png" medium="image">
			<media:title type="html">building_blocks</media:title>
		</media:content>

		<media:content url="http://michaellarouche.files.wordpress.com/2011/12/concrete_parse_tree.png" medium="image">
			<media:title type="html">concrete_parse_tree</media:title>
		</media:content>

		<media:content url="http://michaellarouche.files.wordpress.com/2011/12/abstract_syntax_tree.png" medium="image">
			<media:title type="html">abstract_syntax_tree</media:title>
		</media:content>
	</item>
		<item>
		<title>My Backstage migred to GitHub</title>
		<link>http://michaellarouche.wordpress.com/2009/10/15/my-backstage-migred-to-github/</link>
		<comments>http://michaellarouche.wordpress.com/2009/10/15/my-backstage-migred-to-github/#comments</comments>
		<pubDate>Thu, 15 Oct 2009 15:18:45 +0000</pubDate>
		<dc:creator>michaellarouche</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://michaellarouche.wordpress.com/?p=142</guid>
		<description><![CDATA[The Project &#8220;My Backstage&#8221; was migrated to GitHub. It compile fine on Windows 7 using latest Qt version. Check out Programming page for the link.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=michaellarouche.wordpress.com&amp;blog=6504667&amp;post=142&amp;subd=michaellarouche&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The Project &#8220;My Backstage&#8221; was migrated to GitHub. It compile fine on Windows 7 using latest Qt version. Check out <a href="/programming">Programming</a> page for the link.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/michaellarouche.wordpress.com/142/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/michaellarouche.wordpress.com/142/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/michaellarouche.wordpress.com/142/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/michaellarouche.wordpress.com/142/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/michaellarouche.wordpress.com/142/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/michaellarouche.wordpress.com/142/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/michaellarouche.wordpress.com/142/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/michaellarouche.wordpress.com/142/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/michaellarouche.wordpress.com/142/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/michaellarouche.wordpress.com/142/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/michaellarouche.wordpress.com/142/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/michaellarouche.wordpress.com/142/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/michaellarouche.wordpress.com/142/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/michaellarouche.wordpress.com/142/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=michaellarouche.wordpress.com&amp;blog=6504667&amp;post=142&amp;subd=michaellarouche&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://michaellarouche.wordpress.com/2009/10/15/my-backstage-migred-to-github/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/aeadb8b29fc572b2497dff871d9406bf?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">michaellarouche</media:title>
		</media:content>
	</item>
		<item>
		<title>Mayanezz: it&#8217;s a start !</title>
		<link>http://michaellarouche.wordpress.com/2009/09/26/mayanezz-its-a-start/</link>
		<comments>http://michaellarouche.wordpress.com/2009/09/26/mayanezz-its-a-start/#comments</comments>
		<pubDate>Sun, 27 Sep 2009 00:07:47 +0000</pubDate>
		<dc:creator>michaellarouche</dc:creator>
				<category><![CDATA[Game Programming]]></category>
		<category><![CDATA[Mayanezz]]></category>

		<guid isPermaLink="false">http://michaellarouche.wordpress.com/?p=135</guid>
		<description><![CDATA[Our game project for our final year of university has finally begun ! Even if I&#8217;m not part of the Game Programming degree, I was still able to join the team as part of a project course. What is Mayanezz ? Mayaznezz is 2d game top-view action adventure game (think Zelda) set in a fictional [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=michaellarouche.wordpress.com&amp;blog=6504667&amp;post=135&amp;subd=michaellarouche&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Our game project for our final year of university has finally begun ! Even if I&#8217;m not part of the Game Programming degree, I was still able to join the team as part of a project course.</p>
<h1>What is Mayanezz ?</h1>
<p>Mayaznezz is 2d game top-view action adventure game (think Zelda) set in a fictional Maya universe. We play the role of a young Mayan warrior pursuing the power of 6 Mayans Gods. The story outline is still to be determined. Although we know we&#8217;ll mix some elements from the Mysterious Cities of Gold in it.</p>
<p>Each main dungeon will be available from the beginning of the game. Dungeons will not require special items to be completed, although the items will make the dungeons much easier.</p>
<p>The items will be scattered across the land, either given by key people or found in secret places.</p>
<p>Note that Mayanezz is code name, it is not the final name of the game</p>
<h1>The Team</h1>
<p>Meet our team ! Each member is involved in every part of the process, except for the graphic side.</p>
<ul>
<li>Fred Imbeault: Programmer, ScrumMaster, Game Design, Level Design</li>
<li>Jimmy Provencher: Programmer, Game Design, Level Design, Story Planner</li>
<li>Bryan Bergeron: Integration programmer, Artist, Game Design, Level Design</li>
<li>and me: Programmer, Level Design, Story Planner</li>
</ul>
<h1>First meetings</h1>
<p>Each game development team (3 for this year) is assigned to a local where only the member of the teams has access. It took about 3 weeks to get our local. Thus, our first meeting was delayed a lot. During our first meeting, we got a crash course of SCRUM from Fred. For those who doesn&#8217;t know <a title="SCRUM on wikipedia" href="http://en.wikipedia.org/wiki/Scrum_(development)">SCRUM</a>, it&#8217;s a agile project management methodology used by many game and software company. SCRUM is focused on user stories, which are requirement based on the point of view of the user. For example: &#8220;As a player, I want to attack enemies&#8221;.</p>
<p>After the crash course on SCRUM, we started the product backlog. A Product Backlog is a collection of user stories that we want to get into the game. It is saved during the whole project and serve as basis for the sprint.</p>
<p>A Sprint is a iteration in the process. It&#8217;s usually last from 2 to 4 weeks (team choice). We chose user stories from the backlog based on our velocity. A velocity is the amount of user points we can do in a sprint.</p>
<p>In our cases, the Sprint 1 looks like this:</p>
<ul>
<li>Allow the character to navigate on a map (screen size)</li>
<li>Listen to background music</li>
<li>Showing a enemy with basic movements</li>
</ul>
<p>Our sprint time is set to 4 weeks. Our velocity is quite low because we don&#8217;t work full time on the project.</p>
<h1>Setuping the environment</h1>
<p>A great deal of our time was to setup our environment. Each PC is installed with Windows 7 Professional, yes the final version (gotta love MSDN-AA) with Visual Studio 2008 SP1. We&#8217;ve setup our own server, which is running on Windows Server 2008. The server is running <a href="http://www.visualsvn.com/server/">VisualSVN server</a> for source code management,<a href="http://www.agile42.com/cms/pages/agilo/"> Trac+Agilo</a> for SCRUM, ticket management and wiki, <a href="http://ccnet.thoughtworks.com/">CruiseControl.NET</a> for continous integration. As for myself, I am using <a href="http://www.devexpress.com/Products/Visual_Studio_Add-in/RefactorCPP/">DevExpress Refactor for C++</a> and <a href="http://ankhsvn.open.collab.net/">AnkhSVN</a> Visual Studio plugin.</p>
<h1>Technologies used</h1>
<p>We are coding our own engine from scratch in C++.  Most of the backend will use SDL and related libraries. We are using <a href="http://unittest-cpp.sourceforge.net/">UnitTest++</a> and <a href="http://code.google.com/p/googlemock/">Google Mock</a> for unit testing. We are planning to use <a href="http://eigen.tuxfamily.org/index.php?title=Main_Page">Eigen</a> for linear algebra related code. For map editing, we will modify <a href="http://mapeditor.org/">Tiled</a> to suit our needs. For scripting, we are going to use <a href="http://www.lua.org/">Lua</a>.</p>
<h1>As for today</h1>
<p>The code is started ! I started working on the user story &#8220;Listen to background music&#8221;. It should be done soon enough and move to another task.</p>
<p>See you next time !</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/michaellarouche.wordpress.com/135/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/michaellarouche.wordpress.com/135/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/michaellarouche.wordpress.com/135/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/michaellarouche.wordpress.com/135/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/michaellarouche.wordpress.com/135/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/michaellarouche.wordpress.com/135/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/michaellarouche.wordpress.com/135/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/michaellarouche.wordpress.com/135/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/michaellarouche.wordpress.com/135/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/michaellarouche.wordpress.com/135/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/michaellarouche.wordpress.com/135/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/michaellarouche.wordpress.com/135/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/michaellarouche.wordpress.com/135/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/michaellarouche.wordpress.com/135/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=michaellarouche.wordpress.com&amp;blog=6504667&amp;post=135&amp;subd=michaellarouche&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://michaellarouche.wordpress.com/2009/09/26/mayanezz-its-a-start/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/aeadb8b29fc572b2497dff871d9406bf?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">michaellarouche</media:title>
		</media:content>
	</item>
		<item>
		<title>Programming page updated</title>
		<link>http://michaellarouche.wordpress.com/2009/09/13/programming-page-updated/</link>
		<comments>http://michaellarouche.wordpress.com/2009/09/13/programming-page-updated/#comments</comments>
		<pubDate>Mon, 14 Sep 2009 02:15:02 +0000</pubDate>
		<dc:creator>michaellarouche</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://michaellarouche.wordpress.com/?p=128</guid>
		<description><![CDATA[I updated my programming page to include libpcg  and LegacySPC source code, both hosted on GitHub. Also, I added links to my trainers for the following games: Final Fantasy 1, Final Fantasy 3, Super Robot Wars EX<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=michaellarouche.wordpress.com&amp;blog=6504667&amp;post=128&amp;subd=michaellarouche&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I updated my programming page to include libpcg  and LegacySPC source code, both hosted on <a href="http://github.com/">GitHub</a>. Also, I added links to my trainers for the following games: Final Fantasy 1, Final Fantasy 3, Super Robot Wars EX</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/michaellarouche.wordpress.com/128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/michaellarouche.wordpress.com/128/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/michaellarouche.wordpress.com/128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/michaellarouche.wordpress.com/128/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/michaellarouche.wordpress.com/128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/michaellarouche.wordpress.com/128/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/michaellarouche.wordpress.com/128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/michaellarouche.wordpress.com/128/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/michaellarouche.wordpress.com/128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/michaellarouche.wordpress.com/128/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/michaellarouche.wordpress.com/128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/michaellarouche.wordpress.com/128/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/michaellarouche.wordpress.com/128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/michaellarouche.wordpress.com/128/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=michaellarouche.wordpress.com&amp;blog=6504667&amp;post=128&amp;subd=michaellarouche&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://michaellarouche.wordpress.com/2009/09/13/programming-page-updated/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/aeadb8b29fc572b2497dff871d9406bf?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">michaellarouche</media:title>
		</media:content>
	</item>
		<item>
		<title>Refactoring game legacy code Part 2 &#8211; Cleaning up the terrain</title>
		<link>http://michaellarouche.wordpress.com/2009/07/29/refactoring-game-legacy-code-part-2-cleaning-up-the-terrain/</link>
		<comments>http://michaellarouche.wordpress.com/2009/07/29/refactoring-game-legacy-code-part-2-cleaning-up-the-terrain/#comments</comments>
		<pubDate>Thu, 30 Jul 2009 03:24:46 +0000</pubDate>
		<dc:creator>michaellarouche</dc:creator>
				<category><![CDATA[Game Programming]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[clean code]]></category>
		<category><![CDATA[craftmanship]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[legacy code]]></category>
		<category><![CDATA[refactoring]]></category>
		<category><![CDATA[tdd]]></category>

		<guid isPermaLink="false">http://michaellarouche.wordpress.com/2009/07/29/refactoring-game-legacy-code-part-2-cleaning-up-the-terrain/</guid>
		<description><![CDATA[In last episode, I introduced my project about refactoring my Tetris clone code. In order to get a feel of the code, I&#8217;m going to do some small mechanical refactorings. I will do only safe renames and extract method in the class related to Block and Piece. I will not try to alter behavior before [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=michaellarouche.wordpress.com&amp;blog=6504667&amp;post=116&amp;subd=michaellarouche&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In last <a title="Refactoring game legacy code: Part 1 - Introduction" href="http://michaellarouche.wordpress.com/2009/07/26/refactoring-game-legacy-code-part-1-introduction/">episode</a>, I introduced my project about refactoring my Tetris clone code.</p>
<p>In order to get a feel of the code, I&#8217;m going to do some small mechanical refactorings. I will do only safe renames and extract method in the class related to Block and Piece. I will not try to alter behavior before I write my characterization tests (will explain in a later article). I am using Visual Studio 2005 and <a title="CodeRush" href="http://www.devexpress.com/Products/Visual_Studio_Add-in/Coding_Assistance/">Code Rush</a>(which include Refactor! C++) free version. <a title="Visual Assist X home page" href="http://www.wholetomato.com">Visual Assist X</a> will be fine too but it costs money.</p>
<p>Let&#8217;s go deep into the code !</p>
<p>The first thing I noticed when reading the code related to Piece and Block was this:</p>
<p><pre class="brush: cpp;">
void Piece::moveBlocks(int deltaX, int deltaY)
{
 for(int i=0; i&lt;TetraSize; i++)
 {
  m_tetra[i].move( m_tetra[i].x() + deltaX, m_tetra[i].y() + deltaY );
 }
}
</pre></p>
<p>Why do I need to query the X and Y coordinates of each block forming the tetra in order to move them ? This is a violation of the principle &#8220;<a title="Tell, don't ask" href="http://www.pragprog.com/articles/tell-dont-ask">Tell, don&#8217;t ask</a>&#8220;. I&#8217;m going to refactor in small steps (like strongly suggested in Martin Folwer&#8217;s Refactoring).</p>
<p>First I renamed the <code>move</code> method of Block class to <code>moveAbsolute</code> since it&#8217;s clearly what this method is doing. After, I created a new method called <code>Block::move</code> which really moved according to deltaX and deltaY. Then, I looked up all the calls of moveAbsolute and make then use the new <code>move()</code> if they required this new behavior.</p>
<p>I found a interesting thing. All calls to <code>moveAbsolute</code> are in my poor handling of the Piece. Thus in the future, we will be able to remove this method after several refactorings. This single rename make my bad code stood out of the rest.</p>
<p>After, my random look into the code, I saw this big function:</p>
<p><pre class="brush: cpp;">
SDL_Surface *BlockSurfaceManager::get(BlockColor color)
{
 if( d_ptr-&gt;surfaceCache.count(color) == 0 )
 {
  SDL_Surface *newSurface = SDL_CreateRGBSurface(SDL_HWSURFACE, BlockWidth, BlockHeight, ColorDepth, 0, 0, 0, 0);

  SDL_Rect blockRect;
  blockRect.x=0;
  blockRect.y=0;
  blockRect.w=BlockWidth-1;
  blockRect.h=BlockHeight-1;

  Uint8 r=0,g=0,b=0;
  switch(color)
  {
   case BlockCyan:
    r=0; g=240; b=240;
    break;
   case BlockBlue:
    r=0; g=0; b=240;
    break;
   case BlockOrange:
    r=240; g=160; b=0;
    break;
   case BlockYellow:
    r=240; g=240; b=0;
    break;
   case BlockGreen:
    r=0; g=240; b=0;
    break;
   case BlockPurple:
    r=160; g=0; b=240;
    break;
   case BlockRed:
    r=240; g=0; b=0;
    break;
  }

  SDL_FillRect(newSurface, &amp;blockRect, SDL_MapRGB(newSurface-&gt;format, r,g,b));

  d_ptr-&gt;surfaceCache[color] = newSurface;
 }

 return d_ptr-&gt;surfaceCache[color];
}
</pre></p>
<p>As you can see, the if block is creating a new surface if the color has not been asked yet. The problem is this block is in the way of the main logic of my method, which is to retrieve a cached surface for the given color. Using &#8220;Extract Method&#8221;, I can make my intent more clear.</p>
<p><pre class="brush: cpp;">
SDL_Surface *BlockSurfaceManager::get(BlockColor color)
{
 if( d_ptr-&gt;surfaceCache.count(color) == 0 )
 {
  createCachedSurfaceFor(color);
 }

 return d_ptr-&gt;surfaceCache[color];
}
</pre></p>
<p>I could go very zealot while refactoring this class. For the intend of this article, I&#8217;m going to &#8216;give my life for Aiur&#8217; errrr make my intention even more clear <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>I will extract the if condition into its own method:</p>
<p><pre class="brush: cpp;">
SDL_Surface *BlockSurfaceManager::get(BlockColor color)
{
 if( hasNoCachedSurfaceFor(color) )
 {
  createCachedSurfaceFor(color);
 }

 return d_ptr-&gt;surfaceCache[color];
}

bool BlockSurfaceManager::hasNoCachedSurfaceFor(BlockColor &amp;color)
{
 return d_ptr-&gt;surfaceCache.count(color) == 0;
}
</pre></p>
<p>As you can see, it makes my intent more clear. Also, when you extract method with CodeRush, please double check the header if your method has the correct visibility.</p>
<p>For the beverity of the article, I will not describe in details the mechanics of the refactoring of <code>createCachedSurfaceFor()</code> but here&#8217;s for your viewing pleasure:</p>
<p><pre class="brush: cpp;">
void BlockSurfaceManager::createCachedSurfaceFor(BlockColor &amp;color)
{
 d_ptr-&gt;surfaceCache[color] = createBlockSurface(color);
}

SDL_Surface* BlockSurfaceManager::createBlockSurface(BlockColor &amp;color)
{
 SDL_Surface *newSurface = SDL_CreateRGBSurface(SDL_HWSURFACE, BlockWidth, BlockHeight, ColorDepth, 0, 0, 0, 0);

 Uint8 r=0,g=0,b=0;

 getBlockColorRgb(color, r, g, b);

 drawBlockSurface(newSurface, r, g, b);

 return newSurface;
}

void BlockSurfaceManager::drawBlockSurface(SDL_Surface *newSurface, Uint8 &amp;r, Uint8 &amp;g, Uint8 &amp;b)
{
 SDL_Rect blockRect = createBlockRect();
 SDL_FillRect(newSurface, &amp;blockRect, SDL_MapRGB(newSurface-&gt;format, r,g,b));
}

SDL_Rect BlockSurfaceManager::createBlockRect()
{
 SDL_Rect blockRect;
 blockRect.x=0;
 blockRect.y=0;
 blockRect.w=BlockWidth-1;
 blockRect.h=BlockHeight-1;
 return blockRect;
}

void BlockSurfaceManager::getBlockColorRgb(BlockColor &amp;color, Uint8 &amp;r, Uint8 &amp;g, Uint8 &amp;b)
{
 switch(color)
 {
 case BlockCyan:
  r=0; g=240; b=240;
  break;
 case BlockBlue:
  r=0; g=0; b=240;
  break;
 case BlockOrange:
  r=240; g=160; b=0;
  break;
 case BlockYellow:
  r=240; g=240; b=0;
  break;
 case BlockGreen:
  r=0; g=240; b=0;
  break;
 case BlockPurple:
  r=160; g=0; b=240;
  break;
 case BlockRed:
  r=240; g=0; b=0;
  break;
 }
}
</pre></p>
<p>The code has more lines, but the intend of the code is more clear. I stop here for the moment but I should really factor the surface creator into its own class. BlockSurfaceManager has two responsibilities: Managing the surface cache AND creating the surface. This does not follow the <a title="SRP" href="http://en.wikipedia.org/wiki/Single_responsibility_principle">Single Responsibility Principle </a> where each class should have <strong>one reason to change.</strong></p>
<p>I will commit the changes I&#8217;ve made. Let&#8217;s return back to Piece. In <code>Piece::rotate()</code> I have this snipped of code</p>
<p><pre class="brush: cpp;">
if( type() == Piece_Square )
 {
  m_direction = newDirection;
  return;
 }

 Block *rotatedBlocks = getBlocks(newDirection);
 if( tryMove(0, 0, rotatedBlocks) )
 {
  delete[] m_tetra;
  m_tetra = rotatedBlocks;
  m_direction = newDirection;
 }
 else
 {
  delete[] rotatedBlocks;
 }
</pre></p>
<p>I smell something bad in here. The base class make assumption about its derived classes. I will leave it here for now because I haven&#8217;t written my test and I could alter behavior by removing the faulty block.</p>
<p>Here&#8217;s another obscure method:</p>
<p><pre class="brush: cpp;">
bool Piece::tryMove(int deltaX, int deltaY, Block *block)
{
 bool canMove=true;

 for(int i=0; i&lt;TetraSize; i++)
 {
  if( (block[i].x() + deltaX) &gt;= BoardEndX ||
   (block[i].x() + deltaX) &lt; BoardX ||
   m_board-&gt;blockAt(block[i].x() + deltaX, block[i].y() + deltaY) ||
   (block[i].y() + deltaY) &lt; BoardY ||
   (block[i].y() + deltaY) &gt;= BoardEndY
   )
  {
   canMove=false;
   break;
  }
 }

 return canMove;
}
</pre></p>
<p>The if condition is confusing as hell. With simple reordering and extract method, it becomes this:</p>
<p><pre class="brush: cpp;">
if( isOutsideBoard(block[i], deltaX, deltaY) ||
   hasBlockingBlock(block[i], deltaX, deltaY) )
  {
   canMove=false;
   break;
  }
</pre></p>
<p><pre class="brush: cpp;">
bool Piece::isOutsideBoard(const Block &amp;block, int deltaX, int deltaY) const
{
 return (block.x() + deltaX) &gt;= BoardEndX ||
   (block.x() + deltaX) &lt; BoardX ||
   (block.y() + deltaY) &lt; BoardY ||
   (block.y() + deltaY) &gt;= BoardEndY;
}

bool Piece::hasBlockingBlock(const Block &amp;block, int deltaX, int deltaY)
{
 return m_board-&gt;blockAt(block.x() + deltaX, block.y() + deltaY);
}
</pre></p>
<p>Make much more sense. Let&#8217;s commit this. As for the derived classes of Piece, I won&#8217;t ever dare to touch them before I write some tests.</p>
<p>Let&#8217;s call done for the day. In the next episode, I will show you how to make the code into test harness.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/michaellarouche.wordpress.com/116/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/michaellarouche.wordpress.com/116/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/michaellarouche.wordpress.com/116/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/michaellarouche.wordpress.com/116/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/michaellarouche.wordpress.com/116/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/michaellarouche.wordpress.com/116/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/michaellarouche.wordpress.com/116/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/michaellarouche.wordpress.com/116/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/michaellarouche.wordpress.com/116/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/michaellarouche.wordpress.com/116/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/michaellarouche.wordpress.com/116/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/michaellarouche.wordpress.com/116/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/michaellarouche.wordpress.com/116/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/michaellarouche.wordpress.com/116/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=michaellarouche.wordpress.com&amp;blog=6504667&amp;post=116&amp;subd=michaellarouche&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://michaellarouche.wordpress.com/2009/07/29/refactoring-game-legacy-code-part-2-cleaning-up-the-terrain/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/aeadb8b29fc572b2497dff871d9406bf?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">michaellarouche</media:title>
		</media:content>
	</item>
		<item>
		<title>How to make your internal classes accessible to your unit tests in .NET</title>
		<link>http://michaellarouche.wordpress.com/2009/07/27/how-to-make-your-internal-classes-accessible-to-your-unit-tests-in-net/</link>
		<comments>http://michaellarouche.wordpress.com/2009/07/27/how-to-make-your-internal-classes-accessible-to-your-unit-tests-in-net/#comments</comments>
		<pubDate>Tue, 28 Jul 2009 02:36:58 +0000</pubDate>
		<dc:creator>michaellarouche</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[unit test]]></category>

		<guid isPermaLink="false">http://michaellarouche.wordpress.com/2009/07/27/how-to-make-your-internal-classes-accessible-to-your-unit-tests-in-net/</guid>
		<description><![CDATA[Here a little tip I&#8217;ve found while reading Moq source code. If you want to test some internal classes of your library, you don&#8217;t have to make your classes public in order to be visible in your test assembly (like how I was doing untill now&#8230;) The trick is to add this little line into [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=michaellarouche.wordpress.com&amp;blog=6504667&amp;post=112&amp;subd=michaellarouche&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Here a little tip I&#8217;ve found while reading <a title="Moq" href="http://code.google.com/p/moq/">Moq</a> source code. If you want to test some internal classes of your library, you don&#8217;t have to make your classes public in order to be visible in your test assembly (like how I was doing untill now&#8230;)</p>
<p>The trick is to add this little line into your assembly properties (AssemblyInfo.cs)</p>
<p><pre class="brush: csharp;">
[assembly: InternalsVisibleTo(&quot;NameOfYourTestAssembly&quot;)]
</pre></p>
<p>Now all class with the internal visibility will be accessible to your unit tests. For private visibility, one solution is to use reflection or just look on Google <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/michaellarouche.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/michaellarouche.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/michaellarouche.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/michaellarouche.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/michaellarouche.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/michaellarouche.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/michaellarouche.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/michaellarouche.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/michaellarouche.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/michaellarouche.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/michaellarouche.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/michaellarouche.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/michaellarouche.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/michaellarouche.wordpress.com/112/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=michaellarouche.wordpress.com&amp;blog=6504667&amp;post=112&amp;subd=michaellarouche&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://michaellarouche.wordpress.com/2009/07/27/how-to-make-your-internal-classes-accessible-to-your-unit-tests-in-net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/aeadb8b29fc572b2497dff871d9406bf?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">michaellarouche</media:title>
		</media:content>
	</item>
		<item>
		<title>Refactoring game legacy code: Part 1 &#8211; Introduction</title>
		<link>http://michaellarouche.wordpress.com/2009/07/26/refactoring-game-legacy-code-part-1-introduction/</link>
		<comments>http://michaellarouche.wordpress.com/2009/07/26/refactoring-game-legacy-code-part-1-introduction/#comments</comments>
		<pubDate>Mon, 27 Jul 2009 00:56:29 +0000</pubDate>
		<dc:creator>michaellarouche</dc:creator>
				<category><![CDATA[Game Programming]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[clean code]]></category>
		<category><![CDATA[craftmanship]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[legacy code]]></category>
		<category><![CDATA[refactoring]]></category>
		<category><![CDATA[tdd]]></category>

		<guid isPermaLink="false">http://michaellarouche.wordpress.com/?p=103</guid>
		<description><![CDATA[Introduction Hello, welcome to my series of article about refactoring game legacy code. Game legacy code is no different than other types of code. What&#8217;s makes game different than others type of codes ? With a game, you are dealing with a event driven, simulated real-time architecture. They have different needs and requirements than a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=michaellarouche.wordpress.com&amp;blog=6504667&amp;post=103&amp;subd=michaellarouche&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h2>Introduction</h2>
<p>Hello, welcome to my series of article about refactoring game legacy code. Game legacy code is no different than other types of code. What&#8217;s makes game different than others type of codes ? With a game, you are dealing with a event driven, simulated real-time architecture. They have different needs and requirements than a business, Web or rich client application. Games need to react fast to events such as &#8216;gun was triggered&#8217;, you have parts of your code that are called 30 or 60 times per second, loading times need to be short, etc.</p>
<p>There is a lot of things going on in a game, where in a typical business application you just need to collect some input, process it and persist into a database (good &#8216;ol <a title="CRUD" href="http://en.wikipedia.org/wiki/Create,_read,_update_and_delete" target="_blank">CRUD</a>).</p>
<h2>The subject</h2>
<p>The subject is my first complete game ever written. If you look at the projects sections of this site, you&#8217;ll see some failed attempts. The game is a Tetris clone called “That Game From Russia (you know it)”. It is written in C++ using <a title="SDL library" href="http://www.libsdl.org/" target="_blank">SDL</a> library. It was done in two weeks back in January 2009. If you have read my article <a title="Paradigm Shift" href="http://michaellarouche.wordpress.com/2009/06/01/paradigm-shift/">Paradigm Shift</a>, you know this was before I read many books and articles that changed my way of thinking about code, design and architecture.</p>
<p>The code can be found at <a title="That Game From Russia" href="http://code.google.com/p/gamefromrussia" target="_blank">http://code.google.com/p/gamefromrussia/</a></p>
<p>We start with SVN revision 5.</p>
<h2>Current state of the architecture</h2>
<p>The architecture is based on a basic state machine. There is a bit of duplicated code scattered around the code, like the input handling. The code is pretty tightly coupled. They are little to no abstraction in the code. SDL types are found in public API.  When I created the code, I intended to do my Tetris in one day. Yeah silly I know. I didn&#8217;t intend to create a flexible architecture. I wanted to create a Tetris for fun thus I didn&#8217;t try to care that much about the craft of my code.</p>
<p>Anyway so you have the states (DropPiece, InitGame, Setup, GameOver, RemoveLines, SetNextPiece, PauseState, LinesRemoveAnimation) that define the core behavior of the game. Each state has a enter(), execute() and exit() methods. enter() initialize the state, execute() is called on every iteration of the game loop and exit() is called before changing to another state.</p>
<p>The “domain” or the core of the game are in the classes Block, BlockLine, Board, Piece. Block is a single block. BlockLine is a line of blocks in the Board. Board contains a list of block lines. Piece is a collection of blocks that represent the shapes in a Tetris game.</p>
<p>The other classes are support classes. SoundPlayer manages sound effects and music. TextPrinter manage drawing the current level, the score and the number of lines.</p>
<h2>What we are trying to refactor ?</h2>
<p>What I want to refactor is all the code related to the Piece handling. I want to make the rotation code easier to understand. Currently, for each type of piece, you have obscure code that move the blocs depending of the direction. It assume that the x, y is for the center block and the other blocks are moving according to this block. While developing this code, I had sketches on paper to help me map the blocks for each piece. I shouldn&#8217;t needed a piece of paper to manage the complexity of this code.</p>
<p>The right way to rotate a piece is to use a rotation matrix and apply it to each of the blocks coordinate. I found this when looking at other Tetris clone code. You don&#8217;t need to use a matrix explicitly thought. I will explain the solution a little deeper in a following article.</p>
<p>When you refactor code, you should keep in mind your main goal. When dealing with legacy code, you can be tempted to do large scale refactoring of your ugly, stupid code. Having a goal help you focus of refactoring code that matters for what you are trying to accomplish.</p>
<h2>Why I am doing this ?</h2>
<p>In this case, I want to train my refactoring, <a title="TDD" href="http://en.wikipedia.org/wiki/Test-driven_development">TDD</a>, <a title="Getting started with SOLID" href="http://blog.objectmentor.com/articles/2009/02/12/getting-a-solid-start">SOLID</a> skills in a C++ code base in preparation for a much larger game project this fall. Also, I am to experiment some techniques illustrated in Michael C. Feathers book <a title="Amazon link to the book" href="http://www.amazon.ca/Working-Effectively-Legacy-Michael-Feathers/dp/0131177052/ref=sr_1_1?ie=UTF8&amp;qid=1248656093&amp;sr=8-1">Working Effectively with Legacy Code</a>.</p>
<h2>Refactoring legacy code is hard</h2>
<p>I don&#8217;t claim to be an expert in refactoring legacy code. In fact, it&#8217;s quite the opposite. I am still a beginner in refactoring legacy code. Up to this point, I only refactored one legacy code base I written in C# back in 2007. It is quite a hard journey depending of the quality of the code base. My current code base has a lot of inner, subtle dependencies that we will need to break over time.</p>
<h2>Next episode</h2>
<p>In next episode, we&#8217;ll start doing some code cleanups to get a feel of the code base before setting up the tests in preparation for larger refactorings.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/michaellarouche.wordpress.com/103/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/michaellarouche.wordpress.com/103/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/michaellarouche.wordpress.com/103/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/michaellarouche.wordpress.com/103/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/michaellarouche.wordpress.com/103/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/michaellarouche.wordpress.com/103/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/michaellarouche.wordpress.com/103/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/michaellarouche.wordpress.com/103/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/michaellarouche.wordpress.com/103/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/michaellarouche.wordpress.com/103/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/michaellarouche.wordpress.com/103/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/michaellarouche.wordpress.com/103/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/michaellarouche.wordpress.com/103/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/michaellarouche.wordpress.com/103/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=michaellarouche.wordpress.com&amp;blog=6504667&amp;post=103&amp;subd=michaellarouche&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://michaellarouche.wordpress.com/2009/07/26/refactoring-game-legacy-code-part-1-introduction/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/aeadb8b29fc572b2497dff871d9406bf?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">michaellarouche</media:title>
		</media:content>
	</item>
		<item>
		<title>Paradigm Shift</title>
		<link>http://michaellarouche.wordpress.com/2009/06/01/paradigm-shift/</link>
		<comments>http://michaellarouche.wordpress.com/2009/06/01/paradigm-shift/#comments</comments>
		<pubDate>Tue, 02 Jun 2009 01:39:35 +0000</pubDate>
		<dc:creator>michaellarouche</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://michaellarouche.wordpress.com/2009/06/01/paradigm-shift/</guid>
		<description><![CDATA[This title sums up pretty well the last school year for me. I changed a lot of things in my programming(geek ?) life that I need to write a whole post about it. First of all, I used to be a Linux user AND developer (for KDE) all the way. I used Linux primely on [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=michaellarouche.wordpress.com&amp;blog=6504667&amp;post=102&amp;subd=michaellarouche&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This title sums up pretty well the last school year for me. I changed a lot of things in my programming(geek ?) life that I need to write a whole post about it.</p>
<p>First of all, I used to be a Linux user AND developer (for KDE) all the way. I used Linux primely on my desktop and my laptop. I used to be a KDE contributor until University took all my time. Up to that point, I managed to do all my stuff in Linux except for certain things that really required Visual Studio or a Windows-only program. Now I need Visual Studio more than ever, and since I was tired to swag my girlfriend&#8217;s laptop every time I needed Windows, I wiped out my Mac partition and installed Windows Vista. Oh the horror some might say, but nowadays I&#8217;m been using Windows 7 RC and there were much rejoicing. I haven&#8217;t felt the need to restore GRUB on my laptop and suspend-to-RAM is painless.</p>
<p>The seed that started this whole shift is when I was working on my summer work and I grew tired of writing the same CRUD database stuff and business rules. From this day, I decided that I needed something more challenging. Fortunately I was in the good curriculum, only my future goal changed.</p>
<p>Then I (re)found gaming. I thought games would be the challenge I was looking for.</p>
<p>When I started university, I have no intention to become a game developer, I didn&#8217;t play game that much for several years where in my young years I used to be a Nintendo freak. My goal when leaving University was to join a open source company to work on free software or find something that wasn&#8217;t related to IT(not custom apps for biz) or gaming. Now, I want to join a game company or maybe form my own with friends, time will tell.</p>
<p>Back to open source. Like I said, I was a big Linux user. I still think they are cool stuff in Linux and open source is nice. I still use a lot of free software. But the reality as a developer is slapping me back to the face. The fact is Windows is the dominant platform and if you want to get the most users, either you go the Web approach (which I kinda dislike) or you develop for Windows. Also, I want to be a game developer and Windows is THE platform for developing games no matter the target platform.</p>
<p>Beside my switch in careers goals, I also change a lot my mentality. Since the last months I&#8217;ve been reading a lot of books about software practices such a Clean Code, Agile Patterns Principles and Practices, Refactoring, Code Complete. I also read a lot of articles from Joel on Software. I might say that I got influenced a lot by those articles and that changed my way of thinkings about software.</p>
<p>
Lots of changes. Lots of contracting ideas that I need to sort out and came out with the mixture that will work for me.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/michaellarouche.wordpress.com/102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/michaellarouche.wordpress.com/102/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/michaellarouche.wordpress.com/102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/michaellarouche.wordpress.com/102/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/michaellarouche.wordpress.com/102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/michaellarouche.wordpress.com/102/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/michaellarouche.wordpress.com/102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/michaellarouche.wordpress.com/102/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/michaellarouche.wordpress.com/102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/michaellarouche.wordpress.com/102/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/michaellarouche.wordpress.com/102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/michaellarouche.wordpress.com/102/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/michaellarouche.wordpress.com/102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/michaellarouche.wordpress.com/102/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=michaellarouche.wordpress.com&amp;blog=6504667&amp;post=102&amp;subd=michaellarouche&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://michaellarouche.wordpress.com/2009/06/01/paradigm-shift/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/aeadb8b29fc572b2497dff871d9406bf?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">michaellarouche</media:title>
		</media:content>
	</item>
		<item>
		<title>API simplicity and design</title>
		<link>http://michaellarouche.wordpress.com/2006/06/14/api-simplicity-and-design/</link>
		<comments>http://michaellarouche.wordpress.com/2006/06/14/api-simplicity-and-design/#comments</comments>
		<pubDate>Thu, 15 Jun 2006 01:51:10 +0000</pubDate>
		<dc:creator>michaellarouche</dc:creator>
				<category><![CDATA[KDE]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.tehbisnatch.org/2006/06/14/api-simplicity-and-design/</guid>
		<description><![CDATA[I just ported Kopete to new KDialog API and to new KPageDialog, which was done by Tobias Koenig last week. The new API is really nicer to use, you have done a great job Tobias and this helped me to adapt a lot of old code. This new API use a philosophy called &#8220;Write once, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=michaellarouche.wordpress.com&amp;blog=6504667&amp;post=66&amp;subd=michaellarouche&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I just ported Kopete to new KDialog API and to new KPageDialog, which was done by Tobias Koenig last week. The new API is really nicer to use, you have done a great job Tobias and this helped me to adapt a lot of old code.</p>
<p>This new API use a philosophy called &#8220;Write once, read multiple of times.&#8221;, I use the same philosophy for some time while doing libpapillon. I will give you a case, when you read a constructor like this <code>new KDialog(i18n("Caption"),parent, name, User1 | User2, User1, true, ...)</code>. This make sense when you first write it because you have the API documentation open in another window. Now, re-read this code 1 month later, the code will make less much sense and you will need to remember and try to guess what each parameter do. By simplicifing your API, you make your code more readable and more maintainable, not just for you but for yours followers too because we are in open source world and other people read our code.<br />
Updated code:<br />
<code><br />
KDialog *newDialog =new KDialog(parent);<br />
newDialog-&gt;setCaption( i18n("Caption") );<br />
newDialog-&gt;setButtons( KDialog::User1 | KDialog::User2 );<br />
newDialog-&gt;setDefaultButton( KDialog::User1 );<br />
</code><br />
Easier to read isn&#8217;t ? <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Another philosophy should be used while designing API is make your interface ease to use and harder to misuse. In fact, that&#8217;s one item from book &#8220;Effective C++ 3rd Edition.&#8221;. Every serious C++ Programmer should have a copy of this book IMO. The goal is to reduce the complexity of your API to avoid confusion when using it, thus avoiding bugs and unexpected behavior. Also to report error at compile-time and not at runtime. I will reuse the example from the book (that I paste from my memory <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> ).</p>
<p>Here a simple encapsulation of a Date.<br />
<code><br />
class Date<br />
{<br />
public:<br />
   Date(int year, int month, int day);<br />
};<br />
</code></p>
<p>What happen if someone call <code>Date(21, 02, 1986)</code> or <code>Date(02, 21, 1986)</code> ? This can lead to an unexpected behavior.</p>
<p>Here a good way to encapsulate Date:<br />
<code><br />
struct Month<br />
{<br />
 explicit Month(int);<br />
};<br />
struct Day<br />
{<br />
  explicit Day(int);<br />
};<br />
struct Year<br />
{<br />
  explicit Year(int);<br />
};<br />
class Date<br />
{<br />
public:<br />
  explicit Date(Day day, Month month, Year year);<br />
};<br />
</code><br />
By making use of keyword <code>explicit</code>, you avoid implicit conversion to <code>int</code> and compiler will generate an error if you try to use the wrong date component.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/michaellarouche.wordpress.com/66/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/michaellarouche.wordpress.com/66/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/michaellarouche.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/michaellarouche.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/michaellarouche.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/michaellarouche.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/michaellarouche.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/michaellarouche.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/michaellarouche.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/michaellarouche.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/michaellarouche.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/michaellarouche.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/michaellarouche.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/michaellarouche.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/michaellarouche.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/michaellarouche.wordpress.com/66/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=michaellarouche.wordpress.com&amp;blog=6504667&amp;post=66&amp;subd=michaellarouche&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://michaellarouche.wordpress.com/2006/06/14/api-simplicity-and-design/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/aeadb8b29fc572b2497dff871d9406bf?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">michaellarouche</media:title>
		</media:content>
	</item>
	</channel>
</rss>
