aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorquou <quou@disroot.org>2023-05-02 21:37:07 +1000
committerquou <quou@disroot.org>2023-05-02 21:37:07 +1000
commit65fa1051585f5345c3270db73ab53b7a0dbbaa50 (patch)
tree30fed63168b86f8de48698174b265ffbdfae4343
parentd3745895ca0107c705b2d89b8b80e254536dad86 (diff)
Add ECS.
-rw-r--r--Makefile2
-rw-r--r--components.h21
-rw-r--r--main.c27
-rw-r--r--sprite_system.c23
-rw-r--r--systems.h8
-rw-r--r--world.c21
-rw-r--r--world.h24
7 files changed, 119 insertions, 7 deletions
diff --git a/Makefile b/Makefile
index d7224bc..7ea122c 100644
--- a/Makefile
+++ b/Makefile
@@ -91,7 +91,9 @@ sources = \
platform.c \
rect.c \
render.c \
+ sprite_system.c \
standard.c \
+ world.c \
assets = $(wildcard $(data_dir)/*)
diff --git a/components.h b/components.h
new file mode 100644
index 0000000..cdd7c46
--- /dev/null
+++ b/components.h
@@ -0,0 +1,21 @@
+#ifndef components_h
+#define components_h
+
+#include "asset.h"
+#include "rect.h"
+
+typedef struct {
+ int x, y;
+} CPosition;
+
+typedef struct {
+ Asset_ID id;
+ Rectangle rect;
+} CSprite;
+
+typedef enum {
+ ctype_sprite = 1 << 0,
+ ctype_position = 1 << 1
+} CType;
+
+#endif
diff --git a/main.c b/main.c
index c9cede7..499f313 100644
--- a/main.c
+++ b/main.c
@@ -1,20 +1,33 @@
#include "asset.h"
#include "render.h"
+#include "systems.h"
+#include "world.h"
+
+World world;
+Entity player;
void on_init(int argc, char** argv) {
+ CSprite* sprite;
+ CPosition* pos;
+
init_renderer();
load_assets();
-}
-void on_update() {
- const Bitmap* b = get_bitmap(asset_id_usr);
- Rectangle r;
+ init_world(&world);
- r = make_rect(0, 0, 189, 89);
+ player = new_entity(&world);
+ add_components(&world, player, ctype_sprite | ctype_position);
+ sprite = &world.sprites[player];
+ pos = &world.positions[player];
+ pos->x = 32;
+ pos->y = 70;
+ sprite->id = asset_id_char;
+ sprite->rect = make_rect(0, 16, 16, 16);
+}
- renderer_begin_frame();
- render_bitmap(b, 0, 0, &r);
+void on_update() {
+ sprite_system(&world);
}
void on_deinit() {
diff --git a/sprite_system.c b/sprite_system.c
new file mode 100644
index 0000000..99360fc
--- /dev/null
+++ b/sprite_system.c
@@ -0,0 +1,23 @@
+#include "asset.h"
+#include "components.h"
+#include "render.h"
+#include "world.h"
+
+void sprite_system(const World* world) {
+ int i;
+ unsigned bits;
+ const CSprite* sprite;
+ const CPosition* pos;
+ const Bitmap* b;
+
+ for (i = 0; i < world->entity_count; i++) {
+ bits = world->bitmask[i];
+ if ((bits & ctype_position) && (bits & ctype_sprite)) {
+ pos = &world->positions[i];
+ sprite = &world->sprites[i];
+
+ b = get_bitmap(sprite->id);
+ render_bitmap(b, pos->x, pos->y, &sprite->rect);
+ }
+ }
+}
diff --git a/systems.h b/systems.h
new file mode 100644
index 0000000..15abce1
--- /dev/null
+++ b/systems.h
@@ -0,0 +1,8 @@
+#ifndef systems_h
+#define systems_h
+
+#include "world.h"
+
+void sprite_system(const World* world);
+
+#endif
diff --git a/world.c b/world.c
new file mode 100644
index 0000000..b271c3d
--- /dev/null
+++ b/world.c
@@ -0,0 +1,21 @@
+#include "world.h"
+
+void init_world(World* world) {
+ world->entity_count = 0;
+}
+
+Entity new_entity(World* world) {
+ Entity e;
+
+ e = world->entity_count++;
+ world->bitmask[e] = 0;
+ return e;
+}
+
+void add_components(World* world, Entity e, CType bits) {
+ world->bitmask[e] |= bits;
+}
+
+void remove_components(World* world, Entity e, CType bits) {
+ world->bitmask[e] &= ~bits;
+}
diff --git a/world.h b/world.h
new file mode 100644
index 0000000..7f19520
--- /dev/null
+++ b/world.h
@@ -0,0 +1,24 @@
+#ifndef world_h
+#define world_h
+
+#include "components.h"
+
+#define max_entities 256
+
+typedef int Entity;
+
+typedef struct {
+ int entity_count;
+
+ unsigned char bitmask[max_entities];
+
+ CSprite sprites[max_entities];
+ CPosition positions[max_entities];
+} World;
+
+void init_world(World* world);
+Entity new_entity(World* world);
+void add_components(World* world, Entity e, CType bits);
+void remove_components(World* world, Entity e, CType bits);
+
+#endif