Techneck/code/source-c/world.h

141 lines
4.1 KiB
C
Raw Normal View History

2023-10-11 08:10:06 +00:00
#ifndef TC_WORLD_H
#define TC_WORLD_H
#include <stdbool.h>
#include <stdint.h>
#include <blocks.h>
#include <utility/math.h>
2023-10-11 08:10:06 +00:00
2023-10-14 08:47:21 +00:00
#define UPDATE_DISTANCE 2
2023-10-11 08:10:06 +00:00
typedef struct tc_worldgen tc_worldgen_s;
typedef struct tc_block tc_block_s;
typedef struct tc_chunk tc_chunk_s;
typedef struct tc_world tc_world_s;
2023-10-12 16:52:05 +00:00
typedef struct tc_chunk_pool tc_chunk_pool_s;
typedef struct tc_chunk_pool_entry tc_chunk_pool_entry_s;
2023-10-11 08:10:06 +00:00
struct tc_worldgen
{
char *name;
bool (*fn_generate_chunk) (tc_worldgen_s *gen, tc_chunk_s *chunk);
};
struct tc_block
{
uint32_t type_identifier;
tc_vec3f_s position;
tc_vec3f_s rotation;
2023-10-11 08:10:06 +00:00
};
struct tc_chunk
{
2023-10-14 19:17:30 +00:00
tc_world_s *world;
2023-10-14 08:47:21 +00:00
tc_chunk_pool_entry_s *pool_entry;
2023-10-12 16:52:05 +00:00
2023-10-13 06:00:53 +00:00
tc_vec3i_s position;
uint32_t blocks[32][32][32];
2023-10-11 08:10:06 +00:00
uint32_t num_vertices;
float *vertex_positions;
float *vertex_uvs;
2023-10-11 08:10:06 +00:00
uint32_t vao;
uint32_t vertex_data;
};
2023-10-12 16:52:05 +00:00
struct tc_chunk_pool_entry
{
tc_chunk_pool_entry_s *next;
tc_chunk_pool_entry_s *previous;
tc_chunk_s chunk;
};
typedef struct tc_chunkloader
2023-10-11 08:10:06 +00:00
{
2023-10-13 06:00:53 +00:00
tc_vec3i_s center;
tc_vec3i_s extent;
2023-10-12 16:52:05 +00:00
2023-10-11 08:10:06 +00:00
uint32_t chunks_capacity;
uint32_t num_chunks;
tc_chunk_s **chunks;
2023-10-13 06:00:53 +00:00
tc_world_s *world;
2023-10-11 08:10:06 +00:00
2023-10-12 16:52:05 +00:00
bool needs_reload;
} tc_chunkloader_s;
struct tc_chunk_pool
{
2023-10-14 19:17:30 +00:00
tc_world_s *world;
2023-10-12 16:52:05 +00:00
uint32_t capacity;
uint32_t used_entries;
tc_chunk_pool_entry_s *entries;
tc_chunk_pool_entry_s *first_free;
tc_chunk_pool_s *continuation;
};
struct tc_world
{
// tc_chunk_s *center_chunk;
tc_chunk_pool_s *pool;
2023-10-11 08:10:06 +00:00
tc_worldgen_s *worldgen;
2023-10-12 16:52:05 +00:00
uint32_t num_loaders;
tc_chunkloader_s *loaders;
// chunk_loading_center: The center of loading ON THE CHUNK GRID COORDINATES
2023-10-14 08:47:21 +00:00
tc_chunkloader_s *spawn_loader;
2023-10-11 08:10:06 +00:00
};
2023-10-12 16:52:05 +00:00
extern tc_worldgen_s tc_default_terrain_generator_g;
void tc_init_worlds ();
2023-10-11 08:10:06 +00:00
void tc_draw_world (tc_world_s *world);
2023-10-12 16:52:05 +00:00
tc_world_s * tc_new_world (tc_worldgen_s *generator);
tc_chunk_s * tc_new_chunk (tc_world_s *world, int32_t x, int32_t y, int32_t z); // must be integers
2023-10-11 08:10:06 +00:00
void tc_set_block_in_chunk(
tc_chunk_s *chunk,
2023-10-12 16:52:05 +00:00
uint8_t x, uint8_t y, uint8_t z,
2023-10-11 08:10:06 +00:00
tc_block_s block
);
uint32_t tc_count_chunk_vertices (tc_chunk_s *chunk);
2023-10-11 08:10:06 +00:00
void tc_meshize_chunk (tc_chunk_s *chunk);
2023-10-14 14:48:09 +00:00
void tc_upload_chunk (tc_chunk_s *chunk);
2023-10-11 08:10:06 +00:00
bool tc_generate_default_terrain_chunk (tc_worldgen_s *gen, tc_chunk_s *chunk);
2023-10-14 14:48:09 +00:00
void tc_add_chunk_to_loader (tc_chunkloader_s *chunkloader, tc_chunk_s *chunk);
2023-10-12 16:52:05 +00:00
tc_chunk_s * tc_load_chunk_at (tc_world_s *world, int32_t x, int32_t y, int32_t z);
tc_chunk_s * tc_get_loaded_chunk_at (tc_world_s *world, int32_t x, int32_t y, int32_t z);
bool tc_chunk_is_loaded (tc_world_s *world, int32_t x, int32_t y, int32_t z);
void tc_update_world (tc_world_s *world);
void tc_on_each_loaded_chunk (
tc_world_s *world,
bool (*fn_do)(tc_chunk_s *chunk, void *userdata),
void *userdata
);
2023-10-14 19:17:30 +00:00
void tc_init_chunk_pool (uint32_t capacity);
tc_chunk_s * tc_allocate_chunk ();
void tc_free_chunk (tc_chunk_s *chunk);
2023-10-12 16:52:05 +00:00
2023-10-11 08:10:06 +00:00
#endif