Compare commits

..

No commits in common. "1602130459a40ff20809e95338aa5c9569628ae1" and "dc3a81b7b4e82eedc9852e13db6a03b8f9fb2c1c" have entirely different histories.

16 changed files with 171 additions and 462 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_entity_s *entity) void tc_add_chunk_to_loader(tc_chunkloader_s *chunkloader, tc_chunk_s *chunk)
{ {
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_entity_s *) * chunkloader->chunks_capacity); chunkloader->chunks = realloc(chunkloader->chunks, sizeof(tc_chunk_s) * chunkloader->chunks_capacity);
} }
chunkloader->chunks[chunkloader->num_chunks] = entity; chunkloader->chunks[chunkloader->num_chunks] = chunk;
++chunkloader->num_chunks; ++chunkloader->num_chunks;
} }
@ -37,19 +37,16 @@ tc_chunkloader_s * tc_get_corresponding_loader(tc_world_s *world, tc_vec3i_s coo
return NULL; return NULL;
} }
tc_entity_s * tc_get_chunk_of_loader(tc_chunkloader_s *loader, tc_vec3i_s coord) tc_chunk_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_entity_s * tc_get_loaded_chunk(tc_world_s *world, tc_vec3i_s coord) tc_chunk_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;
@ -57,29 +54,27 @@ tc_entity_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_entity_s * tc_load_chunk_of_loader(tc_chunkloader_s *loader, tc_vec3i_s coord) tc_chunk_s * tc_load_chunk_of_loader(tc_chunkloader_s *loader, tc_vec3i_s coord)
{ {
tc_entity_s *entity = tc_get_chunk_of_loader(loader, coord); tc_chunk_s *chunk = tc_get_chunk_of_loader(loader, coord);
if(entity == NULL) if(chunk == 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, entity); tc_add_chunk_to_loader(loader, chunk);
} }
return entity; return chunk;
} }
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_entity_s *entity = tc_get_chunk_of_loader(loader, coord); tc_chunk_s *chunk = 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);
@ -96,7 +91,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_delete_entity(loader->chunks[index]); tc_free_chunk(loader->chunks[index]);
if(loader->num_chunks != 0) if(loader->num_chunks != 0)
{ {
@ -114,9 +109,7 @@ 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_entity_s *entity = loader->chunks[chunk_index]; tc_chunk_s *chunk = 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,10 +10,12 @@ 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;
} }
@ -32,10 +34,6 @@ 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;
@ -43,10 +41,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)
{ {
u32_t num_types = tc_game_state_g.entity_registry.num_types; uint32_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;
u32_t index = 0; uint32_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];
@ -55,43 +53,14 @@ 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 = tc_allocate_entity(); tc_entity_s *entity = type_struct->functions.fn_create();
entity->type = type_struct; entity->type = type_struct;
entity->num_attributes = 0; ++tc_game_state_g.entity_registry.num_entities;
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;
} }
@ -113,30 +82,14 @@ void tc_draw_entity(tc_entity_s *entity)
void tc_delete_entity(tc_entity_s *entity) void tc_delete_entity(tc_entity_s *entity)
{ {
tc_entity_type_s *type = entity->type; entity->type->functions.fn_draw(entity);
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)
{ {
u32_t attribute_index = 0; uint32_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];
@ -222,7 +175,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)
{ {
u32_t attribute_index = 0; uint32_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];
@ -245,9 +198,9 @@ void tc_forget_entity_attribute(tc_entity_s *entity, char *attr_name)
i32_t tc_find_index_of_entity_in_intervals(tc_entity_s *entity) int32_t tc_find_index_of_entity_in_intervals(tc_entity_s *entity)
{ {
u32_t interval_index = 0; uint32_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)
@ -258,7 +211,7 @@ i32_t tc_find_index_of_entity_in_intervals(tc_entity_s *entity)
return -1; return -1;
} }
u32_t tc_allocate_interval() uint32_t tc_allocate_interval()
{ {
if( if(
tc_game_state_g.entity_registry.num_intervals tc_game_state_g.entity_registry.num_intervals
@ -272,7 +225,7 @@ u32_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
); );
} }
u32_t last = tc_game_state_g.entity_registry.num_intervals; uint32_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;
@ -281,7 +234,7 @@ u32_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))
{ {
i32_t interval_index; int32_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();
@ -296,7 +249,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.identifier = event_name; event.event_type = 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);
} }
@ -304,7 +257,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.identifier = event_name; event.event_type = 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);
} }
@ -312,7 +265,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.identifier = event_name; event.event_type = 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);
} }
@ -320,7 +273,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.identifier = event_name; event.event_type = 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);
} }
@ -330,11 +283,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;
u32_t interval_index = 0; uint32_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];
u32_t elapsed_time = current_ms - interval.last_invocation; uint32_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)
@ -352,15 +305,19 @@ 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;
u32_t drawn_entities = 0; uint32_t drawn_entities = 0;
u32_t entity_index = 0; uint32_t entity_index = 0;
while(entity_index < type->num_instances) while(entity_index < registry.num_entities)
{
tc_entity_s *entity = registry.entities[entity_index];
if(entity->type == type)
{ {
tc_entity_s *entity = type->instances[entity_index];
tc_draw_entity(entity); tc_draw_entity(entity);
++drawn_entities; ++drawn_entities;
}
++entity_index; ++entity_index;
} }
} }

View File

@ -5,19 +5,17 @@
#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_entity_pool tc_entity_pool_s; typedef struct tc_interval tc_interval_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
{ {
void (*fn_create) (tc_entity_s *entity); tc_entity_s * (*fn_create) ();
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);
@ -28,14 +26,14 @@ typedef struct
struct tc_entity_event struct tc_entity_event
{ {
char *identifier; char *event_type;
union union
{ {
void *pointer; void *pointer;
char *string; char *string;
i64_t integer; int64_t integer;
f64_t floating; double floating;
} value; } value;
}; };
@ -45,36 +43,30 @@ 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;
u32_t index; uint32_t index;
union union
{ {
void *pointer; void *pointer;
char *string; char *string;
i64_t integer; int64_t integer;
f64_t floating; double 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;
u16_t attributes_capacity; uint16_t attributes_capacity;
u16_t num_attributes; uint16_t num_attributes;
tc_entity_attribute_s *attributes; tc_entity_attribute_s *attributes;
void *specific; void *specific;
@ -83,43 +75,27 @@ struct tc_entity
struct tc_interval struct tc_interval
{ {
u64_t last_invocation; uint64_t last_invocation;
u64_t wanted_delta; uint64_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_pool_entry
{
tc_entity_pool_entry_s *next;
tc_entity_pool_entry_s *previous;
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 struct tc_entity_registry
{ {
u32_t types_capacity; uint32_t types_capacity;
u32_t num_types; uint32_t num_types;
tc_entity_type_s *types; tc_entity_type_s *types;
tc_entity_pool_s *pool; uint32_t entities_capacity;
uint32_t num_entities;
tc_entity_s **entities;
u32_t intervals_capacity; uint32_t intervals_capacity;
u32_t num_intervals; uint32_t num_intervals;
tc_interval_s *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);
@ -133,18 +109,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, i64_t integer); 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, f64_t floating); void tc_set_float_for_entity (tc_entity_s *entity, char *attr_name, double 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);
i64_t tc_get_integer_from_entity (tc_entity_s *entity, char *attr_name); int64_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); double 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, i64_t integer); 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, f64_t floating); void tc_send_float_to_entity (tc_entity_s *entity, char *event_name, double 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,11 +4,18 @@
tc_entity_type_s *tc_player_entity_type_g = NULL; tc_entity_type_s *tc_player_entity_type_g = NULL;
void tc_fn_create_player_entity(tc_entity_s *entity) tc_entity_s * tc_fn_create_player_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)
@ -41,7 +48,6 @@ 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,27 +5,6 @@
// 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);
@ -35,32 +14,58 @@ void tc_reset_entity(tc_entity_s *entity)
} }
tc_entity_s * tc_allocate_entity_from_pool(tc_entity_pool_s *pool)
{
if(pool->first_free == NULL)
{
if(pool->continuation == NULL)
pool->continuation = tc_create_entity_pool(pool->entities_capacity * 2);
return tc_allocate_entity_from_pool(pool->continuation);
}
tc_entity_s *entity = &pool->first_free->entity;
entity->pool_entry = pool->first_free;
pool->first_free = pool->first_free->next;
return entity;
}
tc_entity_s * tc_allocate_entity() tc_entity_s * tc_allocate_entity()
{ {
return tc_allocate_entity_from_pool(tc_game_state_g.entity_registry.pool); 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] == 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;
}
registry.entities =
realloc(registry.entities, sizeof(tc_entity_s *) * registry.entities_capacity * 2);
memset(&registry.entities[registry.entities_capacity], 0x00, sizeof(tc_entity_s *) * registry.entities_capacity);
registry.entities_capacity *= 2;
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];
} }
void tc_deallocate_entity(tc_entity_s *entity) void tc_deallocate_entity(tc_entity_s *entity)
{ {
tc_entity_pool_s *pool = tc_game_state_g.entity_registry.pool; if(entity->specific != NULL) free(entity->specific);
entity->pool_entry->next = pool->first_free; if(entity->attributes != NULL) free(entity->attributes);
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,6 +290,9 @@ 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

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

View File

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

View File

@ -1,42 +0,0 @@
#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

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

View File

@ -1,97 +0,0 @@
#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

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

View File

@ -1,78 +0,0 @@
#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 <utility/math.h> #include <world.h>
typedef struct tc_location typedef struct tc_location
{ {
void *world; tc_world_s *world;
tc_vec3f_s position; tc_vec3f_s position;
tc_vec3f_s rotation; tc_vec3f_s rotation;

View File

@ -84,13 +84,16 @@ void tc_init_world_generators()
void tc_create_chunk_entity(tc_entity_s *entity) tc_entity_s * tc_create_chunk_entity()
{ {
tc_set_string_for_entity(entity, "type", "chunk"); tc_entity_s *chunk = tc_allocate_entity();
tc_set_integer_for_entity(entity, "grid_x", 0); tc_set_string_for_entity(chunk, "type", "chunk");
tc_set_integer_for_entity(entity, "grid_y", 0); tc_set_integer_for_entity(chunk, "grid_x", 0);
tc_set_integer_for_entity(entity, "grid_z", 0); tc_set_integer_for_entity(chunk, "grid_y", 0);
entity->specific = tc_allocate_chunk(); tc_set_integer_for_entity(chunk, "grid_z", 0);
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)
@ -115,11 +118,16 @@ 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()
@ -136,7 +144,6 @@ 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");
@ -173,7 +180,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_entity_s *), loader.chunks_capacity); loader.chunks = calloc(sizeof(tc_chunk_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
{ {
u32_t type_identifier; uint32_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;
u32_t blocks[32][32][32]; uint32_t blocks[32][32][32];
u32_t num_vertices; uint32_t num_vertices;
f32_t *vertex_positions; float *vertex_positions;
f32_t *vertex_uvs; float *vertex_uvs;
u32_t vao; uint32_t vao;
u32_t vertex_data; uint32_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;
u32_t chunks_capacity; uint32_t chunks_capacity;
u32_t num_chunks; uint32_t num_chunks;
tc_entity_s **chunks; tc_chunk_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;
u32_t capacity; uint32_t capacity;
u32_t used_entries; uint32_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;
u32_t num_loaders; uint32_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, i32_t x, i32_t y, i32_t z); // must be integers tc_chunk_s * tc_new_chunk (tc_world_s *world, int32_t x, int32_t y, int32_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,
u8_t x, u8_t y, u8_t z, uint8_t x, uint8_t y, uint8_t z,
tc_block_s block tc_block_s block
); );
u32_t tc_count_chunk_vertices (tc_chunk_s *chunk); uint32_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_entity_s *entity); 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, i32_t x, i32_t y, i32_t z); 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, 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);
bool tc_chunk_is_loaded (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);
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 (u32_t capacity); void tc_init_chunk_pool (uint32_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);