diff options
| author | Aaditya Dhruv <[email protected]> | 2026-01-30 00:17:39 -0600 |
|---|---|---|
| committer | Aaditya Dhruv <[email protected]> | 2026-01-30 00:17:39 -0600 |
| commit | 8150b91d4076d15f8df5cd66acc1b8076a2ee1a9 (patch) | |
| tree | fe74fd18e409a215ed05074efe29b3b0a465bfd5 /src/world.c | |
| parent | 3729fe29b862a8b1d58967c45942535e7087b73b (diff) | |
Update chunk rendering, improve chunk loading
- On world_init, LOAD_CHUNK amount of chunks are preloaded
- Chunks are loaded around the player's current chunk, in a square
shape. The size of the shape is controlled by CHUNK_DISTANCE
- Allow chunks struct to be independent of a position. We load a chunk
TO a position in the world chunk grid (x, y). This allows us to "wrap"
chunks, so we have an endless world, but we are really just wrapping
around
Diffstat (limited to 'src/world.c')
| -rw-r--r-- | src/world.c | 37 |
1 files changed, 28 insertions, 9 deletions
diff --git a/src/world.c b/src/world.c index 72a0c21..41fdd17 100644 --- a/src/world.c +++ b/src/world.c @@ -1,29 +1,48 @@ #include "world.h" +#include "cglm/io.h" +#include "chunk.h" #include <stdlib.h> #include <string.h> +// LOAD_DISTANCE determines how many chunks are loaded on world creation +#define LOAD_DISTANCE 4 + int world_init(int32_t seed, struct world** world) { srand(seed); struct world* wld = malloc(sizeof(struct world)); memset(wld, 0, sizeof(struct world)); wld->seed = seed; //TODO: Improve loading here - for (int i = 0; i < 2; i++) { - for (int j = 0; j < 2; j++) { + for (int i = 0; i < LOAD_DISTANCE; i++) { + for (int j = 0; j < LOAD_DISTANCE; j++) { struct chunk* chunk; - vec2 coords = { i, j }; - chunk_gen(wld, coords, &chunk); - wld->chunks[i][j] = chunk; - + int coords[2] = { i, j }; + world_get_chunk(wld, coords, &chunk); } } *world = wld; return 0; } -int world_get_chunk(struct world* world, vec2 coord, struct chunk** chunk) { +int world_get_chunk(struct world* world, int coord[2], struct chunk** chunk) { + int w = ((abs(coord[0]) / WORLD_WIDTH) + 1) * WORLD_WIDTH; + int l = ((abs(coord[1]) / WORLD_LENGTH) + 1) * WORLD_LENGTH; + int x = (coord[0] + w) % WORLD_WIDTH; + int y = (coord[1] + l) % WORLD_LENGTH; + vec2 new_coord = { x, y }; + struct chunk* c = world->chunks[x][y]; + if (c != NULL) { + *chunk = c; + } else { + chunk_gen(world, new_coord, chunk); + world->chunks[x][y] = *chunk; + } + return 0; +} + +void world_get_chunk_real_coord(struct world* world, vec2 coord, int out[2]) { int x = (int)coord[0] % WORLD_WIDTH; int y = (int)coord[1] % WORLD_LENGTH; - *chunk = world->chunks[x][y]; - return 0; + out[0] = x; + out[1] = y; } |
