2

So, I'm trying to learn how to use joysticks with SDL2, and I'm following LazyFoo's Tutorial(Gamepads and Joysticks), but I must've been doing something wrong because my program won't detect my controller!

Windows detects it(I'm using Windos 7 and a wireless Xbox 360 controller) and LazyFoo's example(which I downloaded from the link above) also detects my controller!

I searched and tried some things, but nothing that I tried seems to work...

Here's my Init:

if (SDL_Init(SDL_INIT_EVERYTHING) < 0){
    cout << "Error initializing SDL!" << endl;
    return 1;
}

And this part is inside the constructor of my Input class, whic was supposed to "start" the joystick(and also detects event from the keyborad):

if (SDL_NumJoysticks() < 1){
    cout << "No joystick detected." << endl;
}
else{
    controller = SDL_JoystickOpen(0);
    if (controller == NULL){
        cout << "Error: Unable to open Joystick." << endl;
    }
    else{
        isUsingJoystick = true;
    }
}

I also tried using SDL_JoystickEventState(SDL_ENABLE) after the Init and SDL_JoystickUpdate(), but it did'nt worked either...

What am I forgetting?

If you guys want, I can edit the post with the codes of my Input class...

EDIT: I forgot to say that, even though my controller is connected and everything, SDL_NumJoysticks() returns 0...

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
Deive_Ex
  • 83
  • 2
  • 9
  • Does the second part of the code work?, do you read the events correctly? – this Jun 20 '14 at 03:30
  • @this I don't think so... I can read events from the keyboard, but not from the Joystick. Also, as I edited in the post, `SDL_NumJoysticks()` returns 0, so it's like there's no controller plugged... – Deive_Ex Jun 20 '14 at 04:32
  • Please don't add "[SOLVED]" to the title. The way to indicate that the problem is solved is to accept an answer. (It's perfectly acceptable to accent your own answer.) – Keith Thompson Jun 22 '14 at 18:54

1 Answers1

2

I figured out what I was doing wrong. It turns out that I was trying to open my joystick before initializing SDL itself (I was opening my Joystick in the constructor of my Input class, but I was creating the one and only instance of my Input class before SDL_INIT_EVERYTHING.)

I think it would be impossible to discover this only with the information I wrote, so sorry for bothering.

Tot Zam
  • 8,406
  • 10
  • 51
  • 76
Deive_Ex
  • 83
  • 2
  • 9