[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] pygame.Joystick Broken in 1.8.1?



It's definitely SDL. I made a demo that checks for hats, hat presses, and button presses. Output:

Name of joystick: Logitech Logitech Dual Action
Number of hats: 0
Number of buttons: 12
Number of balls: 0
1 was released
2 was released
1 was pressed
1 was released
2 was pressed
2 was released

Here's the code:

#include "SDL/SDL.h"

int main() {
    SDL_Init(SDL_INIT_JOYSTICK|SDL_INIT_VIDEO);
    SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
    SDL_Joystick *joy;
    joy = SDL_JoystickOpen(0);
    printf("Name of joystick: %s\n", SDL_JoystickName(0));
    printf("Number of hats: %d\n", SDL_JoystickNumHats(joy));
    printf("Number of buttons: %d\n", SDL_JoystickNumButtons(joy));
    printf("Number of balls: %d\n", SDL_JoystickNumBalls(joy));

    SDL_Event event;
    Uint8 hat_state;

    int running = 1;
    while( running ) {
       
        SDL_JoystickUpdate();
       
        hat_state = SDL_JoystickGetHat(0, 0);
        if( hat_state == SDL_HAT_UP )
            printf("Hat is pointing up");
       
        while(SDL_PollEvent(&event)) {
            switch(event.type) {
                case SDL_QUIT:
                    running = 0;
                    break;
                case SDL_KEYDOWN:
                    switch(event.key.keysym.sym) {
                        case SDLK_ESCAPE:
                            running = 0;
                            break;
                        default:
                            break;
                    }
                   
                    break;
                case SDL_JOYBUTTONDOWN:
                    switch(event.jbutton.button) {
                        case 1:
                            printf("1 was pressed\n");
                            break;
                        case 2:
                            printf("2 was pressed\n");
                            break;
                    }
                   
                    break;
                case SDL_JOYBUTTONUP:
                    switch(event.jbutton.button) {
                        case 1:
                            printf("1 was released\n");
                            break;
                        case 2:
                            printf("2 was released\n");
                            break;
                    }
                   
                    break;
            }
        }
    }
   
    SDL_Quit();
    return 1;
}


--
- pymike