Compare commits

...

2 Commits

Author SHA1 Message Date
Eric-Paul Ickhorn dc3a81b7b4 Partially integrated own mathematics library 2023-10-15 10:07:33 +02:00
Eric-Paul Ickhorn 40be22f69b An ECS-oid structure 2023-10-14 21:17:30 +02:00
22 changed files with 709 additions and 245 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 dependencies/build/glad.o \ code/source-c/*.c code/source-c/entity/*.c code/source-c/utility/*.c dependencies/build/glad.o \
-I dependencies/include -lGL -lSDL2 -lm -Wall -I dependencies/include -I code/source-c/ -lGL -lSDL2 -lm -Wall

View File

@ -1,4 +1,5 @@
#include "world.h" #include "world.h"
#include "entity.h"
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
@ -59,7 +60,8 @@ tc_chunk_s * tc_load_chunk_of_loader(tc_chunkloader_s *loader, tc_vec3i_s coord)
if(chunk == NULL) if(chunk == NULL)
{ {
chunk = tc_allocate_chunk(loader->world); tc_entity_s *entity = tc_create_entity("chunk"); // TODO: FURTHER RESTRUCTUING FOR ENTITIES
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);
@ -89,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_free_chunk(loader->world, loader->chunks[index]); tc_free_chunk(loader->chunks[index]);
if(loader->num_chunks != 0) if(loader->num_chunks != 0)
{ {

View File

@ -3,6 +3,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
tc_chunk_pool_s *tc_chunk_pool_g = NULL;
void tc_reset_chunk_pool(tc_chunk_pool_s *chunk_pool) void tc_reset_chunk_pool(tc_chunk_pool_s *chunk_pool)
{ {
chunk_pool->used_entries = 0; chunk_pool->used_entries = 0;
@ -20,7 +22,7 @@ void tc_reset_chunk_pool(tc_chunk_pool_s *chunk_pool)
chunk_pool->entries[chunk_pool->capacity-1].next = NULL; chunk_pool->entries[chunk_pool->capacity-1].next = NULL;
} }
tc_chunk_pool_s * tc_new_chunk_pool(uint32_t capacity) tc_chunk_pool_s * tc_internal_new_chunk_pool(uint32_t capacity)
{ {
tc_chunk_pool_s *pool = malloc(sizeof(tc_chunk_pool_s)); tc_chunk_pool_s *pool = malloc(sizeof(tc_chunk_pool_s));
pool->capacity = capacity; pool->capacity = capacity;
@ -30,11 +32,17 @@ tc_chunk_pool_s * tc_new_chunk_pool(uint32_t capacity)
return pool; return pool;
} }
void tc_init_chunk_pool(uint32_t capacity)
{
tc_chunk_pool_g = tc_internal_new_chunk_pool(capacity);
}
tc_chunk_pool_entry_s * tc_allocate_chunk_pool_entry(tc_chunk_pool_s *pool) tc_chunk_pool_entry_s * tc_allocate_chunk_pool_entry(tc_chunk_pool_s *pool)
{ {
if(pool->first_free == NULL) if(pool->first_free == NULL)
{ {
pool->continuation = tc_new_chunk_pool(pool->capacity * 2); if(pool->continuation == NULL)
pool->continuation = tc_internal_new_chunk_pool(pool->capacity * 2);
return tc_allocate_chunk_pool_entry(pool->continuation); return tc_allocate_chunk_pool_entry(pool->continuation);
} }
tc_chunk_pool_entry_s *allocated = pool->first_free; tc_chunk_pool_entry_s *allocated = pool->first_free;
@ -43,18 +51,16 @@ tc_chunk_pool_entry_s * tc_allocate_chunk_pool_entry(tc_chunk_pool_s *pool)
return allocated; return allocated;
} }
tc_chunk_s * tc_allocate_chunk(tc_world_s *world) tc_chunk_s * tc_allocate_chunk()
{ {
tc_chunk_pool_entry_s *entry = tc_allocate_chunk_pool_entry(world->pool); tc_chunk_pool_entry_s *entry = tc_allocate_chunk_pool_entry(tc_chunk_pool_g);
entry->chunk.pool_entry = entry; entry->chunk.pool_entry = entry;
return &entry->chunk; return &entry->chunk;
} }
void tc_free_chunk(tc_world_s *world, tc_chunk_s *chunk) void tc_free_chunk(tc_chunk_s *chunk)
{ {
tc_chunk_pool_s *pool = world->pool;
if(chunk->vao != 0) if(chunk->vao != 0)
{ {
glDeleteVertexArrays(1, &chunk->vertex_data); glDeleteVertexArrays(1, &chunk->vertex_data);
@ -84,7 +90,7 @@ void tc_free_chunk(tc_world_s *world, tc_chunk_s *chunk)
chunk->position.y = 0; chunk->position.y = 0;
chunk->position.z = 0; chunk->position.z = 0;
memset(&chunk->blocks, 0x00, sizeof(uint32_t) * 32 * 32 * 32); memset(&chunk->blocks, 0x00, sizeof(uint32_t) * 32 * 32 * 32);
chunk->pool_entry->next = pool->first_free; chunk->pool_entry->next = tc_chunk_pool_g->first_free;
pool->first_free = chunk->pool_entry; tc_chunk_pool_g->first_free = chunk->pool_entry;
} }

View File

@ -10,9 +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 = 512; registry.entities_capacity = 1024;
registry.num_entities = 0; registry.num_entities = 0;
registry.entities = calloc(sizeof(tc_entity_s *), registry.entities_capacity); registry.entities = calloc(sizeof(tc_entity_s *), registry.entities_capacity);
registry.intervals_capacity = 256;
registry.num_intervals = 0;
registry.intervals = calloc(sizeof(tc_interval_s *), registry.intervals_capacity);
return registry; return registry;
} }
@ -55,7 +58,11 @@ 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;
return type_struct->functions.fn_create(); tc_entity_s *entity = type_struct->functions.fn_create();
entity->type = type_struct;
++tc_game_state_g.entity_registry.num_entities;
return entity;
} }
void tc_spawn_entity(tc_entity_s *entity, tc_location_s location) void tc_spawn_entity(tc_entity_s *entity, tc_location_s location)
@ -68,9 +75,14 @@ void tc_teleport_entity(tc_entity_s *entity, tc_location_s location)
entity->type->functions.fn_teleport(entity, location); entity->type->functions.fn_teleport(entity, location);
} }
void tc_draw_entity(tc_entity_s *entity)
{
entity->type->functions.fn_draw(entity);
}
void tc_delete_entity(tc_entity_s *entity) void tc_delete_entity(tc_entity_s *entity)
{ {
entity->type->functions.fn_delete(entity); entity->type->functions.fn_draw(entity);
} }
@ -98,7 +110,7 @@ tc_entity_attribute_s * tc_allocate_entity_attribute(tc_entity_s *entity)
if(entity->num_attributes >= entity->attributes_capacity) if(entity->num_attributes >= entity->attributes_capacity)
{ {
entity->attributes_capacity *= 2; entity->attributes_capacity *= 2;
entity->attributes = realloc(entity->attributes, sizeof(tc_entity_s) * entity->attributes_capacity); entity->attributes = realloc(entity->attributes, sizeof(tc_entity_attribute_s) * entity->attributes_capacity);
} }
++entity->num_attributes; ++entity->num_attributes;
return &entity->attributes[entity->num_attributes-1]; return &entity->attributes[entity->num_attributes-1];
@ -186,6 +198,54 @@ void tc_forget_entity_attribute(tc_entity_s *entity, char *attr_name)
int32_t tc_find_index_of_entity_in_intervals(tc_entity_s *entity)
{
uint32_t interval_index = 0;
while(interval_index < tc_game_state_g.entity_registry.num_intervals)
{
if(tc_game_state_g.entity_registry.intervals[interval_index].entity == entity)
return interval_index;
++interval_index;
}
return -1;
}
uint32_t tc_allocate_interval()
{
if(
tc_game_state_g.entity_registry.num_intervals
>=
tc_game_state_g.entity_registry.intervals_capacity
) {
tc_game_state_g.entity_registry.intervals_capacity *= 2;
tc_game_state_g.entity_registry.intervals =
realloc(
tc_game_state_g.entity_registry.intervals,
sizeof(tc_interval_s) * tc_game_state_g.entity_registry.intervals_capacity
);
}
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.num_intervals;
return last;
}
void tc_schedule_interval(tc_entity_s *entity, float wanted_delta, void (*fn_interval)(tc_entity_s *entity))
{
int32_t interval_index;
if((interval_index = tc_find_index_of_entity_in_intervals(entity)) == -1)
{
interval_index = tc_allocate_interval();
}
tc_interval_s entry;
entry.entity = entity;
entry.wanted_delta = wanted_delta;
entry.fn_interval = fn_interval;
tc_game_state_g.entity_registry.intervals[interval_index] = entry;
}
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;
@ -218,3 +278,46 @@ void tc_send_float_to_entity(tc_entity_s *entity, char *event_name, double floa
entity->type->functions.fn_send_event(entity, event); entity->type->functions.fn_send_event(entity, event);
} }
void tc_tick_intervals()
{
uint64_t current_ms = SDL_GetTicks64();
tc_entity_registry_s registry = tc_game_state_g.entity_registry;
uint32_t interval_index = 0;
while(interval_index < registry.num_intervals)
{
tc_interval_s interval = registry.intervals[interval_index];
uint32_t elapsed_time = current_ms - interval.last_invocation;
if(elapsed_time >= interval.wanted_delta)
{
if(interval.fn_interval == NULL)
{
++interval_index;
continue;
}
interval.fn_interval(interval.entity);
}
++interval_index;
}
}
void tc_draw_all_entities_of_type(char *name)
{
tc_entity_type_s *type = tc_get_entity_type_with_name(name);
tc_entity_registry_s registry = tc_game_state_g.entity_registry;
uint32_t drawn_entities = 0;
uint32_t entity_index = 0;
while(entity_index < registry.num_entities)
{
tc_entity_s *entity = registry.entities[entity_index];
if(entity->type == type)
{
tc_draw_entity(entity);
++drawn_entities;
}
++entity_index;
}
}

View File

@ -2,13 +2,14 @@
#ifndef TC_ENTITY_H #ifndef TC_ENTITY_H
#define TC_ENTITY_H #define TC_ENTITY_H
#include "location.h" #include "utility/location.h"
#include "utility.h" #include "utility/utility.h"
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_interval tc_interval_s;
typedef struct tc_entity_registry tc_entity_registry_s; typedef struct tc_entity_registry tc_entity_registry_s;
@ -17,6 +18,7 @@ typedef struct
tc_entity_s * (*fn_create) (); 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_delete) (tc_entity_s *entity); void (*fn_delete) (tc_entity_s *entity);
void (*fn_send_event) (tc_entity_s *entity, tc_entity_event_s event); void (*fn_send_event) (tc_entity_s *entity, tc_entity_event_s event);
@ -41,7 +43,6 @@ struct tc_entity_type
char *display_name; char *display_name;
tc_fn_entity_s functions; tc_fn_entity_s functions;
}; };
struct tc_entity_attribute struct tc_entity_attribute
@ -72,6 +73,14 @@ struct tc_entity
}; };
struct tc_interval
{
uint64_t last_invocation;
uint64_t wanted_delta;
tc_entity_s *entity;
void (*fn_interval) (tc_entity_s *entity);
};
struct tc_entity_registry struct tc_entity_registry
{ {
uint32_t types_capacity; uint32_t types_capacity;
@ -81,6 +90,10 @@ struct tc_entity_registry
uint32_t entities_capacity; uint32_t entities_capacity;
uint32_t num_entities; uint32_t num_entities;
tc_entity_s **entities; tc_entity_s **entities;
uint32_t intervals_capacity;
uint32_t num_intervals;
tc_interval_s *intervals;
}; };
tc_entity_registry_s tc_init_entity_registry (); tc_entity_registry_s tc_init_entity_registry ();
@ -91,6 +104,7 @@ 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 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_delete_entity (tc_entity_s *entity); void tc_delete_entity (tc_entity_s *entity);
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);
@ -108,6 +122,10 @@ void tc_send_string_to_entity (tc_entity_s *en
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);
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);
void tc_schedule_interval (tc_entity_s *entity, float wanted_delta, void (*fn_interval)(tc_entity_s *entity));
void tc_tick_intervals ();
void tc_draw_all_entities_of_type (char *name);
tc_entity_s * tc_allocate_entity (); tc_entity_s * tc_allocate_entity ();
void tc_deallocate_entity (tc_entity_s *entity); void tc_deallocate_entity (tc_entity_s *entity);

View File

@ -3,6 +3,17 @@
#include <stdlib.h> #include <stdlib.h>
// TODO: Write a *real* pool allocator
void tc_reset_entity(tc_entity_s *entity)
{
if(entity->attributes != NULL) free(entity->attributes);
entity->attributes_capacity = 8;
entity->attributes =
calloc(sizeof(tc_entity_attribute_s), entity->attributes_capacity);
}
tc_entity_s * tc_allocate_entity() tc_entity_s * tc_allocate_entity()
{ {
tc_entity_registry_s registry = tc_game_state_g.entity_registry; tc_entity_registry_s registry = tc_game_state_g.entity_registry;
@ -14,16 +25,24 @@ tc_entity_s * tc_allocate_entity()
{ {
registry.entities[entity_index] = calloc(sizeof(tc_entity_s), 1); registry.entities[entity_index] = calloc(sizeof(tc_entity_s), 1);
++registry.num_entities; ++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]; return registry.entities[entity_index];
} }
++entity_index; ++entity_index;
} }
registry.entities_capacity *= 2;
registry.entities = registry.entities =
realloc(registry.entities, sizeof(tc_entity_s *) * registry.entities_capacity); 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)); registry.entities[registry.num_entities] = malloc(sizeof(tc_entity_s));
tc_reset_entity(registry.entities[registry.num_entities]);
++registry.num_entities; ++registry.num_entities;
return registry.entities[registry.num_entities-1]; return registry.entities[registry.num_entities-1];

View File

@ -73,6 +73,8 @@ void tc_init()
tc_game_state_g.viewer = tc_game_state_g.player; tc_game_state_g.viewer = tc_game_state_g.player;
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;
puts("Finished initializing!"); puts("Finished initializing!");
} }

View File

@ -1,69 +1,10 @@
#include "state.h" #include <state.h>
#include <utility/math.h>
#include <glad/glad.h> #include <glad/glad.h>
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <linmath.h> #include <linmath.h>
mat4x4 projection_matrix;
mat4x4 view_matrix;
float triangle_vertices[108] =
{
// Front
-0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
// Back
0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
// Left
-0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, -0.5f,
// Right
0.5f, -0.5f, 0.5f,
0.5f, -0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
0.5f, 0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
// Top
-0.5f, 0.5f, -0.5f,
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, -0.5f,
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, -0.5f,
// Bottom
-0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, -0.5f
};
void tc_move_viewer_to(float x, float y, float z) void tc_move_viewer_to(float x, float y, float z)
{ {
if(tc_game_state_g.viewer == NULL) return; if(tc_game_state_g.viewer == NULL) return;
@ -100,45 +41,11 @@ void tc_get_viewer_rotation(float *x, float *y, float *z)
if(z != NULL) (*z) = tc_get_float_from_entity(tc_game_state_g.viewer, "rot_z"); if(z != NULL) (*z) = tc_get_float_from_entity(tc_game_state_g.viewer, "rot_z");
} }
void render_block(tc_block_s block)
{
mat4x4 model_matrix;
mat4x4_identity(model_matrix);
mat4x4_rotate_X(view_matrix, model_matrix, block.rotation.x);
mat4x4_rotate_Y(view_matrix, model_matrix, block.rotation.y);
mat4x4_rotate_Z(view_matrix, model_matrix, block.rotation.z);
mat4x4_translate_in_place(model_matrix, block.position.x, block.position.y, block.position.z);
int model_matrix_uniform_location = glGetUniformLocation(tc_game_state_g.renderer.draw_shader.program_id, "model_matrix");
glUniformMatrix4fv(model_matrix_uniform_location, 1, GL_FALSE, &model_matrix[0][0]);
glBindBuffer(GL_ARRAY_BUFFER, block.drawing.vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *) 0);
glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLES, 0, 36);
}
tc_block_s tc_new_block_at_3f(float x, float y, float z)
{
tc_block_s block;
block.position.x = x;
block.position.y = y;
block.position.z = z;
block.rotation.x = 0.0f;
block.rotation.y = 0.0f;
block.rotation.z = 0.0f;
glGenBuffers(1, &block.drawing.vbo);
glBindBuffer(GL_ARRAY_BUFFER, block.drawing.vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 108, &triangle_vertices[0], GL_STATIC_DRAW);
return block;
}
void render() void render()
{ {
float color[4] = { 0.12f, 0.5f, 0.8f, 1.0f }; float color[4] = { 0.12f, 0.5f, 0.8f, 1.0f };
glClearBufferfv(GL_COLOR, 0, color); glClearBufferfv(GL_COLOR, 0, color);
float depth = 10000000.0f; float depth = 1000.0f;
glClearBufferfv(GL_DEPTH, 0, &depth); glClearBufferfv(GL_DEPTH, 0, &depth);
int fb_width = 0; int fb_width = 0;
@ -146,29 +53,35 @@ void render()
SDL_GetWindowSize(tc_game_state_g.renderer.window, &fb_width, &fb_height); SDL_GetWindowSize(tc_game_state_g.renderer.window, &fb_width, &fb_height);
glViewport(0, 0, fb_width, fb_height); glViewport(0, 0, fb_width, fb_height);
mat4x4_perspective(projection_matrix, 120, ((float) fb_width) / ((float) fb_height), 0.0001f, 1000.0f);
tc_vec3_s camera_rotation; tc_vec3f_s camera_rotation;
tc_vec3f_s camera_position;
tc_get_viewer_rotation(&camera_rotation.x, &camera_rotation.y, &camera_rotation.z); tc_get_viewer_rotation(&camera_rotation.x, &camera_rotation.y, &camera_rotation.z);
tc_vec3_s camera_position;
tc_get_viewer_position(&camera_position.x, &camera_position.y, &camera_position.z); tc_get_viewer_position(&camera_position.x, &camera_position.y, &camera_position.z);
mat4x4_identity(view_matrix); tc_vec3f_s camera_offset;
mat4x4_rotate_X(view_matrix, view_matrix, camera_rotation.x); camera_offset.z = camera_position.z;
mat4x4_rotate_Y(view_matrix, view_matrix, camera_rotation.y); camera_offset.y = -camera_position.y;
mat4x4_rotate_Z(view_matrix, view_matrix, camera_rotation.z); camera_offset.x = -camera_position.x;
mat4x4_translate_in_place(view_matrix, -camera_position.x, -camera_position.y, camera_position.z); tc_mat4f_s view_matrix = tc_mat4f_identity();
tc_mat4f_s projection_matrix =
tc_mat4f_perspective(120,
((float) fb_width) / ((float) fb_height),
0.0001f, 1000.0f
);
view_matrix = tc_mat4f_translate(view_matrix, camera_offset);
view_matrix = tc_mat4_rotate_z(view_matrix, tc_deg_to_rad(camera_rotation.z));
view_matrix = tc_mat4_rotate_y(view_matrix, tc_deg_to_rad(camera_rotation.y));
view_matrix = tc_mat4_rotate_x(view_matrix, tc_deg_to_rad(camera_rotation.x));
glUseProgram(tc_game_state_g.renderer.draw_shader.program_id); glUseProgram(tc_game_state_g.renderer.draw_shader.program_id);
int projection_uniform_location = glGetUniformLocation(tc_game_state_g.renderer.draw_shader.program_id, "projection_matrix"); tc_shader_uniform_mat4f(&tc_game_state_g.renderer.draw_shader, "projection_matrix", projection_matrix);
glUniformMatrix4fv(projection_uniform_location, 1, GL_FALSE, &projection_matrix[0][0]); tc_shader_uniform_mat4f(&tc_game_state_g.renderer.draw_shader, "view_matrix", view_matrix);
int view_uniform_location = glGetUniformLocation(tc_game_state_g.renderer.draw_shader.program_id, "view_matrix");
glUniformMatrix4fv(view_uniform_location, 1, GL_FALSE, &view_matrix[0][0]);
// printf("%d\n", view_uniform_location);
tc_draw_world(tc_game_state_g.main_world); tc_draw_world(tc_game_state_g.main_world);
@ -184,9 +97,11 @@ bool go_backwards = false;
bool rotate_left = false; bool rotate_left = false;
bool rotate_right = false; bool rotate_right = false;
uint64_t tc_last_update = 0;
bool update() bool update()
{ {
tc_vec3_s camera_rotation; tc_vec3f_s camera_rotation;
tc_get_viewer_rotation(&camera_rotation.x, &camera_rotation.y, &camera_rotation.z); tc_get_viewer_rotation(&camera_rotation.x, &camera_rotation.y, &camera_rotation.z);
SDL_Event event; SDL_Event event;
@ -297,20 +212,20 @@ bool update()
case SDL_MOUSEMOTION: case SDL_MOUSEMOTION:
{ {
if(!(event.motion.state & SDL_BUTTON_LMASK)) break; if(!(event.motion.state & SDL_BUTTON_LMASK)) break;
camera_rotation.y += event.motion.xrel / 50.0f; camera_rotation.y += event.motion.xrel / 10.0f;
camera_rotation.x += event.motion.yrel / 50.0f; camera_rotation.x += event.motion.yrel / 10.0f;
if(camera_rotation.x > (3.1415f/2.0f)) if(camera_rotation.x > 90)
camera_rotation.x = (3.1415f/2.0f); camera_rotation.x = 90;
if(camera_rotation.x < -(3.1415f/2.0f)) if(camera_rotation.x < -90)
camera_rotation.x = -(3.1415f/2.0f); camera_rotation.x = -90;
} break; } break;
} }
} }
tc_vec3_s camera_position; tc_vec3f_s camera_position;
tc_get_viewer_position(&camera_position.x, &camera_position.y, &camera_position.z); tc_get_viewer_position(&camera_position.x, &camera_position.y, &camera_position.z);
if(shift_pressed) if(shift_pressed)
@ -325,7 +240,7 @@ bool update()
tc_vec3_s difference_in_perspective; tc_vec3f_s difference_in_perspective;
difference_in_perspective.x = 0.0f; difference_in_perspective.x = 0.0f;
difference_in_perspective.y = 0.0f; difference_in_perspective.y = 0.0f;
difference_in_perspective.z = 0.0f; difference_in_perspective.z = 0.0f;
@ -355,9 +270,9 @@ bool update()
mat4x4_translate(position_matrix, difference_in_perspective.x, 0.0f, difference_in_perspective.z); mat4x4_translate(position_matrix, difference_in_perspective.x, 0.0f, difference_in_perspective.z);
mat4x4_rotate_X(position_matrix, position_matrix, camera_rotation.x); mat4x4_rotate_Z(position_matrix, position_matrix, tc_deg_to_rad(camera_rotation.z));
mat4x4_rotate_Y(position_matrix, position_matrix, camera_rotation.y); mat4x4_rotate_Y(position_matrix, position_matrix, tc_deg_to_rad(camera_rotation.y));
mat4x4_rotate_Z(position_matrix, position_matrix, camera_rotation.z); mat4x4_rotate_X(position_matrix, position_matrix, tc_deg_to_rad(camera_rotation.x));
vec4 viewed_difference = { difference_in_perspective.x, 0.0f, difference_in_perspective.z }; vec4 viewed_difference = { difference_in_perspective.x, 0.0f, difference_in_perspective.z };
vec4 raw_difference = { 0.0f, 0.0f, 0.0f }; vec4 raw_difference = { 0.0f, 0.0f, 0.0f };
@ -371,10 +286,13 @@ bool update()
tc_game_state_g.main_world->spawn_loader->center.x = ((int32_t) camera_position.x) / 32 - UPDATE_DISTANCE; tc_game_state_g.main_world->spawn_loader->center.x = ((int32_t) camera_position.x) / 32 - UPDATE_DISTANCE;
tc_game_state_g.main_world->spawn_loader->center.y = ((int32_t) camera_position.y) / 32 - UPDATE_DISTANCE; tc_game_state_g.main_world->spawn_loader->center.y = ((int32_t) camera_position.y) / 32 - UPDATE_DISTANCE;
tc_game_state_g.main_world->spawn_loader->center.z = ((int32_t) camera_position.z) / 32 - UPDATE_DISTANCE; tc_game_state_g.main_world->spawn_loader->center.z = - ((int32_t) camera_position.z) / 32 - UPDATE_DISTANCE;
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;
} }
@ -389,6 +307,8 @@ int main(int argc, char **argv)
{ {
frame_index = -1; frame_index = -1;
} }
tc_tick_intervals();
render(); render();
if(frame_index == 1) if(frame_index == 1)
@ -397,7 +317,7 @@ int main(int argc, char **argv)
} }
++frame_index; ++frame_index;
SDL_Delay(1000/60); SDL_Delay(2);
} }
tc_cleanup(); tc_cleanup();

View File

@ -92,3 +92,10 @@ tc_shader_program_s tc_make_shader_program(char *vertex_path, char *fragment_pat
return shader_program; return shader_program;
} }
void tc_shader_uniform_mat4f(tc_shader_program_s *program, char *location, tc_mat4f_s matrix)
{
int location_id =
glGetUniformLocation(program->program_id, location);
glUniformMatrix4fv(location_id, 1, GL_TRUE, &matrix.values[0][0]);
}

View File

@ -9,13 +9,18 @@
#include <glad/glad.h> #include <glad/glad.h>
#include <utility/math.h>
typedef struct tc_shader_program typedef struct tc_shader_program
{ {
uint32_t program_id; uint32_t program_id;
} tc_shader_program_s; } tc_shader_program_s;
tc_shader_program_s tc_make_shader_program(char *vertex_path, char *fragment_path); tc_shader_program_s tc_make_shader_program (char *vertex_path, char *fragment_path);
void tc_shader_uniform_mat4f (tc_shader_program_s *program, char *location, tc_mat4f_s matrix);
#endif #endif

View File

@ -6,17 +6,17 @@
#include <stdint.h> #include <stdint.h>
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include "assets.h" #include <assets.h>
#include "shaders.h" #include <shaders.h>
#include "entity.h" #include <entity.h>
#include "blocks.h" #include <blocks.h>
#include "world.h" #include <world.h>
#include "utility.h" #include <utility/utility.h>
typedef struct tc_camera typedef struct tc_camera
{ {
tc_vec3_s position; tc_vec3f_s position;
tc_vec3_s rotation; tc_vec3f_s rotation;
float fov; float fov;
} tc_camera_s; } tc_camera_s;
@ -41,6 +41,9 @@ typedef struct
tc_entity_s *viewer; tc_entity_s *viewer;
tc_entity_s *player; tc_entity_s *player;
uint16_t fps;
uint16_t tps;
tc_image_s *block_texture_atlas; tc_image_s *block_texture_atlas;
} techneck_s; } techneck_s;

View File

@ -1,36 +0,0 @@
#ifndef TC_UTILITY_H
#define TC_UTILITY_H
#include <stdint.h>
#include <stdbool.h>
typedef struct tc_vec3
{
float x;
float y;
float z;
} tc_vec3_s;
typedef struct tc_vec3i
{
int32_t x;
int32_t y;
int32_t z;
} tc_vec3i_s;
bool tc_vec3i_equ(tc_vec3i_s first, tc_vec3i_s second);
typedef struct tc_object
{
uint32_t vbo;
uint32_t vao;
tc_vec3_s position;
tc_vec3_s rotation;
} tc_object_s;
#endif

View File

@ -1,4 +1,4 @@
#include "location.h" #include <utility/location.h>
#include <stdlib.h> #include <stdlib.h>

View File

@ -2,13 +2,13 @@
#ifndef TC_LOCATION_H #ifndef TC_LOCATION_H
#define TC_LOCATION_H #define TC_LOCATION_H
#include "world.h" #include <world.h>
typedef struct tc_location typedef struct tc_location
{ {
tc_world_s *world; tc_world_s *world;
tc_vec3_s position; tc_vec3f_s position;
tc_vec3_s rotation; tc_vec3f_s rotation;
} tc_location_s; } tc_location_s;

View File

@ -0,0 +1,12 @@
#include <utility/math.h>
f32_t tc_deg_to_rad(f32_t degrees)
{
return (degrees / 360.0f) * (3.1415 * 2.0f);
}
f32_t tc_rad_to_deg(f32_t radians)
{
return radians / (3.1415*2.0f) * 360.0f;
}

View File

@ -0,0 +1,96 @@
#ifndef TC_MATH_H
#define TC_MATH_H
#include <utility/utility.h>
typedef struct tc_vec4f tc_vec4f_s;
typedef struct tc_vec3f tc_vec3f_s;
typedef struct tc_vec2f tc_vec2f_s;
typedef struct tc_vec4i tc_vec4i_s;
typedef struct tc_vec3i tc_vec3i_s;
typedef struct tc_vec2i tc_vec2i_s;
typedef struct tc_mat4f tc_mat4f_s;
typedef struct tc_mat3f tc_mat3f_s;
struct tc_vec4f
{
f32_t x;
f32_t y;
f32_t z;
f32_t w;
};
struct tc_vec3f
{
f32_t x;
f32_t y;
f32_t z;
};
struct tc_vec2f
{
f32_t x;
f32_t y;
};
struct tc_vec4i
{
i32_t x;
i32_t y;
i32_t z;
i32_t w;
};
struct tc_vec3i
{
i32_t x;
i32_t y;
i32_t z;
};
struct tc_vec2i
{
i32_t x;
i32_t y;
};
struct tc_mat4f
{
f32_t values[4][4];
};
struct tc_mat3f
{
f32_t values[3][3];
};
f32_t tc_deg_to_rad (f32_t degrees);
f32_t tc_rad_to_deg (f32_t radians);
bool_t tc_vec4f_equ (tc_vec4f_s vector_1, tc_vec4f_s vector_2);
bool_t tc_vec3f_equ (tc_vec3f_s vector_1, tc_vec3f_s vector_2);
bool_t tc_vec2f_equ (tc_vec2f_s vector_1, tc_vec2f_s vector_2);
bool_t tc_vec4i_equ (tc_vec4i_s vector_1, tc_vec4i_s vector_2);
bool_t tc_vec3i_equ (tc_vec3i_s vector_1, tc_vec3i_s vector_2);
bool_t tc_vec2i_equ (tc_vec2i_s vector_1, tc_vec2i_s vector_2);
tc_mat4f_s tc_mat4f_identity ();
tc_mat3f_s tc_mat3f_identity ();
tc_mat4f_s tc_mat4_rotate_x (tc_mat4f_s matrix, float radians);
tc_mat4f_s tc_mat4_rotate_y (tc_mat4f_s matrix, float radians);
tc_mat4f_s tc_mat4_rotate_z (tc_mat4f_s matrix, float radians);
tc_mat4f_s tc_mat4f_translate (tc_mat4f_s matrix, tc_vec3f_s translation);
tc_mat3f_s tc_mat3f_translate (tc_mat3f_s matrix, tc_vec3f_s translation);
tc_mat4f_s tc_mat4f_perspective (float fov, float aspect_ratio, float near, float far);
#endif // TC_MATH_H

View File

@ -0,0 +1,169 @@
#include <utility/math.h>
#include <math.h>
tc_mat4f_s tc_mat4f_zero()
{
tc_mat4f_s matrix;
matrix.values[0][0] = 0.0f;
matrix.values[0][1] = 0.0f;
matrix.values[0][2] = 0.0f;
matrix.values[0][3] = 0.0f;
matrix.values[1][0] = 0.0f;
matrix.values[1][1] = 0.0f;
matrix.values[1][2] = 0.0f;
matrix.values[1][3] = 0.0f;
matrix.values[2][0] = 0.0f;
matrix.values[2][1] = 0.0f;
matrix.values[2][2] = 0.0f;
matrix.values[2][3] = 0.0f;
matrix.values[3][0] = 0.0f;
matrix.values[3][1] = 0.0f;
matrix.values[3][2] = 0.0f;
matrix.values[3][3] = 0.0f;
return matrix;
}
tc_mat4f_s tc_mat4f_identity()
{
tc_mat4f_s matrix;
matrix.values[0][0] = 1.0f;
matrix.values[0][1] = 0.0f;
matrix.values[0][2] = 0.0f;
matrix.values[0][3] = 0.0f;
matrix.values[1][0] = 0.0f;
matrix.values[1][1] = 1.0f;
matrix.values[1][2] = 0.0f;
matrix.values[1][3] = 0.0f;
matrix.values[2][0] = 0.0f;
matrix.values[2][1] = 0.0f;
matrix.values[2][2] = 1.0f;
matrix.values[2][3] = 0.0f;
matrix.values[3][0] = 0.0f;
matrix.values[3][1] = 0.0f;
matrix.values[3][2] = 0.0f;
matrix.values[3][3] = 1.0f;
return matrix;
}
tc_mat3f_s tc_mat3f_identity()
{
tc_mat3f_s matrix;
matrix.values[0][0] = 1.0f;
matrix.values[0][1] = 0.0f;
matrix.values[0][2] = 0.0f;
matrix.values[1][0] = 0.0f;
matrix.values[1][1] = 1.0f;
matrix.values[1][2] = 0.0f;
matrix.values[2][0] = 0.0f;
matrix.values[2][1] = 0.0f;
matrix.values[2][2] = 1.0f;
return matrix;
}
tc_mat4f_s tc_mat4f_multiply(tc_mat4f_s first, tc_mat4f_s second)
{
tc_mat4f_s result;
u32_t k, r, c;
for(c = 0; c < 4; ++c)
{
for(r = 0; r < 4; ++r)
{
result.values[c][r] = 0.f;
for(k=0; k<4; ++k)
{
result.values[c][r] += first.values[k][r] * second.values[c][k];
}
}
}
return result;
}
tc_mat4f_s tc_mat4_rotate_x(tc_mat4f_s matrix, float radians)
{
tc_mat4f_s rotation = tc_mat4f_identity();
rotation.values[1][1] = cosf(radians);
rotation.values[1][2] = -sinf(radians);
rotation.values[2][1] = sinf(radians);
rotation.values[2][2] = cosf(radians);
return tc_mat4f_multiply(matrix, rotation);
}
tc_mat4f_s tc_mat4_rotate_y(tc_mat4f_s matrix, float radians)
{
tc_mat4f_s rotation = tc_mat4f_identity();
rotation.values[0][0] = cosf(radians);
rotation.values[0][2] = sinf(radians);
rotation.values[2][0] = -sinf(radians);
rotation.values[2][2] = cosf(radians);
return tc_mat4f_multiply(matrix, rotation);
}
tc_mat4f_s tc_mat4_rotate_z(tc_mat4f_s matrix, float radians)
{
tc_mat4f_s rotation = tc_mat4f_identity();
rotation.values[0][0] = cosf(radians);
rotation.values[0][1] = -sinf(radians);
rotation.values[1][0] = sinf(radians);
rotation.values[1][1] = cosf(radians);
return tc_mat4f_multiply(matrix, rotation);
}
tc_mat4f_s tc_mat4f_translate(tc_mat4f_s matrix, tc_vec3f_s translation)
{
matrix.values[0][3] += translation.x;
matrix.values[1][3] += translation.y;
matrix.values[2][3] += translation.z;
return matrix;
}
tc_mat3f_s tc_mat3f_translate(tc_mat3f_s matrix, tc_vec3f_s translation)
{
matrix.values[0][2] += translation.x;
matrix.values[1][2] += translation.y;
matrix.values[2][2] += translation.z;
return matrix;
}
tc_mat4f_s tc_mat4f_perspective(float fov, float aspect_ratio, float near, float far)
{
float range = tanf(fov / 2) * near;
float scale_x = (2 * near) / (range * aspect_ratio * 2);
float scale_y = near / range;
float scale_z = -(far + near) / (far - near);
float position_z = -(2 * far * near) / (far - near);
tc_mat4f_s matrix = tc_mat4f_zero();
matrix.values[0][0] = scale_x;
matrix.values[1][1] = scale_y;
matrix.values[2][2] = scale_z;
matrix.values[3][2] = -1.0f;
matrix.values[2][3] = position_z;
return matrix;
}

View File

@ -0,0 +1,23 @@
#ifndef TC_UTILITY_H
#define TC_UTILITY_H
typedef unsigned char u8_t;
typedef unsigned short u16_t;
typedef unsigned int u32_t;
typedef unsigned long u64_t;
typedef signed char i8_t;
typedef signed short i16_t;
typedef signed int i32_t;
typedef signed long i64_t;
typedef float f32_t;
typedef double f64_t;
typedef unsigned char bool_t;
#define TRUE 1
#define FALSE 0
#endif

View File

@ -0,0 +1,52 @@
#include <utility/math.h>
bool_t tc_vec4f_equ(tc_vec4f_s vector_1, tc_vec4f_s vector_2)
{
if(vector_1.x != vector_2.x) return FALSE;
if(vector_1.y != vector_2.y) return FALSE;
if(vector_1.z != vector_2.z) return FALSE;
if(vector_1.w != vector_2.w) return FALSE;
return TRUE;
}
bool_t tc_vec3f_equ(tc_vec3f_s vector_1, tc_vec3f_s vector_2)
{
if(vector_1.x != vector_2.x) return FALSE;
if(vector_1.y != vector_2.y) return FALSE;
if(vector_1.z != vector_2.z) return FALSE;
return TRUE;
}
bool_t tc_vec2f_equ(tc_vec2f_s vector_1, tc_vec2f_s vector_2)
{
if(vector_1.x != vector_2.x) return FALSE;
if(vector_1.y != vector_2.y) return FALSE;
return TRUE;
}
bool_t tc_vec4i_equ(tc_vec4i_s vector_1, tc_vec4i_s vector_2)
{
if(vector_1.x != vector_2.x) return FALSE;
if(vector_1.y != vector_2.y) return FALSE;
if(vector_1.z != vector_2.z) return FALSE;
if(vector_1.w != vector_2.w) return FALSE;
return TRUE;
}
bool_t tc_vec3i_equ(tc_vec3i_s vector_1, tc_vec3i_s vector_2)
{
if(vector_1.x != vector_2.x) return FALSE;
if(vector_1.y != vector_2.y) return FALSE;
if(vector_1.z != vector_2.z) return FALSE;
return TRUE;
}
bool_t tc_vec2i_equ(tc_vec2i_s vector_1, tc_vec2i_s vector_2)
{
if(vector_1.x != vector_2.x) return FALSE;
if(vector_1.y != vector_2.y) return FALSE;
return TRUE;
}

View File

@ -1,9 +0,0 @@
#include "utility.h"
bool tc_vec3i_equ(tc_vec3i_s first, tc_vec3i_s second)
{
if(first.x != second.x) return false;
if(first.y != second.y) return false;
if(first.z != second.z) return false;
return true;
}

View File

@ -1,10 +1,10 @@
#include "world.h" #include <world.h>
#include "state.h" #include <state.h>
#include <linmath.h>
#include <stdlib.h> #include <stdlib.h>
tc_worldgen_s tc_default_terrain_generator_g; tc_worldgen_s tc_default_terrain_generator_g;
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)
{ {
@ -36,17 +36,23 @@ void tc_upload_chunk(tc_chunk_s *chunk)
); );
glBindVertexArray(0); glBindVertexArray(0);
} }
void tc_draw_chunk(tc_chunk_s *chunk, tc_world_s *world)
void tc_draw_chunk_entity(tc_entity_s *entity)
{ {
mat4x4 model_matrix; tc_chunk_s *chunk = entity->specific;
mat4x4_identity(model_matrix);
mat4x4_translate_in_place(model_matrix, chunk->position.x*32, chunk->position.y*32, -chunk->position.z*32);
int model_matrix_uniform_location = tc_mat4f_s model_matrix = tc_mat4f_identity();
glGetUniformLocation(tc_game_state_g.renderer.draw_shader.program_id, "model_matrix");
glUniformMatrix4fv(model_matrix_uniform_location, 1, GL_FALSE, &model_matrix[0][0]); tc_vec3f_s position_3f;
position_3f.x = chunk->position.x * 32;
position_3f.y = chunk->position.y * 32;
position_3f.z = chunk->position.z * 32;
model_matrix =
tc_mat4f_translate(model_matrix, position_3f);
tc_shader_uniform_mat4f(&tc_game_state_g.renderer.draw_shader, "model_matrix", model_matrix);
// glBindVertexArray(chunk->vao);
glBindBuffer(GL_ARRAY_BUFFER, chunk->vertex_data); glBindBuffer(GL_ARRAY_BUFFER, chunk->vertex_data);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *) 0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *) 0);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
@ -76,8 +82,77 @@ void tc_init_world_generators()
tc_default_terrain_generator_g.fn_generate_chunk = &tc_generate_default_terrain_chunk; tc_default_terrain_generator_g.fn_generate_chunk = &tc_generate_default_terrain_chunk;
} }
tc_entity_s * tc_create_chunk_entity()
{
tc_entity_s *chunk = tc_allocate_entity();
tc_set_string_for_entity(chunk, "type", "chunk");
tc_set_integer_for_entity(chunk, "grid_x", 0);
tc_set_integer_for_entity(chunk, "grid_y", 0);
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_teleport_chunk_entity(tc_entity_s *entity, tc_location_s location)
{
tc_chunk_s *chunk = entity->specific;
chunk->world = location.world;
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_free_chunk(entity->specific);
}
void tc_receive_chunk_entity_event(tc_entity_s *entity, tc_entity_event_s event)
{
}
void tc_add_chunk_entity_type()
{
tc_fn_entity_s chunk_functions;
chunk_functions.fn_create = &tc_create_chunk_entity;
chunk_functions.fn_spawn = &tc_spawn_chunk_entity;
chunk_functions.fn_teleport = &tc_teleport_chunk_entity;
chunk_functions.fn_draw = &tc_draw_chunk_entity;
chunk_functions.fn_delete = &tc_delete_chunk_entity;
chunk_functions.fn_send_event = &tc_receive_chunk_entity_event;
tc_entity_type_s chunk_type;
chunk_type.internal_name = "chunk";
chunk_type.display_name = NULL;
chunk_type.functions = chunk_functions;
tc_register_entity_type(chunk_type);
tc_chunk_entity_type_g = tc_get_entity_type_with_name("chunk");
}
void tc_init_worlds() void tc_init_worlds()
{ {
tc_init_chunk_pool(256);
tc_add_chunk_entity_type();
tc_init_world_generators(); tc_init_world_generators();
} }
@ -91,14 +166,10 @@ void tc_set_block_in_chunk(
chunk->blocks[x][y][z] = block.type_identifier; chunk->blocks[x][y][z] = block.type_identifier;
} }
void tc_draw_world(tc_world_s *world) void tc_draw_world(tc_world_s *world)
{ {
for(uint32_t loader_index = 0; loader_index < world->num_loaders; ++loader_index) tc_draw_all_entities_of_type("chunk");
for(uint32_t chunk_index = 0; chunk_index < world->loaders[loader_index].num_chunks; ++chunk_index)
tc_draw_chunk(world->loaders[loader_index].chunks[chunk_index], world);
} }
tc_chunkloader_s tc_create_chunkloader(tc_world_s *world) tc_chunkloader_s tc_create_chunkloader(tc_world_s *world)
{ {
tc_chunkloader_s loader; tc_chunkloader_s loader;
@ -128,7 +199,6 @@ tc_chunkloader_s tc_create_spawn_loader(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_world_s *world = calloc(sizeof(tc_world_s), 1); tc_world_s *world = calloc(sizeof(tc_world_s), 1);
world->pool = tc_new_chunk_pool(512);
world->worldgen = &tc_default_terrain_generator_g; world->worldgen = &tc_default_terrain_generator_g;
world->num_loaders = 1; world->num_loaders = 1;
world->loaders = malloc(sizeof(tc_chunkloader_s) * world->num_loaders); world->loaders = malloc(sizeof(tc_chunkloader_s) * world->num_loaders);

View File

@ -5,8 +5,8 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include "blocks.h" #include <blocks.h>
#include "utility.h" #include <utility/math.h>
#define UPDATE_DISTANCE 2 #define UPDATE_DISTANCE 2
@ -30,14 +30,14 @@ struct tc_worldgen
struct tc_block struct tc_block
{ {
uint32_t type_identifier; uint32_t type_identifier;
tc_vec3_s position; tc_vec3f_s position;
tc_vec3_s rotation; tc_vec3f_s rotation;
tc_object_s drawing;
}; };
struct tc_chunk struct tc_chunk
{ {
tc_world_s *world;
tc_chunk_pool_entry_s *pool_entry; tc_chunk_pool_entry_s *pool_entry;
tc_vec3i_s position; tc_vec3i_s position;
@ -75,6 +75,8 @@ typedef struct tc_chunkloader
struct tc_chunk_pool struct tc_chunk_pool
{ {
tc_world_s *world;
uint32_t capacity; uint32_t capacity;
uint32_t used_entries; uint32_t used_entries;
tc_chunk_pool_entry_s *entries; tc_chunk_pool_entry_s *entries;
@ -130,9 +132,9 @@ void tc_on_each_loaded_chunk (
void *userdata void *userdata
); );
tc_chunk_pool_s * tc_new_chunk_pool (uint32_t capacity); void tc_init_chunk_pool (uint32_t capacity);
tc_chunk_s * tc_allocate_chunk (tc_world_s *world); tc_chunk_s * tc_allocate_chunk ();
void tc_free_chunk (tc_world_s *world, tc_chunk_s *chunk); void tc_free_chunk (tc_chunk_s *chunk);
#endif #endif