Add extensive tests for animation, input handling, and map rendering

- Enhanced animation archive tests to validate frame metadata and start frame selection logic.
- Introduced new tests for entity dispatch entry handling, including key event queuing and processing.
- Added tests for fixed map item interactions, including glob egg hatching and item movement.
- Expanded flex archive tests to cover teleport egg spawning and usecode event selection.
- Implemented isometric controls tests to verify directional key handling and animation state changes.
- Improved map renderer tests to ensure correct rendering of map previews and collision detection.
This commit is contained in:
Maddo 2026-09-27 11:48:14 +02:00
commit e757bb33d0
20 changed files with 1247 additions and 142 deletions

View file

@ -39,7 +39,7 @@ struct SceneItem {
struct RenderNode {
SceneItem item;
DecodedShapeFrame decoded;
const DecodedShapeFrame* decoded = nullptr;
s32 left = 0;
s32 top = 0;
s32 right = 0;
@ -82,14 +82,27 @@ std::vector<SceneItem> collectSceneItems(
const FixedMap& fixedMap,
const std::vector<ShapeTypeInfo>& typeFlags,
const std::vector<GlobObject>& globs,
const MapRenderOptions& options)
const MapRenderOptions& options,
const LiveFixedMapItems* liveItems)
{
std::vector<SceneItem> pending;
pending.reserve(fixedMap.entries.size());
for (const FixedMapEntry& entry : fixedMap.entries) {
std::vector<const StagedFixedMapItem*> liveByRecord;
if (liveItems != nullptr) {
liveByRecord.resize(fixedMap.entries.size(), nullptr);
for (const auto& item : liveItems->mapItems()) {
if (item.recordIndex < liveByRecord.size()) {
liveByRecord[item.recordIndex] = &item;
}
}
}
for (std::size_t recordIndex = 0; recordIndex < fixedMap.entries.size(); ++recordIndex) {
const FixedMapEntry& entry = liveByRecord.empty() || liveByRecord[recordIndex] == nullptr
? fixedMap.entries[recordIndex] : liveByRecord[recordIndex]->data;
const s32 scale = liveByRecord.empty() || liveByRecord[recordIndex] == nullptr ? 1 : 2;
pending.push_back(SceneItem{
static_cast<s32>(entry.x),
static_cast<s32>(entry.y),
static_cast<s32>(entry.x) * scale,
static_cast<s32>(entry.y) * scale,
static_cast<s32>(entry.z),
entry.shape,
entry.frame,
@ -98,6 +111,16 @@ std::vector<SceneItem> collectSceneItems(
entry.mapNumber,
true});
}
if (liveItems != nullptr) {
for (const auto& item : liveItems->mapItems()) {
if (item.recordIndex == static_cast<std::size_t>(-1)) {
pending.push_back(SceneItem{static_cast<s32>(item.data.x) * 2,
static_cast<s32>(item.data.y) * 2, item.data.z, item.data.shape,
item.data.frame, item.data.flags, item.data.quality, item.data.mapNumber,
false});
}
}
}
std::vector<SceneItem> items;
for (std::size_t index = 0; index < pending.size(); ++index) {
@ -116,7 +139,10 @@ std::vector<SceneItem> collectSceneItems(
continue;
}
if (options.expandGlobs && info.family == 3 && item.fromFixed) {
const auto glob = findGlobObject(globs, item.quality);
const auto* stagedEgg = liveByRecord.empty() || index >= liveByRecord.size()
? nullptr : liveByRecord[index];
const auto glob = stagedEgg && liveItems->isGlobHatched(stagedEgg->itemNumber)
? std::nullopt : findGlobObject(globs, item.quality);
if (glob) {
for (const GlobObjectEntry& entry : glob->entries) {
pending.push_back(SceneItem{
@ -153,7 +179,7 @@ s32 screenY(const SceneItem& item)
return projectToScreen(WorldPoint{item.x, item.y, item.z}).y;
}
std::optional<DecodedShapeFrame> cachedDecode(
const DecodedShapeFrame* cachedDecode(
const ShapeArchive& archive,
std::map<std::pair<u32, u32>, DecodedShapeFrame>& cache,
u32 shape,
@ -162,28 +188,27 @@ std::optional<DecodedShapeFrame> cachedDecode(
const auto key = std::make_pair(shape, frame);
const auto found = cache.find(key);
if (found != cache.end()) {
return found->second;
return &found->second;
}
const auto decoded = decodeShapeFrame(archive, shape, frame);
if (!decoded) {
return std::nullopt;
return nullptr;
}
cache.emplace(key, *decoded);
return decoded;
return &cache.emplace(key, std::move(*decoded)).first->second;
}
std::vector<RenderNode> prepareRenderNodes(
const std::vector<SceneItem>& items,
const ShapeArchive& shapeArchive,
const std::vector<ShapeTypeInfo>& typeFlags)
const std::vector<ShapeTypeInfo>& typeFlags,
std::map<std::pair<u32, u32>, DecodedShapeFrame>& cache)
{
std::map<std::pair<u32, u32>, DecodedShapeFrame> cache;
std::vector<RenderNode> nodes;
std::vector<detail::RenderSortItem> sortItems;
nodes.reserve(items.size());
sortItems.reserve(items.size());
for (const SceneItem& item : items) {
const auto decoded = cachedDecode(shapeArchive, cache, item.shape, item.frame);
const auto* decoded = cachedDecode(shapeArchive, cache, item.shape, item.frame);
if (!decoded) {
continue;
}
@ -193,13 +218,13 @@ std::vector<RenderNode> prepareRenderNodes(
const bool flipped = (item.flags & flagFlipped) != 0;
RenderNode node;
node.item = item;
node.decoded = *decoded;
node.decoded = decoded;
node.left = flipped
? bottomX + node.decoded.frame.xOffset - static_cast<s32>(node.decoded.frame.width)
: bottomX - node.decoded.frame.xOffset;
node.top = bottomY - node.decoded.frame.yOffset;
node.right = node.left + static_cast<s32>(node.decoded.frame.width);
node.bottom = node.top + static_cast<s32>(node.decoded.frame.height);
? bottomX + decoded->frame.xOffset - static_cast<s32>(decoded->frame.width)
: bottomX - decoded->frame.xOffset;
node.top = bottomY - decoded->frame.yOffset;
node.right = node.left + static_cast<s32>(decoded->frame.width);
node.bottom = node.top + static_cast<s32>(decoded->frame.height);
const ShapeTypeInfo& info = typeFlags[item.shape];
const s32 xDimension = static_cast<s32>(flipped ? info.y : info.x) * 32;
@ -251,15 +276,15 @@ std::vector<RenderNode> prepareRenderNodes(
void paintNode(RenderedMapImage& image, const RenderNode& node, const Palette256& palette)
{
const bool flipped = (node.item.flags & flagFlipped) != 0;
for (u32 sourceY = 0; sourceY < node.decoded.frame.height; ++sourceY) {
for (u32 sourceY = 0; sourceY < node.decoded->frame.height; ++sourceY) {
const s32 destY = node.top - image.sourceTop + static_cast<s32>(sourceY);
if (destY < 0 || destY >= static_cast<s32>(image.height)) {
continue;
}
const std::size_t sourceRow = static_cast<std::size_t>(sourceY) * node.decoded.frame.width;
for (u32 sourceX = 0; sourceX < node.decoded.frame.width; ++sourceX) {
const u32 actualSourceX = flipped ? node.decoded.frame.width - 1 - sourceX : sourceX;
const s16 colorIndex = node.decoded.pixels[sourceRow + actualSourceX];
const std::size_t sourceRow = static_cast<std::size_t>(sourceY) * node.decoded->frame.width;
for (u32 sourceX = 0; sourceX < node.decoded->frame.width; ++sourceX) {
const u32 actualSourceX = flipped ? node.decoded->frame.width - 1 - sourceX : sourceX;
const s16 colorIndex = node.decoded->pixels[sourceRow + actualSourceX];
if (colorIndex < 0 || colorIndex >= 256) {
continue;
}
@ -293,7 +318,7 @@ ScreenPoint projectToScreen(const WorldPoint& point)
return ScreenPoint{(point.x - point.y) / 4, (point.x + point.y) / 8 - point.z};
}
std::optional<RenderedMapImage> renderMapPreview(const GameDataRoot& root, const MapRenderOptions& options)
std::optional<MapSceneRenderer> loadMapSceneRenderer(const GameDataRoot& root, u32 mapIndex)
{
const auto fixedPath = resolveGameDataPath(root, std::filesystem::path("STATIC") / "FIXED.DAT");
const auto globPath = resolveGameDataPath(root, std::filesystem::path("STATIC") / "GLOB.FLX");
@ -305,7 +330,7 @@ std::optional<RenderedMapImage> renderMapPreview(const GameDataRoot& root, const
}
const auto fixedArchive = loadFixedMapArchive(*fixedPath);
const auto fixedMap = fixedArchive ? loadFixedMap(*fixedArchive, options.mapIndex) : std::nullopt;
const auto fixedMap = fixedArchive ? loadFixedMap(*fixedArchive, mapIndex) : std::nullopt;
const auto globs = loadGlobObjects(*globPath);
const auto shapeArchive = loadShapeArchive(*shapePath);
const auto palette = loadPalette256(*palettePath);
@ -313,9 +338,15 @@ std::optional<RenderedMapImage> renderMapPreview(const GameDataRoot& root, const
if (!fixedMap || !globs || !shapeArchive || !palette || !typeFlags) {
return std::nullopt;
}
return MapSceneRenderer{std::move(*fixedMap), std::move(*globs), std::move(*shapeArchive),
*palette, std::move(*typeFlags), {}};
}
const auto items = collectSceneItems(*fixedMap, *typeFlags, *globs, options);
auto nodes = prepareRenderNodes(items, *shapeArchive, *typeFlags);
std::optional<RenderedMapImage> MapSceneRenderer::render(const MapRenderOptions& options,
const LiveFixedMapItems* liveItems)
{
const auto items = collectSceneItems(map, typeFlags, globs, options, liveItems);
auto nodes = prepareRenderNodes(items, shapes, typeFlags, decodedFrames);
if (nodes.empty()) {
return std::nullopt;
}
@ -332,7 +363,7 @@ std::optional<RenderedMapImage> renderMapPreview(const GameDataRoot& root, const
}
RenderedMapImage image;
image.rawItemCount = static_cast<u32>(fixedMap->entries.size());
image.rawItemCount = static_cast<u32>(map.entries.size());
image.renderItemCount = static_cast<u32>(items.size());
image.decodedItemCount = static_cast<u32>(nodes.size());
image.fullWidth = maxRight - minLeft;
@ -361,12 +392,25 @@ std::optional<RenderedMapImage> renderMapPreview(const GameDataRoot& root, const
node.top >= image.sourceTop + static_cast<s32>(image.height)) {
continue;
}
paintNode(image, node, *palette);
paintNode(image, node, palette);
++image.paintedItemCount;
}
return image;
}
std::optional<RenderedMapImage> renderMapPreview(const GameDataRoot& root, const MapRenderOptions& options)
{
auto renderer = loadMapSceneRenderer(root, options.mapIndex);
return renderer ? renderer->render(options) : std::nullopt;
}
std::optional<RenderedMapImage> renderLiveMapPreview(const GameDataRoot& root,
const MapRenderOptions& options, const LiveFixedMapItems& items)
{
auto renderer = loadMapSceneRenderer(root, options.mapIndex);
return renderer ? renderer->render(options, &items) : std::nullopt;
}
bool writeBmp24(const std::filesystem::path& path, const RenderedMapImage& image)
{
if (image.width == 0 || image.height == 0 || image.pixels.size() != static_cast<std::size_t>(image.width) * image.height) {