IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

SDL Discussion :

comment utiliser SDL_GetKeyboardState?


Sujet :

SDL

  1. #1
    Membre régulier Avatar de fifafou
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Janvier 2016
    Messages
    173
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 22
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Janvier 2016
    Messages : 173
    Points : 92
    Points
    92
    Par défaut comment utiliser SDL_GetKeyboardState?
    Bonjour,
    comment utiliser SDL_GetKeyboardState?
    Merci.

  2. #2
    Responsable 2D/3D/Jeux


    Avatar de LittleWhite
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Mai 2008
    Messages
    26 858
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Mai 2008
    Messages : 26 858
    Points : 218 577
    Points
    218 577
    Billets dans le blog
    120
    Par défaut
    Bonjour,

    D'abord :

    Qu'est ce qui vous bloque ?
    Vous souhaitez participer à la rubrique 2D/3D/Jeux ? Contactez-moi

    Ma page sur DVP
    Mon Portfolio

    Qui connaît l'erreur, connaît la solution.

  3. #3
    Membre régulier Avatar de fifafou
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Janvier 2016
    Messages
    173
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 22
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Janvier 2016
    Messages : 173
    Points : 92
    Points
    92
    Par défaut
    j'ai enfin réussi a utiliser cette fonction mais elle ne permet pas d'avoir un mouvement continu dès le début.
    faut-il créer soi-même le tableau ou on peut régler cette vitesse?
    j'ai vu aussi qu'il fallait faire une boucle d'evenement pour ça mais je n'ai pas reussi a en faire une.

  4. #4
    Responsable 2D/3D/Jeux


    Avatar de LittleWhite
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Mai 2008
    Messages
    26 858
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Mai 2008
    Messages : 26 858
    Points : 218 577
    Points
    218 577
    Billets dans le blog
    120
    Par défaut
    Vous pouvez trouver des réponses (je l'espère) dans ce tutoriel : http://alexandre-laurent.developpez....boucle-de-jeu/
    Sinon, vous n'utilisez pas correctement le tableau, si au début cela ne fonctionne pas correctement. Maintenant, sans voir le code, dur de comprendre la raison.
    Vous souhaitez participer à la rubrique 2D/3D/Jeux ? Contactez-moi

    Ma page sur DVP
    Mon Portfolio

    Qui connaît l'erreur, connaît la solution.

  5. #5
    Membre régulier Avatar de fifafou
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Janvier 2016
    Messages
    173
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 22
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Janvier 2016
    Messages : 173
    Points : 92
    Points
    92
    Par défaut
    Je voulais dire que cela avançait une fois et après un petit temps cela se répétait,ce qui n'est pas l'idéal dans un jeu
    et je crois qu'il n'y a pas d'équivalent du enablekeyrepeat de la SDL 1.0 pour regler ça
    mon code est
    Code juste pour tester : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    #include <SDL2/SDL.h>
    #include <stdio.h>
    int main (int argc, char *argv[])
    {
        SDL_Init (SDL_INIT_VIDEO);
     
        SDL_Window *screen;
        screen = SDL_CreateWindow("SDL2",SDL_WINDOWPOS_CENTERED,
                                  SDL_WINDOWPOS_CENTERED,
                                  640, 480, SDL_WINDOW_RESIZABLE);
        SDL_Surface* pSprite = SDL_LoadBMP("test.bmp");
        SDL_Rect dest = {0,0,0,0};
        SDL_BlitSurface(pSprite,NULL,SDL_GetWindowSurface(screen),&dest);
        SDL_UpdateWindowSurface(screen);
        const Uint8 *state = SDL_GetKeyboardState(NULL);
        SDL_Event event;
        bool continuer=1;
        int pos_x=0,pos_y=0;
        while(continuer)
        {
            if (SDL_WaitEvent(&event) )
            {
                switch(event.type)
                {
                    case SDL_WINDOWEVENT:
                        if ( event.window.event == SDL_WINDOWEVENT_CLOSE )
                        {
                            continuer=0;
                        }
                        break;
                    case SDL_KEYDOWN:
                        switch(event.key.keysym.sym)
                        {
                            case SDLK_ESCAPE:
                            {
                                continuer=0;
                                break;
                            }
                        }
                        break;
                }
            }
            if(state[SDL_SCANCODE_RIGHT])
                pos_x++;
            if(state[SDL_SCANCODE_LEFT])
                pos_x--;
            if(state[SDL_SCANCODE_UP])
                pos_y--;
            if(state[SDL_SCANCODE_DOWN])
                pos_y++;
            SDL_Rect dest = {pos_x,pos_y,0,0};
            SDL_BlitSurface(pSprite,NULL,SDL_GetWindowSurface(screen),&dest);
            SDL_UpdateWindowSurface(screen);
        }
        SDL_FreeSurface(pSprite);
        SDL_Quit();
        return 0;
    }

  6. #6
    Responsable 2D/3D/Jeux


    Avatar de LittleWhite
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Mai 2008
    Messages
    26 858
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Mai 2008
    Messages : 26 858
    Points : 218 577
    Points
    218 577
    Billets dans le blog
    120
    Par défaut
    Si vous utilisez SDL_GetKeyboardState() qui permet d'éviter d'utiliser le SDL_EnableKeyRepeat(), vous devez aussi arrêter d'utiliser les SDL_WaitEvent/SDL_PollEvent().
    De plus, vous devez utiliser SDL_GetKeyboardState() dans la boucle principale (enfin, il faut que je vérifie ce point). -> Vérifié et en effet, pas besoin de le mettre dans la boucle principale.
    Vous souhaitez participer à la rubrique 2D/3D/Jeux ? Contactez-moi

    Ma page sur DVP
    Mon Portfolio

    Qui connaît l'erreur, connaît la solution.

  7. #7
    Membre régulier Avatar de fifafou
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Janvier 2016
    Messages
    173
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 22
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Janvier 2016
    Messages : 173
    Points : 92
    Points
    92
    Par défaut
    mais comment surveiller la fermeture de la fenêtre et que la fenêtre réponde?
    j'ai essayé avec ça mais ça ne répond pas
    Code qui bug : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    #include <SDL2/SDL.h>
    #include <stdio.h>
    int main (int argc, char *argv[])
    {
        SDL_Init (SDL_INIT_VIDEO);
     
        SDL_Window *screen;
        screen = SDL_CreateWindow("SDL2",SDL_WINDOWPOS_CENTERED,
                                  SDL_WINDOWPOS_CENTERED,
                                  640, 480, SDL_WINDOW_RESIZABLE);
        SDL_Surface* pSprite = SDL_LoadBMP("test.bmp");
        SDL_Rect dest = {0,0,0,0};
        SDL_BlitSurface(pSprite,NULL,SDL_GetWindowSurface(screen),&dest);
        SDL_UpdateWindowSurface(screen);
        const Uint8 *state = SDL_GetKeyboardState(NULL);
        SDL_Event event;
        bool continuer=1;
        int pos_x=0,pos_y=0,tempsDebut;
        while(continuer)
        {
            tempsDebut = SDL_GetTicks();
            const Uint8 *state = SDL_GetKeyboardState(NULL);
            if(state[SDL_SCANCODE_RIGHT])
                pos_x++;
            if(state[SDL_SCANCODE_LEFT])
                pos_x--;
            if(state[SDL_SCANCODE_UP])
                pos_y--;
            if(state[SDL_SCANCODE_DOWN])
                pos_y++;
            SDL_Rect dest = {pos_x,pos_y,0,0};
            SDL_BlitSurface(pSprite,NULL,SDL_GetWindowSurface(screen),&dest);
            SDL_UpdateWindowSurface(screen);
            SDL_Delay(16 - (SDL_GetTicks() - tempsDebut));
        }
        SDL_FreeSurface(pSprite);
        SDL_Quit();
        return 0;
    }

  8. #8
    Responsable 2D/3D/Jeux


    Avatar de LittleWhite
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Mai 2008
    Messages
    26 858
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Mai 2008
    Messages : 26 858
    Points : 218 577
    Points
    218 577
    Billets dans le blog
    120
    Par défaut
    Pour cela, on peut utiliser SDL_SetEventFilter() et après, regarder si le callback reçoit un SDL SDL_QUIT.
    Vous souhaitez participer à la rubrique 2D/3D/Jeux ? Contactez-moi

    Ma page sur DVP
    Mon Portfolio

    Qui connaît l'erreur, connaît la solution.

  9. #9
    Membre régulier Avatar de fifafou
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Janvier 2016
    Messages
    173
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 22
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Janvier 2016
    Messages : 173
    Points : 92
    Points
    92
    Par défaut
    je n'ai pas réussi a surveiller la fermeture de la fenetre avec SDL_SetEventFilter,il n'y a pas autre méthode?

  10. #10
    Responsable 2D/3D/Jeux


    Avatar de LittleWhite
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Mai 2008
    Messages
    26 858
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Mai 2008
    Messages : 26 858
    Points : 218 577
    Points
    218 577
    Billets dans le blog
    120
    Par défaut
    Je pense que vous pouvez continuer avec SDL_PollEvent, uniquement pour la fenêtre. D’ailleurs en y repensant, votre problème de base venait sûrement du fait que vous utilisiez SDL_WaitEvent() (attente bloquante) au lieu de SDL_PollEvent().
    Vous souhaitez participer à la rubrique 2D/3D/Jeux ? Contactez-moi

    Ma page sur DVP
    Mon Portfolio

    Qui connaît l'erreur, connaît la solution.

  11. #11
    Membre régulier Avatar de fifafou
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Janvier 2016
    Messages
    173
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 22
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Janvier 2016
    Messages : 173
    Points : 92
    Points
    92
    Par défaut
    j'ai essayé de faire avec pollevent mais ça ne repond pas
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    #include <SDL2/SDL.h>
    #include <stdio.h>
    int main (int argc, char *argv[])
    {
        SDL_Init (SDL_INIT_VIDEO);
        SDL_Window *screen;
        screen = SDL_CreateWindow("SDL2",SDL_WINDOWPOS_CENTERED,
                                  SDL_WINDOWPOS_CENTERED,
                                  640, 480, SDL_WINDOW_RESIZABLE);
        SDL_Surface* pSprite = SDL_LoadBMP("test.bmp");
        SDL_Rect dest = {0,0,0,0};
        SDL_BlitSurface(pSprite,NULL,SDL_GetWindowSurface(screen),&dest);
        SDL_UpdateWindowSurface(screen);
        const Uint8 *state = SDL_GetKeyboardState(NULL);
        SDL_Event event;
        bool continuer=1;
        int pos_x=0,pos_y=0,tempsDebut;
        while(continuer)
        {
            tempsDebut = SDL_GetTicks();
            SDL_Event event;
            if ( SDL_PollEvent(&event) )
            {
                switch(event.type)
                {
                    case SDL_WINDOWEVENT:
                        if ( event.window.event == SDL_WINDOWEVENT_CLOSE )
                        {
                            continuer=0;
                        }
                        break;
                }
            }
            const Uint8 *state = SDL_GetKeyboardState(NULL);
            if(state[SDL_SCANCODE_RIGHT])
                pos_x++;
            if(state[SDL_SCANCODE_LEFT])
                pos_x--;
            if(state[SDL_SCANCODE_UP])
                pos_y--;
            if(state[SDL_SCANCODE_DOWN])
                pos_y++;
            SDL_Rect dest = {pos_x,pos_y,0,0};
            SDL_BlitSurface(pSprite,NULL,SDL_GetWindowSurface(screen),&dest);
            SDL_UpdateWindowSurface(screen);
            SDL_Delay(30 - (SDL_GetTicks() - tempsDebut));
        }
        SDL_FreeSurface(pSprite);
        SDL_Quit();
        return 0;
    }

  12. #12
    Membre régulier Avatar de fifafou
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Janvier 2016
    Messages
    173
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 22
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Janvier 2016
    Messages : 173
    Points : 92
    Points
    92
    Par défaut
    j'ai réessayé mais dès que je met un SDL_delay,ça ne repond pas.

  13. #13
    Responsable 2D/3D/Jeux


    Avatar de LittleWhite
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Mai 2008
    Messages
    26 858
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Mai 2008
    Messages : 26 858
    Points : 218 577
    Points
    218 577
    Billets dans le blog
    120
    Par défaut
    J'ai essayé votre dernier code, je n'ai remarqué aucun souci.
    Il y a des erreurs de codage (double déclaration de state, de dest, de event) mais cela ne gêne pas il me semble.
    Vous souhaitez participer à la rubrique 2D/3D/Jeux ? Contactez-moi

    Ma page sur DVP
    Mon Portfolio

    Qui connaît l'erreur, connaît la solution.

  14. #14
    Membre régulier Avatar de fifafou
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Janvier 2016
    Messages
    173
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 22
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Janvier 2016
    Messages : 173
    Points : 92
    Points
    92
    Par défaut
    ça doit etre Windows qui fait ça

Discussions similaires

  1. Réponses: 4
    Dernier message: 24/02/2009, 12h06
  2. Comment utiliser un cache ?
    Par TOM-Z dans le forum XMLRAD
    Réponses: 4
    Dernier message: 14/03/2003, 09h55
  3. comment utiliser actionscript ?
    Par webs dans le forum Flash
    Réponses: 3
    Dernier message: 09/02/2003, 23h11
  4. Comment utiliser OUT ?
    Par Bouziane Abderraouf dans le forum CORBA
    Réponses: 3
    Dernier message: 20/07/2002, 09h35
  5. Réponses: 5
    Dernier message: 11/06/2002, 15h21

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo