From f19636edb1a835d3b797ce8a0b09ee9df8c2bd74 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Tue, 4 Mar 2025 11:50:47 -0600 Subject: [PATCH] Cleaned map a bit --- src/dusk/overworld/entity/entity.c | 8 +-- src/dusk/overworld/map/CMakeLists.txt | 1 + src/dusk/overworld/map/map.c | 55 ++++++++++++++++ src/dusk/overworld/map/map.h | 66 ++++++++++++++++++- src/dusk/overworld/overworld.c | 37 +---------- src/dusk/overworld/overworld.h | 35 +--------- src/duskgl/display/shader/data/CMakeLists.txt | 2 +- src/duskgl/display/shader/data/map.c | 50 -------------- .../display/shader/data/mapshaderdata.c | 50 ++++++++++++++ .../shader/data/{map.h => mapshaderdata.h} | 12 ++-- .../display/shader/mapshader/mapshader.c | 4 +- src/duskgl/display/shader/shadermanager.c | 4 +- src/duskgl/overworld/overworld.c | 2 +- 13 files changed, 192 insertions(+), 134 deletions(-) delete mode 100644 src/duskgl/display/shader/data/map.c create mode 100644 src/duskgl/display/shader/data/mapshaderdata.c rename src/duskgl/display/shader/data/{map.h => mapshaderdata.h} (75%) diff --git a/src/dusk/overworld/entity/entity.c b/src/dusk/overworld/entity/entity.c index d4b9ec3..0e85b8b 100644 --- a/src/dusk/overworld/entity/entity.c +++ b/src/dusk/overworld/entity/entity.c @@ -73,14 +73,14 @@ void entityMove(entity_t *ent, const facingdir_t dir) { facingDirAdd(ent->direction, &targetX, &targetY); // Check oob - if(targetX < 0 || targetX >= OVERWORLD.mapWidth) return; - if(targetY < 0 || targetY >= OVERWORLD.mapHeight) return; + if(targetX < 0 || targetX >= OVERWORLD.map.width) return; + if(targetY < 0 || targetY >= OVERWORLD.map.height) return; // Check tile at target uint8_t i = 0; tileid_t tileId; - for(i = 0; i < OVERWORLD.mapLayerCount; i++) { - tileId = overworldTileGet(0, targetX, targetY); + for(i = 0; i < OVERWORLD.map.layerCount; i++) { + tileId = mapGetTileIdAtPosition(&OVERWORLD.map, i, targetX, targetY); if(tileIsSolid(tileId)) return; } diff --git a/src/dusk/overworld/map/CMakeLists.txt b/src/dusk/overworld/map/CMakeLists.txt index 81d07e0..6a46430 100644 --- a/src/dusk/overworld/map/CMakeLists.txt +++ b/src/dusk/overworld/map/CMakeLists.txt @@ -7,6 +7,7 @@ target_sources(${DUSK_TARGET_NAME} PRIVATE tile.c + map.c ) # Subdirs \ No newline at end of file diff --git a/src/dusk/overworld/map/map.c b/src/dusk/overworld/map/map.c index b6338d7..a2a1f3e 100644 --- a/src/dusk/overworld/map/map.c +++ b/src/dusk/overworld/map/map.c @@ -11,4 +11,59 @@ void mapInit(map_t *map) { 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]; } \ No newline at end of file diff --git a/src/dusk/overworld/map/map.h b/src/dusk/overworld/map/map.h index c5a922d..120a1df 100644 --- a/src/dusk/overworld/map/map.h +++ b/src/dusk/overworld/map/map.h @@ -8,6 +8,13 @@ #pragma once #include "tile.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 { tileid_t tileIds[OVERWORLD_TILE_COUNT_MAX]; @@ -15,4 +22,61 @@ typedef struct { uint8_t width, height, layerCount; } map_t; -void mapInit(map_t *map); \ No newline at end of file +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 +); \ No newline at end of file diff --git a/src/dusk/overworld/overworld.c b/src/dusk/overworld/overworld.c index e658979..7bb5bb0 100644 --- a/src/dusk/overworld/overworld.c +++ b/src/dusk/overworld/overworld.c @@ -14,21 +14,10 @@ overworld_t OVERWORLD; void overworldInit() { memoryZero(&OVERWORLD, sizeof(overworld_t)); - // Test - 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." - ); - + mapInit(&OVERWORLD.map); + entityInit(OVERWORLD.entities + OVERWORLD.entityCount++, ENTITY_TYPE_PLAYER); + entityInit(OVERWORLD.entities + OVERWORLD.entityCount++, ENTITY_TYPE_NPC); OVERWORLD.entities[1].x = 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) { uint8_t i = 0; while(i < OVERWORLD.entityCount) { diff --git a/src/dusk/overworld/overworld.h b/src/dusk/overworld/overworld.h index b1cb7fa..2185ed8 100644 --- a/src/dusk/overworld/overworld.h +++ b/src/dusk/overworld/overworld.h @@ -8,15 +8,12 @@ #pragma once #include "overworlddefs.h" #include "overworld/entity/entity.h" -#include "overworld/map/tile.h" +#include "overworld/map/map.h" typedef struct { entity_t entities[OVERWORLD_ENTITY_COUNT_MAX]; uint8_t entityCount; - - tileid_t tileIds[OVERWORLD_TILE_COUNT_MAX]; - tiledata_t tileData[OVERWORLD_TILE_COUNT_MAX]; - uint8_t mapWidth, mapHeight, mapLayerCount; + map_t map; } overworld_t; extern overworld_t OVERWORLD; @@ -47,34 +44,6 @@ void overworldUpdate(); */ 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. * diff --git a/src/duskgl/display/shader/data/CMakeLists.txt b/src/duskgl/display/shader/data/CMakeLists.txt index e9ef5a3..babf1c4 100644 --- a/src/duskgl/display/shader/data/CMakeLists.txt +++ b/src/duskgl/display/shader/data/CMakeLists.txt @@ -8,5 +8,5 @@ target_sources(${DUSK_TARGET_NAME} PRIVATE transforms.c entities.c - map.c + mapshaderdata.c ) \ No newline at end of file diff --git a/src/duskgl/display/shader/data/map.c b/src/duskgl/display/shader/data/map.c deleted file mode 100644 index 3141f5f..0000000 --- a/src/duskgl/display/shader/data/map.c +++ /dev/null @@ -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); -} \ No newline at end of file diff --git a/src/duskgl/display/shader/data/mapshaderdata.c b/src/duskgl/display/shader/data/mapshaderdata.c new file mode 100644 index 0000000..f072de5 --- /dev/null +++ b/src/duskgl/display/shader/data/mapshaderdata.c @@ -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); +} \ No newline at end of file diff --git a/src/duskgl/display/shader/data/map.h b/src/duskgl/display/shader/data/mapshaderdata.h similarity index 75% rename from src/duskgl/display/shader/data/map.h rename to src/duskgl/display/shader/data/mapshaderdata.h index dbf1a57..9fa75a9 100644 --- a/src/duskgl/display/shader/data/map.h +++ b/src/duskgl/display/shader/data/mapshaderdata.h @@ -16,22 +16,22 @@ typedef struct { uvec4_t tileIds[OVERWORLD_TILE_COUNT_MAX / MAP_TILE_PACK_SIZE]; uint_t mapSize; uvec3_t _p0; -} mapdata_t; +} mapshaderdata_t; -extern shaderbuffer_t MAP_BUFFER; -extern mapdata_t MAP_DATA; +extern shaderbuffer_t MAP_SHADER_DATA_BUFFER; +extern mapshaderdata_t MAP_SHADER_DATA_DATA; /** * Initializes the map buffer and data. */ -void mapInit(); +void mapShaderDataInit(); /** * Updates the map buffer with the current data. */ -void mapUpdate(); +void mapShaderDataUpdate(); /** * Destroys the map buffer. */ -void mapDispose(); \ No newline at end of file +void mapShaderDataDispose(); \ No newline at end of file diff --git a/src/duskgl/display/shader/mapshader/mapshader.c b/src/duskgl/display/shader/mapshader/mapshader.c index 12f277b..8ea1f56 100644 --- a/src/duskgl/display/shader/mapshader/mapshader.c +++ b/src/duskgl/display/shader/mapshader/mapshader.c @@ -7,7 +7,7 @@ #include "mapshader.h" #include "util/memory.h" -#include "display/shader/data/map.h" +#include "display/shader/data/mapshaderdata.h" #include "display/shader/data/transforms.h" #include "map_vert.glsl.h" #include "map_frag.glsl.h" @@ -37,7 +37,7 @@ void mapShaderInit() { void mapShaderUse() { shaderUse(&MAP_SHADER.shader); - shaderBufferBindToBlock(&MAP_BUFFER, MAP_SHADER.mapBlock); + shaderBufferBindToBlock(&MAP_SHADER_DATA_BUFFER, MAP_SHADER.mapBlock); shaderBufferBindToBlock(&TRANSFORMS_BUFFER, MAP_SHADER.transformsBlock); } diff --git a/src/duskgl/display/shader/shadermanager.c b/src/duskgl/display/shader/shadermanager.c index 93f8257..dd16e22 100644 --- a/src/duskgl/display/shader/shadermanager.c +++ b/src/duskgl/display/shader/shadermanager.c @@ -9,14 +9,14 @@ #include "assert/assert.h" #include "display/shader/data/transforms.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/mapshader/mapshader.h" shadermanagerdatacallback_t SHADER_MANAGER_DATA_CALLBACKS[] = { { transformsInit, transformsUpdate, transformsDispose }, { entitiesInit, entitiesUpdate, entitiesDispose }, - { mapInit, mapUpdate, mapDispose } + { mapShaderDataInit, mapShaderDataUpdate, mapShaderDataDispose } }; diff --git a/src/duskgl/overworld/overworld.c b/src/duskgl/overworld/overworld.c index 59fc1da..8552d1d 100644 --- a/src/duskgl/overworld/overworld.c +++ b/src/duskgl/overworld/overworld.c @@ -13,7 +13,7 @@ void overworldRender() { mapShaderUse(); quadRender( - OVERWORLD.mapWidth * OVERWORLD.mapHeight * OVERWORLD.mapLayerCount + OVERWORLD.map.width * OVERWORLD.map.height * OVERWORLD.map.layerCount ); entityShaderUse();