Cleaned map a bit

This commit is contained in:
2025-03-04 11:50:47 -06:00
parent ad9e8a78d3
commit f19636edb1
13 changed files with 192 additions and 134 deletions

View File

@ -73,14 +73,14 @@ void entityMove(entity_t *ent, const facingdir_t dir) {
facingDirAdd(ent->direction, &targetX, &targetY); facingDirAdd(ent->direction, &targetX, &targetY);
// Check oob // Check oob
if(targetX < 0 || targetX >= OVERWORLD.mapWidth) return; if(targetX < 0 || targetX >= OVERWORLD.map.width) return;
if(targetY < 0 || targetY >= OVERWORLD.mapHeight) return; if(targetY < 0 || targetY >= OVERWORLD.map.height) return;
// Check tile at target // Check tile at target
uint8_t i = 0; uint8_t i = 0;
tileid_t tileId; tileid_t tileId;
for(i = 0; i < OVERWORLD.mapLayerCount; i++) { for(i = 0; i < OVERWORLD.map.layerCount; i++) {
tileId = overworldTileGet(0, targetX, targetY); tileId = mapGetTileIdAtPosition(&OVERWORLD.map, i, targetX, targetY);
if(tileIsSolid(tileId)) return; if(tileIsSolid(tileId)) return;
} }

View File

@ -7,6 +7,7 @@
target_sources(${DUSK_TARGET_NAME} target_sources(${DUSK_TARGET_NAME}
PRIVATE PRIVATE
tile.c tile.c
map.c
) )
# Subdirs # Subdirs

View File

@ -11,4 +11,59 @@
void mapInit(map_t *map) { void mapInit(map_t *map) {
memoryZero(map, sizeof(map_t)); memoryZero(map, sizeof(map_t));
// Test
map->width = 16;
map->height = 16;
map->layerCount = 1;
memorySet(&map->tileIds, 0x01, sizeof(map->tileIds));
// Test size
assertTrue(
(map->width * map->height * map->layerCount) <= OVERWORLD_TILE_COUNT_MAX,
"Map size exceeds tile count."
);
}
uint32_t mapGetTileIndex(
const map_t *map,
const uint8_t layer,
const uint8_t x,
const uint8_t y
) {
assertNotNull(map, "Map cannot be NULL");
assertTrue(layer < map->layerCount, "Invalid layer");
assertTrue(x < map->width, "Invalid x");
assertTrue(y < map->height, "Invalid y");
return (layer * map->width * map->height) + (y * map->width) + x;
}
tileid_t mapGetTileId(
const map_t *map,
const uint32_t tileIndex
) {
assertNotNull(map, "Map cannot be NULL");
assertMapIndexValid(map, tileIndex);
return map->tileIds[tileIndex];
}
tileid_t mapGetTileIdAtPosition(
const map_t *map,
const uint8_t layer,
const uint8_t x,
const uint8_t y
) {
return mapGetTileId(map, mapGetTileIndex(map, layer, x, y));
}
tiledata_t mapGetTileData(
const map_t *map,
const uint32_t tileIndex
) {
assertNotNull(map, "Map cannot be NULL");
assertMapIndexValid(map, tileIndex);
return map->tileData[tileIndex];
} }

View File

@ -8,6 +8,13 @@
#pragma once #pragma once
#include "tile.h" #include "tile.h"
#include "overworld/overworlddefs.h" #include "overworld/overworlddefs.h"
#include "assert/assert.h"
#define assertMapIndexValid(map, index) \
assertTrue( \
index < (map->layerCount * map->width * map->height), \
"Invalid tile index" \
);
typedef struct { typedef struct {
tileid_t tileIds[OVERWORLD_TILE_COUNT_MAX]; tileid_t tileIds[OVERWORLD_TILE_COUNT_MAX];
@ -16,3 +23,60 @@ typedef struct {
} map_t; } map_t;
void mapInit(map_t *map); void mapInit(map_t *map);
/**
* Returns the index of the tile at the given position.
*
* @param map The map to get the tile index from.
* @param layer The layer to get the tile from.
* @param x The x position of the tile.
* @param y The y position of the tile.
* @return The index of the tile at the given position.
*/
uint32_t mapGetTileIndex(
const map_t *map,
const uint8_t layer,
const uint8_t x,
const uint8_t y
);
/**
* Returns the tile ID at the given index.
*
* @param map The map to get the tile ID from.
* @param tileIndex The index of the tile to get.
* @return The tile ID at the given index.
*/
tileid_t mapGetTileId(
const map_t *map,
const uint32_t tileIndex
);
/**
* Returns the tile ID at the given position. Shorthand for both getting the
* map index and the tile ID.
*
* @param map The map to get the tile ID from.
* @param layer The layer to get the tile from.
* @param x The x position of the tile.
* @param y The y position of the tile.
* @return The tile ID at the given position.
*/
tileid_t mapGetTileIdAtPosition(
const map_t *map,
const uint8_t layer,
const uint8_t x,
const uint8_t y
);
/**
* Returns the tile data at the given index.
*
* @param map The map to get the tile data from.
* @param tileIndex The index of the tile to get.
* @return The tile data at the given index.
*/
tiledata_t mapGetTileData(
const map_t *map,
const uint32_t tileIndex
);

View File

@ -14,21 +14,10 @@ overworld_t OVERWORLD;
void overworldInit() { void overworldInit() {
memoryZero(&OVERWORLD, sizeof(overworld_t)); memoryZero(&OVERWORLD, sizeof(overworld_t));
// Test mapInit(&OVERWORLD.map);
OVERWORLD.mapWidth = 16;
OVERWORLD.mapHeight = 16;
OVERWORLD.mapLayerCount = 1;
memset(&OVERWORLD.tileIds, 0x01, sizeof(OVERWORLD.tileIds));
assertTrue(
(
OVERWORLD.mapWidth * OVERWORLD.mapHeight * OVERWORLD.mapLayerCount
) <= OVERWORLD_TILE_COUNT_MAX,
"Map size exceeds tile count."
);
entityInit(OVERWORLD.entities + OVERWORLD.entityCount++, ENTITY_TYPE_PLAYER); entityInit(OVERWORLD.entities + OVERWORLD.entityCount++, ENTITY_TYPE_PLAYER);
entityInit(OVERWORLD.entities + OVERWORLD.entityCount++, ENTITY_TYPE_NPC); entityInit(OVERWORLD.entities + OVERWORLD.entityCount++, ENTITY_TYPE_NPC);
OVERWORLD.entities[1].x = 2; OVERWORLD.entities[1].x = 2;
OVERWORLD.entities[1].y = 2; OVERWORLD.entities[1].y = 2;
@ -49,26 +38,6 @@ void overworldUpdate() {
} }
} }
uint32_t overworldTileGetIndex(
const uint8_t layer,
const uint8_t x,
const uint8_t y
) {
return (
(layer * OVERWORLD.mapWidth * OVERWORLD.mapHeight) +
(y * OVERWORLD.mapWidth) +
x
);
}
tileid_t overworldTileGet(
const uint8_t layer,
const uint8_t x,
const uint8_t y
) {
return OVERWORLD.tileIds[overworldTileGetIndex(layer, x, y)];
}
entity_t * overworldEntityGetAtPosition(const uint8_t x, const uint8_t y) { entity_t * overworldEntityGetAtPosition(const uint8_t x, const uint8_t y) {
uint8_t i = 0; uint8_t i = 0;
while(i < OVERWORLD.entityCount) { while(i < OVERWORLD.entityCount) {

View File

@ -8,15 +8,12 @@
#pragma once #pragma once
#include "overworlddefs.h" #include "overworlddefs.h"
#include "overworld/entity/entity.h" #include "overworld/entity/entity.h"
#include "overworld/map/tile.h" #include "overworld/map/map.h"
typedef struct { typedef struct {
entity_t entities[OVERWORLD_ENTITY_COUNT_MAX]; entity_t entities[OVERWORLD_ENTITY_COUNT_MAX];
uint8_t entityCount; uint8_t entityCount;
map_t map;
tileid_t tileIds[OVERWORLD_TILE_COUNT_MAX];
tiledata_t tileData[OVERWORLD_TILE_COUNT_MAX];
uint8_t mapWidth, mapHeight, mapLayerCount;
} overworld_t; } overworld_t;
extern overworld_t OVERWORLD; extern overworld_t OVERWORLD;
@ -47,34 +44,6 @@ void overworldUpdate();
*/ */
void overworldRender(); void overworldRender();
/**
* Returns the index of the tile at the given position.
*
* @param layer The layer to get the tile from.
* @param x The x position of the tile.
* @param y The y position of the tile.
* @return The index of the tile at the given position.
*/
uint32_t overworldTileGetIndex(
const uint8_t layer,
const uint8_t x,
const uint8_t y
);
/**
* Returns the tile at the given position.
*
* @param layer The layer to get the tile from.
* @param x The x position of the tile.
* @param y The y position of the tile.
* @return The tile id at the given position.
*/
tileid_t overworldTileGet(
const uint8_t layer,
const uint8_t x,
const uint8_t y
);
/** /**
* Returns the entity at the given position. * Returns the entity at the given position.
* *

View File

@ -8,5 +8,5 @@ target_sources(${DUSK_TARGET_NAME}
PRIVATE PRIVATE
transforms.c transforms.c
entities.c entities.c
map.c mapshaderdata.c
) )

View File

@ -1,50 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "map.h"
#include "assert/assert.h"
#include "util/memory.h"
shaderbuffer_t MAP_BUFFER;
mapdata_t MAP_DATA;
void mapInit() {
memoryZero(&MAP_DATA, sizeof(mapdata_t));
shaderBufferInit(&MAP_BUFFER, sizeof(mapdata_t));
assertTrue(
sizeof(MAP_DATA.tileIds) == sizeof(OVERWORLD.tileIds),
"Map shader tile data and Overworld tile data are not the same size."
);
}
void mapUpdate() {
// Copy tile ids.
memoryCopyRangeSafe(
MAP_DATA.tileIds,
OVERWORLD.tileIds,
&OVERWORLD.tileIds[
OVERWORLD.mapWidth * OVERWORLD.mapHeight * OVERWORLD.mapLayerCount
],
sizeof(MAP_DATA.tileIds)
);
// Copy map size.
memoryCopyRangeSafe(
&MAP_DATA.mapSize,
&OVERWORLD.mapWidth,
&OVERWORLD.mapLayerCount + sizeof(uint8_t),
sizeof(MAP_DATA.mapSize)
);
shaderBufferBind(&MAP_BUFFER);
shaderBufferSetData(&MAP_BUFFER, &MAP_DATA);
}
void mapDispose() {
shaderBufferDispose(&MAP_BUFFER);
}

View File

@ -0,0 +1,50 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "mapshaderdata.h"
#include "assert/assert.h"
#include "util/memory.h"
shaderbuffer_t MAP_SHADER_DATA_BUFFER;
mapshaderdata_t MAP_SHADER_DATA_DATA;
void mapShaderDataInit() {
memoryZero(&MAP_SHADER_DATA_DATA, sizeof(mapshaderdata_t));
shaderBufferInit(&MAP_SHADER_DATA_BUFFER, sizeof(mapshaderdata_t));
assertTrue(
sizeof(MAP_SHADER_DATA_DATA.tileIds) == sizeof(OVERWORLD.map.tileIds),
"Map shader tile data and Overworld tile data are not the same size."
);
}
void mapShaderDataUpdate() {
// Copy tile ids.
memoryCopyRangeSafe(
MAP_SHADER_DATA_DATA.tileIds,
OVERWORLD.map.tileIds,
&OVERWORLD.map.tileIds[
OVERWORLD.map.width * OVERWORLD.map.height * OVERWORLD.map.layerCount
],
sizeof(MAP_SHADER_DATA_DATA.tileIds)
);
// Copy map size.
memoryCopyRangeSafe(
&MAP_SHADER_DATA_DATA.mapSize,
&OVERWORLD.map.width,
&OVERWORLD.map.layerCount + sizeof(uint8_t),
sizeof(MAP_SHADER_DATA_DATA.mapSize)
);
shaderBufferBind(&MAP_SHADER_DATA_BUFFER);
shaderBufferSetData(&MAP_SHADER_DATA_BUFFER, &MAP_SHADER_DATA_DATA);
}
void mapShaderDataDispose() {
shaderBufferDispose(&MAP_SHADER_DATA_BUFFER);
}

View File

@ -16,22 +16,22 @@ typedef struct {
uvec4_t tileIds[OVERWORLD_TILE_COUNT_MAX / MAP_TILE_PACK_SIZE]; uvec4_t tileIds[OVERWORLD_TILE_COUNT_MAX / MAP_TILE_PACK_SIZE];
uint_t mapSize; uint_t mapSize;
uvec3_t _p0; uvec3_t _p0;
} mapdata_t; } mapshaderdata_t;
extern shaderbuffer_t MAP_BUFFER; extern shaderbuffer_t MAP_SHADER_DATA_BUFFER;
extern mapdata_t MAP_DATA; extern mapshaderdata_t MAP_SHADER_DATA_DATA;
/** /**
* Initializes the map buffer and data. * Initializes the map buffer and data.
*/ */
void mapInit(); void mapShaderDataInit();
/** /**
* Updates the map buffer with the current data. * Updates the map buffer with the current data.
*/ */
void mapUpdate(); void mapShaderDataUpdate();
/** /**
* Destroys the map buffer. * Destroys the map buffer.
*/ */
void mapDispose(); void mapShaderDataDispose();

View File

@ -7,7 +7,7 @@
#include "mapshader.h" #include "mapshader.h"
#include "util/memory.h" #include "util/memory.h"
#include "display/shader/data/map.h" #include "display/shader/data/mapshaderdata.h"
#include "display/shader/data/transforms.h" #include "display/shader/data/transforms.h"
#include "map_vert.glsl.h" #include "map_vert.glsl.h"
#include "map_frag.glsl.h" #include "map_frag.glsl.h"
@ -37,7 +37,7 @@ void mapShaderInit() {
void mapShaderUse() { void mapShaderUse() {
shaderUse(&MAP_SHADER.shader); shaderUse(&MAP_SHADER.shader);
shaderBufferBindToBlock(&MAP_BUFFER, MAP_SHADER.mapBlock); shaderBufferBindToBlock(&MAP_SHADER_DATA_BUFFER, MAP_SHADER.mapBlock);
shaderBufferBindToBlock(&TRANSFORMS_BUFFER, MAP_SHADER.transformsBlock); shaderBufferBindToBlock(&TRANSFORMS_BUFFER, MAP_SHADER.transformsBlock);
} }

View File

@ -9,14 +9,14 @@
#include "assert/assert.h" #include "assert/assert.h"
#include "display/shader/data/transforms.h" #include "display/shader/data/transforms.h"
#include "display/shader/data/entities.h" #include "display/shader/data/entities.h"
#include "display/shader/data/map.h" #include "display/shader/data/mapshaderdata.h"
#include "display/shader/entityshader/entityshader.h" #include "display/shader/entityshader/entityshader.h"
#include "display/shader/mapshader/mapshader.h" #include "display/shader/mapshader/mapshader.h"
shadermanagerdatacallback_t SHADER_MANAGER_DATA_CALLBACKS[] = { shadermanagerdatacallback_t SHADER_MANAGER_DATA_CALLBACKS[] = {
{ transformsInit, transformsUpdate, transformsDispose }, { transformsInit, transformsUpdate, transformsDispose },
{ entitiesInit, entitiesUpdate, entitiesDispose }, { entitiesInit, entitiesUpdate, entitiesDispose },
{ mapInit, mapUpdate, mapDispose } { mapShaderDataInit, mapShaderDataUpdate, mapShaderDataDispose }
}; };

View File

@ -13,7 +13,7 @@
void overworldRender() { void overworldRender() {
mapShaderUse(); mapShaderUse();
quadRender( quadRender(
OVERWORLD.mapWidth * OVERWORLD.mapHeight * OVERWORLD.mapLayerCount OVERWORLD.map.width * OVERWORLD.map.height * OVERWORLD.map.layerCount
); );
entityShaderUse(); entityShaderUse();