FeuFolet.cpp 2.0 KB

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