Map shader base, add memory util.
This commit is contained in:
@ -8,4 +8,5 @@ target_sources(${DUSK_TARGET_NAME}
|
||||
PRIVATE
|
||||
transforms.c
|
||||
entities.c
|
||||
map.c
|
||||
)
|
@ -8,6 +8,7 @@
|
||||
#include "entities.h"
|
||||
#include "overworld/overworld.h"
|
||||
#include "assert/assert.h"
|
||||
#include "util/memory.h"
|
||||
|
||||
shaderbuffer_t ENTITIES_BUFFER;
|
||||
entitiesdata_t ENTITIES_DATA;
|
||||
@ -27,13 +28,12 @@ void entitiesUpdate() {
|
||||
entitiesdataent_t *dst = &ENTITIES_DATA.entities[i];
|
||||
|
||||
// Copy position data.
|
||||
assertMemoryRangeMatches(
|
||||
memoryCopyRangeSafe(
|
||||
&dst->position,
|
||||
&src->x,
|
||||
&src->direction,
|
||||
sizeof(uint_t),
|
||||
"Entity data is not packed correctly any more."
|
||||
&src->subY + sizeof(uint8_t),
|
||||
sizeof(uint_t)
|
||||
);
|
||||
memcpy(&dst->position, &src->x, sizeof(uint_t));
|
||||
}
|
||||
}
|
||||
|
||||
|
43
src/duskgl/display/shader/data/map.c
Normal file
43
src/duskgl/display/shader/data/map.c
Normal file
@ -0,0 +1,43 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "map.h"
|
||||
#include "util/memory.h"
|
||||
|
||||
shaderbuffer_t MAP_BUFFER;
|
||||
mapdata_t MAP_DATA;
|
||||
|
||||
void mapInit() {
|
||||
memset(&MAP_DATA, 0, sizeof(mapdata_t));
|
||||
shaderBufferInit(&MAP_BUFFER, sizeof(mapdata_t));
|
||||
}
|
||||
|
||||
void mapUpdate() {
|
||||
|
||||
// Copy map size.
|
||||
memoryCopyRangeSafe(
|
||||
&MAP_DATA.mapSize,
|
||||
&OVERWORLD.mapWidth,
|
||||
&OVERWORLD.mapHeight + sizeof(uint8_t),
|
||||
sizeof(uint_t)
|
||||
);
|
||||
|
||||
// Copy tile ids.
|
||||
memoryCopyRangeSafe(
|
||||
&MAP_DATA.tileIds,
|
||||
&OVERWORLD.tileIds[0],
|
||||
&OVERWORLD.tileIds[OVERWORLD.mapWidth * OVERWORLD.mapHeight],
|
||||
OVERWORLD_TILE_COUNT_MAX * sizeof(tileid_t)
|
||||
);
|
||||
|
||||
shaderBufferBind(&MAP_BUFFER);
|
||||
shaderBufferSetData(&MAP_BUFFER, &MAP_DATA);
|
||||
}
|
||||
|
||||
void mapDispose() {
|
||||
shaderBufferDispose(&MAP_BUFFER);
|
||||
}
|
42
src/duskgl/display/shader/data/map.glsl
Normal file
42
src/duskgl/display/shader/data/map.glsl
Normal file
@ -0,0 +1,42 @@
|
||||
// Copyright (c) 2025 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "../../../../dusk/overworld/overworlddefs.h"
|
||||
#include "../fragments/packed.glsl"
|
||||
#include "../fragments/quad.glsl"
|
||||
|
||||
#define MAP_PACKED_SIZE 4
|
||||
|
||||
struct TileData {
|
||||
uint nothing;
|
||||
uvec3 _padding0;
|
||||
};
|
||||
|
||||
layout(std140) uniform b_Map {
|
||||
uint mapSize;
|
||||
uint mapTileIds[OVERWORLD_TILE_COUNT_MAX / MAP_PACKED_SIZE];
|
||||
TileData mapTileData[OVERWORLD_TILE_COUNT_MAX];
|
||||
uvec3 _padding0;
|
||||
};
|
||||
|
||||
vec2 mapTileGetSize() {
|
||||
return vec2(float(OVERWORLD_TILE_WIDTH), float(OVERWORLD_TILE_HEIGHT));
|
||||
}
|
||||
|
||||
vec2 mapGetVertice(uint instanceIndex, uint indiceIndex) {
|
||||
vec2 quad = quadGetVertice(indiceIndex);
|
||||
|
||||
uint mapWidth = packedGetU8(0u, mapSize);
|
||||
uint mapHeight = packedGetU8(1u, mapSize);
|
||||
|
||||
quad += vec2(
|
||||
float(instanceIndex % mapWidth),
|
||||
float(instanceIndex / mapWidth)
|
||||
);
|
||||
|
||||
quad *= mapTileGetSize();
|
||||
|
||||
return quad;
|
||||
}
|
44
src/duskgl/display/shader/data/map.h
Normal file
44
src/duskgl/display/shader/data/map.h
Normal file
@ -0,0 +1,44 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma ocne
|
||||
#include "display/shader/shaderbuffer.h"
|
||||
#include "overworld/overworld.h"
|
||||
|
||||
#define MAP_BLOCK_NAME "b_Map"
|
||||
#define MAP_PACKED_SIZE 4
|
||||
|
||||
typedef struct {
|
||||
uint32_t nothing;
|
||||
uvec3_t _padding0;
|
||||
} maptiledata_t;
|
||||
|
||||
typedef struct {
|
||||
uint_t mapSize;
|
||||
uvec3_t _padding0;
|
||||
|
||||
uint32_t tileIds[OVERWORLD_TILE_COUNT_MAX / MAP_PACKED_SIZE];
|
||||
maptiledata_t tileData[OVERWORLD_TILE_COUNT_MAX];
|
||||
} mapdata_t;
|
||||
|
||||
extern shaderbuffer_t MAP_BUFFER;
|
||||
extern mapdata_t MAP_DATA;
|
||||
|
||||
/**
|
||||
* Initializes the map buffer and data.
|
||||
*/
|
||||
void mapInit();
|
||||
|
||||
/**
|
||||
* Updates the map buffer with the current data.
|
||||
*/
|
||||
void mapUpdate();
|
||||
|
||||
/**
|
||||
* Destroys the map buffer.
|
||||
*/
|
||||
void mapDispose();
|
Reference in New Issue
Block a user