Partage
  • Partager sur Facebook
  • Partager sur Twitter

[SDL]Gérer deux axes simultanément

avec le joystick

Sujet résolu
    27 juillet 2006 à 20:55:51

    Bonsoir à tous,
    Voilà, le titre est assez explicite. En gros, je suis en train de coder un petit essai afin d'essayer l'utilisation du joystick avec la SDL. Jusque là, tout va bien, tout se charge parfaitement, le joy est reconnu et se charge sans prob. Le problème, c'est qu'avec l'utilisation proposée dans le cour, SDL ne gère qu'un seul axe à la fois.
    Exemple, je suis à fond à gauche avec le joy (mon object tourne vers la gauche, ok pas de soucis), mais dès que je tire un tout petit peu vers moi, la SDL ne prend plus en compte que ce dernier (et donc mon objet commence à redresser mais ne tourne plus vers la gauche même si je suis toujours à fond à gauche).
    Donc ma question est simple (mais la réponse ne le sera peut-être pas): comment gérer deux, en fait tous les axes simultanément? (je sais pas si c'est possible, d'autant plus que la SDL utilise la même variable pour stocker la valeur pour tous les axes je me demande bien comment on peut faire, mais parallèlement, ça me paraîtrait un peu idiot de ne pouvoir gérer qu'un seul axe.

    Le code de mon main (mais si ça sert à rien en fait ...)

    #include <stdlib.h>
    #include <math.h>
    #include <SDL/SDL.h>
    #include <GL/gl.h>
    #include <GL/glu.h>

    #include "struct.h"
    #include "rotation.h"

    void draw(Rotation rotate);


    ////////// fonction primaire //////////

    int main(int argc, char *argv[])
    {
        SDL_Event event;
        SDL_Joystick *joystick = NULL;
        Rotation rotate = {0.0 ,0.0 ,0.0};

        Uint32 avant;
        Uint32 now;
        Uint32 temps;

        SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);

        SDL_JoystickEventState(SDL_ENABLE);
        joystick = SDL_JoystickOpen(0);

        SDL_WM_SetCaption("Test de rotation", NULL);

        SDL_SetVideoMode(640, 480, 32, SDL_OPENGL);

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(70,(double)640/480,1,1000);
        glEnable(GL_DEPTH_TEST);

        char continuer = 1;

        avant = SDL_GetTicks();

        while(continuer)
        {
            avant = now;
            now = SDL_GetTicks();
            temps = now - avant;

            SDL_PollEvent(&event);
            switch(event.type)
            {
                case SDL_KEYDOWN:
                    switch(event.key.keysym.sym)
                    {
                        case SDLK_ESCAPE:
                            continuer = 0;
                        break;

                        case SDLK_UP:
                            RotateYp(temps, &rotate);
                        break;

                        case SDLK_DOWN:
                            RotateYn(temps, &rotate);
                        break;

                        case SDLK_RIGHT:
                            RotateXn(temps, &rotate);
                        break;

                        case SDLK_LEFT:
                            RotateXp(temps, &rotate);
                        break;

                        case SDLK_r:
                            rotate.x = 0.0;
                            rotate.y = 0.0;
                            rotate.z = 0.0;
                        break;
                    }
                break;

                case SDL_JOYAXISMOTION:
                    switch(event.jaxis.axis)
                    {
                        case 1:
                            if(event.jaxis.value < -4000)
                            {
                                RotateYp(temps, &rotate);
                            }
                            else if(event.jaxis.value > 4000)
                            {
                                RotateYn(temps, &rotate);
                            }
                        break;

                        case 0:
                            if(event.jaxis.value < -4000)
                            {
                                RotateXp(temps, &rotate);
                            }
                            else if(event.jaxis.value > 4000)
                            {
                                RotateXn(temps, &rotate);
                            }
                        break;

                        /* case 3:
                            if(event.jaxis.value < -4000)
                            {
                                RotateZn(temps, &rotate);
                            }
                            else if(event.jaxis.value > 4000)
                            {
                                RotateZp(temps, &rotate);
                            }
                        break; */

                    }
                break;

                case SDL_QUIT:
                    continuer = 0;
                break;
            }

        redresser(&rotate);

        draw(rotate);
        }

        SDL_JoystickClose(joystick);
        SDL_Quit();
        return 0;
    }

    ////////// fonctions secondaires //////////

    void draw(Rotation rotate)
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();

        gluLookAt(-5, 0, 5, 0, 0, 0, 0, 0, 1);

        glRotated(rotate.x , 0, 0, 1);
        glRotated(rotate.y , 0, 1, 0);
        glRotated(rotate.z , 1, 0, 0);

        glBegin(GL_QUADS);

        glColor3ub(255,0,0);
        glVertex3d(2,1,1);
        glVertex3d(2,1,-1);
        glVertex3d(-2,1,-1);
        glVertex3d(-2,1,1);

        glColor3ub(0,255,0);
        glVertex3d(2,-1,1);
        glVertex3d(2,-1,-1);
        glVertex3d(2,1,-1);
        glVertex3d(2,1,1);

        glColor3ub(0,0,255);
        glVertex3d(-2,-1,1);
        glVertex3d(-2,-1,-1);
        glVertex3d(2,-1,-1);
        glVertex3d(2,-1,1);

        glColor3ub(255,255,0);
        glVertex3d(-2,1,1);
        glVertex3d(-2,1,-1);
        glVertex3d(-2,-1,-1);
        glVertex3d(-2,-1,1);

        glColor3ub(0,255,255);
        glVertex3d(2,1,-1);
        glVertex3d(2,-1,-1);
        glVertex3d(-2,-1,-1);
        glVertex3d(-2,1,-1);

        glColor3ub(255,0,255);
        glVertex3d(2,-1,1);
        glVertex3d(2,1,1);
        glVertex3d(-2,1,1);
        glVertex3d(-2,-1,1);

        glEnd();

        glFlush();
        SDL_GL_SwapBuffers();
    }




    Voilà, merci d'avance pour vos réponse et bonne fin de soirée.

    EDIT: Après quelques recherches fructueuses sur le net, j'ai quand même trouvé le moyen de règler le problème.
    Voici le code pour ceux que ça intéresserait:
    int main(int argc, char *argv[])
    {
        SDL_Event event;
        SDL_Joystick *joystick = NULL;
        Rotation rotate = {0.0 ,0.0 ,0.0};
        char touches[6] = {0, 0, 0, 0, 0, 0};
        int axes[2] = {0, 0};

        Uint32 avant;
        Uint32 now;
        Uint32 temps;

        SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);

        SDL_JoystickEventState(SDL_ENABLE);
        joystick = SDL_JoystickOpen(0);

        SDL_WM_SetCaption("Test de rotation", NULL);

        SDL_SetVideoMode(640, 480, 32, SDL_OPENGL);

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(70,(double)640/480,1,1000);
        glEnable(GL_DEPTH_TEST);

        char continuer = 1;

        avant = SDL_GetTicks();
        fprintf(stderr, "état de la touche escape %ld\n", touches[ESCAPE]);
        while(continuer)
        {
            avant = now;
            now = SDL_GetTicks();
            temps = now - avant;

            SDL_PollEvent(&event);
            switch(event.type)
            {
                case SDL_KEYDOWN:
                    switch(event.key.keysym.sym)
                    {
                        case SDLK_ESCAPE:
                            touches[ESCAPE] = 1;
                        break;

                        case SDLK_UP:
                            touches[UP] = 1;
                        break;

                        case SDLK_DOWN:
                            touches[DOWN] = 1;
                        break;

                        case SDLK_LEFT:
                            touches[LEFT] = 1;
                        break;

                        case SDLK_RIGHT:
                            touches[RIGHT] = 1;
                        break;

                        case SDLK_r:
                            touches[R] = 1;
                        break;
                    }
                break;

                case SDL_KEYUP:
                    switch(event.key.keysym.sym)
                    {
                        case SDLK_ESCAPE:
                            touches[ESCAPE] = 0;
                        break;

                        case SDLK_UP:
                            touches[UP] = 0;
                        break;

                        case SDLK_DOWN:
                            touches[DOWN] = 0;
                        break;

                        case SDLK_LEFT:
                            touches[LEFT] = 0;
                        break;

                        case SDLK_RIGHT:
                            touches[RIGHT] = 0;
                        break;

                        case SDLK_r:
                            touches[R] = 0;
                        break;
                    }
                break;

                case SDL_JOYAXISMOTION:
                    axes[event.jaxis.axis] = event.jaxis.value;
                break;

                case SDL_QUIT:
                    continuer = 0;
                break;
            }
        if(touches[ESCAPE] == 1) continuer = 0;
        if(touches[UP] == 1) RotateYp(temps, &rotate);
        if(touches[DOWN] == 1) RotateYn(temps, &rotate);
        if(touches[LEFT] == 1) RotateXp(temps, &rotate);
        if(touches[RIGHT] == 1) RotateXn(temps, &rotate);
        if(touches[R] == 1) {rotate.x = 0.0;
                            rotate.y = 0.0;
                            rotate.z = 0.0;}

        if(axes[LACET] < -4000) RotateXp(temps, &rotate);
        if(axes[LACET] > 4000) RotateXn(temps, &rotate);

        if(axes[TANGAGE] < -4000) RotateYp(temps, &rotate);
        if(axes[TANGAGE] > 4000) RotateYn(temps, &rotate);

        redresser(&rotate);

        draw(rotate);
        }

        SDL_JoystickClose(joystick);
        SDL_Quit();
        return 0;
    }

    Voilà, et désolé du dérangement.
    • Partager sur Facebook
    • Partager sur Twitter

    [SDL]Gérer deux axes simultanément

    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
    × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
    • Editeur
    • Markdown