Black&White Game of Life with SDL

menu.c 2.1KB

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