snake.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * Interface definition of the Snake game.
  3. *
  4. * This code is public domain. Feel free to use it for any purpose!
  5. */
  6. #ifndef SNAKE_H
  7. #define SNAKE_H
  8. #include <SDL3/SDL.h>
  9. #define SNAKE_GAME_WIDTH 24U
  10. #define SNAKE_GAME_HEIGHT 18U
  11. #define SNAKE_MATRIX_SIZE (SNAKE_GAME_WIDTH * SNAKE_GAME_HEIGHT)
  12. typedef enum
  13. {
  14. SNAKE_CELL_NOTHING = 0U,
  15. SNAKE_CELL_SRIGHT = 1U,
  16. SNAKE_CELL_SUP = 2U,
  17. SNAKE_CELL_SLEFT = 3U,
  18. SNAKE_CELL_SDOWN = 4U,
  19. SNAKE_CELL_FOOD = 5U
  20. } SnakeCell;
  21. #define SNAKE_CELL_MAX_BITS 3U /* floor(log2(SNAKE_CELL_FOOD)) + 1 */
  22. typedef enum
  23. {
  24. SNAKE_DIR_RIGHT,
  25. SNAKE_DIR_UP,
  26. SNAKE_DIR_LEFT,
  27. SNAKE_DIR_DOWN
  28. } SnakeDirection;
  29. typedef struct
  30. {
  31. unsigned char cells[(SNAKE_MATRIX_SIZE * SNAKE_CELL_MAX_BITS) / 8U];
  32. char head_xpos;
  33. char head_ypos;
  34. char tail_xpos;
  35. char tail_ypos;
  36. char next_dir;
  37. char inhibit_tail_step;
  38. unsigned occupied_cells;
  39. } SnakeContext;
  40. typedef Sint32 (SDLCALL *RandFunc)(Sint32 n);
  41. void snake_initialize(SnakeContext *ctx, RandFunc rand);
  42. void snake_redir(SnakeContext *ctx, SnakeDirection dir);
  43. void snake_step(SnakeContext *ctx, RandFunc rand);
  44. SnakeCell snake_cell_at(const SnakeContext *ctx, char x, char y);
  45. #endif /* SNAKE_H */