Access_Denied's Forums

Site Resources => Tutorials => Topic started by: Access_Denied on June 11, 2007, 04:03:52 AM



Title: Tutorial Two: Printing Text
Post by: Access_Denied on June 11, 2007, 04:03:52 AM
Hello, and welcome to the second PSP programming tutorial. In this tutorial, we'll be learning how to create a basic Hello World. So, let's get started. First, go into your Cygwin directory. Go to cygwin/usr/local/pspdev/psp/sdk/include. In that folder, put the file pspcallbacks.h that is attached to this post. After this, go to cygwin/home/(Username)/. In that folder, create another folder called 'projects'. Now go into the projects folder, and create a folder called 'hello'. Now go into that folder and create two files, 'main.c' and 'Makefile'. Now, to do this, you must turn on file extensions. To do this, go in the hello folder. On the toolbar, go to Tools -> Folder Options. Then go to the 'View' tab. Then, uncheck the box that says 'Hide File Extensions for know file types'. Now click OK.

Now, we're finally ready to do some programming. Now, open up the file called 'main.c' in your 'hello' directory with a text editor.

TAKING NOTES
Taking notes is very important. It helps you remember things, and makes it easier for other people to read. So here's two ways to take notes.

// Notes go after the double backslash

/*
Or notes can go between the asterisk and the backslash.
*/

So all the text in orange will not be read by the compiler, so you can write anything you want. So, let's add some notes to our program. Write this in main.c.

//Hello World Program
/*
 *Created on (Date Here)
 *By (Your name here)
 */

STARTING OUT
There are three things that need to go in the beginning of your main.c. Your includes, your defines and the module info. First off, includes are files of code.  So add these three lines at the top, then I'll explain.

#include <pspkernel.h>
#include <pspdebug.h>
#include <pspcallbacks.h>

Now, the words inside the <> are files. If you remember, pspcallbacks.h is the file that you downloaded. By including those files, we are including all the code that they contain. So instead of having to write out all the code in those files, we just include that file. This is especially handy if you don't know how to write the code in the file. Now add this line:

#define printf pspDebugScreenPrintf

A define simply gives something a replacement. If we wrote this:
#define foo Hello
Then everywhere the computer sees the word 'foo', it would replace it with the word 'Hello'. So in our line, everywhere the computer sees 'printf', it will read it as 'pspDebugScreenPrint'. It makes typing easier, so you don't have to write out that big long word, just the short word. Now add this line:

PSP_MODULE_INFO("Hello World",0,1,1);

This, you don't have to worry too much about. Just know, that it has to be there. But you can change the text in the quotes.

MAIN FUNCTION
Every program has a main function. It's where everything happens. Here's how we start our main function.

int main() {

Let's break it down.
int - This just means that our function is of tthe 'integer' type. So at the end of the function, we have to return a number, since and integer is a number.
main - Just signifies our main function, instead of a normal function.
() - These hold the function arguments, but we don't have any. We'll get to arguments later.
{ - This signifies that our function has officially started.

Now, add these two lines:

     pspDebugScreenInit();
     SetupCallbacks();

Now, the first line just initiates the screen for writing. The second line, makes the HOME button work, so we can exit. But the second line only works if you included pspcallbacks.h. Now, add these lines:

     pspDebugScreenClear();
     printf("Hello");

The first line just clears the screen. The second line prints the words "Hello World" to the screen. See how we used 'printf' instead of the longer word? Easier huh? Now, add these lines:

     sceKernelSleepThread();
     return 0;
}

The first line makes our program wait, instead of exiting. Otherwise, the text would appear for a millisecond, then disappear. The second line returns our number, because we made our function and integer type. The third line ends the function. It's the opposite of the '{'.

Now, save that, and open up your 'Makefile' with a text editor. Type this:

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

Now, save it, and exit. Open up Cygwin. Type these lines in, hitting enter after each line:

cd projects
cd hello
make


If everything went right, you should see some text like this:

$ make
psp-strip hello.elf -o hello_strip.elf
pack-pbp EBOOT.PBP PARAM.SFO NULL  \
NULL NULL NULL  \
NULL  hello_strip.elf NULL
rm -f hello_strip.elf


If not, go back and check for errors. You should now have an EBOOT in your 'hello' directory. Also, instead of 'make', if you type 'make kxploit', it will automatically make the two EBOOTs for you. Anyway, now run it. Congrats. You just made your first program. Now you can move on to tutorial number three.


Hosted by www.Geocities.ws

1