From 5a8710cc764553e9706f1dddbd4e8ac9a16b3e8f Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Sun, 9 Nov 2025 13:00:43 -0600 Subject: [PATCH] Map loading and rendering --- src/rpg/rpg.c | 2 +- src/rpg/rpgcamera.c | 9 ++++---- src/rpg/world/chunk.h | 6 ++--- src/rpg/world/map.h | 6 ++--- src/scene/scene/scenemap.c | 45 +++++++++++++++++++++++++++++++++++++- src/scene/scene/scenemap.h | 1 + 6 files changed, 57 insertions(+), 12 deletions(-) diff --git a/src/rpg/rpg.c b/src/rpg/rpg.c index dfffbf6..9b848f3 100644 --- a/src/rpg/rpg.c +++ b/src/rpg/rpg.c @@ -31,7 +31,7 @@ errorret_t rpgInit(void) { entityInit(ent, ENTITY_TYPE_PLAYER); RPG_CAMERA.mode = RPG_CAMERA_MODE_FOLLOW_ENTITY; RPG_CAMERA.followEntity.followEntityId = ent->id; - ent->position.x = 4, ent->position.y = 4; + ent->position.x = 0, ent->position.y = 0; ent = &ENTITIES[1]; entityInit(ent, ENTITY_TYPE_NPC); diff --git a/src/rpg/rpgcamera.c b/src/rpg/rpgcamera.c index afb8c81..4926e04 100644 --- a/src/rpg/rpgcamera.c +++ b/src/rpg/rpgcamera.c @@ -26,11 +26,12 @@ void rpgCameraUpdate(void) { entity_t *entity = &ENTITIES[RPG_CAMERA.followEntity.followEntityId]; if(entity->type == ENTITY_TYPE_NULL) return; - // Update map position to match camera. + // Update map position to match camera. By default map wants to know the + // top left but we want to set the center, so we need to sub half map size mapPositionSet( - (int16_t)(entity->position.x / CHUNK_WIDTH), - (int16_t)(entity->position.y / CHUNK_HEIGHT), - (int16_t)(entity->position.z / CHUNK_DEPTH) + (int16_t)(entity->position.x / CHUNK_WIDTH) - (MAP_CHUNK_WIDTH / 2), + (int16_t)(entity->position.y / CHUNK_HEIGHT) - (MAP_CHUNK_HEIGHT / 2), + (int16_t)(entity->position.z / CHUNK_DEPTH) - (MAP_CHUNK_DEPTH / 2) ); break; } diff --git a/src/rpg/world/chunk.h b/src/rpg/world/chunk.h index 0bf00b5..31a0004 100644 --- a/src/rpg/world/chunk.h +++ b/src/rpg/world/chunk.h @@ -8,9 +8,9 @@ #pragma once #include "rpg/world/tile.h" -#define CHUNK_WIDTH 16 -#define CHUNK_HEIGHT 16 -#define CHUNK_DEPTH 16 +#define CHUNK_WIDTH 4 +#define CHUNK_HEIGHT 4 +#define CHUNK_DEPTH 32 #define CHUNK_TILE_COUNT (CHUNK_WIDTH * CHUNK_HEIGHT * CHUNK_DEPTH) typedef struct chunk_s { diff --git a/src/rpg/world/map.h b/src/rpg/world/map.h index e20b1f1..62576a0 100644 --- a/src/rpg/world/map.h +++ b/src/rpg/world/map.h @@ -8,9 +8,9 @@ #pragma once #include "rpg/world/chunk.h" -#define MAP_CHUNK_WIDTH 4 -#define MAP_CHUNK_HEIGHT 4 -#define MAP_CHUNK_DEPTH 4 +#define MAP_CHUNK_WIDTH 3 +#define MAP_CHUNK_HEIGHT 3 +#define MAP_CHUNK_DEPTH 1 #define MAP_CHUNK_COUNT (MAP_CHUNK_WIDTH * MAP_CHUNK_HEIGHT * MAP_CHUNK_DEPTH) typedef struct map_s { diff --git a/src/scene/scene/scenemap.c b/src/scene/scene/scenemap.c index 0fb9dc7..840d932 100644 --- a/src/scene/scene/scenemap.c +++ b/src/scene/scene/scenemap.c @@ -10,6 +10,7 @@ #include "display/spritebatch.h" #include "assert/assert.h" #include "rpg/entity/entity.h" +#include "rpg/world/map.h" #include "display/screen.h" #include "rpg/rpgcamera.h" #include "util/memory.h" @@ -80,7 +81,6 @@ void sceneMapEntityGetPosition(const entity_t *entity, vec3 outPosition) { } } - void sceneMapRender(scenedata_t *data) { // Look at target. vec3 cameraTarget; @@ -108,6 +108,9 @@ void sceneMapRender(scenedata_t *data) { // Push camera cameraPushMatrix(&data->sceneMap.camera); + // Render map probably. + sceneMapRenderMap(); + // Render ents entity_t *ent = ENTITIES; do { @@ -119,6 +122,8 @@ void sceneMapRender(scenedata_t *data) { cameraPopMatrix(); } + + void sceneMapRenderEntity(entity_t *entity) { assertNotNull(entity, "Entity cannot be NULL"); @@ -156,5 +161,43 @@ void sceneMapRenderEntity(entity_t *entity) { spriteBatchPush3D(NULL, posMin, posMax, testColor, uv0, uv1); } +void sceneMapRenderMap() { + // For each chunk. + for(uint32_t i = 0; i < MAP_CHUNK_COUNT; i++) { + chunk_t *chunk = MAP.chunkOrder[i]; + + vec3 min, max; + min[0] = chunk->x * CHUNK_WIDTH * TILE_SIZE; + min[1] = chunk->y * CHUNK_HEIGHT * TILE_SIZE; + min[2] = chunk->z * CHUNK_DEPTH * TILE_SIZE; + + // center tile + min[0] -= TILE_SIZE / 2.0f; + min[1] -= TILE_SIZE / 2.0f; + min[2] -= TILE_SIZE / 2.0f; + + max[0] = min[0] + (CHUNK_WIDTH * TILE_SIZE); + max[1] = min[1] + (CHUNK_HEIGHT * TILE_SIZE); + max[2] = min[2]; + + + color_t color = COLOR_WHITE; + if(chunk->x % 2 == 0) { + color = (chunk->y % 2 == 0) ? COLOR_BLACK : COLOR_WHITE; + } else { + color = (chunk->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) { } diff --git a/src/scene/scene/scenemap.h b/src/scene/scene/scenemap.h index 1eaad44..d9381f2 100644 --- a/src/scene/scene/scenemap.h +++ b/src/scene/scene/scenemap.h @@ -18,6 +18,7 @@ errorret_t sceneMapInit(scenedata_t *data); void sceneMapUpdate(scenedata_t *data); void sceneMapRender(scenedata_t *data); void sceneMapRenderEntity(entity_t *entity); +void sceneMapRenderMap(); void sceneMapDispose(scenedata_t *data); static scene_t SCENE_MAP = {