Chunk loading finalized more or less
This commit is contained in:
17
src/engine/world/chunk.c
Normal file
17
src/engine/world/chunk.c
Normal file
@ -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 <stdio.h>
|
||||
|
||||
|
||||
void chunkLoad(chunk_t *chunk, int32_t x, int32_t y, int32_t z) {
|
||||
}
|
||||
|
||||
void chunkUnload(chunk_t *chunk) {
|
||||
|
||||
}
|
@ -8,4 +8,21 @@
|
||||
|
||||
typedef struct {
|
||||
int32_t x, y, z;
|
||||
} chunk_t;
|
||||
} 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);
|
@ -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);
|
||||
}
|
@ -6,7 +6,6 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include <malloc.h>
|
||||
#include <string.h>
|
||||
#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;
|
||||
|
||||
/**
|
||||
|
18
src/engine/world/world.c
Normal file
18
src/engine/world/world.c
Normal file
@ -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);
|
||||
}
|
14
src/engine/world/world.h
Normal file
14
src/engine/world/world.h
Normal file
@ -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);
|
Reference in New Issue
Block a user