Fr:SDL Joystick mapping

From wiki.gp2x.org

Contents

Les évènements

Dans la SDL et PyGame, chaque action produit un évènement de type "bouton" (SDL_JOYBUTTONUP, SDL_JOYBUTTONDOWN), y compris le joystick, qui ne produit pas d'évenement joystick en particulier. Chaque action est comptée comme l'appui d'un bouton. Appuyer et relâcher un bouton déclenchent respectivement un évènement SDL_JOYBUTTONDOWN et un évènement SDL_JOYBUTTONUP.

... au lieu d'évènements "continus" lorsqu'un bouton ou le joystick ne sont pas dans leur état normal. En fait, la SDL n'a pas d'évènement pour dire "le bouton est encore appuyé". Pour gérer cela, on utilise une pile dévènement en considérant que tant que l'on n'a pas reçu l'évènement "bouton relaché", c'est que celui-ci est reste appuyé.

=== Joystick ===
Visual Mapping

Le joystick a 8 directions. Si vous le maintenez entre une diagonale et une autre direction (par exemple, entre gauche et haut-gauche), vous pouvez obtenir les deux évènements de bouton simultanément. Voir Suggested Joystick Configurations pour "mapper" les directions sur le joystick.

Joystick Direction Event Value
Up 0
Down 4
Left 2
Right 6
Up-Left 1
Up-Right 7
Down-Left 3
Down-Right 5
Stick "Click" 18

Other Buttons

Il est très recommandé de se référer à la Common User Interface Recommendations pour mapper les actions de votre jeu, application ou émulateur.

Button Event Value
A 12
B 13
Y 14
X 15
R 10
L 11
Start 8
Select 9
Vol - 17
Vol + 16

Note: These mappings are assuming you are using Guyfawkes' library. X/Y and L/R may be the other way around. See the talk page.

Implementation

  • Lorsque vous appelez SDL_Init, assurez-vous d'inclure SDL_INIT_JOYSTICK en paramètre.
  • Il faut appeler SDL_JoystickOpen(0)
  • Dans votre boucle d'évènements, vérifiez tant SDL_JOYBUTTONDOWN que SDL_JOYBUTTONUP events
  • Comparez event.jbutton.button au constantes ci-dessus
  • Parfois, vous pourrez recevoir un évènement SDL_JOYBUTTONDOWN initial, du au boutton appuyé pour lancer l'application (habituellement, le bouton B). Cela dépend du temps d'exécution avant le lancement de SDL_Init() et combien de temps le bouton a été appuyé.

Examples

C

/* initialize */
SDL_Init(SDL_INIT_JOYSTICK|SDL_INIT_VIDEO);
SDL_JoystickOpen(0);

SDL_Event event;

/* event loop */
while(SDL_PollEvent(&event))
{
    switch(event.type)
    {
        case SDL_JOYBUTTONDOWN:
            switch(event.jbutton.button)
            {
                case GP2X_BUTTON_X: /* X pressed */
                    printf("X was pressed\n");
                    break;
                case GP2X_BUTTON_L: /* L pressed */
                    printf("L was pressed\n");
                    break;
            }
            
            break;
        case SDL_JOYBUTTONUP:
            switch(event.jbutton.button)
            {
                case GP2X_BUTTON_X: /* X released */
                    printf("X was released\n");
                    break;
                case GP2X_BUTTON_L: /* L released */
                    printf("L was released\n");
                    break;
            }
            
            break;
    }
}

Python

import gp2x

# initialize
num_joysticks = pygame.joystick.get_count()
if num_joysticks > 0:
    stick = pygame.joystick.Joystick(0)
    stick.init() # now we will receive events for the joystick

# event loop
while 1:
    event = pygame.event.poll()
    print "got event"
    print repr(event)
    if event.type == pygame.NOEVENT:
        print "No more events for now"
        break
    if event.type == pygame.QUIT:
        print "got quit event"
        running = 0
    elif event.type == pygame.JOYBUTTONDOWN:
        if event.button == gp2x.BUTTON_X:
            print "X pressed"
        elif event.button == gp2x.BUTTON_Y:
            print "Y pressed"
        else:
            print "unknown button pushed"
    elif event.type == pygame.JOYBUTTONUP:
        if event.button == gp2x.BUTTON_X:
            print "X released"
        elif event.button == gp2x.BUTTON_Y:
            print "Y released"
        else:
            print "unknown button released"

Headers

C

#define GP2X_BUTTON_UP              (0)
#define GP2X_BUTTON_DOWN            (4)
#define GP2X_BUTTON_LEFT            (2)
#define GP2X_BUTTON_RIGHT           (6)
#define GP2X_BUTTON_UPLEFT          (1)
#define GP2X_BUTTON_UPRIGHT         (7)
#define GP2X_BUTTON_DOWNLEFT        (3)
#define GP2X_BUTTON_DOWNRIGHT       (5)
#define GP2X_BUTTON_CLICK           (18)
#define GP2X_BUTTON_A               (12)
#define GP2X_BUTTON_B               (13)
#define GP2X_BUTTON_X               (14)
#define GP2X_BUTTON_Y               (15)
#define GP2X_BUTTON_L               (10)
#define GP2X_BUTTON_R               (11)
#define GP2X_BUTTON_START           (8)
#define GP2X_BUTTON_SELECT          (9)
#define GP2X_BUTTON_VOLUP           (16)
#define GP2X_BUTTON_VOLDOWN         (17)

Python

# GP2X joystick button mappings
BUTTON_UP            = (0)
BUTTON_DOWN          = (4)
BUTTON_LEFT          = (2)
BUTTON_RIGHT         = (6)
BUTTON_UPLEFT        = (1)
BUTTON_UPRIGHT       = (7)
BUTTON_DOWNLEFT      = (3)
BUTTON_DOWNRIGHT     = (5)
BUTTON_CLICK         = (18)
BUTTON_A             = (12)
BUTTON_B             = (13)
BUTTON_X             = (14)
BUTTON_Y             = (15)
BUTTON_L             = (10)
BUTTON_R             = (11)
BUTTON_START         = (8) 
BUTTON_SELECT        = (9)
BUTTON_VOLUP         = (16)
BUTTON_VOLDOWN       = (17)
Personal tools