Przeglądaj źródła

Import fire rain project

DricomDragon 4 lat temu
rodzic
commit
18261cbc48

+ 7 - 0
sdl1/fireRain/Coordonnee.cpp

@@ -0,0 +1,7 @@
+#include "Coordonnee.h"
+
+void initialiserDbAxes(DoubleAxe* paquet)
+{
+    paquet->x=0;
+    paquet->y=0;
+}

+ 13 - 0
sdl1/fireRain/Coordonnee.h

@@ -0,0 +1,13 @@
+#ifndef COORDONNEE_H_INCLUDED
+#define COORDONNEE_H_INCLUDED
+
+typedef struct DoubleAxe DoubleAxe;
+struct DoubleAxe
+{
+    int x;
+    int y;
+};
+
+void initialiserDbAxes(DoubleAxe* paquet);
+
+#endif // COORDONNEE_H_INCLUDED

+ 71 - 0
sdl1/fireRain/FeuFolet.cpp

@@ -0,0 +1,71 @@
+#include "FeuFolet.h"
+
+SDL_Surface* createFeuFolet(int rayon, Uint32 couleurRGB)
+{
+    //[1] Création des surfaces
+    int largeur(rayon*2);
+    SDL_Surface* resultat = SDL_CreateRGBSurface(SDL_HWSURFACE, largeur, largeur, 32, 0, 0, 0, 0);
+
+    //[2] Traitements
+    blitFeuFollet(resultat, 0, 0, rayon, couleurRGB);
+
+    //[FIN] Renvoi
+    return resultat;
+}///createFeuFolet()
+
+void blitFeuFollet(SDL_Surface* support, int x, int y, int rayon, Uint32 couleurRGB, short opacite)
+{
+    //[1] Création des surfaces
+    SDL_Surface* pixel = SDL_CreateRGBSurface(SDL_HWSURFACE, 1, 1, 32, 0, 0, 0, 0);
+
+    SDL_Surface* whitePixel = SDL_CreateRGBSurface(SDL_HWSURFACE, 1, 1, 32, 0, 0, 0, 0);
+
+    //[2] Traitements
+    SDL_FillRect(pixel,0,couleurRGB);
+    SDL_FillRect(whitePixel,0,SDL_MapRGB(whitePixel->format,255,255,255));
+
+    if (opacite>255)
+        opacite=255;
+    else if (opacite<0)
+        opacite=0;
+
+    if (rayon<=0)
+        rayon=1;
+
+    //[3] Balayage
+    int distance(0);//distance au carré, évidemment ;-)
+    Uint8 transparence(0);
+    SDL_Rect position;
+    for (position.y=y; position.y<y+2*rayon; position.y++)
+    {
+        for (position.x=x; position.x<x+2*rayon; position.x++)
+        {
+            distance=carre(position.x-rayon-x)+carre(position.y-rayon-y);
+            if (distance<carre(rayon))
+            {
+                transparence=255-(float)distance/carre(rayon)*256;//+ c'est élevé, + c'est opaque
+                if (transparence>opacite)
+                    transparence-=opacite;
+                else
+                    transparence=0;
+
+                SDL_SetAlpha(pixel,SDL_SRCALPHA,transparence);//On met la transparence
+                SDL_BlitSurface(pixel, 0, support, &position);
+
+                SDL_SetAlpha(whitePixel,SDL_SRCALPHA,transparence);//On met la transparence
+                SDL_BlitSurface(whitePixel, 0, support, &position);
+            }
+        }
+    }
+
+    //[5] Destruction des éléments
+    SDL_FreeSurface(pixel);
+    SDL_FreeSurface(whitePixel);
+}
+
+int carre(int nombre)
+{
+    return nombre*nombre;
+}///carre()
+
+

+ 41 - 0
sdl1/fireRain/FeuFolet.h

@@ -0,0 +1,41 @@
+#ifndef FEUFOLET_H_INCLUDED
+#define FEUFOLET_H_INCLUDED
+
+#include <iostream>
+#include <SDL.h>
+
+///Développeur: Jovian Hersemeule
+///Dernière mise à jour le: 02/08/2013
+///Objet: nouvel argument facultatif : opacité
+
+/*
+==La fonction createFeuFolet==
+    +Arguments:
+        -Un entier pour le rayon de la boule
+        -Un Uint 32 pour la couleur
+    +Renvoi:
+        -Une SDL surface avecl le feu follet
+    +Descriptif:
+        Permet de créer un effet de dégradé circulaire; ressemeblant à un feu follet.
+==La fonction blitFeuFolet==
+    +Arguments:
+        -Une SDL_Surface pour bliter le feu
+        -Un int donnant la coordonnée en abscisse
+        -Un int pour l'ordonnée ATTENTION !!! Coordonnées du carré, pas à partir du centre !
+        -Un entier pour le rayon de la boule
+        -Un Uint 32 pour la couleur
+        FACULTATIF - Un short (petit int) de transparence (0 VISIBLE, 255 NON VISIBLE)
+    +Renvoi:
+        -Une SDL surface avec le feu follet
+    +Descriptif:
+        Permet de créer un effet de dégradé circulaire; ressemeblant à un feu follet.
+
+*/
+
+SDL_Surface* createFeuFolet(int rayon, Uint32 couleurRGB);
+
+void blitFeuFollet(SDL_Surface* support, int x, int y, int rayon, Uint32 couleurRGB, short opacite=0);
+
+int carre(int nombre);
+
+#endif // FEUFOLET_H_INCLUDED

+ 57 - 0
sdl1/fireRain/PluieFlamme.cpp

@@ -0,0 +1,57 @@
+#include "PluieFlamme.h"
+
+PluieFlamme::PluieFlamme(DoubleAxe origine,int nbPop,int power,
+                         float graviteY,int rayon,
+                         Uint32 couleur, float resistance)
+:m_graviteY(0,graviteY),m_rayon(rayon),m_couleur(couleur),m_resist(resistance)
+{
+    //Créations des particules
+    Vecteur depart(origine.x,origine.y);
+    Vecteur direction(1.0,0.0);
+    for (int i(0); i<nbPop; i++)
+    {
+        //Positions
+        m_position.push_back(depart);//Position de pop
+        std::cout << "Rotate result ("<<direction.getX()<<";"<<direction.getY()<<")"<<std::endl;//Debug
+        direction.rotateD(360.0/nbPop);
+        m_force.push_back(direction*power);
+    }
+}///PluieFlamme
+
+void PluieFlamme::deplacement()
+{
+    for (Uint32 i(0); i<m_position.size(); i++)
+    {
+        m_position[i]+=m_force[i];
+        m_force[i]+=m_graviteY;
+        m_force[i]*=m_resist;
+    }
+}///PluieFlamme
+
+void PluieFlamme::dessiner(SDL_Surface* support)
+{
+    if (!m_position.empty())
+        for (Uint32 i(0); i<m_position.size(); i++)
+            blitFeuFollet(support, m_position[i].getX(), m_position[i].getY(), m_rayon, m_couleur, 128);
+}///PluieFlamme
+
+bool PluieFlamme::estFini(SDL_Surface* support)
+{
+    //Combien de billes sont en dehors ?
+    Uint32 inventaire(0);
+    for (Uint32 i(0); i<m_position.size(); i++)
+    {
+        if( (m_position[i].getX()>support->w || m_position[i].getX()<0
+             || m_position[i].getY()>support->h || m_position[i].getY()<0)
+           && !m_position.empty())
+        inventaire++;
+    }
+    //Sont elles toutes en dehors ?
+    if (inventaire==m_position.size())
+        return true;
+    else
+        return false;
+
+}///PluieFlamme
+
+

+ 49 - 0
sdl1/fireRain/PluieFlamme.h

@@ -0,0 +1,49 @@
+#ifndef PLUIEFLAMME_H_INCLUDED
+#define PLUIEFLAMME_H_INCLUDED
+
+/**
+    Modifié le: 28_08_2013
+    Objet: Vecteurs + floats et resistance
+    Problème(s) constaté(s):    - La résistance donne un bel effet, mais très bref.
+                                - La résistance est constante.
+    A faire: Faire évoluer la transparence.
+**/
+
+#include <iostream>
+#include <SDL.h>
+
+#include <ctime>
+#include <cstdlib>
+
+#include <vector>
+
+#include "Vecteur.h"
+#include "Coordonnee.h"
+#include "FeuFolet.h"
+
+/*
+ + Permet de créer une explosion de couleurs.
+ ! Initiliser la SDL et le srand
+*/
+
+class PluieFlamme
+{
+    public:
+    PluieFlamme(DoubleAxe origine,int nbPop,int power,//Parramètres de construction
+                float graviteY,int rayon,//Attributs physiques
+                Uint32 couleur, float resistance);//Couleurs
+    void deplacement();
+    void dessiner(SDL_Surface* support);
+    bool estFini(SDL_Surface* support);//Support pour savoir les dimensions
+
+    protected:
+    Vecteur m_graviteY;
+    int m_rayon;
+    Uint32 m_couleur;
+    float m_resist;
+
+    std::vector<Vecteur> m_force;
+    std::vector<Vecteur> m_position;
+};
+
+#endif // PLUIEFLAMME_H_INCLUDED

+ 235 - 0
sdl1/fireRain/Vecteur.cpp

@@ -0,0 +1,235 @@
+#include "Vecteur.h"
+#include <cmath>
+
+Vecteur::Vecteur() : m_x(0.0), m_y(0.0), m_z(0.0)
+{
+
+}
+Vecteur::Vecteur(float x, float y, float z) : m_x(x), m_y(y), m_z(z)
+{
+
+}
+Vecteur::Vecteur(const Vecteur &vecteur) : m_x(vecteur.getX()), m_y(vecteur.getY()), m_z(vecteur.getZ())
+{
+
+}
+Vecteur::~Vecteur()
+{
+
+}
+float Vecteur::getX() const
+{
+    return m_x;
+}
+
+float Vecteur::getY() const
+{
+    return m_y;
+}
+
+float Vecteur::getZ() const
+{
+    return m_z;
+}
+
+
+// Setters
+
+void Vecteur::setVecteur(float x, float y, float z)
+{
+    m_x = x;
+    m_y = y;
+    m_z = z;
+}
+
+void Vecteur::setX(float x)
+{
+    m_x = x;
+}
+
+void Vecteur::setY(float y)
+{
+    m_y = y;
+}
+
+void Vecteur::setZ(float z)
+{
+    m_z = z;
+}
+void Vecteur::normaliser()
+{
+    // La fonction sqrt() permet de trouver la racine carré d'un nombre
+
+    float longueur (sqrt(m_x * m_x + m_y * m_y + m_z * m_z));
+
+
+    // Normalisation du vecteur
+
+    if(longueur != 0.0)
+    {
+        m_x /= longueur;
+        m_y /= longueur;
+        m_z /= longueur;
+    }
+}
+Vecteur& Vecteur::operator=(const Vecteur &vecteur)
+{
+    // Copie des valeurs
+
+    m_x = vecteur.m_x;
+    m_y = vecteur.m_y;
+    m_z = vecteur.m_z;
+
+
+    // Retour de l'objet
+
+    return *this;
+}
+Vecteur Vecteur::operator+(const Vecteur &vecteur)
+{
+    // Création d'un objet résultat
+
+    Vecteur resultat;
+
+
+    // Addition des coordonnées
+
+    resultat.m_x = m_x + vecteur.m_x;
+    resultat.m_y = m_y + vecteur.m_y;
+    resultat.m_z = m_z + vecteur.m_z;
+
+
+    // Retour de résultat
+
+    return resultat;
+}
+Vecteur Vecteur::operator-(const Vecteur &vecteur)
+{
+    // Création d'un objet résultat
+
+    Vecteur resultat;
+
+
+    // Soustraction des coordonnées
+
+    resultat.m_x = m_x - vecteur.m_x;
+    resultat.m_y = m_y - vecteur.m_y;
+    resultat.m_z = m_z - vecteur.m_z;
+
+
+    // Retour de résultat
+
+    return resultat;
+}
+Vecteur Vecteur::operator*(float multiplicateur)
+{
+    // Création d'un objet résultat
+
+    Vecteur resultat;
+
+
+    // Multiplication des coordonnées
+
+    resultat.m_x = m_x * multiplicateur;
+    resultat.m_y = m_y * multiplicateur;
+    resultat.m_z = m_z * multiplicateur;
+
+
+    // Retour du résultat
+
+    return resultat;
+}
+Vecteur Vecteur::operator/(float diviseur)
+{
+    // Création d'un objet résultat
+
+    Vecteur resultat;
+
+
+    // Multiplication des coordonnées
+
+    resultat.m_x = m_x / diviseur;
+    resultat.m_y = m_y / diviseur;
+    resultat.m_z = m_z / diviseur;
+
+
+    // Retour du résultat
+
+    return resultat;
+}
+Vecteur Vecteur::operator*(const Vecteur &vecteur)
+{
+    // Création d'un objet résultat
+
+    Vecteur resultat;
+
+
+    // Produit Vectoriel
+
+    resultat.m_x = (m_y * vecteur.m_z) - (m_z * vecteur.m_y);
+    resultat.m_y = (m_z * vecteur.m_x) - (m_x * vecteur.m_z);
+    resultat.m_z = (m_x * vecteur.m_y) - (m_y * vecteur.m_x);
+
+
+    // Retour de l'objet
+
+    return resultat;
+}
+void Vecteur::operator*=(const Vecteur &vecteur)
+{
+    *this = *this * vecteur;
+}
+void Vecteur::operator-=(const Vecteur &vecteur)
+{
+    *this = *this - vecteur;
+}
+void Vecteur::operator+=(const Vecteur &vecteur)
+{
+    *this = *this + vecteur;
+}
+void Vecteur::operator*=(float multiplicateur)
+{
+    *this = *this * multiplicateur;
+}
+void Vecteur::operator/=(float multiplicateur)
+{
+    *this = *this * multiplicateur;
+}
+float Vecteur::scalair(const Vecteur &vecteur)
+{
+    Vecteur v1(*this), v2(vecteur);
+    v1.normaliser();
+    v2.normaliser();
+    return v1.getX() * v2.getX() + v1.getY() * v2.getY() + v1.getZ() * v2.getZ();
+}
+bool Vecteur::operator==(const Vecteur &vecteur)
+{
+    if(m_x == vecteur.getX() && m_y == vecteur.getY() && m_z == vecteur.getZ())
+        return true;
+    else
+        return false;
+}
+bool Vecteur::operator!=(const Vecteur &vecteur)
+{
+    if(*this == vecteur)
+        return false;
+    else
+        return true;
+}
+double Vecteur::norme()
+{
+    return sqrt(m_x*m_x+m_y*m_y+m_z*m_z);
+}
+void Vecteur::rotateR(float angle)
+{
+    float tmp = m_x;
+    m_x = cos(angle)*m_x-sin(angle)*m_y;
+    m_y = sin(angle)*tmp+cos(angle)*m_y;
+}
+void Vecteur::rotateD(float angle)
+{
+    float _angle = angle*M_PI/180;
+    float tmp = m_x;
+    m_x = cos(_angle)*m_x-sin(_angle)*m_y;
+    m_y = sin(_angle)*tmp+cos(_angle)*m_y;
+}

+ 46 - 0
sdl1/fireRain/Vecteur.h

@@ -0,0 +1,46 @@
+#ifndef VECTEUR_H_INCLUDED
+#define VECTEUR_H_INCLUDED
+
+#include <cmath>
+
+class Vecteur
+{
+    public:
+
+    Vecteur();
+    Vecteur(float x, float y, float z = 0);
+    Vecteur(const Vecteur &vecteur);
+    ~Vecteur();
+    float getX() const;
+    float getY() const;
+    float getZ() const;
+    void setVecteur(float x, float y, float z);
+    void setX(float x);
+    void setY(float y);
+    void setZ(float z);
+    void normaliser();
+    Vecteur& operator=(const Vecteur &vecteur);
+    Vecteur operator+(const Vecteur &vecteur);
+    Vecteur operator-(const Vecteur &vecteur);
+    Vecteur operator*(float multiplicateur);
+    Vecteur operator/(float diviseur);
+    Vecteur operator*(const Vecteur &vecteur);
+    void operator*=(const Vecteur &vecteur);
+    void operator-=(const Vecteur &vecteur);
+    void operator+=(const Vecteur &vecteur);
+    void operator*=(float multiplicateur);
+    void operator/=(float multiplicateur);
+    float scalair(const Vecteur &vecteur);
+    bool operator==(const Vecteur &vecteur);
+    bool operator!=(const Vecteur &vecteur);
+    double norme();
+    void rotateR(float angle); // angle en radians
+    void rotateD(float angle); // angle en degrées
+
+private:
+    float m_x;
+    float m_y;
+    float m_z;
+};
+
+#endif // VECTEUR_H_INCLUDED

+ 145 - 0
sdl1/fireRain/main.cpp

@@ -0,0 +1,145 @@
+#include <iostream>
+#include <SDL.h>
+
+#include <ctime>
+#include <cstdlib>
+
+#include "PluieFlamme.h"
+/*
+#include <deque>*/
+
+using namespace std;
+
+int main ( int argc, char** argv )
+{
+    /// [1] Démarrage
+    // [1.1] Démarrages SDL
+    if ( SDL_Init( SDL_INIT_VIDEO ) < 0)
+    {
+        cout << "Impossible d'initialiser la SDL: " << SDL_GetError() << endl;
+        return 1;
+    }
+
+    // [1.2] Préparation de fermeture
+    atexit(SDL_Quit);
+
+    // [1.3] Para-fenêtre
+    SDL_WM_SetCaption("Application SDL", 0);
+
+    /// [2] Préparation des composants
+    // [2.1] Préparation de la fenêtre
+    int largeur(640);
+    int hauteur(480);
+    SDL_Surface* screen = SDL_SetVideoMode(largeur, hauteur, 32,
+                                           SDL_HWSURFACE|SDL_DOUBLEBUF);
+    if ( !screen )
+    {
+        cout << "Unable to set 640x480 video: " << SDL_GetError() << endl;
+        return 1;
+    }
+
+    // [2.2] Préparation
+    DoubleAxe souris;
+    Uint32 tempsPrecedent;
+
+    Uint8 rouge(0);
+    Uint8 vert(0);
+    Uint8 bleu(0);
+    Uint32 couleur;
+    PluieFlamme* boum(0);
+
+    int nbPop, power,  rayon;
+    float graviteY, resistance;
+
+    // [2.3] Requête de parmètres
+    cout << "Entrez les paramètres."<< endl;
+    cout << "Nombre de particules par salve: ";
+    cin >> nbPop;
+    cout << "Puissance de dissipation des particules (defaut 5): ";
+    cin >> power;
+    cout << "Gravité (défaut 0.5): ";
+    cin >> graviteY;
+    cout << "Rayon des particules (défaut 12): ";
+    cin >> rayon;
+    cout << "Resistance de l'air (défaut 0.98): ";
+    cin >> resistance;
+    cout << "Démarrage du lanceur chinois." << endl;
+
+    /// [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_MOUSEBUTTONDOWN:
+                if (event.button.button==SDL_BUTTON_LEFT && boum==0)
+                {
+                    souris.x=event.button.x;
+                    souris.y=event.button.y;
+                    boum = new PluieFlamme(souris, nbPop, power, graviteY, rayon, couleur, resistance);
+                }
+                break;
+            case SDL_MOUSEMOTION:
+                vert = event.motion.x*255/largeur;
+                rouge = event.motion.y*255/hauteur;
+                if ((int)vert+rouge > 255)
+                    bleu = 0;
+                else
+                    bleu = 255-vert-rouge;
+
+                couleur=SDL_MapRGB(screen->format,rouge,vert,bleu);
+                break;
+            } // end switch event type
+        } // end of message processing
+
+        // [3.2] Calculs
+        //nettoyer
+        if (boum!=0 && boum->estFini(screen))
+        {
+            delete boum;
+            boum = 0;
+        }
+
+        // [3.3] Dessin des composants
+        SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
+
+        if (boum!=0)
+        {
+            boum->deplacement();
+            boum->dessiner(screen);
+        }
+
+        SDL_Flip(screen);
+
+        // [3.4] Gestion du temps
+        if (SDL_GetTicks()-tempsPrecedent > 40)
+            cout << "Temps de calcul trop long ! Cadence dépassée de "
+            <<SDL_GetTicks()-tempsPrecedent-40
+            <<" mili secondes." << endl;
+        while (40>SDL_GetTicks()-tempsPrecedent)
+        {
+            //ça bloque pendant 30 ms moins le temps de calcul
+        }
+        tempsPrecedent=SDL_GetTicks();
+
+    } //fin bcl principale
+
+    ///[4] Destruction des composants
+    SDL_FreeSurface(screen);
+    if (boum!=0)
+        delete boum;
+
+    cout << "Aucune erreur détectée." << endl;
+    return 0;
+}