Collisions.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "Collisions.h"
  2. Collisions::Collisions(SDL_Surface *screen, int const xBlocSize, int const yBlocSize)
  3. : m_screen(screen), m_xSize(xBlocSize), m_ySize(yBlocSize)
  4. {
  5. // Tableau dynamique
  6. m_tableau = new int*[yBlocSize];
  7. for (int i(0); i<yBlocSize; i++)
  8. m_tableau[i] = new int[xBlocSize];
  9. // Différents murs
  10. m_carreStd = SDL_LoadBMP("Textures/Mur.bmp");
  11. for (int i(0); i<12; i++)
  12. m_carre[i] = colorMightyObjet(m_carreStd, i);
  13. // Autres images
  14. m_sol = SDL_LoadBMP("Textures/Sol.bmp");
  15. m_terrain = SDL_CreateRGBSurface(SDL_HWSURFACE, m_screen->w, m_screen->h, 32, 0, 0, 0, 0);
  16. m_position.x = m_position.y = 0;
  17. // Initialisation standard
  18. reinitialiser();
  19. }///Constructeur
  20. Collisions::~Collisions()
  21. {
  22. // Destruction du tableau dynamique
  23. for (int i(0); i < m_ySize; i++)
  24. delete[] m_tableau[i];
  25. delete[] m_tableau;
  26. m_tableau = 0;
  27. // Libération des surfaces
  28. for (int i(0); i<4; i++)
  29. SDL_FreeSurface(m_carre[i]);
  30. SDL_FreeSurface(m_sol);
  31. SDL_FreeSurface(m_terrain);
  32. SDL_FreeSurface(m_carreStd);
  33. }///Destructeur
  34. void Collisions::ajouter(int x, int y, int ID)
  35. {
  36. // Correction position
  37. coorectPos(x, y);
  38. // Blitage de surface
  39. m_position.y=y*20;
  40. m_position.x=x*20;
  41. if (0<=ID && ID<=4 && (m_tableau[y][x]==VIDE || m_tableau[y][x] == ID))
  42. {
  43. m_tableau[y][x] = ID;
  44. SDL_BlitSurface(m_carre[ID], 0, m_terrain, &m_position);
  45. }
  46. else
  47. {
  48. m_tableau[y][x] = NEUTRE;
  49. SDL_BlitSurface(m_carreStd, 0, m_terrain, &m_position);
  50. }
  51. // Signalement
  52. }///ajouter
  53. void Collisions::enlever(int x, int y)
  54. {
  55. // Correction position
  56. coorectPos(x, y);
  57. // Blitage de surface
  58. m_position.y=y*20;
  59. m_position.x=x*20;
  60. SDL_BlitSurface(m_sol, 0, m_terrain, &m_position);
  61. // Signalement
  62. m_tableau[y][x] = VIDE;
  63. }///enlever
  64. int Collisions::tester(int x,int y) const
  65. {
  66. coorectPos(x,y);
  67. return m_tableau[y][x];
  68. }///tester
  69. void Collisions::afficher()
  70. {
  71. SDL_BlitSurface(m_terrain, 0, m_screen, 0);
  72. }///dessiner
  73. void Collisions::reinitialiser()
  74. {
  75. for (int i(0); i < m_ySize; i++)
  76. {
  77. for (int j(0); j < m_xSize; j++)
  78. {
  79. m_tableau[i][j] = VIDE;
  80. m_position.y = i*20;
  81. m_position.x = j*20;
  82. SDL_BlitSurface(m_sol, 0, m_terrain, &m_position);
  83. }
  84. }
  85. }///reinitialiser
  86. void Collisions::coorectPos(int &x, int &y) const
  87. {
  88. while ( x >= m_xSize )
  89. x -= m_xSize;
  90. while ( x < 0 )
  91. x += m_xSize;
  92. while ( y >= m_ySize )
  93. y -= m_ySize;
  94. while ( y < 0 )
  95. y += m_ySize;
  96. }///coorectPos
  97. int Collisions::getSize(int idCoord)
  98. {
  99. if (idCoord) // y -> 1
  100. return m_ySize;
  101. else // x -> 0
  102. return m_xSize;
  103. }