Sunday, July 12, 2020

Basics on the code PART 2

Today we are going to review our second sample, which is going to be "ctrl". Because it is about the console keypad and remote controller status, a.k.a. Buttons.

In this sample, the only difference with the "hello_world" one, is the main.c file, that is why are only focusing on that.

The goal of this sample is knowing the functions that control the buttons of the console, which are going to be very important if we want to make our app interactive.

To understand the functions we have to read the vitasdk documentation, for this specific sample we will use the control section

SAMPLE "ctrl"

 ctrl: A minimal controller (button) sample.

// headers and other required files which need to be linked for the homebrew to work

#include <stdio.h>
#include <psp2/kernel/processmgr.h>

#include <psp2/ctrl.h>

#include "debugScreen.h"

#define printf psvDebugScreenPrintf

// main program

int main(int argc, char *argv[]) {

    psvDebugScreenInit();  // needed for the prints to show on the vita screen.

    printf("input test\n");

    printf("press Select+Start+L+R to stop\n");

    /* to enable analog sampling */
    sceCtrlSetSamplingMode(SCE_CTRL_MODE_ANALOG);  // Set the controller mode.
  
    SceCtrlData ctrl; // Returned controller data.

    const char* btn_label[]={"SELECT ","","","START ",
        "UP ","RIGHT ","DOWN ","LEFT ","L ","R ","","",
        "TRIANGLE ","CIRCLE ","CROSS ","SQUARE "};
    do{
        sceCtrlPeekBufferPositive(0, &ctrl, 1); // Get the controller state information (polling, positive logic).
        printf("Buttons:%08X == ", ctrl.buttons);
        int i;
        for(i=0; i < sizeof(btn_label)/sizeof(*btn_label); i++){
            printf("\e[9%im%s",(ctrl.buttons & (1<<i)) ? 7 : 0, btn_label[i]);
        }
        printf("\e[m Stick:[%3i:%3i][%3i:%3i]\r", ctrl.lx,ctrl.ly, ctrl.rx,ctrl.ry); // print analogue stick values

    }while(ctrl.buttons != (SCE_CTRL_START | SCE_CTRL_SELECT | SCE_CTRL_LTRIGGER | SCE_CTRL_RTRIGGER) );   // as the printed line at the begining says, it defines a way to exit from the input mode


    sceKernelExitProcess(0);  // close the vita proccess runing, usually close the app

    return 0;
}

-----------

Some screenshots from the sample running on the vita:

No button pressed

Down button pressed

Start and Up button pressed

Triangle button pressed



And that's all for this sample.

No comments:

Post a Comment