Results 1 to 13 of 13
[TUT] Beginner C Programming Tutorials
This is a discussion on [TUT] Beginner C Programming Tutorials within the Guides: PSP Development forums, part of the PSP FAQs, Guides, and Tutorials category; Well, it's been a long time since I wrote these tutorials. The site is long gone and some of the ...
-
09-22-2009, 10:14 AM #1I'm Baaaack!

- Join Date
- May 2006
- Location
- Nowhere
- Posts
- 2,186
- QJ Pts
- 17,067
- Level
- 83
- Downloads
- 0
- Uploads
- 0
[TUT] Beginner C Programming Tutorials
Well, it's been a long time since I wrote these tutorials. The site is long gone and some of the tutorials went missing. I don't have the will to look for them. Anyway, I'll be posting the tutorials in this thread after this post.
You'll notice that throughout the tutorials I link to files that don't exist. Well, here they are:
graphics.c/.h
http://www.psp-programming.com/tutorials/c/lesson04.zip
pspcallbacks.h
MEGAUPLOAD - The leading online storage and file delivery service
Also, you'll see in a few tutorial the references to images that don't exist. Just make your own. The background image can be any 480x272 image and the player image can be any 32x32 image. (.PNG of course)
Anyway, hope these tutorials are helpful.
Here's the links to the posts, for easier browsing:
Tutorial 1 - Hello World
Tutorial 2 - Variables
Tutorial 3 - Button Input
Tutorial 4 - Images
Tutorial 5 - Player Movement
Tutorial 6 - Loops and Arrays
Tutorial 7 - Functions
Tutorial 8 - Copying Files
Tutorial 9 - If, Else-If and Switch-Case Statements
Tutorial 10 - Menus
Tutorial 11 - OSLib Images
Tutorial 12 - OSLib Menus

-
09-22-2009, 10:15 AM #2I'm Baaaack!

- Join Date
- May 2006
- Location
- Nowhere
- Posts
- 2,186
- QJ Pts
- 17,067
- Level
- 83
- Downloads
- 0
- Uploads
- 0
Tutorial 1 - Hello World
First off, a little about the tutorials. Only code that is blue will go into your code. NOTHING ELSE. For a text editor, I HIGHLY suggest Notepad++. (Google it.) And finally, download this file. Now, dive into the tutorial.
Welcome to my first tutorial.This is a C programming tutorial for the PSP. In this tutorial, I'll be focusing on printing text. The reason why I didn't do a Cygwin setup tutorial, is because there's already so many. Anyway, we'll be compiling our first program. First off, I want to get something out of the way. Download the attachment on this page. (pspcallbacks.h) Place that file inside X:/cygwin/usr/local/pspdev/psp/sdk/include/. What we just did will save many lines of code in the future. So, go into the ~ directory. Now, create a folder called 'helloworld'. (Or whatever you want, just remember the name.) Now, inside that folder, create 2 files. 'main.c' and 'Makefile' Note that main.c has a small m, and Makefile has no extension. Now, we're ready. Grab your text editor and open those files up. Now, let's start coding.
First in our code are comments. Comments are lines of code that ignored as code. Basically, they're there for you and you only. The compiler will NOT read this code, so it can contain anything. Example:
//Comments can go after two slashes, until the end of the line
/*
Or comments can take as many lines
as you want, as long as they're between
the star and asterik.
*/
They may not seem usefull, but if you have a variable named 'aldfsk', and you come back to your code six months later, you'll never know what that variable is for. But if you put a comment before it, you will remember. See, easy, huh? So, add this next code.
Code://Hello World Program /* Created by (Name) On (Date) */
Next in our code are usually the include files. Include files contain code that is pre-written for us, so we don't have to do so much work. Remember pspcallbacks.h? Well, that's a file we're going to include, so we don't have to put all the code in our main file. There's three files we'll include in this lesson. Two are files for the PSP to use functions that we need for our program. The other is pspcallbacks.h. The code in that file will allow us to use the HOME button in our prgoram. So, put in this code.
Code:#include <pspkernel.h> #include <pspdebug.h> #include <pspcallbacks.h>
After includes, we usually put our defines. Say you had a variable named 'thisisareallylongvariabl ename'. Well, who the hell wants to type that our every time you use it? No one, that's who. So, you can do this.
#define short thisisareallylongvariable name
So, instead of the long name, you can just write 'short' and the compiler will read it the same way. Or you could do this:
#define billion 1000000000
So, intead of writing the number, just write billion. Same thing. So, throw in this code.
Code:#define printf pspDebugScreenPrintf
pspDebugScreenPrintf is the function we'll use to print text. It's long, so we shortened it. Some people wouldn't suggest shortening things, but screw them. One more thing before we start coding. Our module info. This is a PSP specific thing. I'm not sure exactly what it does, but I know you need it. So, add this.
Code:PSP_MODULE_INFO("Name Of Program",0,1,1);
Now, let's start coding. First, add this, then I'll explain.
Code:int main() {
What we just did is start our main function. A function is something that performs a task for us. So, if our function was supposed to print text, whenever we call the function, it'll print text. Anyway, more on that later. Our main function is the very first function that the PSP does, so this is where the main stuff happens. Now, 'main' is the name of our fucntion. Fucntions always have parentheses after them, but we'll get to that later. For now, just know that they have to be there. And, the curly bracket '{' marks the start of the function. But, what about the 'int' you say? Well, int stands for integer. And integer is a number. So, the type of function is an integer function. So, at the very end, we have to return a number. Sounds difficult, but we'll get to it later. For now, add this.
Code:pspDebugScreenInit(); pspDebugScreenClear(); SetupCallbacks();
Now, what we just did was call three functions. Those functions are going to do what they're supposed to do. The first will initiate the screen for printing. The second will clear the screen. And the third, comes from pspcallbacks.h, so it's going to allow us the use of the HOME button. Now, add some more.
Code:printf("Hello World"); sceKernelSleepThread();
Well, in case you couldn't guess, the first one prints the text. Now, the reason why we put it in the parentheses. Anything inside partheses are called arguments. Just like a real argument, these ones decide what's going to happen. So, since you put "Hello World", it's going to print "Hello World". But if you chose some other word, it would print that instead. The second line is to make the program Sleep. If the program didn't sleep, it would print the words, then continue reading code, and when it ran out, it would exit. And this would all happen so fast, you wouldn't be able to see it. That's why we sleep the program. Now, let's finish.
Code:return 0; }
Remember how the function type was integer, and that we were going to have to return a number? Well, we returned 0. Why 0? I don't actually know. Just always put that at the end of your main function. And, speaking of the end, the curly bracket '}' ends our main function. So, we're done. Yay! But wait, did you put that extra line after the ending curly bracket? If you don't, the compiler will complain. Now, you need one more thing. Your Makefile! So, open up 'Makefile' and edit it to say this.
Code: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 won't understand any of this, but just know this. You can change the target to any name you want, but it CAN NOT have a space in it. And, the PSP_EBOOT_TITLE is the name that appears in the XMB, so that can be anything. In a later tutorial, I'll explain the code, but for now, I don't want to confuse you anymore. Now, let's compile. But first, make sure your code matches this. (It'd be nice to have indentations, but you don't have to.)
Code://Hello World Program /* Created by (Name) On (Date) */ #include <pspkernel.h> #include <pspdebug.h> #include <pspcallbacks.h> #define printf pspDebugScreenPrintf PSP_MODULE_INFO("Name Of Program",0,1,1); int main() { pspDebugScreenInit(); pspDebugScreenClear(); SetupCallbacks(); printf("Hello World"); sceKernelSleepThread(); return 0; }
So, open up the Cygwin batch file. Use the 'cd' function to change into your helloworld directory. Now, type 'make'. (Or 'make kxploit' if you're on 1.5) That will compile your code into an EBOOT. If there's errors, try to decifer them on your own. If you can't, use the tutorial to go back over the code. Now, I'm going to bed. See ya next time.
-
09-22-2009, 10:16 AM #3I'm Baaaack!

- Join Date
- May 2006
- Location
- Nowhere
- Posts
- 2,186
- QJ Pts
- 17,067
- Level
- 83
- Downloads
- 0
- Uploads
- 0
Tutorial 2 - Variables
Welcome to the second C tutorial. In this tutorial, I'll be focusing on variables, which is the most important topic in programming, no matter what langugage. In this tutorial, I'll be introducing you to three types of variables that are common in C and C++ for all platforms, not just the PSP. So, start out by making a new folder in your cygwin directory and make your main.c and Makefile. Now, let's get going.
First, add your comments, includes, defines, and module info just like in the last tutorial.
Code://Variable Example //(Date Here) #include <pspkernel.h> #include <pspdebug.h> #include <pspcallbacks.h> #define printf pspDebugScreenPrintf PSP_MODULE_INFO("Variables",0,1,1);
Now is the point where we define our variables. So, add this, even though you may not recognize anything.
Code:int number = 397; float decimal = 3.1415; char string[13] = "Hello World!";
OK, do you remember from the last tutorial that 'int' stands for integer? Good, well, that's the type of variable we created. We created an integer with the name 'number'. Note that you could have named these anything, not just what I put. An 'int' variable type can hold numbers from -32,768 to 32,768 or 2^15. (In programming everything is based off of 2 because of binary code.) The next line, we created a 'float' variable. A 'float' is just like an 'int' except that it can hold decimals. Our 'float' variable is named 'decimal' and is roughly equal to pi. And finally is our 'char' variable or character. As the name suggests, it hold characters. Our 'char' is name 'string' and has the value of "Hello World!". The reason for the 13 is that we have to specify how many characters we are going to use. But you say, "Hey that string is only 12 characters long!". Well, yeah, it is. But you have to leave an extra space for a terminator. (You don't have to understand it, just always leave an extra space.) Now, do you understand all the stuff we just wrote? I hope so, because we're moving on.
Do you remember what we do next? That's OK, you won't remember until the 50th time. For now, put this.
Code:int main() { pspDebugScreenInit(); pspDebugScreenClear() SetupCallbacks();
Got all that? Now, we want to print our variables, but how in the hell do we do that. If you put the variable name inside parentheses, it just prints the name, not the value. Well, you have to use what's called a conversion character. So, let's print our variable 'number'.
Code:printf("%i",number);
You're probably in your seat like, WHAT?!? OK, you know printf right? OK, good. The %i is our conversion character. It basically says this, "Hey, here I want to print a variable instead of a word". But, then the computer is like, "What variable do you want to print, there's so many of them?" Then, that's why you put the variable name after the comma. Do you understand now? Now, let's try the others.
Code:printf("\n %f",decimal);
OK, I admit, it's fun confusing you. We put an 'f' instead of an 'i' for 'float'. 'f' for 'flaot' and 'i' for 'int'. See? But, you're wondering why we put the \n there. Well, that's what's called an escape sequence. The \n stands for a new line. If we didn't put that in there, it would print both variables on the same line, but instead, we told it to start a new line first. Now, the last one, no tricks this time. (Or is there?)
Code:printf("\n %s",string);
Now, why didn't we put 'c' for 'char'? Well, %c is for ONE character, while %s is for a sting, or many characters. Now I'm going to throw this in as an example to you.
Code:printf("\n %i %f %s Look I can add words in combination with the conversion characters!",number,decimal,string);
That time, we did the same thing as before, only all on the same line. Make sure to put the variables in the correct order after the comma. Now that I'm done confusing the hell out of you, let's finish.
Code:sceKernelSleepThread(); return 0; }
Yeah! We're done. Now, compile the code using the makefile from the last tutorial. Just make sure to change the TARGET and PSP_EBOOT_TITLE lines to something that tickles your fancy. Compile it and test it. You should see your beautiful variables printed on the screen. See you all next time. Happy Coding!
-
09-22-2009, 10:17 AM #4I'm Baaaack!

- Join Date
- May 2006
- Location
- Nowhere
- Posts
- 2,186
- QJ Pts
- 17,067
- Level
- 83
- Downloads
- 0
- Uploads
- 0
Tutorial 3 - Button Input
We've done some programming for the PSP by now, but it's a little boring. I mean, to make a GAME you need user interaction. So, let's add that. In this tutorials we'll cover button input, and two fundamental aspects of programming, loops and if statements. So let's get started. Add the stuff that you know already. But add one extra include.
Code://Button Input Example //December 3, 2007 #include <pspdebug.h> #include <pspkernel.h> #include <pspcallbacks.h> #include <pspctrl.h> #define printf pspDebugScreenPrintf PSP_MODULE_INFO("Button Input",0,1,1);
Now, in case you couldn't guess, the extra include is for button input. Ctrl standing for control. Now, let's start our main loop.
Code:int main() { pspDebugScreenInit(); pspDebugScreenClear(); SetupCallbacks(); SceCtrlData pad;
Understand all that? No, of course not. Let me explain. 'SceCtrlData' is no differe nt from 'int', 'float' or 'char'. They're ALL variable types. So we just created a variable named 'pad' with the type of 'SceCtrlData'. We didn't give it a value, because it will be assigned later. Now, let's put in a new concept, loops.
Code:while(1) {
Now, one thing you should recognize is the starting curly bracket. Which means we're starting a new block of code. But, what exactly does a loop do. Well, a loop happens over and over again, just like a real loop (circle). So it starts at the starting curly bracket and executes all of the code until the ending curly bracket. When it hits the end, it goes back to the beginning and starts again. Now, let me explain the '1' now. Anything you put in the parentheses is compared to the number 1, and as long as it's equal to one, the loop continues. So, take this for example.
int var = 1;
while(var) {
The PSP would compare 1 to var. So if var is equal to 1, it'll continue, if it's not equal to 1, the loop will stop. Get it? Good. Now, some more new code.
Code:sceCtrlPeekBufferPositive(&pad, 1);
What this does, every time the loop starts again (a few hundred times per second) it will read the PSP buttons. And it stores the button pressed inside of 'pad' our variable that we made a little while ago. So if you press up, then 'pad' will be equal to 'up'. Now, some MORE new code.
Code:if (pad.Buttons & PSP_CTRL_UP) { printf("You pressed Up\n"); }
Now, this shouldn't be too hard to understand. Remember how every loop, the button we press gets stored into 'pad'? Well, now, we're using that variable. If the button we press is UP, then it will print that text. Notice how the 'if' statement has curly brackets too. This shouldn't too hard to understand. Let's do one more.
Code:if (pad.Buttons & PSP_CTRL_DOWN) { printf("You pressed Down\n"); }
Get it? Now, let's throw in some change.
Code:if (pad.Buttons & PSP_CTRL_RIGHT) { printf("You pressed Right\n"); } if (pad.Buttons & PSP_CTRL_LEFT) { printf("You pressed Left\n"); }
Ooh, tricky. You can put it all on the same line. But this is only for short ones. For long ones, use multiple lines.
Code:if (pad.Buttons & PSP_CTRL_CROSS) { printf("You pressed "); printf("Cross\n"); }
It may seem like I'm tricking you, but I'm just showing you the way.
Code:if (pad.Buttons & PSP_CTRL_CIRCLE) printf("You pressed Circle\n"); if (pad.Buttons & PSP_CTRL_TRIANGLE) printf("You pressed Triangle\n"); if (pad.Buttons & PSP_CTRL_SQUARE) printf("You pressed Square\n");
Now do you see all the different ways we can use it? They may come in handy. Anyway, here's the rest of the buttons.
RTRIGGER
LTRIGGER
HOME
SELECT
NOTE
Now, we do one last thing. First we put a closing bracket FOR THE LOOP.
Code:}
Now, we put one FOR OUR MAIN FUNCTION. There's a difference.
Code:return 0; }
Don't forget the return value.
Now, you can use the same Makefile as before, just edit the title. Hopefully everything I wrote works. Since I'm PSPless, I can't test it. Let me know of any errors.
-
09-22-2009, 10:17 AM #5I'm Baaaack!

- Join Date
- May 2006
- Location
- Nowhere
- Posts
- 2,186
- QJ Pts
- 17,067
- Level
- 83
- Downloads
- 0
- Uploads
- 0
Tutorial 4 - Images
Finally, some life to our programs. In this tutorial, I'll be introducing the graphics library by Psilocybeing. I know it's not the best, but I know it the best, and I'll be teaching it. Anyway, quite a few changes in this tutorial, so first thing's first. Create your new folder with your main.c and Makefile. Now, download the 5 attached files and put them in the same folder. But now, we need to add some libraries to our Cygwin setup. So, type this into Cygwin.
svn checkout svn://svn.pspdev.org/psp/trunk/zlib
If all goes right, a bunch of lines should go by, and it should download zlib. If not, post in the forums. Now type these lines, hitting enter after each one.
cd zlib
make
make install
cd
rm -Rf zlib
Now these lines too. This will install the .png library.
svn checkout svn://svn.pspdev.org/psp/trunk/libpng
cd libpng
make
make install
cd
rm -Rf libpng
Now, everything should be good to go, now we can start coding.
First thing, we have to add our includes and defines.
Code://Image Example //(Date) #include <pspkernel.h> #include <pspdebug.h> #include <pspcallbacks.h> #include "graphics.h" PSP_MODULE_INFO("Image Example",0,1,1);
There should be two different things here. The graphics.h file is the one you downloaded and put in the directory. The reason why we don't have to add all four files, is because graphics.h does that for you. Also, it's in parentheses because it's in the same directory as the main.c file. And we don't need printf anymore, as we won't be using it. So, let's continue.
Code:int main() { SetupCallbacks(); initGraphics();
All we did here is replace 'pspDebugScreenInit()' with 'initGraphics'. We won't be using the debug functions anymore, we'll be using the graphics library functions. So, let's load our image and blit(print) it.
Code:Image* background = loadImage("Background.png");
Now, that shouldn't be too hard to understand. The hardest part should be figuring out why we put a * next to image. Don't worry about that for now, just know to put one next to image. So, we just made a variable with the type 'Image' and the name 'background'. Then we assigned it the value of 'loadImage("Background.pn g")'. Loadimage is a function in the graphics lib, and would be extremely hard to code ourselves, but lucky we have the graphics lib. Anyway, I won't elaborate too much, as you should be understanding this stuff by now. Now, let's blit the image.
Code:blitAlphaImageToScreen(0,0,480,272,background,0,0);
Whoah, that a lot of data! The function is just the function to blit images to the screen. The first two numbers are the source x and y for if you have a tile sheet. Since we have a single image, they're just 0. (You might want to look up tilesheets if you don't know what they are.) The next two numbers are the width and height of the image. Then comes the image variable, which we just made. And finally, where on the screen we want to blit the image. Since our image takes up the entire screen, we'll blit it at 0,0. Now, let's finish up.
Code:flipScreen(); sceKernelSleepThread(); return 0; }
FlipScreen is a function used to draw the stuff to the screen. At first, it's drawn off-screen, this puts it on-screen. And we put the thread to sleep so it won't exit and you can see your lovely image. Now we're done. Let's compile that bad boy. But first we need a new Makefile. Here:
Code:TARGET = image OBJS = main.o graphics.o framebuffer.o CFLAGS = -O2 -G0 -Wall CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti ASFLAGS = $(CFLAGS) LIBS = -lpspgu -lpng -lz -lm EXTRA_TARGETS = EBOOT.PBP PSP_EBOOT_TITLE = Image Example PSPSDK=$(shell psp-config --pspsdk-path) include $(PSPSDK)/lib/build.mak
When running your EBOOT, don't forget to put the image in the folder with the EBOOT. If you get errors, tell me, because again, I don't have a PSP to test on.
-
09-22-2009, 10:18 AM #6I'm Baaaack!

- Join Date
- May 2006
- Location
- Nowhere
- Posts
- 2,186
- QJ Pts
- 17,067
- Level
- 83
- Downloads
- 0
- Uploads
- 0
Tutorial 5 - Player Movement
Welcome to the next tutorial. This tutorial wraps up everything that we've learned an puts it together. So, I'd have to say that this is the hardest tutorial so far, so be ready. First thing's first. You'll need to create a new project folder with the graphics library in it along with main.c and the Makefile we used in the last tutorial. Also, download the images attached. Now let's get started, but please know this. I won't be explaining everything like in the previous tutorials, as this is a mostly a review. So don't expect this to be easy. Let's continue, shall we? Throw in these includes and defines.
Code://Movement example //(Date Here) #include <pspkernel.h> #include <pspcallbaks.h> #include <pspdebug.h> #include <pspctrl.h> #include "graphics.h" #define prints printTextScreen PSP_MODULE_INFO("Movement Example",0,1,1);
All this should be familiar. The define is the function to print text to the screen, as you can't use printf if you're using the graphics library. Anyway more on that later. Now, let's continue.
Code:int main() { initGraphics(); SetupCallbacks(); SceCtrlData pad;
We brought back button input in this tutorial, so we added a variable for that.
Code:int x = 100; int y = 100;
These are our player's coordinates on the screen which we will use later.
Code:Image* player = loadImage("Player.png"); Image* background = loadImage("Background.png");
Those are our images. One is the player's image, and one is the background image.
Code:Color white = RGB(255,255,255);
We just made a color variable. If you're not familiar with RGB, look it up. Anyway, we just made white.
Code:while(1) { sceCtrlPeekBufferPositive(&pad, 1);
You know all about that right? Good, now, some new stuff. But first, an explanation. Imagine you're drawing on a chalkboard. You draw a picture of dog. Well, now you want to draw a pig, but you can't draw the pig over the dog. First, you'd have to erase the board. Well, I know this may be hard to comprehend, but you need to clear the PSP screen too. So, I'm going to put in the function to clear the screen. Then when you're done, I want you to run the program. Then, go back and take out the clear scree function and then run the program. You'll understand them. But basically, the PSP is drawing something on the screen every time the loop repeats, and we have to clear the screen first, or else everything gets pasted on top of each other. So add this:
Code:clearScreen(white);
We just cleared our screen off, with the color white that we made earlier. Now, let's blit our background and player.
Code:blitAlphaImageToScreen(0,0,480,272,background,0,0); blitAlphaImageToScreen(0,0,32,32,player,x,y);
First off, blit the background first, so the player is on top of the background. Next, you should understand it all except the player's coordinates. x and y aren't numbers. Ah, but they are. x and y are both equal to 100 right now. You ask, well, why not just put 100 in for each of them? Well, then they would always be in the same spot. What if we put the image at x,y and the values of x and y change? The player's position changes right? This is how we're going to make him move. Speaking of that, let's do it now.
Code:if(pad.Buttons & PSP_CTRL_UP) { y--; } if(pad.Buttons & PSP_CTRL_DOWN) { y++; }
You understand if statements right? Well, the y-- and y++ should confuse you. The -- means subtract 1 and the ++ means add one. So if y = 100 and you put y++, y is now equal to 101. So when you press up, y's value will go down. When y's value goes down, the player moves up the screen because the player is pasted at y. (Note that the top of the screen is 0 and the bottom is 272, like an inverse coordinate plane) So, let's do left and right.
Code:if(pad.Buttons & PSP_CTRL_RIGHT) { x++; } if(pad.Buttons & PSP_CTRL_LEFT) { x--; }
This time we used x because left and right is the x axis. (The left side of the screen is 0 and the right side is 480) So now when we press buttons our player will move! I know it's hard to understand, but you'll get it. Let's finish up shall we.
Code:prints(0,0,"This shows you how to print text",white); flipScreen(); } return 0; }
Now you should understand that. But in case you didn't get the prints line, the first two numbers are the x and y coordinates of the text, then the text, then the color. I was going to show you something else with that, but it'd be too confusing and I think you're stumped already. So maybe next time. Anyway, we're done. Here's a quick review of our loop:
start
clear the screen
paste our background first
paste our player second
check for button input
put everything on the screen
end
Now, use the Makefile from last time to compile. Make sure to put the images in the same directory as the EBOOT when running it. I wrote this tutorial kind of quick, so please tell me about errors.
-
09-22-2009, 10:19 AM #7I'm Baaaack!

- Join Date
- May 2006
- Location
- Nowhere
- Posts
- 2,186
- QJ Pts
- 17,067
- Level
- 83
- Downloads
- 0
- Uploads
- 0
Tutorial 6 - Loops and Arrays
So, I stayed home today from school, because I was sick. Decided to write this tutorial. We'll be covering Arrays, which are variables that hold large amounts of data. And, we'll be covering a new type of loop, a for loop. So, let's get started. First, we're going to want add our usual stuff. Nothing too special in this tutorial.
Code:#include <pspkernel.h> #include <pspcallbacks.h> #include <pspdebug.h> #define printf pspDebugScreenPrintf PSP_MODULE_INFO("Arrays",0,1,1);
Am I forgetting something? If so, tell me. If not, then let's make our first array. Ready?
Code:int array[10];
Now, we just made an array. It looks like a normal integer, but it's got the '[10]' there. That means that we made not only one variable, but ten! We have ten variables named 'array[0]', 'array[1]'..'array[9]'. They behave just like normal variable, there's just more of them. And notice that we start counting at 0, and go up to 9, and 0-9 is then numbers. So, this is pretty easy huh? But don't forget, just like a normal int, none of these have values yet. But I'm going to show you how to make an array that assigns the values automatically.
Let's make a float array.
Code:float data[5] = {3.14, 2.71, 8.46, 0.43, 6.45};
Now these are regular floats, but they have values too. Now, I'm going to show you how I make a char array, as this way keeps it a bit cleaner.
Code:char buf[5][20] = { {"Hello"}, {"Goodbye"}, {"Programming"}, {"Is"}, {"Fun"}, };
It's more organized that if you wrote it on one line. And notice there's two numbers in brackets. The first is the number of variable in the array. The second is how many characters each variable can hold. Now, we have our variables. So, let's introduce the for loop.
Code:int main() { pspDebugScreenInit(); pspDebugScreenClear(); SetupCallbacks(); int x;
OK, so I prepared everything. I even made the variable I'll need for the loop. Now, here we go.
Code:for(x=0;x<10;x++) { array[ x ] = 5; }
Whoah, that's confusing. First, let's take the three arguments of the loop. Argument one is at what value the variable starts. In our case, our variable 'x', started at the value 0. The second argument is what defines when the loop stops. For ours, the second argument says this. "While x is less than 10". So as long as x is less than 10, it'll continue, like a while loop. The third argument is what happens to x every time the loop starts over. In our case, we add one to x each time it starts over. So, here's a rundown.
X starts at 0, and the loop starts. When the loop ends, it starts over, and adds one to x. This will do this all the way up to nine. When x is equal to nine, and the loop starts over, the loop will stop. It will try to continue, but it will add one to x, making x 10, and since our statement is no longer true, it stops.
I tried my best. Do you get it? I hope so. Anyway. What's inside the loop. Just substitute the value of x in for the x. So the first time around it will set the variable 'array[0]' to the value of 5. The second time around, 'array[1]' will equal 5. This happens all the way up to nine. Now, let's use our knowledge, and just quickly print the contents of our arrays, as I'm sick of typing.
Code:for(x=0;x<10;x++) { printf("%i ",array[ x ]); } printf("\n\n"); for(x=0;x<5;x++) { printf("%f ",data[ x ]); } printf("\n\n"); for(x=0;x<5;x++) { printf("%s ",buf[ x ]); }
I hope you got all that. If not, stop by the forums and ask some questions. You're bound to have some. So, let's finish up.
Code:sceKernelSleepThread(); return 0; }
So, go ahead and compile your masterpiece. You can use a Makefile from the Hello World lesson. If something is wrong, tell me. I now have a PSP, but I don't have Cygwin installed to test. So there might be a mistake. Anyway, see you next time.
-
09-22-2009, 10:20 AM #8I'm Baaaack!

- Join Date
- May 2006
- Location
- Nowhere
- Posts
- 2,186
- QJ Pts
- 17,067
- Level
- 83
- Downloads
- 0
- Uploads
- 0
Tutorial 7 - Functions
Hello and welcome to the tutorial on functions! You use functions all the time in programs, but many of you don't realize it, and if you do, you don't know exactly what functions are or what makes them up. Today, we'll clear all that up. We're going to make two simple functions. So, let's get started.
Code:#include <pspkernel.h> #include <pspdebug.h> #include <pspcallbacks.h> #define printf pspDebugScreenPrintf PSP_MODULE_INFO("Functions",0,1,1);
Now, that's all the normal stuff we need. So, let's add our first function. A very simple mathematical one.
This may be a bit confusing. First off, note the type of the function. It's an 'int'. It's just like out main function, it return an integer. So that means you can do this:Code:int function(int x) { x = x * x * x; x = x * 5; x = x + 20; return x; }
int foobar = function(5);
And after you do that, foobar will equal function(5). Well, how exactly do we calculate function(5). Well, the function takes an 'int' as an argument. (The argument is the number inside the parentheses.) When we put function(int x), it means that the function will take an integer as a parameter. And by labeling it x, it means that when we want to use the variable that is inputted, we use x. So, to calculate function(5), put 5 in for all the x's in the function. If you do it right, it should come out as 645. So the return value will be 645. So that means foobar will equal 645. Do you get it now? For those of you that are in Algebra 2 or Calculus, thing of that function like this:
y = x^3 + 5x + 20
Or to make the connection
f(x) = x^3 + 5x + 20
See how that's a function too? Anyway, let's make one more function. This one won't have a return value. I'll show you.
Code:void printText() { printf("This function\n"); printf("prints a lot of\n"); printf("text by using\n"); printf("only one line.\n\n"); }
You should see how this one works. But, you might be confused about the void. Well, since it has no return value, the function doesn't have a type. And when a function doesn't have a type, we just use the type of 'void'. So, now that we have our functions, let's start our main function.
Code:int main() { pspDebugScreenInit(); pspDebugScreenClear(); SetupCallbacks(); int foobar = function(5); int foo = function(7); int hello = function(3.14); printf("%i %i %i \n \n",foobar,foo,hello); printText(); printText(); sceKernelSleepThread(); return 0; }
All of that should be fairly easy to understand. Notice that we don't put anything inside the printText() parentheses because it doesn't take any arguments. Also, when you run this program, function(3.14) prints out 155, when it should printf out 174.79572. That's because the function takes 'int' as an argument, not a 'float'. So, when you give it a decimal number, it rounds to the nearest number, then computes it. So, use a Makefile, compile and run it. I'll see you all in the next tutorial.
-
09-22-2009, 10:22 AM #9I'm Baaaack!

- Join Date
- May 2006
- Location
- Nowhere
- Posts
- 2,186
- QJ Pts
- 17,067
- Level
- 83
- Downloads
- 0
- Uploads
- 0
Tutorial 8 - Copying Files
Welcome to my hardest tutorial yet. The reason I say this, is because out of a 41 line file, you probably understand about 15 lines. Anyway, let's get started. This tutorial is on reading and writing files. So, we'll be copying a file. (Or in other words, reading a source file, and writing to a blank destination file.) Here's some stuff you understand. (The two new includes contain file reading and writing functions. And we don't need the callbacks because our program will exit on it's own.)
Now, we'll be starting a function.Code:#include <pspkernel.h> #include <pspdebug.h> #include <stdio.h> #include <stdlib.h> #define printf pspDebugScreenPrintf PSP_MODULE_INFO("File Copier",0,1,1);
Code:void fcopy(char * source, char * destination) {
That's the first line, and you're probably already confused. Why the *s? And why no size for the chars? Well, they both go together. Someone is going to input a filename as an argument. So, how long will it be? We don't know. So, we take that into account. By adding the '*' we're creating a pointer. A pointer points to an address in the memory of the PSP. And since this points to an address, but doesn't specify how much memory we can use, it's perfect for our case, because we don't know how long the file will be. When pointers are used as function arguments, the PSP automatically sizes the pointer to the correct size of the file path. Anyway, let's continue.
Code:printf("Preparing Variables... "); FILE * src, * dst; char * buffer; int size;
The first line is just so we can have some text output, to make it look more professional. The second line is where we declare our files. For reasons unknown to me, C requires the 'FILE' type to be a pointer. So when you declare a FILE, add the * before the name like I did. After that, we have another char pointer. This will be used to store the file in, as a file is just an array of characters. Again we don't know the file size, so we'll create a pointer. But unlike before, the PSP won't auto size this one, so we'll have to do it manually later. And finally, the int is for the size of our file. We'll use it later to size our char pointer. Now, some more.
Code:src = fopen(source,"r");
Now, we're opening our file. fopen is the function we use to open the file. We're opening 'source' (from our argument). If you don't use a variable as this parameter, you just use text inside quotes for your file path. And the second parameter is the mode to open it in. We'll be opening in Read mode, as we're just going to read it.
Code:fseek(src,0,SEEK_END); size = ftell(src); rewind(src);
OK, now this is confusing. The first line is the function that 'seeks' through the file. In other words, it goes to a position in the file you want it to. So if you wanted to seek to the 20th character, you could. The first argument is the file to seek through. The second is where to start. We're starting at 0. The third argument is where to go to. We're going to the end of the file. Why would we do this. Well, say we had a file that was 400 bytes. If we seeked to the end, we'd be at position 400 in the file right? So, the position of the end of the file, is equal to the file size. Well, how do we get teh position? That's the next line. Ftell tells us the current position in the file. And since we're at the end, it'll tell us the file size. So, 'size' is now equal to the file size. And, the third line rewinds us back to the beginning of the file so we can read it.
Code:buffer = malloc(size); printf("Done. \n"); printf("Reading %s... ",source);
So, here's where we make the size of out char pointer. We need the buffer to be big enough to hold the entire file. Well, luckily for us, we know know the size of the file, 'size' bytes. So, that line makes our buffer 'size' bytes big. (malloc is defined in stdlib.h, if you don't use it, you don't need stdlib.h) The next two lines are just messages to the user.
Code:fread(buffer,1,size,src); printf("Done. \n"); printf("Writing to %s... ",destination);
Finally, we're reading our file. Fread takes 4 arguments. The first is where to put the data we read. We're going to store it in 'buffer'. The second argument is how big each chunk will be. We put one, as we're going to read the file one byte at a time. The third argument is how many bytes to read. We're going to read 'size' bytes. (Our file size, so, we're going to read the entire file.) And the fourth argument is the pointer to the file. Then, we have more messages.
Code:fclose(src); dst = fopen(destination,"w");
Now that we're done reading, we can close the source file. (That's what the first line does. Wink ) Then, we open our destination file. We're opening it in 'write' mode. Well, what if the destination file doesn't exist? Well, if you open a file in write mode, and it doesn't exist, it'll be created for you. But be careful. If the file does exist, and you open it in write mode, it'll erase all the data.
Code:fwrite(buffer,1,size,dst);
Fwrite is just like fread. There's only two different things. The first argument is the data to write, not where the data read goes. And, the last argument is the file to write to, not the file to read.
Code:fclose(dst); free(buffer); printf("Done. \n"); }
Now, we close our destination file. (We're done writing to it.) Then, we free the memory we used for buffer. You don't have to free it, but why take up the memory if you're not using it? Some other program might need it. And finally, another message stating that we're done. And that's the end of our function. So, let's create our main function, and put it to use.
Code:int main() { pspDebugScreenInit(); pspDebugScreenClear(); fcopy("flash0:/vsh/resource/topmenu_plugin.rco","ms0:/topmenu_plugin.rco"); fcopy("flash0:/font/ltn0.pgf","ms0:/ltn0.pgf"); printf("Done. Auto-Exiting in 10 seconds..."); sceKernelDelayThread(10*1000*1000); sceKernelExitGame(); return 0; }
That's not too hard to understand is it? sceKernelDelayThread pauses the program for some number of milliseconds. We used 10 million milliseconds, so it pauses for 10 seconds. sceKernelExitGame exits the game. (Who would have thought?) That's why we didn't need the callbacks. But note, that we can read from the flash0, NOT write to it. To write to it, you have to do something different, which I'll teach you later. And if you did everything right, you should have topmenu_plugin.rco and ltn0.pgf in the root of your memory stick. See you in the next tutorial.
EDIT: Due to the way I've coded this program, it will only copy files up to 32kb. (About.) To make that bigger, change this:
int size;
To this:
long size;
If you need to know what long is, check out the C Cheat Sheet in the snippets section. And after changing it to long, it will still only copy about 20MB files, as that's all the RAM the PSP has, and I didn't want to go too in depth by copying the file in parts if it was too big. We'll get to that later.
-
09-22-2009, 10:22 AM #10I'm Baaaack!

- Join Date
- May 2006
- Location
- Nowhere
- Posts
- 2,186
- QJ Pts
- 17,067
- Level
- 83
- Downloads
- 0
- Uploads
- 0
Tutorial 9 - If, Else-If and Switch-Case Statements
Welcome to the next tutorial of PSP-Coding.com. Today, we'll be focusing on different types of if statements, and Switch-case loops. This one is short, simple, and has no point. So, let's get to it.
Code:#include <pspkernel.h> #include <pspdebug.h> #include <pspcallbacks.h> #define printf pspDebugScreenPrintf PSP_MODULE_INFO("Switch-Case",0,1,1); int age,fav_number,legs;
Not too much going on. Just declared three variables. (Integers to be exact) Now, let's start our loop, and I'll show you 3 quick examples.
Code:int main() { pspDebugScreenInit(); SetupCallbacks(); age = 42; fav_number = 3; legs = 3;
So, first example?
Code:if(age < 18) { printf("You're young, you're still in school.\n\n"); } else if((age > 18) && (age < 100)) { printf("Geez, you have one foot in the grave already\n\n"); } else if(age > 100) { printf("You're not dead yet?\n\n"); }
Do you see what we did? By putting 'else if' we're connecting all the if statements together. Well, why do that? Well, say you had those three, but were all separate. It would read the first, not true. It would read the second, true. It would read the third, not true. Well, why read the third one? We already know it's false. So, with else if statements, it skips the rest when it finds a correct one. It makes it more efficient. And, the && is to make sure both are true. So, 'if age is greater then 18, AND age is less than 100, then do blah'. Now, another similar example.
Code:if(fav_number == 4) { printf("Our favorite numbers are the same\n\n"); } else { printf("Our favorite numbers aren't the same\n\n); }
With just the else statement, it checks the first statement to see if it's true, if it's not, it automatically does the 'else' clause. So, in our case, fav_number is not equal to 4, so it'll do the 'else' part. Now, last, but hardest.
Code:switch(legs) { case 0: printf("I'm sorry you don't have legs.\n\n"); break; case 1: printf("One is better than none.\n\n"); break; case 2: printf("Glad to see you're normal.\n\n"); break; case 3: printf("You're just inhuman.\n\n"); break; default: printf("How many do you have?\n\n"); break; }
Complicated, right? Well, let's explain. Our argument was legs. (switch(legs)) So, our loop will be comparing the variable legs. It will compare it to the first case, 0. If legs is equal to 0, it will print that line. Then, it will break. Break is a keyword to skip out of the current loop. Basically, if it's equal to 0, we don't need to test for any of the other ones. So, break, will make it jump out of the loop. So, it will continue reading your code after the '}'. Now, say it isn't equal to 0, it will check for 1,2 and 3, because we told it to. Well, what if it isn't equal to any of those? Well, then it does the 'default' case. If none of the others come up true, it does this. But, you still have to have the break, so the loop doesn't start over and start checking values again.
I know I was vague, but none of them were that hard to understand. If you need help, just ask. Anyway, finish up.
Code:sceKernelSleepThread(); return 0; }
Now compile and run. Try changing the values of the variables to get different results, go wild. And don't forget to tune in to the next tutorial.
-
09-22-2009, 10:23 AM #11I'm Baaaack!

- Join Date
- May 2006
- Location
- Nowhere
- Posts
- 2,186
- QJ Pts
- 17,067
- Level
- 83
- Downloads
- 0
- Uploads
- 0
Tutorial 10 - Menus
Hello, and welcome to the newest tutorial. Today, I'll be teaching you how to make a menu. I realize that I said this would be a sound tutorial, but it's not. I'm investigating MP3 decoding, this way, I can use my own code, rather than use someone else's. So, this tutorial is all about a menu. It'll be quick, clean and easy to understand. So, let's get to it. We'll be using the graphics library again, so put those four files in your project folder.
Code:#include <pspkernel.h> #include <pspcallbacks.h> #include <pspctrl.h> #include "graphics.h" #define RGB(r, g, b) ((r)|((g)<<8)|((b)<<16)) PSP_MODULE_INFO("Menu Example",0,1,1);
This time, we didn't include pspdebug.h. This is because we won't be using it this time. It's usually for printing and clearing the screen, but this time, we have the graphics lib. The define is so we can use RGB color format, rather than hexadecimal BGR format. We'll get to that later. Or now. I'll introduce the color variable type. It's defined in graphics.h, so if you use it, include that.
Wait, don't put that in your code yet. Do you see how it works? RGB is simple to understand, if you don't know what it is, look it up. But, we define the type, then give a name, then a value. Simple. But let's shorten it up.Code:Color white = RGB(255,255,255); Color red = RGB(255,0,0); Color black = (RGB0,0,0);
Now you can put it in your code. White will be used for the background, black for the menu items, and red for the highlighted item. Speaking of items, let's make our menu items.Code:Color white = RGB(255,255,255), red = RGB(255,0,0), black = RGB(0,0,0);
Code:char menuItems[10][25] = { {"Item 0"}, {"Start"}, {"Options"}, {"Item 2"}, {"Controls"}, {"Online Play"}, {"Item 6"}, {"Item 7"}, {"Credits"}, {"Exit"}, };
You remember arrays, don't you? If not, look back. We could put this on one line, but there's too many. Now, some variables.
Code:int timer,select,foo; SceCtrlData pad;
The timer is used for button input, as you'll see later. Select is used for the highlighted item in the menu. Foo is a variable we'll use for a 'for' loop later on. The second you should know already. Let's start our function.
Code:int main() { initGraphics(); SetupCallbacks(); while(1) { sceCtrlReadBufferPositive(&pad,1); fillScreenRect(white,0,0,480,272); timer++;
Not too much new there, except fillScreenRect. What this does, is create a 480x272 rectangle, and blit it to 0,0, covering the entire screen in white. There is a clearScreen function in the graphics.h, but it never works, so I do this. And we increase timer by one. We'll use timer to control how fast buttons can be pressed.
Code:for(foo=0;foo<10;foo++) { printTextScreen(200,foo*10+70,menuItems[foo],black); }
Whoah, that's a lot to take in. You know what for loops are. We stop when foo is less than 10 because we only have 10 menu items. So the loop stops and 9, and menuItems[9] is the last one. The printTextScreen function is defined in graphics.h and takes four arguments. The first and second are the x and y coordinates. The third is the text to print. The fourth is the color. We're printing our menu items 200 pixels from the left side of the screen. The y coordinate is a little trick though. Basically, since foo increases by 1 every time, the y coordinate will increase by 10, since foo is multiplied by 10. We add the 70 to make it more centered in the y direction. Manually put in the numbers and you'll see what happens. The third argument is our menuItems. Since foo changes every time, so will the menu item. When you run the program, you'll see what happens. Now, onwards!
Code:printTextScreen(200,select*10+70,menuItems[select],red);
Tis is the same thing as before, only this time, we're using select. Select has only one value, so we'll have only one highlighted item. As we increase and decrease 'select', the highlighted menu item will change. Now, let's add the button input.
Code:if((pad.Buttons & PSP_CTRL_UP) && (timer > 10) && (select > 0)) { select--; timer = 0; } else if ((pad.Buttons & PSP_CTRL_DOWN) && (timer > 10) && (select < 9)) { select++; timer = 0; }
This isn't so hard. If we press up, make select go down. But, we only want it to work if timer > 10. Timer starts at 0, so that means you can't press the button until after 10 loops. When we're done, we set timer back at 0. Then, you have to wait ten more loops before the button will work again. This makes it so the selector in the menu doesn't go up and down so fast. And we added the restrictions on 'select' because we only have 10 items. So if it was below 0, or greater than 9, calling menuItems[select] would give us an error. So, finally, let's finish.
Code:flipScreen(); } return 0; }
Finally, use an old Makefile from the image tutorial, then compile and run. If all goes well, you should have a working example. To make clickable items, do something like this:
if((pad.Buttons & PSP_CTRL_CROSS) && (select == 9)) {
//blah blah, this will exit the game, since menuItems[9] = "Exit"
}
See you in the next tutorial.
-
09-22-2009, 10:24 AM #12I'm Baaaack!

- Join Date
- May 2006
- Location
- Nowhere
- Posts
- 2,186
- QJ Pts
- 17,067
- Level
- 83
- Downloads
- 0
- Uploads
- 0
Tutorial 11 - OSLib Images
Welcome to the next tutorial in the set of C tutorials. This is part one of two in a set that will help you convert from the graphics library by Psilocybeing to OSlib. First thing's first, you have to download OSlib. So, download the file attached and extract it. Then in the 'Install' folder, right-click on 'Install.bat' and click 'Edit'. The third line should say this:
set PSPSDKDIR=E:\cygwin\usr\l ocal\pspdev\psp\sdk
I want you to replace 'E' with the drive letter that Cygwin is installed on. Afterward, save the file, then run it. Congrats, you've installed OSlib!
EDIT: I just realized that you need mikmodlib. So type this in Cygwin:
svn co svn://svn.ps2dev.org/psp/trunk/mikmodlib
cd mikmodlib
make
make install
That should fix some errors that you have. As I've said, I don't have a PSP or Cygwin, so I can't test ANY of this.
Now, this is going to work a little different, as you're a lot more skilled now. I'm going to give you the OSlib code, and explain what it does afterward. So here it is. This code simply blits an image to the screen.
Code:#include <oslib/oslib.h> PSP_MODULE_INFO("OSLib Image Simple", 0, 1, 1); int main() { oslInit(0); oslInitGfx(OSL_PF_8888,1); OSL_IMAGE * background = oslLoadImageFile("Background.png",OSL_IN_RAM,OSL_PF_8888); oslStartDrawing(); background->x = 0; background->y = 0; oslDrawImage(background); oslEndDrawing(); oslSyncFrame(); oslWaitKey(); oslEndGfx(); oslQuit(); return 0; }
Now, the structure may be familiar, but that's about it, so let's go through it.
Line 1 simply includes out OSlib header file, and note that it's the only one we need.
Line 2 is our module info, no difference.
Line 3 is the normal start of our main function.
Line 4 does two things. It starts OSlib up for us to use it, and it deals with our callbacks. That's right, no more SetupCallbacks();, it's already included in that function.
Line 5 preps our screen for drawing. The first parameter means we want 32-bit color. (Highest available on the PSP.) The second parameter simply means we want to use a single buffer. (Leave as-is unless you know what you're doing.)
Line 6 loads our image. We know use OSL_IMAGE rather than Image, but it's used in pretty much the same way. The first parameter is out image location, the second states we want to load it into the RAM, and not VRAM, and the third parameter just means it's a 32-bit color image. (If the real image has a lower resolution, no big deal.)
Line 7 lets the PSP know that we will be drawing items. (Text, pictures, lines, etc.) Just make sure to put it before you start drawing.
Line 8 and Line 9 are kind of new. The x and y coordinates of the image we are going to blit is stored in the structure of OSL_IMAGE. (Read the snippet on structures.) So, to change the x and y coordinate, just change those values. (Shouldn't be too hard to understand.)
Line 10 actually blits our image. It doesn't need the x and y coordinates, because they're already stored in the structure.
Line 11 says that we're finished drawing.
Line 12 syncs our buffer with the screen. (Equivalent of flipScreen()Wink
Line 13 tells the PSP to wait until any button is pressed. When you press a button, it will continue.
Line 14 un-initializes the PSP screen.
Line 15 exits the game.
And lines 16 and 17 are just the end to our main function.
Now, that was a fairly easy tutorial. The next OSlib conversion tutorial will be harder, but not too difficult. After the next one, it's back on to learning new programming techniques!
EDIT: OH MY GOD! I nearly forgot the Makefile. Here it is.
Code:TARGET = oslibimages OBJS = main.o CFLAGS = -G4 -Wall -O2 CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti ASFLAGS = $(CFLAGS) LIBS = -losl -lpng -lz -lpspsdk -lpspctrl -lpspumd -lpsprtc -lpsppower -lpspgu -lpspaudiolib -lpspaudio -lm EXTRA_TARGETS = EBOOT.PBP PSP_EBOOT_TITLE = OSlib Images PSPSDK=$(shell psp-config --pspsdk-path) include $(PSPSDK)/lib/build.mak
BTW- If there's any problems, let me know. I don't have a PSP to test it on anymore. Cry
-
09-22-2009, 10:26 AM #13I'm Baaaack!

- Join Date
- May 2006
- Location
- Nowhere
- Posts
- 2,186
- QJ Pts
- 17,067
- Level
- 83
- Downloads
- 0
- Uploads
- 0
Tutorial 12 - OSLib Menus
OK, well, it's been a while since I've written a tutorial, but I am back, and back with a working PSP! That's right, this EXACT code has been tested and works 100%. This is my menu tutorial, ported to OSlib. (It's much sexier.) So, let's get started.
Code:#include <oslib/oslib.h> PSP_MODULE_INFO("OSlib Menu Example",0,1,1);
Does that really need to be explained?
Code:OSL_COLOR black = RGB(0,0,0),red = RGB(255,0,0), white = RGB(255,255,255);
This is similar to the old code. Only, RGB is defined in OSlib, so you don't need to do it.
Code:int select,foo;
Notice I took out the timer variable. You'll see why later.
Code:void oslPrintText(int x, int y, char * text, OSL_COLOR color) { oslSetTextColor(color); oslSetBkColor(RGBA(0,0,0,0)); oslPrintf_xy(x,y,text); }
OK, this is a function of my own creation. I made it because there was no simple way to print text in the color you wanted. So, this function, sets the text color to the color of your choice. It then sets the background color to clear, that way you don't get boxes around your text. Then, it prints it in the X and Y locations specified.
Code:char menuItems[10][25] = { {"Item 0"}, {"Start"}, {"Options"}, {"Item 2"}, {"Controls"}, {"Online Play"}, {"Item 6"}, {"Item 7"}, {"Credits"}, {"Exit"}, };
Nothing new here.
Code:int main() { oslInit(0); oslInitGfx(OSL_PF_8888,1); oslInitConsole();
Not much new since the last OSlib tutorial. Just getting the PSP ready.
Code:while(!osl_quit) { oslStartDrawing(); oslReadKeys(); oslClearScreen(white);
If you couldn't guess, this reads our buttons and clears the screen to the color white. Oh, and the !osl_quit part. That just means that the loop will continue as long as osl_quit is NOT true. The ! means 'not' is a simplified term. So !true means 'false'. Not too hard to understand. The only way to make osl_quit true, is to call oslQuit();.
Code:for(foo=0;foo<10;foo++) { oslPrintText(200,foo*10+70,menuItems[foo],black); } oslPrintText(200,select*10+70,menuItems[select],red);
Same as before, only using the function I made.
Code:if ((osl_keys->pressed.up) && (select > 0)) select--; if ((osl_keys->pressed.down) && (select < 9)) select++;
Ah, the biggest difference. Instead of using the timer to limit things, just use pressed.up. It only reads the button ONCE. So, if somebody presses up, and then holds it, it'll only move the cursor once. You can also use held.up if you want that effect.
Code:oslEndDrawing(); oslSyncFrame(); } oslQuit(); return 0; }
OK, you're done drawing, you sync the framebuffer with the screen (paste the stuff you did on the screen), and you ended the loop and function. And, on top of that, you remembered to put an extra line after everything. So now, you're done. Easy to convert, huh? Well, next tutorial is all new stuff, so be ready.
Code:TARGET = oslibmenu OBJS = main.o CFLAGS = -G4 -Wall -O2 CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti ASFLAGS = $(CFLAGS) LIBS = -losl -lpng -lz -lpspsdk -lpspctrl -lpspumd -lpsprtc -lpsppower -lpspgu -lpspaudiolib -lpspaudio -lm EXTRA_TARGETS = EBOOT.PBP PSP_EBOOT_TITLE = OSlib Menu PSPSDK=$(shell psp-config --pspsdk-path) include $(PSPSDK)/lib/build.mak


LinkBack URL
About LinkBacks

Thank you for sharing. You can download the app at mobidescargar and feel
Android App Reviews?