<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title> &#187; Snippets</title>
	<atom:link href="http://blog.vortixgames.com/tag/snippets/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.vortixgames.com</link>
	<description></description>
	<lastBuildDate>Sun, 05 Feb 2012 00:55:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Performance vs Error Checking</title>
		<link>http://blog.vortixgames.com/performance-vs-error-checking</link>
		<comments>http://blog.vortixgames.com/performance-vs-error-checking#comments</comments>
		<pubDate>Sun, 05 Feb 2012 00:55:11 +0000</pubDate>
		<dc:creator>Vlad</dc:creator>
				<category><![CDATA[Dev Journal: Danger Zone]]></category>
		<category><![CDATA[The code of VGS]]></category>
		<category><![CDATA[Bold Pixel Engine]]></category>
		<category><![CDATA[Books]]></category>
		<category><![CDATA[Danger Zone]]></category>
		<category><![CDATA[FlashGameBlogs]]></category>
		<category><![CDATA[Snippets]]></category>
		<category><![CDATA[Software Engineering]]></category>

		<guid isPermaLink="false">http://blog.vortixgames.com/?p=1104</guid>
		<description><![CDATA[Hi everyone, Vlad here! Seems like on a thing versus some other thing rampage with Application Design vs Game Design shortly followed by this post. I guess that&#8217;s a good thing, it means there are challenges and doubts and that we need to research. This post is also inspired by some books I&#8217;ve been reading lately and [...]]]></description>
			<content:encoded><![CDATA[<p>Hi everyone, Vlad here!</p>
<p>Seems like on a thing versus some other thing rampage with <a href="http://blog.vortixgames.com/application-design-vs-game-design-round-1">Application Design vs Game Design</a> shortly followed by this post. I guess that&#8217;s a good thing, it means there are challenges and doubts and that we need to research.</p>
<p>This post is also inspired by some books I&#8217;ve been reading lately and that I highly recommend: <a href="http://shop.oreilly.com/product/9780596519544.do">The Productive Programmer</a> that was influenced by <a href="http://pragprog.com/the-pragmatic-programmer">The Pragmatic Programmer</a>.</p>
<h2>Problem #1</h2>
<p>We are writing code and there&#8217;s not such thing as writing code without bugs. Sometimes our oldest, most used code, that code that we trust each of our games with gets into a specific spot where a hidden bug appears and we spend hours and hours figuring out what&#8217;s happening.</p>
<h2>Solution #1 is Problem #2</h2>
<p>There are several solutions to this. Writing code that includes testing is in my opinion the best. The problem with this, game development wise and specifically in scripted languages such as AS3, is that this makes the code much slower. The solution was checking for inconsistencies and not performing operations instead of throwing errors and exceptions.</p>
<p>I ended up creating conditions for use. For instance our blit package uses three different types: the core, the object and the texture. The core keeps the state of the whole system, the object keeps the state of what and where to render and the texture keeps the state of BitmapData. If the object did not have a texture it would simply not render. Sounds pretty cool, right?</p>
<p>To be honest&#8230; wrong&#8230;</p>
<h3>Problem #2.1</h3>
<p>First problem was the times where the position had NaN value instead of a Number. This rarely happened but every time it did it was a huge headache to identify why and then track the source of the bug down. While this seems unrelated, the fact was the it looked like the texture did not exist. So I had to check if the texture was created, if it was passed, if it was there at all and the worst part was that if the texture was there as it should, where the hell was the problem?</p>
<h3>Problem #2.2</h3>
<p>Performance wise checking if there is a texture is absolutely dumb! 99% of the times there is a texture present. More, the object class has a visible variable. If the object does not have a texture why is it visible in the first place?</p>
<p>So while apparently I solved a problem by checking if the texture was present and not performing any blit operation if it wasn&#8217;t, the truth is that both performance wise (not that serious by the way, it&#8217;s just a if statement) and the unwanted problems it induced in the code backfired from time to time.</p>
<p>I spread this type of behavior through the code. While one if statement does not have a large impact in performance, a lot of checking piles up and I had a lot!</p>
<h2>Solution #2</h2>
<p>A short story and a riddle both taken from those books that explain my thoughts on the &#8216;final&#8217; (we&#8217;ll see about that!) solution for these problems.</p>
<p>The first one was when one of the authors was hired as a consultant for a development project where the coders sent broken code to the version control system. What he did was to create a routine where the version control server would compile the code that was pushed. Every time the compilation didn&#8217;t work the server would send an email with the error. With the amount of mail with errors constantly piling up on mailboxes, one of the coders asked if there was a way of not getting that much email. The consultant replied: &#8220;Sure, don&#8217;t push code with errors!&#8221;</p>
<p>The riddle is&#8230; what code takes not time to be executed? The one that is not compiled.</p>
<p>So the solution is to write code that allows zero error tolerance while not compiling it. The way to achieve this is with <a href="http://livedocs.adobe.com/flex/3/html/help.html?content=compilers_21.html">conditional compilation</a> which can be used in several way but these are the ones I believe the most useful is to use code blocks, like this:</p><pre class="crayon-plain-tag"><code>private function someFunction():void {
	CONFIG::debug {
		// Development code
	}

	CONFIG::release {
		// Final code
	}

	// Both dev and final code
}</code></pre><p>
Inside the same function, different code is compiled depending on the current configuration. We can have several checks with exceptions and errors being thrown and then have a &#8216;don&#8217;t check at all&#8217; version in the release configuration.</p>
<p>This is a two stage solution really. On one hand every time I do something stupid, I get errors so my code has to be perfectly implemented at least when it comes to our framework. On the other hand the same code that make me code as best as I can is also the code that does not run in the final version.</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.vortixgames.com/performance-vs-error-checking/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Enhancing by refactoring</title>
		<link>http://blog.vortixgames.com/enhancing-by-refactoring</link>
		<comments>http://blog.vortixgames.com/enhancing-by-refactoring#comments</comments>
		<pubDate>Sat, 28 Jan 2012 02:00:03 +0000</pubDate>
		<dc:creator>Vlad</dc:creator>
				<category><![CDATA[Dev Journal: Danger Zone]]></category>
		<category><![CDATA[The code of VGS]]></category>
		<category><![CDATA[Bold Pixel Engine]]></category>
		<category><![CDATA[Danger Zone]]></category>
		<category><![CDATA[Encapsulation]]></category>
		<category><![CDATA[Inheritance]]></category>
		<category><![CDATA[Snippets]]></category>
		<category><![CDATA[Software Engineering]]></category>

		<guid isPermaLink="false">http://blog.vortixgames.com/?p=1091</guid>
		<description><![CDATA[Two years ago I read a lot about OOP. Every time I picked a new concept in some forum or chat I&#8217;d do my best to understand and apply it. I was eager to improve my coding knowledge and skills but everything we code as a purpose and an audience and if we forget the [...]]]></description>
			<content:encoded><![CDATA[<p>Two years ago I read a lot about OOP. Every time I picked a new concept in some forum or chat I&#8217;d do my best to understand and apply it. I was eager to improve my coding knowledge and skills but everything we code as a purpose and an audience and if we forget the purpose or the audience we are introducing more problems with the knowledge we have than solving problems.</p>
<p>That happened with Bold Pixel. At some point in time Bold Pixel became a monster to please developers instead of a tool to be more productive in my purpose to my audience: create games for players! People play games, not code!</p>
<p>I stopped over-engineering sometime ago and started reading about how to become a better programmer not by writing better code, but by writing the exact code to solve the exact problem. This brought a new problem: what to do with Bold Pixel and how to maintain and evolve it? First thing was to start a new package and only bring in classes that I needed but wire the classes up to be as slim and straight to the point as possible. I do that all the time to build up my code base right now, but that&#8217;s not all. Here&#8217;s the whole process I&#8217;m using right now.</p>
<h2>Start simple</h2>
<p>&nbsp;</p><pre class="crayon-plain-tag"><code>class BlitObject {

	// Position
	public var x:Number = 0;
	public var y:Number = 0;

	// Rendering
	public var visible:Boolean = true;

	public function BlitObject():void {
		// Constructor
	}

	internal function render():void {
		// Render stuff here
	}
}</code></pre><p>The class above can be a stub for a simple blit object. From a OOP perspective it breaks encapsulation completely since x, y and visible variables should be private but seriously, do I really need those to be private or protected and have getters and setters? No I don&#8217;t so no need to over-engineer it. The only thing I want to have contained is the render method that should only be called by the blit engine that lives in the same package.</p>
<p>The blit object only responsibility is to copyPixels or draw on a given world position translating camera position on the fly.</p>
<p>Writing a simple class like this is fast, performs fast, case closed.</p>
<h2>Evaluate new needs</h2>
<p>Now I need a tile map class. This class is also a blit object so following the <a href="http://en.wikipedia.org/wiki/Is-a">&#8220;is a&#8221;</a> versus <a href="http://en.wikipedia.org/wiki/Has-a">&#8220;has a&#8221;</a> rule tile map should inherit blit object. Considering the blit engine has a camera, the tile map won&#8217;t move. This brings two new problem.</p>
<p>1. If the tile map doesn&#8217;t move, why do I have x and y publicly accessible?<br />
2. I want my camera to follow blit objects so blit objects must have x and y variables (or properties). How can I make tile map be recognized as a blit object, have x and y and have none of those altered?</p>
<p>Sounds messy&#8230;</p>
<p>I could write a Blittable interface that would define a render function and getter and setter methods for x and y. The problem is that if there&#8217;s something I want to keep in the package scope is the render function and interfaces define public interfaces.</p>
<p>The solution is to convert blit object in an abstract class and have its original functionality moved to a new graphic class.</p><pre class="crayon-plain-tag"><code>class BlitObject {

	// Rendering
	public var visible:Boolean = true;

	internal function render():void {
		throw new Error(&quot;Abstract function called. Override render()!&quot;);
	}

	public function get x():Number {
		return NaN;
	}

	public function set x(v:Number):void {
		throw new Error(&quot;Abstract function called or property x not available.&quot;);
	}

	public function get y():Number {
		return NaN;
	}

	public function set y(v:Number):void {
		throw new Error(&quot;Abstract function called or property y not available.&quot;);
	}	
}

class Graphic extends BlitObject {

	// Position
	private var _x:Number = 0;
	private var _y:Number = 0;

	override internal function render():void {
		// Render stuff here
	}

	// Position
	override public function get x():Number { return _x }
	override public function set x(v:Number):void { _x = v }
	override public function get y():Number { return _y }
	override public function set y(v:Number):void { _y = v }
}

class TileMap extends BlitObject {

	override internal function render():void {
		// Render the tile map
	}
}</code></pre><p><p>
Problem solved. The class responsibilities are much better defined.</p>
<p>1. A blit object renders and has x and y coordinates. Since it is an abstract class everything we try to do with it directly will result in an error.<br />
2. A graphic is a world movable blit object that renders by copyPixel or draw.<br />
3. A tile map is a world static blit object that renders very large objects by copyPixel or draw  based on a predefined xml specification.</p>
<h2>Conclusion</h2>
<p>What&#8217;s the conclusion here? Well for starters by this time I&#8217;d have a couple of interfaces and probably a helpers. I&#8217;m not saying it is wrong to have interfaces and helpers, actually it is preferable, but it would be coding for the sake of coding instead of coding for the sake of making a game. It took me 10 minutes to do this and while I was at it the BlitObject class also extends a ListObject class that really speed up the iteration, addition and removal of objects from the blit engine.</p>
<p>Still&#8230; I did repeat my self and the code has suffered another refactoring since then. I&#8217;ll leave that for another post but I wonder if you know what&#8217;s repeated&#8230; <img src='http://blog.vortixgames.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.vortixgames.com/enhancing-by-refactoring/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Opening files and URLs</title>
		<link>http://blog.vortixgames.com/opening-files-and-urls</link>
		<comments>http://blog.vortixgames.com/opening-files-and-urls#comments</comments>
		<pubDate>Wed, 11 Mar 2009 00:00:43 +0000</pubDate>
		<dc:creator>Vlad</dc:creator>
				<category><![CDATA[The code of VGS]]></category>
		<category><![CDATA[FlashGameBlogs]]></category>
		<category><![CDATA[Snippets]]></category>

		<guid isPermaLink="false">http://blog.vortixgames.com/?p=214</guid>
		<description><![CDATA[I don&#8217;t know about you, but I really hate those little bits of code that you will use in every single game but not during the actual development of it. As far as I&#8217;m concerned, even if not the most noble usage of the &#8220;reuse your code&#8221; rule, getting rid of these pesky little pieces [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_215" class="wp-caption alignright" style="width: 136px"><img class="size-full wp-image-215   " title="URL" src="http://blog.vortixgames.com/wp-content/uploads/2009/03/url.jpg" alt="URL" width="126" height="126" /><p class="wp-caption-text">URL</p></div>
<p>I don&#8217;t know about you, but I really hate those little bits of code that you will use in every single game but not during the actual development of it. As far as I&#8217;m concerned, even if not the most noble usage of the &#8220;reuse your code&#8221; rule, getting rid of these pesky little pieces of code is the single most useful usage of object oriented programming, not because I read it somewhere but because I&#8217;ll never have to worry about it again!</p>
<p>Opening files and URLs are two examples those pesky bits of code and to me both are perfect candidates to our framework Toolkit class! So here&#8217;s the code of the two static functions that were created to deal with it.</p><pre class="crayon-plain-tag"><code>/*Opens an external file. Upon completing the task, callbacks to the
 *given function.*/
public static function OpenFile(filename:String, callback:Function)
{
	var ldr:URLLoader = new URLLoader();

	ldr.load(new URLRequest(filename));
	ldr.addEventListener(Event.COMPLETE, callback);
}

/*Opens a URL on a new window.*/
public static function OpenURL(url:String)
{
	var urlReq:URLRequest = new URLRequest(url);
	navigateToURL(urlReq, &quot;_blank&quot;);
}</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.vortixgames.com/opening-files-and-urls/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Site lock and domain name</title>
		<link>http://blog.vortixgames.com/site-lock-and-domain-name</link>
		<comments>http://blog.vortixgames.com/site-lock-and-domain-name#comments</comments>
		<pubDate>Mon, 02 Mar 2009 00:00:30 +0000</pubDate>
		<dc:creator>Vlad</dc:creator>
				<category><![CDATA[The code of VGS]]></category>
		<category><![CDATA[FlashGameBlogs]]></category>
		<category><![CDATA[Snippets]]></category>
		<category><![CDATA[Sponsorship and Licensing]]></category>

		<guid isPermaLink="false">http://blog.vortixgames.com/?p=131</guid>
		<description><![CDATA[If you are selling licenses of your games, site locking is something that you just have to know. One of the mistakes I did early on was to simply use some piece of code I found on the web. It was something like this:if(this.root.loaderInfo.url.indexOf(&#34;domain.com&#34;) != -1) &#160;&#160; &#160;trace(&#34;Good!&#34;); // It's in the right domain else [...]]]></description>
			<content:encoded><![CDATA[<p>If you are selling licenses of your games, site locking is something that you just have to know. One of the mistakes I did early on was to simply use some piece of code I found on the web. It was something like this:</p><pre class="crayon-plain-tag"><code>if(this.root.loaderInfo.url.indexOf(&quot;domain.com&quot;) != -1)
&nbsp;&nbsp; &nbsp;trace(&quot;Good!&quot;); // It's in the right domain
else
&nbsp;&nbsp; &nbsp;trace(&quot;Bad!&quot;); // It's in the wrong domain</code></pre><p>What this does is to get the index (aka position) of the string &#8220;domain.com&#8221; from the string that is returned by the loaderInfo.url. If it returns -1 then the string &#8220;domain.com&#8221; is not present in the loaderInfo.url.</p>
<p>This solution is fine, but it presentes two problems.</p>
<p>First, if someone really wants to use your site-locked game, they can! They just have to find a way of putting the &#8220;domain.com&#8221; string somewhere in the path to your swf since this code does not compare the domain name but rather the presence of a string on another string.</p>
<p>The second problem is that you don&#8217;t know the domain where the game is present. I need that information for other evil schemes of mine, so this was the perfect reason to write a small tool that would fetch me the domain name.</p>
<p>So I wrote this simple static function that lies inside a class Toolkit -name I shamelessly stolen from a chat with Andrew from Epic Shadow- and that simply returns the <a href="http://en.wikipedia.org/wiki/Root_domain" target="_blank">root domain</a> and the <a href="http://en.wikipedia.org/wiki/Top_level_domain" target="_blank">top level domain</a>, separated by a dot from the loaderInfo.url.</p><pre class="crayon-plain-tag"><code>/*Returns the name of the domain without subdomain, e.g. vortixgames.com
 * instead of www.vortixgames.com.*/
public static function Domain(root:MovieClip):String
{
	/* Splits the full path into an array of strings separated by &quot;/&quot;
	 * and gets the index 2 value which is the full domain name.*/
	var currentDomain:String = root.loaderInfo.url.split(&quot;/&quot;)[2];

	/* Splits the result into an array of strings separated by &quot;.&quot; and
	 * checks the length of the array. If the array has a length of 3
	 * the domain contains a subdomain, for instance &quot;www&quot; and it must
	 * be discarded. If the length is 2 then, there's no subdomain
	 * present. */

	var fqdn:Array = currentDomain.split(&quot;.&quot;); // FQDN in array form

	var rdi:int = 1; // Root Domain Index defaults to 1
	var tli:int = 2; // Top Level Index defaults to 2

	if (fqdn.length == 2)
	{
		rdi--;
		tli--;
	}

	return fqdn[rdi] + &quot;.&quot; + fqdn[tli];
}</code></pre><p><p>
The only issue I have with this function is that I have to pass the root as a parameter to the function. Any idea on how I could solve this otherwise is most welcome.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.vortixgames.com/site-lock-and-domain-name/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

