Programming for PSP: Part II. Total control

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

    Hurrah! Subscribe to my PSP programming blog .

    Well and yet, this is a separate manifesto: if you find a mistake in the topic, you can shout “cheers” or be proud of it. The best thing you can do is to write me about it in a personal email (this can be done easily by clicking “add user as friend” and writing about the error in the “introduce yourself” field. If you are disgusted to see me in your friends list, write and I will not click on the button "allow add"). Please do not need to assert oneself of the errors found in the comments, because there is absolutely no benefit from this, and scrolling through such comments in search of really something interesting is annoying.

    Let's create a project according to the template described in the first part. We 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 inside the program, because the output is still carried out by the HOME button, and only so.

    Let's make a standard message processing cycle: receiving and digesting. The first thing to do is to determine the variable in which the keyboard state will be stored:
    SceCtrlData pad; // variable where the information about the state of the buttons is stored (pressed / not pressed).

    The SceCtrlData structure stores TimeStamp — the number of frames, it seems, from the program’s launch (it’s necessary to clarify!), Buttons — the bit mask of the button state (the Select, Start, cross, triangle, square, shifters, arrows, Hold and Home state are taken into account. All 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 have to add a file to the beginning
    #include [pspctrl.h]

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

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

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

    Otherwise, the joystick will freeze at the position (128: 128) (this is the middle, the coordinates themselves vary from 0 to 255).
    So, we read the information, now we need to display it. Here's what I got:
    pspDebugScreenPrintf ("TimeStamp% i | Pressed:", pad.TimeStamp); // display TimeStamp
    if (pad.Buttons == 0) // nothing 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);
    // print the coordinates of the stick

    As you can see, I only take into account the state of the buttons with pictures (well, a triangle, a circle, a square and a cross), it’s easy to add support for the rest, for 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 received code (the password is Habrahabr), convert to EBOOT, drop it on the console and enjoy. As homework, you can try to do a walker, or some other incredibly useful thing; I think I will devote the third part to all other features of text mode, and then move on to the graphics. Good luck

    Also popular now: