The Basics Part 1: Difference between revisions

From PSP Developer wiki
Jump to navigation Jump to search
(Created page with "Lesson 02<br> The Basics<br> Lua Programming Techniques<br> After having read Lua Lesson 01, you should have a basic concept of how to run Lua programs and how to setup a simp...")
 
(Inserted links similar to the original ones on PSP-Programming.com (for easier use with the example files))
 
Line 2: Line 2:
The Basics<br>
The Basics<br>
Lua Programming Techniques<br>
Lua Programming Techniques<br>
After having read Lua Lesson 01, you should have a basic concept of how to run Lua programs and how to setup a simple script. You are now ready to move on to some more advanced concepts that will prepare you to create fully featured applications and games.<br>
After having read [[Hello World Part 1|Lua Lesson 01]], you should have a basic concept of how to run Lua programs and how to setup a simple script. You are now ready to move on to some more advanced concepts that will prepare you to create fully featured applications and games.<br>




Line 11: Line 11:




If you haven't read the first tutorial, I highly recommend it. If you decide to go on anyway, without reading it, and become baffled, and in the process having your mind transformed in to a gooey puddle of radioactive mush, don't come complaining to me. (Aside: actually it's not really that difficult, I just thought I'd add a little drama to the tutorial).<br>


If you haven't read the [[Hello World Part 1|first tutorial]], I highly recommend it. If you decide to go on anyway, without reading it, and become baffled, and in the process having your mind transformed in to a gooey puddle of radioactive mush, don't come complaining to me. (Aside: actually it's not really that difficult, I just thought I'd add a little drama to the tutorial).<br>


Before you start, you will need to download a zip file containing the images used in this tutorial. (The preceding phrase was a link, for those of you who skimmed right over it).<br>
 
 
Before you start, you will need to download a [http://web.archive.org/web/20080508051830/http://www.psp-programming.com/tutorials/lua/lesson02.zip zip file containing the images used in this tutorial]. (The preceding phrase was a link, for those of you who skimmed right over it).<br>




Line 24: Line 26:


Now the USB port on the PSP will be activated when you run your script.<br>
Now the USB port on the PSP will be activated when you run your script.<br>




Line 32: Line 35:
gray = Color.new(153, 153, 153)
gray = Color.new(153, 153, 153)
black = Color.new(0 , 0 , 0)
black = Color.new(0 , 0 , 0)
If you have forgotten how these lines work, go back to the last lesson and refresh your memory.<br>
If you have forgotten how these lines work, go back to [[Hello World Part 1|the last lesson]] and refresh your memory.<br>




Now we want to display a loading screen. Our loading screen will say "Loading: 0%" then "Loading: 1%" and so on and so forth until "Loading: 100%." To start it off, we need to print "Loading: 0%" to the screen. You should remember the screen:print function from Lua Lesson 01. But in case you don't, here's the code that you will need to insert:
 
Now we want to display a loading screen. Our loading screen will say "Loading: 0%" then "Loading: 1%" and so on and so forth until "Loading: 100%." To start it off, we need to print "Loading: 0%" to the screen. You should remember the screen:print function from [[Hello World Part 2|Lua Lesson 01]]. But in case you don't, here's the code that you will need to insert:
screen:print(194, 136, "Loading: 0%", pink)
screen:print(194, 136, "Loading: 0%", pink)
screen.flip()
screen.flip()
Line 41: Line 45:




Next, we need to load our images (which, if you missed it before, can be downloaded here). Extract the zip file into the folder where your script file is on your PSP. Then add this line. It will load the background.
 
Next, we need to load our images (which, if you missed it before, can be downloaded [http://web.archive.org/web/20080508051830/http://www.psp-programming.com/tutorials/lua/lesson02.zip here]). Extract the zip file into the folder where your script file is on your PSP. Then add this line. It will load the background.
background = Image.load("images/background.jpg")
background = Image.load("images/background.jpg")
Before we move on, let's get a firm understanding of what this line of code actually does. The first thing we see is a variable name, background. We then set that variable equal to the result of the function on the right ("Image.load()") using the assignment operator, or equal sign. The actual loading of the image comes through the "Image.load()" function which is built into Lua. It takes one parameter, the path to the image file we want to load. Our image is in the "images" folder, and the picture we want to load is "background.jpg." Remember, you must always use the file's extension when loading it (in this case, it was a jpeg file, so we used the extension ".jpg").<nowiki>Insert non-formatted text here</nowiki>
Before we move on, let's get a firm understanding of what this line of code actually does. The first thing we see is a variable name, background. We then set that variable equal to the result of the function on the right ("Image.load()") using the assignment operator, or equal sign. The actual loading of the image comes through the "Image.load()" function which is built into Lua. It takes one parameter, the path to the image file we want to load. Our image is in the "images" folder, and the picture we want to load is "background.jpg." Remember, you must always use the file's extension when loading it (in this case, it was a jpeg file, so we used the extension ".jpg").<nowiki>Insert non-formatted text here</nowiki>


When you understand, move on to part two of this tutorial to continue your program.
When you understand, move on to [[The Basics Part 2|part two]] of this tutorial to continue your program.

Latest revision as of 03:54, 27 February 2024

Lesson 02
The Basics
Lua Programming Techniques
After having read Lua Lesson 01, you should have a basic concept of how to run Lua programs and how to setup a simple script. You are now ready to move on to some more advanced concepts that will prepare you to create fully featured applications and games.


In this tutorial, you will learn to: use if statements, display a background, load and display images, get button input, take screenshots, and make a loading screen, along with a bunch of other neat concepts.


So, basically this tutorial could be called "how to make a souped up, useless, graphical Lua script." But, alas, it's not. It's called "The Basics," because that's what this souped up, useless, graphical Lua script will attempt to teach you.


If you haven't read the first tutorial, I highly recommend it. If you decide to go on anyway, without reading it, and become baffled, and in the process having your mind transformed in to a gooey puddle of radioactive mush, don't come complaining to me. (Aside: actually it's not really that difficult, I just thought I'd add a little drama to the tutorial).


Before you start, you will need to download a zip file containing the images used in this tutorial. (The preceding phrase was a link, for those of you who skimmed right over it).


Now that we have all that introductory BS (baloney sandwich) out of the way, on to the tutorial!


For firmware 1.50 users, there's a nifty little trick that you can use (sorry 2.00+ people, it requires kernel mode which isn't yet available to you) to speed up your development cycle. By adding the following line into the top of your code, you can activate USB Disk Mode, which allows you to edit your script without having to exit out of it and go back through the menu. To enable this, create a new lua file and use this as the first line: System.usbDiskModeActivate()

Now the USB port on the PSP will be activated when you run your script.


Next we are going to define our colors like we did in the first tutorial. We will be using pink, blue, orange, gray, and black. Logically, we will name our variables accordingly. So, to instantiate these color objects, we will enter the following lines of code: pink = Color.new(255, 0 , 153) blue = Color.new(0 , 153, 255) orange = Color.new(255, 102, 0) gray = Color.new(153, 153, 153) black = Color.new(0 , 0 , 0) If you have forgotten how these lines work, go back to the last lesson and refresh your memory.


Now we want to display a loading screen. Our loading screen will say "Loading: 0%" then "Loading: 1%" and so on and so forth until "Loading: 100%." To start it off, we need to print "Loading: 0%" to the screen. You should remember the screen:print function from Lua Lesson 01. But in case you don't, here's the code that you will need to insert: screen:print(194, 136, "Loading: 0%", pink) screen.flip() As you should recall, "screen:print()" will store our text in an off-screen` buffer, and then "screen.flip()" draws that buffer to the screen.


Next, we need to load our images (which, if you missed it before, can be downloaded here). Extract the zip file into the folder where your script file is on your PSP. Then add this line. It will load the background. background = Image.load("images/background.jpg") Before we move on, let's get a firm understanding of what this line of code actually does. The first thing we see is a variable name, background. We then set that variable equal to the result of the function on the right ("Image.load()") using the assignment operator, or equal sign. The actual loading of the image comes through the "Image.load()" function which is built into Lua. It takes one parameter, the path to the image file we want to load. Our image is in the "images" folder, and the picture we want to load is "background.jpg." Remember, you must always use the file's extension when loading it (in this case, it was a jpeg file, so we used the extension ".jpg").Insert non-formatted text here

When you understand, move on to part two of this tutorial to continue your program.