dummy archetypes

This commit is contained in:
NukeBird 2025-03-20 20:09:25 +03:00
parent 93a7f31757
commit 3105b8806c
2 changed files with 63 additions and 6 deletions

View file

@ -1,3 +1,4 @@
#include <stdio.h>
#include <stdlib.h>
#define CLOVE_IMPLEMENTATION
#include <clove-unit.h>
@ -33,7 +34,12 @@ CLOVE_TEST(alive_entities)
entity_id e = make_entity(w);
CLOVE_SIZET_NE(e, RESERVED_ENTITY_ID);
CLOVE_IS_TRUE(world_has_entity(w, e));
CLOVE_IS_TRUE(is_alive(w, e));
archetype_signature sig = arhetype_of(w, e);
archetype_signature empty_sig = {0};
CLOVE_IS_TRUE(is_same(sig, empty_sig));
destroy_world(&w);
}
@ -44,7 +50,7 @@ CLOVE_TEST(dead_entities)
entity_id e = make_entity(w);
destroy_entity(w, e);
CLOVE_IS_FALSE(world_has_entity(w, e));
CLOVE_IS_FALSE(is_alive(w, e));
destroy_world(&w);
}

59
zecsy.h
View file

@ -1,17 +1,36 @@
#ifndef __ZECSY_H
#define __ZECSY_H
#ifndef ZECSY_MAX_COMPONENTS
#define ZECSY_MAX_COMPONENTS 128
#endif // !ZECSY_MAX_COMPONENTS
#define STB_DS_IMPLEMENTATION
#include "stb_ds.h"
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
typedef struct
{
uint32_t bitset[(ZECSY_MAX_COMPONENTS + 31) / 32];
} archetype_signature;
#define RESERVED_ENTITY_ID 0
typedef size_t entity_id;
typedef size_t component_id;
typedef struct
{
const char* name;
component_id id;
size_t size;
} component_info;
typedef struct
{
struct{entity_id key; int value;}* entity_map;
struct{entity_id key; archetype_signature value;}* entity_map;
entity_id entity_id_counter;
entity_id alive_entities;
} world;
@ -22,7 +41,10 @@ void destroy_world(world** w);
entity_id make_entity(world* w);
void destroy_entity(world* w, entity_id e);
int world_has_entity(world* w, entity_id e);
int is_alive(world* w, entity_id e);
archetype_signature arhetype_of(world* w, entity_id e);
int is_same(archetype_signature a, archetype_signature b);
#endif // !__ZECSY_H
@ -58,12 +80,14 @@ void destroy_world(world** w)
entity_id make_entity(world* w)
{
entity_id e = RESERVED_ENTITY_ID;
if(w)
{
e = w->entity_id_counter++;
hmput(w->entity_map, e, 1);
hmput(w->entity_map, e, (archetype_signature){0});
w->alive_entities++;
}
@ -76,7 +100,7 @@ void destroy_entity(world* w, entity_id e)
hmdel(w->entity_map, e);
}
int world_has_entity(world* w, entity_id e)
int is_alive(world* w, entity_id e)
{
if(w)
{
@ -86,4 +110,31 @@ int world_has_entity(world* w, entity_id e)
return 0;
}
archetype_signature arhetype_of(world* w, entity_id e)
{
if(w)
{
int index = hmgeti(w->entity_map, e);
if(index >= 0)
{
return w->entity_map[index].value;
}
}
return (archetype_signature){0};
}
int is_same(archetype_signature a, archetype_signature b)
{
for(int i = 0; i < sizeof(a.bitset) / sizeof(a.bitset[0]); ++i)
{
if(a.bitset[i] != b.bitset[i])
{
return 0;
}
}
return 1;
}
#endif // ZECSY_IMPLEMENTATION