Added world chunking.

This commit is contained in:
2021-03-22 22:29:16 +11:00
parent d370ca895b
commit f4bf05da62
6 changed files with 102 additions and 11 deletions

58
src/engine/world/chunk.c Normal file
View File

@ -0,0 +1,58 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "chunk.h"
chunklist_t * chunksCreate(uint32_t width,uint32_t height, uint32_t depth) {
chunklist_t *chunks;
uint32_t i;
chunks = malloc(sizeof(chunklist_t));
if(!chunks) return NULL;
chunks->width = width;
chunks->height = height;
chunks->depth = depth;
chunks->count = width * height * depth;
chunks->indexes = malloc(sizeof(uint32_t) * chunks->count);
if(!chunks->indexes) {
free(chunks);
return NULL;
}
for(i = 0; i < chunks->count; i++) chunks->indexes[i] = i;
return chunks;
}
void chunksDispose(chunklist_t *list) {
free(list->indexes);
free(list);
}
void chunksShift(chunklist_t *list, uint32_t direction[3]) {
uint32_t i, x, y, z, my, mz;
// Precalculate these values
my = list->width * list->height;
mz = my * list->depth;
// For each chunk
for(i = 0; i < list->count; i++) {
// Precalculate, store here for now.
x = i / list->width;
// Calculate the new xyz for the current index.
y = (x + direction[0]) % list->height;
z = (x / list->height + direction[1]) % list->depth;
x = (i + direction[2]) % list->width;
// Set back into the indexes array.
list->indexes[i] = (x * list->width) + (y * my) + (z * mz);
}
}

39
src/engine/world/chunk.h Normal file
View File

@ -0,0 +1,39 @@
// Copyright (c) 2021 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include <stdint.h>
#include <malloc.h>
typedef struct {
uint32_t width, height, depth;
uint32_t count;
uint32_t *indexes;
} chunklist_t;
/**
* Create a new chunk manager.
*
* @param width Width of the chunk list.
* @param height Height of the chunk list.
* @param depth Depth of the chunk list.
* @returns The new chunk list manager
*/
chunklist_t * chunksCreate(uint32_t width,uint32_t height, uint32_t depth);
/**
* Cleans up a previously created chunk list.
*
* @param list List to dispose.
*/
void chunksDispose(chunklist_t *list);
/**
* Shift the chunk list along a set of axis.
*
* @param list List to shift.
* @param direction Array of directions to shift along.
*/
void chunksShift(chunklist_t *list, uint32_t direction[3]);