<?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>FlexWiz Blog &#187; Architecture</title>
	<atom:link href="http://blog.flexwiz.net/category/architecture/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.flexwiz.net</link>
	<description>On Game Development and the Flash platform</description>
	<lastBuildDate>Sat, 17 Dec 2011 22:46:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Molehill and the DisplayList &#8211; A Step Back?</title>
		<link>http://blog.flexwiz.net/molehill-and-the-displaylist-a-step-back/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=molehill-and-the-displaylist-a-step-back</link>
		<comments>http://blog.flexwiz.net/molehill-and-the-displaylist-a-step-back/#comments</comments>
		<pubDate>Sun, 31 Jul 2011 21:00:51 +0000</pubDate>
		<dc:creator>theWiz</dc:creator>
				<category><![CDATA[3D]]></category>
		<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Adobe]]></category>
		<category><![CDATA[Blitting]]></category>
		<category><![CDATA[Molehill]]></category>

		<guid isPermaLink="false">http://blog.flexwiz.net/?p=629</guid>
		<description><![CDATA[There has been a lot of buzz about  Molehill (or Stage3D) and GPU hardware acceleration in the upcoming Flash player 11. However, very little was said on the practical aspects on normal flash applications and games.  Now that the public beta is out, we can get a feel of what it really is, and see [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignright size-medium wp-image-868" title="" src="http://blog.flexwiz.net/wp-content/uploads/2011/07/molehill-300x164.png" alt="" width="300" height="164" />There has been a lot of buzz about  Molehill (or Stage3D) and GPU hardware acceleration in the upcoming Flash player 11. However, very little was said on the practical aspects on normal flash applications and games.  Now that the public beta is out, we can get a feel of what it really is, and see what you can and cannot do with it.</p>
<p>Here is my take on the issue of Stage3D/Molehill. While reading related tweets and posts, I notice a few misconceptions about Molehill &#8211; and this is something I’d like to address here.<br />
<span id="more-629"></span></p>
<h4>Dude, where is my Display List?</h4>
<p>Right from the start I felt something doesn&#8217;t feel right. If all the demos are using engines like Away3D, what&#8217;s in it for &#8216;plain&#8217; flash games? Apparently, Molehill (and Flash 11) does not include GPU hardware acceleration for the Display List.</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-869" src="http://blog.flexwiz.net/wp-content/uploads/2011/07/zombie.jpg" alt="" width="468" height="265" /></p>
<p>First of all a note about architecture. Flash uses a retained mode rendering model, through its Display List. It means we don&#8217;t actually draw, we place display objects on the display list (and the stage) &#8211; the rest is done by the Flash player, where the drawing actually takes place on every frame.</p>
<p>In contrast, Moelhill API  is a thin wrapper for OpenGL  (or Direct3D in some cases) that provides low level drawing of 3D geometry and textures, including programmable shaders. It exists outside of the flash stage, almost as a standalone system. It is not designed to support, or implement drawing of display objects.</p>
<p>While not without its problems, the flash display list is the core feature of Flash: a simple and easy to use scene graph that gets processed and rendered on every frame. Its what makes Flash Flash: you would not want to imagine building any games or applications without it.</p>
<h4>Catching up to current technologies</h4>
<p>This year we started seeing GPU hardware acceleration in JavaScript/CSS, although not on all browsers. Browser have rendering engines that can accelerate primitive drawing for vectors and font when rasterizing them to canvas. I would expect no less from the flash player.<br />
The days when non-accelerated Display List was sufficient for developers are over. Here is what <a href="http://blog.bengarney.com/" target="_blank">Ben Garney</a>, a flash game heavyweight, says on <a href="http://blog.bengarney.com/2010/10/18/the-flash-display-list-and-games/" target="_blank">his blog</a>:</p>
<blockquote><p>[...] I use the display list for lots of things, just like any Flash developer. It’s a useful, flexible, and powerful tool.</p>
<p>But when I have specific, performance oriented needs, I ditch it in favor of something I can control completely. That’s why I always use a raster pipeline – drawing all my own pixels – for my game rendering. Using BitmapData.copyPixels has a consistent, reliable cost, whereas the cost of drawing a DisplayObject hierarchy, either on stage or via draw(), is highly variable and difficult to predict in terms of both memory and CPU consumption. [...]</p></blockquote>
<p>So developers like Ben Garney are opting to write their own renderers in order to gain better performance, but that is not an ideal long term solution. A much better one would be to utilize both multi-threading and GPU hardware acceleration for the standard flash Display List.</p>
<h4>Hello 1993</h4>
<p>For me, this is Deja vu all over again. It brings memories of the early 90s, were all we had was raw OpenGL or DirectX, and programming 3D was considered a black magic, some sort of witchcraft. We had to use special bitmap fonts and skimp on UI features for games, because everything was done in low-level, with no component libraries to speak of.<br />
A lot has changed since then, both in technologies and standards. Are we are expected to use the low level Molehill or use a Molehill based 3D engine for any plain old 2D project?</p>
<div id="attachment_865" class="wp-caption aligncenter" style="width: 410px"><a href="http://blog.flexwiz.net/wp-content/uploads/2011/05/1993-Ford-Mustang.jpg"><img class="size-full wp-image-865 " title="1993-Ford-Mustang" src="http://blog.flexwiz.net/wp-content/uploads/2011/05/1993-Ford-Mustang.jpg" alt="" width="400" height="300" /></a><p class="wp-caption-text">1993 Ford Mustang</p></div>
<p>Bringing GPU acceleration to a small subset of flash applications just won&#8217;t cut it. Casual games that make a large part of Flash projects, need fast performing 2D display as well as complex UI. Forcing developers to shoehorn Molehill into any 2D project is just wrong. It&#8217;s not a good solution and would push more Flash developers to jump ship and look for alternatives.</p>
<h4>GPU Acceleration for All</h4>
<p>Don’t get me wrong: I am a 3D enthusiast myself, and did a lot of 3D programming on different games way before I started using Flash &#8211; and 3D API in flash is better than none. I simply don’t like the idea of having to choose between hardware acceleration or the Display List and not both of them together.</p>
<p>The next logical step for Adobe would be to build GPU hardware acceleration into the Flash Display List, so that the bottleneck of scene rendering would be removed. Yes, it carries some risks since Flash would become dependent on display driver vendors to run consistently on different platforms, but surely this can be resolved by partnering with nVidia and AMD. If done correctly, any existing AS3 code would be accelerated without any changes.</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.flexwiz.net/molehill-and-the-displaylist-a-step-back/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>&#8216;Pileup&#8217; Game Postmortem</title>
		<link>http://blog.flexwiz.net/pileup-game-postmortem/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=pileup-game-postmortem</link>
		<comments>http://blog.flexwiz.net/pileup-game-postmortem/#comments</comments>
		<pubDate>Mon, 13 Jun 2011 18:21:04 +0000</pubDate>
		<dc:creator>theWiz</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Games]]></category>
		<category><![CDATA[Blitting]]></category>
		<category><![CDATA[Box2D]]></category>
		<category><![CDATA[Physics]]></category>
		<category><![CDATA[Tween]]></category>

		<guid isPermaLink="false">http://blog.flexwiz.net/?p=733</guid>
		<description><![CDATA[This article is cross-posted from #AltDevBlogADay. Last month I participated in the Toronto Game Jam, where game developers make a game over one weekend. The game ‘Pileup’ was made that way, with most coding done during the TOJam. This is a detailed postmortem of the game &#8216;Pileup&#8217;. You can read more about my TOJam experience [...]]]></description>
			<content:encoded><![CDATA[<p><em>This article is cross-posted from <a href="http://altdevblogaday.org/" target="_blank">#AltDevBlogADay</a>.</em></p>
<p><a href="http://blog.flexwiz.net/wp-content/uploads/2011/05/pm_img01.png"><img class="alignright size-full wp-image-786" src="http://blog.flexwiz.net/wp-content/uploads/2011/05/pm_img01.png" alt="" width="325" height="266" /></a>Last month I participated in the Toronto Game Jam, where game developers make a game over one weekend. The game ‘Pileup’ was made that way, with most coding done during the TOJam. This is a detailed postmortem of the game &#8216;Pileup&#8217;. You can read more about my TOJam experience on a previous post,  &#8217;<a href="http://blog.flexwiz.net/things-i-learned-in-game-jam/" target="_blank">Things I Learned in TOJam</a>&#8216;.<span id="more-733"></span></p>
<p>&nbsp;</p>
<p>[]</p>
<p style="text-align: center; top: 16px;"><a href="http://amosl.com/pileup" target="_blank"><img class="alignnone size-full wp-image-806" title="play" src="http://blog.flexwiz.net/wp-content/uploads/2011/06/play.png" border="1" alt="" width="148" height="70" /><br />
Play Pileup</a></p>
<h3>What Went Right</h3>
<h4>The Development Platform</h4>
<p>Pileup is a game built on the Flash platform. Making it a Flash game enables us to easily target both desktop web browsers with Flash player, and mobile devices with Adobe AIR. The game is small and simple enough to fit within the technical limits of Flash and its media handling is extremely simple and easy to use &#8211; a perfect fit for a very short development schedule like the one we had.</p>
<h3>Game Framework and Physics</h3>
<p>We made an early decision to base Pileup on physics, specifically the <a href="http://www.box2dflash.org/" target="_blank">Box2D</a> physics engine. It made perfect sense in the context of TOJam, since the game has to be simple enough to create and entertaining at the same time. In order for that to work, we needed a framework or game engine that can handle physics, and so had to consider one of the following  options:</p>
<ol>
<li>Use one of the popular game engines, like Flixel, FlashPunk or PushButton</li>
<li>Integrate physics with a wrapper library, like QuickBox2D</li>
<li>Write our own framework with Box2D integrated into it</li>
</ol>
<p>The first option was lucrative, as it would save a lot of effort. I tried to set up a simple physics demo with these engines and quickly strike out Flixel and FlashPunk, as they are more suitable for simple platform games and don’t work well with physics simulation like Box2D. PushButton was a serious contender, and it even has Box2D built into it &#8211; however the interface for it is not very easy to use, and it require having a physics component attached to every game object. After digging into the source I also discovered that the collision system is far from optimized and uses an older version of Box2D so I decided to drop it as well.</p>
<p>Option #2 was using <a href="http://actionsnippet.com/?page_id=1391" target="_blank">QuickBox2D </a>- very easy to use but way too generic to be used in an actual game. It did find a lot of good ideas inside the source code, like the way they build physics bodies based on shapes.</p>
<p>We ended up with the the last option, and I took a shot at writing my own framework. Taking a cue from libraries like Corona and QuickBox2D, I designed a simple interface that couples a display objects with a physical body. After spending some time with excellent tutorials by <a href="http://kerp.net/box2d/" target="_blank">Todd Kerpelman</a>, <a href="http://blog.allanbishop.com/category/physics/box2d/" target="_blank">Allan Bishop</a> and <a href="http://www.emanueleferonato.com/category/box2d/" target="_blank">Emanuele Feronato</a>, I managed to get a grasp on the basics of Box2D and write a simple engine that would form the core of our game framework. It also helped that by that time I was familiar with the way that the other frameworks integrated Box2D so I had plenty of references.<br />
This effort resulted in our homegrown framework  - a game framework with a physics subsystem, that encapsulates all Box2D functionality as well as an actor system similar to the one used in engines like Unreal3.</p>
<h3>Architecture: Composition vs Inheritance</h3>
<p>Engine architecture is typically based on one of two patterns to define the different game objects: inheritance or composition. Some engines use composition (like PushButton Engine), some use inheritance (Flixel) and others, like Unreal3 use a blend of the two.  We had to pick a side.<br />
We chose to rely on inheritance rather than composition, simply because it suited our needs &#8211; we kept the hierarchy simple and composition would take too much time to implement. The base class for any game object is the Actor class, and the subclasses override specific functionality to define their own behavior.<br />
Here is a snippet of the base actor class:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Actor
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #6699cc; font-weight: bold;">var</span> _body<span style="color: #000066; font-weight: bold;">:</span>b2Body<span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #6699cc; font-weight: bold;">var</span> _geom<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">DisplayObject</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #0033ff; font-weight: bold;">get</span> body<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span>b2Body <span style="color: #000000;">&#123;</span> <span style="color: #0033ff; font-weight: bold;">return</span> _body<span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #0033ff; font-weight: bold;">get</span> geom<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">DisplayObject</span> <span style="color: #000000;">&#123;</span> <span style="color: #0033ff; font-weight: bold;">return</span> _geom<span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Actor<span style="color: #000000;">&#40;</span>disp<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">DisplayObject</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">b</span><span style="color: #000066; font-weight: bold;">:</span>b2Body=<span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			_geom = disp<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">b</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			    _body = <span style="color: #004993;">b</span><span style="color: #000066; font-weight: bold;">;</span>
			    _body<span style="color: #000066; font-weight: bold;">.</span>SetUserData<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> updateNow<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			updatePosition<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			childUpdate<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> destroy<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>  <span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> updatePosition<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>  <span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900; font-style: italic;">// Called when collision occured</span>
		<span style="color: #0033ff; font-weight: bold;">public</span>  <span style="color: #339966; font-weight: bold;">function</span> hit<span style="color: #000000;">&#40;</span>force<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000066; font-weight: bold;">,</span> actorB<span style="color: #000066; font-weight: bold;">:</span>Actor<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span></pre></div></div>

<h3>Assets: Vector vs Bitmap</h3>
<p>Another early decision we made is to try to optimize for mobile, so it would be easier to port the game to mobile platforms later. Even though the framework supports any display object as the visual element, we opted to use bitmaps over vectors and spritesheets over movie clips. This actually made it much easier to handle the art pipeline &#8211; bitmaps get loaded dynamically on game setup and do not require embedding or rebuilding when updated. Performance wise, spritesheets make sense since they render faster &#8211; by blitting the frame and avoiding the overhead of timeline in movie clips. The faster rendering is due to the face that a movieclip frame (or a shape) is drawn (and rasterized) on every frame in the game loop, while a bitmap is only blitted once and then get cached and used on subsequent frames without rasterizing.<br />
Also, we only used a limited number of frames per asset in the game so the memory usage is kept on check.</p>
<p><img class="size-full wp-image-802 aligncenter" src="http://blog.flexwiz.net/wp-content/uploads/2011/06/shot2c.jpg" alt="" width="337" height="190" /></p>
<h3>Engine First, Game Later</h3>
<p>This may sound trivial, but too often ignored: avoiding tight coupling between engine (framework) and the game specific code is not only a best practice, but also a big time saver. Once the scope of development has switched from the framework to the game, we were able to focus on the game code almost exclusively. Tasks become much clearer and easier to implement that way, especially under pressure of a very short schedule. Also the game code is often coupled with specific assets, and involves close collaboration with the artist that creates them, so there is minimal down time for the artist since they don’t need to wait for the coder to implement the infrastructure.</p>
<h3>Level Editing Tool</h3>
<p>Perhaps the biggest win for us was the fact that we made our own level design tool in advance. These kind of platform game requires a lot of time and effort to build a level, and we approached the problem from two aspects- design time and load time.</p>
<p>First we created a script for Flash CS4 (using JSFL) that exports all instances on the stage to an XML file, in the form of a list of objects with type, name and position. Then, we wrote a level loader into the game framework to read the level XML, create the game objects and place them on the game screen. This was simple and crude but extremely effective &#8211; any of us could edit a level in a matter of minutes and creating a new one in an hour or two.</p>
<h2>What Went Wrong</h2>
<h3>Finalizing Game idea</h3>
<p>Up until the last moment before the jam we weren&#8217;t able to nail down a clear idea for the game and how to make it fun. We had to fill in the blanks as we go, often while coding &#8211; that impacted the final product as it often feels like something is missing. Maybe we would add more in future versions.</p>
<h3>Content and Production Value</h3>
<p>For a game created in such a short schedule you really need to prepare goo concept art and visual style guide well in advance. Like every other game out there, content is king &#8211; highly polished visuals are extremely important to give the game high production value. Our visuals fell behind and did not meet our initial expectations, leaving the game without anything that stands out of the ordinary.</p>
<h2>Conclusion</h2>
<p>Pileup was a lot of fun to create. It may not stand out and is not very original, but we learned a lot while making it. The production process is compressed to a few days and the stakes are high, and we managed to reach the finish line with a complete game. Lots of features did not make it to this release, so hopefully we would be able to include them future updates.</p>
<h3>Game Facts</h3>
<p><strong>Development Team: </strong>Two coders and two artists</p>
<p><strong>Tools used</strong><br />
Adobe Flash Builder 4.5, Adobe Flash CS4, Photoshop CS4, SVN</p>
<p><strong>3rd Party libraries</strong><br />
Greensock TweenLite, AS3 Signals, Box2D 2.1a (Boris the brave AS3 port)</p>
<p><strong>Source</strong> 3600 lines of code in 44 classes<br />
<strong>Build Size </strong>Binary SWF file: 112k, Total Assets: 6 MB</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.flexwiz.net/pileup-game-postmortem/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Go Beyond Flex, Part 1 &#8211; How to make your code smaller and faster</title>
		<link>http://blog.flexwiz.net/go-beyond-flex-part-1-how-to-make-your-code-smaller-and-faster/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=go-beyond-flex-part-1-how-to-make-your-code-smaller-and-faster</link>
		<comments>http://blog.flexwiz.net/go-beyond-flex-part-1-how-to-make-your-code-smaller-and-faster/#comments</comments>
		<pubDate>Fri, 11 Jun 2010 02:19:12 +0000</pubDate>
		<dc:creator>theWiz</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Adobe]]></category>
		<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://flexwiz.amosl.com/?p=262</guid>
		<description><![CDATA[Flex is often taken for granted when implementing flash/AS3  projects. However it is not mandatory to use Flex on the flash platform. Not only that, its not always the preferred solution for most RIAs. In this post, I will cover a few alternatives to Flex while staying in the realm of Actionscript and Flash. Yes, [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignright size-full wp-image-289" title="Bike01a" src="http://blog.flexwiz.net/wp-content/uploads/2010/06/Bike01a.jpg" alt="" width="306" height="206" />Flex is often taken for granted when implementing flash/AS3  projects. However it is not mandatory to use Flex on the flash platform. Not only that, its not always the preferred solution for most RIAs.</p>
<p>In this post, I will cover a few alternatives to Flex while staying in the realm of Actionscript and Flash. Yes, this may sound strange from someone who makes his living using Flex, but there are situations when Flex can become a liability.<br />
<span id="more-262"></span></p>
<h3>The Right Tool?</h3>
<p>A basic rule of thumb is: use the right tool for the job: Different projects have different requirements, so does it make sense to use the exact same tool for all of them?</p>
<p>Some projects require heavy back-end data support or lots of UI, while others need to be light and fast and require very minimal UI. Enterprise projects are completely different from, say, casual games or advertising projects on so many levels, and it&#8217;s not a good idea to build them in the same way.</p>
<p>The Flex framework was designed and written by Adobe (and later became an open source project) as a layer on top of the flash API. While it did prove to be powerful and easy to use, Flex is far from perfect. It was touted as the one solution to fits all problems, and typical to projects developed by large corporations, the objective was to cram as many features as possible, and it quickly grew in size and complexity &#8211; code size and efficiency fell victim and pushed to lower priority.</p>
<p>The fact is that most projects don&#8217;t use 100% of these features, and a good part of them use maybe 20% of them. The underlying overhead is significant. Sure the framework is modular, and the compiler will not include components that are not accessed, but because of so many dependencies in the flex core components the resulting swf file would be significantly larger.</p>
<p>The Flash community has developed a love-hate relationship with Flex, and Adobe did address some of the problems over time. However, even with the recent release of Flex 4 the fact remains that the framework is large, heavy and not very efficient. The good thing about Flex, is that being an open source project, we are able to look under the hood and see exactly what code is being used.</p>
<h3>So How big is it anyway?</h3>
<p>A sample project with a couple of labels and buttons can serve as a basic test case. Create two projects, one as Flex project and the other as pure actionscript project.</p>
<p>Use either MXML or actionscript to place these labels and buttons &#8211; in the flex project it would use the Application, Label and Button components while in the AS3 project it would use TextField and SimpleButton.</p>
<p>Set the linkage in the build properties to &#8216;merge into code&#8217; to create a single swf. Now and build the projects and compare the resulting SWF files.</p>
<div style="padding-left: 30px;"><span style="font-family: Verdana; line-height: normal;"><span style="text-decoration: underline;">Test Results:</span></span></div>
<div style="padding-left: 60px;"><span style="font-family: Verdana; line-height: normal;"> </span><span style="font-family: Verdana; line-height: normal;">Flex project swf: 600-780K debug and 250-330K release</span></div>
<div style="padding-left: 60px;"><span style="font-family: Verdana; line-height: normal;"> </span><span style="font-family: Verdana; line-height: normal;">Plane AS3 project swf: 12-15K debug and 8-9K release </span></div>
<p>600K versus 12K &#8211; that is almost 50 times ratio!</p>
<p>Big deal, you might say, why should we even care about file sizes and memory footprint when we have gigs of memory and bandwidth to spare?</p>
<p>Well, if you want your flash app/game to run properly on platforms like netbooks, smartphones and mobile touch devices you already know the answer: smaller and faster is always better. Even on desktops, loading time might become a factor for some projects.</p>
<p>To understand this issue further, a few words on the Flex SDK architecture.</p>
<p>At the core of the SDK lies the forefather of Flex: UIComponent &#8211; the base class for all visual components. Developers sometimes refer to it as the &#8216;God Component&#8217; &#8211; an all powerful, massive entity that contains all the functionality needed in interactive and non interactive components (lifecycle events, mouse interactions, effects, tool-tips etc.). To iterate, classes like button and label are based on UIComponent.</p>
<p>The class hierarchy for a simple button (spark) is:<br />
Button -&gt; ButtonBase -&gt; SkinnableComponent -&gt; UIComponent.<br />
To get a rough estimate for the size in memory of a button, simply add up all of the classes up the hierarchy. Actual results may vary depending on how optimized the compiler and player are.</p>
<p>Now lets take a look at a few popular controls in the Flex SDK code and try to get an idea of how big they are.</p>
<div style="margin-top: 0px; margin-bottom: 0px; padding-left: 30px;">
<table border="1" cellspacing="0" cellpadding="3" bordercolor="#000000">
<tbody>
<tr>
<td style="padding-left: 30px;" width="33.333333333333336%"></td>
<td style="padding-left: 30px;" width="33.333333333333336%">AS3 File size</td>
<td style="padding-left: 30px;" width="33.333333333333336%">Lines of code</td>
</tr>
<tr>
<td width="33.333333333333336%">Spark Label</td>
<td width="33.333333333333336%">60K</td>
<td width="33.333333333333336%">1560</td>
</tr>
<tr>
<td width="33.333333333333336%">Spark Button</td>
<td width="33.333333333333336%">7K</td>
<td width="33.333333333333336%">216</td>
</tr>
<tr>
<td width="33.333333333333336%">Spark Group</td>
<td width="33.333333333333336%">69K</td>
<td width="33.333333333333336%">2000</td>
</tr>
<tr>
<td width="33.333333333333336%">UIComponent</td>
<td width="33.333333333333336%">427K</td>
<td width="33.333333333333336%">12,000</td>
</tr>
<tr>
<td width="33.333333333333336%">DataGrid</td>
<td width="33.333333333333336%">201K</td>
<td width="33.333333333333336%">5750</td>
</tr>
<tr>
<td width="33.333333333333336%">ButtonBase</td>
<td width="33.333333333333336%">33K</td>
<td width="33.333333333333336%">1140</td>
</tr>
</tbody>
</table>
</div>
<p><span style="font-family: Verdana; line-height: normal;">Wow, 1560 lines of code for a simple label? that&#8217;s not exactly compact and efficient. Also 2000 lines for a group, the container used to layout child components, looks a bit bloated. </span></p>
<p><span style="font-family: Verdana; line-height: normal;">Well, you get the point. Since all Flex visual components are derived from the jumbo size UIComponent, we start with a few hundred KBs no matter how basic our project is. And we didn&#8217;t even cover actual memory footprint for these components. </span></p>
<p><span style="font-family: Verdana; line-height: normal;">To conclude, with thousands of lines of code that make up most flex components, its no wonder we end up with SWF files that are over a Meg, and sometimes a few megabytes before adding any assets.</span></p>
<p><span style="font-family: Verdana; line-height: normal;"> </span></p>
<h3 style="font-size: 12pt;">The Code behind</h3>
<p>Another aspect of Flex is the usage of MXML for layouts to simplify code. Other than simple layout it offer other features, like states, data binding and styles. MXML is actually only a markup that is pre-complied into Actionscript code. The generated code is added to the &#8216;plain&#8217; code and then complied into a swf.</p>
<div style="margin-top: 0px; margin-bottom: 0px;">You can look at the code generated from the MXML by setting the &#8216;-keep&#8217; compiler option <a href="http://blog.flexexamples.com/2008/08/02/viewing-a-flex-applications-generated-source-code/" target="_blank">as described here</a>.</div>
<div style="margin-top: 0px; margin-bottom: 0px;"><a href="http://blog.flexexamples.com/2008/08/02/viewing-a-flex-applications-generated-source-code/"></a>The generated source code will appear in a &#8216;generated&#8217; folder under the project’s /bin-debug (in Flex Builder). You&#8217;ll see that it contains dozens of files, related to styles, properties and whatever you included in the layout.</div>
<div style="margin-top: 0px; margin-bottom: 0px;">Excessive usage of states and data binding will generate massive code that (like any automatically generated code) is not optimized. To demonstrate,  here is the code genrated for binding a variable.</div>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>Bindable<span style="color: #000000;">&#93;</span>
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> myData<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>Will generate somthing like this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #0033ff; font-weight: bold;">set</span> myData<span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
   <span style="color: #6699cc; font-weight: bold;">var</span> oldValue<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Object</span> = <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span>_1167269232myData<span style="color: #000066; font-weight: bold;">;</span>
   <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>oldValue <span style="color: #000066; font-weight: bold;">!</span>== <span style="color: #004993;">value</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
      <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span>_1167269232myData= <span style="color: #004993;">value</span><span style="color: #000066; font-weight: bold;">;</span>
      <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">hasEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;propertyChange&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
         <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">dispatchEvent</span><span style="color: #000000;">&#40;</span>mx<span style="color: #000066; font-weight: bold;">.</span>events<span style="color: #000066; font-weight: bold;">.</span>PropertyChangeEvent<span style="color: #000066; font-weight: bold;">.</span>createUpdateEvent
         <span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #990000;">&quot;myData&quot;</span><span style="color: #000066; font-weight: bold;">,</span> oldValue<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">value</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
      <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>And MXML state code like the following</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:states<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:State</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;normal&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:State</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;before&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:State</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;after&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/s:states<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:Label</span> <span style="color: #000066;">text</span>=<span style="color: #ff0000;">&quot;aaa&quot;</span> <span style="color: #000066;">color.before</span>=<span style="color: #ff0000;">&quot;#cccccc&quot;</span> <span style="color: #000066;">color.after</span>=<span style="color: #ff0000;">&quot;#888888&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:Group</span> <span style="color: #000066;">includeIn</span>=<span style="color: #ff0000;">&quot;after&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:Label</span> <span style="color: #000066;">text</span>=<span style="color: #ff0000;">&quot;bbb&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/s:Group<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:Group</span> <span style="color: #000066;">visible.before</span>=<span style="color: #ff0000;">&quot;false&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:Label</span> <span style="color: #000066;">text</span>=<span style="color: #ff0000;">&quot;www&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/s:Group<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Generates:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> _TestApp_Group1_factory<span style="color: #000066; font-weight: bold;">:</span>DeferredInstanceFromFunction =
 <span style="color: #0033ff; font-weight: bold;">new</span> mx<span style="color: #000066; font-weight: bold;">.</span>core<span style="color: #000066; font-weight: bold;">.</span>DeferredInstanceFromFunction<span style="color: #000000;">&#40;</span>_TestApp_Group1_i<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
&nbsp;
states = <span style="color: #000000;">&#91;</span>
 <span style="color: #0033ff; font-weight: bold;">new</span> State <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#123;</span>
   <span style="color: #004993;">name</span><span style="color: #000066; font-weight: bold;">:</span> <span style="color: #990000;">&quot;normal&quot;</span><span style="color: #000066; font-weight: bold;">,</span>
   overrides<span style="color: #000066; font-weight: bold;">:</span> <span style="color: #000000;">&#91;</span>
   <span style="color: #000000;">&#93;</span>
 <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span>
 <span style="color: #000066; font-weight: bold;">,</span>
 <span style="color: #0033ff; font-weight: bold;">new</span> State <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#123;</span>
   <span style="color: #004993;">name</span><span style="color: #000066; font-weight: bold;">:</span> <span style="color: #990000;">&quot;before&quot;</span><span style="color: #000066; font-weight: bold;">,</span>
   overrides<span style="color: #000066; font-weight: bold;">:</span> <span style="color: #000000;">&#91;</span>
     <span style="color: #0033ff; font-weight: bold;">new</span> mx<span style="color: #000066; font-weight: bold;">.</span>states<span style="color: #000066; font-weight: bold;">.</span>SetStyle<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">.</span>initializeFromObject<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#123;</span>
       <span style="color: #004993;">target</span><span style="color: #000066; font-weight: bold;">:</span> <span style="color: #990000;">&quot;_TestApp_Label1&quot;</span><span style="color: #000066; font-weight: bold;">,</span>
       <span style="color: #004993;">name</span><span style="color: #000066; font-weight: bold;">:</span> <span style="color: #990000;">&quot;color&quot;</span><span style="color: #000066; font-weight: bold;">,</span>
       <span style="color: #004993;">value</span><span style="color: #000066; font-weight: bold;">:</span> <span style="color: #000000; font-weight:bold;">13421772</span>
     <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span>
     <span style="color: #000066; font-weight: bold;">,</span>
     <span style="color: #0033ff; font-weight: bold;">new</span> mx<span style="color: #000066; font-weight: bold;">.</span>states<span style="color: #000066; font-weight: bold;">.</span>SetProperty<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">.</span>initializeFromObject<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#123;</span>
       <span style="color: #004993;">target</span><span style="color: #000066; font-weight: bold;">:</span> <span style="color: #990000;">&quot;_TestApp_Group2&quot;</span><span style="color: #000066; font-weight: bold;">,</span>
       <span style="color: #004993;">name</span><span style="color: #000066; font-weight: bold;">:</span> <span style="color: #990000;">&quot;visible&quot;</span><span style="color: #000066; font-weight: bold;">,</span>
       <span style="color: #004993;">value</span><span style="color: #000066; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">false</span>
     <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span>
   <span style="color: #000000;">&#93;</span>
 <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span>
 <span style="color: #000066; font-weight: bold;">,</span>
 <span style="color: #0033ff; font-weight: bold;">new</span> State <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#123;</span>
   <span style="color: #004993;">name</span><span style="color: #000066; font-weight: bold;">:</span> <span style="color: #990000;">&quot;after&quot;</span><span style="color: #000066; font-weight: bold;">,</span>
<span style="color: #000066; font-weight: bold;">...</span></pre></div></div>

<p>All that code only to switch a couple of properties when a state changes. A similar result could be achieved in only a few lines of hand written code.</p>
<p><span style="font-family: Verdana; line-height: normal;"> </span></p>
<div style="margin-top: 0px; margin-bottom: 0px;">
<h3 style="font-size: 12pt;">Every Byte Counts</h3>
<p>As we have seen, the code for the seemingly simple application could easily gain weight.  Large code not only makes a larger swf file, it also uses more memory at runtime. A large memory footprint can translate directly into a slow and sluggish performance, as the OS is frequently required to allocate more memory, triggering cache faults and page faults that dramatically slow down the running process.</p>
</div>
<div style="margin-top: 0px; margin-bottom: 0px;">This is specially significant in low memory environments like netbooks and mobile devices. Small memory footprint is a must for lightweight applications and it benefits all platforms, as the application will run well anywhere. It should be high on the priority list when your application is required to be smooth and snappy.</div>
<p>So we want to start a project that will use some simple UI elements and layouts. We might want to be able to use mxml and data binding as well. In the list of requirements we also have a light and fast user experience. For the type of projects that requires very simple UI, like interactive ads or games we would need a minimal set of components, and in most cases we want to heavily customize their visual appearance.</p>
<p>But for this minimal set, we would drag the entire Flex framework with all its overhead with it. Does it really have to be that way?</p>
<p>What are the options out there?<br />
Part 2 will lay out some of the available lightweight alternatives, their pros and cons and how to incorporate them into your projects.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.flexwiz.net/go-beyond-flex-part-1-how-to-make-your-code-smaller-and-faster/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

