Build for Mobiles and Desktop from the same source

Build for all devices from the same source...? what?? Before you go to check the system clock and see if today is the 1st of April, let me assure you that it is not and this is not a joke.

Marketing, or rather the corporate marketers are like spiders, they build the webs of deceit around the unsuspecting consumer, but then that is their job and if they can lie very well, they have done their job well. I am not a marketeer and so I shall tell you no lies. When I say that you can build for multiple platforms using the same source, I mean that.

It started out with a rather well known lua based gaming framework touting that they can port some samples from other languages in a few lines of code, the point that was missing was that there was a framework that encapsulated all of the other stuff that needed to be done to get that working. The size of a blank app created using that framework is over 8 MB, where it is of concern to some developers, it is of less concern to others as they are happy about the tradeoffs. After all there was a time in the past when good developers made used of bit flags for binary, true/false type fields and then there were the wasteful ones that used a byte to represent the same with a character Y/N. We have all survived that and now that we have gigabytes of memory, how does a few bytes matter? it didn't till the mobile devices brought in constraints. I had a client that literally asked me, I have a 32GB iPhone, why can it only use 200MB of the memory before it starts to fail/give memory warnings... well

Many of you that have been professional developers, might have read about Design Patterns, classes and other related stuff for those that haven't, in simple words, if working on Windows or a Mac, if you want to open a file, there is a fileOpenDialog that you can invoke from your app, it will display the file open dialog and then return the filename you chose. Would it make sense to write that from scratch for your code? So came up the need for API's and Libraries or code snippets. Plop in the code to do blah and you are done.

So what is a framework? For those that were older developers, you may recollect OWL, MFC, ATL, et for the newer ones, you may recognise the dotNet framework, Swing Library, frameworks are a layer in between the users program and the machine code that allows the user to access functionality easier. So rather than write 20 lines of Assembly, you can write one line of high level code that the framework will translate into the 20 lines of assembly (hopefully).

Then there are the Components/Libraries that can be said to be equivalent of ActiveX Controls for the Windows platform or widgets for the others. These can be dropped into a project and set with altering a few parameters and the component does rest of the work.

So now that we have understood what a framework, component and library is, let us look at how it all works together, for this example, we take a lua based framework, what happens behind the scenes is that it is wrapped into C/C++ code to provide the bindings. So to the user, it is totally transparent and looks only like lua, while in the background the lua code is interpreted and run with C/C++ code to provide the experience you want. so when you type in the one line of code

local myimage = image("theimagename.png")

though this is one line of code, there might be several lines of code that are run to load and display that image, that differentiates the High Level and Low Level API's. The Low level API's provide the user with full control on customisation and the the high level ones abstract everything and provide little adaptability. In some case the High level ones are fine, as there might not be much to customise and in some cases they become the road blocks.

Let us revisit that fictitious line of code in lua

if we were to use Gideros, that line would look like

local myimage = Bitmap.new(Texture.new("theimagename.png"))
 stage:addChild(myimage)

if we were to use Moai, it would look like
MOAISim.openWindow ( "test", 320, 480 )

viewport = MOAIViewport.new ()
viewport:setSize ( 320, 480 )
viewport:setScale ( 320, -480 )

layer = MOAILayer2D.new ()
layer:setViewport ( viewport )
MOAISim.pushRenderPass ( layer )

gfxQuad = MOAIGfxQuad2D.new ()
gfxQuad:setTexture ( "theimagename.png" )
gfxQuad:setRect ( -64, -64, 64, 64 )
gfxQuad:setUVRect ( 0, 0, 1, 1 )

prop = MOAIProp2D.new ()
prop:setDeck ( gfxQuad )
layer:insertProp ( prop )

Now you might almost gasp at the size of code used for Moai, but then you have to realise that this is the lowest level API that one can have, it is almost the lua version of C++

in CoronaSDK you would use
local myimage = display.newImage("myimage.png")

so, in comparison there is only one line of code used in CoronaSDK, and a couple of lines for Moai, but recollect our discussion on frameworks, libraries, etc? We can create a library that can encapsulate all of the extra lines of code and work with just one line of code.

So let's say we want to make a generic library that will load images and place them on screen as
local myimage = image("myimagename.png")

we can create a function called image, that takes a parameter called myfilename and loads the relevant image

so in Gideros, it would look like
local function image(theFilename)
  local _image = Bitmap.new(Texture.new(theFilename))
  stage:addChild(_image)
  return _image
 end

in Moai, it would look like
local function image(theFilename)
  local gfxQuad = MOAIGfxQuad2D.new()
  gfxQuad:setTexture(theFilename)
  gfxQuad:setUVRect(0,0,1,1)
 
  local prop = MOAIProp2D.new()
  prop:setDeck(gfxQuad)
  layer:insertProp(prop)

  return prop
 end

the reason why the initialization code is removed is that you wouldn't create and set the window every time you load an image. So we are assuming that the window is already created and there is a layer variable that will accept the bitmaps/textures/images to be added to it.

for CoronaSDK
local function image(theFilename)
  return display.newImage(theFilename)
 end

As you can see, we have created the same function image(theFilename) in all the three apps, so if you were building your apps/games, you can have a library/framework that can help port your code over to any framework of your choice as you will be working with a generic common code/API language than understanding the nuances for every option available.

I have not forgotten that the article is about multi-platform development using the same source. So if you see, having a framework offers the flexibility to have portable source code. It works pretty much in the same manner when dealing with multiple platforms, the code remains the same but the underlying machine code changes based on the platform, so it can be ARM based, x86, x64, RISC, and others.

At this point of time, it is supposedly the most difficult to master as it reminds many of the complexities of C++ / Obj-C and a whole lot of stuff that they should ideally not deal with. It is the raw power, it needs to be packaged into a nice shiny box and then it will be appealing to users. The point remains that the base code for Moai, apart from being Open Source and Free, can compile for Windows, Mac OSX, *nix, iOS and Android. So it does offer true cross platform development capabilities. The only restricting factor for many users is the low level API's. The best part of this is that you can compile the Desktop version of Moai and run your lua project against that, that will spawn and run the app natively. no need for a simulator and most of all, you can set the size of the window as per your needs. If all is fine, just compile the mobile project with this lua project and you have a mobile app. So you can test on a Windows Box, a Mac Box, even a *nix box.

Well, Every Rose has it's thorn so all this cool stuff comes with the catch that it is low level API, there are a couple of developers that re working on an intermediate wrapper to provide one liners, so as to say.

The other point being that the next time you see the Marketing Glitz talk about a framework that uses only one line of code to achieve things, remember that so can you and not by using their framework, but using any language, even Obj-C, it is all about creating the framework right.

So, take a visit to Moai, take it as a rite of passage, are you a developer enough to evaluate it? If you get it, you have cross platform and raw awesome power at your fingertips, no more asking for features, you can add them yourself. However, I must add that it is a work in progress, which means that with every release the creators are adding features and functionality to make things manageable.


Comments

Popular Posts