## Tuesday, April 8, 2014

### Programming on PSP - Lua Dev Tutorial#1 - The Basics

These would be a series of tutorial for all those who want to learn luadev on PSP. All these tutorials are written by Joel16 so all credits goes to him.

Introduction:

First thing’s first, we need:
- LUA Dev’s interpreter (eboot that runs the lua scripts, “script.lua”). Download here
- Documentation (a text file or a website that lists all the known functions compatible with the interpreter) no worries the download above comes with it.
- Time, patience and a brain

Hello World – Just a simple text displaying program

1. Open up notepad++ or whatever text editor you have, and create a new file.

2. Let’s start off with this line of code

```red = color.new(255,0,0)
```

What this does, is that it defines a color ‘red’ using RGB format. The first number defines R, the second or middle number defines G and the third or last number defines B. (R = red, G = green, B = blue). Have a look here for more colors  http://www.rapidtables.com/web/color/RGB_Color.htm

3. Now that we’ve defined our color, we need to print text on the screen. So therefore we use this function:

```screen.print(x,y,"Text",size,color,color)
```

The ‘color,color’ is used in LuaDev only when you’ve set a certain size. Take note the size isn’t required. I’m only including it to make things neat.

Okay so in LuaDev, “screen.print” is a function used for displaying text on a screen. Whenever you want text to be displayed on a screen you must use this function.

The “x” after the braket defines the x axis of the PSP screen. We all know the PSP screen’s resolution is 480x272. 480, is the width and 272 is the height (in pixels) of the screen.
So the “x” represents the X axis (anywhere from 0 to 480), and the “y” represents the Y axis. (anywhere from 0 to 272)

Now the “Text” bit represents the characters that are needed to be displayed on the screen, once the program is executed. It can be anything like “Hey there, name’s Joel” or “I love mum”.

The size.. Do I really explain this? Haha this obviously determines the size of the text. Take note these sizes aren’t like Microsoft office documents, they’re in decimal points. That is for example “0.6” is normal. Anything over one would appear quite large, well atleast that’s how it is in LuaDev.

The color, color – The first color determines the color of the text. The second determines the color of the shadow/highlight. This ‘second color’ (“,color)” is only called when you’ve set a certain size to your text. Although this isn’t necessary, we’re only going to be using it to make our program user friendly.

So now this is what it should look like:

```screen.print(240,136,"Hello World",0.6,red,red)
```

This is basically all you need to know to run a simple hello world program. To use fonts, it is required that you set a variable called font, and used the function ‘font.load’ to load a specific font. Take note that LuaDev uses PGF fonts. The download above includes the software that convers TTF fonts to PGF. Anyways I won’t be getting into detail with this until later on.

4. This is what the whole code should look like:

```red = color.new(255,0,0)

screen.print(240,136,"Hello World",0.6,red,red)
screen.flip()

while true do
screen.waitvblankstart()
end
```

I know I haven’t defined a few things, so here’s what they mean.

screen.flip()- This function is called because when you print text, it is set to offscreen buffer, all drawing functions are. So, this means your text won't be visible until you type/call
screen.flip, which changes it to offscreen buffer to visible screen buffer.

while true do
screen.waitvblankstart() – What this does is that it loop the code in an endless loop while it is true (which it is), so now the text will stay displayed for a longer period of time (forever to be precise).

end - this is where we end our code.

Now on your text editor, save this file as “script.lua” without quotations. (If you’re using notepad, where you see the bit that says “.txt” select that box, and select “All files” then save the file as script.lua”
Now create a folder, it can be called anything. I’ll call mine HelloWorld. Now in this folder, place the Eboot,pbp(lua Dev interpreter) that was included in the archive I mentioned earlier and the lua file; script.lua. Now place this folder in your PSP/Game folder, and run the program in game section.

This is what the code should look like

That’s all for now, I’ll write up how to load, images, sounds and fonts later on.

Written By Joel16 & Edited by Ahmed Aziz