Compare commits

..

4 Commits

20 changed files with 358 additions and 101 deletions

View File

@ -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

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)
{
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);
}

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;
}
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);
@ -137,15 +137,15 @@ void tc_delete_entity(tc_entity_s *entity)
tc_entity_attribute_s * tc_get_entity_attribute_with_name(tc_entity_s *entity, char *name)
{
u32_t attribute_index = 0;
while(attribute_index < entity->attributes_capacity)
while(attribute_index < entity->num_attributes)
{
tc_entity_attribute_s attribute = entity->attributes[attribute_index];
if(attribute.name == NULL)
{
puts("NAAJAA");
++attribute_index;
continue;
}
if(!strcmp(attribute.name, name)) return &entity->attributes[attribute_index];
++attribute_index;
}

View File

@ -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);

View File

@ -1,4 +1,5 @@
#include "registration.h"
#include <state.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)
{
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)

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_hook_s));
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_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!");
}

View File

@ -212,8 +212,8 @@ bool update()
case SDL_MOUSEMOTION:
{
if(!(event.motion.state & SDL_BUTTON_LMASK)) break;
camera_rotation.y += event.motion.xrel / 10.0f;
camera_rotation.x += event.motion.yrel / 10.0f;
camera_rotation.y += event.motion.xrel / 5.0f;
camera_rotation.x += event.motion.yrel / 5.0f;
if(camera_rotation.x > 90)
camera_rotation.x = 90;
@ -230,12 +230,12 @@ bool update()
if(shift_pressed)
{
camera_position.y -= 0.25;
camera_position.y -= 0.75;
}
if(go_up)
{
camera_position.y += 0.25;
camera_position.y += 0.75;
}

View File

@ -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);

View File

@ -1,4 +1,16 @@
#include "primitives.h"
tc_physics_mesh_s tc_wrap_aabb(tc_vec3f_s start, tc_vec3f_s size)
{
tc_aabb_s aabb;
aabb.start = start;
aabb.size = size;
tc_physics_mesh_s mesh;
mesh.num_primitives = 1;
mesh.value.simple.primitive.type = TC_PRIMITIVE_AABB;
mesh.value.simple.primitive.data.aabb = aabb;
return mesh;
}

View File

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

View File

@ -1,42 +1,117 @@
#include "simulation.h"
tc_physics_simulation_s tc_new_physics_simulation()
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
tc_physics_simulation_s * tc_new_physics_simulation()
{
tc_physics_simulation_s *simulation = malloc(sizeof(tc_physics_simulation_s));
simulation->root = tc_new_physics_group(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)
{
tc_add_physics_object_to(simulation->root, 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)
{
if((container->value.group.num_entities+1)
>=
container->value.group.entities_capacity
) {
container->value.group.entities_capacity *= 2;
container->value.group.entities =
realloc(container->value.group.entities, sizeof(tc_physics_entity_s *) * container->value.group.entities_capacity);
}
container->value.group.entities[container->value.group.num_entities] = object;
++container->value.group.num_entities;
}
tc_phyiscs_entity_s tc_new_physics_entity()
tc_physics_entity_s * tc_new_physics_entity(tc_physics_simulation_s *simulation)
{
tc_physics_entity_s *entity = malloc(sizeof(tc_physics_entity_s));;
entity->super = NULL;
entity->simulation = simulation;
entity->relative_position.x = 0.0f;
entity->relative_position.y = 0.0f;
entity->relative_position.z = 0.0f;
entity->type = TC_PHYSICS_ENTITY;
entity->value.entity.mesh.num_primitives = 0;
entity->value.entity.name = NULL;
return entity;
}
void tc_tick_physics(tc_physics_simulation_s *simulation, float delta)
tc_physics_entity_s * tc_new_physics_group(tc_physics_simulation_s *simulation)
{
tc_physics_entity_s *entity = malloc(sizeof(tc_physics_entity_s));;
entity->super = NULL;
entity->simulation = simulation;
entity->relative_position.x = 0.0f;
entity->relative_position.y = 0.0f;
entity->relative_position.z = 0.0f;
entity->type = TC_PHYSICS_ENTITY_GROUP;
entity->value.group.entities_capacity = 8;
entity->value.group.num_entities = 0;
entity->value.group.entities =
calloc(sizeof(tc_physics_entity_s), entity->value.group.entities_capacity);
return entity;
}
i32_t tc_physics_find_entity_in_group(tc_physics_entity_s *group, tc_physics_entity_s *entity)
{
i32_t index = 0;
while(index < group->value.group.num_entities)
{
if(entity->super->value.group.entities[group->value.group.num_entities]
==
entity
) return index;
++index;
}
return -1;
}
void tc_free_physics_entity(tc_physics_entity_s *entity)
{
if(entity->type == TC_PHYSICS_ENTITY_GROUP)
{
for(u32_t index = 0; index < entity->value.group.num_entities; ++index)
tc_free_physics_entity(entity->value.group.entities[index]);
}
free(entity);
}
void tc_remove_physics_entity(tc_physics_entity_s *entity)
{
u32_t num_entities = entity->simulation->root->value.group.num_entities;
if(entity->super == NULL) return;
i32_t index = 0;
if((index = tc_physics_find_entity_in_group(entity->super, entity)) == -1) return;
entity->super->value.group.entities[index] =
entity->super->value.group.entities[entity->super->value.group.num_entities-1];
}
void tc_tick_physics(tc_physics_simulation_s *simulation, f32_t delta)
{
}

View File

@ -5,7 +5,7 @@
#include <stdbool.h>
#include <stdint.h>
#include "../utility.h"
#include <utility/math.h>
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,10 +87,24 @@ typedef struct tc_primitive
struct tc_physics_mesh
{
uint32_t primitives_capacity;
uint32_t num_primitives;
u32_t num_primitives;
union
{
struct complex
{
u32_t primitives_capacity;
tc_primitive_s *primitives;
} complex;
struct simple
{
tc_primitive_s primitive;
} simple;
} value;
};
tc_physics_mesh_s tc_wrap_aabb (tc_vec3f_s start, tc_vec3f_s size);
#endif // TC_PHYSICS_PRIMITIVES_H

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)
{

View File

@ -4,21 +4,23 @@
#include <stdint.h>
#include <physics/primitives.h>
#include <utility/utility.h>
#include <../entity.h>
#include <entity.h>
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;
typedef struct tc_physics_simulation tc_physics_simulation_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,49 +32,51 @@ typedef enum
struct tc_physics_entity
{
tc_physics_entity *super;
tc_physics_simulation_s *simulation;
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
{
tc_entity_s *entity;
char *name;
tc_physics_mesh_s mesh;
} entity;
struct subentity_group
{
uint32_t num_entities;
tc_physics_entity_s *entities;
u32_t entities_capacity;
u32_t num_entities;
tc_physics_entity_s **entities;
} group;
} value;
};
typedef struct tc_physics_simulation
struct tc_physics_simulation
{
tc_vec3_s limits;
tc_physics_entity_s *root;
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_s *object);
void tc_add_physics_object_to (tc_physics_entity_s *container, tc_physics_entity_s *object);
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_physics_entity_s * tc_new_physics_entity (tc_physics_simulation_s *simulation);
tc_physics_entity_s * tc_new_physics_group (tc_physics_simulation_s *simulation);
tc_phyiscs_entity_s tc_new_physics_entity ();
void tc_remove_physics_entity (tc_physics_entity_s *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

View File

@ -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;

View File

@ -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,20 @@ 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;
chunk->world = world;
tc_run_hooklist(&world->on_chunk_create, entity);
}
void tc_spawn_chunk_entity(tc_entity_s *entity, tc_location_s location)
@ -107,13 +112,12 @@ 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)
{
tc_physics_entity_s *physics_body = tc_get_pointer_from_entity(entity, "physics_body");
tc_remove_physics_entity(physics_body);
tc_free_chunk(entity->specific);
}
@ -197,6 +201,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;
}

View File

@ -5,6 +5,8 @@
#include <stdbool.h>
#include <blocks.h>
#include <hook.h>
#include <physics/simulation.h>
#include <utility/math.h>
#include <entity.h>
@ -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;
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

View File

@ -0,0 +1,50 @@
#include <world.h>
#include <stddef.h> // For NULL
#include <stdio.h>
void tc_initialize_chunk_physics(tc_entity_s *chunk_entity, void *userdata)
{
tc_chunk_s *chunk = chunk_entity->specific;
tc_world_s *world = chunk->world;
tc_physics_simulation_s *simulation = world->physics;
tc_physics_entity_s *chunk_body = tc_new_physics_group(simulation);
tc_vec3f_s block_start;
block_start.x = -0.5f;
block_start.y = -0.5f;
block_start.z = -0.5f;
tc_vec3f_s block_size;
block_size.x = 1.0f;
block_size.y = 1.0f;
block_size.z = 1.0f;
tc_physics_mesh_s block_mesh = tc_wrap_aabb(block_start, block_size);
for(u32_t x = 0; x < 32; ++x)
for(u32_t y = 0; y < 32; ++y)
for(u32_t z = 0; z < 32; ++z)
{
tc_physics_entity_s *block_entity = tc_new_physics_entity(simulation);
block_entity->relative_position.x = x;
block_entity->relative_position.y = y;
block_entity->relative_position.z = z;
block_entity->type = TC_PHYSICS_ENTITY;
block_entity->value.entity.mesh = block_mesh;
tc_add_physics_object_to(chunk_body, block_entity);
}
tc_add_physics_object(world->physics, chunk_body);
tc_set_pointer_for_entity(chunk_entity, "physics_body", chunk_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_initialize_chunk_physics, NULL);
tc_add_to_hooklist(&world->on_chunk_update, &tc_initialize_chunk_physics, NULL);
}