diff options
Diffstat (limited to 'lib/physics.c')
| -rw-r--r-- | lib/physics.c | 38 |
1 files changed, 30 insertions, 8 deletions
diff --git a/lib/physics.c b/lib/physics.c index adcf433..0039bd7 100644 --- a/lib/physics.c +++ b/lib/physics.c @@ -10,8 +10,9 @@ #include "physics.h" struct context * -dp_physics_context_create(size_t snapshots) +dp_physics_context_create(int32_t snapshots, int32_t players, int32_t balls) { + int32_t i, j; struct context *ctx = NULL; if (!(ctx = malloc(sizeof(*ctx)))) @@ -26,22 +27,42 @@ dp_physics_context_create(size_t snapshots) } memset(ctx->snapshots, 0, sizeof(*ctx->snapshots) * snapshots); + for (i = 0; i < snapshots; i++) + { + if (dp_snapshot_init(&ctx->snapshots[i], players, balls) != 0) + { + for (j = 0; j < i; j++) + { + dp_snapshot_deinit(&ctx->snapshots[j]); + } + free(ctx); + } + } + return ctx; } void dp_physics_context_free(struct context *ctx) { + int32_t i; if (!ctx) return; if (ctx->snapshots) + { + for (i = 0; i < ctx->snapshots_len; i++) + { + dp_snapshot_deinit(&ctx->snapshots[i]); + } free(ctx->snapshots); + } free(ctx); } -struct snapshot *dp_physics_context_get_snapshot(struct context *ctx) +struct snapshot * +dp_physics_context_get_snapshot(struct context *ctx) { - size_t i; + int32_t i; struct snapshot *s; for (i = 0; i < ctx->snapshots_len; i++) @@ -56,9 +77,10 @@ struct snapshot *dp_physics_context_get_snapshot(struct context *ctx) return NULL; } -void dp_physics_context_return_snapshot(struct context *ctx, struct snapshot *s) +void +dp_physics_context_return_snapshot(struct context *ctx, struct snapshot *s) { - size_t i; + int32_t i; assert(s->active); @@ -104,17 +126,17 @@ dp_physics_tick_ball(const struct ball *cur, double delta, struct ball *next) int dp_physics_tick(const struct snapshot *cur, double delta, struct snapshot *next) { - size_t i; + int32_t i; assert(cur->active); assert(next->active); - for (i = 0; i < LENGTH(cur->players); i++) + for (i = 0; i < cur->players_len; i++) { dp_physics_tick_player(&cur->players[i], delta, &next->players[i]); } - for (i = 0; i < LENGTH(cur->balls); i++) + for (i = 0; i < cur->balls_len; i++) { dp_physics_tick_ball(&cur->balls[i], delta, &next->balls[i]); } |
