PhobosLab http://www.phoboslab.org/log Latest news en Are We Fast Yet? http://www.phoboslab.org/log/2011/08/are-we-fast-yet <p>Mozilla constantly asks this question, but <a href="http://arewefastyet.com/">their answer</a> really doesn't tell us all that much. Yes, JavaScript has been getting faster and faster and the <em>v8bench</em>, <em>kraken</em> and <em>sunspider</em> benchmarks are a good indicator for that. Yet, they all miss some issues that are quite important for HTML5 Gaming.</p> <p>One of these issues was made blatantly obvious when I finished work on the performance graph for the <a href="http://playbiolab.com/?debug">new debug menu</a> in my <a href="http://Impactjs.com/">JavaScript Game Engine</a>: Garbage Collector pauses.<br/></p> <p>All JavaScript engines periodically run a Garbage Collector to release unused memory. In some cases this GC step can take several hundred milliseconds, in which the game comes to a complete halt. Even if that only happens every 600 frames, or 10 seconds, it's far more annoying than a game that runs at a lower, but constant framerate.<br/></p> <p><img class="center" src="http://www.phoboslab.org/files/images/impact-debug-menu.png" alt="Impact Debug Menu"/> <em>Performance Graph for Firefox 6 – the orange spikes indicate GC pauses</em></p> <p>GC pauses have been a huge problem with Firefox 3.6 and Firefox 6 is still the worst offender here. Other browsers have shorter GC pauses, but there's still room for improvement. To gain some more insight, I constructed a new benchmark that essentially simulates a playthrough for a game level.<br/></p> <p>Give it a try at <a href="http://html5-benchmark.com">HTML5-Benchmark.com</a>.</p> <p>Now, to be clear, this benchmark is very <em>“opinionated”</em>. It's by no means a complete benchmark for all HTML5 features, but only tests one very specific use case: smooth running games rendered with the <code>&lt;canvas&gt;</code> element. It's a very real use case though; not an average for hundreds or thousands of small tests.</p> <p>The score for the HTML5-Benchmark takes the total time the browser spent rendering frames and a big penalty for pauses into account (the exact formula is <code>1000000/(sqrt(totalTime) + lagTime * 0.1)</code>). The benchmark automatically runs at a “reasonable” screen size - i.e. a screen size that makes sense for a game on the particular device. Thus, the final score is a real indicator for the browser's ability to smoothly run HTML5 games.</p> <p><a href="http://www.phoboslab.org/files/images/html5-benchmark-scores.png" rel="whitebox"><img class="center" src="http://www.phoboslab.org/cache/thumbs/500x330/files/images/html5-benchmark-scores.jpg" alt="HTML5-Benchmark Scores"/></a></p> <p>A score of about 1500 means the game is playable. Note that the benchmark actually runs a bit slower on mobile devices than the game itself. This is because the game utilizes a special pre-rendered background mode on mobile devices, but doesn't do so in the benchmark to keep the scores comparable.</p> <p>It's almost comical how bad the Android's Browser performed in my tests: the Samsung Galaxy Tab 10.1 is actually <em>slower</em> than a four year old 1st gen iPod Touch. Firefox 4 on the same tablet performs a lot better though.</p> <p>The desktop version of Firefox 6 still suffers a lot from the occasional GC pauses. This makes Firefox the last choice if you want to enjoy HTML5 games. The performance <em>decrease</em> from the stable Chrome 13 to the dev version of Chrome 15 is quite strange too. It may have to do something with the new hardware acceleration for Canvas' 2d context.</p> <p><img class="center" src="http://www.phoboslab.org/files/images/impact-benchmark.jpg" alt="Impac Benchmark"/></p> <p>There are some things that the benchmark can't measure. Opera for instance has a very steady timing and never misses a frame. On my machine however, the benchmark still looks choppy - probably because the benchmark's timer isn't synchronized with the refresh rate of the display. Opera essentially renders frames that it never displays. The new <a href="https://developer.mozilla.org/en/DOM/window.mozRequestAnimationFrame">requestAnimationFrame()</a> could fix this, but it hasn't arrived in all browsers yet.</p> <p>IE9's smoothness on the other hand is remarkable. Of all browsers and systems I tested, IE9 subjectively produced the best results. It seems to precisely synchronize JavaScript execution with the refresh rate: the benchmark runs at exactly 60 FPS on my machine, even though the timer is scheduled every 16 milliseconds, or 62.5 FPS.</p> <p>This leads me to ask a few questions: shouldn't browser vendors concentrate on fixing <code>setInterval()</code> first, before implementing <code>requestAnimationFrame()</code>? And shouldn't it behave more like <code>setInterval()</code> instead of <code>setTimeout()</code>? That is – I don't want to call a function to schedule each and every frame I want to draw, but just call it once and let the browser schedule the frames.</p> <p><code>requestAnimationFrame()</code> looks good on paper, but it doesn't actually produce visually better results than <code>setInterval()</code> in those browsers that implement it.<br/></p> <p>On my machine, Chrome 13 and Firefox 6 awkwardly jump between 120 and 60 FPS when using it (it's disabled in the benchmark) and even without GC pauses there are noticeable hiccups, while IE9 is able to produce perfectly smooth animations without it. At the moment <code>requestAnimationFrame()</code> is truly worthless for games.</p> <p>So, no – we aren't fast yet. But we're getting there.</p> Thu, 11 Aug 2011 02:17:06 +0200 http://www.phoboslab.org/log/2011/08/are-we-fast-yet JavaScriptCore Project Files for iOS http://www.phoboslab.org/log/2011/06/javascriptcore-project-files-for-ios <p>Some weeks ago <a href="http://www.phoboslab.org/log/2011/04/ios-and-javascript-for-real-this-time">I released</a> a compiled version of the JavaScriptCore library for iOS as part of my <a href="http://impactjs.com/">game engine</a>. Since then I had many people asking for the project files necessary to build the library - and I promised to release them as well. But before I do so, let me just rant about Apple's politics real quick:</p> <p>They suck.</p> <p>You see, JavaScriptCore is an Open Source library. In fact the whole <a href="http://www.webkit.org/">WebKit project</a> is. It's licensed under BSD and LGPL licenses. The latter of which requires that if you modify the software you have to release your modified version – including the complete source – under the LGPL as well. Furthermore, it <a href="http://www.opensource.org/licenses/lgpl-2.1.php">states</a>: <em>&quot;For a library, complete source code means all the source code for all modules it contains, plus any associated interface definition files, <strong>plus the scripts used to control compilation and installation of the library</strong>.&quot;</em></p> <p>Yet the JavaScriptCore sources that you get from <a href="http://opensource.apple.com/source/JavaScriptCore/JavaScriptCore-721.26/">Apple's Open Source page</a> come without a project file. So while you get the source code for the library, it's useless because you can't compile it.</p> <p>Luckily you can also get the current JavaScriptCore sources directly from the <a href="http://svn.webkit.org/repository/webkit/trunk/Source/JavaScriptCore/">SVN repository</a> – and behold, they even come with an Xcode project file, ready to be build for MacOSX with the touch of a button. But – and here's the kicker – this project file curiously misses the iOS platform target. You can't build it for the iPhone or iPad.</p> <p>Adding this iOS target to the project file is by no means a trivial endeavor, mind you. Especially not in Xcode. I ended up duplicating the MacOSX target, setting the <code>Base SDK</code> to <code>Latest iOS</code>, changing the <code>Supported Platforms</code> to <code>iphoneos iphonesimulator</code> and poking around in the project file with a text editor to change the <code>productType</code> to <code>library.static</code>, because you're not allowed to build a <code>framework</code> for iOS.</p> <p>After some more changes that I can't remember (I'll be sure to document this next time I do it), I was finally able to compile the library for iOS. I submitted my game <a href="http://itunes.apple.com/us/app/biolab-disaster/id433062854">Biolab Disaster</a> to the App Store only to see that it was rejected the next day.</p> <p>JavaScriptCore utilizes the libicucore library to sort strings in a unicode fashion. Apparently libicucore is a &quot;private&quot; API on iOS – which is curious because I can add this library to my iOS project without any dirty hacks, I'm just not allowed to use any functions of it. So I compiled JavaScriptCore again, this time setting the <code>UCONFIG_NO_COLLATION</code> preprocessor macro to disable unicode sorting.</p> <p>Did I mention Apple's politics suck?</p> <p>Anyway, here's the statically compiled <code>libiOSJavaScriptCore.a</code> that you can use in your iOS projects and the source and project file if you want to build it yourself for whatever reason:</p> <ul> <li><a href="http://www.phoboslab.org/files/jsc/libiOSJavaScriptCore-534.27.zip">libiOSJavaScriptCore-534.27.zip</a> – compiled library</li> <li><a href="http://www.phoboslab.org/files/jsc/iOSJavaScriptCore-534.27.zip">iOSJavaScriptCore-534.27.zip</a> – source code and project file</li> </ul> <p>This all is based on the slightly outdated 534.27 version of JavaScriptCore, but at least this version seems to be AppStore compatible.</p> <h3>Using libiOSJavaScriptCore.a in your Project:</h3> <ul> <li>Copy the JavaScriptCore directory with the header files from the ZIP to your project folder and add them to your Xcode project</li> <li>Copy the libiOSJavaScriptCore.a into your project folder</li> <li>Add the <code>libstdc++.dylib</code>, <code>libicucore.dylib</code> from the list and the <code>libiOSJavaScriptCore.a</code> from &quot;Add other...&quot; <a href="http://stackoverflow.com/questions/3352664/how-to-add-existing-frameworks-in-xcode-4/3377682#3377682">like so</a></li> <li><code><h>import &lt;JavaScriptCore/JavaScriptCore.h&gt;</code></li> </ul> <h3>Building JavaScriptCore from the Source:</h3> <ul> <li>Select <em>iOSJavaScriptCore iPhone</em> as the active Scheme and build</li> <li>Select <em>iOSJavaScriptCore iPhone Simulator</em> as the active Scheme and build</li> <li>Navigate your terminal to the build directory: depending on your Xcode settings either <code>build/Products/</code> directly in your project directory or <code>~/Library/Developer/Xcode/DerivedData/JavaScriptCore-xxx/Build/Products/</code> </li> <li>Combine the ARM6/7 and x86 (Simulator) libs into one: <code>lipo -create Production-iphoneos/libiOSJavaScriptCore.a Production-iphonesimulator/libiOSJavaScriptCore.a -output libiOSJavaScriptCore.a</code></li> </ul> <p>Btw.: AppCelerator <a href="https://github.com/appcelerator/webkit_titanium/tree/master/Source/JavaScriptCore">maintains their own version of JavaScriptCore</a> that they use in Titanium, but I have no idea how to build it. Any hints are greatly appreciated!</p> <p>If you want to support me, please consider buying a license of <a href="http://impactjs.com/">my game engine</a>. It also makes a great gift ;-) </p> Mon, 13 Jun 2011 23:32:54 +0200 http://www.phoboslab.org/log/2011/06/javascriptcore-project-files-for-ios iOS and JavaScript - for Real this Time! http://www.phoboslab.org/log/2011/04/ios-and-javascript-for-real-this-time <p>Less talk, more action – Apple just approved two of my JavaScript games for the AppStore: <a href="http://itunes.apple.com/us/app/biolab-disaster/id433062854">Biolab Disaster</a> and <a href="http://itunes.apple.com/us/app/drop-js/id433062287">Drop</a>. Both are free; go check them out. You can also play them in your browser <a href="http://playbiolab.com/">here</a> and <a href="http://impactjs.com/drop/">here</a>.<br/></p> <p>Both games are pretty simple (the <a href="http://impactjs.com/documentation/drop-source-code">source</a> for Drop is only 300 odd lines long) and written with my JavaScript Game Engine <a href="http://impactjs.com/">Impact</a>.</p> <p>These are certainly not the first games written in JavaScript to be available in the AppStore. Tools like <a href="http://www.appmobi.com/">AppMobi</a>, <a href="http://www.phonegap.com/">PhoneGap</a> or <a href="http://www.appcelerator.com/">Titanium</a> make it easy to bundle some HTML pages and JavaScript together in an App and display them in a UIWebView, which is basically just a browser window (<strong>correction:</strong> Titanium doesn't use a UIWebView but instead has some native bindings). Games written with Impact already work okay-ish in the iPhone's browser and thus also in AppMobi and PhoneGap.</p> <p>So what's so special about these two games now? They don't use PhoneGap or Titanium. They don't even use a UIWebView. Instead, they bypass the iPhone's browser altogether and use Apple's JavaScript interpreter (JavaScriptCore) directly. All graphics are rendered with OpenGL instead of in a browser window and all sound and music is played back with OpenAL instead of… well, having <a href="http://www.phoboslab.org/log/2011/03/the-state-of-html5-audio">no sound at all</a>.</p> <p>What makes this possible is a compatibility layer that mimics the HTML5 Canvas and Audio APIs but is implemented with OpenGL and OpenAL behind the scenes. Think of it as a browser that can only display a Canvas element and play Audio elements, but does not render generic HTML pages. A browser perfectly suited for HTML5 games.</p> <p>This means you can take your JavaScript games written for Impact and run them on iOS with perfect sound and touch input and way better drawing performance than with Mobile Safari. Now, to be clear, I only implemented a bare minimum of the Canvas API - just enough to be able to run Impact. The whole thing is still in a very experimental state.</p> <p>If you have a license for Impact you will find the complete source code for this all on your <a href="http://impactjs.com/download/">download page</a>. I also wrote some <a href="http://impactjs.com/documentation/ios/overview">basic documentation</a> to get you started. In theory, you don't have to know anything about Objective-C to use this, but at this stage some Objective-C knowledge will sure come in handy. Again, this is very experimental. Don't expect it to work at all.</p> <p>If you've been following this blog for a while, you might remember a <a href="http://www.phoboslab.org/log/2010/10/impact-for-ios">post from October 2010</a> where I attempted the exact same thing. Back then I used the JavaScriptCore library that is already available on iOS and used by Apple in Mobile Safari. The problem was, that this library is &quot;private&quot;, meaning that Apple does not want you to use it. And since you can't publish anything in the AppStore that uses private libraries I abandoned the idea.</p> <p>However I recently revisited the project, because there was still a chance to make this work: JavaScriptCore is a part of the open source WebKit project. Instead of using the private library that comes with iOS, you <em>theoretically</em> could compile your own version of this library and bundle it together with your App. Which is exactly what I did.</p> <p>Since Apple does not provide any project files to compile JavaScriptCore for iOS (presumably to annoy us) and JavaScriptCore itself uses some of iOS' private APIs, compiling this beast into a static library - in an App Store compatible fashion - took me a few days.<br/></p> <p>I also had to make a small sacrifice: JavaScriptCore uses libicu to sort strings according to a unicode locale. Sadly, libicu is also private on iOS and bundling it is not an option because of its size. So I got rid of libicu completely. This means that only ASCII strings are now sorted correctly (e.g. the umlaut &quot;Ä&quot; will come after &quot;Z&quot;, not after &quot;A&quot; as it should). Other than that, the JavaScript library should behave exactly as the private one that comes with iOS.</p> <p>Also, the JavaScriptCore library bundled with iOSImpact does <strong>not</strong> use the JIT compiler (Nitro). You can't allocate executable memory on the iPhone and if Apple doesn't lift that restriction, there's nothing I can do about it. However, Apple recently made an exception for Mobile Safari – <em>if</em> they would make their JavaScriptCore API public, they probably <em>could</em> enable the JIT for everyone. That's a big <code>if</code> though; I don't see it happening, because Apple loves native code (Objective-C) and hates scripting languages.</p> <p>I was afraid Apple would reject the two games for some obscure reason, but they didn't. Which leaves me to wonder why the JavaScriptCore library that comes with iOS is private in the first place. Bundling JavaScriptCore with your App adds about 2MB in size – not much, but I fail to see how this can be in Apple's interest.</p> <p>Anyway, the performance of both games is pretty good. I still get some occasional slowdowns (~20fps) on my iPhone3GS in Biolab Disaster when there are too many particles on the screen, but it remains playable at all times. It's also nice to have perfectly working sound on iOS now - something even some desktop browsers still struggle with.</p> Wed, 27 Apr 2011 10:02:28 +0200 http://www.phoboslab.org/log/2011/04/ios-and-javascript-for-real-this-time Multiple Channels for HTML5 Audio http://www.phoboslab.org/log/2011/03/multiple-channels-for-html5-audio <p>Now that I've calmed down a bit after my <a href="http://www.phoboslab.org/log/2011/03/the-state-of-html5-audio">The State of HTML5 Audio</a> article, let's see if can actually write something productive.</p> <p>Here's the deal: for a typical game you may have a sound effect that you want to play very often. In <a href="http://playbiolab.com">Biolab Disaster</a> the sound of your plasma gun is such an effect. This particular sound has a length of 0.6 seconds, but you sure can mash the shoot button quicker than 1.66 times per second – so to not interrupt the plasma sound each time you press the button, but instead play a <em>new</em> sound we need to have multiple sound “channels”.</p> <p><a href="http://www.phoboslab.org/log/2011/03/multiple-channels-for-html5-audio">Read complete post &raquo;</a></p> Fri, 11 Mar 2011 02:04:52 +0100 http://www.phoboslab.org/log/2011/03/multiple-channels-for-html5-audio The State of HTML5 Audio http://www.phoboslab.org/log/2011/03/the-state-of-html5-audio <p>When I started to work on my <a href="http://impactjs.com">JavaScript Game Engine</a> back in October 2009, the biggest problems I encountered were with the new HTML5 Audio Element. The Canvas Element already worked nicely in all browsers that supported it at the time, albeit some were a little slow.<br/></p> <p>Now, in 2011, the rendering performance for Canvas has been improved dramatically, audio however is still broken in large parts. I think it is time for a change in tone. Be warned, there's some profanity ahead because HTML5 Audio is still <strong>that fucked up</strong>.</p> <p>Before we start, you may want to play a quick round of <a href="http://playbiolab.com/">Biolab Disaster</a> or <a href="http://www.phoboslab.org/ztype/">Z-Type</a> and have a look at a simple <a href="http://www.phoboslab.org/files/html5audio/">test case</a> to experience the sound issues first hand.</p> <p><a href="http://www.phoboslab.org/log/2011/03/the-state-of-html5-audio">Read complete post &raquo;</a></p> Wed, 09 Mar 2011 16:43:52 +0100 http://www.phoboslab.org/log/2011/03/the-state-of-html5-audio Game On Spotlight: Z-Type http://www.phoboslab.org/log/2011/02/game-on-spotlight-z-type <p>The Mozilla Gaming Blog just published a guest post by yours truly. I write about the creation process, graphics and gameplay aspects of Z-Type, complain about HTML5 Audio and, of course, advertise my JavaScript Game Engine <a href="http://impactjs.com/">Impact</a>.</p> <p>Please enjoy <a href="http://mozillalabs.com/gaming/2011/02/14/z-type/">Game On Spotlight: Z-Type</a>.</p> Mon, 14 Feb 2011 20:35:00 +0100 http://www.phoboslab.org/log/2011/02/game-on-spotlight-z-type Impact Sales Report http://www.phoboslab.org/log/2011/01/impact-sales-report <p>It has been a month now that I launched my <a href="http://impactjs.com/">JavaScript Game Engine</a>. My decision to charge for licenses to use the Engine has been met with a lot of discussions to say the least. People told me I would sell thousands of licenses more, if I had &quot;dual licensed&quot; Impact under the GPL. Some told me I was doing the right thing and putting it under GPL would be a bad idea. Others called me a scammer for charging for something that I had previously advertised as free (which I did not).</p> <p>So, with all this controversy how has it been going? Quite good actually! In the last 31 days I sold about 150 licenses for Impact. After the initial hype died down, I'm now selling 2 or 3 licenses a day.</p> <p>In these 31 days I also made about 700€ from AdSense banners that I put on the <a href="http://playbiolab.com/">Biolab Disaster</a> page and later on the <a href="http://www.phoboslab.org/ztype/">Z-Type</a> page (the first few days after Z-Type hit <a href="http://www.reddit.com/">Reddit's</a> front page it was ad-free).</p> <p>So here's an immensely ugly chart that I put together from Google Analytics, AdSense and my License database:</p> <p><a href="http://www.phoboslab.org/files/images/impact-webstats.png?"><img class="noalign" src="http://www.phoboslab.org/files/images/impact-webstats-scaled.png" alt=""/></a></p> <p>All the payments for Impact went through PayPal. I've read a lot about how PayPal likes to keep your money, closes your account for no reason and a number of other shenanigans. I also had my share of bad experiences with PayPal a few years earlier, when someone purchased a software of mine through a hacked PayPal account. This time however, I haven't had any problems at all.</p> <p>Sure, the PayPal website is still as bad as it ever was. I need about 60 seconds to login in to my account each time. Every single page needs at least 10 seconds to load and I'm forwarded from <code>paypal.com</code> to <code>paypal-deutschland.de</code> and back to <code>paypal.com</code> again. PayPals <em>Instant Payment Notification</em> API is equally horrible, but what can I say – it works and there's no other viable solution here in Germany anyway.</p> <p>On a lighter note: I've sent more emails in the last 31 days than in the 12 month before that combined (236 vs. 124). I got a lot of job offers and even decided to go through a phone interview with Facebook after they approached me (<a href="http://twitter.com/#!/phoboslab/status/24910672471007232">I failed</a>). I also got a lot of &quot;business offers&quot; that I still don't know how to deal with – emails talking about &quot;partnerships&quot; but never with a concrete idea. Maybe I just don't understand marketing.</p> <p>So all in all I'm very happy with the success of <a href="http://impactjs.com">Impact</a> and I will definitely continue development for it.<br/></p> Mon, 24 Jan 2011 03:31:03 +0100 http://www.phoboslab.org/log/2011/01/impact-sales-report Z-Type http://www.phoboslab.org/log/2011/01/z-type <p>I planned for a while now to participate in Mozilla's <a href="https://gaming.mozillalabs.com/">Game On</a> contest. I didn't want to submit <a href="http://playbiolab.com">Biolab Disaster</a> as my entry, but instead make something new and fresh. After all, the deadline for the contest was still months away. When I read <a href="http://twitter.com/#!/mozlabs/status/22375032894267392">this tweet</a> I realized it wasn't anymore.</p> <p>At this point I started to work on a game I wanted to make for a long time. I always was a fan of <em>Crimsonland's</em> (a 2003 arcade game) <em>Type-o-Shooter</em> mode and recently discovered the Dreamcast classic <em>Typing of the Dead</em>. So the overall direction of the game was clear.</p> <p>Today I finished Z-Type. <a href="http://phoboslab.org/ztype/">Please Enjoy!</a></p> <p><a href="http://phoboslab.org/ztype/"><img class="noalign" src="http://www.phoboslab.org/files/images/ztype.png" alt="Z-Type"/></a></p> Mon, 10 Jan 2011 23:04:38 +0100 http://www.phoboslab.org/log/2011/01/z-type