PSP Programming Guide 2

Source: Internet
Author: User
Tags comments mkdir printf
After you have learned the first lesson, you have set up a compilation environment to write PSP programs. Maybe you can't wait to start your first

PSP program. This tutorial will provide you with some basic knowledge of C language and lay the groundwork for your programming path.
We first set up a directory to store the program project we are writing. Open the Cygwin bash shell and type "mkdir

Projects ", enter. (mkdir, this command is used to build folders or directories, and friends who are familiar with Linux and Unix will not

Unfamiliar), now, type "CD projects" to enter the projects directory. Now we're going to set up another directory to store it right away.

"HelloWorld", all programming tutorials almost all "HelloWorld" as the first beginner program, we are no exception.

In the Projects directory, type "mkdir HelloWorld" carriage return, then type "CD HelloWorld" to enter HelloWorld

Recorded.
Open a text editor that can be Notepad, WordPad, and whatever you like with it. I like to use support C + +

Syntax highlighting editor, to tell the truth, it doesn't matter if it's true. Now, create a name in the HelloWorld directory

Files that are "main.c". We're going to write the code snippet in this file. Notice that the exciting work is about to begin ...
The first part of the program should be some comment sections that tell people what our program is used for, when it's written, who writes

Of Comments are not compiled and executed by the compiler, but it is important that if you re-edit your code later, comments can help you

A quick recap of the code, all you can write some notes in notes. Comments are marked with the "//" and "/*" symbols. Once you look

See "//" to indicate that future text is a comment. The content between "/*" and "* *" is also commented, ignored by the compiler, and

Content can span lines, but comments marked with "//" are only one line.
Let's start our program by telling someone in the comments what the program is, who wrote it, when it was written.
Hello World-my First App for the PSP

/*
This program is created by (Your Name here) on (Date here)
It's a simple "Hello world" application.
*/
Next, we will tell the compiler in the program that we need those header files and introduce the files in our program. Actually "#include"

The role is to copy the code you introduced into your program, so that you can use the already written code and keep your program

Simple The #include can reference a file provided by the compiler or you can reference a header file that you have created yourself. xxxxx Me

Two header files will be introduced in our program. The first is "pspkernel.h", each PSP program will introduce this file, it is a package

Contains all the code prepared for the PSP. If you do not introduce this header file, your program will not be able to run on the PSP. A second

The file to be introduced is "Pspdebug.h", which contains some of the functions required by the debugger to display the text on the screen.

The function is contained therein, and it is exactly what we need. So, in your program, write down:
#include <pspkernel.h>
#include <pspdebug.h>
Now, we're going to tell the PSP a little bit about our program. It is not very important, it is ignored when the program compiles, but the

It is really useful to write in a program (if only for forward compatibility). The first property is the name of the program, and the remaining attributes include

The largest version, the smallest version, let's not take care of them, let them remain the default values. OK, then add the following in your program

Code:
Sp_module_info ("Hello World", 0, 1, 1);
We're going to create a function to write on the screen. The following step is optional, but it can make your programming work change

Easily, the PSP built-in function "pspdebugscreenprintf" to write to the screen is renamed "printf", and later we

Every time we use "printf", the compiler treats it as "pspdebugscreenprintf". Rename the code as follows:
#define PRINTF pspdebugscreenprintf
OK, I have a good news and a bad news right now. The bad news is that the following code snippet is really complicated. The good news is that you

There is no need to understand it. Let's briefly explain what it is used for (it will tell you the exact syntax and line-by-row explanation), below

Code snippet contains a function that we will call in the program, which allows your program to run on the PSP without worrying about your

The PSP will crash and will quit the game when you don't want it. Add the following code snippet to your program:
/* Exit CALLBACK */
int exit_callback (int arg1, int arg2, void *common) {
Scekernelexitgame ();
return 0;
}

/* Callback Thread */
int Callbackthread (scesize args, void *argp) {
int cbid;

Cbid = Scekernelcreatecallback ("Exit Callback", Exit_callback, NULL);
Scekernelregisterexitcallback (Cbid);

SCEKERNELSLEEPTHREADCB ();

return 0;
}

/* Sets up the callback thread and returns its thread ID */
int setupcallbacks (void) {
int thid = 0;

Thid = Scekernelcreatethread ("Update_thread", Callbackthread, 0x11, 0xfa0, 0,

0);
if (thid >= 0) {
Scekernelstartthread (thid, 0, 0);
}

return thid;
}

Next, we want to define the main function, each C or C + + function requires a main function, which is the "main" function, which is a

The execution entrance of the program. As an example:
Do not put the this in your program.
It's an example.

int MyFunction () {
Print out ' A '
return 0;
}
int main () {
Print out ' B '
MyFunction ();
Print out ' C '
return 0;
}
The result of this program execution is B a C, because the program starts with the main function. When it outputs B, it is called

MyFunction (), this function is defined above, after the mufunction () function outputs A, the program goes back to the main function and continues to

Line, Output C. All C programs follow this linear sequence. The main function is critical to your program. Definition (gen

Build) a function to follow the format: "[return value type] [function name] (parameter) {code snippet}". The return value type refers to the function return

The type of data that is returned to the program. The main function return value is generally integer (int). Function name as the name implies, the main function is named

is main. Add the following code to your program:
int main () {
Now we need to add two lines of code, do some work on the screen and invoke the previously defined function (so far, I

They don't know how it works). Although you don't need to know how those functions work, you have to master the method of calling the function.

Very simple, the function name + parenthesis is ok (if you need to pass parameters, the parameters are written in parentheses, we will

). Each line of the program must end with ";" because the compiler does not see spaces, even if you have 100 blank lines in the middle of two rows

, the compiler ignores it. So you can write the code in the format you want, and you can write the code in one line (I'm not

Yes, or you can add it anywhere you want to join a blank area. But you have to have semicolons at the end of each line of statements.
The two lines of code are as follows:
Pspdebugscreeninit ();
Setupcallbacks ();
Now it's time to write some code to get us excited (is it because the tutorial is too boring) and why is it exciting? Because

The results will be seen in a minute. Remember, we used to define "printf" instead of "pspdebugscreenprintf", and we immediately

will see how wise this decision is. How do we make the text appear on the screen, which requires invoking the "printf" function and entering a

A parameter. A parameter is a variable passed to the function execution. When you write your own function, it will come in handy sooner or later. We need to

to function "printf" output character "HelloWorld" to the screen, so we pass "HelloWorld" as a parameter to the letter

Number. "printf" is a very useful function, and we can also use it to output other variables to the screen. Now, we'll

Output "HelloWorld":
printf ("Hello World");
So far, you've told the "printf" function to Output "HelloWorld" to the screen, and then we need to do some cleanup work.

And then our program can be build. We need to pause the program, or we won't see the output on the screen. If

Do not pause, you see the result is not freeze is back to the PSP directory, the output of the results flashed so that you can't see

The beautiful text appears on the screen. Add the following line to pause the output until you press the "Home" button or go back to the PSP

For the system.
Scekernelsleepthread ();
We also need a return value for our function, preceded by the definition of "int main ()", and all the return values must be a

Integral type. Return "0" is OK, it represents normal exit, note oh, not "O"
return 0;
Finally, adding a brace, the function ends.
}
For the execution of the program, we also need to write a makefile to tell the compiler how to execute our project and create a file "
Makefile ", note that there are no suffix names. Open it with a text editor, and then type:
TARGET = Hello
OBJS = MAIN.O

CFLAGS =-o2-g0-wall
Cxxflags = $ (CFLAGS)-fno-exceptions-fno-rtti
Asflags = $ (CFLAGS)

Extra_targets = Eboot. PBP
Psp_eboot_title = Hello World

pspsdk=$ (Shell Psp-config--pspsdk-path)
Include $ (PSPSDK)/lib/build.mak

You can use the makefile created above as a template for any small project, and sometimes you may need to add some libraries. It tells

The compiler used PSPSDK to compile the mian.c into a. pbp file that the PSP can recognize. If you need to use this in a future project,

Makefile file you need to put "Psp_eboot_title = Hello world." "Hello World" in the title of your program.

, this topic is the name of the game directory that will be displayed on the PSP later.
Now, open the Cygwin Bash Shell, type "CD Projects/helloworld", and then "make" to enter, you will see the screen

The details of make are shown on the screen, and if not compiled successfully, it will tell you what caused the error. If your program compiles

It's okay to encounter some errors. Mistakes are something you need to watch out for, and there may be some mistakes that make you feel like crazy.
If there are no errors during the compilation process, congratulations. You have successfully created and compiled your first PSP app

Software. I bet you're eager to experiment on the PSP, so put "c:/cygwin/home/your name

Word/projects/helloworld/eboot. PBP "Copy to the PSP and try it yourself.
Lesson 03 will allow you to learn more about PSP programming knowledge, such as if/then statements, variables, loops and keystrokes.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.