snake.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * Logic implementation of the Snake game. It is designed to efficiently
  3. * represent in memory the state of the game.
  4. *
  5. * This code is public domain. Feel free to use it for any purpose!
  6. */
  7. #define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
  8. #include <SDL3/SDL.h>
  9. #include <SDL3/SDL_main.h>
  10. #define STEP_RATE_IN_MILLISECONDS 125
  11. #define SNAKE_BLOCK_SIZE_IN_PIXELS 24
  12. #define SDL_WINDOW_WIDTH (SNAKE_BLOCK_SIZE_IN_PIXELS * SNAKE_GAME_WIDTH)
  13. #define SDL_WINDOW_HEIGHT (SNAKE_BLOCK_SIZE_IN_PIXELS * SNAKE_GAME_HEIGHT)
  14. #define SNAKE_GAME_WIDTH 24U
  15. #define SNAKE_GAME_HEIGHT 18U
  16. #define SNAKE_MATRIX_SIZE (SNAKE_GAME_WIDTH * SNAKE_GAME_HEIGHT)
  17. #define THREE_BITS 0x7U /* ~CHAR_MAX >> (CHAR_BIT - SNAKE_CELL_MAX_BITS) */
  18. #define SHIFT(x, y) (((x) + ((y) * SNAKE_GAME_WIDTH)) * SNAKE_CELL_MAX_BITS)
  19. typedef enum
  20. {
  21. SNAKE_CELL_NOTHING = 0U,
  22. SNAKE_CELL_SRIGHT = 1U,
  23. SNAKE_CELL_SUP = 2U,
  24. SNAKE_CELL_SLEFT = 3U,
  25. SNAKE_CELL_SDOWN = 4U,
  26. SNAKE_CELL_FOOD = 5U
  27. } SnakeCell;
  28. #define SNAKE_CELL_MAX_BITS 3U /* floor(log2(SNAKE_CELL_FOOD)) + 1 */
  29. typedef enum
  30. {
  31. SNAKE_DIR_RIGHT,
  32. SNAKE_DIR_UP,
  33. SNAKE_DIR_LEFT,
  34. SNAKE_DIR_DOWN
  35. } SnakeDirection;
  36. typedef struct
  37. {
  38. unsigned char cells[(SNAKE_MATRIX_SIZE * SNAKE_CELL_MAX_BITS) / 8U];
  39. char head_xpos;
  40. char head_ypos;
  41. char tail_xpos;
  42. char tail_ypos;
  43. char next_dir;
  44. char inhibit_tail_step;
  45. unsigned occupied_cells;
  46. } SnakeContext;
  47. typedef Sint32 (SDLCALL *RandFunc)(Sint32 n);
  48. typedef struct
  49. {
  50. SDL_Window *window;
  51. SDL_Renderer *renderer;
  52. SDL_TimerID step_timer;
  53. SnakeContext snake_ctx;
  54. } AppState;
  55. SnakeCell snake_cell_at(const SnakeContext *ctx, char x, char y)
  56. {
  57. const int shift = SHIFT(x, y);
  58. unsigned short range;
  59. SDL_memcpy(&range, ctx->cells + (shift / 8), sizeof(range));
  60. return (SnakeCell)((range >> (shift % 8)) & THREE_BITS);
  61. }
  62. static void set_rect_xy_(SDL_FRect *r, short x, short y)
  63. {
  64. r->x = (float)(x * SNAKE_BLOCK_SIZE_IN_PIXELS);
  65. r->y = (float)(y * SNAKE_BLOCK_SIZE_IN_PIXELS);
  66. }
  67. static void put_cell_at_(SnakeContext *ctx, char x, char y, SnakeCell ct)
  68. {
  69. const int shift = SHIFT(x, y);
  70. const int adjust = shift % 8;
  71. unsigned char *const pos = ctx->cells + (shift / 8);
  72. unsigned short range;
  73. SDL_memcpy(&range, pos, sizeof(range));
  74. range &= ~(THREE_BITS << adjust); /* clear bits */
  75. range |= (ct & THREE_BITS) << adjust;
  76. SDL_memcpy(pos, &range, sizeof(range));
  77. }
  78. static int are_cells_full_(SnakeContext *ctx)
  79. {
  80. return ctx->occupied_cells == SNAKE_GAME_WIDTH * SNAKE_GAME_HEIGHT;
  81. }
  82. static void new_food_pos_(SnakeContext *ctx, RandFunc rand)
  83. {
  84. while (SDL_TRUE) {
  85. const char x = (char) rand(SNAKE_GAME_WIDTH);
  86. const char y = (char) rand(SNAKE_GAME_HEIGHT);
  87. if (snake_cell_at(ctx, x, y) == SNAKE_CELL_NOTHING) {
  88. put_cell_at_(ctx, x, y, SNAKE_CELL_FOOD);
  89. break;
  90. }
  91. }
  92. }
  93. void snake_initialize(SnakeContext *ctx, RandFunc rand)
  94. {
  95. int i;
  96. SDL_zeroa(ctx->cells);
  97. ctx->head_xpos = ctx->tail_xpos = SNAKE_GAME_WIDTH / 2;
  98. ctx->head_ypos = ctx->tail_ypos = SNAKE_GAME_HEIGHT / 2;
  99. ctx->next_dir = SNAKE_DIR_RIGHT;
  100. ctx->inhibit_tail_step = ctx->occupied_cells = 4;
  101. --ctx->occupied_cells;
  102. put_cell_at_(ctx, ctx->tail_xpos, ctx->tail_ypos, SNAKE_CELL_SRIGHT);
  103. for (i = 0; i < 4; i++) {
  104. new_food_pos_(ctx, rand);
  105. ++ctx->occupied_cells;
  106. }
  107. }
  108. void snake_redir(SnakeContext *ctx, SnakeDirection dir)
  109. {
  110. SnakeCell ct = snake_cell_at(ctx, ctx->head_xpos, ctx->head_ypos);
  111. if ((dir == SNAKE_DIR_RIGHT && ct != SNAKE_CELL_SLEFT) ||
  112. (dir == SNAKE_DIR_UP && ct != SNAKE_CELL_SDOWN) ||
  113. (dir == SNAKE_DIR_LEFT && ct != SNAKE_CELL_SRIGHT) ||
  114. (dir == SNAKE_DIR_DOWN && ct != SNAKE_CELL_SUP)) {
  115. ctx->next_dir = dir;
  116. }
  117. }
  118. static void wrap_around_(char *val, char max)
  119. {
  120. if (*val < 0) {
  121. *val = max - 1;
  122. } else if (*val > max - 1) {
  123. *val = 0;
  124. }
  125. }
  126. void snake_step(SnakeContext *ctx, RandFunc rand)
  127. {
  128. const SnakeCell dir_as_cell = (SnakeCell)(ctx->next_dir + 1);
  129. SnakeCell ct;
  130. char prev_xpos;
  131. char prev_ypos;
  132. /* Move tail forward */
  133. if (--ctx->inhibit_tail_step == 0) {
  134. ++ctx->inhibit_tail_step;
  135. ct = snake_cell_at(ctx, ctx->tail_xpos, ctx->tail_ypos);
  136. put_cell_at_(ctx, ctx->tail_xpos, ctx->tail_ypos, SNAKE_CELL_NOTHING);
  137. switch (ct) {
  138. case SNAKE_CELL_SRIGHT:
  139. ctx->tail_xpos++;
  140. break;
  141. case SNAKE_CELL_SUP:
  142. ctx->tail_ypos--;
  143. break;
  144. case SNAKE_CELL_SLEFT:
  145. ctx->tail_xpos--;
  146. break;
  147. case SNAKE_CELL_SDOWN:
  148. ctx->tail_ypos++;
  149. break;
  150. default:
  151. break;
  152. }
  153. wrap_around_(&ctx->tail_xpos, SNAKE_GAME_WIDTH);
  154. wrap_around_(&ctx->tail_ypos, SNAKE_GAME_HEIGHT);
  155. }
  156. /* Move head forward */
  157. prev_xpos = ctx->head_xpos;
  158. prev_ypos = ctx->head_ypos;
  159. switch (ctx->next_dir) {
  160. case SNAKE_DIR_RIGHT:
  161. ++ctx->head_xpos;
  162. break;
  163. case SNAKE_DIR_UP:
  164. --ctx->head_ypos;
  165. break;
  166. case SNAKE_DIR_LEFT:
  167. --ctx->head_xpos;
  168. break;
  169. case SNAKE_DIR_DOWN:
  170. ++ctx->head_ypos;
  171. break;
  172. }
  173. wrap_around_(&ctx->head_xpos, SNAKE_GAME_WIDTH);
  174. wrap_around_(&ctx->head_ypos, SNAKE_GAME_HEIGHT);
  175. /* Collisions */
  176. ct = snake_cell_at(ctx, ctx->head_xpos, ctx->head_ypos);
  177. if (ct != SNAKE_CELL_NOTHING && ct != SNAKE_CELL_FOOD) {
  178. snake_initialize(ctx, rand);
  179. return;
  180. }
  181. put_cell_at_(ctx, prev_xpos, prev_ypos, dir_as_cell);
  182. put_cell_at_(ctx, ctx->head_xpos, ctx->head_ypos, dir_as_cell);
  183. if (ct == SNAKE_CELL_FOOD) {
  184. if (are_cells_full_(ctx)) {
  185. snake_initialize(ctx, rand);
  186. return;
  187. }
  188. new_food_pos_(ctx, rand);
  189. ++ctx->inhibit_tail_step;
  190. ++ctx->occupied_cells;
  191. }
  192. }
  193. static Uint32 sdl_timer_callback_(void *payload, SDL_TimerID timer_id, Uint32 interval)
  194. {
  195. /* NOTE: snake_step is not called here directly for multithreaded concerns. */
  196. SDL_Event event;
  197. SDL_zero(event);
  198. event.type = SDL_EVENT_USER;
  199. SDL_PushEvent(&event);
  200. return interval;
  201. }
  202. static int handle_key_event_(SnakeContext *ctx, SDL_Scancode key_code)
  203. {
  204. switch (key_code) {
  205. /* Quit. */
  206. case SDL_SCANCODE_ESCAPE:
  207. case SDL_SCANCODE_Q:
  208. return SDL_APP_SUCCESS;
  209. /* Restart the game as if the program was launched. */
  210. case SDL_SCANCODE_R:
  211. snake_initialize(ctx, SDL_rand);
  212. break;
  213. /* Decide new direction of the snake. */
  214. case SDL_SCANCODE_RIGHT:
  215. snake_redir(ctx, SNAKE_DIR_RIGHT);
  216. break;
  217. case SDL_SCANCODE_UP:
  218. snake_redir(ctx, SNAKE_DIR_UP);
  219. break;
  220. case SDL_SCANCODE_LEFT:
  221. snake_redir(ctx, SNAKE_DIR_LEFT);
  222. break;
  223. case SDL_SCANCODE_DOWN:
  224. snake_redir(ctx, SNAKE_DIR_DOWN);
  225. break;
  226. default:
  227. break;
  228. }
  229. return SDL_APP_CONTINUE;
  230. }
  231. SDL_AppResult SDL_AppIterate(void *appstate)
  232. {
  233. AppState *as;
  234. SnakeContext *ctx;
  235. SDL_FRect r;
  236. unsigned i;
  237. unsigned j;
  238. int ct;
  239. as = (AppState *)appstate;
  240. ctx = &as->snake_ctx;
  241. r.w = r.h = SNAKE_BLOCK_SIZE_IN_PIXELS;
  242. SDL_SetRenderDrawColor(as->renderer, 0, 0, 0, 255);
  243. SDL_RenderClear(as->renderer);
  244. for (i = 0; i < SNAKE_GAME_WIDTH; i++) {
  245. for (j = 0; j < SNAKE_GAME_HEIGHT; j++) {
  246. ct = snake_cell_at(ctx, i, j);
  247. if (ct == SNAKE_CELL_NOTHING)
  248. continue;
  249. set_rect_xy_(&r, i, j);
  250. if (ct == SNAKE_CELL_FOOD)
  251. SDL_SetRenderDrawColor(as->renderer, 0, 0, 128, 255);
  252. else /* body */
  253. SDL_SetRenderDrawColor(as->renderer, 0, 128, 0, 255);
  254. SDL_RenderFillRect(as->renderer, &r);
  255. }
  256. }
  257. SDL_SetRenderDrawColor(as->renderer, 255, 255, 0, 255); /*head*/
  258. set_rect_xy_(&r, ctx->head_xpos, ctx->head_ypos);
  259. SDL_RenderFillRect(as->renderer, &r);
  260. SDL_RenderPresent(as->renderer);
  261. return SDL_APP_CONTINUE;
  262. }
  263. SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
  264. {
  265. if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER)) {
  266. return SDL_APP_FAILURE;
  267. }
  268. AppState *as = SDL_calloc(1, sizeof(AppState));
  269. *appstate = as;
  270. if (!SDL_CreateWindowAndRenderer("examples/game/snake", SDL_WINDOW_WIDTH, SDL_WINDOW_HEIGHT, 0, &as->window, &as->renderer)) {
  271. return SDL_APP_FAILURE;
  272. }
  273. snake_initialize(&as->snake_ctx, SDL_rand);
  274. as->step_timer = SDL_AddTimer(STEP_RATE_IN_MILLISECONDS, sdl_timer_callback_, NULL);
  275. if (as->step_timer == 0) {
  276. return SDL_APP_FAILURE;
  277. }
  278. return SDL_APP_CONTINUE;
  279. }
  280. SDL_AppResult SDL_AppEvent(void *appstate, const SDL_Event *event)
  281. {
  282. SnakeContext *ctx = &((AppState *)appstate)->snake_ctx;
  283. switch (event->type) {
  284. case SDL_EVENT_QUIT:
  285. return SDL_APP_SUCCESS;
  286. case SDL_EVENT_USER:
  287. snake_step(ctx, SDL_rand);
  288. break;
  289. case SDL_EVENT_KEY_DOWN:
  290. return handle_key_event_(ctx, event->key.scancode);
  291. }
  292. return SDL_APP_CONTINUE;
  293. }
  294. void SDL_AppQuit(void *appstate)
  295. {
  296. if (appstate != NULL) {
  297. AppState *as = (AppState *)appstate;
  298. SDL_RemoveTimer(as->step_timer);
  299. SDL_DestroyRenderer(as->renderer);
  300. SDL_DestroyWindow(as->window);
  301. SDL_free(as);
  302. }
  303. }