Working on tile maps

This commit is contained in:
2021-04-05 20:46:05 +10:00
parent 493477bf0d
commit 351c501140
15 changed files with 273 additions and 67 deletions

View File

@ -4,7 +4,6 @@
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "chunk.h"
void chunkCreate(chunk_t *chunk) {
@ -12,7 +11,6 @@ void chunkCreate(chunk_t *chunk) {
count = CHUNK_WIDTH * CHUNK_HEIGHT * CHUNK_DEPTH;
chunk->batch = spriteBatchCreate(count);
chunk->tiles = malloc(sizeof(tile_t) * count);
}
void chunkRender(chunk_t *chunk, shader_t *shader) {
@ -25,24 +23,4 @@ void chunkRender(chunk_t *chunk, shader_t *shader) {
// 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

@ -5,23 +5,19 @@
#pragma once
#include <stdint.h>
#include "../display/primitive.h"
#include <memory.h>
#include "../display/shader.h"
#include "../display/spritebatch.h"
#include "tile.h"
#define CHUNK_WIDTH 3
#define CHUNK_HEIGHT CHUNK_WIDTH
#define CHUNK_DEPTH CHUNK_HEIGHT
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;
/** Sprite Batch for holding static tiles */
spritebatch_t *batch;
} chunk_t;
@ -35,22 +31,9 @@ typedef struct {
void chunkCreate(chunk_t *chunk);
/**
* Loads a given chunk into the memory specified.
* Render a given chunk.
*
* @param chunk Chunk to load into.
* @param x X of the chunk.
* @param y Y of the chunk.
* @param z Z of the chunk.
* @param chunk Chunk to render
* @param
*/
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);
void chunkRender(chunk_t *chunk, shader_t *shader);

View File

@ -7,7 +7,9 @@
#include "chunklist.h"
chunklist_t * chunkListCreate(int32_t width, int32_t height, int32_t depth) {
chunklist_t * chunkListCreate(int32_t width, int32_t height, int32_t depth,
tilemap_t *tilemap
) {
chunklist_t *list;
chunk_t *chunk;
int32_t i, x, y, z;
@ -18,6 +20,7 @@ chunklist_t * chunkListCreate(int32_t width, int32_t height, int32_t depth) {
list->width = width, list->height = height, list->depth = depth;
list->x = 0, list->y = 0, list->z = 0;
list->count = width * height * depth;
list->tilemap = tilemap;
//Create chunks
list->chunks = malloc(list->count * sizeof(chunk_t));
@ -47,7 +50,7 @@ chunklist_t * chunkListCreate(int32_t width, int32_t height, int32_t depth) {
chunk->z = z;
chunkCreate(chunk);
chunkLoad(chunk, x, y, z);
chunkListChunkLoad(list, chunk, x, y, z);
i++;
}
}
@ -105,8 +108,8 @@ void chunkListShift(chunklist_t *list, int32_t x, int32_t y, int32_t z) {
az = lz + nz;
// Load new chunk.
chunkUnload(chunk);
chunkLoad(chunk, ax, ay, az);
chunkListChunkUnload(list, chunk);
chunkListChunkLoad(list, chunk, ax, ay, az);
// Update the absolute coordinates.
chunk->x = ax;
@ -117,7 +120,7 @@ void chunkListShift(chunklist_t *list, int32_t x, int32_t y, int32_t z) {
// Now, based off those new local positions, calculate the new index.
ni = (
nx +
(ny * list->width) +
(ny * list->width) +
(nz * wh)
);
chunkList[ni] = chunk;
@ -141,4 +144,41 @@ void chunkListAlign(chunklist_t *list, int32_t x, int32_t y, int32_t z) {
lz = z - list->z;
chunkListShift(list, lx, ly, lz);
}
void chunkListChunkLoad(chunklist_t *list, chunk_t *chunk,
int32_t x, int32_t y, int32_t z
) {
int32_t tx, ty, tz, i, count;
tilesetdiv_t *div;
tileref_t *tiles;
tileref_t tile;
count = CHUNK_WIDTH * CHUNK_DEPTH * CHUNK_HEIGHT;
tiles = calloc(count, sizeof(tileref_t));
tiles[0] = 0x01;
// Render the quads.
i = -1;
for(tx = 0; tx < CHUNK_WIDTH; tx++) {
for(ty = 0; ty < CHUNK_HEIGHT; ty++) {
for(tz = 0; tz < CHUNK_DEPTH; tz++) {
tile = tiles[++i];
if(tile == TILE_NULL) continue;
div = &list->tilemap->tileset->divisions[tile];
spriteBatchQuad(chunk->batch, -1,
tx, ty, tz, 1, 1,
div->x0, div->y0, div->x1, div->y1
);
}
}
}
free(tiles);
}
void chunkListChunkUnload(chunklist_t *list, chunk_t *chunk) {
// Flush the sprite batch.
spriteBatchFlush(chunk->batch);
}

View File

@ -6,8 +6,10 @@
#pragma once
#include <stdint.h>
#include <malloc.h>
#include "chunk.h"
#include "./../util/math.h"
#include "chunk.h"
#include "tile.h"
#include "../display/texture.h"
typedef struct {
/** Dimensions of the chunk list */
@ -22,6 +24,9 @@ typedef struct {
/** Current order of each chunk in the list. */
chunk_t **chunkList;
/** Chunk list tile map. */
tilemap_t *tilemap;
/** The actual chunks in the list */
chunk_t *chunks;
} chunklist_t;
@ -33,9 +38,12 @@ typedef struct {
* @param width The width (x axis) of chunks to keep loaded.
* @param height The height (y axis) of chunks to keep loaded.
* @param depth The depth (z axis) of chunks to keep loaded.
* @param tilemap Tilemap of the chunk list.
* @return A new chunk list.
*/
chunklist_t * chunkListCreate(int32_t width, int32_t height, int32_t depth);
chunklist_t * chunkListCreate(int32_t width, int32_t height, int32_t depth,
tilemap_t *tilemap
);
/**
* Disposes and frees a previously created list. This does not free the chunks
@ -63,4 +71,25 @@ void chunkListShift(chunklist_t *list, int32_t x, int32_t y, int32_t z);
* @param y Y movement to shift chunk along.
* @param z Z movement to shift chunk along.
*/
void chunkListAlign(chunklist_t *list, int32_t x, int32_t y, int32_t z);
void chunkListAlign(chunklist_t *list, int32_t x, int32_t y, int32_t z);
/**
* Loads a given chunk.
*
* @param list List to load the chunk for.
* @param chunk The chunk to load.
* @param x X of the chunk.
* @param y Y of the chunk.
* @param z Z of the chunk.
*/
void chunkListChunkLoad(chunklist_t *list, chunk_t *chunk,
int32_t x, int32_t y, int32_t z
);
/**
* Unload a given chunk.
*
* @param list List that the chunk belongs to.
* @param chunk Chunk to unload.
*/
void chunkListChunkUnload(chunklist_t *list, chunk_t *chunk);

22
src/engine/world/tile.c Normal file
View File

@ -0,0 +1,22 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "tile.h"
tilemap_t * tileMapCreate(tileset_t *tileset) {
tilemap_t *tilemap = malloc(sizeof(tilemap_t));
if(tilemap == NULL) return NULL;
tilemap->tileset = tileset;
tilemap->tiles = malloc(sizeof(tile_t) * tileset->columns * tileset->rows);
return tilemap;
}
void tileMapDispose(tilemap_t *tilemap) {
free(tilemap);
}

41
src/engine/world/tile.h Normal file
View File

@ -0,0 +1,41 @@
// 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>
#include "../display/tileset.h"
typedef uint8_t tileflag_t;
typedef uint16_t tileref_t;
typedef struct {
tileflag_t flags;
} tile_t;
typedef struct {
tile_t *tiles;
tileset_t *tileset;
} tilemap_t;
#define TILE_NULL (tileref_t)0
#define TILE_FLAG_DYNAMIC (tileflag_t)1
#define TILE_FLAG_3D (tileflag_t)2
#define TILE_FLAG_SOLID (tileflag_t)4
/**
* Creates a tilemap from a tileset.
*
* @param tileset Tileset to create from.
* @returns The tilemap.
*/
tilemap_t * tileMapCreate(tileset_t *tileset);
/**
* Destroys a previously created tilemap.
*
* @param tilemap Tilemap to dispose.
*/
void tileMapDispose(tilemap_t *tilemap);

View File

@ -11,8 +11,40 @@ world_t * worldCreate() {
world_t *world = malloc(sizeof(world_t));
if(world == NULL) return NULL;
world->chunkList = chunkListCreate(3, 3, 3);
// Texture
world->texture = assetTextureLoad("tileset.png");
if(world->texture == NULL) {
free(world);
return NULL;
}
// Tileset
world->tileset = tilesetCreate(
world->texture->width / 16, world->texture->height / 16,
world->texture->width, world->texture->height,
0, 0, 0, 0, 0
);
if(world->tileset == NULL) {
textureDispose(world->texture);
free(world);
return NULL;
}
// Tilemap
world->tilemap = tileMapCreate(world->tileset);
if(world->tilemap == NULL) {
textureDispose(world->texture);
tilesetDispose(world->tileset);
free(world);
return NULL;
}
// Chunk Lists
world->chunkList = chunkListCreate(3, 3, 3, world->tilemap);
if(world->chunkList == NULL) {
textureDispose(world->texture);
tilesetDispose(world->tileset);
tileMapDispose(world->tilemap);
free(world);
return NULL;
}
@ -22,6 +54,9 @@ world_t * worldCreate() {
void worldRender(world_t *world, shader_t *shader) {
int32_t i;
shaderUseTexture(shader, world->texture);
for(i = 0; i < world->chunkList->count; i++) {
chunkRender(world->chunkList->chunks + i, shader);
}

View File

@ -5,10 +5,17 @@
#pragma once
#include "chunklist.h"
#include "tile.h"
#include "../display/shader.h"
#include "../display/tileset.h"
#include "../display/texture.h"
#include "../file/asset.h"
typedef struct {
chunklist_t *chunkList;
texture_t *texture;
tileset_t *tileset;
tilemap_t *tilemap;
} world_t;
world_t * worldCreate();