Cleaned map a bit
This commit is contained in:
@ -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;
|
||||
}
|
||||
|
||||
|
@ -7,6 +7,7 @@
|
||||
target_sources(${DUSK_TARGET_NAME}
|
||||
PRIVATE
|
||||
tile.c
|
||||
map.c
|
||||
)
|
||||
|
||||
# Subdirs
|
@ -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];
|
||||
}
|
@ -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);
|
||||
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
|
||||
);
|
@ -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) {
|
||||
|
@ -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.
|
||||
*
|
||||
|
@ -8,5 +8,5 @@ target_sources(${DUSK_TARGET_NAME}
|
||||
PRIVATE
|
||||
transforms.c
|
||||
entities.c
|
||||
map.c
|
||||
mapshaderdata.c
|
||||
)
|
@ -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);
|
||||
}
|
50
src/duskgl/display/shader/data/mapshaderdata.c
Normal file
50
src/duskgl/display/shader/data/mapshaderdata.c
Normal 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);
|
||||
}
|
@ -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();
|
||||
void mapShaderDataDispose();
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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 }
|
||||
};
|
||||
|
||||
|
||||
|
@ -13,7 +13,7 @@
|
||||
void overworldRender() {
|
||||
mapShaderUse();
|
||||
quadRender(
|
||||
OVERWORLD.mapWidth * OVERWORLD.mapHeight * OVERWORLD.mapLayerCount
|
||||
OVERWORLD.map.width * OVERWORLD.map.height * OVERWORLD.map.layerCount
|
||||
);
|
||||
|
||||
entityShaderUse();
|
||||
|
Reference in New Issue
Block a user