Making Desktop Apps with LUA

I can recount my journey of programming, it started with Basic, then Z80 Assembly, C, dBase variants, Pascal, back to Visual Basic, Java, C++, PHP, Perl, Python, Obj-C, and then Lua. I had to go through so many languages and each of the languages offered some advantages and some disadvantages. It was all fun and games till I moved to Lua, then the fun and games became a party, it was such an amazing experience, that now going back to developing on other languages is not motivating enough. Sometimes there is a need to create a Mobile app, for which one of the better tools is CoronaSDK, but then for the Desktop, there is a void. There is no *lua* based framework that fills in the gap.

This is where this article comes in. I can write a lua program and run it like a shell/batch program but then the issue of providing the source code just does not make me comfortable. There are some other options like using the Wax toolkit, which exposes the entire Objective-C app to lua bindings which can then be scripted. Easy, not much to do than register the Obj-C classes, etc, the issue is the way to call the Obj-C functions., still manageable. Then there is Moai, the issue with this is that it is more powerful than using Cocos2D and from a RAD perspective, this is not for the quick and dirty app creation. This is for the deep rooted programmer, that wants all access to the machine/device.

So how can we leverage the Lua knowledge and also create a desktop application?

There is a wonderful OpenSource framework called LÖVE, this works on all, Windows, Mac OSX and Linux, so creating a desktop app is now possible and it is using pure Lua, which also brings us to the issue of Source code, well, the source code can be compiled into bytecode and then distributed (this is not suggested as it breaks portability) . However for the moment this is the easiest of them all frameworks and fits in the quick app making category. So here's a quick tutorial in making an app with LÖVE.

The structure

Like all lua apps, this one is also housed in main.lua.

The first steps - Hello World

The first and the easiest way is to print to the console, but then that would defeat the purpose of having a GUI environment, so our first Hello World program will display Hello World on the Window.

function love.draw()
    love.graphics.print("Hello World", 400,300)
end

The first thing to note is that love has some standard calls that it makes, which we override to get things done. The love.draw() function is called whenever love feels that the window needs to be repainted/refreshed.

Just like Java or C# or any Object Oriented language, there is a class hierarchy/namespace, so we use the love.graphics.print function to display text on the on-screen window, which takes three paramters, the Text, and x and y co-ordinates. We print "Hello World" at 400, 300 in this window.

Calling/Running the program

This is a rather tricky part, as many beginners will wonder how to get love apps up and running. Love is an application that will display the standard screen or run the app when invoked with the script name, so the easiest way is to create path variable to the love app and thereby one can invoke it from anywhere. One can after setting the path, go to the directory where the main.lua was saved and invoke it using love main.lua There are subtle differences between invoking an app on windows and on a *nix based system like Mac OSX or Linux.

Other basic functions

Like any OO language, there are different stages, like when the object is created, or when the object is destroyed, and then the intermediate stages, when other events occur. Löve offers a few functions which can be overridden to handle the application better. Some of them that one might be interested in are


love.draw Function called to draw every frame on screen
love.focus Function triggered when the focus is lost or gotten
love.keypressed Function called when a key is pressed
love.keyreleased Function called when a key is released
love.load Function called exactly once when the app starts
love.mousepressed Function called when a mouse button is pressed
love.mousereleased Function called when a mouse button is released
love.quit Function called when the app quits/terminates
love.update Function called to update the state every frame


What are the Löve modules (built-in)

Löve has a few modules built-in for interacting with the system and they are

love.audio
love.event
love.filesystem
love.font
love.graphics
love.image
love.keyboard
love.mouse
love.physics
love.thread
love.timer


Threads in Löve are different to the co-routines available in Lua, these are separate instances running in parallel. They work separately than the main thread.

Final words

Löve is quite easy to learn and reference once you start using it. Lua itself is an easy language to use, Love has in a way brought the functionality of Flash type packages to a lua based app, making it easy to develop with.

Next Time

So next time we shall explore into the standard functions and use the love.graphics set of functions to do more than just display text on the screen.

Comments

  1. good atricle i found love2d last year when i was looking for something like corona for the desktop. although i have not used it other then displaying a background image it seems to be good with a lot of examples

    ReplyDelete
  2. great article, wish there was a way to develop apps for webOS using lua (and love2d or corona!)

    ReplyDelete
  3. Anonymous, you must be one of the few lucky millions like myself that got their hands on the HP Touchpads. Yes, wish there was some kind of a GUI lua on it.

    However you know that you can install a Ubuntu variant with the LXDE desktop and run the full fledged Lua on it, which also means that you can run Love2D on it...

    ReplyDelete
  4. OZ Apps:
    That is actually an interesting idea.. Love2D is great (for game-style applications, at least), but this does limit my ability to distribute any app..
    hmmm

    ReplyDelete

Post a Comment

Popular Posts