[joystick] pooling

hello,
I use joystick pooling to get key,
and I found that my pooling function only can run once,
It means I can only press key one time and finished but I run pooling
function twice.
how I fix that?
thank you~

Here is my code:
#include <SDL/SDL.h> /* All SDL App’s need this */
#include

using namespace std;

class Joystick
{
//???
SDL_Joystick g_pJoy;
public:
Joystick()
{
cout << “Initializing SDL.”<< endl;
/
Initialize defaults, Video and Audio */
if((SDL_Init(SDL_INIT_JOYSTICK |
SDL_INIT_VIDEO|SDL_INIT_AUDIO)==-1)) {
cout << (“Could not initialize SDL: %s.\n”, SDL_GetError());
exit(-1);
}

    cout <<"SDL initialized."<< endl;

    //??Joy Stick??????,?????0?????
    if (SDL_NumJoysticks() > 0)
    {
        //??JoyStick??,#0???????,???/dev/input/js0
        cout << "open joystick!" << endl;
        g_pJoy = SDL_JoystickOpen(0);
        if (g_pJoy != NULL)
        {
            //??JoyStick????
            SDL_JoystickEventState(SDL_ENABLE);
        }
    }
    else
    {
        //?????Joystick???????SDL???
        cout << "No joystick!" << endl;
        /* Shutdown all subsystems */
        cout <<"Quiting SDL"<< endl;
        SDL_Quit();
    }//end of if (SDL_NumJoysticks() > 0)
}
void pooling()
{
    SDL_Event test_event;
    //???joystick????
    while(!SDL_PollEvent(&test_event)) {}
    switch(test_event.type)
    {
    case SDL_JOYBUTTONDOWN:
        //??????,++???????????1????API?0??
        cout << "pressed : " << (int)++test_event.jbutton.button<< endl;
        break;
    default:
        break;
    }
}
void close()
{
    //??Joy Stick??
    cout <<"Close Joystick."<< endl;
    if(g_pJoy != NULL)
        SDL_JoystickClose(g_pJoy);
    g_pJoy = NULL;

    /* Shutdown all subsystems */
    cout <<"Quiting SDL"<< endl;
    SDL_Quit();

}

};

int main(int argc, char *argv[])
{
Joystick joy;
joy.pooling();
//could not work again…
joy.pooling();
joy.close();
}

You are probably catching the SDL_JOYBUTTONUP event on the second call
to pooling().

Jonny D

2011/1/15 sam :> hello,

I use joystick pooling to get key,
and I found that my pooling function only can run once,
It means I can only press key one time and finished but I run pooling
function twice.
how I fix that?
thank you~

Here is my code:
#include <SDL/SDL.h> /* All SDL App’s need this */
#include

using namespace std;

class Joystick
{
//???
SDL_Joystick g_pJoy;
public:
Joystick()
{
cout << “Initializing SDL.”<< endl;
/
Initialize defaults, Video and Audio */
if((SDL_Init(SDL_INIT_JOYSTICK |
SDL_INIT_VIDEO|SDL_INIT_AUDIO)==-1)) {
cout << (“Could not initialize SDL: %s.\n”, SDL_GetError());
exit(-1);
}

    cout <<"SDL initialized."<< endl;

    //??Joy Stick??????,?????0?????
    if (SDL_NumJoysticks() > 0)
    {
        //??JoyStick??,#0???????,???/dev/input/js0
        cout << "open joystick!" << endl;
        g_pJoy = SDL_JoystickOpen(0);
        if (g_pJoy != NULL)
        {
            //??JoyStick????
            SDL_JoystickEventState(SDL_ENABLE);
        }
    }
    else
    {
        //?????Joystick???????SDL???
        cout << "No joystick!" << endl;
        /* Shutdown all subsystems */
        cout <<"Quiting SDL"<< endl;
        SDL_Quit();
    }//end of if (SDL_NumJoysticks() > 0)
}
void pooling()
{
    SDL_Event test_event;
    //???joystick????
    while(!SDL_PollEvent(&test_event)) {}
    switch(test_event.type)
    {
    case SDL_JOYBUTTONDOWN:
        //??????,++???????????1????API?0??
        cout << "pressed : " << (int)++test_event.jbutton.button<< endl;
        break;
    default:
        break;
    }
}
void close()
{
    //??Joy Stick??
    cout <<"Close Joystick."<< endl;
    if(g_pJoy != NULL)
        SDL_JoystickClose(g_pJoy);
    g_pJoy = NULL;

    /* Shutdown all subsystems */
    cout <<"Quiting SDL"<< endl;
    SDL_Quit();

}

};

int main(int argc, char *argv[])
{
Joystick joy;
joy.pooling();
//could not work again…
joy.pooling();
joy.close();
}


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Johnny D is absolutely correct.------------------------
EM3 Nathaniel Fries, U.S. Navy

http://natefries.net/