Point.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include "Point.h"
  2. Point::Point(int rayon,Uint32 couleur)
  3. :m_rayon(rayon)
  4. {
  5. m_pixel = SDL_CreateRGBSurface(SDL_HWSURFACE, 1, 1, 16, 0, 0, 0, 0);
  6. SDL_FillRect(m_pixel,NULL,couleur);
  7. if (!m_pixel)
  8. {
  9. std::cout << "Impossible de créer la surface dans l'objet Point " << SDL_GetError() << std::endl;
  10. }
  11. }///Constructeur standard
  12. Point::Point(int rayon,Uint32 couleur,int transparence)
  13. :m_rayon(rayon)
  14. {
  15. m_pixel = SDL_CreateRGBSurface(SDL_HWSURFACE, 1, 1, 16, 0, 0, 0, 0);
  16. SDL_FillRect(m_pixel,NULL,couleur);
  17. if (!m_pixel)
  18. {
  19. std::cout << "Impossible de créer la surface dans l'objet Point " << SDL_GetError() << std::endl;
  20. }
  21. }///Constructeur +transparence
  22. Point::~Point()
  23. {
  24. SDL_FreeSurface(m_pixel);
  25. m_pixel=0;
  26. std::cout << "Destruction du point effective. " << std::endl;
  27. }///Destructeur
  28. int Point::getRayon()
  29. {
  30. return m_rayon;
  31. }///getRayon
  32. void Point::afficherPoint(SDL_Surface *screen,int const x, int const y)
  33. {
  34. int distance(0);
  35. SDL_Rect position;
  36. for (int i(x-m_rayon); i<=x+m_rayon; i++)
  37. {
  38. for (int j(y-m_rayon); j<=y+m_rayon; j++)
  39. {
  40. distance = pow(i-x,2)+pow(j-y,2);
  41. if (distance <= pow(m_rayon,2))
  42. {
  43. position.x=i;
  44. position.y=j;
  45. SDL_BlitSurface(m_pixel, 0, screen, &position);
  46. }//test blit
  47. }//for j
  48. }//for i
  49. }///afficherPoint
  50. SDL_Surface* createPoint(int rayon,Uint32 couleur)
  51. {
  52. return createCircle(rayon,couleur,0);
  53. }///createPoint
  54. SDL_Surface* createCircle(int rayOut,Uint32 couleur,int rayIn)
  55. {
  56. // [1] Création des composants
  57. int diametre(rayOut*2);
  58. SDL_Surface* point = SDL_CreateRGBSurface(SDL_HWSURFACE, diametre, diametre, 16, 0, 0, 0, 0);
  59. SDL_Surface* pixel = SDL_CreateRGBSurface(SDL_HWSURFACE, 1, 1, 16, 0, 0, 0, 0);
  60. SDL_FillRect(pixel,NULL,couleur);
  61. int distance(0);
  62. int ext(rayOut*rayOut);
  63. int ins(rayIn*rayIn);
  64. SDL_Rect pos={0,0,0,0};
  65. int x;
  66. int y;
  67. // [2] Traçage du cercle
  68. for (x=-rayOut; x<rayOut; x++)
  69. {
  70. for (y=-rayOut; y<rayOut; y++)
  71. {
  72. distance = y*y+x*x;
  73. if (distance <= ext && distance >= ins)
  74. {
  75. pos.x=x+rayOut;
  76. pos.y=y+rayOut;
  77. SDL_BlitSurface(pixel, 0, point, &pos);
  78. }//test blit
  79. }//for j
  80. }//for i
  81. // [3] Traitement transparence
  82. SDL_SetColorKey(point,SDL_SRCCOLORKEY,SDL_MapRGB(point->format,0,0,0));
  83. // [4] Renvoi
  84. SDL_FreeSurface(pixel);
  85. return point;
  86. }///createCircle