Added hooks for physics-chunk creation

This commit is contained in:
Eric-Paul Ickhorn 2023-10-15 14:41:53 +02:00
parent 1602130459
commit 29ef3b0b27
18 changed files with 190 additions and 78 deletions

View File

@ -7,6 +7,6 @@ gcc -g3 -o dependencies/build/glad.o \
-I dependencies/include/ -Wall -I dependencies/include/ -Wall
gcc -g3 -o techneck.elf \ 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 -I dependencies/include -I code/source-c/ -lGL -lSDL2 -lm -Wall

View File

@ -63,12 +63,13 @@ tc_entity_s * tc_load_chunk_of_loader(tc_chunkloader_s *loader, tc_vec3i_s coord
if(entity == NULL) if(entity == NULL)
{ {
tc_entity_s *entity = tc_create_entity("chunk"); // TODO: FURTHER RESTRUCTUING FOR ENTITIES tc_entity_s *entity = tc_create_entity("chunk", loader->world);
tc_chunk_s *chunk = entity->specific; 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_run_hooklist(&loader->world->after_chunk_generate, chunk);
tc_upload_chunk(chunk); tc_upload_chunk(chunk);
tc_add_chunk_to_loader(loader, entity); 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; tc_chunk_s *chunk = entity->specific;
if(chunk == NULL) return; if(chunk == NULL) return;
tc_meshize_chunk(chunk); tc_meshize_chunk(chunk, NULL);
tc_upload_chunk(chunk); tc_upload_chunk(chunk);
} }

View File

@ -77,7 +77,7 @@ void tc_add_entity_instance_to_type(tc_entity_type_s *type, tc_entity_s *entity)
++type->num_instances; ++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); 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;
@ -89,7 +89,7 @@ tc_entity_s * tc_create_entity(char *type)
entity->attributes = entity->attributes =
calloc(sizeof(tc_entity_attribute_s), entity->attributes_capacity); 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); tc_add_entity_instance_to_type(type_struct, entity);

View File

@ -17,7 +17,7 @@ typedef struct tc_entity_registry tc_entity_registry_s;
typedef struct 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_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);
@ -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_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_spawn_entity (tc_entity_s *entity, tc_location_s location);
void tc_teleport_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); void tc_draw_entity (tc_entity_s *entity);

View File

@ -1,4 +1,5 @@
#include "registration.h" #include "registration.h"
#include <state.h>
#include <stdlib.h> #include <stdlib.h>
@ -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) 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) void tc_fn_delete_player_entity(tc_entity_s *entity)

35
code/source-c/hook.c Normal file
View File

@ -0,0 +1,35 @@
#include <hook.h>
#include <stdlib.h>
#include <stdio.h>
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);
}
}

29
code/source-c/hook.h Normal file
View File

@ -0,0 +1,29 @@
#ifndef TC_HOOK_H
#define TC_HOOK_H
#include <utility/utility.h>
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

View File

@ -69,12 +69,23 @@ void tc_init()
tc_create_blocks(); tc_create_blocks();
tc_game_state_g.entity_registry = tc_init_entity_registry(); tc_game_state_g.entity_registry = tc_init_entity_registry();
tc_register_entities(); 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_init_worlds();
tc_game_state_g.main_world = tc_new_world(&tc_default_terrain_generator_g); 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!"); puts("Finished initializing!");
} }

View File

@ -177,7 +177,7 @@ uint32_t tc_meshize_block(tc_chunk_s *chunk,
return num_vertices; 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->num_vertices = tc_count_chunk_vertices(chunk);
chunk->vertex_positions = calloc(sizeof(float), 3 * chunk->num_vertices); chunk->vertex_positions = calloc(sizeof(float), 3 * chunk->num_vertices);

View File

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

View File

@ -2,14 +2,14 @@
tc_physics_simulation_s tc_new_physics_simulation() 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)
{ {

View File

@ -5,7 +5,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include "../utility.h" #include <utility/math.h>
typedef struct tc_physics_mesh tc_physics_mesh_s; typedef struct tc_physics_mesh tc_physics_mesh_s;
@ -29,40 +29,40 @@ typedef enum
struct tc_aabb struct tc_aabb
{ {
tc_vec3_s start; tc_vec3f_s start;
tc_vec3_s size; tc_vec3f_s size;
}; };
struct tc_obb struct tc_obb
{ {
tc_vec3_s start; tc_vec3f_s start;
tc_vec3_s size; tc_vec3f_s size;
tc_vec3_s rotation; tc_vec3f_s rotation;
}; };
struct tc_plane struct tc_plane
{ {
tc_vec3_s center; tc_vec3f_s center;
tc_vec3_s rotation; tc_vec3f_s rotation;
tc_vec2i_s size; tc_vec2i_s size;
}; };
struct tc_ellipsoid struct tc_ellipsoid
{ {
tc_vec3_s center; tc_vec3f_s center;
tc_vec3_s extent; tc_vec3f_s extent;
}; };
struct tc_ray struct tc_ray
{ {
tc_vec3_s start; tc_vec3f_s start;
tc_vec3_s rotation; tc_vec3f_s rotation;
}; };
struct tc_line struct tc_line
{ {
tc_vec3_s start; tc_vec3f_s start;
tc_vec3_s end; tc_vec3f_s end;
}; };
@ -72,14 +72,13 @@ typedef union tc_primitive
tc_obb_s obb; tc_obb_s obb;
tc_plane_s plane; tc_plane_s plane;
tc_ellipsoid_s sphere; tc_ellipsoid_s sphere;
tc_plane_s plane;
tc_line_s line; tc_line_s line;
tc_ray_s ray; tc_ray_s ray;
} tc_primitive_u; } tc_primitive_u;
typedef struct tc_primitive typedef struct tc_primitive_container
{ {
tc_primitive_type_e type; tc_primitive_type_e type;
tc_primitive_u data; tc_primitive_u data;
@ -88,8 +87,8 @@ typedef struct tc_primitive
struct tc_physics_mesh struct tc_physics_mesh
{ {
uint32_t primitives_capacity; u32_t primitives_capacity;
uint32_t num_primitives; u32_t num_primitives;
tc_primitive_s *primitives; tc_primitive_s *primitives;
}; };

View File

@ -1,4 +1,4 @@
#include "primitives.h" #include <physics/simulation.h>
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)
{ {

View File

@ -4,21 +4,22 @@
#include <stdint.h> #include <stdint.h>
#include <physics/primitives.h>
#include <utility/utility.h> #include <utility/utility.h>
#include <../entity.h> #include <entity.h>
typedef struct tc_physics_entity tc_physics_entity_s; 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 struct tc_entity_physics_attributes
{ {
bool has_gravity; bool_t has_gravity;
bool raw_movement; bool_t raw_movement;
bool raw_rotation; bool_t raw_rotation;
tc_vec3_s acceleration; tc_vec3f_s acceleration;
tc_vec3_s spin; tc_vec3f_s spin;
}; };
typedef enum typedef enum
@ -30,23 +31,24 @@ typedef enum
struct tc_physics_entity 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 // 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 union specific
{ {
struct entity struct entity
{ {
char *name;
tc_entity_s *entity; tc_entity_s *entity;
} entity; } entity;
struct subentity_group struct subentity_group
{ {
uint32_t num_entities; u32_t num_entities;
tc_physics_entity_s *entities; tc_physics_entity_s *entities;
} group; } group;
@ -56,23 +58,19 @@ struct tc_physics_entity
typedef struct tc_physics_simulation typedef struct tc_physics_simulation
{ {
tc_vec3_s limits;
tc_physics_entity_s world_root; tc_physics_entity_s world_root;
} tc_physics_simulation_s; } tc_physics_simulation_s;
tc_physics_simulation_s tc_new_physics_simulation (); 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); 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 #endif

View File

@ -41,6 +41,8 @@ typedef struct
tc_entity_s *viewer; tc_entity_s *viewer;
tc_entity_s *player; tc_entity_s *player;
tc_hooklist_s on_world_create;
uint16_t fps; uint16_t fps;
uint16_t tps; uint16_t tps;

View File

@ -8,11 +8,10 @@ tc_entity_type_s *tc_chunk_entity_type_g = NULL;
void tc_upload_chunk(tc_chunk_s *chunk) 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) if(chunk->num_vertices == 0)
{ {
// Create one return;
tc_meshize_chunk(chunk);
} }
if(chunk->vao != 0) glDeleteVertexArrays(1, &chunk->vao); if(chunk->vao != 0) glDeleteVertexArrays(1, &chunk->vao);
@ -83,14 +82,19 @@ void tc_init_world_generators()
} }
// The userdata should be the corresponding world
void tc_create_chunk_entity(tc_entity_s *entity) 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_string_for_entity(entity, "type", "chunk");
tc_set_integer_for_entity(entity, "grid_x", 0); 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_y", 0);
tc_set_integer_for_entity(entity, "grid_z", 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) 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.x = location.position.x / 32;
chunk->position.y = location.position.y / 32; chunk->position.y = location.position.y / 32;
chunk->position.z = location.position.z / 32; chunk->position.z = location.position.z / 32;
} }
void tc_delete_chunk_entity(tc_entity_s *entity) 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 = malloc(sizeof(tc_chunkloader_s) * world->num_loaders);
world->loaders[0] = tc_create_spawn_loader(world); world->loaders[0] = tc_create_spawn_loader(world);
world->spawn_loader = &world->loaders[0]; 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; return world;
} }

View File

@ -5,6 +5,8 @@
#include <stdbool.h> #include <stdbool.h>
#include <blocks.h> #include <blocks.h>
#include <hook.h>
#include <physics/simulation.h>
#include <utility/math.h> #include <utility/math.h>
#include <entity.h> #include <entity.h>
@ -87,16 +89,20 @@ struct tc_chunk_pool
struct tc_world struct tc_world
{ {
// tc_chunk_s *center_chunk;
tc_chunk_pool_s *pool; tc_chunk_pool_s *pool;
tc_worldgen_s *worldgen; tc_worldgen_s *worldgen;
u32_t num_loaders; u32_t num_loaders;
tc_chunkloader_s *loaders; tc_chunkloader_s *loaders;
// chunk_loading_center: The center of loading ON THE CHUNK GRID COORDINATES
tc_chunkloader_s *spawn_loader; 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; 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); 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); 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);
@ -136,5 +142,7 @@ void tc_init_chunk_pool (u32_t capacity);
tc_chunk_s * tc_allocate_chunk (); tc_chunk_s * tc_allocate_chunk ();
void tc_free_chunk (tc_chunk_s *chunk); void tc_free_chunk (tc_chunk_s *chunk);
void tc_create_world_physics (tc_world_s *world, void *userdata);
#endif #endif

View File

@ -0,0 +1,15 @@
#include <world.h>
#include <stddef.h> // 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);
}