Browse Source

Import feu follet project

DricomDragon 4 years ago
parent
commit
1cda92174c
3 changed files with 204 additions and 0 deletions
  1. 73 0
      feuFollet/FeuFolet.cpp
  2. 41 0
      feuFollet/FeuFolet.h
  3. 90 0
      feuFollet/main.cpp

+ 73 - 0
feuFollet/FeuFolet.cpp

@@ -0,0 +1,73 @@
+#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);
+    SDL_FillRect(resultat,0,couleurRGB);
+    SDL_SetColorKey(resultat, SDL_SRCCOLORKEY, couleurRGB);
+
+    //[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
feuFollet/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: 19/05/2014
+///Objet: nouvelle fonction: create feu follet
+
+/*
+==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

+ 90 - 0
feuFollet/main.cpp

@@ -0,0 +1,90 @@
+#include <iostream>
+#include <SDL.h>
+
+#include "FeuFolet.h"
+
+#define RAYON 160
+
+int main ( int argc, char** argv )
+{
+    /// [1] Démarrage
+    // [1.1] Démarrages SDL
+    if ( SDL_Init( SDL_INIT_VIDEO ) < 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("Application SDL: Algorythme du Feu Follet", 0);
+
+    /// [2] Préparation des composants
+    // [2.1] Préparation de la fenêtre
+    SDL_Surface* screen = SDL_SetVideoMode(640, 480, 32,
+                                           SDL_HWSURFACE|SDL_DOUBLEBUF);
+    if ( !screen )
+    {
+        std::cout << "Unable to set 640x480 video: " << SDL_GetError() << std::endl;
+        return 1;
+    }
+
+    // [2.2] Préparation
+    SDL_Surface* fond = SDL_LoadBMP("soleil.bmp");
+    SDL_Surface* follet = createFeuFolet(RAYON, SDL_MapRGB(screen->format, 0, 0, 255));
+    SDL_Rect pos;
+    pos.x=pos.y=0;
+
+    short opacite (0);
+
+    /// [3] Boucle principale
+    SDL_EnableKeyRepeat(10,10);
+    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;
+                else if (event.key.keysym.sym == SDLK_UP)
+                    opacite++;
+                else if (event.key.keysym.sym == SDLK_DOWN)
+                    opacite--;
+                break;
+            case SDL_MOUSEMOTION:
+                pos.x = event.motion.x-RAYON;
+                pos.y = event.motion.y-RAYON;
+                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));
+
+        SDL_BlitSurface(fond, 0, screen, 0);
+        SDL_BlitSurface(follet, 0, screen, &pos);
+
+        //blitFeuFollet(screen, xFeu, yFeu, RAYON, SDL_MapRGB(screen->format,0,0,255),opacite);
+
+        SDL_Flip(screen);
+    } //fin bcl principale
+
+    ///[4] Destruction des composants
+    SDL_FreeSurface(fond);
+    SDL_FreeSurface(follet);
+
+    std::cout << "Aucune erreur détectée." << std::endl;
+    return 0;
+}