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/engine.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/engine.c')
| -rw-r--r-- | src/engine.c | 82 |
1 files changed, 51 insertions, 31 deletions
diff --git a/src/engine.c b/src/engine.c index ed2b6c2..2d509f3 100644 --- a/src/engine.c +++ b/src/engine.c @@ -1,11 +1,13 @@ #include "engine.h" #include "block.h" #include "camera.h" +#include "cglm/io.h" #include "chunk.h" #include "input.h" #include "window.h" #include "world.h" #include <junk/vector.h> +#include <string.h> #include <time.h> void _engine_insert_chunk_ptrs(struct engine* engine, struct chunk* chunk); @@ -31,42 +33,49 @@ int engine_init(struct engine *engine) { engine->shader = shader; // Setup Objects to draw - vector_init(&engine->objects); - // Setup root chunk - struct world* world; - world_init(time(NULL), &world); - vec2 curr_chunk = { 0, 0 }; - int chunk_distance = 2; - for (int i = 0; i < chunk_distance; i++) { - for (int j = 0; j < chunk_distance; j++) { - struct chunk* chunk; - vec2 chunk_coord = { curr_chunk[0] + i, curr_chunk[1] + j }; - world_get_chunk(world, chunk_coord, &chunk); - _engine_insert_chunk_ptrs(engine, chunk); - } - } - + // memset(engine->loaded_chunks, 0, (1 + CHUNK_DISTANCE * 2) * (1 + CHUNK_DISTANCE * 2)); // Setup camera camera_init(&engine->camera); - vec3 camera_pos = { 0.0f, 5.0f, 0.0f }; + vec3 camera_pos = { 0.0f, 15.0f, 0.0f }; camera_set_position(engine->camera, camera_pos); + // Setup root chunk + struct world* world; + world_init(time(NULL), &world); + engine->world = world; // Final step - Start the game engine->game_loop = 1; return 0; } -void _engine_insert_chunk_ptrs(struct engine* engine, struct chunk* chunk) { - int counter = 0; - for (int i = 0; i < CHUNK_WIDTH; i++) { - for (int j = 0; j < CHUNK_LENGTH; j++) { - for (int k = 0; k < CHUNK_HEIGHT; k++) { - struct block* blk = chunk->blocks[i][j][k]; - if (blk == NULL) { - continue; - } - if (VECTOR_INSERT(engine->objects, (void*)blk) == -1) exit(1); - counter += 1; + +void engine_update(struct engine* engine) { + int curr_chunk[2] = { (engine->camera->position[0] / CHUNK_WIDTH), (engine->camera->position[2]) / CHUNK_LENGTH }; + // Chunk update + struct chunk* c = {0}; + int coord[2] = { curr_chunk[0], curr_chunk[1] }; + world_get_chunk(engine->world, coord, &c); + + // We moved a chunk - load new chunks with chunk_load + if (engine->curr_chunk[0] != curr_chunk[0] || engine->curr_chunk[1] != curr_chunk[1]) { + fprintf(stderr, "CHUNK Update! From (%d, %d) to (%d, %d)\n", + engine->curr_chunk[0], + engine->curr_chunk[1], + curr_chunk[0], + curr_chunk[1]); + // Update the curr_chunk + memcpy(engine->curr_chunk, curr_chunk, sizeof(vec2)); + // Load chunks of CHUNK_DISTANCE around curr_chunk + for (int i = -CHUNK_DISTANCE; i <= CHUNK_DISTANCE; i++) { + for (int j = -CHUNK_DISTANCE; j <= CHUNK_DISTANCE; j++) { + struct chunk* chunk; + int chunk_coord[2] = { engine->curr_chunk[0] + i, engine->curr_chunk[1] + j }; + world_get_chunk(engine->world, chunk_coord, &chunk); + // Get "real" coords as in non-negative numbers, that can go in a array + int real_coord[2] = { i + CHUNK_DISTANCE, j + CHUNK_DISTANCE }; + // engine->loaded_chunks[real_coord[0]][real_coord[1]] = chunk; + // Load chunk + chunk_load(chunk, chunk_coord); } } } @@ -79,10 +88,21 @@ void engine_draw(struct engine* engine) { glEnable(GL_DEPTH_TEST); //glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); glUseProgram(engine->shader->program); - for (int i = 0; i < vector_length(engine->objects); i++) { - struct block* block = vector_get(engine->objects, i); - camera_update(engine->camera, engine->shader); - block_draw(block, engine->shader); + // Update engine managed objects + engine_update(engine);//(1 + CHUNK_DISTANCE * 2) * (1 + CHUNK_DISTANCE * 2) + camera_update(engine->camera, engine->shader); + for (int i = -CHUNK_DISTANCE; i <= CHUNK_DISTANCE; i++) { + for (int j = -CHUNK_DISTANCE; j <= CHUNK_DISTANCE; j++) { + struct chunk* chunk = {0}; + // // Load chunk + // Ensure the y coordinate is negative, because in OpenGL +z-axis (y in chunk system) is towards + // user, so we want inwards to be positive, so flip sign + int chunk_coord[2] = { engine->curr_chunk[0] + i, -engine->curr_chunk[1] + j }; + world_get_chunk(engine->world, chunk_coord, &chunk); + // Load chunk + chunk_load(chunk, chunk_coord); + chunk_draw(chunk, engine->shader); + } } SDL_RenderPresent(engine->window->renderer); } |
