Lesson 03
A Programming Primer
A crash course in the basics of C programming for the PSP.
After reading Lesson 01 and Lesson 02,
you should now have a development environment set up, and have compiled your first basic program for the PSP. Now it's time to move on to
bigger and better things. A "hello world" application is fine and dandy, a great learning experience, but it doesn't do anything.
That's what this tutorial is about. How to make your programs do things.
What you need to understand is that this tutorial is not meant to be the be all and end all of PSP programming. It is much less
a "how to create a game" than it is a foot in the door. What you will gain here are the basic building blocks of PSP (and C)
programming. You will need to rearrange and add to these blocks together to create your own, functional application.
Having read Lesson 02, or knowing how to set up a basic program is a prerequisite for this tutorial. We will not go through the process of setting up your program in this tutorial. So, to test the code that follows replace the line
The final product of this tutorial will be a program that counts upwards until the user presses a button or until the counter
hits a predefined limit. This is the perfect application for this tutorial; it combines several core elements of C programming
to create a simple, yet useful result without requiring many program-specific functions. Covered in this tutorial are:
variables, if/then statements, loops, text formatting, and button input.
First off, we will need to add two more header includes. The first one is to give us a little more control over the screen (we need this for one of the functions we will be using later). The second file we will be including allows us to get button input. So to do add these two files (pspdisplay.h and pspctrl.h), we need to add these two lines right below our first two "#include" statements:
So, to declare the two variables that we are going to use, we insert the following two lines of code into our program (these should go where your "printf("Hello World.");" line was in Lesson 02; right after the line "SetupCallbacks();."
Now we're going to give the user some instructions. The way we do this is by using "printf" to output a statement to them. So put this after the variable declarations:
Now we need the program to wait until the user presses the [X] button before it does anything else. Now, this could be a very difficult, nearly impossible thing to do. Fortunately, we have something perfect for dealing with it. It's called a loop. Basically what it does is run through a block of code multiple times. There are different types of loops available to you (for loops, while loops, do while loops, etc), but this tutorial will only introduce two of those loops, the "while" loop and the "for" loop. How this loop works is that you give it a statement, and it will execute a block of code while that statement is true. For example, if you had a block of code that incremented a variable (starting with a value of zero) by one each time the loop was run, and the statement that you passed in the loop was "i<10" it would execute your code ten times, because on the eleventh time that it checked the value of the variable, it would be 10, which is not less than 10, so the statement would be false (man, this is a run on sentence if I've ever seen one). One other important concept that you will need to grasp is that "true" and "false" are synonymous with "1" and "0" respectively.
Phew. It's not so hard is it? I'm glad you agree (like how I just assume the answer to my rhetorical questions because you can't answer?). So let's move on to the second part of this tutorial.
Having read Lesson 02, or knowing how to set up a basic program is a prerequisite for this tutorial. We will not go through the process of setting up your program in this tutorial. So, to test the code that follows replace the line
printf("Hello World.");
from Lesson 02 except where otherwise noted. Since the other lines were just setup for our program,
we need not go through what they do again.First off, we will need to add two more header includes. The first one is to give us a little more control over the screen (we need this for one of the functions we will be using later). The second file we will be including allows us to get button input. So to do add these two files (pspdisplay.h and pspctrl.h), we need to add these two lines right below our first two "#include" statements:
#include <pspdisplay.h>
#include <pspctrl.h>
And that's all the extra setup we need. Now we'll move on to putting in the functional code. From here on, all code listed should go
in your main function, replacing the "printf" statement from Lesson 02. The first thing we will do is declare
the variables that we will use in our program. A variable declaration takes the following form:
#include <pspctrl.h>
//EXAMPLE (Psuedo Code)
//DO NOT ADD TO YOUR PROGRAM
type name=value;
The type is the data type of variable. Each data type can contain data (and only that data). For example, type "int" (meaning integer)
can hold any non-decimal number from -32767 to 32767. A "float" is like an "int," but it can hold decimals. The "char" type can hold a
letter. For our program, we will only use the built in type "int" and a PSP specific type called "SceCtrlData" which holds the button
state of the PSP controls.//DO NOT ADD TO YOUR PROGRAM
type name=value;
So, to declare the two variables that we are going to use, we insert the following two lines of code into our program (these should go where your "printf("Hello World.");" line was in Lesson 02; right after the line "SetupCallbacks();."
int counter = 0;
int i = 0;
SceCtrlData pad;
So now we have three variables that we can use. Two of type "int," named "counter" and "i," containing a value of zero for now. And
one of type "SceCtrlData," named "pad," which is filled with junk at the moment (since we haven't yet initialized it). This is something
to keep in mind when writing your own programs. If you declare a variable without initializing it (like our pad variable), it will not
just be empty. It will contain the information that was previously in that chunk of memory. Declaring the variable just allocates a certain
amount of memory space for the variable to be stored in. The initialization is what clears it out and allows us to use it. The reason
that we haven't initialized "pad" at the same time that we declared it is because it wouldn't really make sense. Since it holds button
input, you can't just initialize it by typing in a value. We'll initialize it later, before we use it for anything.int i = 0;
SceCtrlData pad;
Now we're going to give the user some instructions. The way we do this is by using "printf" to output a statement to them. So put this after the variable declarations:
printf("Press [X] To Start the Timer");
Look familiar? Good. It's the same function we used to output "Hello World" in Lesson 02. So if you ran the
program at this point, it would be just like the "hello world" application, except it would print out "Press [X] To Start the Timer" instead
of "Hello World."Now we need the program to wait until the user presses the [X] button before it does anything else. Now, this could be a very difficult, nearly impossible thing to do. Fortunately, we have something perfect for dealing with it. It's called a loop. Basically what it does is run through a block of code multiple times. There are different types of loops available to you (for loops, while loops, do while loops, etc), but this tutorial will only introduce two of those loops, the "while" loop and the "for" loop. How this loop works is that you give it a statement, and it will execute a block of code while that statement is true. For example, if you had a block of code that incremented a variable (starting with a value of zero) by one each time the loop was run, and the statement that you passed in the loop was "i<10" it would execute your code ten times, because on the eleventh time that it checked the value of the variable, it would be 10, which is not less than 10, so the statement would be false (man, this is a run on sentence if I've ever seen one). One other important concept that you will need to grasp is that "true" and "false" are synonymous with "1" and "0" respectively.
Phew. It's not so hard is it? I'm glad you agree (like how I just assume the answer to my rhetorical questions because you can't answer?). So let's move on to the second part of this tutorial.
