Access_Denied's Forums

Site Resources => Tutorials => Topic started by: Access_Denied on August 13, 2007, 04:19:46 PM



Title: Tutorial Four: Button Input
Post by: Access_Denied on August 13, 2007, 04:19:46 PM
OK, I've been really lazy lately. And now, I don't have a PSP.  :'( But, I've still decided to do the tutorials, so here it goes.

OK, in this lesson, we'll be learning button input. It's actually really easy, so expect this tutorial to be a little short.

First, our includes, info and defines.

#include <pspdebug.h>
#include <pspkernel.h>
#include <pspcallbacks.h>
#include <pspctrl.h>
#define printf pspDebugScreenPrintf

PSP_MODULE_INFO("Button Input Test",0,1,1);

Now, our main function.

int main() {
     pspDebugScreenInit();
     pspDebugSetBackColor(0xFF0000);
     pspDebugSetTextColor(0x000000);
     SceCtrlData pad;

OK, you should recognize the first line. The next two lines are pretty self explanitory. They set the background and text color. The values inside are 32bit RGB Colors. If you don't know what it is, look up RGB on wikipedia. And the fourth line is our button variable. We used the variable "pad", but you can use anything. And since our variable is named pad, anytime we want to use the buttons, we use the word, "pad", easy huh?
Now, we're going to do another new thing, our main loop.

     while(1) {
          sceCtrlPeekBufferPositive(&pad, 1);

Now, a loop is something that happens over and over again, like a real loop or circle, it never ends. So, any code that is inside the loop is read and re-read over and over again forever. For example, this loop:

while(1){ //Start of loop
     printf("Hello"); //loop executes code inside
} //loop ends and goes back to start of loop

This would print "Hello" infinite times. Now, you might wonder what the one is for. Well, a loop compares anything in the parentheses to 1, or true. So take this for example.

while variable {

This loop will loop over and over again, as long as 'variable' equals 1. Got it? Now, since we put a 1 instead of a variable, and 1 always equals 1, the loop will continue forever. Not too hard. Now, the second line.

The second line in our loop allows us to read input from the buttons. That's sort of a dumbed down version, but that's all you really need to know. If you want to use the buttons, use that line in the loop. Now, let's continue, shall we?

          if(pad.Buttons & PSP_CTRL_CROSS) {
               printf("You pressed Cross\n");
          }

Now, this is what's called an 'if statement'. What it does converts to this:

if you press the cross button then
print the words "You pressed Cross"
done

Easy, right? Now, I'm going to add one more button, but you can add as many as you like.

          if(pad.Buttons & PSP_CTRL_LTRIGGER) {
               printf("You pressed L-Trigger\n");
          }
     } //end of our main loop (while(1) {)
     return 0;
} //end of our main function (int main() {)

Here's the other buttons codes.

PSP_CTRL_SELECT
PSP_CTRL_START
PSP_CTRL_UP
PSP_CTRL_RIGHT
PSP_CTRL_DOWN
PSP_CTRL_LEFT
PSP_CTRL_LTRIGGER
PSP_CTRL_RTRIGGER
PSP_CTRL_TRIANGLE
PSP_CTRL_CIRCLE
PSP_CTRL_CROSS
PSP_CTRL_SQUARE
PSP_CTRL_HOME

Now, just use this makefile and you're done.

TARGET = Button
OBJS = main.o

CFLAGS = -O2 -G0 -Wall
CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti
ASFLAGS = $(CFLAGS)

EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = Button Test

PSPSDK=$(shell psp-config --pspsdk-path)
include $(PSPSDK)/lib/build.mak

**If it doesn't work, please tell me the errors, and I will try to fix it. Remember, I don't have a PSP to test it on. **


Hosted by www.Geocities.ws

1