Black&White Game of Life with SDL

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //
  2. // menu.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. static void eventChoice(int* choice, int* running, int* current);
  12. int menu(Sprites *Sprites, SDL_Window *ecran, SDL_Surface *screen)
  13. {
  14. int running = 1, choice = 0, current = 1;
  15. SDL_Rect select, position, element1 = {4,7}, element2={5,9};
  16. while (running) {
  17. for (int i = 0 ; i < MAXMAP ; i++)
  18. {
  19. position.x = i%NB_BLOCS * TAILLE_BLOC;
  20. position.y = i/NB_BLOCS * TAILLE_BLOC;
  21. SDL_BlitSurface(Sprites->cell, NULL, screen, &position);
  22. }
  23. position.x = position.y = 0;
  24. SDL_BlitSurface(Sprites->menu, NULL, screen, &position);
  25. (current == 1) ? (select.x = element1.x * TAILLE_BLOC) : (select.x = element2.x * TAILLE_BLOC);
  26. (current == 1) ? (select.y = element1.y * TAILLE_BLOC) : (select.y = element2.y * TAILLE_BLOC);
  27. SDL_BlitSurface(Sprites->arrow, NULL, screen, &select);
  28. SDL_UpdateWindowSurface(ecran);
  29. eventChoice(&choice, &running, &current);
  30. }
  31. return choice;
  32. }
  33. static void eventChoice(int* choice, int* running, int* current)
  34. {
  35. SDL_Event event;
  36. SDL_WaitEvent(&event);
  37. switch (event.type){
  38. case SDL_QUIT:
  39. exit(EXIT_SUCCESS);
  40. break;
  41. case SDL_KEYDOWN:
  42. switch (event.key.keysym.sym) {
  43. case SDLK_ESCAPE:
  44. exit(EXIT_SUCCESS);
  45. break;
  46. case SDLK_1:
  47. *choice = 1;
  48. running = 0;
  49. break;
  50. case SDLK_2:
  51. *choice = 2;
  52. running = 0;
  53. break;
  54. case SDLK_UP:
  55. *current = 1;
  56. break;
  57. case SDLK_DOWN:
  58. *current = 2;
  59. break;
  60. case SDLK_RETURN:
  61. *running = 0;
  62. *choice = *current;
  63. break;
  64. }
  65. break;
  66. }
  67. }