Khởi tạo SDL_JoyStick như thế nào?

Em đã thử theo cách người ta bày như sau

bool init() {

    //Initialize what we need
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
        printf("SDL could not initialized! Error: %s\n", SDL_GetError());
        return false;
    }

    .........
    .........
    .........

    SDL_JoystickEventState(SDL_ENABLE);
    
    //Check for joystick subsystem
    if (SDL_NumJoysticks() <= 0) { // <<<<<<< get stuck in here
        printf("Warning: No joysticks connected\n");
    }
    else {
        //Load joysticks subsystem
        controller = SDL_JoystickOpen(0);
        if (controller == NULL) {
            printf("Warning: Unable to open game controller! Error: %s\n", SDL_GetError());
            return false;
        }
    }
    
    .......
    .......
    .......
    
    return true;
}

Khi chạy đến phần kiểm tra số lượng joystick khởi tạo được

if (SDL_NumJoysticks() <= 0) {
        printf("Warning: No joysticks connected\n");
}

thì nhận được thông báo “No joysticks connected”.

Vậy có nghĩa là Joystick subsystem vẫn được khởi tạo nhưng không tạo ra đc joystick nào.

Em nên khởi tạo thế nào cho đúng?

1 Like

À chắc là do làm trên laptop nên ko có joystick nào (giống như tay cầm xbox 360) kết nối vào, nên mới không có kết nối.

Có lẽ nên chuyển qua dùng SDL_GameController

1 Like
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?