summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJoel Stålnacke <joel@saker.fi>2026-03-20 21:38:46 +0200
committerJoel Stålnacke <joel@saker.fi>2026-03-20 21:38:46 +0200
commit3870982f640260822bf605e693782ce9f5d79cf6 (patch)
tree29cba57f08ae2d0ea32968cba49b1c4e6f69987f /lib
parent745406dc65aecc438a53f96badc387a32b7a338f (diff)
Get a player drawing on screenHEADmibomaster
Diffstat (limited to 'lib')
-rw-r--r--lib/physics.c38
-rw-r--r--lib/physics.h4
-rw-r--r--lib/snapshot.c69
-rw-r--r--lib/snapshot.h24
4 files changed, 102 insertions, 33 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]);
}
diff --git a/lib/physics.h b/lib/physics.h
index 7aa2b31..e1c53f3 100644
--- a/lib/physics.h
+++ b/lib/physics.h
@@ -4,11 +4,11 @@ struct snapshot;
/* A game world */
struct context {
- size_t snapshots_len;
+ int32_t snapshots_len;
struct snapshot *snapshots;
};
-struct context *dp_physics_context_create(size_t snapshots);
+struct context *dp_physics_context_create(int32_t snapshots, int32_t players, int32_t balls);
void dp_physics_context_free(struct context *);
struct snapshot *dp_physics_context_get_snapshot(struct context *);
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 <stdlib.h>
#include <string.h>
#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);
diff --git a/lib/snapshot.h b/lib/snapshot.h
index 022d989..028eac2 100644
--- a/lib/snapshot.h
+++ b/lib/snapshot.h
@@ -11,7 +11,6 @@ enum {
struct player {
uint8_t state;
uint8_t input;
- char pad[2];
vec2 pos;
vec2 vel;
};
@@ -25,15 +24,20 @@ struct ball {
struct snapshot {
char active;
uint32_t tick;
- struct player players[WORLD_MAX_PLAYERS];
- struct ball balls[WORLD_MAX_BULLETS];
+ int32_t players_len;
+ struct player *players;
+ int32_t balls_len;
+ struct ball *balls;
};
-int dp_snapshot_put_player(struct snapshot *, uint32_t id, const struct player *);
-struct player *dp_snapshot_get_player(struct snapshot *, uint32_t id);
-int dp_snapshot_remove_player(struct snapshot *, uint32_t id);
-int dp_snapshot_set_player_input(struct snapshot *, uint32_t id, uint8_t input);
+int dp_snapshot_init(struct snapshot *, int32_t players_len, int32_t balls_len);
+void dp_snapshot_deinit(struct snapshot *);
-int dp_snapshot_put_ball(struct snapshot *, uint32_t id, const struct ball *);
-struct ball *dp_snapshot_get_ball(struct snapshot *, uint32_t id);
-int dp_snapshot_remove_ball(struct snapshot *, uint32_t id);
+int dp_snapshot_put_player(struct snapshot *, int32_t id, const struct player *);
+struct player *dp_snapshot_get_player(struct snapshot *, int32_t id);
+int dp_snapshot_remove_player(struct snapshot *, int32_t id);
+int dp_snapshot_set_player_input(struct snapshot *, int32_t id, uint8_t input);
+
+int dp_snapshot_put_ball(struct snapshot *, int32_t id, const struct ball *);
+struct ball *dp_snapshot_get_ball(struct snapshot *, int32_t id);
+int dp_snapshot_remove_ball(struct snapshot *, int32_t id);