Lesson 01
Hello World
The creation of a simple Lua application for the PSP: A walkthrough.
Original Source: Yeldarb's Web Site
This tutorial series focusses on writing programs for the Playstation
Portable (PSP) in Lua. Lua is a general purpose scripting language
allowing for rapid development and easy distribution. It is a
cross-platform language (meaning that you can run your scripts with
minor modifications on PC, PSP, Linux, and any other system that has a
Lua interpreter). It is a bit different than C in that it is a
scripting language. This means that you will not compile your program
as you would if you were writing a C Program. You, rather, just have a
.lua file which really is just a text file with a different extention.
All of the processing goes on through the Lua interpreter (which has
been ported to the PSP by Shine).
So, before you start, you will need to download the latest version of the Lua Player from the official website here.
If you have a 1.50 Firmware PSP, you will need to take the two folders,
'luaplayer' and 'luaplayer%' in the PSP/GAME/ directory on your PSP's
memory stick. With newer firmwares, you will need to use Fanjita's Eboot Loader. If you have a version 1.00 PSP, I trust you know how to
install homebrew (if not, do a quick Google search and I'm sure you'll find what you're after).
Now create a blank text file (you can do this in your favorite text
editor, whether it be Notepad, Wordpad, or whatever else tickles your
fancy) in the 'luaplayer' folder. Rename it to "script.lua" (from
"whatever.txt").
You are now all set up, so on to the tutorial!
This is definately a very easy program to create. It is only about 4
lines of code. They are very easy to understand. And no compiling is
necessary!
This program will just print one line of text that says "Hello World."
It is the classic introductory program ("Hello World" programs have
been written in perhaps every language in existance). The point is not
to create a functional program per se, but rather to introduce you to
the syntax of the language.
So, start your code in "script.lua" out with this line:
blue = Color.new(0, 0, 255)
What this does is define a colour. The particular color that we have
defined, this time, is blue. It creates a variable and stores the
information that will tell the Lua Player what "blue" is. And the Lua
Player will then interpret this line and allocate the appropriate
system resources on the PSP to store it. But, the beauty of Lua is that
we don't have to worry about that. The Player will handle all of the
details.
Let's examine the line of code more closely to see exactly how it is instantiating (which is just a long word for creating) this color object.
"blue = Color.new," is telling the Lua interpreter "Hey, doofus, make a
new color and call it blue." So now when we use the abstract identifier
"blue," we are referencing the tangible information coding for the
color blue in the PSP's memory.
"(0, 0, 255)"
is the most important part of the code for us. This is how we actually
tell the player what color we want our variable, "blue," to be. When we
typed blue = Color.new, we could have typed, 'red', 'yellow', 'green'
or even 'Cublecursome.' But if we had used this same "(0, 0, 255)," it
would have still come out as blue. The interpreter has no idea what
"blue" means, all it knows is what we tell it. We simply used "blue" as
the identifier for simplicity. Obviously, if we had named it "white,"
and it was actually storing the information for the color blue, it
would have gotten confusing down the road in our program (and much more
so if our program was going to be more complex).
So the three numbers, in this case "0" "0" and "255", tell the PSP what
the color displayed will be. These numbers are part of the RGB color
mode. The first number stands for red, the second for green, and the
third for blue.
Here is a picture to explain this concept better:
The values go from 0 to 255. All of the colors your PSP (and your
monitor for that matter) can display are a combnination of these three
colors. For example (as is evident through the above graphic), yellow
can be made by mixing red and green.
Since we want the text to be blue, we will set the third number (or parameter) as high as it will go, 255.
If we had wanted magenta, for example, we would have set both the blue
and red numbers to their highest possible values, so the code would
have been "(255, 0, 255)."
So there's the first line of code broken down and examined for you. Now on to the next one!
We want to display some text, right? So this is the next line of code we need:
screen:print(200, 130, "Hello World!", blue)
The first part of this line is almost self explanatory but, for the
purpose of congruency, I'm going to explain it anyway. "screen:print"
simply tells the PSP to print text to the screen. It's a function that
is built into Lua. It's really as simple as that. Where it says "200,
130" is what tells the PSP where on the screen to display the text. It
is an "x" coordinate and a "y" coordinate. The PSP has a native
resolution of 480x272 pixels (width x height). Our text will be
displayed at the coordinate (200, 130), or 200 pixels from the left of
the screen and 130 pixels from the top.
After that, the next parameter is where we tell it what we want to
print to the screen. Obviously, because this is a "Hello World"
tutorial, we set this string to "Hello World!" You could change this to
say anything you want... for example, "Aklin poopie pants." Finally, we
tell the function the color that we want the text to display as. We
will use our variable "blue" here, which we defined and initialized
earlier.
First two lines, done!
Here's the next one:
screen.flip()
You might be wondering why we need this code or what it does. It may
not seem like anything we need, but ah! We do indeed need it, or else
our program won't work.
The reason for this is that when you print text, it is set to offscreen
buffer, all drawing functions are. So, this means your text won't be
visible on the screen until you type/call "screen.flip()," which
changes it from the offscreen buffer to visible screen buffer. This is
akin to thinking about a sentence before you write it down. When it is
in your head, it is like being stored in the offscreen buffer. It is
only in memory. When you write it down, it then goes onto the paper, or
the screen.
There, now the code is "technically" done. The program will run, the
text will be shown, but not for long. It will only display the text for
a millisecond, and then it will disappear. To avoid this and make the
text stay there, we need to insert the following lines of code:
while true do
screen.waitVblankStart()
end
This will fix the problem of our code quickly vanishing.
How it works is through a coding mechanism called a "loop." The basic syntax of a loop is: "while something do something else end." "something" is usually a condition,
such as "myVariable
is greater than one" or something to that effect, but in our case we
just want our loop to go on forever, so we just say "true." This will
run the lines of code between "do" and "end" forever. The line we have
in there is basically filler. It doesn't really do anything, it just
allows the PSP to take a break. Don't you think your PSP needs a break?
No? Well, do you? Well, that's alright, because you're done!
Save your file and then run it through the Lua Player on your PSP. There you have it, your first Lua script for the PSP.
Be sure to add the feed to your RSS Aggregator (or Google Homepage, or Firefox Live Bookmark) to stay updated with the latest
tutorials.
If you have enjoyed this tutorial and have a spare buck or two, please consider
donating to the author.
If there's a calling, I will consider making more tutorials. Please
contact me with your feedback on the tutorials and on what you'd like
to see in the next lessons. My e-mail is whitehawk45 [at] gmail [dot]
com.
--- Author, Hacking the PSP www.hackingpsp.com
|