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