<?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>Colin Eberhardt&#039;s Adventures in .NET &#187; mix10k</title>
	<atom:link href="http://www.scottlogic.co.uk/blog/colin/tag/mix10k/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.scottlogic.co.uk/blog/colin</link>
	<description>Colin Eberhardt&#039;s Adventures in .NET</description>
	<lastBuildDate>Thu, 09 Feb 2012 10:21:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>My Mix10k entry &#8211; Old Skool demo &#8211; plus a few tips</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2010/01/my-mix10k-entry-old-skool-demo-plus-a-few-tips/</link>
		<comments>http://www.scottlogic.co.uk/blog/colin/2010/01/my-mix10k-entry-old-skool-demo-plus-a-few-tips/#comments</comments>
		<pubDate>Thu, 07 Jan 2010 15:27:33 +0000</pubDate>
		<dc:creator>Colin Eberhardt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[mix10k]]></category>
		<category><![CDATA[silverlight]]></category>
		<category><![CDATA[WriteableBitmap]]></category>

		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=539</guid>
		<description><![CDATA[This blog post is about my entry to the Mix10k code competition, and old-skool demo, plus a few tips about how to keep you code size to below 10k. The mix10k challenge, where you are given 10k to create a Silverlight / HTML5 application, has been on my mind for a while &#8230; the Christmas [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_light-green" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwww.scottlogic.co.uk%252Fblog%252Fcolin%252F2010%252F01%252Fmy-mix10k-entry-old-skool-demo-plus-a-few-tips%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22My%20Mix10k%20entry%20-%20Old%20Skool%20demo%20-%20plus%20a%20few%20tips%20%23%22%20%7D);"></div>
<p><em>This blog post is about my entry to the Mix10k code competition, and old-skool demo, plus a few tips about how to keep you code size to below 10k.</em></p>
<p>The <a href="http://mix10k.visitmix.com/">mix10k challenge</a>, where you are given 10k to create a Silverlight / HTML5 application, has been on my mind for a while &#8230; the Christmas vacation was the perfect excuse to put together my entry. Inspired by the demo&#8217;s of the Amiga era, here is my mix10k old-skool demo entry:</p>
<div id="slPluginHost1" > <object id="SilverlightPlugin1" width="700" height="500" data="data:application/x-silverlight," type="application/x-silverlight-2" ><param name="source" value="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2010/01/S.xap"/><a href="http://go.microsoft.com/fwlink/?LinkID=124807"  style="text-decoration: none;"> <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/></a></object></div>
<p>You can view my entry (and more importantly vote for it!) at the <a href="http://mix10k.visitmix.com/Entry/Details/147">mix10k gallery</a>.</p>
<p>For me the greatest challenge was the music, not least for the fact that I am no good at writing the stuff myself! The mix10k rules are fairly relaxed, you can download content, such as images and music, allowing you to circumnavigate the 10k limit. However, I thought it would be a more interesting challenge to pack everything into 10k.</p>
<p>Size-limited demos typically use synthesis rather than sampling for the musics. Unfortunately Silverlight isn&#8217;t really geared towards sound synthesis, however, a few people have managed to create <a href="http://community.irritatedvowel.com/blogs/pete_browns_blog/archive/2009/03/23/Creating-Sound-using-MediaStreamSource-in-Silverlight-3-Beta.aspx">synthesized media sources</a>. I decided to go the sampling route and use an MP3 track for my music, the idea being to use a short, highly compressed MP3 track and loop it. I posted a message on the <a href="http://8bitcollective.com/">8bitcollective</a> forums and within a day had a funky, and highly compressed, 4 second MP3 clip for my demo. The MP3 filesize of 4,248 bytes left me 5,992 to play with.</p>
<p>However, the next challenge was looping the audio, which is unfortunately something that is not supported out-of-the-box. For longer media clips it is acceptable to <a href="http://www.williablog.net/williablog/post/2007/10/30/How-to-Make-a-Media-Element-Loop-Indefinitely.aspx">catch the MediaEnded event, re-wind and re-start</a>. However, there is a delay of about half a second, which with a 4 second looped audio clip is pretty disruptive! Fortunately Silverlight does expose a lower level API for media via the abstract class MediaStreamSource, which has virtual methods which you can override to supply audio direct from a stream. The codeplex project <a href="http://code.msdn.microsoft.com/ManagedMediaHelpers">ManagedMediaHelpers</a> is a great place to start for information on how to load MP3 audio from a file. Typically the code to read and stream an MP3 file is over 10k, however, I was able to trim this down to roughly 1k by removing all the code for parsing the MP3 headers, replacing it with the hard-coded values for my specific MP3 file.</p>
<p>Unfortunately, when the audio was looped, it just didn&#8217;t sound right. The trouble is, within MP3 files, the audio is split into frames. Using a useful utility from <a href="http://www.codeproject.com/KB/audio-video/mpegaudioinfo.aspx">codeproject</a>, I found that my MP3 file consisted of 56 frames of ~67ms in length. However, the audio loop was not a multiple of 67ms. To rectify this, I downloaded a copy of the free audio editor <a href="http://audacity.sourceforge.net/">Audacity</a>, and &#8216;stretched&#8217; the track to fit into an integer number of frames, and re-encoded in MP3 format via <a href="http://lame.sourceforge.net/">lame</a>. Finally, I had looping MP3 audio! (and 4,958 bytes left)</p>
<p><a href="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2009/12/audacity.png"><img class="aligncenter size-medium wp-image-542" title="audacity" src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2009/12/audacity-300x187.png" alt="audacity" width="300" height="187" /></a></p>
<p>After the battle I had with the audio, constructing the visuals, the scrolling text, interference patterns, plasma and starfield was a piece of cake!</p>
<p>Here are a few quick tips for keeping your filesize under 10k.</p>
<ul>
<li><strong>Use the <code>var</code> keyword wherever possible</strong><br />
&nbsp;
</li>
<li><strong>Use the <code>using</code> for aliasing frequently used types</strong>
<p>For example:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">K </span><span style="color: #008000;">=</span> <span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Windows</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Media</span><span style="color: #008000;">.</span><span style="color: #0000FF;">MediaSourceAttributesKeys</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">J</span><span style="color: #008000;">=</span> <span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Windows</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Media</span><span style="color: #008000;">.</span><span style="color: #0000FF;">MediaStreamAttributeKeys</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">M </span><span style="color: #008000;">=</span> <span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Math</span><span style="color: #008000;">;</span></pre></div></div>

<p>With type names as long as <code>MediaSourceAttributesKeys</code>, you only need to use them three times for a using alias to make sense!</p>
</li>
<li><strong><em>chain </em>initialisation of variables</strong>
<p>The following code:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">int</span> x1 <span style="color: #008000;">=</span> sin1<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">8</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #FF0000;">200</span>
<span style="color: #6666cc; font-weight: bold;">int</span> y1 <span style="color: #008000;">=</span> sin1<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">3</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">*</span> <span style="color: #FF0000;">10</span> <span style="color: #008000;">+</span> <span style="color: #FF0000;">100</span>
<span style="color: #6666cc; font-weight: bold;">int</span> g <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>tx <span style="color: #008000;">-</span> x1<span style="color: #008000;">&#41;</span> <span style="color: #008000;">*</span> <span style="color: #008000;">&#40;</span>tx <span style="color: #008000;">-</span> x1<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Can be shortened to:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">int</span> x1 <span style="color: #008000;">=</span> sin1<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">8</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #FF0000;">200</span>, y1 <span style="color: #008000;">=</span> sin1<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">3</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">*</span> <span style="color: #FF0000;">10</span> <span style="color: #008000;">+</span> <span style="color: #FF0000;">100</span>, g <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>tx <span style="color: #008000;">-</span> x1<span style="color: #008000;">&#41;</span> <span style="color: #008000;">*</span> <span style="color: #008000;">&#40;</span>tx <span style="color: #008000;">-</span> x1<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

</li>
<li><strong>Tabs not spaces</strong>
<p>If you are not to bothered about squeezing the most out of those 10k bytes and want your code to look pretty, at least save yourself a bit of room by using Tabs!</p>
</li>
<li><strong>Choose wisely between XAML and C#</strong>
<p>One big advantage that XAML has over C# is its use of value converters, compare the following XAML:</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;Ellipse</span> <span style="color: #000066;">Fill</span>=<span style="color: #ff0000;">&quot;White&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>to its equivalent in C#:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var el <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Ellipse<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
   Fill <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SolidColorBrush<span style="color: #008000;">&#40;</span>Color<span style="color: #008000;">.</span><span style="color: #0000FF;">FromArgb</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">255</span>,<span style="color: #FF0000;">255</span>,<span style="color: #FF0000;">255</span>,<span style="color: #FF0000;">255</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span></pre></div></div>

<p>In this case, XAML is a clear winner.</p>
</li>
<li><strong>Use lambda expressions for event handlers</strong>
<p>You can achieve great savings by including your event handling logic within anonymous delegates (via lambda expressions), it also means that you can move variables from class scope (i.e. files) to method scope and use the <code>var</code> keyword. Compare:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">WriteableBitmap bitmap <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> WriteableBitmap<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">200</span>,<span style="color: #FF0000;">200</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
MyPage<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	var t <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DispatcherTimer<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	t<span style="color: #008000;">.</span><span style="color: #0000FF;">Tick</span> <span style="color: #008000;">+=</span><span style="color: #008000;">new</span> EventHandler<span style="color: #008000;">&#40;</span>t_Tick<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #6666cc; font-weight: bold;">void</span>  t_Tick<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, EventArgs e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #008080; font-style: italic;">// do stuff with the bitmap</span>
	bitmap<span style="color: #008000;">.</span><span style="color: #0000FF;">Invalidate</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>to:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">MyPage<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	var bitmap <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> WriteableBitmap<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">200</span>,<span style="color: #FF0000;">200</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	var t <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DispatcherTimer<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	t<span style="color: #008000;">.</span><span style="color: #0000FF;">Tick</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">&#40;</span>s,e<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #008080; font-style: italic;">// do stuff with the bitmap</span>
			bitmap<span style="color: #008000;">.</span><span style="color: #0000FF;">Invalidate</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

</li>
<li><strong>Ditch that App!</strong>
<p>The <code>App.xaml</code> and <code>App.xaml.cs</code> full of template code that you do not need. Simply ditch these files entirely and replace with a minimal application that indicates the page to display:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> A <span style="color: #008000;">:</span> Application
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> A<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Startup <span style="color: #008000;">+=</span> <span style="color: #008000;">&#40;</span>i, j<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> RootVisual <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MainPage<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

</li>
<li><strong>Minify your code</strong>
<p>Perhaps the greatest savings can be achieved by &#8216;minifying&#8217; your code, i.e. removing any extra whitespace and reducing variable, method, types names to single characters. The problem is that in doing so, your code will become unreadable!</p>
<p>In order to minify my code yet maintain readability I created a very simple console application that read my application source and performed a bunch of string replacements in order to reduce its size &#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">static</span> Dictionary<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span>, <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;</span> reps <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Dictionary<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span>, <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #008000;">&#123;</span><span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Environment</span><span style="color: #008000;">.</span><span style="color: #0000FF;">NewLine</span>, <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">&#125;</span>, 
  <span style="color: #008000;">&#123;</span><span style="color: #666666;">&quot;  &quot;</span>, <span style="color: #666666;">&quot; &quot;</span><span style="color: #008000;">&#125;</span>,
  <span style="color: #008000;">&#123;</span><span style="color: #666666;">&quot; = &quot;</span>, <span style="color: #666666;">&quot;=&quot;</span><span style="color: #008000;">&#125;</span>,
  <span style="color: #008000;">&#123;</span><span style="color: #666666;">&quot; == &quot;</span>, <span style="color: #666666;">&quot;==&quot;</span><span style="color: #008000;">&#125;</span>,
  <span style="color: #008000;">&#123;</span><span style="color: #666666;">&quot; * &quot;</span>, <span style="color: #666666;">&quot;*&quot;</span><span style="color: #008000;">&#125;</span>,
  <span style="color: #008000;">&#123;</span><span style="color: #666666;">&quot; &gt; &quot;</span>, <span style="color: #666666;">&quot;&gt;&quot;</span><span style="color: #008000;">&#125;</span>,
  <span style="color: #008000;">&#123;</span><span style="color: #666666;">&quot; &lt; &quot;</span>, <span style="color: #666666;">&quot;&lt;&quot;</span><span style="color: #008000;">&#125;</span>,
  <span style="color: #008000;">...</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">// methods </span>
  <span style="color: #008000;">&#123;</span><span style="color: #666666;">&quot;Timer_Tick&quot;</span>, <span style="color: #666666;">&quot;T&quot;</span><span style="color: #008000;">&#125;</span>,
  <span style="color: #008000;">&#123;</span><span style="color: #666666;">&quot;CreateLine&quot;</span>, <span style="color: #666666;">&quot;g&quot;</span><span style="color: #008000;">&#125;</span>,
  <span style="color: #008000;">&#123;</span><span style="color: #666666;">&quot;DrawLines&quot;</span>, <span style="color: #666666;">&quot;ln&quot;</span><span style="color: #008000;">&#125;</span>,
  <span style="color: #008000;">...</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">// xaml</span>
  <span style="color: #008000;">&#123;</span><span style="color: #666666;">&quot;grid&quot;</span>, <span style="color: #666666;">&quot;q&quot;</span><span style="color: #008000;">&#125;</span>,
  <span style="color: #008000;">&#123;</span><span style="color: #666666;">&quot;image&quot;</span>, <span style="color: #666666;">&quot;o&quot;</span><span style="color: #008000;">&#125;</span>,
  <span style="color: #008000;">&#123;</span><span style="color: #666666;">&quot;canvas&quot;</span>, <span style="color: #666666;">&quot;c&quot;</span><span style="color: #008000;">&#125;</span>,
  <span style="color: #008000;">&#123;</span><span style="color: #666666;">&quot;MainPage&quot;</span>, <span style="color: #666666;">&quot;Y&quot;</span><span style="color: #008000;">&#125;</span>,
  <span style="color: #008000;">...</span>
<span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span></pre></div></div>

<p>The output of this console application is sent to another project which builds my final entry. The result of this was to make my reasonably compact code <strong>47%</strong> smaller!
</li>
</ul>
<p>You can download my thoroughly messy and unreadable code: <a href='http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2010/01/mix10ksrc.zip'>mix10ksrc.zip</a></p>
<p>And vote for it on the <a href="http://mix10k.visitmix.com/Entry/Details/147">mix10k site</a> (Did I say that already?)</p>
<p>Regards, Colin E.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.scottlogic.co.uk/blog/colin/2010/01/my-mix10k-entry-old-skool-demo-plus-a-few-tips/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

