Black&White Game of Life with SDL

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // sim.c
  3. // Life
  4. //
  5. // Created by Benoit Sida on 2014-06-13.
  6. // Copyright (c) 2014 Benoit Sida. All rights reserved.
  7. //
  8. #include <stdio.h>
  9. #include "constante.h"
  10. #include "sim.h"
  11. static void randmap(Cell map[]);
  12. static void drawmap(Sprites *Sprites, SDL_Surface *screen, Cell map[]);
  13. static void eventWatch();
  14. static void next(Cell map[]);
  15. static void drawCell(Sprites *Sprites, SDL_Surface *screen, SDL_Rect position, Cell Cell);
  16. static void refreshCells(Cell map[]);
  17. void sim(Sprites *Sprites, SDL_Window *ecran, SDL_Surface *screen, int cycles)
  18. {
  19. Cell map[MAXMAP];
  20. randmap(map);
  21. int remaining = cycles;
  22. while (remaining != 0){
  23. refreshCells(map);
  24. drawmap(Sprites, screen, map);
  25. SDL_UpdateWindowSurface(ecran);
  26. eventWatch(&remaining);
  27. SDL_Delay(WAIT);
  28. next(map);
  29. remaining--;
  30. }
  31. }
  32. static void randmap(Cell map[])
  33. {
  34. for (int i = 0 ; i < MAXMAP ; i++)
  35. {
  36. map[i].x = i%NB_BLOCS;
  37. map[i].y = i/NB_BLOCS;
  38. map[i].type = rand()%2;
  39. }
  40. }
  41. static void drawmap(Sprites *Sprites, SDL_Surface *screen, Cell map[])
  42. {
  43. SDL_Rect position = {0,0};
  44. SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255));
  45. for (int i = 0 ; i < MAXMAP ; i++)
  46. {
  47. position.x = map[i].x * TAILLE_BLOC;
  48. position.y = map[i].y * TAILLE_BLOC;
  49. drawCell(Sprites, screen, position, map[i]);
  50. }
  51. }
  52. static void eventWatch()
  53. {
  54. SDL_Event event;
  55. int pause = 0;
  56. SDL_PollEvent(&event);
  57. switch(event.type)
  58. {
  59. case SDL_QUIT:
  60. exit(EXIT_SUCCESS);
  61. break;
  62. case SDL_KEYDOWN:
  63. switch(event.key.keysym.sym)
  64. {
  65. case SDLK_ESCAPE:
  66. exit(EXIT_SUCCESS);
  67. break;
  68. case SDLK_SPACE:
  69. pause = 1;
  70. while (pause) {
  71. SDL_WaitEvent(&event);
  72. switch(event.type)
  73. {
  74. case SDL_KEYDOWN:
  75. switch(event.key.keysym.sym)
  76. {
  77. case SDLK_SPACE:
  78. pause = 0;
  79. break;
  80. }
  81. }
  82. }
  83. break;
  84. }
  85. break;
  86. }
  87. }
  88. static void next(Cell map[])
  89. {
  90. for (int i = 0 ; i < MAXMAP ; i++)
  91. {
  92. map[i].type = map[i].type == 1 ? (map[i].voisines == 2 || map[i].voisines == 3 ? 1 : 2) : (map[i].voisines == 3 ? 1 : 0);
  93. }
  94. }
  95. static void drawCell(Sprites *Sprites, SDL_Surface *screen, SDL_Rect position, Cell Cell)
  96. {
  97. if (Cell.type == 0)
  98. SDL_BlitSurface(Sprites->cell, NULL, screen, &position);
  99. else if (Cell.type == 2)
  100. SDL_BlitSurface(Sprites->deadcell, NULL, screen, &position);
  101. else {
  102. //SDL_BlitSurface(Sprites->alivecell, NULL, screen, &position);
  103. SDL_BlitSurface(Sprites->cellsheet, &Sprites->clip[Cell.clip], screen, &position);
  104. }
  105. }
  106. static void refreshCells(Cell map[])
  107. {
  108. for (int i = 0; i < MAXMAP; i++)
  109. {
  110. // CELL.near
  111. for (int j = 0; j < 4; j++)
  112. {
  113. map[i].near[j] = false;
  114. }
  115. map[i].near[TOP] = i-NB_BLOCS < 0 ? false : (map[i-NB_BLOCS].type == 1 ? true : false);
  116. map[i].near[LEFT] = i%NB_BLOCS == 0 ? false : (map[i-1].type == 1 ? true : false);
  117. map[i].near[RIGHT] = i%NB_BLOCS == 11 ? false : (map[i+1].type == 1 ? true : false);
  118. map[i].near[LOW] = i+NB_BLOCS > MAXMAP ? false : (map[i+NB_BLOCS].type == 1 ? true : false);
  119. //CELL.voisines
  120. map[i].voisines = 0;
  121. for (int j = 0; j < 4; j ++)
  122. {
  123. map[i].voisines = map[i].near[j] == true ? ++map[i].voisines : map[i].voisines;
  124. }
  125. // CELL.clip
  126. map[i].clip = 0;
  127. switch (map[i].voisines)
  128. {
  129. case 1:
  130. map[i].clip = map[i].near[TOP] ? 10 : (map[i].near[RIGHT] ? 11 : (map[i].near[LOW] ? 12 : 13));
  131. break;
  132. case 2:
  133. if (map[i].near[TOP])
  134. map[i].clip = map[i].near[RIGHT] ? 9 : (map[i].near[LOW] ? 8 : 7);
  135. else if (map[i].near[RIGHT])
  136. map[i].clip = map[i].near[LOW] ? 6 : 5;
  137. else
  138. map[i].clip = 4;
  139. break;
  140. case 3:
  141. if (!map[i].near[TOP])
  142. map[i].clip = 0;
  143. else if (!map[i].near[RIGHT])
  144. map[i].clip = 1;
  145. else if (!map[i].near[LOW])
  146. map[i].clip = 2;
  147. else
  148. map[i].clip = 3;
  149. break;
  150. case 4:
  151. map[i].clip = 14;
  152. break;
  153. case 0:
  154. map[i].clip = 15;
  155. break;
  156. }
  157. }
  158. }