#include <iostream> #include <SDL/SDL.h> #include "Point.h" #define LARGEUR 640 #define HAUTEUR 480 int main ( int argc, char** argv ) { /// [1] Démarrage // [1.1] Démarrages SDL if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) { std::cout << "Impossible d'initialiser la SDL: " << SDL_GetError() << std::endl; return 1; } // [1.2] Préparation de fermeture atexit(SDL_Quit); // [1.3] Para-fenêtre SDL_WM_SetCaption("Joysticks test", 0); /// [2] Préparation des composants // [2.1] Préparation de la fenêtre SDL_Surface* screen = SDL_SetVideoMode(LARGEUR, HAUTEUR, 16, SDL_HWSURFACE|SDL_DOUBLEBUF); if ( !screen ) { std::cout << "Unable to set 640x480 video: " << SDL_GetError() << std::endl; return 1; } // [2.2] Préparation de la manette if (SDL_NumJoysticks()<1) { std::cout<< "Il n'y a aucune manette de branchée."<<std::endl <<"Veuillez réessayer avec un pad connecté."<<std::endl; return 1; } SDL_Joystick *joystick; SDL_JoystickEventState(SDL_ENABLE); joystick = SDL_JoystickOpen(0); // [2.3] Affichage en console std::cout << "Il y a " <<SDL_NumJoysticks()<< " manette(s) disponible(s)." <<std::endl; std::cout << "Leur(s) adresse(s) sont(est): "; for (int i(0);i<SDL_NumJoysticks();i++) { std::cout<< "==> Manette '" <<SDL_JoystickName(i)<<"'; "; } std::cout << std::endl; std::cout << "La manette selectionnée est : "<<SDL_JoystickName(0)<<". "<<std::endl << "Elle possède " <<SDL_JoystickNumAxes(joystick)<< " axes; "<<std::endl << "Mais également " <<SDL_JoystickNumButtons(joystick)<< " boutons. " << std::endl; std::cout << "Lancement du test pour "<<SDL_JoystickName(0)<<". "<<std::endl; /* [2.4] Préparation des composants */ //[2.4.1] Les sticks Point rond(20,SDL_MapRGB(screen->format,255,255,255)); Point milieu(25,SDL_MapRGB(screen->format,255,0,54)); SDL_Rect stickGauche; stickGauche.x=LARGEUR/4; stickGauche.y=HAUTEUR/4; SDL_Rect stickDroit; stickDroit.x=LARGEUR/4; stickDroit.y=HAUTEUR/4*3; //[2.4.1] Les axes SDL_Surface* carre(0); SDL_Surface* centre(0); carre = SDL_CreateRGBSurface(SDL_HWSURFACE, 20, 20, 16, 0, 0, 0, 0); SDL_FillRect(carre,NULL,SDL_MapRGB(screen->format,255,255,255)); centre = SDL_CreateRGBSurface(SDL_HWSURFACE, 36, 10, 16, 0, 0, 0, 0); SDL_FillRect(centre,NULL,SDL_MapRGB(screen->format,255,0,54)); SDL_Rect referenceAxe; referenceAxe.x=LARGEUR/6; referenceAxe.y=HAUTEUR/2-centre->h/2; SDL_Rect axeGauche; axeGauche.x=referenceAxe.x*4; axeGauche.y=HAUTEUR/2; SDL_Rect axeDroit; axeDroit.x=referenceAxe.x*5; axeDroit.y=HAUTEUR/2; /// [3] Boucle principale bool done = false; while (!done) { // [3.1] Gestion évènements SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: done = true; break; case SDL_KEYDOWN: if (event.key.keysym.sym == SDLK_ESCAPE) done = true; break; case SDL_JOYAXISMOTION: /* Handle Joystick Motion */ if ( ( event.jaxis.value < -1/*-3200*/ ) || (event.jaxis.value > 1/*3200*/ ) ) { if( event.jaxis.axis == 0) { std::cout<<"Mouvement du stick 1 en x."; stickGauche.x=event.jaxis.value/400+LARGEUR/4; } if( event.jaxis.axis == 1) { std::cout<<"Mouvement du stick 1 en y."; stickGauche.y=event.jaxis.value/400+HAUTEUR/4; } if( event.jaxis.axis == 2) { std::cout<<"Mouvement vertical, axe y nemero 1."; axeGauche.y=event.jaxis.value/200+HAUTEUR/2; } if( event.jaxis.axis == 3) { std::cout<<"Mouvement du deuxième stick par x."; stickDroit.x=event.jaxis.value/400+LARGEUR/4; } if( event.jaxis.axis == 4) { std::cout<<"Mouvement du deuxième stick par y."; stickDroit.y=event.jaxis.value/400+HAUTEUR/4*3; } if( event.jaxis.axis == 5) { std::cout<<"Mouvement vertical du second axe :"; axeDroit.y=event.jaxis.value/200+HAUTEUR/2; } std::cout<<" Nouvelle position :"<<event.jaxis.value<<std::endl; } break; } // end switch event type } // end of message processing // [3.2] Calculs // [3.3] Dessin des composants SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 128, 128)); // [3.3.1] Les sticks milieu.afficherPoint(screen,LARGEUR/4,HAUTEUR/4); rond.afficherPoint(screen,stickGauche.x,stickGauche.y); milieu.afficherPoint(screen,LARGEUR/4,HAUTEUR/4*3); rond.afficherPoint(screen,stickDroit.x,stickDroit.y); // [3.3.2] Les axes referenceAxe.x=LARGEUR/6*4; SDL_BlitSurface(centre,0,screen,&referenceAxe); SDL_BlitSurface(carre,0,screen,&axeGauche); referenceAxe.x=LARGEUR/6*5; SDL_BlitSurface(centre,0,screen,&referenceAxe); SDL_BlitSurface(carre,0,screen,&axeDroit); SDL_Flip(screen); } //fin bcl principale ///[4] Destruction des composants SDL_JoystickClose(joystick); std::cout << "Aucune erreur détectée." << std::endl; return 0; }