mirror of
https://github.com/fredrikr79/SnakeDL3.git
synced 2025-12-24 19:20:21 +01:00
fix snake using vector and arena
This commit is contained in:
26
src/main.c
26
src/main.c
@@ -31,12 +31,12 @@ typedef enum DIRECTION { LEFT, RIGHT, UP, DOWN } DIRECTION;
|
||||
typedef struct Snake {
|
||||
DIRECTION direction;
|
||||
int length;
|
||||
Vector *body;
|
||||
Vector body;
|
||||
} Snake;
|
||||
|
||||
typedef struct State {
|
||||
Snake *player;
|
||||
Vector *fruits;
|
||||
Vector fruits;
|
||||
Uint64 last_frame;
|
||||
} State;
|
||||
|
||||
@@ -86,11 +86,9 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) {
|
||||
}
|
||||
state->player->direction = UP;
|
||||
state->player->length = 3;
|
||||
state->player->body = (Vector *)arena_alloc(&arena, 1, sizeof(Vector), _Alignof(Vector));
|
||||
if (!state->player->body) {
|
||||
SDL_Log("Failed to allocate player body");
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
vector_new(&state->player->body);
|
||||
vector_pusha(&state->player->body, (int[]){1, 2, 3}, 3);
|
||||
vector_print(state->player->body);
|
||||
|
||||
state->last_frame = SDL_GetTicks();
|
||||
*appstate = state;
|
||||
@@ -131,21 +129,21 @@ void game_step(State *stateptr) {
|
||||
// SDL_Log("snek: %d %d %d %d", player->direction, player->length, player->head_index,
|
||||
// player->tail_index);
|
||||
Snake *playerptr = stateptr->player;
|
||||
int head = vector_pop(stateptr->player->body);
|
||||
vector_push(stateptr->player->body, head);
|
||||
int head = vector_pop(&stateptr->player->body);
|
||||
vector_push(&stateptr->player->body, head);
|
||||
|
||||
switch (playerptr->direction) {
|
||||
case RIGHT:
|
||||
vector_push(playerptr->body, head + 1);
|
||||
vector_push(&playerptr->body, head + 1);
|
||||
break;
|
||||
case UP:
|
||||
vector_push(playerptr->body, head - GRID_WIDTH);
|
||||
vector_push(&playerptr->body, head - GRID_WIDTH);
|
||||
break;
|
||||
case LEFT:
|
||||
vector_push(playerptr->body, head - 1);
|
||||
vector_push(&playerptr->body, head - 1);
|
||||
break;
|
||||
case DOWN:
|
||||
vector_push(playerptr->body, head + GRID_WIDTH);
|
||||
vector_push(&playerptr->body, head + GRID_WIDTH);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -183,7 +181,7 @@ SDL_AppResult SDL_AppIterate(void *appstate) {
|
||||
|
||||
SDL_SetRenderDrawColor(renderer, COLOR_SNAKE);
|
||||
for (int i = 0; i < playerptr->length; i++) {
|
||||
int b = playerptr->body->data[i];
|
||||
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);
|
||||
|
||||
@@ -43,7 +43,6 @@ void arena_free(Arena *a) {
|
||||
free(a->buffer);
|
||||
a->size = 0;
|
||||
a->offset = 0;
|
||||
free(a);
|
||||
}
|
||||
|
||||
// int main(void) {
|
||||
|
||||
@@ -2,25 +2,14 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void vector_init(Vector **v, int capacity) {
|
||||
*v = malloc(sizeof(Vector));
|
||||
if (!*v) {
|
||||
printf("vector_init: failed to allocate vector pointer\n");
|
||||
return;
|
||||
}
|
||||
(*v)->data = malloc(capacity * sizeof(int));
|
||||
if (!(*v)->data) {
|
||||
void vector_init(Vector *v, int capacity) {
|
||||
v->data = malloc(capacity * sizeof(int));
|
||||
if (!v->data) {
|
||||
printf("vector_init: failed to allocate data\n");
|
||||
return;
|
||||
}
|
||||
(*v)->size = 0;
|
||||
(*v)->_capacity = capacity;
|
||||
}
|
||||
|
||||
Vector *vector_new(void) {
|
||||
Vector *v;
|
||||
vector_init(&v, VEC_MIN);
|
||||
return v;
|
||||
v->size = 0;
|
||||
v->_capacity = capacity;
|
||||
}
|
||||
|
||||
void vector_grow(Vector *v) {
|
||||
@@ -61,7 +50,6 @@ void vector_free(Vector *v) {
|
||||
free(v->data);
|
||||
v->size = 0;
|
||||
v->_capacity = 0;
|
||||
free(v);
|
||||
}
|
||||
|
||||
void vector_print(Vector v) {
|
||||
|
||||
@@ -3,20 +3,22 @@
|
||||
|
||||
#define VEC_MIN 8
|
||||
|
||||
#define vector_new(v) vector_init(v, VEC_MIN)
|
||||
|
||||
typedef struct {
|
||||
int *data;
|
||||
int size;
|
||||
int _capacity;
|
||||
} Vector;
|
||||
|
||||
void vector_init(Vector **v, int capacity);
|
||||
|
||||
Vector *vector_new(void);
|
||||
void vector_init(Vector *v, int capacity);
|
||||
|
||||
void vector_grow(Vector *v);
|
||||
|
||||
void vector_push(Vector *v, int val);
|
||||
|
||||
void vector_pusha(Vector *v, int *vals, int n);
|
||||
|
||||
int vector_pop(Vector *v);
|
||||
|
||||
void vector_free(Vector *v);
|
||||
|
||||
Reference in New Issue
Block a user