Advertising (This ad goes away for registered users. You can Login or Register)

Programming on PSP - Lua Dev Tutorial

All the Help you need is here
User avatar
Joel16
Posts: 914
Joined: Wed Oct 12, 2011 8:47 pm

Programming on PSP - Lua Dev Tutorial

Post by Joel16 » Mon Mar 10, 2014 8:28 pm

Okay so this is for wololo’s tutorial contest. I haven’t seen any LUA tutorials for PSP on wololo, so I thought I’d make one myself. If you need any questions feel free to PM me, or comment below, I’ll try to be as detailed as possible. Take note with tiny modifications, you can learn to program on different Lua interpreters using their documentation.

Thanks to DeViaNTe for LuaDev.

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.
- Text editor ( I recommend notepad++, which can be downloaded here)
- Time, patience and a brain :P

Hello World – Just a simple text displaying program

So you’ve downloaded the interpreter and the documentation, and you’re ready to start programming on lua!

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

Code: Select all

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:

Code: Select all

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”. :lol:

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:

Code: Select all

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:

Code: Select all

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
[spoiler]Image[/spoiler]

That’s all for now, I’ll write up how to load, images, sounds and fonts later on.
Advertising
Last edited by Joel16 on Thu Mar 27, 2014 3:31 am, edited 4 times in total.
"Forever in darkness, a guardian devil."

User avatar
Joel16
Posts: 914
Joined: Wed Oct 12, 2011 8:47 pm

Re: Programming on PSP - Lua Dev Tutorial

Post by Joel16 » Mon Mar 10, 2014 8:32 pm

Loading and displaying images

Loading an Image

So now you've learned the basics of printing text, using a certain color on the screen. Now we'll learn how to use images. Before displaying an image we must load it. To load an image, we set a variable to it. (A variable is basically a value that we define) for example in the tutorial above, red was our variable. It defined what color is. Anyways let’s get back to coding.
To load an image in LuaDev, we set a variable and call this function. Take note the quotations are required.

Code: Select all

image.load("location/filename.format")  -- common ‘I’ is used in LuaDev
Location - To load an image, you must set up a location. If the location is in the same folder where the lua scipt is located, you can simply use the filename. Example if you want to load an image from a folder called “Images”, this is how you’d do it.

Code: Select all

image.load("Images/filename.format")
Filename – This is the name of the image you are using. For example

Code: Select all

image.load("background.format")
Format – LuaDev can load jpeg/png and gif images. These are the file formats.
This is how you load an image.

Code: Select all

bg = image.load("Images/background.png")
bg if our variable.


Displaying an Image

After loading our image we need to display it. To display our image in Luadev, you can do it in two ways.
1. By calling the variable itself:

Code: Select all

variable:blit(x,y) – I already stated above what the ‘x’ and ‘y’ means. Look into it for reference 

2. By calling “image.blit”

Code: Select all

image.blit(x,y,variable)
This is how display an image.

Code: Select all

bg:blit(0,0)
Or
image.blit(0,0,bg)
This is what our code should look like altogether.

Code: Select all

bg = image.load("Images/background.png")

while true do
image.blit(0,0,bg)
screen.waitvblankstart()
end
or

Code: Select all

bg = image.load("Images/background.png")

while true do
bg:blit(0,0)
screen.waitvblankstart()
end
Both carry out the same operation.
Advertising
Last edited by Joel16 on Wed Mar 12, 2014 1:33 pm, edited 3 times in total.
"Forever in darkness, a guardian devil."

User avatar
Joel16
Posts: 914
Joined: Wed Oct 12, 2011 8:47 pm

Re: Programming on PSP - Lua Dev Tutorial

Post by Joel16 » Tue Mar 11, 2014 12:54 am

Using Sounds

Okay now that we know how to use images, the steps are basically the same. First you load your sound file, and then you play it whenever it’s needed. There are functions that allow you to loop the sound file (to keep it playing continuously) and pause the sound file.


Loading a sound file

LuaDev only loads MP3, AT3, BGM and WAV files. So keep that in mind. To load a sound, you must set a variable to the sound file. For example:

Code: Select all

bgmusic = sound.load("Sounds/music.wav")
The “sound.load” operation locates the sound file called “music.wav” in the “Sounds” folder and loads it into our program.


Playing a sound file.

Now that we’ve loaded our sound file, we can play it whenever we need to. To do this we use this function:

Code: Select all

 sound.play(bgmusic)
The “sound.play” operation plays the sound that we loaded earlier, which was defined as a variable, “bgmusic”


Pausing a sound file

To pause a sound file, we use this function:

Code: Select all

sound.pause(bgmusic,-1)
The number “-1” indicates that the pause/play function is automatic. If not paused it pauses, if paused it resumes.
You can use “0” to resume the sound if paused and “1” to pause the sound if playing. (make sure you use a comma after indicating the variable)


Stopping a sound file

To stop a sound file we call this function.

Code: Select all

sound.stop(bgmusic)
This is pretty self-explanatory. I don’t think I need to get into detail with that.


To keep playing a sound file constantly – a loop.

To use this we call this function “sound.loop()”
For example:

Code: Select all

sound.loop(bgmusic)
This will keep playing the sound file continuously until the loop is stopped. To stop the loop we use this function.

Code: Select all

sound.loop()
Or you can free the sound file, using this function – This also frees more memory, it is recommended that you used this function.

Code: Select all

sound.free(bgmusic)
Last edited by Joel16 on Sun Mar 16, 2014 6:40 pm, edited 1 time in total.
"Forever in darkness, a guardian devil."

User avatar
Joel16
Posts: 914
Joined: Wed Oct 12, 2011 8:47 pm

Re: Programming on PSP - Lua Dev Tutorial

Post by Joel16 » Wed Mar 12, 2014 2:37 am

Updated tutorial for images. See second post :)
"Forever in darkness, a guardian devil."

User avatar
Joel16
Posts: 914
Joined: Wed Oct 12, 2011 8:47 pm

Re: Programming on PSP - Lua Dev Tutorial

Post by Joel16 » Sun Mar 16, 2014 6:41 pm

Updates third post: Using sound files.
"Forever in darkness, a guardian devil."

User avatar
alevisawesom
Posts: 106
Joined: Sun Mar 16, 2014 5:34 pm

Re: Programming on PSP - Lua Dev Tutorial

Post by alevisawesom » Thu Mar 20, 2014 9:51 pm

Wow! This is a really good tutorial! You're probably going to win the contest :lol:. I'm saying that, even though I'm against you... so yeah. Nice job!
Please refer to me as arcticsn0w, not alevisawesom.

PSP 2001 - 6.61 ME-2.3 (main)
PS2 Slim - FMCB
iPod touch 5 - 8.1.1 jailbroken
iPad 2 - 8.1.1 jailbroken
OG Pebble - 2.8.1 PebbleBits
Android Tab - 4.2.2 rooted
Wii - 4.3, HBC
2010 MBP - OS X 10.10

User avatar
Joel16
Posts: 914
Joined: Wed Oct 12, 2011 8:47 pm

Re: Programming on PSP - Lua Dev Tutorial

Post by Joel16 » Thu Mar 20, 2014 10:28 pm

alevisawesom wrote:Wow! This is a really good tutorial! You're probably going to win the contest :lol:. I'm saying that, even though I'm against you... so yeah. Nice job!
Haha thanks mate :) I've put quite a lot of effort in explaining every bit of detail lol :lol: your tutorial is pretty good too. I wouldn't be surprised if you won :) You've added quite a lot of detail!
"Forever in darkness, a guardian devil."

User avatar
DS_Marine
Developer
Posts: 276
Joined: Wed Oct 10, 2012 1:32 pm
Location: Argentina
Contact:

Re: Programming on PSP - Lua Dev Tutorial

Post by DS_Marine » Mon Mar 24, 2014 12:54 am

Nice tut, but many newbies will hate you, because the first example doesn't work and most people won't figure out why.
Point 4. the double quotes in there will generate an error in lua. Please edit and put the correct double quotes. (also 3. and 3.5)
ImagePSP-Controlled drone
"Hackers don't have superpowers. Just a hackable PSP and a brain (ships by default on most humans models)" - A famous guy
Image

User avatar
qwikrazor87
Guru
Posts: 2874
Joined: Sat Apr 21, 2012 1:23 pm
Location: The North Pole

Re: Programming on PSP - Lua Dev Tutorial

Post by qwikrazor87 » Mon Mar 24, 2014 12:58 am

DS_Marine wrote:Nice tut, but many newbies will hate you, because the first example doesn't work and most people won't figure out why.
Point 4. the double quotes in there will generate an error in lua. Please edit and put the correct double quotes. (also 3. and 3.5)
You talking about this?

Code: Select all

screen.print(240,136,”Hello World”,0.6,red,red)
It'll work with single/double quotes and double brackets, [[text]].
PSP 2001 - TA-085 - 6.61 PRO-C2
PS Vita 3G - PCH-1101 - 3.65 HENkaku Ensō
Alcatel phone - Android 8.1.0
Laptop - Toshiba Satellite L305D-S5974 - Ubuntu 16.04 LTS

User avatar
Joel16
Posts: 914
Joined: Wed Oct 12, 2011 8:47 pm

Re: Programming on PSP - Lua Dev Tutorial

Post by Joel16 » Mon Mar 24, 2014 1:18 am

DS_Marine wrote:Nice tut, but many newbies will hate you, because the first example doesn't work and most people won't figure out why.
Point 4. the double quotes in there will generate an error in lua. Please edit and put the correct double quotes. (also 3. and 3.5)
Oh I see now. It's because I typed out all of that on MS word. I've edited them and added in the proper double quotes. Funny cause I did run it on my psp, and it did work as you can see on the screenshot provided in the first post.
"Forever in darkness, a guardian devil."

Post Reply

Return to “Tutorials”