Lesson 3
Getting started with programming
Programming skills. A quick tutorial on C language basics on PSP.
After reading the first and second lessons, you should now have a development environment and have compiled your first basic PSP application. Now it's time to write something bigger and better. A "Hello World" program is very good. It greatly enriches our experience by learning it, but it does not do anything. This tutorial describes how to do something.
What you need to know is that this tutorial does not mean all PSP programming. It is not so much a tutorial as "How to Build a game. Here you will get the basic materials for PSP (and c) programming. You must reorganize and integrate these basic materials to build useful programs.
I have read the second lesson, or I know how to establish a basic program, which is necessary for this tutorial. We will not repeat the process of creating a program. Therefore, to test the code below, replace
Printf ("Hello world .");
Because other code is only used to build our programs, we don't need to talk about what they have done.
The final product of this tutorial is such a program: the counter displays an increasing number of values until the user presses a button or the counter reaches the defined upper limit. This is the perfect program for this tutorial. It combines some core elements of C language programming to create a simple but useful program without any special program functions. This tutorial covers variables, if/then statements, loops, text formatting, And button input.
First, we need to include two more header files. The first one allows us to control the screen (a function we will need soon ). The second enables us to obtain the button input. Therefore, to add these two files (pspdisplay. h and pspctrl. h), we need to add the following code to the two "# include" statements in the original code:
# Include <pspdisplay. h>
# Include <pspctrl. h>
All we have to do during program creation is this. Now let's put functional code into it. From this point on, all the listed code should be placed in your main function, replacing the "printf" statement in the second lesson. The first thing we need to do is to name the variables used in the program. The name of a variable is in the following format:
// Sample code
// Do not add it to your program
Type name = value;
Type is the data type of the variable. Each data type can and can only contain one type of data. For example, "int" (an integer) type can store any non-decimal number between-32767 and 32767. "Float" is similar to "int", but it can store decimals. "Char" type can store letters. For our program, we only use the built-in "int" type and the scectrldata type unique to PSP (it is used to store the button status that controls PSP ).
Therefore, for the sake of fame, We need to insert the following two lines of code into the program (they should be in the "printf (" Hello world. ");" the location of this line is under "setupcallbacks)
Int counter = 0;
Int I = 0; scectrldata pad;
Now we have three available variables. The two "int" types, whose names are "counter" and "I", are all zero. Another variable of the "scectrldata" type is "pad". The current value is unpredictable (because we have not initialized it ). This is something you need to remember when writing a program. If you name a variable but do not initialize it (just like our pad variable), it is not a null value. It contains information about the last time in the memory. The famous variable only allocates a certain amount of memory space that can store it. Initialization is to clear it and allow us to use it. We did not initialize "pad" because it does not mean anything. Because it is used to accept button input, You Cannot initialize it by typing a value. We will initialize it before using it.
Now we provide some instructions for users. We use "printf" to output a sentence. So put this line of code after the variable name:
Printf ("Press [x] to start the timer ");
Familiar? By the way. This is the function that we use to output "Hello World" in the second lesson. So if you run the program here, it is like a "hello World" program, but it prints "Press [x] to start the timer" instead of "Hello World ".
Now we need the program to wait until the user presses the [x] button. Now, this seems very difficult and almost impossible to accomplish. Fortunately, we have a perfect thing to deal. This is called a loop. Essentially, it executes a piece of code many times. You can use many types of loops (for loop, while loop, do while loop, etc.), but this tutorial will only introduce two types: "while" loop and "for" loop. Loop works like this: You give a statement (control statement). When it returns true, the loop will repeatedly execute a piece of code. For example, if you have a piece of code that increases variable I (starting from 0) by 1 every time you run it, the control statement you pass to the loop is "I <10 ", it will execute your code 10 times, because the value of I is 10 at 11th times, so the control statement "I <10" returns false. Another important concept you need to understand is that "true" and "false" are synonymous with "1" and "0.
Our cycle is not in a typical format. It can be a typical format, but I have browsed some PSP project code. Most of them are written in this format, so I guess it should be better to write it as the format you will often see. In short, what we need to do is an "Infinite Loop ". But it is not really an infinite loop, because (I didn't mention it above) in addition to the control statement to return false, there is another way to exit the loop. That is the "break" statement. Every time this statement is allowed, it will exit your current loop and the program will continue to run the code after this loop. Therefore, our cycle starts like this:
While (1 ){
As you can see, the value of the control statement is always equal to "true", because "1" is equivalent to "true ". Therefore, the following code (between braces) will be repeatedly executed until a "break" statement is encountered.
Set the "pad" value in the next line. It uses a function defined in the "pspctrl. H" (which we typed in before) file. This is a function call as shown in Lesson 2, but it looks awkward. First, we pass it two values. That's nothing. You just need to use a comma to separate the two values. The second difference is that we pass a variable. This is no big deal. You only need to replace a number, string, or something with the variable name. The third strange thing is that we add a "&" operator before the passed parameter. This is actually the "get address" operator. When you program deeply, you will be familiar with it. Essentially, what it does is to pass the memory address of the variable rather than the value of the variable itself. For now, you only need to use the "&" operator to know the value of the variable that a function can pass. Then, this is how we save the current state of PSP control:
Scectrlreadbufferpositive (& pad, 1 );
If you are confused about what "1" means, don't worry. It sets the number of caches to be read, but I have seen that "1" is used here. To be honest, I don't know why you want to use other values there (if someone has a reason, please remind me that I will add it here ).
The next line is an "if" statement. Here is how you compile the logic into your program. What it does is run a piece of code if the ("if") Statement is equivalent to true. So what we want to do is to "Jump out" ("break") loop when the [x] button is pressed. This will effectively make our program pass the "pause" state and enter the next stage. If the statement is equivalent to false, it will skip this code, and if there is an "else" statement, it will execute that code, if not, it will continue. We do not have the "else" statement, so it will go through the loop again, continue to check the button status, and observe whether the [x] button is pressed. At the same time, we end the loop with a right braces.
If (pad. Buttons & psp_ctrl_cross ){
Break;
}
}
The statement in "If ()" is translated into Chinese as "if the [x] button is pressed ". So if the button is pressed, the loop stops. If the button is not pressed, the loop continues. Now let's look at the counter.
After you press the [x] button, we will open the counter until the user presses the [O] button and stops. So what will we do? You guess, another loop! So we start this loop and add the code to exit (psp_ctrl_circle is replaced with "psp_ctrl_cross ").
While (1 ){
Scectrlreadbufferpositive (& pad, 1 );
If (pad. Buttons & psp_ctrl_circle ){
Break;
}
Now, we need to use a new function. What it does is clear the screen. Because the "Press [x] to start the timer" statement is still displayed on the screen, we need to erase it. In addition, when the next loop runs here and this line of code is executed, we need other text to be displayed on the screen. We need to clear it. So we need to do this:
Pspdebugscreenclear ();
This will erase the content on the screen, so now we are ready to display the timer (and tell the user how to stop the counter and exit the program ).
The first line should look familiar. It is just a simple "printf" statement. What seems a little strange is that the string ends "/N ". "/N" is a special symbol, representing a line break. Therefore, line breaks are equivalent to pressing the "enter" key on the keyboard. The second "printf" statement is somewhat different. We need to print out the "counter" variable. To do this, we need to use the "%" operator. Essentially, this indicates that you want to display a variable, and the characters after "%" Tell the program the type of the variable. Because it is an integer, we use "I" (integer; Understand ?). The second parameter we pass is the variable we want to print out. Here, we want to print the "counter" variable. So Add the following two lines to your program:
Printf ("Press [O] to stop the timer/N ");
Printf ("counter: % I", counter );
Now we need to increase our counter by 1 so that the number displayed on the screen will increase by 1 in the next loop. If we do not do this, it will not be a real counter. We can do this in two ways. First, a less common practice is "counter = counter + 1;" which is perfect, but there is a simpler approach. You can use the "++" operator to add one of the values of a variable:
Counter ++;
It does the same thing as "counter = counter + 1;", but it must be more elegant. Don't you think?
Now we need to insert a short pause to make the "home" key work. Here we use a "for" loop. This loop is only slightly different from the "while" loop. It has three parameters. The first 10 initialization; it sets the variable for the beginning of the loop. The second is the control statement, which is the same as the control statement in the "while" loop. The third is the operation you want to perform on your variables at the end of each loop.
For (I = 0; I <5; I ++ ){
Scedisplaywaitvblankstart ();
}
This will execute our code five times (when I = 0, 1, 2, 3, and 4 ).
Finally, we need to use a simple right brace to end the code snippet for loop running:
}
Now it's only a small difference. This is the code that will run after the "break" statement. We want to display a simple message and the final result of our counter. So we know the screen, and we need to use two "printf" statements, just as we used in the loop, but the text is different. Add the following code:
Pspdebugscreenclear ();
Printf ("counter finished .");
Printf ("final count: % I", counter );
We finally finished our code! Now let's make some superficial changes based on the second lesson.
To reflect our new program, make some modifications to the makefile. Open it and change psp_eboot_title to a word that you think is suitable for your new program. I name it counter program ". And change your target to "count ". Compile your program and run it. You should now have the basic skills to create your own programs (at least text-based skills should be fine ). You can read other C language programming materials and learn how to use more complex logic control (if/If else/else), loops, functions, pointers, and so on. PSP programs may be somewhat different, but the basic things are standard C. Good luck! Have a good time!
Note: original address http://www.scriptscribbler.com/psp/tutorials/lesson03.htm
Because the original text may be updated occasionally, this translation cannot be synchronized with the original text.