This repository has been archived on 2024-02-21. You can view files and clone it, but cannot push or open issues or pull requests.
MainTree/core/src-c/entity_lookup.c

60 lines
1.8 KiB
C

#include <entity.h>
#include <state.h>
#include <stdlib.h>
MtEntity * mt_create_entity(void *state_voidptr, u32_t identifier)
{
MtState *state = state_voidptr;
MtEntityListL1 *level1 = state->entity_registry.lookup.sublists[identifier >> 24];
if(level1 == NULL)
{
state->entity_registry.lookup.sublists[identifier >> 24] = calloc(sizeof(MtEntityListL1), 1);
level1 = state->entity_registry.lookup.sublists[identifier >> 24];
}
MtEntityListL2 *level2 = level1->sublists[(identifier >> 16) & 0xff];
if(level2 == NULL)
{
level1->sublists[(identifier >> 16) & 0xff] = calloc(sizeof(MtEntityListL2), 1);
level2 = level1->sublists[(identifier >> 16) & 0xff];
}
MtEntityListL3 *level3 = level2->sublists[(identifier >> 8) & 0xff];
if(level3 == NULL)
{
level2->sublists[(identifier >> 8) & 0xff] = calloc(sizeof(MtEntityListL3), 1);
level3 = level2->sublists[(identifier >> 8) & 0xff];
}
MtEntity *entity = level3->entities[identifier & 0xff];
if(entity == NULL)
{
entity = mt_allocate_entity_pool_slot(&state->entity_registry.pool);
entity->parent_state = state;
level3->entities[identifier & 0xff] = entity;
}
return entity;
}
MtEntity * mt_get_entity(void *state_voidptr, u32_t identifier)
{
MtState *state = state_voidptr;
MtEntityListL1 *level1 = state->entity_registry.lookup.sublists[identifier >> 24];
if(level1 == NULL)
return NULL;
MtEntityListL2 *level2 = level1->sublists[(identifier >> 16) & 0xff];
if(level2 == NULL)
return NULL;
MtEntityListL3 *level3 = level2->sublists[(identifier >> 8) & 0xff];
if(level3 == NULL)
return NULL;
MtEntity *entity = level3->entities[identifier & 0xff];
if(entity == NULL)
return NULL;
return entity;
}