basically chunk loading
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
#include "type/assetalphaimage.h"
|
||||
#include "type/assetlanguage.h"
|
||||
#include "type/assetmap.h"
|
||||
#include "type/assetchunk.h"
|
||||
#include <zip.h>
|
||||
|
||||
typedef enum {
|
||||
@@ -19,6 +20,7 @@ typedef enum {
|
||||
ASSET_TYPE_ALPHA_IMAGE,
|
||||
ASSET_TYPE_LANGUAGE,
|
||||
ASSET_TYPE_MAP,
|
||||
ASSET_TYPE_CHUNK,
|
||||
|
||||
ASSET_TYPE_COUNT,
|
||||
} assettype_t;
|
||||
@@ -73,5 +75,11 @@ static const assettypedef_t ASSET_TYPE_DEFINITIONS[ASSET_TYPE_COUNT] = {
|
||||
.loadStrategy = ASSET_LOAD_STRAT_ENTIRE,
|
||||
.dataSize = sizeof(assetmap_t),
|
||||
.entire = assetMapLoad
|
||||
},
|
||||
|
||||
[ASSET_TYPE_CHUNK] = {
|
||||
.header = "DCF",
|
||||
.loadStrategy = ASSET_LOAD_STRAT_CUSTOM,
|
||||
.custom = assetChunkLoad
|
||||
}
|
||||
};
|
||||
@@ -10,4 +10,5 @@ target_sources(${DUSK_TARGET_NAME}
|
||||
assetpaletteimage.c
|
||||
assetlanguage.c
|
||||
assetmap.c
|
||||
assetchunk.c
|
||||
)
|
||||
122
src/asset/type/assetchunk.c
Normal file
122
src/asset/type/assetchunk.c
Normal file
@@ -0,0 +1,122 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "asset/asset.h"
|
||||
#include "assert/assert.h"
|
||||
|
||||
#pragma pack(push, 1)
|
||||
typedef struct {
|
||||
uint32_t tileCount;
|
||||
uint8_t modelCount;
|
||||
} assetchunkheader_t;
|
||||
#pragma pack(pop)
|
||||
|
||||
#pragma pack(push, 1)
|
||||
typedef struct {
|
||||
tile_t tile;
|
||||
} assetchunktiledata_t;
|
||||
#pragma pack(pop)
|
||||
|
||||
#pragma pack(push, 1)
|
||||
typedef struct {
|
||||
uint32_t vertexCount;
|
||||
} assetchunkmodelheader_t;
|
||||
#pragma pack(pop)
|
||||
|
||||
errorret_t assetChunkLoad(assetcustom_t custom) {
|
||||
assertNotNull(custom.output, "Output pointer cannot be NULL");
|
||||
assertNotNull(custom.zipFile, "Zip file pointer cannot be NULL");
|
||||
|
||||
chunk_t *chunk = (chunk_t *)custom.output;
|
||||
|
||||
// Read header
|
||||
assetchunkheader_t header;
|
||||
size_t bytesRead = zip_fread(
|
||||
custom.zipFile, &header, sizeof(assetchunkheader_t)
|
||||
);
|
||||
if(bytesRead != sizeof(assetchunkheader_t)) {
|
||||
zip_fclose(custom.zipFile);
|
||||
errorThrow("Failed to read chunk asset header.");
|
||||
}
|
||||
|
||||
if(header.tileCount != CHUNK_TILE_COUNT) {
|
||||
zip_fclose(custom.zipFile);
|
||||
errorThrow(
|
||||
"Chunk asset has invalid tile count: %d (expected %d).",
|
||||
header.tileCount,
|
||||
CHUNK_TILE_COUNT
|
||||
);
|
||||
}
|
||||
|
||||
if(header.modelCount > CHUNK_MESH_COUNT_MAX) {
|
||||
zip_fclose(custom.zipFile);
|
||||
errorThrow(
|
||||
"Chunk asset has too many models: %d (max %d).",
|
||||
header.modelCount,
|
||||
CHUNK_MESH_COUNT_MAX
|
||||
);
|
||||
}
|
||||
|
||||
chunk->meshCount = header.modelCount;
|
||||
|
||||
// Read tile data
|
||||
bytesRead = zip_fread(
|
||||
custom.zipFile,
|
||||
chunk->tiles,
|
||||
sizeof(assetchunktiledata_t) * header.tileCount
|
||||
);
|
||||
if(bytesRead != sizeof(assetchunktiledata_t) * header.tileCount) {
|
||||
zip_fclose(custom.zipFile);
|
||||
errorThrow("Failed to read chunk tile data.");
|
||||
}
|
||||
|
||||
// For each model...
|
||||
uint32_t vertexIndex = 0;
|
||||
for(uint8_t i = 0; i < header.modelCount; i++) {
|
||||
assetchunkmodelheader_t modelHeader;
|
||||
bytesRead = zip_fread(
|
||||
custom.zipFile, &modelHeader, sizeof(assetchunkmodelheader_t)
|
||||
);
|
||||
if(bytesRead != sizeof(assetchunkmodelheader_t)) {
|
||||
zip_fclose(custom.zipFile);
|
||||
errorThrow("Failed to read chunk model header.");
|
||||
}
|
||||
|
||||
if(
|
||||
vertexIndex + modelHeader.vertexCount >
|
||||
CHUNK_VERTEX_COUNT_MAX
|
||||
) {
|
||||
zip_fclose(custom.zipFile);
|
||||
errorThrow("Chunk model vertex count exceeds maximum.");
|
||||
}
|
||||
|
||||
// Read vertex data.
|
||||
bytesRead = zip_fread(
|
||||
custom.zipFile,
|
||||
&chunk->vertices[vertexIndex],
|
||||
sizeof(meshvertex_t) * modelHeader.vertexCount
|
||||
);
|
||||
if(bytesRead != sizeof(meshvertex_t) * modelHeader.vertexCount) {
|
||||
zip_fclose(custom.zipFile);
|
||||
errorThrow("Failed to read chunk model vertex data.");
|
||||
}
|
||||
|
||||
|
||||
// Init the mesh
|
||||
mesh_t *mesh = &chunk->meshes[i];
|
||||
meshInit(
|
||||
mesh,
|
||||
MESH_PRIMITIVE_TRIANGLES,
|
||||
modelHeader.vertexCount,
|
||||
&chunk->vertices[vertexIndex]
|
||||
);
|
||||
|
||||
vertexIndex += modelHeader.vertexCount;
|
||||
}
|
||||
|
||||
errorOk();
|
||||
}
|
||||
20
src/asset/type/assetchunk.h
Normal file
20
src/asset/type/assetchunk.h
Normal file
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "error/error.h"
|
||||
#include "rpg/world/chunk.h"
|
||||
|
||||
typedef struct assetcustom_s assetcustom_t;
|
||||
|
||||
/**
|
||||
* Handles loading of chunk data from a chunk asset file.
|
||||
*
|
||||
* @param custom The custom asset loading parameters.
|
||||
* @return An error code.
|
||||
*/
|
||||
errorret_t assetChunkLoad(assetcustom_t custom);
|
||||
@@ -8,10 +8,18 @@
|
||||
#pragma once
|
||||
#include "rpg/world/tile.h"
|
||||
#include "worldpos.h"
|
||||
#include "display/mesh/mesh.h"
|
||||
|
||||
#define CHUNK_VERTEX_COUNT_MAX (6 * CHUNK_TILE_COUNT * 3)
|
||||
#define CHUNK_MESH_COUNT_MAX 16
|
||||
|
||||
typedef struct chunk_s {
|
||||
chunkpos_t position;
|
||||
tile_t tiles[CHUNK_TILE_COUNT];
|
||||
|
||||
uint8_t meshCount;
|
||||
meshvertex_t vertices[CHUNK_VERTEX_COUNT_MAX];
|
||||
mesh_t meshes[CHUNK_MESH_COUNT_MAX];
|
||||
} chunk_t;
|
||||
|
||||
/**
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
#include "map.h"
|
||||
#include "util/memory.h"
|
||||
#include "assert/assert.h"
|
||||
#include "scene/scene/scenemap.h"
|
||||
#include "asset/asset.h"
|
||||
|
||||
map_t MAP;
|
||||
|
||||
@@ -122,20 +122,7 @@ void mapChunkUnload(chunk_t* chunk) {
|
||||
}
|
||||
|
||||
void mapChunkLoad(chunk_t* chunk) {
|
||||
// printf("Loading chunk at (%d, %d, %d)\n",
|
||||
// chunk->position.x,
|
||||
// chunk->position.y,
|
||||
// chunk->position.z
|
||||
// );
|
||||
|
||||
memoryZero(chunk->tiles, sizeof(tile_t) * CHUNK_TILE_COUNT);
|
||||
|
||||
if(chunk->position.x == 0 && chunk->position.y == 0 && chunk->position.z == 0) {
|
||||
if(TEST_MAP_READY) {
|
||||
|
||||
}
|
||||
printf("LOAD CHUNK\n");
|
||||
}
|
||||
errorCatch(errorPrint(assetLoad("map/map/0_0.dcf", chunk)));
|
||||
}
|
||||
|
||||
chunkindex_t mapGetChunkIndexAt(const chunkpos_t position) {
|
||||
|
||||
@@ -19,8 +19,6 @@
|
||||
#define TILE_WIDTH 16.0f
|
||||
#define TILE_HEIGHT 16.0f
|
||||
#define TILE_DEPTH 11.36f
|
||||
assetmap_t TEST_MAP;
|
||||
bool_t TEST_MAP_READY = false;
|
||||
|
||||
errorret_t sceneMapInit(scenedata_t *data) {
|
||||
// Init the camera.
|
||||
@@ -39,9 +37,6 @@ errorret_t sceneMapInit(scenedata_t *data) {
|
||||
);
|
||||
data->sceneMap.camera.lookatPixelPerfect.pixelsPerUnit = 1.0f;
|
||||
|
||||
|
||||
errorChain(assetLoad("map/map.dmf", &TEST_MAP));
|
||||
TEST_MAP_READY = true;
|
||||
errorOk();
|
||||
}
|
||||
|
||||
@@ -118,10 +113,7 @@ void sceneMapRender(scenedata_t *data) {
|
||||
cameraPushMatrix(&data->sceneMap.camera);
|
||||
|
||||
// Render map probably.
|
||||
// sceneMapRenderMap();
|
||||
|
||||
textureBind(NULL);
|
||||
meshDraw(&TEST_MAP.models[0].mesh, -1, -1);
|
||||
sceneMapRenderMap();
|
||||
|
||||
// Render ents
|
||||
entity_t *ent = ENTITIES;
|
||||
@@ -175,33 +167,38 @@ void sceneMapRenderMap() {
|
||||
for(uint32_t i = 0; i < MAP_CHUNK_COUNT; i++) {
|
||||
chunk_t *chunk = MAP.chunkOrder[i];
|
||||
|
||||
vec3 min, max;
|
||||
min[0] = chunk->position.x * CHUNK_WIDTH * TILE_WIDTH;
|
||||
min[1] = chunk->position.y * CHUNK_HEIGHT * TILE_HEIGHT;
|
||||
min[2] = chunk->position.z * CHUNK_DEPTH * TILE_DEPTH;
|
||||
|
||||
max[0] = min[0] + (CHUNK_WIDTH * TILE_WIDTH);
|
||||
max[1] = min[1] + (CHUNK_HEIGHT * TILE_HEIGHT);
|
||||
max[2] = min[2];
|
||||
|
||||
color_t color = COLOR_WHITE;
|
||||
if(chunk->position.x % 2 == 0) {
|
||||
color = (chunk->position.y % 2 == 0) ? COLOR_BLACK : COLOR_WHITE;
|
||||
} else {
|
||||
color = (chunk->position.y % 2 == 0) ? COLOR_WHITE : COLOR_BLACK;
|
||||
for(uint8_t j = 0; j < chunk->meshCount; j++) {
|
||||
mesh_t *mesh = &chunk->meshes[j];
|
||||
textureBind(NULL);
|
||||
meshDraw(mesh, -1, -1);
|
||||
}
|
||||
|
||||
spriteBatchPush3D(
|
||||
NULL,
|
||||
min,
|
||||
max,
|
||||
color,
|
||||
(vec2){ 0.0f, 0.0f },
|
||||
(vec2){ 1.0f, 1.0f }
|
||||
);
|
||||
// vec3 min, max;
|
||||
// min[0] = chunk->position.x * CHUNK_WIDTH * TILE_WIDTH;
|
||||
// min[1] = chunk->position.y * CHUNK_HEIGHT * TILE_HEIGHT;
|
||||
// min[2] = chunk->position.z * CHUNK_DEPTH * TILE_DEPTH;
|
||||
|
||||
// max[0] = min[0] + (CHUNK_WIDTH * TILE_WIDTH);
|
||||
// max[1] = min[1] + (CHUNK_HEIGHT * TILE_HEIGHT);
|
||||
// max[2] = min[2];
|
||||
|
||||
// color_t color = COLOR_WHITE;
|
||||
// if(chunk->position.x % 2 == 0) {
|
||||
// color = (chunk->position.y % 2 == 0) ? COLOR_BLACK : COLOR_WHITE;
|
||||
// } else {
|
||||
// color = (chunk->position.y % 2 == 0) ? COLOR_WHITE : COLOR_BLACK;
|
||||
// }
|
||||
|
||||
// spriteBatchPush3D(
|
||||
// NULL,
|
||||
// min,
|
||||
// max,
|
||||
// color,
|
||||
// (vec2){ 0.0f, 0.0f },
|
||||
// (vec2){ 1.0f, 1.0f }
|
||||
// );
|
||||
}
|
||||
}
|
||||
|
||||
void sceneMapDispose(scenedata_t *data) {
|
||||
meshDispose(&TEST_MAP.models[0].mesh);
|
||||
}
|
||||
|
||||
@@ -15,9 +15,6 @@ typedef struct {
|
||||
camera_t camera;
|
||||
} scenemap_t;
|
||||
|
||||
extern assetmap_t TEST_MAP;
|
||||
extern bool_t TEST_MAP_READY;
|
||||
|
||||
errorret_t sceneMapInit(scenedata_t *data);
|
||||
void sceneMapUpdate(scenedata_t *data);
|
||||
void sceneMapRender(scenedata_t *data);
|
||||
|
||||
Reference in New Issue
Block a user