Black&White Game of Life with SDL

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //
  2. // main.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 "menu.h"
  11. #include "sim.h"
  12. static void initSprites(Sprites *Sprites);
  13. int main(int argc, const char * argv[])
  14. {
  15. if (SDL_Init(SDL_INIT_VIDEO) == -1) {
  16. printf("Erreur lors de l'initialisation du module SDL. Erreur : %s", SDL_GetError());
  17. exit(EXIT_FAILURE);
  18. }
  19. int choice = 0;
  20. SDL_Window *ecran;
  21. SDL_Surface *screen;
  22. Sprites Sprites;
  23. ecran = SDL_CreateWindow("LIFE", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, LARGEUR_ECRAN, HAUTEUR_ECRAN, SDL_WINDOW_SHOWN);
  24. screen = SDL_GetWindowSurface(ecran);
  25. initSprites(&Sprites);
  26. SDL_SetWindowIcon(ecran, Sprites.icon);
  27. while (choice == 0) {
  28. choice = menu(&Sprites, ecran, screen);
  29. switch (choice) {
  30. case 1:
  31. sim(&Sprites, ecran, screen, CYCLES);
  32. choice = 0;
  33. break;
  34. case 2:
  35. exit(EXIT_SUCCESS);
  36. break;
  37. default :
  38. choice = 0;
  39. break;
  40. }
  41. }
  42. SDL_Quit();
  43. return EXIT_SUCCESS;
  44. }
  45. static void initSprites(Sprites *Sprites)
  46. {
  47. Sprites->cell = IMG_Load("sprites/cell.png");
  48. Sprites->alivecell = IMG_Load("sprites/alivecell.png");
  49. Sprites->deadcell = IMG_Load("sprites/deadcell.png");
  50. Sprites->cellsheet = IMG_Load("sprites/cellsheet.png");
  51. Sprites->menu = IMG_Load("sprites/menu.png");
  52. Sprites->arrow = IMG_Load("sprites/arrow.png");
  53. Sprites->icon = IMG_Load("sprites/icon.png");
  54. for (int i = 0; i < SHEETSIZE; i++)
  55. {
  56. Sprites->clip[i].x = (i%(SHEETSIZE/4))*TAILLE_BLOC;
  57. Sprites->clip[i].y = (i/(SHEETSIZE/4))*TAILLE_BLOC;
  58. Sprites->clip[i].h = Sprites->clip[i].w = TAILLE_BLOC;
  59. }
  60. }