Techneck/code/source-c/world.c

84 lines
2.4 KiB
C
Raw Normal View History

#include <world.h>
#include <state.h>
2023-10-11 08:10:06 +00:00
#include <stdlib.h>
2023-10-17 11:25:02 +00:00
void tc_add_chunk_to_loader(tc_chunkloader_s *loader, tc_entity_s *entity);
2023-10-14 19:17:30 +00:00
2023-10-11 08:10:06 +00:00
2023-10-17 11:25:02 +00:00
tc_worldgen_s tc_default_terrain_generator_g;
2023-10-11 08:10:06 +00:00
void tc_init_world_generators()
{
tc_default_terrain_generator_g.name = "Default Terrain Generator";
tc_default_terrain_generator_g.fn_generate_chunk = &tc_generate_default_terrain_chunk;
}
2023-10-14 19:17:30 +00:00
2023-10-12 16:52:05 +00:00
void tc_init_worlds()
2023-10-11 08:10:06 +00:00
{
2023-10-14 19:17:30 +00:00
tc_init_chunk_pool(256);
2023-10-11 08:10:06 +00:00
tc_init_world_generators();
}
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
) {
chunk->blocks[x][y][z] = block.type_identifier;
2023-10-11 08:10:06 +00:00
}
void tc_draw_world(tc_world_s *world)
{
2023-10-17 11:25:02 +00:00
for(u32_t loader_index = 0; loader_index < world->num_loaders; ++loader_index)
{
if(world->loaders[loader_index].is_rendered) tc_draw_chunkloader_zone(&world->loaders[loader_index]);
}
2023-10-12 16:52:05 +00:00
}
tc_chunkloader_s tc_create_spawn_loader(tc_world_s *world)
{
2023-10-17 11:25:02 +00:00
tc_chunk_location_s loader_location;
loader_location.world = world;
loader_location.grid_coords.x = 0.0f;
loader_location.grid_coords.y = 0.0f;
loader_location.grid_coords.z = 0.0f;
tc_chunkloader_s loader = tc_create_chunkloader(loader_location);
loader.is_rendered = TRUE;
2023-10-12 16:52:05 +00:00
return loader;
}
tc_world_s * tc_new_world(tc_worldgen_s *generator)
{
tc_world_s *world = calloc(sizeof(tc_world_s), 1);
2023-10-17 11:25:02 +00:00
world->on_chunk_create = tc_new_hooklist(16);
world->before_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);
world->worldgen = &tc_default_terrain_generator_g;
world->num_loaders = 1;
world->loaders = malloc(sizeof(tc_chunkloader_s) * world->num_loaders);
world->loaders[0] = tc_create_spawn_loader(world);
world->spawn_loader = &world->loaders[0];
tc_add_to_hooklist(&world->after_chunk_generate, (tc_fn_hook) tc_meshize_chunk, NULL);
2023-10-15 12:41:53 +00:00
tc_run_hooklist(&tc_game_state_g.on_world_create, world);
2023-10-11 08:10:06 +00:00
2023-10-17 11:25:02 +00:00
tc_update_world(world);
2023-10-12 16:52:05 +00:00
return world;
2023-10-11 08:10:06 +00:00
}
2023-10-17 11:25:02 +00:00
void tc_unload_world(tc_world_s *world)
{
tc_delete_chunkloader(*world->spawn_loader);
free(world);
}