150 lines
4.4 KiB
C
150 lines
4.4 KiB
C
|
|
#ifndef TC_WORLD_H
|
|
#define TC_WORLD_H
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <blocks.h>
|
|
#include <hook.h>
|
|
#include <physics/simulation.h>
|
|
#include <utility/math.h>
|
|
#include <entity.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
|
|
{
|
|
u32_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;
|
|
u32_t blocks[32][32][32];
|
|
|
|
u32_t num_vertices;
|
|
f32_t *vertex_positions;
|
|
f32_t *vertex_uvs;
|
|
|
|
u32_t vao;
|
|
u32_t vertex_data;
|
|
};
|
|
|
|
struct tc_chunk_pool_entry
|
|
{
|
|
tc_chunk_pool_entry_s *next;
|
|
tc_chunk_pool_entry_s *previous;
|
|
|
|
tc_chunk_s chunk;
|
|
};
|
|
|
|
struct tc_chunk_pool
|
|
{
|
|
tc_world_s *world;
|
|
|
|
u32_t capacity;
|
|
u32_t used_entries;
|
|
tc_chunk_pool_entry_s *entries;
|
|
tc_chunk_pool_entry_s *first_free;
|
|
|
|
tc_chunk_pool_s *continuation;
|
|
};
|
|
|
|
typedef struct tc_chunkloader
|
|
{
|
|
tc_vec3i_s center;
|
|
tc_vec3i_s extent;
|
|
|
|
u32_t chunks_capacity;
|
|
u32_t num_chunks;
|
|
tc_entity_s **chunks;
|
|
tc_world_s *world;
|
|
|
|
bool needs_reload;
|
|
|
|
} tc_chunkloader_s;
|
|
|
|
struct tc_world
|
|
{
|
|
tc_chunk_pool_s *pool;
|
|
tc_worldgen_s *worldgen;
|
|
|
|
u32_t num_loaders;
|
|
tc_chunkloader_s *loaders;
|
|
|
|
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;
|
|
|
|
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, i32_t x, i32_t y, i32_t z); // must be integers
|
|
|
|
void tc_set_block_in_chunk(
|
|
tc_chunk_s *chunk,
|
|
u8_t x, u8_t y, u8_t z,
|
|
tc_block_s block
|
|
);
|
|
|
|
u32_t tc_count_chunk_vertices (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);
|
|
|
|
void tc_add_chunk_to_loader (tc_chunkloader_s *chunkloader, tc_entity_s *entity);
|
|
tc_chunk_s * tc_load_chunk_at (tc_world_s *world, i32_t x, i32_t y, i32_t z);
|
|
tc_chunk_s * tc_get_loaded_chunk_at (tc_world_s *world, i32_t x, i32_t y, i32_t z);
|
|
bool tc_chunk_is_loaded (tc_world_s *world, i32_t x, i32_t y, i32_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_cleanup_chunk_pool ();
|
|
void tc_init_chunk_pool (u32_t capacity);
|
|
tc_chunk_s * tc_allocate_chunk ();
|
|
void tc_deallocate_chunk (tc_chunk_s *chunk);
|
|
|
|
void tc_create_world_physics (tc_world_s *world, void *userdata);
|
|
|
|
#endif
|
|
|