PSP Programming/General/Button Input
Button Input
editIn this tutorial we will learn how to work with handling user input. Let's start by including some headers. You should already know what the first two are for, and the third one is to get controller input. As before we will also include the callback header.
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspctrl.h>
#include "../common/callback.h"
We'll raise the revision so that our program is now at version 1.1. This is assuming each progression of tutorial deserves a raise to the revision. The rest of the code is the same aside from the name change in the psp module info.
#define VERS 1
#define REVS 1
PSP_MODULE_INFO("Button Input", PSP_MODULE_USER, VERS, REVS);
PSP_MAIN_THREAD_ATTR(PSP_THREAD_ATTR_USER);
PSP_HEAP_THRESHOLD_SIZE_KB(0);
#define printf pspDebugScreenPrintf
In our main function we do the usual, but this time create an 'running' variable so that we can update it if the exit callback occurs or if the user presses the button 'start' (more on this later). Then we create the button input object and set the sampling rate to 0, and allow analog mode so that we can read where the analog pad is.
int main()
{
pspDebugScreenInit();
setupExitCallback();
int running = isRunning();
SceCtrlData buttonInput;
sceCtrlSetSamplingCycle(0);
sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);
Now we start the while loop, update our variable from the exit callback telling us if we should quit or not. We reset the position to print, and print out the analog pad position. Printing the variable is done by passing it in the second parameter and using '%d' as a placeholder for it.
One thing to remember, when you get the position of the analog stick using the 'Lx' and 'Ly' properties, you get a value from 0 to 255. You can subtract 128 from that so that the center would be 0. Unfortunately there is such a thing as a “dead-zone” which means that the user might not even be touching the analog stick, but it may still detect a value between -30 to 30. We'll learn how to deal with this problem in later tutorials.
while(running)
{
running = isRunning();
pspDebugScreenSetXY(0, 0);
printf("Analog X = %d ", buttonInput.Lx);
printf("Analog Y = %d \n", buttonInput.Ly);
To update our button input variable with the button information we call 'sceCtrlPeekBufferPositive' and give it the reference to our variable. When we want to check what buttons are pressed we first check if any buttons are pressed at all. If the 'start' button is pressed, we print it out and then close our program by making 'running' equal to zero.
sceCtrlPeekBufferPositive(&buttonInput, 1);
if(buttonInput.Buttons != 0)
{
if(buttonInput.Buttons & PSP_CTRL_START){
printf("Start");
running = 0;
}
if(buttonInput.Buttons & PSP_CTRL_SELECT) printf("Select");
We do the same thing with the other buttons, but we do not close the program, just print out the name of the button.
if(buttonInput.Buttons & PSP_CTRL_UP) printf("Up");
if(buttonInput.Buttons & PSP_CTRL_DOWN) printf("Down");
if(buttonInput.Buttons & PSP_CTRL_RIGHT) printf("Right");
if(buttonInput.Buttons & PSP_CTRL_LEFT) printf("Left");
if(buttonInput.Buttons & PSP_CTRL_CROSS) printf("Cross");
if(buttonInput.Buttons & PSP_CTRL_CIRCLE) printf("Circle");
if(buttonInput.Buttons & PSP_CTRL_SQUARE) printf("Square");
if(buttonInput.Buttons & PSP_CTRL_TRIANGLE) printf("Triangle");
if(buttonInput.Buttons & PSP_CTRL_RTRIGGER) printf("R-Trigger");
if(buttonInput.Buttons & PSP_CTRL_LTRIGGER) printf("L-Trigger");
}
}
sceKernelExitGame();
return 0;
}
At the end we close our program. You may notice that we don't check for all buttons, this is because we need kernel control to check for those buttons. More on that in later tutorials or if you want to check how to get kernel control in your program, you can skip right to it.
Makefile
editOnce again create the 'Makefile' file and make it like so, the only difference being the names again.
TARGET = ButtonInput
OBJS = main.o ../common/callback.o
INCDIR =
CFLAGS = -O2 -G0 -Wall
CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti
ASFLAGS = $(CFLAGS)
BUILD_PRX = 1
LIBDIR = ./
LIBS = -lm
LDFLAGS =
EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = ButtonInput
PSPSDK = $(shell psp-config --pspsdk-path)
include $(PSPSDK)/lib/build.mak
Once you're done with this and everything is working, you can proceed to the next tutorial.