Built tileset logic

This commit is contained in:
2021-04-05 13:58:17 +10:00
parent 7ce4516399
commit 493477bf0d
12 changed files with 193 additions and 45 deletions

View File

@ -8,38 +8,39 @@
#include "chunk.h"
void chunkCreate(chunk_t *chunk) {
int32_t count, x, y, z, i;
int32_t count, x, y, i;
count = CHUNK_WIDTH * CHUNK_HEIGHT * CHUNK_DEPTH;
chunk->primitive = primitiveCreate(count * 4, count * 6);
i = 0;
for(z = 0; z < CHUNK_DEPTH; z++) {
for(y = 0; y < CHUNK_HEIGHT; y++) {
for(x = 0; x < CHUNK_WIDTH; x++) {
quadBuffer(chunk->primitive,
x, y, 0, 0,
x+1, y+1, 1, 1,
i*4, i*6
);
i++;
}
}
}
chunk->batch = spriteBatchCreate(count);
chunk->tiles = malloc(sizeof(tile_t) * count);
}
void chunkRender(chunk_t *chunk, shader_t *shader) {
// Coordinates of the chunk.
float x, y, z;
x = (chunk->x * CHUNK_WIDTH);
y = (chunk->y * CHUNK_HEIGHT);
z = (chunk->z * CHUNK_DEPTH);
shaderUsePosition(shader, x*2, y*2, z*2, 0, 0, 0);
primitiveDraw(chunk->primitive, 0, chunk->primitive->indiceCount);
// Render the batch.
shaderUsePosition(shader, x, y, z, 0, 0, 0);
spriteBatchDraw(chunk->batch, 0, -1);
}
void chunkLoad(chunk_t *chunk, int32_t x, int32_t y, int32_t z) {
int32_t tx, ty, tz;
spriteBatchFlush(chunk->batch);
// for(tx = 0; tx < CHUNK_WIDTH; tx++) {
// for(ty = 0; ty < CHUNK_HEIGHT; ty++) {
// for(tz = 0; tz < CHUNK_DEPTH; tz++) {
// spriteBatchQuad(chunk->batch, -1,
// x, y, z, 1, 1, 0, 0, 1, 1
// );
// }
// }
// }
}
void chunkUnload(chunk_t *chunk) {

View File

@ -7,7 +7,7 @@
#include <stdint.h>
#include "../display/primitive.h"
#include "../display/shader.h"
#include "../display/primitives/quad.h"
#include "../display/spritebatch.h"
#define CHUNK_WIDTH 3
#define CHUNK_HEIGHT CHUNK_WIDTH
@ -16,9 +16,14 @@
typedef uint8_t tile_t;
typedef struct {
/** Absolute X Y Z chunklist coordinates */
int32_t x, y, z;
/** Tiles within the chunk */
tile_t *tiles;
primitive_t *primitive;
/** Sprite Batch for holding static tiles */
spritebatch_t *batch;
} chunk_t;
@ -29,8 +34,6 @@ typedef struct {
*/
void chunkCreate(chunk_t *chunk);
void chunkRender(chunk_t *chunk, shader_t *shader);
/**
* Loads a given chunk into the memory specified.
*
@ -46,4 +49,8 @@ void chunkLoad(chunk_t *chunk, int32_t x, int32_t y, int32_t z);
*
* @param chunk Chunk to unload.
*/
void chunkUnload(chunk_t *chunk);
void chunkUnload(chunk_t *chunk);
void chunkRender(chunk_t *chunk, shader_t *shader);

View File

@ -90,9 +90,9 @@ void chunkListShift(chunklist_t *list, int32_t x, int32_t y, int32_t z) {
chunk = list->chunkList[i];
// Calculate the new local positions for the chunk.
nx = mod(chunk->x - list->x - x, list->width);
ny = mod(chunk->y - list->y - y, list->height);
nz = mod(chunk->z - list->z - z, list->depth);
nx = mathMod(chunk->x - list->x - x, list->width);
ny = mathMod(chunk->y - list->y - y, list->height);
nz = mathMod(chunk->z - list->z - z, list->depth);
// Load the chunk if we need to. We also use this to calculate new absolutes
if(

View File

@ -11,7 +11,7 @@ world_t * worldCreate() {
world_t *world = malloc(sizeof(world_t));
if(world == NULL) return NULL;
world->chunkList = chunkListCreate(3, 3, 1);
world->chunkList = chunkListCreate(3, 3, 3);
if(world->chunkList == NULL) {
free(world);
return NULL;