Sfoglia il codice sorgente

Import SDL1 joystick project

DricomDragon 4 anni fa
parent
commit
ee65e41b08
3 ha cambiato i file con 287 aggiunte e 0 eliminazioni
  1. 56 0
      sdl1/joystickVisual/Point.cpp
  2. 42 0
      sdl1/joystickVisual/Point.h
  3. 189 0
      sdl1/joystickVisual/main.cpp

+ 56 - 0
sdl1/joystickVisual/Point.cpp

@@ -0,0 +1,56 @@
+#include "Point.h"
+
+Point::Point(int rayon,Uint32 couleur)
+:m_rayon(rayon)
+{
+    m_pixel = SDL_CreateRGBSurface(SDL_HWSURFACE, 1, 1, 16, 0, 0, 0, 0);
+    SDL_FillRect(m_pixel,NULL,couleur);
+    if (!m_pixel)
+    {
+        std::cout << "Impossible de créer la surface dans l'objet Point " << SDL_GetError() << std::endl;
+    }
+}///Constructeur standard
+
+Point::Point(int rayon,Uint32 couleur,int transparence)
+:m_rayon(rayon)
+{
+    m_pixel = SDL_CreateRGBSurface(SDL_HWSURFACE, 1, 1, 16, 0, 0, 0, 0);
+    SDL_FillRect(m_pixel,NULL,couleur);
+    if (!m_pixel)
+    {
+        std::cout << "Impossible de créer la surface dans l'objet Point " << SDL_GetError() << std::endl;
+    }
+}///Constructeur +transparence
+
+Point::~Point()
+{
+    SDL_FreeSurface(m_pixel);
+    m_pixel=0;
+    std::cout << "Destruction du point effective. " << std::endl;
+}///Destructeur
+
+int Point::getRayon()
+{
+    return m_rayon;
+}///getRayon
+
+void Point::afficherPoint(SDL_Surface *screen,int const x, int const y)
+{
+    int distance(0);
+    SDL_Rect position;
+    for (int i(x-m_rayon); i<=x+m_rayon; i++)
+    {
+        for (int j(y-m_rayon); j<=y+m_rayon; j++)
+        {
+            distance = pow(i-x,2)+pow(j-y,2);
+            if (distance <= pow(m_rayon,2))
+            {
+                position.x=i;
+                position.y=j;
+                SDL_BlitSurface(m_pixel, 0, screen, &position);
+            }//test blit
+        }//for j
+    }//for i
+}///afficherPoint
+
+

+ 42 - 0
sdl1/joystickVisual/Point.h

@@ -0,0 +1,42 @@
+#ifndef POINT_H_INCLUDED
+#define POINT_H_INCLUDED
+
+///Modifié le: 10/06/2013
+///Objet: débuggage, mise en fonction
+
+#include <iostream>
+#include <SDL.h>
+#include <cmath>
+
+/*
+===La classe Point===
+    +Définition:
+        La classe Point permet de créer un  point.
+    +Fonctions:
+        -Constructeur standard:
+            Ne prend que le rayon et la couleur en paramètre, mesure en nb de pixels.
+        -Constructeur amélioré:
+            Permet de définir en plus du rayon et de la couleur
+            le coefficient de transparence. (0=visible à 255=invsible)
+        -getRayon:
+            Permet d'obtenir le rayon.
+
+*/
+
+class Point
+{
+    public:
+    Point(int rayon,Uint32 couleur);
+    Point(int rayon,Uint32 couleur,int transparence);
+    ~Point();
+    int getRayon();
+    void afficherPoint(SDL_Surface *screen,int const x, int const y);
+
+    private:
+    //Attributs standards:
+    int const m_rayon;
+
+    //Attributs SDL:
+    SDL_Surface *m_pixel;
+};
+#endif // POINT_H_INCLUDED

+ 189 - 0
sdl1/joystickVisual/main.cpp

@@ -0,0 +1,189 @@
+#include <iostream>
+#include <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;
+}