From 6795c9afb682574a31289143b6b098c2044f0be6 Mon Sep 17 00:00:00 2001 From: Laserlicht <13953785+Laserlicht@users.noreply.github.com> Date: Sat, 30 Nov 2024 15:44:09 +0100 Subject: [PATCH] animations working --- client/mapView/MapRenderer.cpp | 26 ++++++++++++++++---------- client/mapView/MapRenderer.h | 3 ++- client/render/AssetGenerator.cpp | 1 + client/render/CAnimation.cpp | 7 ++++--- client/render/CAnimation.h | 2 +- 5 files changed, 24 insertions(+), 15 deletions(-) diff --git a/client/mapView/MapRenderer.cpp b/client/mapView/MapRenderer.cpp index 32762304f..ad9d88ab7 100644 --- a/client/mapView/MapRenderer.cpp +++ b/client/mapView/MapRenderer.cpp @@ -121,15 +121,25 @@ void MapTileStorage::load(size_t index, const AnimationPath & filename, EImageBl terrainAnimations[3]->horizontalFlip(); } -std::shared_ptr MapTileStorage::find(size_t fileIndex, size_t rotationIndex, size_t imageIndex) +std::shared_ptr MapTileStorage::find(size_t fileIndex, size_t rotationIndex, size_t imageIndex, size_t groupIndex) { const auto & animation = animations[fileIndex][rotationIndex]; if (animation) - return animation->getImage(imageIndex); // ask for group + return animation->getImage(imageIndex, groupIndex); // ask for group else return nullptr; } +int MapTileStorage::groupCount(size_t fileIndex, size_t rotationIndex, size_t imageIndex) +{ + const auto & animation = animations[fileIndex][rotationIndex]; + if (animation) + for(int i = 0; true; i++) + if(!animation->getImage(imageIndex, i, false)) + return i; + return 1; +} + MapRendererTerrain::MapRendererTerrain() : storage(VLC->terrainTypeHandler->objects.size()) { @@ -147,7 +157,8 @@ void MapRendererTerrain::renderTile(IMapRendererContext & context, Canvas & targ int32_t imageIndex = mapTile.terView; int32_t rotationIndex = mapTile.extTileFlags % 4; - const auto & image = storage.find(terrainIndex, rotationIndex, imageIndex); + int groupCount = storage.groupCount(terrainIndex, rotationIndex, imageIndex); + const auto & image = storage.find(terrainIndex, rotationIndex, imageIndex, groupCount > 1 ? context.terrainImageIndex(groupCount) : 0); assert(image); if (!image) @@ -156,9 +167,6 @@ void MapRendererTerrain::renderTile(IMapRendererContext & context, Canvas & targ return; } - for( auto const & element : mapTile.getTerrain()->paletteAnimation) - image->shiftPalette(element.start, element.length, context.terrainImageIndex(element.length)); - target.draw(image, Point(0, 0)); } @@ -191,10 +199,8 @@ void MapRendererRiver::renderTile(IMapRendererContext & context, Canvas & target int32_t imageIndex = mapTile.riverDir; int32_t rotationIndex = (mapTile.extTileFlags >> 2) % 4; - const auto & image = storage.find(terrainIndex, rotationIndex, imageIndex); - - for( auto const & element : mapTile.getRiver()->paletteAnimation) - image->shiftPalette(element.start, element.length, context.terrainImageIndex(element.length)); + int groupCount = storage.groupCount(terrainIndex, rotationIndex, imageIndex); + const auto & image = storage.find(terrainIndex, rotationIndex, imageIndex, groupCount > 1 ? context.terrainImageIndex(groupCount) : 0); target.draw(image, Point(0, 0)); } diff --git a/client/mapView/MapRenderer.h b/client/mapView/MapRenderer.h index e248f8cec..085846c63 100644 --- a/client/mapView/MapRenderer.h +++ b/client/mapView/MapRenderer.h @@ -33,7 +33,8 @@ class MapTileStorage public: explicit MapTileStorage(size_t capacity); void load(size_t index, const AnimationPath & filename, EImageBlitMode blitMode); - std::shared_ptr find(size_t fileIndex, size_t rotationIndex, size_t imageIndex); + std::shared_ptr find(size_t fileIndex, size_t rotationIndex, size_t imageIndex, size_t groupIndex = 0); + int groupCount(size_t fileIndex, size_t rotationIndex, size_t imageIndex); }; class MapRendererTerrain diff --git a/client/render/AssetGenerator.cpp b/client/render/AssetGenerator.cpp index 1fa5f83a2..4f150109f 100644 --- a/client/render/AssetGenerator.cpp +++ b/client/render/AssetGenerator.cpp @@ -409,6 +409,7 @@ void AssetGenerator::createPaletteShiftedSprites() img->shiftPalette(tmp.start, tmp.length, l % tmp.length); } } + Canvas canvas = Canvas(Point(32, 32), CanvasScalingPolicy::IGNORE); canvas.draw(img, Point((32 - img->dimensions().x) / 2, (32 - img->dimensions().y) / 2)); std::shared_ptr image = GH.renderHandler().createImage(canvas.getInternalSurface()); diff --git a/client/render/CAnimation.cpp b/client/render/CAnimation.cpp index eeafff200..50276cf5e 100644 --- a/client/render/CAnimation.cpp +++ b/client/render/CAnimation.cpp @@ -18,11 +18,12 @@ #include "../../lib/filesystem/Filesystem.h" #include "../../lib/json/JsonUtils.h" -bool CAnimation::loadFrame(size_t frame, size_t group) +bool CAnimation::loadFrame(size_t frame, size_t group, bool verbose) { if(size(group) <= frame) { - printError(frame, group, "LoadFrame"); + if(verbose) + printError(frame, group, "LoadFrame"); return false; } @@ -119,7 +120,7 @@ void CAnimation::duplicateImage(const size_t sourceGroup, const size_t sourceFra std::shared_ptr CAnimation::getImage(size_t frame, size_t group, bool verbose) { - if (!loadFrame(frame, group)) + if (!loadFrame(frame, group, verbose)) return nullptr; return getImageImpl(frame, group, verbose); } diff --git a/client/render/CAnimation.h b/client/render/CAnimation.h index 86c0b95a9..6b9e66272 100644 --- a/client/render/CAnimation.h +++ b/client/render/CAnimation.h @@ -41,7 +41,7 @@ private: PlayerColor player = PlayerColor::CANNOT_DETERMINE; //loader, will be called by load(), require opened def file for loading from it. Returns true if image is loaded - bool loadFrame(size_t frame, size_t group); + bool loadFrame(size_t frame, size_t group, bool verbose = true); //unloadFrame, returns true if image has been unloaded ( either deleted or decreased refCount) bool unloadFrame(size_t frame, size_t group);