diff --git a/build.bash b/build.bash index aa06dde..6d5ec99 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 dependencies/build/glad.o \ + code/source-c/*.c code/source-c/entity/*.c code/source-c/utility/*.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/chunk_loader.c b/code/source-c/chunk_loader.c index 536591d..152695b 100644 --- a/code/source-c/chunk_loader.c +++ b/code/source-c/chunk_loader.c @@ -63,12 +63,13 @@ tc_entity_s * tc_load_chunk_of_loader(tc_chunkloader_s *loader, tc_vec3i_s coord if(entity == NULL) { - tc_entity_s *entity = tc_create_entity("chunk"); // TODO: FURTHER RESTRUCTUING FOR ENTITIES + tc_entity_s *entity = tc_create_entity("chunk", loader->world); tc_chunk_s *chunk = entity->specific; chunk = entity->specific; chunk->position = coord; loader->world->worldgen->fn_generate_chunk(loader->world->worldgen, chunk); + tc_run_hooklist(&loader->world->after_chunk_generate, chunk); tc_upload_chunk(chunk); tc_add_chunk_to_loader(loader, entity); @@ -82,7 +83,7 @@ void tc_reload_chunk_of_loader(tc_chunkloader_s *loader, tc_vec3i_s coord) tc_chunk_s *chunk = entity->specific; if(chunk == NULL) return; - tc_meshize_chunk(chunk); + tc_meshize_chunk(chunk, NULL); tc_upload_chunk(chunk); } diff --git a/code/source-c/entity.c b/code/source-c/entity.c index a9908ba..c021b49 100644 --- a/code/source-c/entity.c +++ b/code/source-c/entity.c @@ -77,7 +77,7 @@ void tc_add_entity_instance_to_type(tc_entity_type_s *type, tc_entity_s *entity) ++type->num_instances; } -tc_entity_s * tc_create_entity(char *type) +tc_entity_s * tc_create_entity(char *type, void *userdata) { tc_entity_type_s *type_struct = tc_get_entity_type_with_name(type); if(type_struct == NULL) return NULL; @@ -89,7 +89,7 @@ tc_entity_s * tc_create_entity(char *type) entity->attributes = calloc(sizeof(tc_entity_attribute_s), entity->attributes_capacity); - type_struct->functions.fn_create(entity); + type_struct->functions.fn_create(entity, userdata); tc_add_entity_instance_to_type(type_struct, entity); diff --git a/code/source-c/entity.h b/code/source-c/entity.h index 246a3c9..3bed5a6 100644 --- a/code/source-c/entity.h +++ b/code/source-c/entity.h @@ -17,7 +17,7 @@ typedef struct tc_entity_registry tc_entity_registry_s; typedef struct { - void (*fn_create) (tc_entity_s *entity); + void (*fn_create) (tc_entity_s *entity, void *userdata); 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); @@ -125,7 +125,7 @@ void tc_register_entity_type (tc_entity_type_ tc_entity_type_s * tc_get_entity_type_with_name (char *name); -tc_entity_s * tc_create_entity (char *type); +tc_entity_s * tc_create_entity (char *type, void *userdata); void tc_spawn_entity (tc_entity_s *entity, tc_location_s location); void tc_teleport_entity (tc_entity_s *entity, tc_location_s location); void tc_draw_entity (tc_entity_s *entity); diff --git a/code/source-c/entity/player.c b/code/source-c/entity/player.c index 9161086..6d46ab5 100644 --- a/code/source-c/entity/player.c +++ b/code/source-c/entity/player.c @@ -1,4 +1,5 @@ #include "registration.h" +#include #include @@ -20,7 +21,9 @@ void tc_fn_spawn_player_entity(tc_entity_s *entity, tc_location_s location) void tc_fn_teleport_player_entity(tc_entity_s *entity, tc_location_s location) { - entity->location = location; + tc_set_float_for_entity(tc_game_state_g.viewer, "pos_x", location.position.x); + tc_set_float_for_entity(tc_game_state_g.viewer, "pos_y", location.position.y); + tc_set_float_for_entity(tc_game_state_g.viewer, "pos_z", location.position.z); } void tc_fn_delete_player_entity(tc_entity_s *entity) diff --git a/code/source-c/hook.c b/code/source-c/hook.c new file mode 100644 index 0000000..c4e690d --- /dev/null +++ b/code/source-c/hook.c @@ -0,0 +1,35 @@ +#include + +#include +#include + +tc_hooklist_s tc_new_hooklist(u32_t capacity) +{ + tc_hooklist_s hooklist; + hooklist.capacity = capacity; + hooklist.num_hooks = 0; + hooklist.hooks = malloc(sizeof(tc_fn_hook)); + + return hooklist; +} + +void tc_add_to_hooklist(tc_hooklist_s *list, tc_fn_hook function, void *userdata) +{ + if(list->num_hooks >= list->capacity) + { + puts("hook.c: Hook-Capacity exceeded!\n"); + return; + } + list->hooks[list->num_hooks].function = function; + list->hooks[list->num_hooks].userdata = userdata; + ++list->num_hooks; +} + +void tc_run_hooklist(tc_hooklist_s *list, void *subject) +{ + for(u32_t index = 0; index < list->num_hooks; ++index) + { + list->hooks[index].function(subject, list->hooks[index].userdata); + } +} + diff --git a/code/source-c/hook.h b/code/source-c/hook.h new file mode 100644 index 0000000..a9a697d --- /dev/null +++ b/code/source-c/hook.h @@ -0,0 +1,29 @@ + +#ifndef TC_HOOK_H +#define TC_HOOK_H + +#include + +typedef bool_t (*tc_fn_hook) (void *subject, void *userdata); + +typedef struct tc_hook +{ + tc_fn_hook function; + void *userdata; + +} tc_hook_s; + +typedef struct tc_hooklist +{ + u32_t capacity; + u32_t num_hooks; + tc_hook_s *hooks; + +} tc_hooklist_s; + +tc_hooklist_s tc_new_hooklist (u32_t capacity); +void tc_add_to_hooklist (tc_hooklist_s *list, tc_fn_hook function, void *subject); +void tc_run_hooklist (tc_hooklist_s *list, void *subject); + +#endif // TC_HOOK_h + diff --git a/code/source-c/initialization.c b/code/source-c/initialization.c index 3c8863a..f2c45d0 100644 --- a/code/source-c/initialization.c +++ b/code/source-c/initialization.c @@ -69,12 +69,23 @@ void tc_init() tc_create_blocks(); tc_game_state_g.entity_registry = tc_init_entity_registry(); tc_register_entities(); - tc_game_state_g.player = tc_create_entity("player"); - tc_game_state_g.viewer = tc_game_state_g.player; + + tc_game_state_g.on_world_create = tc_new_hooklist(16); + tc_add_to_hooklist(&tc_game_state_g.on_world_create, tc_create_world_physics, NULL); + tc_init_worlds(); tc_game_state_g.main_world = tc_new_world(&tc_default_terrain_generator_g); - tc_game_state_g.tps = 120; + tc_game_state_g.player = tc_create_entity("player", NULL); + tc_game_state_g.viewer = tc_game_state_g.player; + tc_location_s spawn_location; + spawn_location.world = tc_game_state_g.main_world; + spawn_location.position.x = 0.0f; + spawn_location.position.y = 36.0f; + spawn_location.position.z = 0.0f; + tc_teleport_entity(tc_game_state_g.player, spawn_location); + + tc_game_state_g.tps = 120; puts("Finished initializing!"); } diff --git a/code/source-c/meshize.c b/code/source-c/meshize.c index bd79ae2..607ed18 100644 --- a/code/source-c/meshize.c +++ b/code/source-c/meshize.c @@ -177,7 +177,7 @@ uint32_t tc_meshize_block(tc_chunk_s *chunk, return num_vertices; } -void tc_meshize_chunk(tc_chunk_s *chunk) +void tc_meshize_chunk(tc_chunk_s *chunk, void *userdata) { chunk->num_vertices = tc_count_chunk_vertices(chunk); chunk->vertex_positions = calloc(sizeof(float), 3 * chunk->num_vertices); diff --git a/code/source-c/physics/obb.c b/code/source-c/physics/obb.c index 4b3e9c8..37d6584 100644 --- a/code/source-c/physics/obb.c +++ b/code/source-c/physics/obb.c @@ -1,3 +1,4 @@ #include "primitives.h" + diff --git a/code/source-c/physics/physics.c b/code/source-c/physics/physics.c index fd605c5..764e21c 100644 --- a/code/source-c/physics/physics.c +++ b/code/source-c/physics/physics.c @@ -2,14 +2,14 @@ tc_physics_simulation_s tc_new_physics_simulation() { + tc_physics_simulation_s simulation; - - + return simulation; } -void tc_add_physics_object(tc_physics_simulation_s *simulation, tc_physics_entity object) +void tc_add_physics_object(tc_physics_simulation_s *simulation, tc_physics_entity_s object) { @@ -25,14 +25,14 @@ void tc_add_physics_object_to(tc_physics_entity_s *container, tc_physics_entity_ -tc_phyiscs_entity_s tc_new_physics_entity() +tc_physics_entity_s tc_new_physics_entity() { } -void tc_tick_physics(tc_physics_simulation_s *simulation, float delta) +void tc_tick_physics(tc_physics_simulation_s *simulation, f32_t delta) { diff --git a/code/source-c/physics/primitives.h b/code/source-c/physics/primitives.h index b6364d4..62713b5 100644 --- a/code/source-c/physics/primitives.h +++ b/code/source-c/physics/primitives.h @@ -5,7 +5,7 @@ #include #include -#include "../utility.h" +#include typedef struct tc_physics_mesh tc_physics_mesh_s; @@ -29,40 +29,40 @@ typedef enum struct tc_aabb { - tc_vec3_s start; - tc_vec3_s size; + tc_vec3f_s start; + tc_vec3f_s size; }; struct tc_obb { - tc_vec3_s start; - tc_vec3_s size; - tc_vec3_s rotation; + tc_vec3f_s start; + tc_vec3f_s size; + tc_vec3f_s rotation; }; struct tc_plane { - tc_vec3_s center; - tc_vec3_s rotation; + tc_vec3f_s center; + tc_vec3f_s rotation; tc_vec2i_s size; }; struct tc_ellipsoid { - tc_vec3_s center; - tc_vec3_s extent; + tc_vec3f_s center; + tc_vec3f_s extent; }; struct tc_ray { - tc_vec3_s start; - tc_vec3_s rotation; + tc_vec3f_s start; + tc_vec3f_s rotation; }; struct tc_line { - tc_vec3_s start; - tc_vec3_s end; + tc_vec3f_s start; + tc_vec3f_s end; }; @@ -72,14 +72,13 @@ typedef union tc_primitive 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 +typedef struct tc_primitive_container { tc_primitive_type_e type; tc_primitive_u data; @@ -88,8 +87,8 @@ typedef struct tc_primitive struct tc_physics_mesh { - uint32_t primitives_capacity; - uint32_t num_primitives; + u32_t primitives_capacity; + u32_t num_primitives; tc_primitive_s *primitives; }; diff --git a/code/source-c/physics/raycast.c b/code/source-c/physics/raycast.c index ce43caf..c66d98a 100644 --- a/code/source-c/physics/raycast.c +++ b/code/source-c/physics/raycast.c @@ -1,4 +1,4 @@ -#include "primitives.h" +#include tc_primitive_s * tc_cast_line(tc_physics_simulation_s *simulation, tc_line_s line) { diff --git a/code/source-c/physics/simulation.h b/code/source-c/physics/simulation.h index 12a27ee..2453140 100644 --- a/code/source-c/physics/simulation.h +++ b/code/source-c/physics/simulation.h @@ -4,21 +4,22 @@ #include +#include #include -#include <../entity.h> +#include typedef struct tc_physics_entity tc_physics_entity_s; -typedef struct tc_entity_physics_attributes tc_entity_physics_attributes; +typedef struct tc_entity_physics_attributes tc_entity_physics_attributes_s; struct tc_entity_physics_attributes { - bool has_gravity; + bool_t has_gravity; - bool raw_movement; - bool raw_rotation; + bool_t raw_movement; + bool_t raw_rotation; - tc_vec3_s acceleration; - tc_vec3_s spin; + tc_vec3f_s acceleration; + tc_vec3f_s spin; }; typedef enum @@ -30,23 +31,24 @@ typedef enum struct tc_physics_entity { - tc_physics_entity *super; + tc_physics_entity_s *super; // relative_position: The position of this physics entity relative to its super-entity - tc_vec3_s relative_position; + tc_vec3f_s relative_position; - tc_physics_entity_e type; + tc_physics_entity_type_e type; union specific { struct entity { + char *name; tc_entity_s *entity; } entity; struct subentity_group { - uint32_t num_entities; + u32_t num_entities; tc_physics_entity_s *entities; } group; @@ -56,23 +58,19 @@ struct tc_physics_entity 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 (tc_physics_simulation_s *simulation, tc_physics_entity_s 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 (); +tc_physics_entity_s tc_new_physics_entity (); -void tc_tick_physics (tc_physics_simulation_s *simulation, float delta); +void tc_tick_physics (tc_physics_simulation_s *simulation, f32_t delta); - - -tc_primitive_s tc_cast_line (tc_physics_simulation_s *simulation, tc_line_s line); +tc_primitive_s * tc_cast_line (tc_physics_simulation_s *simulation, tc_line_s line); #endif diff --git a/code/source-c/state.h b/code/source-c/state.h index 923128e..3e1d299 100644 --- a/code/source-c/state.h +++ b/code/source-c/state.h @@ -41,6 +41,8 @@ typedef struct tc_entity_s *viewer; tc_entity_s *player; + tc_hooklist_s on_world_create; + uint16_t fps; uint16_t tps; diff --git a/code/source-c/world.c b/code/source-c/world.c index 8347cf1..c9990da 100644 --- a/code/source-c/world.c +++ b/code/source-c/world.c @@ -8,11 +8,10 @@ tc_entity_type_s *tc_chunk_entity_type_g = NULL; void tc_upload_chunk(tc_chunk_s *chunk) { - // If the chunk has no mesh yet + // If the chunk has no mesh, it's probably only air. if(chunk->num_vertices == 0) { - // Create one - tc_meshize_chunk(chunk); + return; } if(chunk->vao != 0) glDeleteVertexArrays(1, &chunk->vao); @@ -83,14 +82,19 @@ void tc_init_world_generators() } - -void tc_create_chunk_entity(tc_entity_s *entity) +// The userdata should be the corresponding world +void tc_create_chunk_entity(tc_entity_s *entity, void *userdata) { + tc_chunk_s *chunk = tc_allocate_chunk(); + tc_world_s *world = userdata; + 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(); + entity->specific = chunk; + + tc_run_hooklist(&world->on_chunk_create, chunk); } void tc_spawn_chunk_entity(tc_entity_s *entity, tc_location_s location) @@ -107,9 +111,6 @@ void tc_teleport_chunk_entity(tc_entity_s *entity, tc_location_s location) chunk->position.x = location.position.x / 32; chunk->position.y = location.position.y / 32; chunk->position.z = location.position.z / 32; - - - } void tc_delete_chunk_entity(tc_entity_s *entity) @@ -197,6 +198,15 @@ tc_world_s * tc_new_world(tc_worldgen_s *generator) world->loaders = malloc(sizeof(tc_chunkloader_s) * world->num_loaders); world->loaders[0] = tc_create_spawn_loader(world); world->spawn_loader = &world->loaders[0]; + world->on_chunk_create = tc_new_hooklist(16); + world->on_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_update = tc_new_hooklist(16); + + tc_add_to_hooklist(&world->after_chunk_generate, tc_meshize_chunk, NULL); + + tc_run_hooklist(&tc_game_state_g.on_world_create, world); return world; } diff --git a/code/source-c/world.h b/code/source-c/world.h index 77e2495..89ce97d 100644 --- a/code/source-c/world.h +++ b/code/source-c/world.h @@ -5,6 +5,8 @@ #include #include +#include +#include #include #include @@ -29,9 +31,9 @@ struct tc_worldgen struct tc_block { - u32_t type_identifier; - tc_vec3f_s position; - tc_vec3f_s rotation; + u32_t type_identifier; + tc_vec3f_s position; + tc_vec3f_s rotation; }; @@ -41,14 +43,14 @@ struct tc_chunk tc_chunk_pool_entry_s *pool_entry; tc_vec3i_s position; - u32_t blocks[32][32][32]; + u32_t blocks[32][32][32]; - u32_t num_vertices; + u32_t num_vertices; f32_t *vertex_positions; f32_t *vertex_uvs; - u32_t vao; - u32_t vertex_data; + u32_t vao; + u32_t vertex_data; }; struct tc_chunk_pool_entry @@ -64,8 +66,8 @@ typedef struct tc_chunkloader tc_vec3i_s center; tc_vec3i_s extent; - u32_t chunks_capacity; - u32_t num_chunks; + u32_t chunks_capacity; + u32_t num_chunks; tc_entity_s **chunks; tc_world_s *world; @@ -87,16 +89,20 @@ struct tc_chunk_pool struct tc_world { - // tc_chunk_s *center_chunk; - tc_chunk_pool_s *pool; tc_worldgen_s *worldgen; - u32_t num_loaders; + u32_t num_loaders; tc_chunkloader_s *loaders; - // chunk_loading_center: The center of loading ON THE CHUNK GRID COORDINATES tc_chunkloader_s *spawn_loader; + tc_physics_simulation_s physics; + + tc_hooklist_s on_chunk_create; + tc_hooklist_s on_chunk_generate; + tc_hooklist_s after_chunk_generate; + tc_hooklist_s on_chunk_delete; + tc_hooklist_s on_chunk_update; }; extern tc_worldgen_s tc_default_terrain_generator_g; @@ -114,7 +120,7 @@ void tc_set_block_in_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 *userdata); void tc_upload_chunk (tc_chunk_s *chunk); bool tc_generate_default_terrain_chunk (tc_worldgen_s *gen, tc_chunk_s *chunk); @@ -136,5 +142,7 @@ void tc_init_chunk_pool (u32_t capacity); tc_chunk_s * tc_allocate_chunk (); void tc_free_chunk (tc_chunk_s *chunk); +void tc_create_world_physics (tc_world_s *world, void *userdata); + #endif diff --git a/code/source-c/world_physics.c b/code/source-c/world_physics.c new file mode 100644 index 0000000..2650e06 --- /dev/null +++ b/code/source-c/world_physics.c @@ -0,0 +1,15 @@ +#include +#include // For NULL + +void tc_initialize_chunk_physics(tc_chunk_s *chunk, void *userdata) +{ + +} + +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_initialize_chunk_physics, NULL); + tc_add_to_hooklist(&world->on_chunk_update, &tc_initialize_chunk_physics, NULL); +} +