<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="4.4.1">Jekyll</generator><link href="/feed.xml" rel="self" type="application/atom+xml" /><link href="/" rel="alternate" type="text/html" /><updated>2026-02-02T18:57:44+00:00</updated><id>/feed.xml</id><title type="html">Cuppadev</title><subtitle>Cuppalicious coding!</subtitle><entry><title type="html">New Beginnings</title><link href="/2026/02/02/new-beginnings.html" rel="alternate" type="text/html" title="New Beginnings" /><published>2026-02-02T00:00:00+00:00</published><updated>2026-02-02T00:00:00+00:00</updated><id>/2026/02/02/new-beginnings</id><content type="html" xml:base="/2026/02/02/new-beginnings.html"><![CDATA[<p>One of the tragedies of writing a blog is eventually you can lose sight of things and eventually dump it. This is increasingly a problem in the modern world of instant gratification on social media.</p>

<p>I’ve decided to resurrect this blog, in the hope that perhaps I can infuse some much needed useful long-form content about what I’ve been up to for the past decade. Hopefully it will be insightful to those who manage to read it!</p>

<p>Honestly, I’m looking forward to the writing some new engaging blog posts!</p>]]></content><author><name></name></author><category term="blog" /><summary type="html"><![CDATA[One of the tragedies of writing a blog is eventually you can lose sight of things and eventually dump it. This is increasingly a problem in the modern world of instant gratification on social media.]]></summary></entry><entry><title type="html">Anim8gif now Open Source</title><link href="/2016/12/20/anim8gif-now-os.html" rel="alternate" type="text/html" title="Anim8gif now Open Source" /><published>2016-12-20T00:00:00+00:00</published><updated>2016-12-20T00:00:00+00:00</updated><id>/2016/12/20/anim8gif-now-os</id><content type="html" xml:base="/2016/12/20/anim8gif-now-os.html"><![CDATA[<p>START CONTENT
One of the first iPhone apps I made was a little useful free tool called anim8gif, a simple little tool which extracted links to all the gifs and pngs on a web page and allowed you to download them, then view them later.</p>

<p>It started development on the 1st gen iPod touch, all the way up to the iPad 3 and iPhone 5s - truly it was an interesting ride in terms of keeping it reasonably up to date.</p>

<p><a href="/wp-content/uploads/2009/03/picture-1.png"><img src="/wp-content/uploads/2009/03/picture-1-209x300.png" alt="anim8gif screenshot" class="alignnone size-thumbnail" /></a></p>

<p>At its most popular, it was getting around 3.75k downloads a month. These days, it’s around 100. Not too bad considering I last updated it in 2014. Still, considering the increasing irrelevance of the app given the prevalence of superior <a href="https://itunes.apple.com/gb/app/vlc-for-mobile/id650377962?mt=8">media players</a> and even <a href="https://itunes.apple.com/us/app/gif-keyboard/id917932200?mt=8">GIF keyboards</a>, it’s presence on the store is questionable.</p>

<p>So I have decided to Open Source it under GPLV3. I hope it might be of use to someone making a gif or apng playback app.</p>

<p><a href="https://github.com/jamesu/anim8gif">Grab it here</a></p>

<p><strong>NOTE</strong>: while the app is currently still available in the App Store, I am planning on removing it within the next couple of weeks so in the future compiling it yourself or downloading it from one of the various jailbreak pirate platforms will be the only way of installing it on new devices.</p>]]></content><author><name></name></author><category term="blog" /><summary type="html"><![CDATA[START CONTENT One of the first iPhone apps I made was a little useful free tool called anim8gif, a simple little tool which extracted links to all the gifs and pngs on a web page and allowed you to download them, then view them later.]]></summary></entry><entry><title type="html">Time for a cleanup</title><link href="/2016/03/12/cleanup.html" rel="alternate" type="text/html" title="Time for a cleanup" /><published>2016-03-12T00:00:00+00:00</published><updated>2016-03-12T00:00:00+00:00</updated><id>/2016/03/12/cleanup</id><content type="html" xml:base="/2016/03/12/cleanup.html"><![CDATA[<p>After realizing I’ve not posted a thing on here for over 2 years, I figured now is about time for an update.</p>

<p>For the past 2 years i’ve mainly been working with <a href="http://www.mode7games.com/">Mode7 Games</a> on their upcomming title <a href="http://www.frozensynapse2.com/">Frozen Synapse 2</a> as well as their previous, <a href="http://www.frozencortex.com/">Frozen Cortex</a>. As previous subject material might suggest, both of these products have involved working with technology built up from <a href="http://www.garagegames.com/products/torque-3d">Torque 3D</a>.</p>

<p>The indie game development scene has certainly been interesting, though quite demanding in terms of technical challenges - even moreso compared to your typical web development backend job. I hope at some point to write up about some of these challenges on this blog.</p>

<p>Old content has now been pruned, due to it being mostly out of date. Old site content pre-2012 can now be found at the <a href="http://vault.cuppadev.co.uk/vault">vault</a>.</p>]]></content><author><name></name></author><category term="blog" /><summary type="html"><![CDATA[After realizing I’ve not posted a thing on here for over 2 years, I figured now is about time for an update.]]></summary></entry><entry><title type="html">Embedding Lua in Torque2D</title><link href="/2014/02/03/embedding-lua-in-torque2d.html" rel="alternate" type="text/html" title="Embedding Lua in Torque2D" /><published>2014-02-03T00:00:00+00:00</published><updated>2014-02-03T00:00:00+00:00</updated><id>/2014/02/03/embedding-lua-in-torque2d</id><content type="html" xml:base="/2014/02/03/embedding-lua-in-torque2d.html"><![CDATA[<p>For the past couple of weeks, I’ve been experimenting with integrating <a href="http://www.lua.org/">lua</a> into <a href="https://github.com/GarageGames/Torque2D">Torque2D</a>. If you’ve used Torque2D before, you may wonder “Why stick lua in if Torque2D already has <a href="https://github.com/GarageGames/Torque2D/wiki/TorqueScript-Overview">TorqueScript</a>?”. There are several reasons, one main one being many times I’ve felt TorqueScript’s lack of advanced language primitives (e.g. lists, tables, closures) to be an annoyance.</p>

<p>Adding simple keywords to TorqueScript is relatively simple since it uses a simple Yacc grammar coupled with an AST. However the implementation of the AST and the design of the stack makes it quite difficult to implement anything more elaborate. Even worse, TorqueScript has no unit tests, so you can easily break something if you are not careful.</p>

<p>So rather than having the burden of maintaining TorqueScript itself, I decided to try integrating another scripting language.</p>

<p>Its important to note that TorqueScript itself is tightly coupled with the object system in Torque, so its worth understanding how it functions before replacing it.</p>

<h3 id="brief-overview-of-torquescript">Brief overview of TorqueScript</h3>

<p>TorqueScript is a fairly simple scripting language backed by a byte-code interpreter. Internally all variables are strings, though they are simultaneously casted to float and integer types to speed up any math operations. All variable access goes through either a local or global hash table (apart from the name of the variable itself, nothing is cache’d).</p>

<p>Local variables are prefixed using the % <a href="http://en.wikipedia.org/wiki/Sigil_(computer_programming)">sigil</a>, and global variables are prefixed using $. Some variables are registered by the engine itself, in which case they directly reflect whatever variable they point to.</p>

<p>TorqueScript’s object system is rather ingenious. Objects are referenced by either a numeric id or a name with optional quotation marks. You can also use more elaborate search strings, e.g.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>1234
"MyObject"
"RootGroup/MyObject"
</code></pre></div></div>

<p>May all refer to the same object.</p>

<p>As far as the scripting engine is concerned, objects have two things: a list of properties, and a namespace. Through the namespace functions are accessed. The class hierarchy is mirrored through the use of linked namespaces.</p>

<p>When accessing properties, it searches for the property in the class property list first, then looks in the objects property list. When accessing functions, it checks the classes current namespace for the function. If the function doesn’t exist in that namespace, it checks the parent namespace and so on  – though internally this check is cache’d using a hash table in the first namespace which contains the complete set of functions.</p>

<p><a href="http://www.cuppadev.co.uk/wp-content/uploads/2014/02/Example-Namespace-Linkage-1.png"><img class="aligncenter size-medium wp-image-263" alt="Example Namespace Linkage (1)" src="http://www.cuppadev.co.uk/wp-content/uploads/2014/02/Example-Namespace-Linkage-1-300x147.png" width="300" height="147" /></a></p>

<p>When you create an object you can also give it a name, in which case it will link in an extra namespace at the top of the chain allowing you to do cool things such as this:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>new SimObject(FudgeCollector);

function FudgeCollector::collectFudge()
{
   echo("Collected fudge");
}

FudgeCollector.collectFudge(); // prints "Collected fudge
</code></pre></div></div>

<p>Namespaces in TorqueScript don’t necessarily have to be linked to a class. For instance you can create a new namespace at any time by defining a function.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>function MyNewNamespace::doThis()
{
   return 123;
}

MyNewNamespace::doThis(); // No problem
</code></pre></div></div>

<p>Internally all objects are derived from a ConsoleObject class which handles fields, though most of the action happens in the SimObject class which handles assigning names and identifiers to objects, and well as setting up namespaces. There is also a SimSet class which adds nesting support.</p>

<p>Safe to say TorqueScript is a fairly loose and flexible language.</p>

<h3 id="on-to-lua">On to lua…</h3>

<p>“So why lua?” you may ask. Several reasons. Firstly out of all the languages I tried, lua seemed to be the most flexible. This meant that it was reasonably easy to implement something which resembled how TorqueScript functions. There is no real class system, so you are free to invent your own <a href="http://lua-users.org/wiki/SimpleLuaClasses">using linked metatables</a>. Variable access can easily be overridden so you can expose globals and object properties pretty easily.</p>

<p>It’s also ridiculously easy to bind lua code in torque using the current TorqueScript binding system. Instead of having to adding a lua thunk function for every function and method, it’s possible to use a single thunk function and use “upvalues” to point to the correct C function. Although ultimately it would be a better idea to use a specialised thunk for each exposed function, the fact that it isn’t strictly required reduces the complexity of integration  substantially while ensuring existing TorqueScript code can still function without the need of a rewrite.</p>

<p>Comparing lua to other embeddable scripting languages with class systems (e.g. ruby, python), they tend to be a bit stricter when it comes to object classes, making things such as the namespace name trick harder (if not, impossible) to implement correctly. i.e. you can’t just create an object on the fly and bind new methods to it, you need to make a proper class or make the functions references to delegates.</p>

<p>Lua does have a few of its own oddities – for example, arrays start at 1, and there is no switch statement. But these are a reasonable trade-off for flexibility.</p>

<h3 id="on-to-the-binding">On to the binding…</h3>

<p>To be honest, it took me a while to get the right implementation which reflects the way things already work in TorqueScript. Since I wanted to keep the existing binding system, I had to write my own bindings from scratch using some of the existing C++ binding libraries as references to how things should work on a basic level.</p>

<p>For my implementation I ended up using <a href="http://www.lua.org/pil/28.1.html">userdata</a> objects bound to class metatables. The userdata objects point directly to the instance of an object. Property access goes through one of two thunk methods (__index and __newindex), while functions are binded through one of 11 thunk methods depending on the return type and whether or not the function is meant to be used with an object. Functions are assigned to the relevant table in lua (e.g. SimObject methods go in the SimObject table, global functions go in Torque). These tables are linked together to reflect the linkage of each namespace in Torque.</p>

<p><a href="http://www.cuppadev.co.uk/wp-content/uploads/2014/02/Lua-Namespace-1.png"><img class="aligncenter size-medium wp-image-264" alt="Lua Namespace (1)" src="http://www.cuppadev.co.uk/wp-content/uploads/2014/02/Lua-Namespace-1-300x157.png" width="300" height="157" /></a></p>

<p>When looking up properties, the __index method first checks the relevant top namespace table in lua. If it doesn’t exist there, it checks the parent table, and so on. If nothing is found in the end, it assumes the key is meant to refer to a property and acts the same way as in TorqueScript when you get a property.</p>

<p>When creating objects, the equivalent lua reference is stored in a global table called InstanceTable. e.g. “InstanceTable[1234]”.</p>

<p>So for example the following lua script…</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>myObject = SimObject()
myObject.customValue = 'candy'

function SimObject:printInfo()
   Torque.echo(self:getId())
   Torque.echo(self.customValue)
end

myObject:printInfo()
</code></pre></div></div>

<p>Will print the id of the object “myObject” and the word “candy” in the console.</p>

<p>In the engine code, everything is abstracted into a LuaScriptEngine class which handles binding classes, registering objects and functions, and also executing code. There’s also an equivalent implementation of the “Con::executef” function for calling back into script. Unlike the TorqueScript executef, this implementation uses a list of ScriptStackValueRef objects which transparently handles pushing pushing values onto the lua stack with the desired type.</p>

<p>Unfortunately one thing I haven’t implemented as of yet is nested objects, where you can create an object within an object and have the script engine automatically add it to that objects group for you. However given that Torque2D uses <a href="https://github.com/GarageGames/Torque2D/wiki/Taml-Guide">TAML</a> to serialise nested objects this feature can be safely ignored.</p>

<h3 id="conclusion">Conclusion</h3>

<div id="attachment_270" style="width: 310px" class="wp-caption aligncenter">
  <a href="http://www.cuppadev.co.uk/wp-content/uploads/2014/02/Screen-Shot-2014-02-03-at-20.17.33.jpg"><img class="size-medium wp-image-270" alt="Screen Shot 2014-02-03 at 20.17.33" src="http://www.cuppadev.co.uk/wp-content/uploads/2014/02/Screen-Shot-2014-02-03-at-20.17.33-300x231.jpg" width="300" height="231" /></a>
  
  <p class="wp-caption-text">
    Looks like lua
  </p>
</div>

<p>So was it worth embedding lua into Torque2D? I’d say yes. At the very least I now know how deep the TorqueScript rabbit hole goes, making it easier to incorporate any of the other alternative embeddable scripting languages should they become a better fit for my needs.</p>]]></content><author><name>jamesu</name></author><category term="blog" /><summary type="html"><![CDATA[For the past couple of weeks, I’ve been experimenting with integrating lua into Torque2D. If you’ve used Torque2D before, you may wonder “Why stick lua in if Torque2D already has TorqueScript?”. There are several reasons, one main one being many times I’ve felt TorqueScript’s lack of advanced language primitives (e.g. lists, tables, closures) to be an annoyance.]]></summary></entry><entry><title type="html">Torque2D Emscripten port sourcecode released</title><link href="/2014/01/23/torque2d-emscripten-port-sourcecode-released.html" rel="alternate" type="text/html" title="Torque2D Emscripten port sourcecode released" /><published>2014-01-23T00:00:00+00:00</published><updated>2014-01-23T00:00:00+00:00</updated><id>/2014/01/23/torque2d-emscripten-port-sourcecode-released</id><content type="html" xml:base="/2014/01/23/torque2d-emscripten-port-sourcecode-released.html"><![CDATA[<p>Last month I <a href="http://www.cuppadev.co.uk/2013/12/torque2d-in-a-web-browser/" target="_blank" rel="nofollow">unveiled a port of Torque2D which runs in web browsers</a> via <a href="https://github.com/kripken/emscripten" target="_blank" rel="nofollow">emscripten</a>. It was developed largely over a period of a couple or weeks with the help of a few emscripten developers along the way.</p>

<p>After the unveil it generated some interest, but not really enough for me to make a product or anything commercial out of it especially given the current trend for game development engine tech: release everything for free.</p>

<p>Considering this, I’ve decided to release the code for the emscripten port under the same lovely MIT license as Torque2D itself. I hope someone can find it useful, perhaps to help promote their Torque2D game. Or who knows, maybe they can release a web-based browser game for it! The way web browsers are heading, I guess anything can happen!</p>]]></content><author><name>jamesu</name></author><category term="blog" /><summary type="html"><![CDATA[Last month I unveiled a port of Torque2D which runs in web browsers via emscripten. It was developed largely over a period of a couple or weeks with the help of a few emscripten developers along the way.]]></summary></entry><entry><title type="html">Scripting Language Shootout</title><link href="/2014/01/22/scripting-language-shootout.html" rel="alternate" type="text/html" title="Scripting Language Shootout" /><published>2014-01-22T00:00:00+00:00</published><updated>2014-01-22T00:00:00+00:00</updated><id>/2014/01/22/scripting-language-shootout</id><content type="html" xml:base="/2014/01/22/scripting-language-shootout.html"><![CDATA[<p>Picture this scenario. You are developing a game engine, but get to an all-important question: “which scripting language do I use?”. The problem is, there are tons of scripting languages out there. Which one do you choose?</p>

<p>Unfortunately, picking a scripting language is not always easy. A lot of the time its a trade-off between performance, support, and something you like. There’s not much sense in picking the fastest scripting language in the world if you hate it after all.</p>

<p>For me, I don’t particularly care about the speed of a language (<a href="http://upvoid.com/devblog/2013/02/choosing-a-scripting-language/">this has been explored before</a>), so long as it’s reasonably fast. After all, if I’m going to implement something CPU intensive, I’m probably going to just end up writing it in C. I’m also open to anything, so long as it meets the following requirements:</p>

<ul>
  <li>Reasonably easy to embed</li>
  <li>Uses an encapsulated state instead of globals</li>
  <li>Has a capable binding API</li>
  <li>Has some sort of OOP system with objects bound with functions</li>
  <li>Is used by a community of people</li>
</ul>

<p>Since I had time to spare, I decided to evaluate a few popular scripting language implementations which have been described as being embeddable and fit the above criteria. Basically I wanted to:</p>

<ol>
  <li>Bind an object to the API</li>
  <li>Pass an existing instance of an object to the API</li>
  <li>Create an object in the scripting language</li>
  <li>Call methods and get/set properties on the object</li>
  <li>Handle errors sanely (i.e. no stack corruption)</li>
</ol>

<p>In addition I didn’t want to rely on any third-party wrappers – instead I went straight to the C API, since otherwise I would have been evaluating the scripting engine + the “helpful” binding library, rather than the scripting engine itself.</p>

<p>I ended up making a test application which binded a test object to each target language and ran a sample script in order to get a feel for how everything worked. The code is on github, so <a href="https://gist.github.com/jamesu/8552408">feel free to check it out</a> if you are wondering what its like to bind a C++ object to lua, mruby, squirrel and angelscript.</p>

<p>I wrote a few notes in the code, but I felt the need to express my thoughts on each language based on my experience in working with both the API and language myself. So behold! My evaluation of each language is as follows…</p>

<h3 id="luahttpwwwluaorg">Lua(<a href="http://www.lua.org">http://www.lua.org</a>)</h3>

<p>Lua is a powerful, fast, lightweight, embeddable scripting language – or so it is claimed. It seems to be the number 1 choice for game developers, <a href="http://en.wikipedia.org/wiki/Category:Lua-scripted_video_games">as this wikipedia article shows</a>.</p>

<p>Scratching the surface, it appears to be a simple functional programming language. However it features some very powerful constructs centred around the concept of a “metatable” which amongst other things can be used to create class-based object systems. The API is well documented, and its fairly easy to find information on how to do just about anything in Lua since its commonly embedded in apps and games.</p>

<p>An often-heard complaint about lua is how it indexes arrays from 1, though in practice I don’t really find this to be much of an annoyance especially considering luas support for iterators. Another issue is how it isn’t very C-like in general, but IMO this isn’t too big of an issue since the syntax is fairly trivial to learn and get used to anyway.</p>

<p>The binding API in lua is a bit confusing as it uses the stack to set fields on objects, which almost feels like reading some assembler code. Once you realize how the stack works though, it’s not too bad.</p>

<p>Binding objects themselves is an open-ended problem, with many solutions. At a bare minimum you need to create a metatable object for the class, and for each instance of the class you need a userdata object bound to the metatable. You can just use a userdata for object instances and implement “__index” and to return methods, <del>but things start to get tricky when you need to call functions as if you just return a C function from “__index” lua won’t provide any reference to the userdata, so you need to pass in whats known as an “upvalue” when returning the function</del>.</p>

<p><strong>UPDATE:</strong> turns out it does provide a reference to the userdata, you just need to use the “:” instead of “.” when calling the function. Basic Lua 101.</p>

<p>One gotcha with the C API to look out for is when <a href="http://www.knightsgame.org.uk/blog/2012/09/03/notes-on-luac-error-handling/">calling methods within methods</a>. If the method you are calling returns an error, lua will pop the C stack all the way down to your first lua_call. This will ignore destructors for any stack variables you still have in scope in-between. To mitigate this, make sure to use lua_pcall instead.</p>

<p>For derived classes you can simply make a new metatable and set its “__metatable” to the parent class metatable, ad infinitum.</p>

<p>Apart from that binding objects to lua is reasonably simple, and if you are having problems there are a ton of binding libraries out there to help. A fairly strong contender.</p>

<h3 id="squirrelhttpsquirrel-langorg">Squirrel(<a href="http://squirrel-lang.org/">http://squirrel-lang.org/</a>)</h3>

<p>Squirrel is a high level imperative, object-oriented programming language, designed to be a light-weight scripting language that fits in the size, memory bandwidth, and real-time requirements of applications like video games – or so the squirrel website claims.</p>

<p>In reality, I like to think of it as the result of what happens when someone takes a lot of the bad things about lua and fixes them to create a rather neat C-like language.</p>

<p>My only big problem with squirrel from a language perspective is its odd use of the “&lt;-” operator to set object slots, though this can be “fixed” by simply changing the bytecode used by the “=” operator.</p>

<p>Similar to lua you use a stack to bind functions and classes which can be confusing. Thankfully though as with lua there are comprehensive API docs, but with one fatal flaw: I couldn’t find any good example for how to create a class bound to a C++ object, so once again I had to guess. I could excuse this a bit in lua since it’s more ambiguous, but in squirrel classes are a first level construct, making it strange how it isn’t really better explained in the documentation.</p>

<p>In general while I found the API documentation to be comprehensive, I didn’t feel it explained how to do things very well, and combined with the relative lack of resources online it made things a little harder to figure out.</p>

<p>If you are fed up with luas syntax and lack of <a href="http://lua-users.org/wiki/SwitchStatement">certain basic constructs</a>, be sure to give squirrel a try.</p>

<h3 id="mrubyhttpsgithubcommrubymruby">mruby(<a href="https://github.com/mruby/mruby">https://github.com/mruby/mruby</a>)</h3>

<p>mruby is a light-weight, embeddable implementation of the Ruby programming language which has been in development for the past couple of years, so its fairly new.</p>

<p>Ruby itself is a very capable dynamic, object-oriented language which to me at least feels like a cross between perl and python. Ruby also has a proper inbuilt class system which IMO sets it on a higher level than lua. Not only that, it even supports composition through the use of modules.</p>

<p>Unfortunately the binding api is a little under-documented. In fact at time of writing I couldn’t find <strong>any</strong> documentation other than blog posts. In order to write a binding I had to essentially guess what you had to do using a combination of code on github and deduction.</p>

<p>However upon reflection binding an actual object is rather simple, with some rather descriptive function names to define both the class and its methods. For object instances, you use userdata instances bound to a data type.</p>

<p>Similar to lua there is also a gotcha when calling methods within methods. If the method you are calling raises an exception, mruby will pop the C stack all the way down to your first mrb_funcall. This will ignore destructors for any stack variables you still have in scope in-between. To mitigate this you need to save the “jmp” variable before the call and restore it afterwards. If you don’t want the exception to propagate down the stack you also need to clear the “exc” variable. If mruby had mrb_funcall_safe, this hack could have been avoided.</p>

<p>Compared to lua &amp; squirrel, ruby is in general a more extensive language. But mruby itself still feels like it’s not quite there yet, so if you are considering implementing ruby in your app you might want to consider embedding the <a href="http://www.ruby-lang.org/en/">reference implementation</a> instead.</p>

<h3 id="angelscripthttpwwwangelcodecomangelscript">Angelscript(<a href="http://www.angelcode.com/angelscript/">http://www.angelcode.com/angelscript/</a>)</h3>

<p>Angelscript is a real curiosity. In essence it answers the question: “What would happen if you made a scripting language out of C++?”.</p>

<p>What results is a strongly typed, inflexible language. When compiling, everything needs to be defined or declared up-front. There are no fancy metaprogramming constructs, nor any late binding of functions. From what I gathered, if you make your whole game in a set of AngelScript files, you need to compile everything together in a single module. It makes me wonder, <a href="http://forums.beyondunreal.com/showthread.php?t=198378">why not just use a reloadable DLL written in C++</a>?</p>

<p>Unlike C++ Angelscript prefers that you define all class and namespace methods within the class and namespace blocks, making it feel more like C#. This is a fairly minor issue, but if you start thinking “maybe I’d like it better if I just wrote everything in C++”, it does not bode well for the scripting language.</p>

<p>However out of all the scripting languages, I’d have to say AngelScript was by far the easiest to embed. You simply define your function prototype and BAM! It figures out how to call it without having to write your own binding function (using its own <a href="http://en.wikipedia.org/wiki/Foreign_function_interface">FFI</a> system). You can also directly expose object fields too which is a nice touch.</p>

<p>If you want something more direct, Angelscript is a safe bet. If you want something more freeform, look towards the other scripting languages.</p>

<h3 id="conclusion">Conclusion</h3>

<p>So which scripting language do you use? Ultimately there is no perfect solution, you just have to go with the one you feel is the best. Failing that, you can always make your own scripting language, which is not at all an unusual thing to do when making a game engine!</p>

<p><span style="line-height: 1.714285714; font-size: 1rem;"> </span></p>

<p> </p>]]></content><author><name>jamesu</name></author><category term="blog" /><summary type="html"><![CDATA[Picture this scenario. You are developing a game engine, but get to an all-important question: “which scripting language do I use?”. The problem is, there are tons of scripting languages out there. Which one do you choose?]]></summary></entry><entry><title type="html">Introducing libDTShape</title><link href="/2013/12/27/introducing-libdtshape.html" rel="alternate" type="text/html" title="Introducing libDTShape" /><published>2013-12-27T00:00:00+00:00</published><updated>2013-12-27T00:00:00+00:00</updated><id>/2013/12/27/introducing-libdtshape</id><content type="html" xml:base="/2013/12/27/introducing-libdtshape.html"><![CDATA[<p>When developing 3d games, the allure of using a pre-built game engine is all too great. The trouble is nowadays you really need a good content pipeline in order to take 3d objects from your modelling tool and render them in your game, which in the past has usually required someone on the development team to write an elaborate tool-specific exporter script or plugin.</p>

<p>Fortunately nowadays with solutions such as <a href="http://en.wikipedia.org/wiki/COLLADA">collada</a> you can now pretty much skip the “write exporter” stage and move the logic to generate runtime model data into the engine itself. However you still need something to handle animation and skinning. There are a few solutions out there, but they either contain <a href="http://www.ogre3d.org/">too much baggage</a> or the <a href="http://gna.org/projects/cal3d/">licensing isn’t so great</a>.</p>

<p>So I decided to try making something myself. My requirements were as follows:</p>

<ul>
  <li>Written in standard C++</li>
  <li>Engine-Agnostic</li>
  <li>Renderer-Agnostic</li>
  <li>Cross-Platform</li>
  <li>Proven codebase</li>
</ul>

<p>Most importantly I wanted to base my solution off of something I knew already worked, and I knew just the thing: the “dts” code from <a href="http://www.garagegames.com/products/torque-3d">Torque3D</a>, which has been in use in some form or another for over a decade.</p>

<p>DTS itself is quite a featured format, being suitable for rendering level geometry with basic culling and fully rigged and skinned character models. In Torque3D there is even a whole content pipeline so you can go straight from collada dae to “compiled” dts shape.</p>

<p>You might be thinking now “Wait a minute, this DTS stuff must be tightly integrated into Torque3D. Won’t I have to include 99% of Torque3D to use this?”</p>

<p>This is where the beauty of libDTShape comes in. libDTShape is an extremely stripped down version of the DTS code from Torque3D. It includes only the essential code which is needed to both load and create a DTS shape, while leaving you to implement the rendering. It lives in its own namespace, so it can work independently of your own code. It is also configurable, allowing you to strip down the code even further (e.g. to remove collada support).</p>

<p>For those who want to just quickly load a shape and see what you can do, don’t worry! libDTShape also includes a basic example OpenGL renderer.</p>

<p><a href="http://www.cuppadev.co.uk/wp-content/uploads/2013/12/libdtshape.jpg"><img class="alignnone size-medium wp-image-231" alt="libdtshape" src="http://www.cuppadev.co.uk/wp-content/uploads/2013/12/libdtshape-300x233.jpg" width="300" height="233" /></a></p>

<p> </p>

<p><span style="line-height: 1.714285714; font-size: 1rem;">Admittedly as it currently stands libDTShape is a bit of an experiment, but still I hope this can be useful to others.</span></p>

<p><span style="line-height: 1.714285714; font-size: 1rem;">libDTShape can be found here: <a href="https://github.com/jamesu/libdtshape">https://github.com/jamesu/libdtshape</a></span></p>

<p> </p>]]></content><author><name>jamesu</name></author><category term="blog" /><summary type="html"><![CDATA[When developing 3d games, the allure of using a pre-built game engine is all too great. The trouble is nowadays you really need a good content pipeline in order to take 3d objects from your modelling tool and render them in your game, which in the past has usually required someone on the development team to write an elaborate tool-specific exporter script or plugin.]]></summary></entry><entry><title type="html">Torque2D… in a web browser?!</title><link href="/2013/12/16/torque2d-in-a-web-browser.html" rel="alternate" type="text/html" title="Torque2D… in a web browser?!" /><published>2013-12-16T00:00:00+00:00</published><updated>2013-12-16T00:00:00+00:00</updated><id>/2013/12/16/torque2d-in-a-web-browser</id><content type="html" xml:base="/2013/12/16/torque2d-in-a-web-browser.html"><![CDATA[<p>For anyone following the indie game development tool market, there’s one thing that just about every popular commercial tool has: the ability to export your game to the web. Being able to run your code in a web browser is an extremely powerful tool, not only from a platform standpoint but also because it eliminates the requirement of having to install anything.</p>

<p>The tools for doing this in the past sadly either required the end-user to install something (if you made your own plugin), or conforming to a strange runtime (flash, unity, java). <a href="http://www.garagegames.com/products/torque-3d">Torque</a> itself at one point went with the “make your own plugin” approach, which to say the least was a disaster since every developer had to go through the hurdle of making a plugin for each major system and ultimately the end-user had to install something. Unless your game is the best thing since Minecraft, this approach will never work.</p>

<p>Thankfully now there are several more promising solutions on the horizon, one of which is <a href="https://github.com/kripken/emscripten">Emscripten</a>. Emscripten is a compiler that takes standard C/C++ code and compiles it to Javascript suitable for running in any modern web browser. In essence as long as you have a modern 3d graphics card and HTML5-capable browser, you should be able to run just about any game code on all major platforms!</p>

<p>Getting to the point, over the past few weeks I’ve been working on a port of the <a href="http://www.garagegames.com/products/torque-2d">Torque2D game engine</a> to Emscripten. After a bit of help from the Emscripten developers, nearly everything works (excluding networking).<span style="line-height: 1.714285714; font-size: 1rem;"><br /> </span></p>

<p><a href="http://www.cuppadev.co.uk/wp-content/uploads/2013/12/Screen-Shot-2013-12-16-at-00.54.30.jpg"><img class="size-medium wp-image-217 aligncenter" alt="Torque2D Emscripten" src="http://www.cuppadev.co.uk/wp-content/uploads/2013/12/Screen-Shot-2013-12-16-at-00.54.30-300x183.jpg" width="300" height="183" /></a></p>

<p>On a time scale, the basic port took a weekend while the bug fixing was spread over the next few weeks. Porting across code to Emscripten was relatively easy since it exposes a pretty standard unix API with SDL, OpenGL, OpenAL, and a few other useful APIs. However the difficulty was multiplied somewhat when I discovered some rather nasty memory corruption bugs.</p>

<p>Firstly there was a problem with the dynamic_cast handler, in which a cast would fail for no apparent reason. It turns out a key map in the input code was a bit too small, causing an overflow which corrupted some memory related to handling the cast. There was also a problem in the scripting engine in which script strings would randomly be corrupted which I ended up hacking around.</p>

<p>Sadly Emscriptens debugging facilities were not up to the task in this case. While there were several facilities to debug memory corruption, none of them picked up on these issues. Unfortunately this meant  I had to go through every major bit of code to eliminate what was causing the problem, which took a considerable amount of time.</p>

<p>Another thing to note is Emscriptens OpenAL implementation was missing a few crucial functions, though these were fairly trivial to implement.</p>

<p>Overall I was quite impressed with Emscripten, though it would be nice if there were more debugging facilities or even some form of GDB frontend so I could more easily step through code.</p>

<p>For your enjoyment, A build of the port can be found here: <a href="http://stuff.cuppadev.co.uk/torque2d-web/">http://stuff.cuppadev.co.uk/torque2d-web/</a></p>

<p> </p>

<p> </p>]]></content><author><name>jamesu</name></author><category term="blog" /><summary type="html"><![CDATA[For anyone following the indie game development tool market, there’s one thing that just about every popular commercial tool has: the ability to export your game to the web. Being able to run your code in a web browser is an extremely powerful tool, not only from a platform standpoint but also because it eliminates the requirement of having to install anything.]]></summary></entry><entry><title type="html">Current Status</title><link href="/2013/12/09/current-status.html" rel="alternate" type="text/html" title="Current Status" /><published>2013-12-09T00:00:00+00:00</published><updated>2013-12-09T00:00:00+00:00</updated><id>/2013/12/09/current-status</id><content type="html" xml:base="/2013/12/09/current-status.html"><![CDATA[<p>For those who stumble across this blog, you might wonder why I haven’t updated this blog for over a year. Truth be told I’ve been busy working on a few projects for <a href="http://www.mode7games.com/">Mode7 Games</a> such as  <a href="http://www.frozenendzone.com/">Frozen Endzone</a>, which took up a considerable amount of time.</p>

<p>Apart from the problem of not being able to find time to update, I also had a problem of not being to update at all. A while ago I migrated the entire site to <a href="http://jekyllrb.com/">Jekyll</a>, a static site generator. This worked fine for a while, but I quickly grew tired of the long delay when regenerating the site when typing up new posts.</p>

<p>I also felt a bit of disconnect needing to type up each post in a text editor and having to guess how the post formatting would turn out. In short it felt like a big chore to post anything. And for me the ability to post things quickly and get instant feedback is a much needed attribute of a blogging system. Thus I have now migrated back to <a href="http://wordpress.org/">WordPress</a>.</p>

<p>I have a few projects I would like to release before the end of the year, so expect me to write about them here. I’m also going to write about what actually happened this year, including what went wrong during the rather disastrous deployment of the Frozen Synapse server rewrite.</p>

<p>Stay Tuned.</p>]]></content><author><name>jamesu</name></author><category term="blog" /><summary type="html"><![CDATA[For those who stumble across this blog, you might wonder why I haven’t updated this blog for over a year. Truth be told I’ve been busy working on a few projects for Mode7 Games such as  Frozen Endzone, which took up a considerable amount of time.]]></summary></entry><entry><title type="html">Optimizing TorqueScript… Again</title><link href="/2012/11/25/optimizing-torquescript-again.html" rel="alternate" type="text/html" title="Optimizing TorqueScript… Again" /><published>2012-11-25T00:00:00+00:00</published><updated>2012-11-25T00:00:00+00:00</updated><id>/2012/11/25/optimizing-torquescript-again</id><content type="html" xml:base="/2012/11/25/optimizing-torquescript-again.html"><![CDATA[<p>For the past few weeks one of my weekend side-projects was to improve TorqueScript, the scripting language behind the <a href="http://garagegames.com/products/torque-3d">Torque3D</a> game engine.</p>

<p>This time I decided to tackle TorqueScript’s type conversion system. This basically handles converting strings such as “0.1” into native values such as 0.1f, and vice versa. Since everything must be converted to and from strings all the time when manipulating object fields and object variables, it’s a great bottleneck for performance if scripting is used too heavily.</p>

<h2 id="a-little-background-on-the-type-system">A Little Background on the Type System…</h2>

<p>In TorqueScript, values can be one of four types:</p>

<ul>
  <li>Strings</li>
  <li>Unsigned Integers</li>
  <li>Floats</li>
  <li>Custom Types</li>
</ul>

<p>The first three are self-explanatory. The fourth is more involved as it is used to expose engine variables and object fields as strings. Getters are implemented using the ConsoleGetType macro which implements a function returning a string based on the value, setters are implemented with the ConsoleSetType macro which implements a function which parses a string and sets the value.</p>

<p>All access to registered engine variables and objects goes through this type system, so any improvements to it have a good potential to improve overall performance. For instance point values in TorqueScript are always manipulated as strings, so any complex vector math incurs the overhead of string conversion. For this reason I also decided to implement value-based arrays.</p>

<h2 id="what-went-wrong">What went wrong</h2>

<p>One big problem with altering something as monolithic as TorqueScript is little changes can cause big problems since the whole implementation is essentially an inter-connected mess. Changed the syntax? Be prepared to debug the AST. Changed the AST? Be prepared to debug the bytecode. Changed the bytecode? Be prepared to debug the interpreter.</p>

<p>For instance, when changing parts of the AST I frequently found that it wouldn’t generate the correct opcodes. The issue was that the AST nodes had been designed assuming the other statement nodes behaved in a specific way, so when I added new variable types to support arrays it completely failed.</p>

<p>In another case simple changes to the interpreter and the AST caused the float stack to become unbalanced, which required painfully tedious step-by-step debugging in order to resolve the issue.</p>

<h2 id="problems-from-the-ground-up">Problems from the ground up</h2>

<p>TorqueScript suffers from a key design issue: stack values are split into three pools: strings, integers and floats. The only generic value type is a string e.g. when calling functions, the compiler has to assume the return value is a string.</p>

<p>There is some code present in the interpreter which skips the conversion if the next opcodes indicate the value will be converted to an integer or float, but this is more of an elaborate hack around a poorly designed system.</p>

<p>Since there is no generic stack value, operator overloading is impossible (thus the need for specific string concatenation operators). Opcodes relating to field access need to be duplicated in order to handle different types. Object references need to be resolved every time a field is accessed or when an object function is invoked.</p>

<p>TorqueScript also lacks tests to determine if it functions correctly or not, which ultimately means everything needs to be manually tested in order to verify it still works correctly… unless you have the time and resources to create a complete testing suite.</p>

<h2 id="to-conclude">To Conclude</h2>

<p>While this project has been interesting, ultimately I’ve decided to shelve it for the time being.</p>

<p>It turns out fixing a scripting language is harder than it first looks. While I managed to implement arrays and improve the way certain types are set, fixing all the bugs caused by the changes turned out to be an even bigger hurdle. In the end, I was stuck trying to resolve some tricky killer bugs in the editor scripts.</p>

<p>In light of my recent experience, I have the uttermost respect for anyone who maintains a scripting language.</p>

<p>For anyone interested, this code is available in <a href="https://github.com/jamesu/Torque3D/tree/consoletyperefactor">my Torque3D fork</a>.</p>]]></content><author><name></name></author><category term="blog" /><summary type="html"><![CDATA[For the past few weeks one of my weekend side-projects was to improve TorqueScript, the scripting language behind the Torque3D game engine.]]></summary></entry></feed>