89 lines
1.4 KiB
C
89 lines
1.4 KiB
C
#ifndef __ZECSY_H
|
|
#define __ZECSY_H
|
|
|
|
#define STB_DS_IMPLEMENTATION
|
|
#include "stb_ds.h"
|
|
#include <stdlib.h>
|
|
#include <stddef.h>
|
|
|
|
#define RESERVED_ENTITY_ID 0
|
|
typedef size_t entity_id;
|
|
|
|
typedef struct
|
|
{
|
|
struct{entity_id key; int value;}* entity_map;
|
|
entity_id entity_id_counter;
|
|
entity_id alive_entities;
|
|
} world;
|
|
|
|
void make_world(world** w);
|
|
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);
|
|
|
|
#endif // !__ZECSY_H
|
|
|
|
#define ZECSY_IMPLEMENTATION //TODO: REMOVE
|
|
#ifdef ZECSY_IMPLEMENTATION
|
|
|
|
void make_world(world** w)
|
|
{
|
|
if(!w)
|
|
return;
|
|
|
|
(*w) = malloc(sizeof(world));
|
|
|
|
(**w) = (world)
|
|
{
|
|
.entity_map = NULL,
|
|
.entity_id_counter = RESERVED_ENTITY_ID + 1,
|
|
.alive_entities = 0,
|
|
};
|
|
}
|
|
|
|
void destroy_world(world** w)
|
|
{
|
|
if(!w || !(*w))
|
|
return;
|
|
|
|
if((*w)->entity_map)
|
|
hmfree((*w)->entity_map);
|
|
|
|
free((*w));
|
|
(*w) = NULL;
|
|
}
|
|
|
|
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);
|
|
w->alive_entities++;
|
|
}
|
|
|
|
return e;
|
|
}
|
|
|
|
void destroy_entity(world* w, entity_id e)
|
|
{
|
|
if(w)
|
|
hmdel(w->entity_map, e);
|
|
}
|
|
|
|
int world_has_entity(world* w, entity_id e)
|
|
{
|
|
if(w)
|
|
{
|
|
return hmgeti(w->entity_map, e) != -1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
#endif // ZECSY_IMPLEMENTATION
|