FeuFolet.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include "FeuFolet.h"
  2. SDL_Surface* createFeuFolet(int rayon, Uint32 couleurRGB)
  3. {
  4. //[1] Création des surfaces
  5. int largeur(rayon*2);
  6. SDL_Surface* resultat = SDL_CreateRGBSurface(SDL_HWSURFACE, largeur, largeur, 32, 0, 0, 0, 0);
  7. SDL_FillRect(resultat,0,couleurRGB);
  8. SDL_SetColorKey(resultat, SDL_SRCCOLORKEY, couleurRGB);
  9. //[2] Traitements
  10. blitFeuFollet(resultat, 0, 0, rayon, couleurRGB);
  11. //[FIN] Renvoi
  12. return resultat;
  13. }///createFeuFolet()
  14. void blitFeuFollet(SDL_Surface* support, int x, int y, int rayon, Uint32 couleurRGB, short opacite)
  15. {
  16. //[1] Création des surfaces
  17. SDL_Surface* pixel = SDL_CreateRGBSurface(SDL_HWSURFACE, 1, 1, 32, 0, 0, 0, 0);
  18. SDL_Surface* whitePixel = SDL_CreateRGBSurface(SDL_HWSURFACE, 1, 1, 32, 0, 0, 0, 0);
  19. //[2] Traitements
  20. SDL_FillRect(pixel,0,couleurRGB);
  21. SDL_FillRect(whitePixel,0,SDL_MapRGB(whitePixel->format,255,255,255));
  22. if (opacite>255)
  23. opacite=255;
  24. else if (opacite<0)
  25. opacite=0;
  26. if (rayon<=0)
  27. rayon=1;
  28. //[3] Balayage
  29. int distance(0);//distance au carré, évidemment ;-)
  30. Uint8 transparence(0);
  31. SDL_Rect position;
  32. for (position.y=y; position.y<y+2*rayon; position.y++)
  33. {
  34. for (position.x=x; position.x<x+2*rayon; position.x++)
  35. {
  36. distance=carre(position.x-rayon-x)+carre(position.y-rayon-y);
  37. if (distance<carre(rayon))
  38. {
  39. transparence=255-(float)distance/carre(rayon)*256;//+ c'est élevé, + c'est opaque
  40. if (transparence>opacite)
  41. transparence-=opacite;
  42. else
  43. transparence=0;
  44. SDL_SetAlpha(pixel,SDL_SRCALPHA,transparence);//On met la transparence
  45. SDL_BlitSurface(pixel, 0, support, &position);
  46. SDL_SetAlpha(whitePixel,SDL_SRCALPHA,transparence);//On met la transparence
  47. SDL_BlitSurface(whitePixel, 0, support, &position);
  48. }
  49. }
  50. }
  51. //[5] Destruction des éléments
  52. SDL_FreeSurface(pixel);
  53. SDL_FreeSurface(whitePixel);
  54. }
  55. int carre(int nombre)
  56. {
  57. return nombre*nombre;
  58. }///carre()