瀏覽代碼

Import funtain project

DricomDragon 4 年之前
父節點
當前提交
335399775b
共有 5 個文件被更改,包括 287 次插入0 次删除
  1. 7 0
      colorFuntain/Coordonnee.cpp
  2. 13 0
      colorFuntain/Coordonnee.h
  3. 71 0
      colorFuntain/FeuFolet.cpp
  4. 41 0
      colorFuntain/FeuFolet.h
  5. 155 0
      colorFuntain/main.cpp

+ 7 - 0
colorFuntain/Coordonnee.cpp

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

+ 13 - 0
colorFuntain/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
colorFuntain/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
colorFuntain/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

+ 155 - 0
colorFuntain/main.cpp

@@ -0,0 +1,155 @@
+#include <iostream>
+#include <SDL.h>
+
+#include <ctime>
+#include <cstdlib>
+
+#include <deque>
+
+#include "Coordonnee.h"
+#include "FeuFolet.h"
+
+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
+    srand(time(0));
+    Uint32 tempsPrecedent(0);
+
+    int power(5);
+    int graviteY(1);
+    int nbPop(5);
+    int rayon(12);
+    Uint32 couleur(0);
+    Uint8 rouge(0);
+    Uint8 vert(0);
+    Uint8 bleu(0);
+
+    DoubleAxe saved;
+    DoubleAxe souris;
+    initialiserDbAxes(&saved);
+    initialiserDbAxes(&souris);
+
+    deque<DoubleAxe> precedent;
+    deque<DoubleAxe> maintenant;
+    deque<Uint32> couleurBille;
+
+    SDL_ShowCursor(SDL_DISABLE);
+    /// [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_MOUSEMOTION:
+                souris.x = event.motion.x;
+                souris.y = event.motion.y;
+                vert = souris.x*255/largeur;
+                rouge = souris.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
+        //Ajout des nouveaux feux
+        for (int i(0); i<nbPop; i++)
+        {
+            //Positions
+            maintenant.push_back(souris);//Position souris
+            saved.x=souris.x+(power-rand()%(power*2+1));
+            saved.y=souris.y/*+(power-rand()%(power*2+1))*/+power;//+power rajouté
+            precedent.push_back(saved);//Orientation
+
+            //Couleur
+            couleurBille.push_back(couleur);
+        }
+
+        //Deplacement
+        for (Uint32 i(0); i<maintenant.size(); i++)
+        {
+            saved=maintenant[i];
+            maintenant[i].x+=maintenant[i].x-precedent[i].x;
+            maintenant[i].y+=maintenant[i].y-precedent[i].y+graviteY;
+            precedent[i]=saved;
+        }
+        //nettoyer
+        while((maintenant.front().x>largeur || maintenant.front().x<0
+              || maintenant.front().y>hauteur || maintenant.front().y<0)
+              && !maintenant.empty())
+        {
+            maintenant.pop_front();
+            precedent.pop_front();
+            couleurBille.pop_front();
+        }
+
+        // [3.3] Dessin des composants
+        SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
+
+        if (!maintenant.empty())
+            for (Uint32 i(0); i<maintenant.size(); i++)
+                blitFeuFollet(screen, maintenant[i].x, maintenant[i].y, rayon, couleurBille[i], 128);
+
+        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);
+
+    cout << "Aucune erreur détectée." << endl;
+    return 0;
+}