diff --git a/code/source-c/chunk_loader.c b/code/source-c/chunk_loader.c index 8098c25..536591d 100644 --- a/code/source-c/chunk_loader.c +++ b/code/source-c/chunk_loader.c @@ -4,14 +4,14 @@ #include #include -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) { 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; } @@ -37,16 +37,19 @@ tc_chunkloader_s * tc_get_corresponding_loader(tc_world_s *world, tc_vec3i_s coo 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) - 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 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); 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); } -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_chunk_s *chunk = entity->specific; chunk = entity->specific; chunk->position = coord; loader->world->worldgen->fn_generate_chunk(loader->world->worldgen, 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) { - 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; 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) { - tc_free_chunk(loader->chunks[index]); + tc_delete_entity(loader->chunks[index]); 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) { - 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)) { tc_remove_loaders_chunk(loader, chunk_index); diff --git a/code/source-c/entity.c b/code/source-c/entity.c index 7e30d2a..a9908ba 100644 --- a/code/source-c/entity.c +++ b/code/source-c/entity.c @@ -10,12 +10,10 @@ tc_entity_registry_s tc_init_entity_registry() registry.types_capacity = 64; registry.num_types = 0; 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.num_intervals = 0; registry.intervals = calloc(sizeof(tc_interval_s *), registry.intervals_capacity); + registry.pool = tc_create_entity_pool(512); 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.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) { - 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; - uint32_t index = 0; + u32_t index = 0; while(index < num_types) { 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; } +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_type_s *type_struct = tc_get_entity_type_with_name(type); if(type_struct == NULL) return NULL; - tc_entity_s *entity = type_struct->functions.fn_create(); - entity->type = type_struct; - ++tc_game_state_g.entity_registry.num_entities; + tc_entity_s *entity = tc_allocate_entity(); + entity->type = type_struct; + 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; } @@ -82,14 +113,30 @@ void tc_draw_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) { - uint32_t attribute_index = 0; + u32_t attribute_index = 0; while(attribute_index < entity->attributes_capacity) { 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) { - uint32_t attribute_index = 0; + u32_t attribute_index = 0; while(attribute_index < entity->attributes_capacity) { 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) { 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; } -uint32_t tc_allocate_interval() +u32_t tc_allocate_interval() { if( 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 ); } - 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.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)) { - int32_t interval_index; + i32_t interval_index; if((interval_index = tc_find_index_of_entity_in_intervals(entity)) == -1) { 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) { tc_entity_event_s event; - event.event_type = event_name; + event.identifier = event_name; event.value.pointer = pointer; 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) { tc_entity_event_s event; - event.event_type = event_name; + event.identifier = event_name; event.value.string = string; 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) { tc_entity_event_s event; - event.event_type = event_name; + event.identifier = event_name; event.value.integer = integer; 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) { tc_entity_event_s event; - event.event_type = event_name; + event.identifier = event_name; event.value.floating = floating; entity->type->functions.fn_send_event(entity, event); } @@ -283,11 +330,11 @@ void tc_tick_intervals() uint64_t current_ms = SDL_GetTicks64(); 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) { 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(interval.fn_interval == NULL) @@ -305,19 +352,15 @@ void tc_tick_intervals() void tc_draw_all_entities_of_type(char *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; - uint32_t entity_index = 0; - while(entity_index < registry.num_entities) + u32_t drawn_entities = 0; + u32_t entity_index = 0; + while(entity_index < type->num_instances) { - tc_entity_s *entity = registry.entities[entity_index]; - if(entity->type == type) - { - tc_draw_entity(entity); - ++drawn_entities; - } + tc_entity_s *entity = type->instances[entity_index]; + tc_draw_entity(entity); + ++drawn_entities; + ++entity_index; } } - diff --git a/code/source-c/entity.h b/code/source-c/entity.h index aba1a54..246a3c9 100644 --- a/code/source-c/entity.h +++ b/code/source-c/entity.h @@ -5,17 +5,19 @@ #include "utility/location.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_type tc_entity_type_s; typedef struct tc_entity_attribute tc_entity_attribute_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_s * (*fn_create) (); + void (*fn_create) (tc_entity_s *entity); void (*fn_spawn) (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); @@ -26,14 +28,14 @@ typedef struct struct tc_entity_event { - char *event_type; + char *identifier; union { void *pointer; char *string; - int64_t integer; - double floating; + i64_t integer; + f64_t floating; } value; }; @@ -43,30 +45,36 @@ struct tc_entity_type char *display_name; tc_fn_entity_s functions; + + u32_t instances_capacity; + u32_t num_instances; + tc_entity_s **instances; }; struct tc_entity_attribute { char *name; - uint32_t index; + u32_t index; union { void *pointer; char *string; - int64_t integer; - double floating; + i64_t integer; + f64_t floating; } value; }; struct tc_entity { + tc_entity_pool_entry_s *pool_entry; + tc_location_s location; tc_entity_type_s *type; - uint16_t attributes_capacity; - uint16_t num_attributes; + u16_t attributes_capacity; + u16_t num_attributes; tc_entity_attribute_s *attributes; void *specific; @@ -75,27 +83,43 @@ struct tc_entity struct tc_interval { - uint64_t last_invocation; - uint64_t wanted_delta; + u64_t last_invocation; + u64_t wanted_delta; tc_entity_s *entity; void (*fn_interval) (tc_entity_s *entity); }; -struct tc_entity_registry +struct tc_entity_pool_entry { - uint32_t types_capacity; - uint32_t num_types; - tc_entity_type_s *types; + tc_entity_pool_entry_s *next; + tc_entity_pool_entry_s *previous; - uint32_t entities_capacity; - uint32_t num_entities; - tc_entity_s **entities; - - uint32_t intervals_capacity; - uint32_t num_intervals; - tc_interval_s *intervals; + tc_entity_s entity; }; +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 (); 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_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_float_for_entity (tc_entity_s *entity, char *attr_name, double floating); +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, f64_t floating); 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); -int64_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); +i64_t tc_get_integer_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_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_float_to_entity (tc_entity_s *entity, char *event_name, double floating); +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, f64_t floating); void tc_schedule_interval (tc_entity_s *entity, float wanted_delta, void (*fn_interval)(tc_entity_s *entity)); void tc_tick_intervals (); diff --git a/code/source-c/entity/player.c b/code/source-c/entity/player.c index aa9b4db..9161086 100644 --- a/code/source-c/entity/player.c +++ b/code/source-c/entity/player.c @@ -4,18 +4,11 @@ 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->location = tc_zero_location(); 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) @@ -48,6 +41,7 @@ void tc_register_player_entity() tc_entity_type_s type; type.internal_name = "player"; type.display_name = "Player"; + type.instances_capacity = 8; type.functions.fn_create = tc_fn_create_player_entity; type.functions.fn_spawn = tc_fn_spawn_player_entity; type.functions.fn_teleport = tc_fn_teleport_player_entity; diff --git a/code/source-c/entity_pool.c b/code/source-c/entity_pool.c index dfd5f3a..50930a5 100644 --- a/code/source-c/entity_pool.c +++ b/code/source-c/entity_pool.c @@ -5,6 +5,27 @@ // 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) { 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; - - uint32_t entity_index = 0; - while(entity_index < registry.entities_capacity) + if(pool->first_free == NULL) { - if(registry.entities[entity_index] == NULL) - { - registry.entities[entity_index] = calloc(sizeof(tc_entity_s), 1); - ++registry.num_entities; - - 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; + if(pool->continuation == NULL) + pool->continuation = tc_create_entity_pool(pool->entities_capacity * 2); + + return tc_allocate_entity_from_pool(pool->continuation); } - registry.entities = - realloc(registry.entities, sizeof(tc_entity_s *) * registry.entities_capacity * 2); - memset(®istry.entities[registry.entities_capacity], 0x00, sizeof(tc_entity_s *) * registry.entities_capacity); - registry.entities_capacity *= 2; + tc_entity_s *entity = &pool->first_free->entity; + entity->pool_entry = pool->first_free; + pool->first_free = pool->first_free->next; - registry.entities[registry.num_entities] = malloc(sizeof(tc_entity_s)); - tc_reset_entity(registry.entities[registry.num_entities]); - ++registry.num_entities; - - return registry.entities[registry.num_entities-1]; + return entity; +} + +tc_entity_s * tc_allocate_entity() +{ + return tc_allocate_entity_from_pool(tc_game_state_g.entity_registry.pool); } void tc_deallocate_entity(tc_entity_s *entity) { - if(entity->specific != NULL) free(entity->specific); - if(entity->attributes != NULL) free(entity->attributes); - - 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); + tc_entity_pool_s *pool = tc_game_state_g.entity_registry.pool; + entity->pool_entry->next = pool->first_free; + pool->first_free = entity->pool_entry; } diff --git a/code/source-c/utility/location.h b/code/source-c/utility/location.h index a09f9df..ec3694f 100644 --- a/code/source-c/utility/location.h +++ b/code/source-c/utility/location.h @@ -2,11 +2,11 @@ #ifndef TC_LOCATION_H #define TC_LOCATION_H -#include +#include typedef struct tc_location { - tc_world_s *world; + void *world; tc_vec3f_s position; tc_vec3f_s rotation; diff --git a/code/source-c/world.c b/code/source-c/world.c index 36c58c5..8347cf1 100644 --- a/code/source-c/world.c +++ b/code/source-c/world.c @@ -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(chunk, "type", "chunk"); - tc_set_integer_for_entity(chunk, "grid_x", 0); - tc_set_integer_for_entity(chunk, "grid_y", 0); - tc_set_integer_for_entity(chunk, "grid_z", 0); - chunk->specific = tc_allocate_chunk(); - - return chunk; + tc_set_string_for_entity(entity, "type", "chunk"); + tc_set_integer_for_entity(entity, "grid_x", 0); + tc_set_integer_for_entity(entity, "grid_y", 0); + tc_set_integer_for_entity(entity, "grid_z", 0); + entity->specific = tc_allocate_chunk(); } 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) { tc_free_chunk(entity->specific); - - - } void tc_receive_chunk_entity_event(tc_entity_s *entity, tc_entity_event_s event) { - - } void tc_add_chunk_entity_type() @@ -144,6 +136,7 @@ void tc_add_chunk_entity_type() chunk_type.internal_name = "chunk"; chunk_type.display_name = NULL; chunk_type.functions = chunk_functions; + chunk_type.instances_capacity = 512; tc_register_entity_type(chunk_type); 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.z = 0; 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.needs_reload = false; diff --git a/code/source-c/world.h b/code/source-c/world.h index 9ef8b72..77e2495 100644 --- a/code/source-c/world.h +++ b/code/source-c/world.h @@ -3,10 +3,10 @@ #define TC_WORLD_H #include -#include #include #include +#include #define UPDATE_DISTANCE 2 @@ -29,7 +29,7 @@ struct tc_worldgen struct tc_block { - uint32_t type_identifier; + u32_t type_identifier; tc_vec3f_s position; tc_vec3f_s rotation; @@ -41,14 +41,14 @@ struct tc_chunk tc_chunk_pool_entry_s *pool_entry; tc_vec3i_s position; - uint32_t blocks[32][32][32]; + u32_t blocks[32][32][32]; - uint32_t num_vertices; - float *vertex_positions; - float *vertex_uvs; + u32_t num_vertices; + f32_t *vertex_positions; + f32_t *vertex_uvs; - uint32_t vao; - uint32_t vertex_data; + u32_t vao; + u32_t vertex_data; }; struct tc_chunk_pool_entry @@ -64,9 +64,9 @@ typedef struct tc_chunkloader tc_vec3i_s center; tc_vec3i_s extent; - uint32_t chunks_capacity; - uint32_t num_chunks; - tc_chunk_s **chunks; + u32_t chunks_capacity; + u32_t num_chunks; + tc_entity_s **chunks; tc_world_s *world; bool needs_reload; @@ -77,8 +77,8 @@ struct tc_chunk_pool { tc_world_s *world; - uint32_t capacity; - uint32_t used_entries; + u32_t capacity; + u32_t used_entries; tc_chunk_pool_entry_s *entries; tc_chunk_pool_entry_s *first_free; @@ -92,7 +92,7 @@ struct tc_world tc_chunk_pool_s *pool; tc_worldgen_s *worldgen; - uint32_t num_loaders; + u32_t num_loaders; tc_chunkloader_s *loaders; // 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); 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( 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 ); -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_upload_chunk (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); -tc_chunk_s * tc_load_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, int32_t x, int32_t y, int32_t z); -bool tc_chunk_is_loaded (tc_world_s *world, int32_t x, int32_t y, int32_t z); +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, i32_t x, i32_t y, i32_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, i32_t x, i32_t y, i32_t z); void tc_update_world (tc_world_s *world); @@ -132,7 +132,7 @@ void tc_on_each_loaded_chunk ( void *userdata ); -void tc_init_chunk_pool (uint32_t capacity); +void tc_init_chunk_pool (u32_t capacity); tc_chunk_s * tc_allocate_chunk (); void tc_free_chunk (tc_chunk_s *chunk);