From 14d560dbf559832389c883c8962a805fd93dfc4c Mon Sep 17 00:00:00 2001 From: Eric-Paul Ickhorn Date: Tue, 17 Oct 2023 16:37:19 +0200 Subject: [PATCH] A little more restructuring --- build.bash | 2 +- code/source-c/entity/chunk.c | 11 ++++++---- code/source-c/{ => entity}/entity.c | 0 code/source-c/{ => entity}/entity.h | 4 ++-- code/source-c/{ => entity}/entity_pool.c | 0 code/source-c/entity/registration.h | 2 +- code/source-c/initialization.c | 6 +++--- code/source-c/physics/physics.c | 19 ++++++++++------- code/source-c/physics/simulation.h | 2 +- code/source-c/state.h | 4 ++-- code/source-c/{ => utility}/noise.c | 2 +- code/source-c/{ => world}/chunk.c | 2 +- code/source-c/{ => world}/chunk.h | 2 +- .../{meshize.c => world/chunk_meshing.c} | 0 code/source-c/{ => world}/chunk_pool.c | 0 code/source-c/{ => world}/chunk_pool.h | 2 +- code/source-c/{ => world}/chunkloader.c | 21 +++++++++++-------- .../generator}/default_terrain_generator.c | 2 +- code/source-c/{ => world}/world.c | 6 +++++- code/source-c/{ => world}/world.h | 9 ++++---- code/source-c/{ => world}/world_physics.c | 17 +++++++++++---- 21 files changed, 69 insertions(+), 44 deletions(-) rename code/source-c/{ => entity}/entity.c (100%) rename code/source-c/{ => entity}/entity.h (98%) rename code/source-c/{ => entity}/entity_pool.c (100%) rename code/source-c/{ => utility}/noise.c (97%) rename code/source-c/{ => world}/chunk.c (98%) rename code/source-c/{ => world}/chunk.h (99%) rename code/source-c/{meshize.c => world/chunk_meshing.c} (100%) rename code/source-c/{ => world}/chunk_pool.c (100%) rename code/source-c/{ => world}/chunk_pool.h (97%) rename code/source-c/{ => world}/chunkloader.c (92%) rename code/source-c/{ => world/generator}/default_terrain_generator.c (98%) rename code/source-c/{ => world}/world.c (92%) rename code/source-c/{ => world}/world.h (93%) rename code/source-c/{ => world}/world_physics.c (81%) diff --git a/build.bash b/build.bash index 6d5ec99..03cee03 100644 --- a/build.bash +++ b/build.bash @@ -7,6 +7,6 @@ gcc -g3 -o dependencies/build/glad.o \ -I dependencies/include/ -Wall gcc -g3 -o techneck.elf \ - code/source-c/*.c code/source-c/entity/*.c code/source-c/utility/*.c code/source-c/physics/*.c dependencies/build/glad.o \ + code/source-c/*.c code/source-c/entity/*.c code/source-c/utility/*.c code/source-c/world/*.c code/source-c/world/generator/*.c code/source-c/physics/*.c dependencies/build/glad.o \ -I dependencies/include -I code/source-c/ -lGL -lSDL2 -lm -Wall diff --git a/code/source-c/entity/chunk.c b/code/source-c/entity/chunk.c index 037aba5..bb5682a 100644 --- a/code/source-c/entity/chunk.c +++ b/code/source-c/entity/chunk.c @@ -1,7 +1,7 @@ #include -#include -#include -#include +#include +#include +#include #include #include @@ -31,7 +31,10 @@ void tc_create_chunk_entity(tc_entity_s *entity, void *userdata) void tc_delete_chunk_entity(tc_entity_s *entity) { - tc_chunk_s *chunk = entity->specific; + tc_chunk_s *chunk = entity->specific; + tc_world_s *world = chunk->location.world; + tc_run_hooklist(&world->on_chunk_delete, entity); + if(chunk->vao != 0) glDeleteVertexArrays(1, &chunk->vao); if(chunk->vbo != 0) glDeleteBuffers(1, &chunk->vbo); diff --git a/code/source-c/entity.c b/code/source-c/entity/entity.c similarity index 100% rename from code/source-c/entity.c rename to code/source-c/entity/entity.c diff --git a/code/source-c/entity.h b/code/source-c/entity/entity.h similarity index 98% rename from code/source-c/entity.h rename to code/source-c/entity/entity.h index 0b239e5..093a6a3 100644 --- a/code/source-c/entity.h +++ b/code/source-c/entity/entity.h @@ -2,8 +2,8 @@ #ifndef TC_ENTITY_H #define TC_ENTITY_H -#include "utility/location.h" -#include "utility/utility.h" +#include +#include typedef struct tc_interval tc_interval_s; typedef struct tc_entity_event tc_entity_event_s; diff --git a/code/source-c/entity_pool.c b/code/source-c/entity/entity_pool.c similarity index 100% rename from code/source-c/entity_pool.c rename to code/source-c/entity/entity_pool.c diff --git a/code/source-c/entity/registration.h b/code/source-c/entity/registration.h index f740089..abb3abe 100644 --- a/code/source-c/entity/registration.h +++ b/code/source-c/entity/registration.h @@ -2,7 +2,7 @@ #ifndef TC_ENTITY_REGISTRATION_H #define TC_ENTITY_REGISTRATION_H -#include "../entity.h" +#include void tc_register_chunk_entity (); void tc_register_player_entity (); diff --git a/code/source-c/initialization.c b/code/source-c/initialization.c index f3624c1..84cbb0e 100644 --- a/code/source-c/initialization.c +++ b/code/source-c/initialization.c @@ -1,6 +1,6 @@ -#include "state.h" -#include "world.h" -#include "entity/registration.h" +#include +#include +#include #include diff --git a/code/source-c/physics/physics.c b/code/source-c/physics/physics.c index 581770e..e430ad5 100644 --- a/code/source-c/physics/physics.c +++ b/code/source-c/physics/physics.c @@ -21,6 +21,8 @@ void tc_add_physics_object(tc_physics_simulation_s *simulation, tc_physics_entit void tc_add_physics_object_to(tc_physics_entity_s *container, tc_physics_entity_s *object) { + if(container != NULL) object->super = container; + if((container->value.group.num_entities+1) >= container->value.group.entities_capacity @@ -76,7 +78,7 @@ i32_t tc_physics_find_entity_in_group(tc_physics_entity_s *group, tc_physics_ent i32_t index = 0; while(index < group->value.group.num_entities) { - if(entity->super->value.group.entities[group->value.group.num_entities] + if(group->value.group.entities[group->value.group.num_entities] == entity ) return index; @@ -99,15 +101,18 @@ void tc_remove_physics_entity(tc_physics_entity_s *entity) { if(entity->super == NULL) return; - tc_free_physics_entity(entity); - // Fill up the gap created inside the entities-array. - i32_t index = 0; - if((index = tc_physics_find_entity_in_group(entity->super, entity)) == -1) return; + tc_physics_entity_s *super = entity->super; - entity->super->value.group.entities[index] = - entity->super->value.group.entities[entity->super->value.group.num_entities-1]; + i32_t index = 0; + if((index = tc_physics_find_entity_in_group(super, entity)) == -1) return; + + super->value.group.entities[index] = + super->value.group.entities[super->value.group.num_entities-1]; + --super->value.group.num_entities; + + tc_free_physics_entity(entity); } diff --git a/code/source-c/physics/simulation.h b/code/source-c/physics/simulation.h index 76bd4c3..aa8aa6f 100644 --- a/code/source-c/physics/simulation.h +++ b/code/source-c/physics/simulation.h @@ -6,7 +6,7 @@ #include #include -#include +#include typedef struct tc_physics_entity tc_physics_entity_s; typedef struct tc_entity_physics_attributes tc_entity_physics_attributes_s; diff --git a/code/source-c/state.h b/code/source-c/state.h index 3e1d299..ec86260 100644 --- a/code/source-c/state.h +++ b/code/source-c/state.h @@ -8,9 +8,9 @@ #include #include -#include +#include #include -#include +#include #include typedef struct tc_camera diff --git a/code/source-c/noise.c b/code/source-c/utility/noise.c similarity index 97% rename from code/source-c/noise.c rename to code/source-c/utility/noise.c index 25e5b96..73138f7 100644 --- a/code/source-c/noise.c +++ b/code/source-c/utility/noise.c @@ -1,4 +1,4 @@ -#include "world.h" +#include int tc_perlin_permutation[] = { 151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233, 7, 225, diff --git a/code/source-c/chunk.c b/code/source-c/world/chunk.c similarity index 98% rename from code/source-c/chunk.c rename to code/source-c/world/chunk.c index b6b7a2b..f22358e 100644 --- a/code/source-c/chunk.c +++ b/code/source-c/world/chunk.c @@ -1,4 +1,4 @@ -#include +#include #include #include diff --git a/code/source-c/chunk.h b/code/source-c/world/chunk.h similarity index 99% rename from code/source-c/chunk.h rename to code/source-c/world/chunk.h index 6dc117f..d9d30c9 100644 --- a/code/source-c/chunk.h +++ b/code/source-c/world/chunk.h @@ -3,7 +3,7 @@ #define TC_CHUNK_H #include -#include +#include typedef struct tc_chunk tc_chunk_s; typedef struct tc_chunkloader tc_chunkloader_s; diff --git a/code/source-c/meshize.c b/code/source-c/world/chunk_meshing.c similarity index 100% rename from code/source-c/meshize.c rename to code/source-c/world/chunk_meshing.c diff --git a/code/source-c/chunk_pool.c b/code/source-c/world/chunk_pool.c similarity index 100% rename from code/source-c/chunk_pool.c rename to code/source-c/world/chunk_pool.c diff --git a/code/source-c/chunk_pool.h b/code/source-c/world/chunk_pool.h similarity index 97% rename from code/source-c/chunk_pool.h rename to code/source-c/world/chunk_pool.h index 7d4cd40..d6d0744 100644 --- a/code/source-c/chunk_pool.h +++ b/code/source-c/world/chunk_pool.h @@ -3,7 +3,7 @@ #define TC_CHUNK_POOL_H #include -#include +#include typedef struct tc_chunk_pool tc_chunk_pool_s; typedef struct tc_chunk_pool_entry tc_chunk_pool_entry_s; diff --git a/code/source-c/chunkloader.c b/code/source-c/world/chunkloader.c similarity index 92% rename from code/source-c/chunkloader.c rename to code/source-c/world/chunkloader.c index 0d5d030..63b276e 100644 --- a/code/source-c/chunkloader.c +++ b/code/source-c/world/chunkloader.c @@ -1,5 +1,6 @@ -#include "world.h" -#include "entity.h" +#include +#include +#include #include #include @@ -95,7 +96,6 @@ void tc_chunklist_add_item(tc_chunklist_s *list, tc_entity_s *chunk) new_entry.entity = chunk; new_entry.next = list->first; (*list->first_free) = new_entry; - list->first = list->first_free; // Link everything together @@ -104,6 +104,7 @@ void tc_chunklist_add_item(tc_chunklist_s *list, tc_entity_s *chunk) list->first_free->previous = NULL; // Finally assign it + list->first = list->first_free; list->first_free = tc_chunklist_find_first_free(list); } @@ -152,13 +153,13 @@ void tc_delete_chunkloader(tc_chunkloader_s loader) bool_t tc_coord_is_within_loaders_range(tc_chunkloader_s *loader, tc_vec3i_s coord) { - if(coord.x < (loader->center.grid_coords.x - loader->extent.x / 2)) return FALSE; - if(coord.y < (loader->center.grid_coords.y - loader->extent.y / 2)) return FALSE; - if(coord.z < (loader->center.grid_coords.z - loader->extent.z / 2)) return FALSE; + if(coord.x < ((loader->center.grid_coords.x - loader->extent.x / 2)-1)) return FALSE; + if(coord.y < ((loader->center.grid_coords.y - loader->extent.y / 2)-1)) return FALSE; + if(coord.z < ((loader->center.grid_coords.z - loader->extent.z / 2)-1)) return FALSE; - if(coord.x > (loader->center.grid_coords.x + loader->extent.x / 2)) return FALSE; - if(coord.y > (loader->center.grid_coords.y + loader->extent.y / 2)) return FALSE; - if(coord.z > (loader->center.grid_coords.z + loader->extent.z / 2)) return FALSE; + if(coord.x > ((loader->center.grid_coords.x + loader->extent.x / 2)+1)) return FALSE; + if(coord.y > ((loader->center.grid_coords.y + loader->extent.y / 2)+1)) return FALSE; + if(coord.z > ((loader->center.grid_coords.z + loader->extent.z / 2)+1)) return FALSE; return TRUE; } @@ -181,7 +182,9 @@ void tc_unload_chunk_at(tc_chunkloader_s *loader, tc_chunk_location_s location) if(entity == NULL) return; tc_chunk_s *chunk = entity->specific; + tc_world_s *world = location.world; + tc_run_hooklist(&world->on_chunk_unload, entity); --chunk->refcounter; if(chunk->refcounter < 1) diff --git a/code/source-c/default_terrain_generator.c b/code/source-c/world/generator/default_terrain_generator.c similarity index 98% rename from code/source-c/default_terrain_generator.c rename to code/source-c/world/generator/default_terrain_generator.c index 9ed67f4..18596fc 100644 --- a/code/source-c/default_terrain_generator.c +++ b/code/source-c/world/generator/default_terrain_generator.c @@ -1,4 +1,4 @@ -#include "world.h" +#include bool_t tc_generate_default_terrain_chunk(tc_worldgen_s *gen, tc_chunk_s *chunk) { diff --git a/code/source-c/world.c b/code/source-c/world/world.c similarity index 92% rename from code/source-c/world.c rename to code/source-c/world/world.c index acb98b7..95879c5 100644 --- a/code/source-c/world.c +++ b/code/source-c/world/world.c @@ -1,4 +1,4 @@ -#include +#include #include #include @@ -48,6 +48,9 @@ tc_chunkloader_s tc_create_spawn_loader(tc_world_s *world) tc_chunkloader_s loader = tc_create_chunkloader(loader_location); loader.is_rendered = TRUE; + loader.extent.x = 9.0f; + loader.extent.z = 9.0f; + loader.extent.y = 3.0f; return loader; } @@ -59,6 +62,7 @@ tc_world_s * tc_new_world(tc_worldgen_s *generator) world->before_chunk_generate = tc_new_hooklist(16); world->after_chunk_generate = tc_new_hooklist(16); world->on_chunk_delete = tc_new_hooklist(16); + world->on_chunk_unload = tc_new_hooklist(16); world->on_chunk_update = tc_new_hooklist(16); world->worldgen = &tc_default_terrain_generator_g; world->num_loaders = 1; diff --git a/code/source-c/world.h b/code/source-c/world/world.h similarity index 93% rename from code/source-c/world.h rename to code/source-c/world/world.h index c1eede7..a9bc5a8 100644 --- a/code/source-c/world.h +++ b/code/source-c/world/world.h @@ -8,9 +8,9 @@ #include #include #include -#include -#include -#include +#include +#include +#include #define UPDATE_DISTANCE 2 @@ -52,8 +52,9 @@ struct tc_world tc_hooklist_s on_chunk_create; tc_hooklist_s before_chunk_generate; tc_hooklist_s after_chunk_generate; - tc_hooklist_s on_chunk_delete; tc_hooklist_s on_chunk_update; + tc_hooklist_s on_chunk_unload; + tc_hooklist_s on_chunk_delete; }; extern tc_worldgen_s tc_default_terrain_generator_g; diff --git a/code/source-c/world_physics.c b/code/source-c/world/world_physics.c similarity index 81% rename from code/source-c/world_physics.c rename to code/source-c/world/world_physics.c index df10a6d..653eb1a 100644 --- a/code/source-c/world_physics.c +++ b/code/source-c/world/world_physics.c @@ -1,10 +1,11 @@ -#include +#include + #include // For NULL #include void tc_initialize_chunk_physics(tc_entity_s *chunk_entity, void *userdata) { - /* + tc_chunk_s *chunk = chunk_entity->specific; tc_world_s *world = chunk->location.world; tc_physics_simulation_s *simulation = world->physics; @@ -40,13 +41,21 @@ void tc_initialize_chunk_physics(tc_entity_s *chunk_entity, void *userdata) tc_add_physics_object(world->physics, chunk_body); tc_set_pointer_for_entity(chunk_entity, "physics_body", chunk_body); - */ +} + +void tc_free_chunk_physics(tc_entity_s *chunk_entity, void *userdata) +{ + tc_physics_entity_s *body = + tc_get_pointer_from_entity(chunk_entity, "physics_body"); + + tc_remove_physics_entity(body); } void tc_create_world_physics(tc_world_s *world, void *userdata) { world->physics = tc_new_physics_simulation(); tc_add_to_hooklist(&world->on_chunk_create, (tc_fn_hook) tc_initialize_chunk_physics, NULL); - tc_add_to_hooklist(&world->on_chunk_update, (tc_fn_hook) tc_initialize_chunk_physics, NULL); + // tc_add_to_hooklist(&world->on_chunk_update, (tc_fn_hook) tc_initialize_chunk_physics, NULL); + tc_add_to_hooklist(&world->on_chunk_delete, (tc_fn_hook) tc_free_chunk_physics, NULL); }