PSP Programming Tutorial (2)

Source: Internet
Author: User

Lesson 2
Create your first program
An Introduction to installing and executing a simple "Hello World" Application on Sony PSP

After completing the first lesson, you already have a working development environment and can create your applications. What should we do now? Well, the following is the long-awaited part-your first PSP program. This tutorial will introduce the basic knowledge of the C language and the basics of the program you are going to construct.

First, we need to create a directory structure to organize our projects. Open a cygwin bash shell, enter "mkdir projects", and press Enter. This "mkdir" command represents "make directory", which will create a directory or folder under your current operation path. Now we have created a new folder for all the projects, enter the folder, and run the "CD Projects" command. Now we create another directory for this specific project. Enter "mkdir helloworld" and press Enter. Now switch to the new directory through "CD helloworld.

The next step is to open a text editor. What you use is not really important. It can be a notepad or a tablet, no matter what you do. I like to use an editor dedicated to editing C/C ++ source code, because they have syntax highlighting functions (I use Dev-C ++), but to be honest, it doesn't matter if you know how to use it.

Now, create a file named "Main. c" under "helloworld. This will include the source code of our program. For those who do not know what the source code is (I know there will be some), I want to explain that the source code is what we wrote when creating a program. It is written in a way that people can understand. In most languages, the source code needs to be converted into a format that can be understood by a computer (PSP. This is called a compilation language. Both C and C ++ belong to this type of language (this conversion process can be done through the compiler we created in the first lesson ). There are other programming languages that use something called an interpreter to interpret the source code and send out the machine code at the same time. This is called a scripting language (for example, PHP is a scripting language ).

Okay, so we have a new file to place our source code. Now we should start writing. The first part of the program should contain some comments to describe the purpose, Creation Time, and author of the program. Annotations are some code lines that are not compiled into binary files (or code lines that are ignored by the interpreter of the script language ). Annotation is an important part of the code, because when you or someone else comes back to modify your code, you may not remember what these complicated programs did. Therefore, you can leave some notes for yourself. Annotations are indicated by "//" and. When you see "//", it indicates that the rest of the row is comments. "/*" Indicates that the compiler (or interpreter) begins to ignore the code until a "*/" operator is encountered. Comments marked by the "/*" operator can span multiple rows, but comments marked with "//" can only comment out the remaining parts of that row.

So when we start writing our program, we write the following comments at the top, including what it did, when it was created, and who wrote it.
// Hello world-my first app for the PSP

/*
This program was created by (your name here) on (date here)
It is a simple "Hello World" application.
*/
In the next part of the program, we tell the compiler that we will use those header files and inclusion files in the program. Essentially, what the "# include" command does is to copy the code in the file you passed to it to your program. This keeps your program simple and uses advanced code that has been written for you. The include command can contain the header file that comes with the compiler (or the file that you add to the compiler) and the specific header file of a specific project. The method for distinguishing which of the above files is include is to see whether the following files are placed in "<>" or in quotation marks. A file whose size is greater than include is a file in the path contained by the compiler ("include"). The file whose size is enclosed by quotation marks is a file in the same path as the current file. We want to include two files in our program. The first is "pspkernel. H ". This is a file where you should include all simple PSP programs. It contains all the code specifically used for your PSP. If you do not include this file, your program will not be able to run on PSP. The second file we want to include is "pspdebug. H ". This file contains some functions that are useful for debugging programs. Specifically, it includes the functions we use to display text information to the screen. Therefore, add the following code to your program:
# Include <pspkernel. h>
# Include <pspdebug. h>
Next we will tell PSP something about the program. This is actually not that important. Your program can be compiled without it, but it is often a good idea to keep it (if only for forward compatibility ). The first property is the name of the program, but it is never the name that the program will display (we will change the name of the program later ). Other values are other attributes (do not worry), including the main version number and the minor version number. In addition to the name, we only need to keep the default value for all of these items. Therefore, add the following line of code to your program:
Psp_module_info ("Hello World", 0, 1, 1 );
Now we write a function to write information to the screen. This step is optional, but it makes it much easier to write text-based programs. In essence, this line of code changes the PSP built-in function "pspdebugscreenprintf" to a function that is easier to input (with a short name. This function is used to write information to the screen (we will see it soon ). In fact, we need to rename "pspdebugscreenprintf" to "printf ". Therefore, from now on every time we use "printf", the compiler will think that we entered "pspdebugscreenprintf ". This is what we need to do. We define "printf" as "pspdebugscreenprintf", like this:
# Define printf pspdebugscreenprintf
OK, I have a bad message and a good news. The bad news is that the next piece of code is quite complicated. The good news is that you don't need to understand it. Here we will only give a brief explanation of what it does (we will explain the syntax one by one in the future ). Essentially, this Code includes the functions that our program will call. These functions will enable your program to run on PSP and prevent you from worrying that PSP will crash or unexpectedly quit the game. Put this code in 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 will define the "Main" function. Each C and C ++ program requires a main function. This is the starting point of code execution. C (and some C ++) Run in order. This means that the Code follows a straight line path. For example, if you have the following code:
// Do not add these to your program
// It is an example

Int myfunction (){
// Print out 'A'
Return 0;
}
Int main (){
// Print out 'B'
Myfunction ();
// Print out 'C'
Return 0;
}
Note (if you forget that the description is a comment, please refer to the above) to do what it describes. The program will output 'B' and then 'A' and then 'C', because the compiler starts from the main function. It outputs 'B' first, then it sees the call to "myfunction" (a function defined above), and goes there, find the location where 'a' needs to be output and return it (in main), and then output 'C '. All your programs (unless you use advanced C ++ programming) will follow this linear structure. Therefore, the "Main" function is crucial to your program. To define a function, you need to use the following structure: "[return type] [function name] () {". The return type refers to what the function returns to the program. For "Main", the return type is always "int" (representing an integer ). The function name refers to your function name. The "Main" function name is obviously "Main ". Therefore, add the following line to your code to define your function:
Int main (){
Now we need to add two lines of code to create the screen and make some previously defined functions available (you don't need to know how the two lines of code work ). You don't even need to know how those functions work. What's important is to understand how to call functions. It is actually very simple. You only need to write the function name and add parentheses (if there is a parameter, put the parameter in brackets and we will talk about it later ). Each program line should end with a semicolon. This is because the compiler does not see any blank space. You can leave 100 lines blank between two lines of code, and the compiler does not care about it. This is useful because it allows you to write code in the desired and understandable format. You can put some code together through the blank space, or anything else you want to do. However, to end a line of code, you need a semicolon. So Add the following two lines to your program:
Pspdebugscreeninit ();
Setupcallbacks ();
Now it's time to write code that shows the actual results. Do you still remember to define "pspdebugscreenprintf" as "printf? Well, it's time to use it. We call the "printf" function with a parameter to print text information to the screen. A parameter is a variable that you pass to a function for use. These will be useful sooner or later when you write your own functions. Therefore, to make "printf" output to the screen, we need to pass it a string. We can pass the "Hello World" string to the function to output it. "Printf" is a very powerful function, because you can use it to output other variables to the screen. We can pass them as other parameters, but it is not necessary to talk about them first. For now, we just output "Hello World" to the screen, like this:
Printf ("Hello World ");
Here, you tell the "printf" function to output text to the screen. Now I only need to do something more, and our source code is ready for building. We need to pause the program to see the output result. Otherwise, it will not respond at all, or directly return to the PSP menu. You may never see your beautiful phrase output to the screen, because it is erased very quickly. So add the following line of code to pause the program until "home" is pressed and the user is returned to the PSP operating system.
Scekernelsleepthread ();
Now we need to return a value to the function, because when we define it ("int main ()"), we tell the compiler that it will return an integer. Therefore, you only need to return a '0' (this is a zero, not an uppercase 'O'). Do this:
Return 0;
Finally, end the function with a bracket:
}
The program is written in this way! Now we only need to create a makefile to tell the compiler how to compile the project. Therefore, create a file named "makefile" without an extension name (such as. txt ). Open it in your text editor.

Write the following content to makefile:
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 this makefile as the basis for all your simple projects. Sometimes you need to add a library file or other files to this file, but now it is quite simple. Essentially, it only tells the compiler to find the "Main. c" file and build it into a. pbp file that can be executed by PSP using the pspsdk. If you use this makefile for other projects, you need to change it to "psp_eboot_title = Hello World. You can change "Hello World" to the title of your program. This name will be displayed when you select it on the PSP game menu.

Now open a cygwin bash shell and "cd" to go to your "projects/helloworld" directory. After "make" is input, shell will output something for you. It will tell you whether your source code has errors that make it unable to be compiled (errors ). Generally, if it gives you some warnings, there is no big problem. You should pay attention to errors. Warnings are only the places that may cause bugs. NOTE: If your PSP firmware is of version 1.5, you can enter "make kxploit" instead of "make" to automatically generate two folders.

If you have no errors, congratulations! You have successfully created and compiled your first PSP application. I bet you're eager to try it now. So put "C:/cygwin/home/yourusername/projects/helloworld/eboot. pbp" in your PSP, just like installing other self-made programs. Try it yourself!

To learn more about PSP programming, such as if/then statements, variables, loops, and button input, please refer to Lesson 3.

Note: original address http://www.scriptscribbler.com/psp/tutorials/lesson02.htm

Because the original text may be updated occasionally, this translation cannot be synchronized with the original text.

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.