Disconnected world code temporarily.

This commit is contained in:
2021-05-16 17:51:46 -07:00
parent e77893f01c
commit 3aa6a220b1
22 changed files with 1 additions and 8 deletions

View File

@ -0,0 +1,90 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "chunk.h"
void chunkLoad(chunk_t *chunk, int32_t x, int32_t y, int32_t z) {
tileid_t tileId;
tiledef_t *tileDef;
int32_t i, indiceCount, verticeCount, tx, ty, tz;
for(ty = 0; ty < CHUNK_HEIGHT; ty++) {
for(tx = 0; tx < CHUNK_WIDTH; tx++) {
if(z != 0) break;
chunk->tiles[ty*CHUNK_WIDTH + tx] = ty == 5 ? 2 : 1;
}
}
// Start by loading the tiles and figuring out how big we need to make the
// primitive that the chunk uses.
indiceCount = 0, verticeCount = 0;
for(i = 0; i < CHUNK_TILE_COUNT; i++) {
//TODO: Actually load the tileId here
tileId = chunk->tiles[i];
if(tileId == TILE_NULL) continue;
// Increment the primitive size.
tileDef = MAP_STATE.tileDefinitions + tileId;
verticeCount += tileDef->verticeCount;
indiceCount += tileDef->indiceCount;
}
// Do we even need to create a primitive?
if(indiceCount == 0) return;
chunk->primitive = primitiveCreate(verticeCount, indiceCount);
// Render each tile. The ZYX order is important for ordering.
i = 0;
verticeCount = 0, indiceCount = 0;
for(tz = 0; tz < CHUNK_DEPTH; tz++) {
for(ty = 0; ty < CHUNK_HEIGHT; ty++) {
for(tx = 0; tx < CHUNK_WIDTH; tx++) {
tileId = chunk->tiles[i];
if(tileId == TILE_NULL) {
i++;
continue;
}
tileDef = MAP_STATE.tileDefinitions + tileId;
tileRender(
chunk, tileId, tileDef,
i, tx, ty, tz,
verticeCount, indiceCount
);
// Prepare for the next render.
verticeCount += tileDef->verticeCount;
indiceCount += tileDef->indiceCount;
i++;
}
}
}
}
void chunkUnload(chunk_t *chunk) {
// Load chunks to zero. TODO: Necessary?
memset(chunk->tiles, TILE_NULL, CHUNK_TILE_COUNT);
// Unload the primitive. TODO: Can we salvage this and resize instead?
if(chunk->primitive == NULL) return;
primitiveDispose(chunk->primitive);
chunk->primitive = NULL;
}
int32_t chunkGet(int32_t x, int32_t y, int32_t z) {
int32_t i = (
mathMod(x - MAP_STATE.x, MAP_WIDTH) +
(mathMod(y - MAP_STATE.y, MAP_HEIGHT) * MAP_WIDTH) +
(mathMod(z - MAP_STATE.z, MAP_DEPTH) * MAP_WIDTH * MAP_HEIGHT)
);
if(i < 0 || i > MAP_CHUNK_COUNT) return -1;
return i;
}
int32_t chunkGetTile(int32_t x, int32_t y, int32_t z) {
return x + (y * CHUNK_WIDTH) + (z * CHUNK_WIDTH * CHUNK_HEIGHT);
}

View File

@ -0,0 +1,48 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include <dawn/dawn.h>
#include "../../display/primitive.h"
#include "tile.h"
/**
* Loads a given chunk.
*
* @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 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);
/**
* Gets the chunk index from an absolute coordinate.
*
* @param x Absolute chunk X.
* @param y Absolute chunk Y.
* @param z Absolute chunk Z.
* @returns The index for that chunk. -1 if out of bounds of the current list.
*/
int32_t chunkGet(int32_t x, int32_t y, int32_t z);
/**
* Gets the tile index from a local tile coordinate.
*
* @param x The local X coordinate of the tile.
* @param y The local Y coordinate of the tile.
* @param z The local Z coordinate of the tile.
* @return The index within the chunk that the tile resides.
*/
int32_t chunkGetTile(int32_t x, int32_t y, int32_t z);

171
temp/src/world/map/map.c Normal file
View File

@ -0,0 +1,171 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "map.h"
map_t MAP_STATE;
void mapInit() {
int32_t i, z, x, y;
chunk_t *chunk;
// Reset map
MAP_STATE.x = MAP_STATE.y = MAP_STATE.z = 0;
// Load the texture
MAP_STATE.texture = assetTextureLoad(MAP_ASSET_TEXTURE);
// Load the tileset
MAP_STATE.tileset = tilesetCreate(
MAP_STATE.texture->width/TILE_WIDTH, MAP_STATE.texture->height/TILE_HEIGHT,
MAP_STATE.texture->width, MAP_STATE.texture->height,
0, 0, 0, 0
);
// Prepare the tile definitions
MAP_STATE.tileDefinitions = calloc(
MAP_STATE.tileset->count,sizeof(tiledef_t)
);
// TODO: Load the tile definitions here.
for(i = 0; i < MAP_STATE.tileset->count; i++) {
(MAP_STATE.tileDefinitions+i)->verticeCount = 4;
(MAP_STATE.tileDefinitions+i)->indiceCount = 6;
}
// Load each of the chunks. The ZYX order is important for ordering.
i = 0;
for(z = 0; z < MAP_DEPTH; z++) {
for(y = 0; y < MAP_HEIGHT; y++) {
for(x = 0; x < MAP_WIDTH; x++) {
chunk = MAP_STATE.chunks + i;
MAP_STATE.chunkList[i] = chunk;
// Load initial chunk state.
chunk->x = x;
chunk->y = y;
chunk->z = z;
chunk->primitive = NULL;
// Load the chunk.
chunkLoad(chunk, x, y, z);
i++;
}
}
}
}
void mapRender() {
int32_t i;
float x, y, z;
chunk_t *chunk;
// Bind the texture
shaderUseTexture(GAME_STATE.shaderWorld, MAP_STATE.texture);
// Render each chunk.
// TODO: Do we need to render every chunk? (screen area)
for(i = 0; i < MAP_CHUNK_COUNT; i++) {
chunk = MAP_STATE.chunkList[i];
if(chunk->primitive == NULL) continue;
// Offset the primitive (into world space)
x = (chunk->x * CHUNK_WIDTH);
y = (chunk->y * CHUNK_HEIGHT);
z = (chunk->z * CHUNK_DEPTH);
shaderUsePosition(GAME_STATE.shaderWorld, x, y, z, 0, 0, 0);
primitiveDraw(chunk->primitive, 0, chunk->primitive->indiceCount);
}
}
void mapDispose() {
int32_t i;
chunk_t *chunk;
// Unload the chunks
for(i = 0; i < MAP_CHUNK_COUNT; i++) {
chunk = MAP_STATE.chunkList[i];
chunkUnload(chunk);
}
tilesetDispose(MAP_STATE.tileset);
textureDispose(MAP_STATE.texture);
}
void mapShift(int32_t x, int32_t y, int32_t z) {
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 *chunkList[MAP_CHUNK_COUNT];
// Calculate the new chunklist coordinates
lx = MAP_STATE.x + x;
ly = MAP_STATE.y + y;
lz = MAP_STATE.z + z;
// Precalc width * height.
wh = MAP_WIDTH * MAP_HEIGHT;
for(i = 0; i < MAP_CHUNK_COUNT; i++) {
chunk = MAP_STATE.chunkList[i];
// Calculate the new local positions for the chunk.
nx = mathMod(chunk->x - MAP_STATE.x - x, MAP_WIDTH);
ny = mathMod(chunk->y - MAP_STATE.y - y, MAP_HEIGHT);
nz = mathMod(chunk->z - MAP_STATE.z - z, MAP_DEPTH);
// Load the chunk if we need to. We also use this to calculate new absolutes
if(
(ax = lx + nx) != chunk->x ||
(ly + ny) != chunk->y ||
(lz + nz) != chunk->z
) {
// Calculate those things that may have not been done within the if
ay = ly + ny;
az = lz + nz;
// Load new chunk.
chunkUnload(chunk);
chunkLoad(chunk, ax, ay, az);
// Update the absolute coordinates.
chunk->x = ax;
chunk->y = ay;
chunk->z = az;
}
// Now, based off those new local positions, calculate the new index.
ni = (
nx +
(ny * MAP_WIDTH) +
(nz * wh)
);
chunkList[ni] = chunk;
}
// Update Absolutes.
MAP_STATE.x = lx;
MAP_STATE.y = ly;
MAP_STATE.z = lz;
// Now copy that array over.
memcpy(MAP_STATE.chunkList, chunkList, sizeof(chunk_t *) * MAP_CHUNK_COUNT);
}
void mapAlign(int32_t x, int32_t y, int32_t z) {
mapShift(x - MAP_STATE.x, y - MAP_STATE.y, z - MAP_STATE.z);
}

45
temp/src/world/map/map.h Normal file
View File

@ -0,0 +1,45 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include <dawn/dawn.h>
#include "../../display/tileset.h"
#include "../../file/asset.h"
#include "chunk.h"
/**
* Initializes the world map.
*/
void mapInit();
/**
* Renders the map object to the graphics device.
*/
void mapRender();
/**
* Cleans the previously loaded world map.
*/
void mapDispose();
/**
* Shift the map chunk list along a set of axis (in absolute space).
*
* @param x X movement to shift chunks along.
* @param y Y movement to shift chunks along.
* @param z Z movement to shift chunks along.
*/
void mapShift(int32_t x, int32_t y, int32_t z);
/**
* Align the map chunk list (in absolute space).
*
* @param x X movement to shift chunks along.
* @param y Y movement to shift chunks along.
* @param z Z movement to shift chunks along.
*/
void mapAlign(int32_t x, int32_t y, int32_t z);

37
temp/src/world/map/tile.c Normal file
View File

@ -0,0 +1,37 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "tile.h"
void tileRender(
chunk_t *chunk, tileid_t id, tiledef_t *tileDef,
int32_t i, int32_t x, int32_t y, int32_t z,
int32_t verticeStart, int32_t indiceStart
) {
tilesetdiv_t *div = (MAP_STATE.tileset->divisions + id);
quadBuffer(chunk->primitive, z,
x, y, div->x0, div->y0,
x+1, y+1, div->x1, div->y1,
verticeStart, indiceStart
);
}
tilegetresult_t tileGet(int32_t x, int32_t y, int32_t z) {
tilegetresult_t result;
// First, determine the chunk that I belong to.
result.chunkX = x / CHUNK_WIDTH;
result.chunkY = y / CHUNK_HEIGHT;
result.chunkZ = z / CHUNK_DEPTH;
// And determine the local coordinates
result.localX = mathMod(x, CHUNK_WIDTH);
result.localY = mathMod(y, CHUNK_HEIGHT);
result.localZ = mathMod(z, CHUNK_DEPTH);
return result;
}

22
temp/src/world/map/tile.h 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
*/
#pragma once
#include <dawn/dawn.h>
#include "../../display/primitives/quad.h"
typedef struct {
int32_t chunkX, chunkY, chunkZ, localX, localY, localZ;
} tilegetresult_t;
void tileRender(
chunk_t *chunk, tileid_t id, tiledef_t *tileDef,
int32_t i, int32_t x, int32_t y, int32_t z,
int32_t verticeStart, int32_t indiceStart
);
tilegetresult_t tileGet(int32_t x, int32_t y, int32_t z);