📜 ⬆️ ⬇️

Programming for PSP: Part II. Total control

In the past, the tutorial we dealt with “Hellowhold”, now it's time for the next, no less important thing: entering information. Knowing how to display something on the screen and find out what the user clicked, you can already make a useful program. In fact, after this lesson it will be possible to start something different, but I would advise you to wait, at least, for sprites.

Hooray! Subscribe to my PSP programming blog .

And yet, this is already a separate manifesto: if you find a mistake in the topic, you can shout "Hurray" or be proud of it. The best thing you can do is write to me in person (this can be done easily by clicking “add user as a friend” and writing about the error in the “introduce yourself” field. If you dislike seeing me in the list of friends, write and I will not press the "allow add" button). I don’t need to approve myself of the errors found in the comments, because there is absolutely no benefit from this, and it is annoying to scroll through such comments in search of something really interesting.

Let's create the project on the template described in the first part . Let's find in the file the main () function - the main function of the program, and in it the main loop (while (1) {...}). By the way, about while (1): in programming for psp, it is not considered bad form to do infinite loops within a program, since the output is still carried out by the HOME button, and only this way.
')
Let's make a standard message processing loop: getting and digesting. The first thing to do is to define a variable in which the keyboard state will be stored:
SceCtrlData pad; // variable where button status information is stored (pressed / not pressed).

The structure of SceCtrlData stores TimeStamp in itself - the number of frames, it seems, from the start of the program (need to be clarified!) the rest is taken into account only in Kernel Mode, for details see the file devkitPSP \ psp \ sdk \ include \ pspctrl.h) and the coordinates of the stick Lx and Ly. Since it is defined only in pspctrl.h, you will have to add to the beginning of the file
#include [pspctrl.h]

(Instead of [], put angle brackets. Habr for some reason converts <and> into angle brackets, which confuses the browser)

So, erase all the insides while while, and write
sceCtrlReadBufferPositive (& pad, 1); // read the contents of the uh keyboard buffer

This function fills the pad structure with the current state of the buttons and the stick. By the way, in order to get the coordinates of the stick it was possible, you need to cram 2 lines before the while:
sceCtrlSetSamplingCycle (0);
sceCtrlSetSamplingMode (PSP_CTRL_MODE_ANALOG); // set up to receive information about the position of the stick

Otherwise, the joystick will harden in position (128: 128) (this is the middle, the coordinates themselves vary from 0 to 255).
So, we considered the information, now we need to display it. That's what happened with me:
pspDebugScreenPrintf ("TimeStamp% i | Pressed:", pad.TimeStamp); // output TimeStamp
if (pad.Buttons == 0) // nothing is pressed
{
pspDebugScreenPrintf ("NONE");
} else {
if (pad.Buttons & PSP_CTRL_TRIANGLE) pspDebugScreenPrintf (“TRIANGLE„);
if (pad.Buttons & PSP_CTRL_CIRCLE) pspDebugScreenPrintf (“CIRCLE„);
if (pad.Buttons & PSP_CTRL_CROSS) pspDebugScreenPrintf (“CROSS„);
if (pad.Buttons & PSP_CTRL_SQUARE) pspDebugScreenPrintf (“SQUARE„);
// which buttons are pressed?
}
pspDebugScreenPrintf (“(% i) | Stick:% i:% i \ n”, pad.Buttons, pad.Lx, pad.Ly);
// display stick coordinates

As you can see, I consider only the state of the buttons with pictures (well, a triangle, a circle, a square and a cross), it is easy to add support for the rest, for devstats contact devkitPSP \ psp \ sdk \ include \ pspctrl.h. The code in the loop will output something like this:
TimeStamp 68832112 | Pressed NONE (0) | Stick: 126: 150
TimeStamp 72317982 | Pressed TRIANGLE CIRCLE (12288) | Stick: 119: 167


So, compile all the resulting code (password - habrakhabr), convert to EBOOT, throw on the console and enjoy. As a homework, you can try to make a walker, or some other incredibly useful thing; I think that I will devote the third part to any other text mode features, and then move on to the graph. Good luck!

Source: https://habr.com/ru/post/23022/


All Articles