From bec43c4093e87e852c27f123ac5b16cd3c3ab9d7 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Sat, 3 Apr 2021 08:53:18 +1100 Subject: [PATCH] Chunk loading finalized more or less --- src/engine/engine.c | 32 ++++++------ src/engine/world/chunk.c | 17 +++++++ src/engine/world/chunk.h | 19 ++++++- src/engine/world/chunklist.c | 97 ++++++++++++++++++++++++++---------- src/engine/world/chunklist.h | 8 +-- src/engine/world/world.c | 18 +++++++ src/engine/world/world.h | 14 ++++++ 7 files changed, 162 insertions(+), 43 deletions(-) create mode 100644 src/engine/world/chunk.c create mode 100644 src/engine/world/world.c create mode 100644 src/engine/world/world.h diff --git a/src/engine/engine.c b/src/engine/engine.c index b33c293a..edda3046 100644 --- a/src/engine/engine.c +++ b/src/engine/engine.c @@ -10,12 +10,13 @@ #include "display/primitives/quad.h" #include "display/texture.h" #include "display/shader.h" +#include "world/chunklist.h" camera_t *camera; shader_t *shader; - -primitive_t *primitive; -texture_t *texture; +chunklist_t *list; +// primitive_t *primitive; +// texture_t *texture; engine_t * engineInit(platform_t *platform, char *name, uint32_t inputCount) { // Create the engine instance. @@ -50,17 +51,20 @@ engine_t * engineInit(platform_t *platform, char *name, uint32_t inputCount) { cameraPerspective(camera, 45.0f, 1920.0f/1080.0f, 3.0f, 100.0f); shaderUseCamera(shader, camera); - // Test - primitive = quadCreate( - -1, -1, 0, 0, - 1, 1, 1, 1 - ); + list = chunkListCreate(3, 3, 3); + chunkListShift(list, -1, 0, 0); - int32_t w = 1, h = 1; - pixel_t *p = calloc(w * h, sizeof(pixel_t)); - p[0].r=0xFF, p[0].g=0x00, p[0].b=0x00, p[0].a = 0xFF; - texture = textureCreate(w, h, NULL); - textureBufferPixels(texture, 0, 0, w, h, p); + // Test + // primitive = quadCreate( + // -1, -1, 0, 0, + // 1, 1, 1, 1 + // ); + + // int32_t w = 1, h = 1; + // pixel_t *p = calloc(w * h, sizeof(pixel_t)); + // p[0].r=0xFF, p[0].g=0x00, p[0].b=0x00, p[0].a = 0xFF; + // texture = textureCreate(w, h, NULL); + // textureBufferPixels(texture, 0, 0, w, h, p); return engine; } @@ -69,7 +73,7 @@ uint32_t engineUpdate(engine_t *engine) { shaderUse(shader); renderFrame(engine->render); - primitiveDraw(primitive, 0, 6); + // primitiveDraw(primitive, 0, 6); inputUpdate(engine->input); diff --git a/src/engine/world/chunk.c b/src/engine/world/chunk.c new file mode 100644 index 00000000..924c29a4 --- /dev/null +++ b/src/engine/world/chunk.c @@ -0,0 +1,17 @@ +/** + * Copyright (c) 2021 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "chunk.h" +#include + + +void chunkLoad(chunk_t *chunk, int32_t x, int32_t y, int32_t z) { +} + +void chunkUnload(chunk_t *chunk) { + +} \ No newline at end of file diff --git a/src/engine/world/chunk.h b/src/engine/world/chunk.h index 34769f4c..f97a4bfb 100644 --- a/src/engine/world/chunk.h +++ b/src/engine/world/chunk.h @@ -8,4 +8,21 @@ typedef struct { int32_t x, y, z; -} chunk_t; \ No newline at end of file +} chunk_t; + +/** + * Loads a given chunk into the memory specified. + * + * @param chunk Chunk to load into. + * @param x X of the chunk. + * @param y Y of the chunk. + * @param z Z of the chunk. + */ +void chunkLoad(chunk_t *chunk, int32_t x, int32_t y, int32_t z); + +/** + * Unload a given chunk. + * + * @param chunk Chunk to unload. + */ +void chunkUnload(chunk_t *chunk); \ No newline at end of file diff --git a/src/engine/world/chunklist.c b/src/engine/world/chunklist.c index 79a54562..a9bbbfe3 100644 --- a/src/engine/world/chunklist.c +++ b/src/engine/world/chunklist.c @@ -9,7 +9,7 @@ chunklist_t * chunkListCreate(int32_t width, int32_t height, int32_t depth) { chunklist_t *list; - int32_t i; + int32_t i, x, y, z; list = malloc(sizeof(chunklist_t)); if(!list) return NULL; @@ -18,14 +18,40 @@ chunklist_t * chunkListCreate(int32_t width, int32_t height, int32_t depth) { list->x = 0, list->y = 0, list->z = 0; list->count = width * height * depth; - //Empty chunks. - list->chunks = calloc(list->count, sizeof(chunk_t)); - + //Create chunks + list->chunks = malloc(list->count * sizeof(chunk_t)); if(list->chunks == NULL) { free(list); return NULL; } + list->chunkList = malloc(list->count * sizeof(chunk_t *)); + if(list->chunkList == NULL) { + free(list->chunks); + free(list); + return NULL; + } + + // Load initial chunks, ZYX order is necessary. + i = 0; + for(z = 0; z < width; z++) { + for(y = 0; y < height; y++) { + for(x = 0; x < depth; x++) { + chunk_t *chunk = list->chunks + i; + list->chunkList[i] = chunk; + + // Load initial coordinates + chunk->x = x; + chunk->y = y; + chunk->z = z; + + chunkLoad(chunk, x, y, z); + + i++; + } + } + } + return list; } @@ -35,41 +61,62 @@ void chunkListDispose(chunklist_t *list) { } void chunkListShift(chunklist_t *list, int32_t x, int32_t y, int32_t z) { - int32_t i, lx, ly, lz, nx, ny, nz, ni, wh; + int32_t i, + /** New List Coordinates */ + lx, ly, lz, + /** New Local Chunk Coordinates */ + nx, ny, nz, + /** New Absolute Chunk Coordinates */ + ax, ay, az, + /** New Chunk Index */ + ni, + /** Precalculated width * height */ + wh + ; chunk_t *chunk; - chunk_t **chunks; + chunk_t **chunkList; // Calculate the new chunklist coordinates - lx = list->x - x; - ly = list->y - y; - lz = list->z - z; + lx = list->x + x; + ly = list->y + y; + lz = list->z + z; // Precalc width * height. wh = list->width * list->height; - chunks = calloc(list->count, sizeof(chunk_t)); + chunkList = malloc(list->count * sizeof(chunk_t *)); for(i = 0; i < list->count; i++) { - chunk = list->chunks[i]; - if(chunk == NULL) continue; + chunk = list->chunkList[i]; // Calculate the new local positions for the chunk. - nx = chunk->x - list->x - x;// chunk abs x - list abs x - move x - ny = chunk->y - list->y - y; - nz = chunk->z - list->z - y; + nx = (chunk->x - list->x - x) % list->width; + ny = (chunk->y - list->y - y) % list->height; + nz = (chunk->z - list->z - z) % list->depth; - // Set the chunks' new absolute positions - chunk->x = nx + lx; - chunk->y = ny + ly; - chunk->z = nz + lz; - - // Now here we can actually determine which chunk(s) were moved. + // Load the chunk if we need to. We also use this to calculate new absolutes + if( + (ax = lx + nx) != chunk->x || + (ay = ly + ny) != chunk->y || + (az = lz + nz) != chunk->z + ) { + // Load new chunk. + chunkUnload(chunk); + chunkLoad(chunk, ax, ay, az); + chunk->x = ax; + chunk->y = ay; + chunk->z = az; + } //Now, based off those new local positions, calculate the new index. - ni = nx + (ny * list->width) + (nz * wh); - chunks[ni] = chunk; + ni = ( + nx + + (ny * list->width) + + (nz * wh) + ); + chunkList[ni] = chunk; } // Now copy that array over. - memcpy(list->chunks, chunks, sizeof(chunk_t) * list->count); - free(chunks);//cleanup + memcpy(list->chunkList, chunkList, sizeof(chunk_t *) * list->count); + free(chunkList); } \ No newline at end of file diff --git a/src/engine/world/chunklist.h b/src/engine/world/chunklist.h index 0a4f4630..b3ddd456 100644 --- a/src/engine/world/chunklist.h +++ b/src/engine/world/chunklist.h @@ -6,7 +6,6 @@ #pragma once #include #include -#include #include "chunk.h" #define CHUNK_INDEX_NULL = 0 @@ -21,8 +20,11 @@ typedef struct { /** Count of chunks in the internal array */ int32_t count; - /** Chunks in the list */ - int32_t **chunks; + /** Current order of each chunk in the list. */ + chunk_t **chunkList; + + /** The actual chunks in the list */ + chunk_t *chunks; } chunklist_t; /** diff --git a/src/engine/world/world.c b/src/engine/world/world.c new file mode 100644 index 00000000..ecfe63be --- /dev/null +++ b/src/engine/world/world.c @@ -0,0 +1,18 @@ +/** + * Copyright (c) 2021 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "world.h" + +world_t * worldCreate() { + world_t *world = malloc(sizeof(world_t)); + + return world; +} + +void worldDispose(world_t *world) { + free(world); +} \ No newline at end of file diff --git a/src/engine/world/world.h b/src/engine/world/world.h new file mode 100644 index 00000000..2d27176c --- /dev/null +++ b/src/engine/world/world.h @@ -0,0 +1,14 @@ +// Copyright (c) 2021 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "chunklist.h" + +typedef struct { + uint32_t x; +} world_t; + +world_t * worldCreate(); +void worldDispose(world_t *world); \ No newline at end of file