summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJoel Stålnacke <joel@saker.fi>2024-10-11 13:31:44 +0300
committerJoel Stålnacke <joel@saker.fi>2024-10-13 13:34:46 +0300
commit53a1cdf5bee2955995dfbf441f5354d1dcfc1e0c (patch)
treebe8c2894226a2b7e1a47f7583f2041df75f795b3 /lib
parent4bac6ae2e725a1997674fd3369bf4ea032235d8b (diff)
Add Godot client
Diffstat (limited to 'lib')
-rwxr-xr-xlib/build.sh2
-rw-r--r--lib/test.c15
-rw-r--r--lib/types.h6
-rw-r--r--lib/vec2.c46
-rw-r--r--lib/world.c104
-rw-r--r--lib/world.h31
6 files changed, 203 insertions, 1 deletions
diff --git a/lib/build.sh b/lib/build.sh
index 13aaf8e..8cf9d11 100755
--- a/lib/build.sh
+++ b/lib/build.sh
@@ -8,7 +8,7 @@ fail() {
CC=cc
CFLAGS="-std=c99 -Wall -Wextra -Wpedantic -D_POSIX_C_SOURCE=200809L"
LDFLAGS="-fPIC"
-SOURCES="test.c"
+SOURCES="test.c vec2.c world.c"
output="build"
for arg ; do
diff --git a/lib/test.c b/lib/test.c
index 78a0d02..70b32fa 100644
--- a/lib/test.c
+++ b/lib/test.c
@@ -7,3 +7,18 @@ dp_hello_world()
{
printf("Hello World\n");
}
+
+int
+dp_get_num()
+{
+ return 1;
+}
+
+vec2
+dp_get_vec2()
+{
+ vec2 vec;
+ vec.x = 1.0f;
+ vec.y = 2.5f;
+ return vec;
+}
diff --git a/lib/types.h b/lib/types.h
index afffd4f..0619c8a 100644
--- a/lib/types.h
+++ b/lib/types.h
@@ -2,3 +2,9 @@ typedef struct {
float x;
float y;
} vec2;
+
+vec2 dp_vec2_new(float, float);
+vec2 dp_vec2_add(vec2, vec2);
+vec2 dp_vec2_sub(vec2, vec2);
+vec2 dp_vec2_mul(vec2, float scalar);
+vec2 dp_vec2_dot(vec2, vec2);
diff --git a/lib/vec2.c b/lib/vec2.c
new file mode 100644
index 0000000..b417e9c
--- /dev/null
+++ b/lib/vec2.c
@@ -0,0 +1,46 @@
+#include "types.h"
+
+vec2
+dp_vec2_new(float x, float y)
+{
+ vec2 new;
+ new.x = x;
+ new.y = y;
+ return new;
+}
+
+vec2
+dp_vec2_add(vec2 a, vec2 b)
+{
+ vec2 new;
+ new.x = a.x + b.x;
+ new.y = a.y + b.y;
+ return new;
+}
+
+vec2
+dp_vec2_sub(vec2 a, vec2 b)
+{
+ vec2 new;
+ new.x = a.x - b.x;
+ new.y = a.y - b.y;
+ return new;
+}
+
+vec2
+dp_vec2_mul(vec2 vec, float scalar)
+{
+ vec2 new;
+ new.x = vec.x * scalar;
+ new.y = vec.y * scalar;
+ return new;
+}
+
+vec2
+dp_vec2_dot(vec2 a, vec2 b)
+{
+ vec2 dot;
+ dot.x = a.x * b.x;
+ dot.y = a.y * b.y;
+ return dot;
+}
diff --git a/lib/world.c b/lib/world.c
new file mode 100644
index 0000000..954aa94
--- /dev/null
+++ b/lib/world.c
@@ -0,0 +1,104 @@
+#include <stdlib.h>
+#include <string.h>
+
+#include "types.h"
+#include "world.h"
+
+struct world {
+ struct entity entities[WORLD_MAX_ENTITIES];
+};
+
+struct world *
+dp_world_create()
+{
+ struct world *w;
+
+ if (!(w = malloc(sizeof(*w))))
+ return NULL;
+ memset(w, 0, sizeof(*w));
+
+ return w;
+}
+
+void
+dp_free_world(struct world *w)
+{
+ if (w)
+ free(w);
+}
+
+int
+dp_world_create_entity(struct world *w, int kind)
+{
+ int i;
+ struct entity *e;
+
+ if (!w)
+ return -1;
+ if (kind == ENTITY_NONE)
+ return -1;
+
+ for (i = 0; i < WORLD_MAX_ENTITIES; i++) {
+ e = &w->entities[i];
+ if (e->kind != ENTITY_NONE)
+ continue;
+ e->kind = kind;
+ return i;
+ }
+
+ return -1;
+}
+
+struct entity *
+dp_world_find_entity(struct world *w, int entity_id)
+{
+ struct entity *e;
+
+ if (!w || entity_id < 0)
+ return NULL;
+ if (entity_id >= WORLD_MAX_ENTITIES)
+ return NULL;
+
+ e = &w->entities[entity_id];
+
+ if (e->kind == ENTITY_NONE)
+ return NULL;
+
+ return e;
+}
+
+int
+dp_world_remove_entity(struct world *w, int entity_id)
+{
+ struct entity *e;
+
+ if (!(e = dp_world_find_entity(w, entity_id)))
+ return -1;
+
+ memset(e, 0, sizeof(*e));
+ e->kind = ENTITY_NONE;
+ return 0;
+}
+
+int
+dp_world_tick(struct world *w, double delta)
+{
+ int i;
+ struct entity *e;
+ struct entity_ball *ball;
+
+ for (i = 0; i < WORLD_MAX_ENTITIES; i++)
+ {
+ e = &w->entities[i];
+ switch (e->kind) {
+ case ENTITY_BALL:
+ ball = &e->e.ball;
+ /*ball->pos = dp_vec2_new(500.0, 500.0);*/
+ ball->pos = dp_vec2_add(ball->pos, dp_vec2_mul(ball->vel, delta));
+ break;
+ default:
+ break;
+ }
+ }
+ return 0;
+}
diff --git a/lib/world.h b/lib/world.h
new file mode 100644
index 0000000..40c3fef
--- /dev/null
+++ b/lib/world.h
@@ -0,0 +1,31 @@
+#define WORLD_MAX_ENTITIES 5000
+
+enum {
+ ENTITY_NONE,
+ ENTITY_BALL
+};
+
+struct entity_ball {
+ vec2 pos;
+ vec2 vel;
+};
+
+union entity_union {
+ struct entity_ball ball;
+};
+
+struct entity {
+ int kind;
+ union entity_union e;
+};
+
+struct world;
+
+struct world *dp_world_create();
+void dp_world_free(struct world *);
+
+int dp_world_create_entity(struct world *, int kind);
+struct entity *dp_world_find_entity(struct world *, int entity_id);
+int dp_world_remove_entity(struct world *, int entity_id);
+
+int dp_world_tick(struct world *, double delta);