#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()