Black&White Game of Life with SDL

sim.c 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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(SDL_Surface *screen, Cell map[]);
  13. static void eventWatch();
  14. static void next(Cell map[]);
  15. static void drawCell(SDL_Surface *screen, SDL_Rect position, Cell Cell);
  16. //static void blitSprite(int orig, SDL_Surface *screen, SDL_Rect position);
  17. void sim(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. drawmap(screen, map);
  24. SDL_UpdateWindowSurface(ecran);
  25. eventWatch(&remaining);
  26. SDL_Delay(WAIT);
  27. next(map);
  28. remaining--;
  29. }
  30. }
  31. static void randmap(Cell map[])
  32. {
  33. for (int i = 0 ; i < MAXMAP ; i++)
  34. {
  35. map[i].x = i%NB_BLOCS;
  36. map[i].y = i/NB_BLOCS;
  37. map[i].type = rand()%2;
  38. map[i].voisines = 0;
  39. }
  40. }
  41. static void drawmap(SDL_Surface *screen, Cell map[])
  42. {
  43. SDL_Rect position;
  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(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].voisines = 0;
  93. map[i].voisines = i%NB_BLOCS == 0 ? map[i].voisines : (map[i-1].type == 1 ? ++map[i].voisines : map[i].voisines);
  94. map[i].voisines = i%(NB_BLOCS) == 11 ? map[i].voisines : (map[i+1].type == 1 ? ++map[i].voisines : map[i].voisines);
  95. map[i].voisines = i-NB_BLOCS < 0 ? map[i].voisines : (map[i-NB_BLOCS].type == 1 ? ++map[i].voisines : map[i].voisines);
  96. map[i].voisines = i+NB_BLOCS > MAXMAP ? map[i].voisines : (map[i+NB_BLOCS].type == 1 ? ++map[i].voisines : map[i].voisines);
  97. }
  98. for (int i = 0 ; i < MAXMAP ; i++)
  99. {
  100. map[i].type = map[i].type == 1 ? (map[i].voisines == 2 || map[i].voisines == 3 ? 1 : 2) : (map[i].voisines == 3 ? 1 : 0);
  101. }
  102. }
  103. static void drawCell(SDL_Surface *screen, SDL_Rect position, Cell Cell)
  104. {
  105. SDL_Surface *cell = NULL, *deadcell = NULL, *alivecell = NULL;
  106. cell = IMG_Load("sprites/cell.png");
  107. alivecell = IMG_Load("sprites/alivecell.png");
  108. deadcell = IMG_Load("sprites/deadcell.png");
  109. if (Cell.type == 0)
  110. SDL_BlitSurface(cell, NULL, screen, &position);
  111. else if (Cell.type == 2)
  112. SDL_BlitSurface(deadcell, NULL, screen, &position);
  113. else {
  114. SDL_BlitSurface(alivecell, NULL, screen, &position);
  115. }
  116. }
  117. /*static void blitSprite(int orig, SDL_Surface *screen, SDL_Rect position)
  118. {
  119. SDL_Surface *cellsheet;
  120. cellsheet = IMG_Load("sprites/cellsheet.png");
  121. SDL_Rect clip[9];
  122. clip[0].x = clip[0].y = 0;
  123. clip[1].x = 34;clip[1].y = 0;
  124. clip[2].x = 68;clip[2].y = 0;
  125. clip[3].x = 0;clip[3].y = 34;
  126. clip[4].x = clip[4].y = 34;
  127. clip[5].x = 68;clip[5].y = 34;
  128. clip[6].x = 0;clip[6].y = 68;
  129. clip[7].x = 34;clip[7].y = 68;
  130. clip[8].x = clip[8].y = 68;
  131. for (int i = 0; i > 9; i++)
  132. clip[i].h = clip[i].w = 34;
  133. SDL_BlitSurface(cellsheet, &clip[orig], screen, &position);
  134. }
  135. static void switchType(Cell map[])
  136. {
  137. }*/