Techneck/code/source-c/world.h

141 lines
4.1 KiB
C

#ifndef TC_WORLD_H
#define TC_WORLD_H
#include <stdbool.h>
#include <stdint.h>
#include <blocks.h>
#include <utility/math.h>
#define UPDATE_DISTANCE 2
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;
typedef struct tc_chunk_pool tc_chunk_pool_s;
typedef struct tc_chunk_pool_entry tc_chunk_pool_entry_s;
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;
};
struct tc_chunk
{
tc_world_s *world;
tc_chunk_pool_entry_s *pool_entry;
tc_vec3i_s position;
uint32_t blocks[32][32][32];
uint32_t num_vertices;
float *vertex_positions;
float *vertex_uvs;
uint32_t vao;
uint32_t vertex_data;
};
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
{
tc_vec3i_s center;
tc_vec3i_s extent;
uint32_t chunks_capacity;
uint32_t num_chunks;
tc_chunk_s **chunks;
tc_world_s *world;
bool needs_reload;
} tc_chunkloader_s;
struct tc_chunk_pool
{
tc_world_s *world;
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;
tc_worldgen_s *worldgen;
uint32_t num_loaders;
tc_chunkloader_s *loaders;
// chunk_loading_center: The center of loading ON THE CHUNK GRID COORDINATES
tc_chunkloader_s *spawn_loader;
};
extern tc_worldgen_s tc_default_terrain_generator_g;
void tc_init_worlds ();
void tc_draw_world (tc_world_s *world);
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
void tc_set_block_in_chunk(
tc_chunk_s *chunk,
uint8_t x, uint8_t y, uint8_t z,
tc_block_s block
);
uint32_t tc_count_chunk_vertices (tc_chunk_s *chunk);
void tc_meshize_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);
void tc_add_chunk_to_loader (tc_chunkloader_s *chunkloader, tc_chunk_s *chunk);
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
);
void tc_init_chunk_pool (uint32_t capacity);
tc_chunk_s * tc_allocate_chunk ();
void tc_free_chunk (tc_chunk_s *chunk);
#endif