Librairie SDL
Résolu
poparnassus
Messages postés
426
Date d'inscription
Statut
Membre
Dernière intervention
-
poparnassus Messages postés 426 Date d'inscription Statut Membre Dernière intervention -
poparnassus Messages postés 426 Date d'inscription Statut Membre Dernière intervention -
Bonjour,
Je solicite votre aide, car je ne comprends pas ce code:
*
source du code:
https://openclassrooms.com/courses/la-gestion-du-joystick-avec-la-sdl
*
*
Un post que j'ai ouvert sur openclassroom mais quyi reste sans reponce...
https://openclassrooms.com/fr/forum/sujet/utiliser-la-librairie-sdl-pour-piloter-un-robot
*
*
mon fichier imput.h:
mon fichier event.cpp:
_______________________________________________
mon fichier main.cpp:
Lorsque je compile mon main.cpp j'ai ce message d'errreur,
||=== Build: Debug in 6wd_openclass (compiler: GNU GCC Compiler) ===|
C:\Users\Nico\Desktop\Projet C\6wd_openclass\main.cpp||In function 'int SDL_main(int, char**)':|
C:\Users\Nico\Desktop\Projet C\6wd_openclass\main.cpp|9|error: 'Input' was not declared in this scope|
C:\Users\Nico\Desktop\Projet C\6wd_openclass\main.cpp|9|error: expected ';' before 'input'|
C:\Users\Nico\Desktop\Projet C\6wd_openclass\main.cpp|10|error: 'input' was not declared in this scope|
C:\Users\Nico\Desktop\Projet C\6wd_openclass\main.cpp|10|error: 'initialiserInput' was not declared in this scope|
C:\Users\Nico\Desktop\Projet C\6wd_openclass\main.cpp|18|error: 'updateEvent' was not declared in this scope|
C:\Users\Nico\Desktop\Projet C\6wd_openclass\main.cpp|26|error: 'detruireInput' was not declared in this scope|
||=== Build failed: 6 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
----------------------------------------------------
j'aimerai bien avoir des explication, je suis debutant, je ne maitrise absolument pas le C ni la programmation.
Dans ce code il y a des partie que je n'utiliserai pas tel que la sourie et le clavier mise a par la touche ECHAP pour quitter le programme, et je n'utilise qu'un seul joystick. J'ai appris a me servir du C a partir de ce post(lien au debut du post).
Le but finale de ce code sera de recuperer mes evenement joystick uniquement, d'afficher mes valeur des axe et boutton joysticj dans un tableau dans la fentre que SDL genere,et ensuite de pouvoir les envoyers sur le reseau à un robot (sur un raspberry pi B+), donc si j'ai bien compris, ce code m'alloue mes valeurs de joystick dans la memoire vive, donc par la suite je devrai les envoyers sur le reseau mais ceci sera l'etape suivante ^^
question:
Es la bonne méthode utiliser (donc la libraire SDL et le language C) pour piloter mon robot avec un joystick depuis mon ordi portable via le wifi ?
question en rapport au code:
1) Es que mes fichier main, event et imput on la bonne extension ?
2) Es que les directive de préprocesseur sont bonne ?
3) Peut etre dois -je inclure dans mon main.cpp mes fichier event.cpp et imput.h ?
si vous lisez mon post:
https://openclassrooms.com/forum/sujet/utiliser-la-librairie-sdl-pour-piloter-un-robot
je souhaitai recuperer mes valeur dans un fichier.txt, et de les envoyer a laide dun batch sur le reseau, probleme que j'ai eu, impossible d'ouvrir se fichier .txt tant que l'application SDL l'utilisé, donc j'ai chercher une autre facon de faire et me voila ici avec mes 3 fichier main.cpp , event.cpp et imput.h mais qui ne compile pâs.
Merci a celui qui eclairera ma lanterne !!!
Je solicite votre aide, car je ne comprends pas ce code:
*
source du code:
https://openclassrooms.com/courses/la-gestion-du-joystick-avec-la-sdl
*
*
Un post que j'ai ouvert sur openclassroom mais quyi reste sans reponce...
https://openclassrooms.com/fr/forum/sujet/utiliser-la-librairie-sdl-pour-piloter-un-robot
*
*
mon fichier imput.h:
#ifndef EVENT_H_INCLUDED #define EVENT_H_INCLUDED #include <SDL/SDL.h> /* structure qui gère un joystick */ typedef struct InputJoystick InputJoystick; struct InputJoystick { SDL_Joystick *joystick; char *boutons; int *axes; int *chapeaux; int numero; }; /* structure qui gère une souris */ typedef struct InputSouris InputSouris; struct InputSouris { int *boutons; int x; int y; int xrel; int yrel; }; /* la structure qui gère les événements */ typedef struct Input Input; struct Input { char *touches; // on alloue les touches par la suite ... int quitter; // il est géré automatiquement mais peut être demandé à être mis à 1 InputSouris souris; // une souris InputJoystick *joystick; // pointeur car il peut y avoir plusieurs joysticks int nombreJoysticks; // sert à détruire à la fin }; void initialiserInput(Input *input,int utiliserJoystick1,int numeroJoystick1,int utiliserJoystick2,int numeroJoystick2); void detruireInput(Input *input); // fonction qui libère la [/download/telecharger-34085110-memoire mémoire] allouée void updateEvent(Input *input); // fonction qui récupère les événements #endif // EVENT_H_INCLUDED
mon fichier event.cpp:
#include <sdtlib.h> #include <SDL/SDL.h> #include "Input.h" // mettez le nom du fichier où est la structure et ses fonctions void initialiserInput(Input *input,int numeroJoystick) { if(numeroJoystick < SDL_NumJoysticks()) // on vérifie qu'il y a bien un bon numéro de joystick { input->joystick = SDL_JoystickOpen(numeroJoystick); // on met le joystick à numéro correspondant input->numero = numeroJoystick; // je pense que vous comprenez cette ligne... /* on alloue chaque éléments en fonctions de combien il y en a */ input->boutons = (char*) malloc(SDL_JoystickNumButtons(input->joystick) * sizeof(char)); input->axes = (int*) malloc(SDL_JoystickNumAxis(input->joystick) * sizeof(int)); input->chapeaux = (int*) malloc(SDL_JoystickNumHats(input->joystick) * sizeof(int)); input->trackballs = (InputTrackball*) malloc(SDL_JoystickNumBalls(input->joystick) * sizeof(InputTrackball)); for(int i=0;i<SDL_JoystickNumButtons(input->joystick);i++) // tant qu'on a pas atteint le nombre max de boutons input->boutons[i] = 0; // on met les valeurs à 0 for(int i=0;i<SDL_JoystickNumAxes(input->joystick);i++) // tant qu'on a pas atteint le nombre max d'axes input->axes[i] = 0; // on met aussi les valeurs à 0 for(int i=0;i<SDL_JoystickNumHats(input->joystick);i++) // tant qu'on a pas atteint le nombre max de chapeaux input->chapeaux[i] = SDL_HAT_CENTERED; // on dit que les chapeaux son centrés for(int i=0;i<SDL_JoystickNumBalls(input->joystick);i++) // tant qu'il y a des trackballs { // on met à zéro input->trackballs[i]->xrel = 0; input->trackballs[i]->yrel = 0; } } else { // si le numéro du joystick n'était pas correct // on met tout à NULL input->joystick = NULL; input->boutons = NULL; input->axes = NULL; input->chapeaux = NULL; input->trackballs = NULL; } } void detruireInput(Input *input) { if(input->joystick != NULL) // on vérifie que le joystick existe bien { input->numero = 0; // on le remet à zéro // on libère tout free(input->boutons); free(input->axes); free(input->chapeaux); free(input->trackballs); SDL_JoystickClose(input->joystick); } } void updateEvent(Input *input) { static SDL_Event evenements; // en statique car appelle plusieurs fois par seconde for(int i=0;i<SDL_JoystickNumBalls(input->joystick);i++) { // on met à zéro input->trackballs[i]->xrel = 0; input->trackballs[i]->yrel = 0; } while(SDL_PollEvent(&evenements)) // tant qu'il y a des évènements à traiter { if(input->joystick != NULL //si le joystick existe &&(evenements.jbutton.which == input->numero || evenements.jaxis.which == input->numero // et si c'est bien je joystick a gérer || evenements.jhat.which == input->numero || evenements.jball.which == input->numero)) { switch(evenements.type) { case SDL_JOYBUTTONDOWN: input->boutons[evenements.jbutton.button] = 1; // bouton appuyé : valeur du bouton : 1 break; case SDL_JOYBUTTONUP: input->boutons[evenements.jbutton.button] = 0; // bouton relâché : valeur du bouton : 0 break; case SDL_JOYAXISMOTION: input->axes[evenements.jaxis.axis] = evenements.jaxis.value; break; case SDL_JOYHATMOTION: input->chapeaux[evenements.jhat.hat] = evenements.jhat.value; break; case SDL_JOYBALLMOTION input->trackballs[evenements.jball.ball]->xrel = evenements.jball.xrel; input->trackballs[evenements.jball.ball]->yrel = evenements.jball.yrel; break; default: break; } } } }
_______________________________________________
mon fichier main.cpp:
#include <iostream> #include <SDL/SDL.h> //#include <event.cpp> int main(int argc,char **argv) { SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK); // on initialise les sous-programmes vidéo et joystick SDL_JoystickEventState(SDL_ENABLE); // on active les évènements du joystick Input input; // on crée la structure initialiserInput(&input); // on l'initialise au joystick n°0 int quitter = 0; /* tout la blabla des fenêtre et des diverses variables ... */ while(!quitter) { updateEvent(&input); // on récupère les évènements /* traitement des événements (à suivre) ... */ } /* diverses destruction ...*/ detruireInput(&input); // on détruit la structure input SDL_Quit(); // on quitte la SDL return 0; }
Lorsque je compile mon main.cpp j'ai ce message d'errreur,
||=== Build: Debug in 6wd_openclass (compiler: GNU GCC Compiler) ===|
C:\Users\Nico\Desktop\Projet C\6wd_openclass\main.cpp||In function 'int SDL_main(int, char**)':|
C:\Users\Nico\Desktop\Projet C\6wd_openclass\main.cpp|9|error: 'Input' was not declared in this scope|
C:\Users\Nico\Desktop\Projet C\6wd_openclass\main.cpp|9|error: expected ';' before 'input'|
C:\Users\Nico\Desktop\Projet C\6wd_openclass\main.cpp|10|error: 'input' was not declared in this scope|
C:\Users\Nico\Desktop\Projet C\6wd_openclass\main.cpp|10|error: 'initialiserInput' was not declared in this scope|
C:\Users\Nico\Desktop\Projet C\6wd_openclass\main.cpp|18|error: 'updateEvent' was not declared in this scope|
C:\Users\Nico\Desktop\Projet C\6wd_openclass\main.cpp|26|error: 'detruireInput' was not declared in this scope|
||=== Build failed: 6 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
----------------------------------------------------
j'aimerai bien avoir des explication, je suis debutant, je ne maitrise absolument pas le C ni la programmation.
Dans ce code il y a des partie que je n'utiliserai pas tel que la sourie et le clavier mise a par la touche ECHAP pour quitter le programme, et je n'utilise qu'un seul joystick. J'ai appris a me servir du C a partir de ce post(lien au debut du post).
Le but finale de ce code sera de recuperer mes evenement joystick uniquement, d'afficher mes valeur des axe et boutton joysticj dans un tableau dans la fentre que SDL genere,et ensuite de pouvoir les envoyers sur le reseau à un robot (sur un raspberry pi B+), donc si j'ai bien compris, ce code m'alloue mes valeurs de joystick dans la memoire vive, donc par la suite je devrai les envoyers sur le reseau mais ceci sera l'etape suivante ^^
question:
Es la bonne méthode utiliser (donc la libraire SDL et le language C) pour piloter mon robot avec un joystick depuis mon ordi portable via le wifi ?
question en rapport au code:
1) Es que mes fichier main, event et imput on la bonne extension ?
2) Es que les directive de préprocesseur sont bonne ?
3) Peut etre dois -je inclure dans mon main.cpp mes fichier event.cpp et imput.h ?
si vous lisez mon post:
https://openclassrooms.com/forum/sujet/utiliser-la-librairie-sdl-pour-piloter-un-robot
je souhaitai recuperer mes valeur dans un fichier.txt, et de les envoyer a laide dun batch sur le reseau, probleme que j'ai eu, impossible d'ouvrir se fichier .txt tant que l'application SDL l'utilisé, donc j'ai chercher une autre facon de faire et me voila ici avec mes 3 fichier main.cpp , event.cpp et imput.h mais qui ne compile pâs.
Merci a celui qui eclairera ma lanterne !!!
A voir également:
- Librairie SDL
- Librairie anspach tabac - Forum Consommation & Internet
- Logiciel librairie - Forum Bureautique
- Librairie valentine wtf - Forum C
- Librairie lame pour audacity - Télécharger - Création musicale
- Compiler avec une librairie partagée - Forum Linux / Unix
2 réponses
Salut poparnassus,
Sans être entré dans le détail de ton code, que je n'ai pas regardé, tu utilises dans main.cpp des types et prototypes de fonctions qui ne sont pas déclarés dans main.cpp, ni dans aucun des fichiers d'entête inclus.
Par exemple, le
Donc, il me semble que tu devrais faire un
Sinon, pourquoi faire un projet en cpp, alors que la SDL et ton code ton en C ?
Dal
Sans être entré dans le détail de ton code, que je n'ai pas regardé, tu utilises dans main.cpp des types et prototypes de fonctions qui ne sont pas déclarés dans main.cpp, ni dans aucun des fichiers d'entête inclus.
Par exemple, le
typedef"Input", qui est en fait un
struct Input, est déclaré dans
Input.h(ou "imput.h" selon ce que tu dis.. attention à la casse qui peut être signifiante selon les systèmes et à l'orthographe).
Donc, il me semble que tu devrais faire un
#include "Input.h"dans ton main.cpp.
Sinon, pourquoi faire un projet en cpp, alors que la SDL et ton code ton en C ?
Dal
Ok merci des comms,,, Je n'arrive pas editer mon post donc je pose mo mon code modifier:
main.c
fonction.h
fonction.c
Et la les log du compilateur:
||=== Build: Debug in 6wd_SDL2_test (compiler: GNU GCC Compiler) ===|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c||In function 'initialiserInput':|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|10|warning: assignment from incompatible pointer type|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|11|error: 'Input' has no member named 'numero'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|14|error: 'Input' has no member named 'boutons'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|14|warning: passing argument 1 of 'SDL_JoystickNumButtons' from incompatible pointer type|
..\..\..\..\..\Program Files (x86)\CodeBlocks\SDL2-2.0.0\i686-w64-mingw32\include\SDL2\SDL_joystick.h|156|note: expected 'struct SDL_Joystick *' but argument is of type 'struct InputJoystick *'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|15|error: 'Input' has no member named 'axes'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|15|warning: implicit declaration of function 'SDL_JoystickNumAxis' [-Wimplicit-function-declaration]|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|16|error: 'Input' has no member named 'chapeaux'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|16|warning: passing argument 1 of 'SDL_JoystickNumHats' from incompatible pointer type|
..\..\..\..\..\Program Files (x86)\CodeBlocks\SDL2-2.0.0\i686-w64-mingw32\include\SDL2\SDL_joystick.h|151|note: expected 'struct SDL_Joystick *' but argument is of type 'struct InputJoystick *'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|17|error: 'Input' has no member named 'trackballs'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|17|error: 'InputTrackball' undeclared (first use in this function)|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|17|note: each undeclared identifier is reported only once for each function it appears in|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|17|error: expected expression before ')' token|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|19|error: 'for' loop initial declarations are only allowed in C99 or C11 mode|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|19|note: use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 to compile your code|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|19|warning: passing argument 1 of 'SDL_JoystickNumButtons' from incompatible pointer type|
..\..\..\..\..\Program Files (x86)\CodeBlocks\SDL2-2.0.0\i686-w64-mingw32\include\SDL2\SDL_joystick.h|156|note: expected 'struct SDL_Joystick *' but argument is of type 'struct InputJoystick *'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|20|error: 'Input' has no member named 'boutons'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|21|error: redefinition of 'i'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|19|note: previous definition of 'i' was here|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|21|error: 'for' loop initial declarations are only allowed in C99 or C11 mode|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|21|warning: passing argument 1 of 'SDL_JoystickNumAxes' from incompatible pointer type|
..\..\..\..\..\Program Files (x86)\CodeBlocks\SDL2-2.0.0\i686-w64-mingw32\include\SDL2\SDL_joystick.h|138|note: expected 'struct SDL_Joystick *' but argument is of type 'struct InputJoystick *'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|22|error: 'Input' has no member named 'axes'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|23|error: redefinition of 'i'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|21|note: previous definition of 'i' was here|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|23|error: 'for' loop initial declarations are only allowed in C99 or C11 mode|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|23|warning: passing argument 1 of 'SDL_JoystickNumHats' from incompatible pointer type|
..\..\..\..\..\Program Files (x86)\CodeBlocks\SDL2-2.0.0\i686-w64-mingw32\include\SDL2\SDL_joystick.h|151|note: expected 'struct SDL_Joystick *' but argument is of type 'struct InputJoystick *'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|24|error: 'Input' has no member named 'chapeaux'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|25|error: redefinition of 'i'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|23|note: previous definition of 'i' was here|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|25|error: 'for' loop initial declarations are only allowed in C99 or C11 mode|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|25|warning: passing argument 1 of 'SDL_JoystickNumBalls' from incompatible pointer type|
..\..\..\..\..\Program Files (x86)\CodeBlocks\SDL2-2.0.0\i686-w64-mingw32\include\SDL2\SDL_joystick.h|146|note: expected 'struct SDL_Joystick *' but argument is of type 'struct InputJoystick *'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|28|error: 'Input' has no member named 'trackballs'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|29|error: 'Input' has no member named 'trackballs'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|38|error: 'Input' has no member named 'boutons'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|39|error: 'Input' has no member named 'axes'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|40|error: 'Input' has no member named 'chapeaux'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|41|error: 'Input' has no member named 'trackballs'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c||In function 'detruireInput':|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|49|error: 'Input' has no member named 'numero'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|52|error: 'Input' has no member named 'boutons'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|53|error: 'Input' has no member named 'axes'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|54|error: 'Input' has no member named 'chapeaux'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|55|error: 'Input' has no member named 'trackballs'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|56|warning: passing argument 1 of 'SDL_JoystickClose' from incompatible pointer type|
..\..\..\..\..\Program Files (x86)\CodeBlocks\SDL2-2.0.0\i686-w64-mingw32\include\SDL2\SDL_joystick.h|242|note: expected 'struct SDL_Joystick *' but argument is of type 'struct InputJoystick *'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c||In function 'updateEvent':|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|73|error: 'quitter' undeclared (first use in this function)|
||=== Build failed: 29 error(s), 9 warning(s) (0 minute(s), 0 second(s)) ===|
J'aimerai comprendre le fonctionnement des structures, j'ai du mal a ce niveau, si quelqun veut bien detailler MERCI
ET, je comprend pas pourquoi j'ai cette erreur qui ressort tout le temp : 'Input' has no member named "..."
main.c
#include <stdio.h> #include <stdlib.h> #include <iostream> #include "fonction.h" #include <SDL2/SDL.h> int main(int argc, char**argv) { printf("Programme Wild Thumper \n"); //TABLEAU //quitter* quitter = NULL; FILE* fichier = NULL; fichier=fopen("joystick.txt","w+"); // on crée un fichier Joystick.txt SDL_Init( SDL_INIT_VIDEO | SDL_INIT_JOYSTICK); // on initialise les sous-programmes vidéo et joystick //atexit(SDL_Quit); SDL_JoystickEventState(SDL_ENABLE); // on active les évènements du joystick Input input; // on crée la structure InputJoystick InputJoystick; initialiserInput(&input); int quitter = 0; while (!quitter) { updateEvent(&input); } SDL_Quit(); return 0; }
fonction.h
// STURCURE typedef struct InputJoystick InputJoystick; struct InputJoystick { SDL_Joystick *joystick; char *boutons; int *axes; int *chapeaux; int numero; }; /* la structure qui gère les événements */ typedef struct Input Input; struct Input { char *touches; // on alloue les touches par la suite ... int quitter; // il est géré automatiquement mais peut être demandé à être mis à 1 InputJoystick *joystick; // pointeur car il peut y avoir plusieurs joysticks int nombreJoysticks; // sert à détruire à la fin }; //FONCTION void updateEvent(Input *input); void detruireInput(Input *input); void initialiserInput(Input *input,int numeroJoystick);
fonction.c
#include <stdlib.h> #include <SDL2/SDL.h> #include "fonction.h" void initialiserInput(Input *input,int numeroJoystick) { if(numeroJoystick < SDL_NumJoysticks()) // on vérifie qu'il y a bien un bon numéro de joystick { input->joystick = SDL_JoystickOpen(numeroJoystick); // on met le joystick à numéro correspondant input->numero = numeroJoystick; // je pense que vous comprenez cette ligne... /* on alloue chaque éléments en fonctions de combien il y en a */ input->boutons = (char*) malloc(SDL_JoystickNumButtons(input->joystick) * sizeof(char)); input->axes = (int*) malloc(SDL_JoystickNumAxis(input->joystick) * sizeof(int)); input->chapeaux = (int*) malloc(SDL_JoystickNumHats(input->joystick) * sizeof(int)); input->trackballs = (InputTrackball*) malloc(SDL_JoystickNumBalls(input->joystick) * sizeof(InputTrackball)); for(int i=0;i<SDL_JoystickNumButtons(input->joystick);i++) // tant qu'on a pas atteint le nombre max de boutons input->boutons[i] = 0; // on met les valeurs à 0 for(int i=0;i<SDL_JoystickNumAxes(input->joystick);i++) // tant qu'on a pas atteint le nombre max d'axes input->axes[i] = 0; // on met aussi les valeurs à 0 for(int i=0;i<SDL_JoystickNumHats(input->joystick);i++) // tant qu'on a pas atteint le nombre max de chapeaux input->chapeaux[i] = SDL_HAT_CENTERED; // on dit que les chapeaux son centrés for(int i=0;i<SDL_JoystickNumBalls(input->joystick);i++) // tant qu'il y a des trackballs { // on met à zéro input->trackballs[i]->xrel = 0; input->trackballs[i]->yrel = 0; } } else { // si le numéro du joystick n'était pas correct // on met tout à NULL input->joystick = NULL; input->boutons = NULL; input->axes = NULL; input->chapeaux = NULL; input->trackballs = NULL; } } void detruireInput(Input *input) { if(input->joystick != NULL) // on vérifie que le joystick existe bien { input->numero = 0; // on le remet à zéro // on libère tout free(input->boutons); free(input->axes); free(input->chapeaux); free(input->trackballs); SDL_JoystickClose(input->joystick); } } void updateEvent(Input *input) { static SDL_Event event; while (SDL_PollEvent(&event)) { // check for messages switch (event.type) { // exit if the int SDL(int argc, char*argv[])window is closed case SDL_QUIT: { quitter = 1; } break; case SDL_JOYBUTTONDOWN: { input->boutons[evenements.jbutton.button] = 1; // bouton appuyé : valeur du bouton : 1 switch(event.jbutton.button) { case 7 : { quitter = 1; } break; } break; } case SDL_JOYAXISMOTION: { input->axes[evenements.jaxis.axis] = evenements.jaxis.value; break; } } } }
Et la les log du compilateur:
||=== Build: Debug in 6wd_SDL2_test (compiler: GNU GCC Compiler) ===|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c||In function 'initialiserInput':|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|10|warning: assignment from incompatible pointer type|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|11|error: 'Input' has no member named 'numero'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|14|error: 'Input' has no member named 'boutons'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|14|warning: passing argument 1 of 'SDL_JoystickNumButtons' from incompatible pointer type|
..\..\..\..\..\Program Files (x86)\CodeBlocks\SDL2-2.0.0\i686-w64-mingw32\include\SDL2\SDL_joystick.h|156|note: expected 'struct SDL_Joystick *' but argument is of type 'struct InputJoystick *'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|15|error: 'Input' has no member named 'axes'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|15|warning: implicit declaration of function 'SDL_JoystickNumAxis' [-Wimplicit-function-declaration]|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|16|error: 'Input' has no member named 'chapeaux'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|16|warning: passing argument 1 of 'SDL_JoystickNumHats' from incompatible pointer type|
..\..\..\..\..\Program Files (x86)\CodeBlocks\SDL2-2.0.0\i686-w64-mingw32\include\SDL2\SDL_joystick.h|151|note: expected 'struct SDL_Joystick *' but argument is of type 'struct InputJoystick *'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|17|error: 'Input' has no member named 'trackballs'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|17|error: 'InputTrackball' undeclared (first use in this function)|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|17|note: each undeclared identifier is reported only once for each function it appears in|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|17|error: expected expression before ')' token|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|19|error: 'for' loop initial declarations are only allowed in C99 or C11 mode|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|19|note: use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 to compile your code|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|19|warning: passing argument 1 of 'SDL_JoystickNumButtons' from incompatible pointer type|
..\..\..\..\..\Program Files (x86)\CodeBlocks\SDL2-2.0.0\i686-w64-mingw32\include\SDL2\SDL_joystick.h|156|note: expected 'struct SDL_Joystick *' but argument is of type 'struct InputJoystick *'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|20|error: 'Input' has no member named 'boutons'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|21|error: redefinition of 'i'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|19|note: previous definition of 'i' was here|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|21|error: 'for' loop initial declarations are only allowed in C99 or C11 mode|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|21|warning: passing argument 1 of 'SDL_JoystickNumAxes' from incompatible pointer type|
..\..\..\..\..\Program Files (x86)\CodeBlocks\SDL2-2.0.0\i686-w64-mingw32\include\SDL2\SDL_joystick.h|138|note: expected 'struct SDL_Joystick *' but argument is of type 'struct InputJoystick *'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|22|error: 'Input' has no member named 'axes'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|23|error: redefinition of 'i'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|21|note: previous definition of 'i' was here|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|23|error: 'for' loop initial declarations are only allowed in C99 or C11 mode|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|23|warning: passing argument 1 of 'SDL_JoystickNumHats' from incompatible pointer type|
..\..\..\..\..\Program Files (x86)\CodeBlocks\SDL2-2.0.0\i686-w64-mingw32\include\SDL2\SDL_joystick.h|151|note: expected 'struct SDL_Joystick *' but argument is of type 'struct InputJoystick *'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|24|error: 'Input' has no member named 'chapeaux'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|25|error: redefinition of 'i'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|23|note: previous definition of 'i' was here|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|25|error: 'for' loop initial declarations are only allowed in C99 or C11 mode|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|25|warning: passing argument 1 of 'SDL_JoystickNumBalls' from incompatible pointer type|
..\..\..\..\..\Program Files (x86)\CodeBlocks\SDL2-2.0.0\i686-w64-mingw32\include\SDL2\SDL_joystick.h|146|note: expected 'struct SDL_Joystick *' but argument is of type 'struct InputJoystick *'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|28|error: 'Input' has no member named 'trackballs'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|29|error: 'Input' has no member named 'trackballs'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|38|error: 'Input' has no member named 'boutons'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|39|error: 'Input' has no member named 'axes'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|40|error: 'Input' has no member named 'chapeaux'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|41|error: 'Input' has no member named 'trackballs'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c||In function 'detruireInput':|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|49|error: 'Input' has no member named 'numero'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|52|error: 'Input' has no member named 'boutons'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|53|error: 'Input' has no member named 'axes'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|54|error: 'Input' has no member named 'chapeaux'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|55|error: 'Input' has no member named 'trackballs'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|56|warning: passing argument 1 of 'SDL_JoystickClose' from incompatible pointer type|
..\..\..\..\..\Program Files (x86)\CodeBlocks\SDL2-2.0.0\i686-w64-mingw32\include\SDL2\SDL_joystick.h|242|note: expected 'struct SDL_Joystick *' but argument is of type 'struct InputJoystick *'|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c||In function 'updateEvent':|
C:\Users\Nico\Desktop\Projet C\6wd_SDL2_test\fonction.c|73|error: 'quitter' undeclared (first use in this function)|
||=== Build failed: 29 error(s), 9 warning(s) (0 minute(s), 0 second(s)) ===|
J'aimerai comprendre le fonctionnement des structures, j'ai du mal a ce niveau, si quelqun veut bien detailler MERCI
ET, je comprend pas pourquoi j'ai cette erreur qui ressort tout le temp : 'Input' has no member named "..."
Si je comprends bien, dans "fonction.c" ligne 19:
un tableau est cree qui a pour nom "boutton" et pour valeur "le nombre de bouton MAX de ma manette"
Ligne 21 et 24: la meme chose avec "axes" et "chapeau"
Maintenan dans "main.c", je veux faire afficher ces valeur "boutton" , "axe" et "chapeau", donc apres la ligne 30 "updateEvent(&input)"
j'ajoute:
Dans mon fichier fonction.c j'ajoute:
un tableau est cree qui a pour nom "boutton" et pour valeur "le nombre de bouton MAX de ma manette"
Ligne 21 et 24: la meme chose avec "axes" et "chapeau"
Maintenan dans "main.c", je veux faire afficher ces valeur "boutton" , "axe" et "chapeau", donc apres la ligne 30 "updateEvent(&input)"
j'ajoute:
afficherValeurJoystick(&input);
Dans mon fichier fonction.c j'ajoute:
void afficherValeurJoystick(Input *input) { for( int i = 0; i < boutton;i++) { printf( "%d \n\n", boutton[i] ); } for( int i = 0;i < axe; i++ ) { printf ( " %d \n\n" , axe[i]); } for (int i=0; i < chapeau; i++) { printf(" %d \n\n ", chapeau[i]); } }
Salut poparnassus,
Je n'ai pratiqué la SDL que superficiellement, car je lui préfère SFML.
Ton sujet parle de SDL 1.2, mais ton code inclue des entêtes de SDL2. Tu programmes en SDL2 donc.
Si tu as de multiples messages d'erreurs et avertissements, tu dois les traiter en commençant par le premier, puis tu recompiles une fois que tu as éliminé le premier, et tu vois ce qui change pour la suite.
Le premier message :
Selon la doc
https://wiki.libsdl.org/SDL_JoystickOpen
Or le membre
Ta
Donc, si tu dois affecter le résultat de
Tu devrais revoir le nommage des membres de tes struct car utiliser des noms identiques pour signifier des choses différentes est source de confusion.
Dans la suite du code, tu tentes de nouveau d'accéder à des membres de la
Tu ne devrais pas t'interroger sur les lignes 19, 21, 24 ou 30, le code ne compilant pas ou générant des avertissements avant ces lignes. En fait, les lignes 19, 21,.. ne devraient même pas exister.
Lorsqu'on compile et qu'on a des messages d'erreurs et des avertissements, on n'attend pas d'avoir 29 erreurs et 9 warnings pour déboguer et se demander ce qui ne va pas.
Dal
Je n'ai pratiqué la SDL que superficiellement, car je lui préfère SFML.
Ton sujet parle de SDL 1.2, mais ton code inclue des entêtes de SDL2. Tu programmes en SDL2 donc.
Si tu as de multiples messages d'erreurs et avertissements, tu dois les traiter en commençant par le premier, puis tu recompiles une fois que tu as éliminé le premier, et tu vois ce qui change pour la suite.
Le premier message :
fonction.c|10|warning: assignment from incompatible pointer type|se réfère à
input->joystick = SDL_JoystickOpen(numeroJoystick);.
Selon la doc
SDL_JoystickOpenrenvoie un pointeur sur
SDL_Joystick.
https://wiki.libsdl.org/SDL_JoystickOpen
Or le membre
joystickde la
struct Inputn'est pas de type
SDL_Joystick, il est de type pointeur sur
InputJoystick.
Ta
struct InputJoystick, en revanche, comporte bien un membre pointeur sur
SDL_Joystickqui s'appelle (aussi)
joystick, auquel tu peux accéder par l'intermédiaire de la
struct Input.
Donc, si tu dois affecter le résultat de
SDL_JoystickOpen(numeroJoystick);à quelque chose en passant par une variable de type
struct Input, cela serait en faisant
input->joystick->joystick = SDL_JoystickOpen(numeroJoystick);
Tu devrais revoir le nommage des membres de tes struct car utiliser des noms identiques pour signifier des choses différentes est source de confusion.
Dans la suite du code, tu tentes de nouveau d'accéder à des membres de la
struct Inputqui n'existe pas dans celle-ci mais qui existent dans la
struct InputJoystickimbriquée, par exemple :
fonction.c|11|error: 'Input' has no member named 'numero'|, tu dois appliquer le même type de corrections.
Tu ne devrais pas t'interroger sur les lignes 19, 21, 24 ou 30, le code ne compilant pas ou générant des avertissements avant ces lignes. En fait, les lignes 19, 21,.. ne devraient même pas exister.
Lorsqu'on compile et qu'on a des messages d'erreurs et des avertissements, on n'attend pas d'avoir 29 erreurs et 9 warnings pour déboguer et se demander ce qui ne va pas.
Dal
main.c
fonction.h
fonction.c
Voila jai fais les modif ca a compiler, maintenant j'ai rajouter des ligne de code dans
1) Autre pb, ligne 13 du main.c, si j'ecris ma fonction comme ca:
J'ai automatiquement une erreur et ca quitte, alors que sans la condition ca marche !
2) Dans fonction.c les boucle for ne compile on me dit que cest pas compatible en C11 mais C99, cest du C++ ?
Parce que j'ai besoin de mettre en tableau les axe, bouton est valeur associé
https://alexandre-laurent.developpez.com/tutoriels/sdl-2/les-evenements/#LIII-C-3-d
#include <stdio.h> #include <stdlib.h> #include "fonction.h" #include <SDL2/SDL.h> int main(int argc, char** argv) { printf("Programme Wild Thumper \n\n"); FILE* fichier = NULL; fichier=fopen("joystick.txt","w+"); // on crée un fichier Joystick.txt SDL_Init( SDL_INIT_VIDEO | SDL_INIT_JOYSTICK); // on initialise les sous-programmes vidéo et joystick SDL_JoystickEventState(SDL_ENABLE); // on active les évènements du joystick //STRUCTURE Input input; /* Création de la fenêtre */ SDL_Window* pWindow = NULL; pWindow = SDL_CreateWindow("Programme Wild Thumper",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,640,480,SDL_WINDOW_SHOWN); if (pWindow) { int quitter = 1; while (Input.quitter != 0) { updateEvent(&input); //Afficher les valeurs Joystick (numAxe, et valeur) et boutton X,A,B,Y valeur=1 pour boutton apuyer //Envoyer les valeurs Joystiks et boutton sur l'adresse 10.168.50.10:... //afficherDonner(&input); } } SDL_Quit(); return (EXIT_SUCCESS); }
fonction.h
#include <SDL2/SDL.h> // STURCURE typedef struct InputJoystick InputJoystick; struct InputJoystick { SDL_Joystick *joystick; char *boutons; int *axes; int *chapeaux; int numero; }; /* la structure qui gère les événements */ typedef struct Input Input; struct Input { char *touches; // on alloue les touches par la suite ... int quitter; // il est géré automatiquement mais peut être demandé à être mis à 1 InputJoystick *Sjoystick; // pointeur car il peut y avoir plusieurs joysticks }; //FONCTION void initialiserInput(Input *input,int numeroJoystick); void updateEvent(Input *input); void afficherDonner(Input *input);
fonction.c
#include <stdlib.h> #include <SDL2/SDL.h> #include "fonction.h" void initialiserInput(Input *input,int numeroJoystick) { if(numeroJoystick < SDL_NumJoysticks()) // on vérifie qu'il y a bien un bon numéro de joystick { input->Sjoystick->joystick = SDL_JoystickOpen(numeroJoystick); // on met le joystick à numéro correspondant input->Sjoystick->numero = numeroJoystick; // for(int i=0;i<SDL_JoystickNumButtons(input.Sjoystick.boutons);i++) // tant qu'on a pas atteint le nombre max de boutons // {input->Sjoystick->boutons[i] = 0;} // on met les valeurs à 0 //for(int i=0;i<SDL_JoystickNumAxes(input->Sjoystick->joystick);i++) // tant qu'on a pas atteint le nombre max d'axes //input->Sjoystick->axes[i] = 0; // on met aussi les valeurs à 0 //for(int i=0;i<SDL_JoystickNumHats(input->Sjoystick->joystick);i++) // tant qu'on a pas atteint le nombre max de chapeaux //input->Sjoystick->chapeaux[i] = SDL_HAT_CENTERED; // on dit que les chapeaux son centrés } else { // si le numéro du joystick n'était pas correct // on met tout à NULL input->Sjoystick->joystick = NULL; input->Sjoystick->boutons = NULL; input->Sjoystick->axes = NULL; input->Sjoystick->chapeaux = NULL; } } void updateEvent(Input *input) { static SDL_Event event; while (SDL_PollEvent(&event)) { const Uint8* pKeyStates = SDL_GetKeyboardState(NULL); if ( pKeyStates[SDL_SCANCODE_ESCAPE] ){input->quitter = 1;} SDL_Keymod mod = SDL_GetModState(); if ( mod != KMOD_NONE ){printf("Vous avez appuyé sur une touche spéciale : %d\n",mod);} int nbrboutton = SDL_JoystickNumButtons(input.Sjoystick.numero); int bouton = 0; printf("Nombre de boutons : %d\n", nbrboutton); for ( bouton = 0 ; bouton < nbrboutton ; bouton++ ) {printf("Bouton %d : %d\n",bouton,SDL_JoystickGetButton(input.Sjoystick.numero,bouton));} } } void afficherDonner(Input *input) { }
Voila jai fais les modif ca a compiler, maintenant j'ai rajouter des ligne de code dans
void updateEvent(Input *input)
1) Autre pb, ligne 13 du main.c, si j'ecris ma fonction comme ca:
if (SDL_Init( SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) != 0 ); { printf("erreur d'initialisation"); return -1; }
J'ai automatiquement une erreur et ca quitte, alors que sans la condition ca marche !
2) Dans fonction.c les boucle for ne compile on me dit que cest pas compatible en C11 mais C99, cest du C++ ?
Parce que j'ai besoin de mettre en tableau les axe, bouton est valeur associé
https://alexandre-laurent.developpez.com/tutoriels/sdl-2/les-evenements/#LIII-C-3-d
Oui effectivement je vais corriger, et enlever les morceau de code que je n'utilise pas, et pour .cpp j'ai lu dans des commentaires que ca ne posez pas de probleme pour compilez mais je vais corriger cz zussi
Dans mon main j'ajoue et
ou je renome mon fichier event en input.cpp ?
Vois ce document, bien fait, par un professeur de l'université du Michigan, sur les règles de bonne conception et gestion de tes .h et .c :
http://umich.edu/~eecs381/handouts/CHeaderFileGuidelines.pdf
Dal