Add generic dynamic array type
This commit is contained in:
parent
89e548eca8
commit
7769f5fd58
6 changed files with 94 additions and 79 deletions
|
|
@ -2,49 +2,29 @@
|
|||
|
||||
struct kubo_context *kubo_context_init() {
|
||||
struct kubo_context *context = malloc(sizeof(struct kubo_context));
|
||||
if (!context) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
context->num_walls_alloc = KUBO_CONTEXT_NUM_WALLS_ALLOC;
|
||||
context->walls =
|
||||
malloc(sizeof(struct kubo_wall) * context->num_walls_alloc);
|
||||
kubo_wall_arr_init(&context->walls);
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
void kubo_context_cleanup(struct kubo_context *context) {
|
||||
free(context->walls);
|
||||
kubo_wall_arr_free(&context->walls);
|
||||
free(context);
|
||||
}
|
||||
|
||||
bool kubo_context_add_wall(struct kubo_context *context,
|
||||
struct kubo_wall *wall) {
|
||||
|
||||
struct kubo_wall **new_walls;
|
||||
|
||||
if (context->num_walls == context->num_walls_alloc - 1) {
|
||||
|
||||
context->num_walls_alloc *= 2;
|
||||
|
||||
new_walls = realloc(context->walls, sizeof(struct kubo_wall *) *
|
||||
context->num_walls_alloc);
|
||||
|
||||
if (!new_walls)
|
||||
return false;
|
||||
|
||||
context->walls = new_walls;
|
||||
}
|
||||
|
||||
context->walls[context->num_walls++] = wall;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
struct kubo_wall *kubo_context_get_pending_wall(struct kubo_context *context) {
|
||||
|
||||
if (context->num_walls > 0) {
|
||||
switch (context->walls[context->num_walls - 1]->state) {
|
||||
if (context->walls.count > 0) {
|
||||
struct kubo_wall *last_wall =
|
||||
kubo_wall_arr_get(&context->walls, context->walls.count - 1);
|
||||
switch (last_wall->state) {
|
||||
case KUBO_WALL_INIT:
|
||||
case KUBO_WALL_DRAWING:
|
||||
return context->walls[context->num_walls - 1];
|
||||
return last_wall;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
|
@ -52,7 +32,7 @@ struct kubo_wall *kubo_context_get_pending_wall(struct kubo_context *context) {
|
|||
}
|
||||
|
||||
struct kubo_wall *wall = kubo_wall_init();
|
||||
kubo_context_add_wall(context, wall);
|
||||
kubo_wall_arr_add(&context->walls, wall);
|
||||
|
||||
return wall;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue