diff options
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | components.h | 21 | ||||
-rw-r--r-- | main.c | 27 | ||||
-rw-r--r-- | sprite_system.c | 23 | ||||
-rw-r--r-- | systems.h | 8 | ||||
-rw-r--r-- | world.c | 21 | ||||
-rw-r--r-- | world.h | 24 |
7 files changed, 119 insertions, 7 deletions
@@ -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 @@ -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 @@ -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; +} @@ -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 |