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.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.