Lesson 02
Setting up the Development Environment
The setup, creation, and execution or a "Hello World"
Alright, here we are in the second part of Lesson 02. If you somehow got here without reading the first
half you will definitely want to go back now or you will be very confused.
Ok, I have some bad news and some good news. The bad news, the next block of code is pretty complicated. The good news, you don't need to understand it. Here's a brief explanation of what it does (we'll leave the actual syntax and line-by-line explanation until later). Basically what this block of code contains is functions that will be called in our program. The functions will set up your program to run on the PSP and allow you to not worry about your PSP freezing or exiting the game when you don't want it to. Put this block into your program:
Ok, I have some bad news and some good news. The bad news, the next block of code is pretty complicated. The good news, you don't need to understand it. Here's a brief explanation of what it does (we'll leave the actual syntax and line-by-line explanation until later). Basically what this block of code contains is functions that will be called in our program. The functions will set up your program to run on the PSP and allow you to not worry about your PSP freezing or exiting the game when you don't want it to. Put this block into 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 are going to define the "main" function. Each program in C and C++ needs a main function. This is where the code
is executed from. C (and some of C++) functions in the proceedural paradigm. This means that the code goes in a linear path.
For example, if you have the following code:
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;
}
//Do not put this in your program.
//It is an example.
int myFunction() {
//Print Out 'A'
return 0;
}
int main() {
//Print Out 'B'
myFunction();
//Print Out 'C'
return 0;
}
Where the comments (see above if you don't remember what comments are) do what they say. The program would print out 'B' then
it would print out 'A' then 'C' because the compiler starts in the main function. It prints out 'B' then it sees the call
to "myFunction" which is defined above it, and goes there, sees that it needs to print out 'A' and then returns to where it left off
and prints out 'C.' All of your programs (until you get to advanced C++) will follow this linear structure. So, the "main" function
is vital to your program. To define a function, you use the following structure: "[return type] [function name]() {". The return
type is what the function will send back to the program. For "main" this should always be of type "int" (which stands for integer).
The function name is your name for the function, "main" will obviously be named "main." So, define your function by putting the
following line in your code:
//It is an example.
int myFunction() {
//Print Out 'A'
return 0;
}
int main() {
//Print Out 'B'
myFunction();
//Print Out 'C'
return 0;
}
int main() {
Time for another deep breath. You're doing well. The third (and final) part of the tutorial is where we
will put in the meat of our program. So when you're ready, proceed.
