Map loading and rendering

This commit is contained in:
2025-11-09 13:00:43 -06:00
parent 307f3a9dec
commit 5a8710cc76
6 changed files with 57 additions and 12 deletions

View File

@@ -31,7 +31,7 @@ errorret_t rpgInit(void) {
entityInit(ent, ENTITY_TYPE_PLAYER); entityInit(ent, ENTITY_TYPE_PLAYER);
RPG_CAMERA.mode = RPG_CAMERA_MODE_FOLLOW_ENTITY; RPG_CAMERA.mode = RPG_CAMERA_MODE_FOLLOW_ENTITY;
RPG_CAMERA.followEntity.followEntityId = ent->id; 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]; ent = &ENTITIES[1];
entityInit(ent, ENTITY_TYPE_NPC); entityInit(ent, ENTITY_TYPE_NPC);

View File

@@ -26,11 +26,12 @@ void rpgCameraUpdate(void) {
entity_t *entity = &ENTITIES[RPG_CAMERA.followEntity.followEntityId]; entity_t *entity = &ENTITIES[RPG_CAMERA.followEntity.followEntityId];
if(entity->type == ENTITY_TYPE_NULL) return; 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( mapPositionSet(
(int16_t)(entity->position.x / CHUNK_WIDTH), (int16_t)(entity->position.x / CHUNK_WIDTH) - (MAP_CHUNK_WIDTH / 2),
(int16_t)(entity->position.y / CHUNK_HEIGHT), (int16_t)(entity->position.y / CHUNK_HEIGHT) - (MAP_CHUNK_HEIGHT / 2),
(int16_t)(entity->position.z / CHUNK_DEPTH) (int16_t)(entity->position.z / CHUNK_DEPTH) - (MAP_CHUNK_DEPTH / 2)
); );
break; break;
} }

View File

@@ -8,9 +8,9 @@
#pragma once #pragma once
#include "rpg/world/tile.h" #include "rpg/world/tile.h"
#define CHUNK_WIDTH 16 #define CHUNK_WIDTH 4
#define CHUNK_HEIGHT 16 #define CHUNK_HEIGHT 4
#define CHUNK_DEPTH 16 #define CHUNK_DEPTH 32
#define CHUNK_TILE_COUNT (CHUNK_WIDTH * CHUNK_HEIGHT * CHUNK_DEPTH) #define CHUNK_TILE_COUNT (CHUNK_WIDTH * CHUNK_HEIGHT * CHUNK_DEPTH)
typedef struct chunk_s { typedef struct chunk_s {

View File

@@ -8,9 +8,9 @@
#pragma once #pragma once
#include "rpg/world/chunk.h" #include "rpg/world/chunk.h"
#define MAP_CHUNK_WIDTH 4 #define MAP_CHUNK_WIDTH 3
#define MAP_CHUNK_HEIGHT 4 #define MAP_CHUNK_HEIGHT 3
#define MAP_CHUNK_DEPTH 4 #define MAP_CHUNK_DEPTH 1
#define MAP_CHUNK_COUNT (MAP_CHUNK_WIDTH * MAP_CHUNK_HEIGHT * MAP_CHUNK_DEPTH) #define MAP_CHUNK_COUNT (MAP_CHUNK_WIDTH * MAP_CHUNK_HEIGHT * MAP_CHUNK_DEPTH)
typedef struct map_s { typedef struct map_s {

View File

@@ -10,6 +10,7 @@
#include "display/spritebatch.h" #include "display/spritebatch.h"
#include "assert/assert.h" #include "assert/assert.h"
#include "rpg/entity/entity.h" #include "rpg/entity/entity.h"
#include "rpg/world/map.h"
#include "display/screen.h" #include "display/screen.h"
#include "rpg/rpgcamera.h" #include "rpg/rpgcamera.h"
#include "util/memory.h" #include "util/memory.h"
@@ -80,7 +81,6 @@ void sceneMapEntityGetPosition(const entity_t *entity, vec3 outPosition) {
} }
} }
void sceneMapRender(scenedata_t *data) { void sceneMapRender(scenedata_t *data) {
// Look at target. // Look at target.
vec3 cameraTarget; vec3 cameraTarget;
@@ -108,6 +108,9 @@ void sceneMapRender(scenedata_t *data) {
// Push camera // Push camera
cameraPushMatrix(&data->sceneMap.camera); cameraPushMatrix(&data->sceneMap.camera);
// Render map probably.
sceneMapRenderMap();
// Render ents // Render ents
entity_t *ent = ENTITIES; entity_t *ent = ENTITIES;
do { do {
@@ -119,6 +122,8 @@ void sceneMapRender(scenedata_t *data) {
cameraPopMatrix(); cameraPopMatrix();
} }
void sceneMapRenderEntity(entity_t *entity) { void sceneMapRenderEntity(entity_t *entity) {
assertNotNull(entity, "Entity cannot be NULL"); assertNotNull(entity, "Entity cannot be NULL");
@@ -156,5 +161,43 @@ void sceneMapRenderEntity(entity_t *entity) {
spriteBatchPush3D(NULL, posMin, posMax, testColor, uv0, uv1); 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) { void sceneMapDispose(scenedata_t *data) {
} }

View File

@@ -18,6 +18,7 @@ errorret_t sceneMapInit(scenedata_t *data);
void sceneMapUpdate(scenedata_t *data); void sceneMapUpdate(scenedata_t *data);
void sceneMapRender(scenedata_t *data); void sceneMapRender(scenedata_t *data);
void sceneMapRenderEntity(entity_t *entity); void sceneMapRenderEntity(entity_t *entity);
void sceneMapRenderMap();
void sceneMapDispose(scenedata_t *data); void sceneMapDispose(scenedata_t *data);
static scene_t SCENE_MAP = { static scene_t SCENE_MAP = {