From 6a89245bd4c6233b13a09fa03b46ec36a59a578b Mon Sep 17 00:00:00 2001 From: Ivan Savenko Date: Wed, 22 Feb 2023 13:14:11 +0200 Subject: [PATCH] Working version of image caching --- client/mapRenderer/IMapRendererContext.h | 4 +- client/mapRenderer/MapRenderer.cpp | 10 +++-- client/mapRenderer/MapRenderer.h | 2 +- client/mapRenderer/MapRendererContext.cpp | 54 ++++++++++++++++------- client/mapRenderer/MapRendererContext.h | 9 +++- client/mapRenderer/MapViewCache.cpp | 14 +++--- client/mapRenderer/MapViewController.cpp | 8 +++- 7 files changed, 67 insertions(+), 34 deletions(-) diff --git a/client/mapRenderer/IMapRendererContext.h b/client/mapRenderer/IMapRendererContext.h index afd4c760a..5ede9af57 100644 --- a/client/mapRenderer/IMapRendererContext.h +++ b/client/mapRenderer/IMapRendererContext.h @@ -34,8 +34,8 @@ public: /// returns true if chosen coordinates exist on map virtual bool isInMap(const int3 & coordinates) const = 0; -// /// returns true if selected tile has animation and should not be cached -// virtual bool tileAnimated(const int3 & coordinates) const = 0; + /// returns true if selected tile has animation and should not be cached + virtual bool tileAnimated(const int3 & coordinates) const = 0; /// returns tile by selected coordinates. Coordinates MUST be valid virtual const TerrainTile & getMapTile(const int3 & coordinates) const = 0; diff --git a/client/mapRenderer/MapRenderer.cpp b/client/mapRenderer/MapRenderer.cpp index 9fc727c44..05e2b997f 100644 --- a/client/mapRenderer/MapRenderer.cpp +++ b/client/mapRenderer/MapRenderer.cpp @@ -158,7 +158,7 @@ uint8_t MapRendererTerrain::checksum(const IMapRendererContext & context, const if(mapTile.terType->getId() == ETerrainId::LAVA || mapTile.terType->getId() == ETerrainId::WATER) return context.terrainImageIndex(250); - return 0xff-1; + return 0xff - 1; } MapRendererRiver::MapRendererRiver() @@ -361,7 +361,7 @@ uint8_t MapRendererFow::checksum(const IMapRendererContext & context, const int3 const NeighborTilesInfo neighborInfo(context, coordinates); int retBitmapID = neighborInfo.getBitmapID(); if(retBitmapID < 0) - return 0xff-1; + return 0xff - 1; return retBitmapID; } @@ -396,8 +396,10 @@ std::shared_ptr MapRendererObjects::getBaseAnimation(const CGObjectI std::shared_ptr MapRendererObjects::getAnimation(const std::string & filename, bool generateMovementGroups) { - if(animations.count(filename)) - return animations[filename]; + auto it = animations.find(filename); + + if(it != animations.end()) + return it->second; auto ret = std::make_shared(filename); animations[filename] = ret; diff --git a/client/mapRenderer/MapRenderer.h b/client/mapRenderer/MapRenderer.h index bb7ef7dc2..651a0483d 100644 --- a/client/mapRenderer/MapRenderer.h +++ b/client/mapRenderer/MapRenderer.h @@ -69,7 +69,7 @@ public: class MapRendererObjects { - std::map> animations; + std::unordered_map> animations; std::shared_ptr getBaseAnimation(const CGObjectInstance * obj); std::shared_ptr getFlagAnimation(const CGObjectInstance * obj); diff --git a/client/mapRenderer/MapRendererContext.cpp b/client/mapRenderer/MapRendererContext.cpp index e79926663..ee1047b43 100644 --- a/client/mapRenderer/MapRendererContext.cpp +++ b/client/mapRenderer/MapRendererContext.cpp @@ -16,15 +16,8 @@ #include "../CGameInfo.h" #include "../CPlayerInterface.h" #include "../adventureMap/CAdvMapInt.h" -//#include "../gui/CGuiHandler.h" -//#include "../render/CAnimation.h" -//#include "../render/Canvas.h" -//#include "../render/IImage.h" -//#include "../renderSDL/SDL_Extensions.h" -// #include "../../CCallback.h" -#include "../../lib/CConfigHandler.h" #include "../../lib/mapObjects/CGHeroInstance.h" #include "../../lib/mapping/CMap.h" @@ -66,9 +59,9 @@ const CGObjectInstance * MapRendererContext::getObject(ObjectInstanceID objectID bool MapRendererContext::isVisible(const int3 & coordinates) const { - if (showAllTerrain) + if (settingsSessionSpectate || showAllTerrain) return LOCPLINT->cb->isInTheMap(coordinates); - return LOCPLINT->cb->isVisible(coordinates) || settings["session"]["spectate"].Bool(); + return LOCPLINT->cb->isVisible(coordinates); } const CGPath * MapRendererContext::currentPath() const @@ -90,7 +83,7 @@ size_t MapRendererContext::objectImageIndex(ObjectInstanceID objectID, size_t gr if(groupSize == 0) return 0; - if (!settings["adventure"]["objectAnimation"].Bool()) + if (!settingsAdventureObjectAnimation) return 0; // H3 timing for adventure map objects animation is 180 ms @@ -109,7 +102,7 @@ size_t MapRendererContext::objectImageIndex(ObjectInstanceID objectID, size_t gr size_t MapRendererContext::terrainImageIndex(size_t groupSize) const { - if (!settings["adventure"]["terrainAnimation"].Bool()) + if (!settingsAdventureTerrainAnimation) return 0; size_t baseFrameTime = 180; @@ -118,10 +111,33 @@ size_t MapRendererContext::terrainImageIndex(size_t groupSize) const return frameIndex; } -//Point MapRendererContext::getTileSize() const -//{ -// return Point(32, 32); -//} +bool MapRendererContext::tileAnimated(const int3 & coordinates) const +{ + if(movementAnimation) + { + auto objects = getObjects(coordinates); + + if(vstd::contains(objects, movementAnimation->target)) + return true; + } + + if(fadeInAnimation) + { + auto objects = getObjects(coordinates); + + if(vstd::contains(objects, fadeInAnimation->target)) + return true; + } + + if(fadeOutAnimation) + { + auto objects = getObjects(coordinates); + + if(vstd::contains(objects, fadeOutAnimation->target)) + return true; + } + return false; +} bool MapRendererContext::showOverlay() const { @@ -130,17 +146,21 @@ bool MapRendererContext::showOverlay() const bool MapRendererContext::showGrid() const { +<<<<<<< HEAD return settings["gameTweaks"]["showGrid"].Bool(); +======= + return settingsSessionShowGrid; +>>>>>>> 9a847b520 (Working version of image caching) } bool MapRendererContext::showVisitable() const { - return settings["session"]["showVisitable"].Bool(); + return settingsSessionShowVisitable; } bool MapRendererContext::showBlockable() const { - return settings["session"]["showBlockable"].Bool(); + return settingsSessionShowBlockable; } MapRendererContext::MapRendererContext() diff --git a/client/mapRenderer/MapRendererContext.h b/client/mapRenderer/MapRendererContext.h index 06ac24053..73b405f32 100644 --- a/client/mapRenderer/MapRendererContext.h +++ b/client/mapRenderer/MapRendererContext.h @@ -76,7 +76,6 @@ class MapRendererContext : public IMapRendererContext boost::multi_array objects; - //Point tileSize = Point(32, 32); uint32_t animationTime = 0; boost::optional movementAnimation; @@ -89,6 +88,12 @@ class MapRendererContext : public IMapRendererContext bool worldViewModeActive = false; bool showAllTerrain = false; + bool settingsSessionSpectate = false; + bool settingsAdventureObjectAnimation = true; + bool settingsAdventureTerrainAnimation = true; + bool settingsSessionShowGrid = false; + bool settingsSessionShowVisitable = false; + bool settingsSessionShowBlockable = false; size_t selectOverlayImageForObject(const ObjectPosInfo & objectID) const; @@ -102,6 +107,7 @@ public: int3 getMapSize() const final; bool isInMap(const int3 & coordinates) const final; bool isVisible(const int3 & coordinates) const override; + bool tileAnimated(const int3 & coordinates) const override; const TerrainTile & getMapTile(const int3 & coordinates) const override; const MapObjectsList & getObjects(const int3 & coordinates) const override; @@ -114,7 +120,6 @@ public: size_t objectImageIndex(ObjectInstanceID objectID, size_t groupSize) const override; size_t terrainImageIndex(size_t groupSize) const override; size_t overlayImageIndex(const int3 & coordinates) const override; -// Point getTileSize() const override; bool showOverlay() const override; bool showGrid() const override; diff --git a/client/mapRenderer/MapViewCache.cpp b/client/mapRenderer/MapViewCache.cpp index 107b5c016..b55becfcb 100644 --- a/client/mapRenderer/MapViewCache.cpp +++ b/client/mapRenderer/MapViewCache.cpp @@ -66,7 +66,7 @@ void MapViewCache::updateTile(const std::shared_ptr & newCacheEntry.tileY = coordinates.y; newCacheEntry.checksum = mapRenderer->getTileChecksum(*context, coordinates); - if (cachedLevel == coordinates.z && oldCacheEntry == newCacheEntry) + if(cachedLevel == coordinates.z && oldCacheEntry == newCacheEntry) { // debug code to check caching - cached tiles will slowly fade to black //static const auto overlay = IImage::createFromFile("debug/cached"); @@ -96,7 +96,7 @@ void MapViewCache::update(const std::shared_ptr & con { Rect dimensions = model->getTilesTotalRect(); - if (dimensions.w != terrainChecksum.shape()[0] || dimensions.h != terrainChecksum.shape()[1]) + if(dimensions.w != terrainChecksum.shape()[0] || dimensions.h != terrainChecksum.shape()[1]) { boost::multi_array newCache; newCache.resize(boost::extents[dimensions.w][dimensions.h]); @@ -117,7 +117,7 @@ void MapViewCache::render(const std::shared_ptr & con Rect dimensions = model->getTilesTotalRect(); - if (dimensions.w != tilesUpToDate.shape()[0] || dimensions.h != tilesUpToDate.shape()[1]) + if(dimensions.w != tilesUpToDate.shape()[0] || dimensions.h != tilesUpToDate.shape()[1]) { boost::multi_array newCache; newCache.resize(boost::extents[dimensions.w][dimensions.h]); @@ -130,11 +130,11 @@ void MapViewCache::render(const std::shared_ptr & con { int cacheX = (terrainChecksum.shape()[0] + x) % terrainChecksum.shape()[0]; int cacheY = (terrainChecksum.shape()[1] + y) % terrainChecksum.shape()[1]; + int3 tile(x, y, model->getLevel()); - if (lazyUpdate && tilesUpToDate[cacheX][cacheY]) + if(lazyUpdate && tilesUpToDate[cacheX][cacheY] && !context->tileAnimated(tile)) continue; - int3 tile(x, y, model->getLevel()); Canvas source = getTile(tile); Rect targetRect = model->getTargetTileArea(tile); target.draw(source, targetRect.topLeft()); @@ -143,7 +143,7 @@ void MapViewCache::render(const std::shared_ptr & con } } - if (context->showOverlay()) + if(context->showOverlay()) { for(int y = dimensions.top(); y < dimensions.bottom(); ++y) { @@ -153,7 +153,7 @@ void MapViewCache::render(const std::shared_ptr & con Rect targetRect = model->getTargetTileArea(tile); auto overlay = getOverlayImageForTile(context, tile); - if (overlay) + if(overlay) { Point position = targetRect.center() - overlay->dimensions() / 2; target.draw(overlay, position); diff --git a/client/mapRenderer/MapViewController.cpp b/client/mapRenderer/MapViewController.cpp index 59c3bcffb..374e40c0e 100644 --- a/client/mapRenderer/MapViewController.cpp +++ b/client/mapRenderer/MapViewController.cpp @@ -115,8 +115,14 @@ void MapViewController::update(uint32_t timeDelta) } context->animationTime += timeDelta; - //context->tileSize = Point(32,32); //model->getSingleTileSize(); context->worldViewModeActive = model->getSingleTileSize() != Point(32,32); + + context->settingsSessionSpectate = settings["session"]["spectate"].Bool(); + context->settingsAdventureObjectAnimation = settings["adventure"]["objectAnimation"].Bool(); + context->settingsAdventureTerrainAnimation = settings["adventure"]["terrainAnimation"].Bool(); + context->settingsSessionShowGrid = settings["gameTweaks"]["showGrid"].Bool(); + context->settingsSessionShowVisitable = settings["session"]["showVisitable"].Bool(); + context->settingsSessionShowBlockable = settings["session"]["showBlockable"].Bool(); } void MapViewController::onObjectFadeIn(const CGObjectInstance * obj)