Add generic dynamic array type
This commit is contained in:
parent
89e548eca8
commit
7769f5fd58
6 changed files with 94 additions and 79 deletions
33
kubo_wall.c
33
kubo_wall.c
|
|
@ -6,37 +6,22 @@ struct kubo_wall *kubo_wall_init() {
|
|||
if (!wall)
|
||||
return NULL;
|
||||
|
||||
wall->num_vertices_alloc = KUBO_WALL_INIT_VERTICES;
|
||||
wall->vertices = malloc(sizeof(Vector2) * wall->num_vertices_alloc);
|
||||
wall->num_vertices = 0;
|
||||
wall->state = KUBO_WALL_INIT;
|
||||
|
||||
kubo_vector2_arr_init(&wall->vertices);
|
||||
|
||||
return wall;
|
||||
}
|
||||
|
||||
void kubo_wall_cleanup(struct kubo_wall *wall) {
|
||||
free(wall->vertices);
|
||||
kubo_vector2_arr_free(&wall->vertices);
|
||||
free(wall);
|
||||
}
|
||||
|
||||
bool kubo_wall_append(struct kubo_wall *wall, Vector2 vertex) {
|
||||
|
||||
Vector2 *new_vertices;
|
||||
|
||||
if (wall->num_vertices == wall->num_vertices_alloc - 1) {
|
||||
|
||||
wall->num_vertices_alloc *= 2;
|
||||
|
||||
new_vertices =
|
||||
realloc(wall->vertices, sizeof(Vector2) * wall->num_vertices_alloc);
|
||||
|
||||
if (!new_vertices)
|
||||
return false;
|
||||
|
||||
wall->vertices = new_vertices;
|
||||
}
|
||||
|
||||
wall->vertices[wall->num_vertices++] = vertex;
|
||||
|
||||
return true;
|
||||
bool kubo_wall_add_vertex(struct kubo_wall *wall, Vector2 vec) {
|
||||
return kubo_vector2_arr_add(&wall->vertices, vec);
|
||||
}
|
||||
|
||||
Vector2 kubo_wall_get_vertex(struct kubo_wall *wall, size_t index) {
|
||||
return kubo_vector2_arr_get(&wall->vertices, index);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue