1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-08-13 19:54:17 +02:00

Working version of image caching

This commit is contained in:
Ivan Savenko
2023-02-22 13:14:11 +02:00
parent cda78fb30f
commit 6a89245bd4
7 changed files with 67 additions and 34 deletions

View File

@@ -34,8 +34,8 @@ public:
/// returns true if chosen coordinates exist on map /// returns true if chosen coordinates exist on map
virtual bool isInMap(const int3 & coordinates) const = 0; virtual bool isInMap(const int3 & coordinates) const = 0;
// /// returns true if selected tile has animation and should not be cached /// returns true if selected tile has animation and should not be cached
// virtual bool tileAnimated(const int3 & coordinates) const = 0; virtual bool tileAnimated(const int3 & coordinates) const = 0;
/// returns tile by selected coordinates. Coordinates MUST be valid /// returns tile by selected coordinates. Coordinates MUST be valid
virtual const TerrainTile & getMapTile(const int3 & coordinates) const = 0; virtual const TerrainTile & getMapTile(const int3 & coordinates) const = 0;

View File

@@ -158,7 +158,7 @@ uint8_t MapRendererTerrain::checksum(const IMapRendererContext & context, const
if(mapTile.terType->getId() == ETerrainId::LAVA || mapTile.terType->getId() == ETerrainId::WATER) if(mapTile.terType->getId() == ETerrainId::LAVA || mapTile.terType->getId() == ETerrainId::WATER)
return context.terrainImageIndex(250); return context.terrainImageIndex(250);
return 0xff-1; return 0xff - 1;
} }
MapRendererRiver::MapRendererRiver() MapRendererRiver::MapRendererRiver()
@@ -361,7 +361,7 @@ uint8_t MapRendererFow::checksum(const IMapRendererContext & context, const int3
const NeighborTilesInfo neighborInfo(context, coordinates); const NeighborTilesInfo neighborInfo(context, coordinates);
int retBitmapID = neighborInfo.getBitmapID(); int retBitmapID = neighborInfo.getBitmapID();
if(retBitmapID < 0) if(retBitmapID < 0)
return 0xff-1; return 0xff - 1;
return retBitmapID; return retBitmapID;
} }
@@ -396,8 +396,10 @@ std::shared_ptr<CAnimation> MapRendererObjects::getBaseAnimation(const CGObjectI
std::shared_ptr<CAnimation> MapRendererObjects::getAnimation(const std::string & filename, bool generateMovementGroups) std::shared_ptr<CAnimation> MapRendererObjects::getAnimation(const std::string & filename, bool generateMovementGroups)
{ {
if(animations.count(filename)) auto it = animations.find(filename);
return animations[filename];
if(it != animations.end())
return it->second;
auto ret = std::make_shared<CAnimation>(filename); auto ret = std::make_shared<CAnimation>(filename);
animations[filename] = ret; animations[filename] = ret;

View File

@@ -69,7 +69,7 @@ public:
class MapRendererObjects class MapRendererObjects
{ {
std::map<std::string, std::shared_ptr<CAnimation>> animations; std::unordered_map<std::string, std::shared_ptr<CAnimation>> animations;
std::shared_ptr<CAnimation> getBaseAnimation(const CGObjectInstance * obj); std::shared_ptr<CAnimation> getBaseAnimation(const CGObjectInstance * obj);
std::shared_ptr<CAnimation> getFlagAnimation(const CGObjectInstance * obj); std::shared_ptr<CAnimation> getFlagAnimation(const CGObjectInstance * obj);

View File

@@ -16,15 +16,8 @@
#include "../CGameInfo.h" #include "../CGameInfo.h"
#include "../CPlayerInterface.h" #include "../CPlayerInterface.h"
#include "../adventureMap/CAdvMapInt.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 "../../CCallback.h"
#include "../../lib/CConfigHandler.h"
#include "../../lib/mapObjects/CGHeroInstance.h" #include "../../lib/mapObjects/CGHeroInstance.h"
#include "../../lib/mapping/CMap.h" #include "../../lib/mapping/CMap.h"
@@ -66,9 +59,9 @@ const CGObjectInstance * MapRendererContext::getObject(ObjectInstanceID objectID
bool MapRendererContext::isVisible(const int3 & coordinates) const bool MapRendererContext::isVisible(const int3 & coordinates) const
{ {
if (showAllTerrain) if (settingsSessionSpectate || showAllTerrain)
return LOCPLINT->cb->isInTheMap(coordinates); return LOCPLINT->cb->isInTheMap(coordinates);
return LOCPLINT->cb->isVisible(coordinates) || settings["session"]["spectate"].Bool(); return LOCPLINT->cb->isVisible(coordinates);
} }
const CGPath * MapRendererContext::currentPath() const const CGPath * MapRendererContext::currentPath() const
@@ -90,7 +83,7 @@ size_t MapRendererContext::objectImageIndex(ObjectInstanceID objectID, size_t gr
if(groupSize == 0) if(groupSize == 0)
return 0; return 0;
if (!settings["adventure"]["objectAnimation"].Bool()) if (!settingsAdventureObjectAnimation)
return 0; return 0;
// H3 timing for adventure map objects animation is 180 ms // 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 size_t MapRendererContext::terrainImageIndex(size_t groupSize) const
{ {
if (!settings["adventure"]["terrainAnimation"].Bool()) if (!settingsAdventureTerrainAnimation)
return 0; return 0;
size_t baseFrameTime = 180; size_t baseFrameTime = 180;
@@ -118,10 +111,33 @@ size_t MapRendererContext::terrainImageIndex(size_t groupSize) const
return frameIndex; return frameIndex;
} }
//Point MapRendererContext::getTileSize() const bool MapRendererContext::tileAnimated(const int3 & coordinates) const
//{ {
// return Point(32, 32); 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 bool MapRendererContext::showOverlay() const
{ {
@@ -130,17 +146,21 @@ bool MapRendererContext::showOverlay() const
bool MapRendererContext::showGrid() const bool MapRendererContext::showGrid() const
{ {
<<<<<<< HEAD
return settings["gameTweaks"]["showGrid"].Bool(); return settings["gameTweaks"]["showGrid"].Bool();
=======
return settingsSessionShowGrid;
>>>>>>> 9a847b520 (Working version of image caching)
} }
bool MapRendererContext::showVisitable() const bool MapRendererContext::showVisitable() const
{ {
return settings["session"]["showVisitable"].Bool(); return settingsSessionShowVisitable;
} }
bool MapRendererContext::showBlockable() const bool MapRendererContext::showBlockable() const
{ {
return settings["session"]["showBlockable"].Bool(); return settingsSessionShowBlockable;
} }
MapRendererContext::MapRendererContext() MapRendererContext::MapRendererContext()

View File

@@ -76,7 +76,6 @@ class MapRendererContext : public IMapRendererContext
boost::multi_array<MapObjectsList, 3> objects; boost::multi_array<MapObjectsList, 3> objects;
//Point tileSize = Point(32, 32);
uint32_t animationTime = 0; uint32_t animationTime = 0;
boost::optional<HeroAnimationState> movementAnimation; boost::optional<HeroAnimationState> movementAnimation;
@@ -89,6 +88,12 @@ class MapRendererContext : public IMapRendererContext
bool worldViewModeActive = false; bool worldViewModeActive = false;
bool showAllTerrain = 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; size_t selectOverlayImageForObject(const ObjectPosInfo & objectID) const;
@@ -102,6 +107,7 @@ public:
int3 getMapSize() const final; int3 getMapSize() const final;
bool isInMap(const int3 & coordinates) const final; bool isInMap(const int3 & coordinates) const final;
bool isVisible(const int3 & coordinates) const override; bool isVisible(const int3 & coordinates) const override;
bool tileAnimated(const int3 & coordinates) const override;
const TerrainTile & getMapTile(const int3 & coordinates) const override; const TerrainTile & getMapTile(const int3 & coordinates) const override;
const MapObjectsList & getObjects(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 objectImageIndex(ObjectInstanceID objectID, size_t groupSize) const override;
size_t terrainImageIndex(size_t groupSize) const override; size_t terrainImageIndex(size_t groupSize) const override;
size_t overlayImageIndex(const int3 & coordinates) const override; size_t overlayImageIndex(const int3 & coordinates) const override;
// Point getTileSize() const override;
bool showOverlay() const override; bool showOverlay() const override;
bool showGrid() const override; bool showGrid() const override;

View File

@@ -66,7 +66,7 @@ void MapViewCache::updateTile(const std::shared_ptr<const IMapRendererContext> &
newCacheEntry.tileY = coordinates.y; newCacheEntry.tileY = coordinates.y;
newCacheEntry.checksum = mapRenderer->getTileChecksum(*context, coordinates); 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 // debug code to check caching - cached tiles will slowly fade to black
//static const auto overlay = IImage::createFromFile("debug/cached"); //static const auto overlay = IImage::createFromFile("debug/cached");
@@ -96,7 +96,7 @@ void MapViewCache::update(const std::shared_ptr<const IMapRendererContext> & con
{ {
Rect dimensions = model->getTilesTotalRect(); 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<TileChecksum, 2> newCache; boost::multi_array<TileChecksum, 2> newCache;
newCache.resize(boost::extents[dimensions.w][dimensions.h]); newCache.resize(boost::extents[dimensions.w][dimensions.h]);
@@ -117,7 +117,7 @@ void MapViewCache::render(const std::shared_ptr<const IMapRendererContext> & con
Rect dimensions = model->getTilesTotalRect(); 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<bool, 2> newCache; boost::multi_array<bool, 2> newCache;
newCache.resize(boost::extents[dimensions.w][dimensions.h]); newCache.resize(boost::extents[dimensions.w][dimensions.h]);
@@ -130,11 +130,11 @@ void MapViewCache::render(const std::shared_ptr<const IMapRendererContext> & con
{ {
int cacheX = (terrainChecksum.shape()[0] + x) % terrainChecksum.shape()[0]; int cacheX = (terrainChecksum.shape()[0] + x) % terrainChecksum.shape()[0];
int cacheY = (terrainChecksum.shape()[1] + y) % terrainChecksum.shape()[1]; 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; continue;
int3 tile(x, y, model->getLevel());
Canvas source = getTile(tile); Canvas source = getTile(tile);
Rect targetRect = model->getTargetTileArea(tile); Rect targetRect = model->getTargetTileArea(tile);
target.draw(source, targetRect.topLeft()); target.draw(source, targetRect.topLeft());
@@ -143,7 +143,7 @@ void MapViewCache::render(const std::shared_ptr<const IMapRendererContext> & con
} }
} }
if (context->showOverlay()) if(context->showOverlay())
{ {
for(int y = dimensions.top(); y < dimensions.bottom(); ++y) for(int y = dimensions.top(); y < dimensions.bottom(); ++y)
{ {
@@ -153,7 +153,7 @@ void MapViewCache::render(const std::shared_ptr<const IMapRendererContext> & con
Rect targetRect = model->getTargetTileArea(tile); Rect targetRect = model->getTargetTileArea(tile);
auto overlay = getOverlayImageForTile(context, tile); auto overlay = getOverlayImageForTile(context, tile);
if (overlay) if(overlay)
{ {
Point position = targetRect.center() - overlay->dimensions() / 2; Point position = targetRect.center() - overlay->dimensions() / 2;
target.draw(overlay, position); target.draw(overlay, position);

View File

@@ -115,8 +115,14 @@ void MapViewController::update(uint32_t timeDelta)
} }
context->animationTime += timeDelta; context->animationTime += timeDelta;
//context->tileSize = Point(32,32); //model->getSingleTileSize();
context->worldViewModeActive = model->getSingleTileSize() != Point(32,32); 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) void MapViewController::onObjectFadeIn(const CGObjectInstance * obj)