Compare commits

..

2 Commits

16 changed files with 461 additions and 170 deletions

View File

@ -4,14 +4,14 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
void tc_add_chunk_to_loader(tc_chunkloader_s *chunkloader, tc_chunk_s *chunk) void tc_add_chunk_to_loader(tc_chunkloader_s *chunkloader, tc_entity_s *entity)
{ {
if(chunkloader->num_chunks >= chunkloader->chunks_capacity) if(chunkloader->num_chunks >= chunkloader->chunks_capacity)
{ {
chunkloader->chunks_capacity *= 2; chunkloader->chunks_capacity *= 2;
chunkloader->chunks = realloc(chunkloader->chunks, sizeof(tc_chunk_s) * chunkloader->chunks_capacity); chunkloader->chunks = realloc(chunkloader->chunks, sizeof(tc_entity_s *) * chunkloader->chunks_capacity);
} }
chunkloader->chunks[chunkloader->num_chunks] = chunk; chunkloader->chunks[chunkloader->num_chunks] = entity;
++chunkloader->num_chunks; ++chunkloader->num_chunks;
} }
@ -37,16 +37,19 @@ tc_chunkloader_s * tc_get_corresponding_loader(tc_world_s *world, tc_vec3i_s coo
return NULL; return NULL;
} }
tc_chunk_s * tc_get_chunk_of_loader(tc_chunkloader_s *loader, tc_vec3i_s coord) tc_entity_s * tc_get_chunk_of_loader(tc_chunkloader_s *loader, tc_vec3i_s coord)
{ {
for(uint32_t chunk_index = 0; chunk_index < loader->num_chunks; ++chunk_index) for(uint32_t chunk_index = 0; chunk_index < loader->num_chunks; ++chunk_index)
if(tc_vec3i_equ(loader->chunks[chunk_index]->position, coord)) {
tc_chunk_s *chunk = loader->chunks[chunk_index]->specific;
if(tc_vec3i_equ(chunk->position, coord))
return loader->chunks[chunk_index]; return loader->chunks[chunk_index];
}
return NULL; return NULL;
} }
tc_chunk_s * tc_get_loaded_chunk(tc_world_s *world, tc_vec3i_s coord) tc_entity_s * tc_get_loaded_chunk(tc_world_s *world, tc_vec3i_s coord)
{ {
tc_chunkloader_s *loader = tc_get_corresponding_loader(world, coord); tc_chunkloader_s *loader = tc_get_corresponding_loader(world, coord);
if(loader == NULL) return NULL; if(loader == NULL) return NULL;
@ -54,27 +57,29 @@ tc_chunk_s * tc_get_loaded_chunk(tc_world_s *world, tc_vec3i_s coord)
return tc_get_chunk_of_loader(loader, coord); return tc_get_chunk_of_loader(loader, coord);
} }
tc_chunk_s * tc_load_chunk_of_loader(tc_chunkloader_s *loader, tc_vec3i_s coord) tc_entity_s * tc_load_chunk_of_loader(tc_chunkloader_s *loader, tc_vec3i_s coord)
{ {
tc_chunk_s *chunk = tc_get_chunk_of_loader(loader, coord); tc_entity_s *entity = tc_get_chunk_of_loader(loader, coord);
if(chunk == NULL) if(entity == NULL)
{ {
tc_entity_s *entity = tc_create_entity("chunk"); // TODO: FURTHER RESTRUCTUING FOR ENTITIES tc_entity_s *entity = tc_create_entity("chunk"); // TODO: FURTHER RESTRUCTUING FOR ENTITIES
tc_chunk_s *chunk = entity->specific;
chunk = entity->specific; chunk = entity->specific;
chunk->position = coord; chunk->position = coord;
loader->world->worldgen->fn_generate_chunk(loader->world->worldgen, chunk); loader->world->worldgen->fn_generate_chunk(loader->world->worldgen, chunk);
tc_upload_chunk(chunk); tc_upload_chunk(chunk);
tc_add_chunk_to_loader(loader, chunk); tc_add_chunk_to_loader(loader, entity);
} }
return chunk; return entity;
} }
void tc_reload_chunk_of_loader(tc_chunkloader_s *loader, tc_vec3i_s coord) void tc_reload_chunk_of_loader(tc_chunkloader_s *loader, tc_vec3i_s coord)
{ {
tc_chunk_s *chunk = tc_get_chunk_of_loader(loader, coord); tc_entity_s *entity = tc_get_chunk_of_loader(loader, coord);
tc_chunk_s *chunk = entity->specific;
if(chunk == NULL) return; if(chunk == NULL) return;
tc_meshize_chunk(chunk); tc_meshize_chunk(chunk);
@ -91,7 +96,7 @@ void tc_reload_chunk(tc_world_s *world, tc_vec3i_s coord)
void tc_remove_loaders_chunk(tc_chunkloader_s *loader, uint32_t index) void tc_remove_loaders_chunk(tc_chunkloader_s *loader, uint32_t index)
{ {
tc_free_chunk(loader->chunks[index]); tc_delete_entity(loader->chunks[index]);
if(loader->num_chunks != 0) if(loader->num_chunks != 0)
{ {
@ -109,7 +114,9 @@ void tc_remove_unnecessary_chunks_of_loader(tc_chunkloader_s *loader)
{ {
for(uint32_t chunk_index = 0; chunk_index < loader->num_chunks; ++chunk_index) for(uint32_t chunk_index = 0; chunk_index < loader->num_chunks; ++chunk_index)
{ {
tc_chunk_s *chunk = loader->chunks[chunk_index]; tc_entity_s *entity = loader->chunks[chunk_index];
tc_chunk_s *chunk = entity->specific;
if(!tc_coord_is_within_loaders_range(loader, chunk->position)) if(!tc_coord_is_within_loaders_range(loader, chunk->position))
{ {
tc_remove_loaders_chunk(loader, chunk_index); tc_remove_loaders_chunk(loader, chunk_index);

View File

@ -10,12 +10,10 @@ tc_entity_registry_s tc_init_entity_registry()
registry.types_capacity = 64; registry.types_capacity = 64;
registry.num_types = 0; registry.num_types = 0;
registry.types = calloc(sizeof(tc_entity_type_s), registry.types_capacity); registry.types = calloc(sizeof(tc_entity_type_s), registry.types_capacity);
registry.entities_capacity = 1024;
registry.num_entities = 0;
registry.entities = calloc(sizeof(tc_entity_s *), registry.entities_capacity);
registry.intervals_capacity = 256; registry.intervals_capacity = 256;
registry.num_intervals = 0; registry.num_intervals = 0;
registry.intervals = calloc(sizeof(tc_interval_s *), registry.intervals_capacity); registry.intervals = calloc(sizeof(tc_interval_s *), registry.intervals_capacity);
registry.pool = tc_create_entity_pool(512);
return registry; return registry;
} }
@ -34,6 +32,10 @@ void tc_register_entity_type(tc_entity_type_s type)
); );
} }
if(type.instances_capacity == 0) type.instances_capacity = 64;
type.instances = calloc(sizeof(tc_entity_s *), type.instances_capacity);
type.num_instances = 0;
tc_game_state_g.entity_registry.types[tc_game_state_g.entity_registry.num_types] = type; tc_game_state_g.entity_registry.types[tc_game_state_g.entity_registry.num_types] = type;
++tc_game_state_g.entity_registry.num_types; ++tc_game_state_g.entity_registry.num_types;
@ -41,10 +43,10 @@ void tc_register_entity_type(tc_entity_type_s type)
tc_entity_type_s * tc_get_entity_type_with_name(char *internal_name) tc_entity_type_s * tc_get_entity_type_with_name(char *internal_name)
{ {
uint32_t num_types = tc_game_state_g.entity_registry.num_types; u32_t num_types = tc_game_state_g.entity_registry.num_types;
tc_entity_type_s *types = tc_game_state_g.entity_registry.types; tc_entity_type_s *types = tc_game_state_g.entity_registry.types;
uint32_t index = 0; u32_t index = 0;
while(index < num_types) while(index < num_types)
{ {
if(!strcmp(types[index].internal_name, internal_name)) return &types[index]; if(!strcmp(types[index].internal_name, internal_name)) return &types[index];
@ -53,14 +55,43 @@ tc_entity_type_s * tc_get_entity_type_with_name(char *internal_name)
return NULL; return NULL;
} }
i32_t tc_find_entity_instance_index_in_type(tc_entity_type_s *type, tc_entity_s *entity)
{
u32_t index = 0;
while(index < type->num_instances)
{
if(type->instances[index] == entity) return index;
++index;
}
return -1;
}
void tc_add_entity_instance_to_type(tc_entity_type_s *type, tc_entity_s *entity)
{
if(type->num_instances >= type->instances_capacity)
{
type->instances_capacity *= 2;
type->instances = realloc(type->instances, type->instances_capacity);
}
type->instances[type->num_instances] = entity;
++type->num_instances;
}
tc_entity_s * tc_create_entity(char *type) tc_entity_s * tc_create_entity(char *type)
{ {
tc_entity_type_s *type_struct = tc_get_entity_type_with_name(type); tc_entity_type_s *type_struct = tc_get_entity_type_with_name(type);
if(type_struct == NULL) return NULL; if(type_struct == NULL) return NULL;
tc_entity_s *entity = type_struct->functions.fn_create(); tc_entity_s *entity = tc_allocate_entity();
entity->type = type_struct; entity->type = type_struct;
++tc_game_state_g.entity_registry.num_entities; entity->num_attributes = 0;
entity->attributes_capacity = 8;
entity->attributes =
calloc(sizeof(tc_entity_attribute_s), entity->attributes_capacity);
type_struct->functions.fn_create(entity);
tc_add_entity_instance_to_type(type_struct, entity);
return entity; return entity;
} }
@ -82,14 +113,30 @@ void tc_draw_entity(tc_entity_s *entity)
void tc_delete_entity(tc_entity_s *entity) void tc_delete_entity(tc_entity_s *entity)
{ {
entity->type->functions.fn_draw(entity); tc_entity_type_s *type = entity->type;
i32_t instance_index =
tc_find_entity_instance_index_in_type(type, entity);
if(instance_index == -1)
{
puts("Entity can't be found as an instance of it's type?!");
}
else
{
--type->num_instances;
type->instances[instance_index] = type->instances[type->num_instances];
}
entity->type->functions.fn_delete(entity);
tc_deallocate_entity(entity);
} }
tc_entity_attribute_s * tc_get_entity_attribute_with_name(tc_entity_s *entity, char *name) tc_entity_attribute_s * tc_get_entity_attribute_with_name(tc_entity_s *entity, char *name)
{ {
uint32_t attribute_index = 0; u32_t attribute_index = 0;
while(attribute_index < entity->attributes_capacity) while(attribute_index < entity->attributes_capacity)
{ {
tc_entity_attribute_s attribute = entity->attributes[attribute_index]; tc_entity_attribute_s attribute = entity->attributes[attribute_index];
@ -175,7 +222,7 @@ double tc_get_float_from_entity(tc_entity_s *entity, char *attr_name)
void tc_forget_entity_attribute(tc_entity_s *entity, char *attr_name) void tc_forget_entity_attribute(tc_entity_s *entity, char *attr_name)
{ {
uint32_t attribute_index = 0; u32_t attribute_index = 0;
while(attribute_index < entity->attributes_capacity) while(attribute_index < entity->attributes_capacity)
{ {
tc_entity_attribute_s *attribute = &entity->attributes[attribute_index]; tc_entity_attribute_s *attribute = &entity->attributes[attribute_index];
@ -198,9 +245,9 @@ void tc_forget_entity_attribute(tc_entity_s *entity, char *attr_name)
int32_t tc_find_index_of_entity_in_intervals(tc_entity_s *entity) i32_t tc_find_index_of_entity_in_intervals(tc_entity_s *entity)
{ {
uint32_t interval_index = 0; u32_t interval_index = 0;
while(interval_index < tc_game_state_g.entity_registry.num_intervals) while(interval_index < tc_game_state_g.entity_registry.num_intervals)
{ {
if(tc_game_state_g.entity_registry.intervals[interval_index].entity == entity) if(tc_game_state_g.entity_registry.intervals[interval_index].entity == entity)
@ -211,7 +258,7 @@ int32_t tc_find_index_of_entity_in_intervals(tc_entity_s *entity)
return -1; return -1;
} }
uint32_t tc_allocate_interval() u32_t tc_allocate_interval()
{ {
if( if(
tc_game_state_g.entity_registry.num_intervals tc_game_state_g.entity_registry.num_intervals
@ -225,7 +272,7 @@ uint32_t tc_allocate_interval()
sizeof(tc_interval_s) * tc_game_state_g.entity_registry.intervals_capacity sizeof(tc_interval_s) * tc_game_state_g.entity_registry.intervals_capacity
); );
} }
uint32_t last = tc_game_state_g.entity_registry.num_intervals; u32_t last = tc_game_state_g.entity_registry.num_intervals;
tc_game_state_g.entity_registry.intervals[last].last_invocation = 0.0f; tc_game_state_g.entity_registry.intervals[last].last_invocation = 0.0f;
++tc_game_state_g.entity_registry.num_intervals; ++tc_game_state_g.entity_registry.num_intervals;
@ -234,7 +281,7 @@ uint32_t tc_allocate_interval()
void tc_schedule_interval(tc_entity_s *entity, float wanted_delta, void (*fn_interval)(tc_entity_s *entity)) void tc_schedule_interval(tc_entity_s *entity, float wanted_delta, void (*fn_interval)(tc_entity_s *entity))
{ {
int32_t interval_index; i32_t interval_index;
if((interval_index = tc_find_index_of_entity_in_intervals(entity)) == -1) if((interval_index = tc_find_index_of_entity_in_intervals(entity)) == -1)
{ {
interval_index = tc_allocate_interval(); interval_index = tc_allocate_interval();
@ -249,7 +296,7 @@ void tc_schedule_interval(tc_entity_s *entity, float wanted_delta, void (*fn_int
void tc_send_pointer_to_entity(tc_entity_s *entity, char *event_name, void *pointer) void tc_send_pointer_to_entity(tc_entity_s *entity, char *event_name, void *pointer)
{ {
tc_entity_event_s event; tc_entity_event_s event;
event.event_type = event_name; event.identifier = event_name;
event.value.pointer = pointer; event.value.pointer = pointer;
entity->type->functions.fn_send_event(entity, event); entity->type->functions.fn_send_event(entity, event);
} }
@ -257,7 +304,7 @@ void tc_send_pointer_to_entity(tc_entity_s *entity, char *event_name, void *poin
void tc_send_string_to_entity(tc_entity_s *entity, char *event_name, char *string) void tc_send_string_to_entity(tc_entity_s *entity, char *event_name, char *string)
{ {
tc_entity_event_s event; tc_entity_event_s event;
event.event_type = event_name; event.identifier = event_name;
event.value.string = string; event.value.string = string;
entity->type->functions.fn_send_event(entity, event); entity->type->functions.fn_send_event(entity, event);
} }
@ -265,7 +312,7 @@ void tc_send_string_to_entity(tc_entity_s *entity, char *event_name, char *strin
void tc_send_integer_to_entity(tc_entity_s *entity, char *event_name, int64_t integer) void tc_send_integer_to_entity(tc_entity_s *entity, char *event_name, int64_t integer)
{ {
tc_entity_event_s event; tc_entity_event_s event;
event.event_type = event_name; event.identifier = event_name;
event.value.integer = integer; event.value.integer = integer;
entity->type->functions.fn_send_event(entity, event); entity->type->functions.fn_send_event(entity, event);
} }
@ -273,7 +320,7 @@ void tc_send_integer_to_entity(tc_entity_s *entity, char *event_name, int64_t in
void tc_send_float_to_entity(tc_entity_s *entity, char *event_name, double floating) void tc_send_float_to_entity(tc_entity_s *entity, char *event_name, double floating)
{ {
tc_entity_event_s event; tc_entity_event_s event;
event.event_type = event_name; event.identifier = event_name;
event.value.floating = floating; event.value.floating = floating;
entity->type->functions.fn_send_event(entity, event); entity->type->functions.fn_send_event(entity, event);
} }
@ -283,11 +330,11 @@ void tc_tick_intervals()
uint64_t current_ms = SDL_GetTicks64(); uint64_t current_ms = SDL_GetTicks64();
tc_entity_registry_s registry = tc_game_state_g.entity_registry; tc_entity_registry_s registry = tc_game_state_g.entity_registry;
uint32_t interval_index = 0; u32_t interval_index = 0;
while(interval_index < registry.num_intervals) while(interval_index < registry.num_intervals)
{ {
tc_interval_s interval = registry.intervals[interval_index]; tc_interval_s interval = registry.intervals[interval_index];
uint32_t elapsed_time = current_ms - interval.last_invocation; u32_t elapsed_time = current_ms - interval.last_invocation;
if(elapsed_time >= interval.wanted_delta) if(elapsed_time >= interval.wanted_delta)
{ {
if(interval.fn_interval == NULL) if(interval.fn_interval == NULL)
@ -305,19 +352,15 @@ void tc_tick_intervals()
void tc_draw_all_entities_of_type(char *name) void tc_draw_all_entities_of_type(char *name)
{ {
tc_entity_type_s *type = tc_get_entity_type_with_name(name); tc_entity_type_s *type = tc_get_entity_type_with_name(name);
tc_entity_registry_s registry = tc_game_state_g.entity_registry;
uint32_t drawn_entities = 0; u32_t drawn_entities = 0;
uint32_t entity_index = 0; u32_t entity_index = 0;
while(entity_index < registry.num_entities) while(entity_index < type->num_instances)
{ {
tc_entity_s *entity = registry.entities[entity_index]; tc_entity_s *entity = type->instances[entity_index];
if(entity->type == type) tc_draw_entity(entity);
{ ++drawn_entities;
tc_draw_entity(entity);
++drawn_entities;
}
++entity_index; ++entity_index;
} }
} }

View File

@ -5,17 +5,19 @@
#include "utility/location.h" #include "utility/location.h"
#include "utility/utility.h" #include "utility/utility.h"
typedef struct tc_interval tc_interval_s;
typedef struct tc_entity_event tc_entity_event_s; typedef struct tc_entity_event tc_entity_event_s;
typedef struct tc_entity_type tc_entity_type_s; typedef struct tc_entity_type tc_entity_type_s;
typedef struct tc_entity_attribute tc_entity_attribute_s; typedef struct tc_entity_attribute tc_entity_attribute_s;
typedef struct tc_entity tc_entity_s; typedef struct tc_entity tc_entity_s;
typedef struct tc_interval tc_interval_s; typedef struct tc_entity_pool tc_entity_pool_s;
typedef struct tc_entity_pool_entry tc_entity_pool_entry_s;
typedef struct tc_entity_registry tc_entity_registry_s; typedef struct tc_entity_registry tc_entity_registry_s;
typedef struct typedef struct
{ {
tc_entity_s * (*fn_create) (); void (*fn_create) (tc_entity_s *entity);
void (*fn_spawn) (tc_entity_s *entity, tc_location_s location); void (*fn_spawn) (tc_entity_s *entity, tc_location_s location);
void (*fn_teleport) (tc_entity_s *entity, tc_location_s location); void (*fn_teleport) (tc_entity_s *entity, tc_location_s location);
void (*fn_draw) (tc_entity_s *entity); void (*fn_draw) (tc_entity_s *entity);
@ -26,14 +28,14 @@ typedef struct
struct tc_entity_event struct tc_entity_event
{ {
char *event_type; char *identifier;
union union
{ {
void *pointer; void *pointer;
char *string; char *string;
int64_t integer; i64_t integer;
double floating; f64_t floating;
} value; } value;
}; };
@ -43,30 +45,36 @@ struct tc_entity_type
char *display_name; char *display_name;
tc_fn_entity_s functions; tc_fn_entity_s functions;
u32_t instances_capacity;
u32_t num_instances;
tc_entity_s **instances;
}; };
struct tc_entity_attribute struct tc_entity_attribute
{ {
char *name; char *name;
uint32_t index; u32_t index;
union union
{ {
void *pointer; void *pointer;
char *string; char *string;
int64_t integer; i64_t integer;
double floating; f64_t floating;
} value; } value;
}; };
struct tc_entity struct tc_entity
{ {
tc_entity_pool_entry_s *pool_entry;
tc_location_s location; tc_location_s location;
tc_entity_type_s *type; tc_entity_type_s *type;
uint16_t attributes_capacity; u16_t attributes_capacity;
uint16_t num_attributes; u16_t num_attributes;
tc_entity_attribute_s *attributes; tc_entity_attribute_s *attributes;
void *specific; void *specific;
@ -75,27 +83,43 @@ struct tc_entity
struct tc_interval struct tc_interval
{ {
uint64_t last_invocation; u64_t last_invocation;
uint64_t wanted_delta; u64_t wanted_delta;
tc_entity_s *entity; tc_entity_s *entity;
void (*fn_interval) (tc_entity_s *entity); void (*fn_interval) (tc_entity_s *entity);
}; };
struct tc_entity_registry struct tc_entity_pool_entry
{ {
uint32_t types_capacity; tc_entity_pool_entry_s *next;
uint32_t num_types; tc_entity_pool_entry_s *previous;
tc_entity_type_s *types;
uint32_t entities_capacity; tc_entity_s entity;
uint32_t num_entities;
tc_entity_s **entities;
uint32_t intervals_capacity;
uint32_t num_intervals;
tc_interval_s *intervals;
}; };
struct tc_entity_pool
{
u32_t entities_capacity;
tc_entity_pool_entry_s *entities;
tc_entity_pool_entry_s *first_free;
tc_entity_pool_s *continuation;
};
struct tc_entity_registry
{
u32_t types_capacity;
u32_t num_types;
tc_entity_type_s *types;
tc_entity_pool_s *pool;
u32_t intervals_capacity;
u32_t num_intervals;
tc_interval_s *intervals;
};
tc_entity_pool_s * tc_create_entity_pool (u32_t capacity);
tc_entity_registry_s tc_init_entity_registry (); tc_entity_registry_s tc_init_entity_registry ();
void tc_register_entity_type (tc_entity_type_s type); void tc_register_entity_type (tc_entity_type_s type);
@ -109,18 +133,18 @@ void tc_delete_entity (tc_entity_s *en
void tc_set_pointer_for_entity (tc_entity_s *entity, char *attr_name, void *pointer); void tc_set_pointer_for_entity (tc_entity_s *entity, char *attr_name, void *pointer);
void tc_set_string_for_entity (tc_entity_s *entity, char *attr_name, char *string); void tc_set_string_for_entity (tc_entity_s *entity, char *attr_name, char *string);
void tc_set_integer_for_entity (tc_entity_s *entity, char *attr_name, int64_t integer); void tc_set_integer_for_entity (tc_entity_s *entity, char *attr_name, i64_t integer);
void tc_set_float_for_entity (tc_entity_s *entity, char *attr_name, double floating); void tc_set_float_for_entity (tc_entity_s *entity, char *attr_name, f64_t floating);
void * tc_get_pointer_from_entity (tc_entity_s *entity, char *attr_name); void * tc_get_pointer_from_entity (tc_entity_s *entity, char *attr_name);
char * tc_get_string_from_entity (tc_entity_s *entity, char *attr_name); char * tc_get_string_from_entity (tc_entity_s *entity, char *attr_name);
int64_t tc_get_integer_from_entity (tc_entity_s *entity, char *attr_name); i64_t tc_get_integer_from_entity (tc_entity_s *entity, char *attr_name);
double tc_get_float_from_entity (tc_entity_s *entity, char *attr_name); f64_t tc_get_float_from_entity (tc_entity_s *entity, char *attr_name);
void tc_send_pointer_to_entity (tc_entity_s *entity, char *event_name, void *pointer); void tc_send_pointer_to_entity (tc_entity_s *entity, char *event_name, void *pointer);
void tc_send_string_to_entity (tc_entity_s *entity, char *event_name, char *string); void tc_send_string_to_entity (tc_entity_s *entity, char *event_name, char *string);
void tc_send_integer_to_entity (tc_entity_s *entity, char *event_name, int64_t integer); void tc_send_integer_to_entity (tc_entity_s *entity, char *event_name, i64_t integer);
void tc_send_float_to_entity (tc_entity_s *entity, char *event_name, double floating); void tc_send_float_to_entity (tc_entity_s *entity, char *event_name, f64_t floating);
void tc_schedule_interval (tc_entity_s *entity, float wanted_delta, void (*fn_interval)(tc_entity_s *entity)); void tc_schedule_interval (tc_entity_s *entity, float wanted_delta, void (*fn_interval)(tc_entity_s *entity));
void tc_tick_intervals (); void tc_tick_intervals ();

View File

@ -4,18 +4,11 @@
tc_entity_type_s *tc_player_entity_type_g = NULL; tc_entity_type_s *tc_player_entity_type_g = NULL;
tc_entity_s * tc_fn_create_player_entity() void tc_fn_create_player_entity(tc_entity_s *entity)
{ {
tc_entity_s *entity = tc_allocate_entity();
entity->type = tc_player_entity_type_g; entity->type = tc_player_entity_type_g;
entity->location = tc_zero_location(); entity->location = tc_zero_location();
entity->specific = NULL; entity->specific = NULL;
entity->num_attributes = 0;
entity->attributes_capacity = 8;
entity->attributes =
calloc(sizeof(tc_entity_attribute_s), entity->attributes_capacity);
return entity;
} }
void tc_fn_spawn_player_entity(tc_entity_s *entity, tc_location_s location) void tc_fn_spawn_player_entity(tc_entity_s *entity, tc_location_s location)
@ -48,6 +41,7 @@ void tc_register_player_entity()
tc_entity_type_s type; tc_entity_type_s type;
type.internal_name = "player"; type.internal_name = "player";
type.display_name = "Player"; type.display_name = "Player";
type.instances_capacity = 8;
type.functions.fn_create = tc_fn_create_player_entity; type.functions.fn_create = tc_fn_create_player_entity;
type.functions.fn_spawn = tc_fn_spawn_player_entity; type.functions.fn_spawn = tc_fn_spawn_player_entity;
type.functions.fn_teleport = tc_fn_teleport_player_entity; type.functions.fn_teleport = tc_fn_teleport_player_entity;

View File

@ -5,6 +5,27 @@
// TODO: Write a *real* pool allocator // TODO: Write a *real* pool allocator
tc_entity_pool_s * tc_create_entity_pool(u32_t capacity)
{
tc_entity_pool_s *pool = malloc(sizeof(tc_entity_pool_s));
pool->entities_capacity = capacity;
pool->entities = calloc(sizeof(tc_entity_pool_entry_s), capacity);
pool->first_free = &pool->entities[0];
pool->continuation = NULL;
u32_t entity_index = 0;
while(entity_index < capacity)
{
pool->entities[entity_index].next = &pool->entities[entity_index+1];
pool->entities[entity_index].previous = &pool->entities[entity_index-1];
++entity_index;
}
pool->entities[0].previous = NULL;
pool->entities[capacity-1].next = NULL;
return pool;
}
void tc_reset_entity(tc_entity_s *entity) void tc_reset_entity(tc_entity_s *entity)
{ {
if(entity->attributes != NULL) free(entity->attributes); if(entity->attributes != NULL) free(entity->attributes);
@ -14,58 +35,32 @@ void tc_reset_entity(tc_entity_s *entity)
} }
tc_entity_s * tc_allocate_entity() tc_entity_s * tc_allocate_entity_from_pool(tc_entity_pool_s *pool)
{ {
tc_entity_registry_s registry = tc_game_state_g.entity_registry; if(pool->first_free == NULL)
uint32_t entity_index = 0;
while(entity_index < registry.entities_capacity)
{ {
if(registry.entities[entity_index] == NULL) if(pool->continuation == NULL)
{ pool->continuation = tc_create_entity_pool(pool->entities_capacity * 2);
registry.entities[entity_index] = calloc(sizeof(tc_entity_s), 1);
++registry.num_entities; return tc_allocate_entity_from_pool(pool->continuation);
registry.entities[entity_index]->attributes_capacity = 8;
registry.entities[entity_index]->attributes =
calloc(sizeof(tc_entity_attribute_s), registry.entities[entity_index]->attributes_capacity);
tc_reset_entity(registry.entities[entity_index]);
return registry.entities[entity_index];
}
++entity_index;
} }
registry.entities = tc_entity_s *entity = &pool->first_free->entity;
realloc(registry.entities, sizeof(tc_entity_s *) * registry.entities_capacity * 2); entity->pool_entry = pool->first_free;
memset(&registry.entities[registry.entities_capacity], 0x00, sizeof(tc_entity_s *) * registry.entities_capacity); pool->first_free = pool->first_free->next;
registry.entities_capacity *= 2;
registry.entities[registry.num_entities] = malloc(sizeof(tc_entity_s)); return entity;
tc_reset_entity(registry.entities[registry.num_entities]); }
++registry.num_entities;
tc_entity_s * tc_allocate_entity()
return registry.entities[registry.num_entities-1]; {
return tc_allocate_entity_from_pool(tc_game_state_g.entity_registry.pool);
} }
void tc_deallocate_entity(tc_entity_s *entity) void tc_deallocate_entity(tc_entity_s *entity)
{ {
if(entity->specific != NULL) free(entity->specific); tc_entity_pool_s *pool = tc_game_state_g.entity_registry.pool;
if(entity->attributes != NULL) free(entity->attributes); entity->pool_entry->next = pool->first_free;
pool->first_free = entity->pool_entry;
tc_entity_registry_s registry = tc_game_state_g.entity_registry;
uint32_t entity_index = 0;
while(entity_index < registry.entities_capacity)
{
if(registry.entities[entity_index] == entity)
{
registry.entities[entity_index] = NULL;
break;
}
++entity_index;
}
free(entity);
} }

View File

@ -290,9 +290,6 @@ bool update()
tc_update_world(tc_game_state_g.main_world); tc_update_world(tc_game_state_g.main_world);
// uint64_t current_ms = SDL_GetTicks64();
// uint32_t ticks_between = current_ms - tc_last_update;
return true; return true;
} }

View File

@ -0,0 +1,4 @@
#include "primitives.h"

View File

@ -0,0 +1,3 @@
#include "primitives.h"

View File

@ -0,0 +1,42 @@
#include "simulation.h"
tc_physics_simulation_s tc_new_physics_simulation()
{
}
void tc_add_physics_object(tc_physics_simulation_s *simulation, tc_physics_entity object)
{
}
void tc_add_physics_object_to(tc_physics_entity_s *container, tc_physics_entity_s object)
{
}
tc_phyiscs_entity_s tc_new_physics_entity()
{
}
void tc_tick_physics(tc_physics_simulation_s *simulation, float delta)
{
}

View File

@ -0,0 +1,4 @@
#include "primitives.h"

View File

@ -0,0 +1,97 @@
#ifndef TC_PHYSICS_PRIMITIVES_H
#define TC_PHYSICS_PRIMITIVES_H
#include <stdbool.h>
#include <stdint.h>
#include "../utility.h"
typedef struct tc_physics_mesh tc_physics_mesh_s;
typedef struct tc_aabb tc_aabb_s;
typedef struct tc_obb tc_obb_s;
typedef struct tc_plane tc_plane_s;
typedef struct tc_ellipsoid tc_ellipsoid_s;
typedef struct tc_ray tc_ray_s;
typedef struct tc_line tc_line_s;
typedef enum
{
TC_PRIMITIVE_AABB,
TC_PRIMITIVE_OBB,
TC_PRIMITIVE_PLANE,
TC_PRIMITIVE_ELLIPSOID,
TC_PRIMITIVE_RAY,
TC_PRIMITIVE_LINE
} tc_primitive_type_e;
struct tc_aabb
{
tc_vec3_s start;
tc_vec3_s size;
};
struct tc_obb
{
tc_vec3_s start;
tc_vec3_s size;
tc_vec3_s rotation;
};
struct tc_plane
{
tc_vec3_s center;
tc_vec3_s rotation;
tc_vec2i_s size;
};
struct tc_ellipsoid
{
tc_vec3_s center;
tc_vec3_s extent;
};
struct tc_ray
{
tc_vec3_s start;
tc_vec3_s rotation;
};
struct tc_line
{
tc_vec3_s start;
tc_vec3_s end;
};
typedef union tc_primitive
{
tc_aabb_s aabb;
tc_obb_s obb;
tc_plane_s plane;
tc_ellipsoid_s sphere;
tc_plane_s plane;
tc_line_s line;
tc_ray_s ray;
} tc_primitive_u;
typedef struct tc_primitive
{
tc_primitive_type_e type;
tc_primitive_u data;
} tc_primitive_s;
struct tc_physics_mesh
{
uint32_t primitives_capacity;
uint32_t num_primitives;
tc_primitive_s *primitives;
};
#endif // TC_PHYSICS_PRIMITIVES_H

View File

@ -0,0 +1,10 @@
#include "primitives.h"
tc_primitive_s * tc_cast_line(tc_physics_simulation_s *simulation, tc_line_s line)
{
}

View File

@ -0,0 +1,78 @@
#ifndef TC_PHYSICS_SIMULATION_H
#define TC_PHYSICS_SIMULATION_H
#include <stdint.h>
#include <utility/utility.h>
#include <../entity.h>
typedef struct tc_physics_entity tc_physics_entity_s;
typedef struct tc_entity_physics_attributes tc_entity_physics_attributes;
struct tc_entity_physics_attributes
{
bool has_gravity;
bool raw_movement;
bool raw_rotation;
tc_vec3_s acceleration;
tc_vec3_s spin;
};
typedef enum
{
TC_PHYSICS_ENTITY,
TC_PHYSICS_ENTITY_GROUP
} tc_physics_entity_type_e;
struct tc_physics_entity
{
tc_physics_entity *super;
// relative_position: The position of this physics entity relative to its super-entity
tc_vec3_s relative_position;
tc_physics_entity_e type;
union specific
{
struct entity
{
tc_entity_s *entity;
} entity;
struct subentity_group
{
uint32_t num_entities;
tc_physics_entity_s *entities;
} group;
} value;
};
typedef struct tc_physics_simulation
{
tc_vec3_s limits;
tc_physics_entity_s world_root;
} tc_physics_simulation_s;
tc_physics_simulation_s tc_new_physics_simulation ();
void tc_add_physics_object (tc_physics_simulation_s *simulation, tc_physics_entity object);
void tc_add_physics_object_to (tc_physics_entity_s *container, tc_physics_entity_s object);
tc_phyiscs_entity_s tc_new_physics_entity ();
void tc_tick_physics (tc_physics_simulation_s *simulation, float delta);
tc_primitive_s tc_cast_line (tc_physics_simulation_s *simulation, tc_line_s line);
#endif

View File

@ -2,11 +2,11 @@
#ifndef TC_LOCATION_H #ifndef TC_LOCATION_H
#define TC_LOCATION_H #define TC_LOCATION_H
#include <world.h> #include <utility/math.h>
typedef struct tc_location typedef struct tc_location
{ {
tc_world_s *world; void *world;
tc_vec3f_s position; tc_vec3f_s position;
tc_vec3f_s rotation; tc_vec3f_s rotation;

View File

@ -84,16 +84,13 @@ void tc_init_world_generators()
tc_entity_s * tc_create_chunk_entity() void tc_create_chunk_entity(tc_entity_s *entity)
{ {
tc_entity_s *chunk = tc_allocate_entity(); tc_set_string_for_entity(entity, "type", "chunk");
tc_set_string_for_entity(chunk, "type", "chunk"); tc_set_integer_for_entity(entity, "grid_x", 0);
tc_set_integer_for_entity(chunk, "grid_x", 0); tc_set_integer_for_entity(entity, "grid_y", 0);
tc_set_integer_for_entity(chunk, "grid_y", 0); tc_set_integer_for_entity(entity, "grid_z", 0);
tc_set_integer_for_entity(chunk, "grid_z", 0); entity->specific = tc_allocate_chunk();
chunk->specific = tc_allocate_chunk();
return chunk;
} }
void tc_spawn_chunk_entity(tc_entity_s *entity, tc_location_s location) void tc_spawn_chunk_entity(tc_entity_s *entity, tc_location_s location)
@ -118,16 +115,11 @@ void tc_teleport_chunk_entity(tc_entity_s *entity, tc_location_s location)
void tc_delete_chunk_entity(tc_entity_s *entity) void tc_delete_chunk_entity(tc_entity_s *entity)
{ {
tc_free_chunk(entity->specific); tc_free_chunk(entity->specific);
} }
void tc_receive_chunk_entity_event(tc_entity_s *entity, tc_entity_event_s event) void tc_receive_chunk_entity_event(tc_entity_s *entity, tc_entity_event_s event)
{ {
} }
void tc_add_chunk_entity_type() void tc_add_chunk_entity_type()
@ -144,6 +136,7 @@ void tc_add_chunk_entity_type()
chunk_type.internal_name = "chunk"; chunk_type.internal_name = "chunk";
chunk_type.display_name = NULL; chunk_type.display_name = NULL;
chunk_type.functions = chunk_functions; chunk_type.functions = chunk_functions;
chunk_type.instances_capacity = 512;
tc_register_entity_type(chunk_type); tc_register_entity_type(chunk_type);
tc_chunk_entity_type_g = tc_get_entity_type_with_name("chunk"); tc_chunk_entity_type_g = tc_get_entity_type_with_name("chunk");
@ -180,7 +173,7 @@ tc_chunkloader_s tc_create_chunkloader(tc_world_s *world)
loader.center.y = 0; loader.center.y = 0;
loader.center.z = 0; loader.center.z = 0;
loader.chunks_capacity = 512; loader.chunks_capacity = 512;
loader.chunks = calloc(sizeof(tc_chunk_s *), loader.chunks_capacity); loader.chunks = calloc(sizeof(tc_entity_s *), loader.chunks_capacity);
loader.num_chunks = 0; loader.num_chunks = 0;
loader.needs_reload = false; loader.needs_reload = false;

View File

@ -3,10 +3,10 @@
#define TC_WORLD_H #define TC_WORLD_H
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h>
#include <blocks.h> #include <blocks.h>
#include <utility/math.h> #include <utility/math.h>
#include <entity.h>
#define UPDATE_DISTANCE 2 #define UPDATE_DISTANCE 2
@ -29,7 +29,7 @@ struct tc_worldgen
struct tc_block struct tc_block
{ {
uint32_t type_identifier; u32_t type_identifier;
tc_vec3f_s position; tc_vec3f_s position;
tc_vec3f_s rotation; tc_vec3f_s rotation;
@ -41,14 +41,14 @@ struct tc_chunk
tc_chunk_pool_entry_s *pool_entry; tc_chunk_pool_entry_s *pool_entry;
tc_vec3i_s position; tc_vec3i_s position;
uint32_t blocks[32][32][32]; u32_t blocks[32][32][32];
uint32_t num_vertices; u32_t num_vertices;
float *vertex_positions; f32_t *vertex_positions;
float *vertex_uvs; f32_t *vertex_uvs;
uint32_t vao; u32_t vao;
uint32_t vertex_data; u32_t vertex_data;
}; };
struct tc_chunk_pool_entry struct tc_chunk_pool_entry
@ -64,9 +64,9 @@ typedef struct tc_chunkloader
tc_vec3i_s center; tc_vec3i_s center;
tc_vec3i_s extent; tc_vec3i_s extent;
uint32_t chunks_capacity; u32_t chunks_capacity;
uint32_t num_chunks; u32_t num_chunks;
tc_chunk_s **chunks; tc_entity_s **chunks;
tc_world_s *world; tc_world_s *world;
bool needs_reload; bool needs_reload;
@ -77,8 +77,8 @@ struct tc_chunk_pool
{ {
tc_world_s *world; tc_world_s *world;
uint32_t capacity; u32_t capacity;
uint32_t used_entries; u32_t used_entries;
tc_chunk_pool_entry_s *entries; tc_chunk_pool_entry_s *entries;
tc_chunk_pool_entry_s *first_free; tc_chunk_pool_entry_s *first_free;
@ -92,7 +92,7 @@ struct tc_world
tc_chunk_pool_s *pool; tc_chunk_pool_s *pool;
tc_worldgen_s *worldgen; tc_worldgen_s *worldgen;
uint32_t num_loaders; u32_t num_loaders;
tc_chunkloader_s *loaders; tc_chunkloader_s *loaders;
// chunk_loading_center: The center of loading ON THE CHUNK GRID COORDINATES // chunk_loading_center: The center of loading ON THE CHUNK GRID COORDINATES
@ -105,24 +105,24 @@ void tc_init_worlds ();
void tc_draw_world (tc_world_s *world); void tc_draw_world (tc_world_s *world);
tc_world_s * tc_new_world (tc_worldgen_s *generator); tc_world_s * tc_new_world (tc_worldgen_s *generator);
tc_chunk_s * tc_new_chunk (tc_world_s *world, int32_t x, int32_t y, int32_t z); // must be integers tc_chunk_s * tc_new_chunk (tc_world_s *world, i32_t x, i32_t y, i32_t z); // must be integers
void tc_set_block_in_chunk( void tc_set_block_in_chunk(
tc_chunk_s *chunk, tc_chunk_s *chunk,
uint8_t x, uint8_t y, uint8_t z, u8_t x, u8_t y, u8_t z,
tc_block_s block tc_block_s block
); );
uint32_t tc_count_chunk_vertices (tc_chunk_s *chunk); u32_t tc_count_chunk_vertices (tc_chunk_s *chunk);
void tc_meshize_chunk (tc_chunk_s *chunk); void tc_meshize_chunk (tc_chunk_s *chunk);
void tc_upload_chunk (tc_chunk_s *chunk); void tc_upload_chunk (tc_chunk_s *chunk);
bool tc_generate_default_terrain_chunk (tc_worldgen_s *gen, tc_chunk_s *chunk); bool tc_generate_default_terrain_chunk (tc_worldgen_s *gen, tc_chunk_s *chunk);
void tc_add_chunk_to_loader (tc_chunkloader_s *chunkloader, tc_chunk_s *chunk); void tc_add_chunk_to_loader (tc_chunkloader_s *chunkloader, tc_entity_s *entity);
tc_chunk_s * tc_load_chunk_at (tc_world_s *world, int32_t x, int32_t y, int32_t z); tc_chunk_s * tc_load_chunk_at (tc_world_s *world, i32_t x, i32_t y, i32_t z);
tc_chunk_s * tc_get_loaded_chunk_at (tc_world_s *world, int32_t x, int32_t y, int32_t z); tc_chunk_s * tc_get_loaded_chunk_at (tc_world_s *world, i32_t x, i32_t y, i32_t z);
bool tc_chunk_is_loaded (tc_world_s *world, int32_t x, int32_t y, int32_t z); bool tc_chunk_is_loaded (tc_world_s *world, i32_t x, i32_t y, i32_t z);
void tc_update_world (tc_world_s *world); void tc_update_world (tc_world_s *world);
@ -132,7 +132,7 @@ void tc_on_each_loaded_chunk (
void *userdata void *userdata
); );
void tc_init_chunk_pool (uint32_t capacity); void tc_init_chunk_pool (u32_t capacity);
tc_chunk_s * tc_allocate_chunk (); tc_chunk_s * tc_allocate_chunk ();
void tc_free_chunk (tc_chunk_s *chunk); void tc_free_chunk (tc_chunk_s *chunk);