From 3870982f640260822bf605e693782ce9f5d79cf6 Mon Sep 17 00:00:00 2001 From: Joel Stålnacke Date: Fri, 20 Mar 2026 21:38:46 +0200 Subject: Get a player drawing on screen --- lib/snapshot.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 56 insertions(+), 13 deletions(-) (limited to 'lib/snapshot.c') diff --git a/lib/snapshot.c b/lib/snapshot.c index 88c17a1..d7dde50 100644 --- a/lib/snapshot.c +++ b/lib/snapshot.c @@ -1,14 +1,51 @@ +#include #include #include "common.h" #include "snapshot.h" -int dp_snapshot_put_player(struct snapshot *s, uint32_t id, const struct player *p) +int +dp_snapshot_init(struct snapshot *s, int32_t players_len, int32_t balls_len) +{ + if (!s) + return -1; + + if (!(s->players = calloc(players_len, sizeof(*s->players)))) + return -1; + if (!(s->balls = calloc(balls_len, sizeof(*s->balls)))) + { + free(s->players); + return -1; + } + s->players_len = players_len; + s->balls_len = balls_len; + return 0; +} + +void +dp_snapshot_deinit(struct snapshot *s) +{ + if (!s) + return; + if (s->players) + { + free(s->players); + s->players = NULL; + } + if (s->balls) + { + free(s->balls); + s->balls = NULL; + } +} + +int +dp_snapshot_put_player(struct snapshot *s, int32_t id, const struct player *p) { if (!s || !p) return -1; - if (id >= LENGTH(s->players)) + if (id >= s->players_len) return -1; s->players[id] = *p; @@ -16,13 +53,14 @@ int dp_snapshot_put_player(struct snapshot *s, uint32_t id, const struct player return 0; } -struct player *dp_snapshot_get_player(struct snapshot *s, uint32_t id) +struct player * +dp_snapshot_get_player(struct snapshot *s, int32_t id) { struct player *p; if (!s) return NULL; - if (id >= LENGTH(s->players)) + if (id >= s->players_len) return NULL; p = &s->players[id]; @@ -31,13 +69,14 @@ struct player *dp_snapshot_get_player(struct snapshot *s, uint32_t id) return p; } -int dp_snapshot_remove_player(struct snapshot *s, uint32_t id) +int +dp_snapshot_remove_player(struct snapshot *s, int32_t id) { struct player *p; if (!s) return -1; - if (id >= LENGTH(s->players)) + if (id >= s->players_len) return -1; p = dp_snapshot_get_player(s, id); @@ -46,7 +85,8 @@ int dp_snapshot_remove_player(struct snapshot *s, uint32_t id) return 0; } -int dp_snapshot_set_player_input(struct snapshot *s, uint32_t id, uint8_t input) +int +dp_snapshot_set_player_input(struct snapshot *s, int32_t id, uint8_t input) { struct player *p; @@ -56,11 +96,12 @@ int dp_snapshot_set_player_input(struct snapshot *s, uint32_t id, uint8_t input) return 0; } -int dp_snapshot_put_ball(struct snapshot *s, uint32_t id, const struct ball *b) +int +dp_snapshot_put_ball(struct snapshot *s, int32_t id, const struct ball *b) { if (!s || !b) return -1; - if (id >= LENGTH(s->balls)) + if (id >= s->balls_len) return -1; s->balls[id] = *b; @@ -68,13 +109,14 @@ int dp_snapshot_put_ball(struct snapshot *s, uint32_t id, const struct ball *b) return 0; } -struct ball *dp_snapshot_get_ball(struct snapshot *s, uint32_t id) +struct ball * +dp_snapshot_get_ball(struct snapshot *s, int32_t id) { struct ball *b; if (!s) return NULL; - if (id >= LENGTH(s->balls)) + if (id >= s->balls_len) return NULL; b = &s->balls[id]; @@ -83,13 +125,14 @@ struct ball *dp_snapshot_get_ball(struct snapshot *s, uint32_t id) return b; } -int dp_snapshot_remove_ball(struct snapshot *s, uint32_t id) +int +dp_snapshot_remove_ball(struct snapshot *s, int32_t id) { struct ball *b; if (!s) return -1; - if (id >= LENGTH(s->balls)) + if (id >= s->balls_len) return -1; b = dp_snapshot_get_ball(s, id); -- cgit v1.2.3