diff --git a/Makefile b/Makefile index 119ac4d..fe34670 100644 --- a/Makefile +++ b/Makefile @@ -5,6 +5,9 @@ clean: compile: gcc -W -Wall -Wextra -pedantic -o out/main src/main.c src/utils/arena.c src/utils/vector.c -Iinclude -Iutils -lpthread -Llib -lSDL3 +debug: + gcc -W -Wall -Wextra -pedantic -g -O0 -o out/main_debug src/main.c src/utils/arena.c src/utils/vector.c -Iinclude -Iutils -lpthread -Llib -lSDL3 + run: make compile ./out/main diff --git a/src/main.c b/src/main.c index a02774a..c5ec36c 100644 --- a/src/main.c +++ b/src/main.c @@ -1,4 +1,3 @@ -#include "SDL3/SDL_stdinc.h" #define SDL_MAIN_USE_CALLBACKS 1 #include "utils/arena.h" #include "utils/vector.h" @@ -44,10 +43,10 @@ typedef struct State { static Arena arena; #define INDEX_AT(row, col) ((row) * GRID_WIDTH + (col)) -#define ROW_AT(index) (index / GRID_WIDTH) -#define COL_AT(index) (index % GRID_WIDTH) -#define SET_GRID_AT(grid, row, col, value) grid[INDEX_AT(row, col)] = value -#define GET_GRID_AT(grid, row, col) grid[INDEX_AT(row, col)] +#define ROW_AT(index) ((index) / GRID_WIDTH) +#define COL_AT(index) ((index) % GRID_WIDTH) +#define SET_GRID_AT(grid, row, col, value) (grid)[INDEX_AT((row), (col))] = (value) +#define GET_GRID_AT(grid, row, col) (grid)[INDEX_AT((row), (col))] void log_game_grid(Uint8 *game_grid) { char buf[GRID_WIDTH * 2]; @@ -184,7 +183,7 @@ SDL_AppResult SDL_AppIterate(void *appstate) { SDL_SetRenderDrawColor(renderer, COLOR_SNAKE); for (int i = 0; i < playerptr->length; i++) { - int b = vector_pop(playerptr->body); + int b = playerptr->body->data[i]; rect.x = startx + ROW_AT(b) * (rect.w + GRID_PADDING); rect.y = starty + COL_AT(b) * (rect.h + GRID_PADDING); SDL_RenderFillRect(renderer, &rect); diff --git a/src/utils/vector.c b/src/utils/vector.c index b98cfba..2c8b16c 100644 --- a/src/utils/vector.c +++ b/src/utils/vector.c @@ -48,6 +48,10 @@ void vector_pusha(Vector *v, int *vals, int n) { } int vector_pop(Vector *v) { + if (v->size < 1) { + printf("vector_pop: popping empty vector"); + return 0; + } int val = v->data[v->size - 1]; v->size--; return val;