mirror of
https://github.com/vcmi/vcmi.git
synced 2025-07-03 00:46:55 +02:00
Partial support for caching to detect updated tiles
This commit is contained in:
@ -11,6 +11,7 @@
|
||||
#include "StdInc.h"
|
||||
#include "MapViewCache.h"
|
||||
|
||||
#include "IMapRendererContext.h"
|
||||
#include "MapRenderer.h"
|
||||
#include "MapViewModel.h"
|
||||
|
||||
@ -24,6 +25,7 @@ MapViewCache::~MapViewCache() = default;
|
||||
|
||||
MapViewCache::MapViewCache(const std::shared_ptr<MapViewModel> & model)
|
||||
: model(model)
|
||||
, cachedLevel(0)
|
||||
, mapRenderer(new MapRenderer())
|
||||
, iconsStorage(new CAnimation("VwSymbol"))
|
||||
, intermediate(new Canvas(Point(32, 32)))
|
||||
@ -32,6 +34,9 @@ MapViewCache::MapViewCache(const std::shared_ptr<MapViewModel> & model)
|
||||
iconsStorage->preload();
|
||||
for(size_t i = 0; i < iconsStorage->size(); ++i)
|
||||
iconsStorage->getImage(i)->setBlitMode(EImageBlitMode::COLORKEY);
|
||||
|
||||
Point visibleSize = model->getTilesVisibleDimensions();
|
||||
terrainChecksum.resize(boost::extents[visibleSize.x][visibleSize.y]);
|
||||
}
|
||||
|
||||
Canvas MapViewCache::getTile(const int3 & coordinates)
|
||||
@ -39,7 +44,7 @@ Canvas MapViewCache::getTile(const int3 & coordinates)
|
||||
return Canvas(*terrain, model->getCacheTileArea(coordinates));
|
||||
}
|
||||
|
||||
std::shared_ptr<IImage> MapViewCache::getOverlayImageForTile(const std::shared_ptr<MapRendererContext> & context, const int3 & coordinates)
|
||||
std::shared_ptr<IImage> MapViewCache::getOverlayImageForTile(const std::shared_ptr<const IMapRendererContext> & context, const int3 & coordinates)
|
||||
{
|
||||
size_t imageIndex = context->overlayImageIndex(coordinates);
|
||||
|
||||
@ -48,8 +53,28 @@ std::shared_ptr<IImage> MapViewCache::getOverlayImageForTile(const std::shared_p
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void MapViewCache::updateTile(const std::shared_ptr<MapRendererContext> & context, const int3 & coordinates)
|
||||
void MapViewCache::updateTile(const std::shared_ptr<const IMapRendererContext> & context, const int3 & coordinates)
|
||||
{
|
||||
int cacheX = (terrainChecksum.shape()[0] + coordinates.x) % terrainChecksum.shape()[0];
|
||||
int cacheY = (terrainChecksum.shape()[1] + coordinates.y) % terrainChecksum.shape()[1];
|
||||
|
||||
auto & oldCacheEntry = terrainChecksum[cacheX][cacheY];
|
||||
TileChecksum newCacheEntry;
|
||||
|
||||
newCacheEntry.tileX = coordinates.x;
|
||||
newCacheEntry.tileY = coordinates.y;
|
||||
newCacheEntry.checksum = mapRenderer->getTileChecksum(*context, coordinates);
|
||||
|
||||
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");
|
||||
//Canvas target = getTile(coordinates);
|
||||
//target.draw(overlay, Point(0,0));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
Canvas target = getTile(coordinates);
|
||||
|
||||
if(model->getSingleTileSize() == Point(32, 32))
|
||||
@ -61,18 +86,29 @@ void MapViewCache::updateTile(const std::shared_ptr<MapRendererContext> & contex
|
||||
mapRenderer->renderTile(*context, *intermediate, coordinates);
|
||||
target.drawScaled(*intermediate, Point(0, 0), model->getSingleTileSize());
|
||||
}
|
||||
|
||||
oldCacheEntry = newCacheEntry;
|
||||
}
|
||||
|
||||
void MapViewCache::update(const std::shared_ptr<MapRendererContext> & context)
|
||||
void MapViewCache::update(const std::shared_ptr<const IMapRendererContext> & context)
|
||||
{
|
||||
Rect dimensions = model->getTilesTotalRect();
|
||||
|
||||
if (dimensions.w != terrainChecksum.shape()[0] || dimensions.h != terrainChecksum.shape()[1])
|
||||
{
|
||||
boost::multi_array<TileChecksum, 2> newCache;
|
||||
newCache.resize(boost::extents[dimensions.w][dimensions.h]);
|
||||
std::swap(newCache, terrainChecksum);
|
||||
}
|
||||
|
||||
for(int y = dimensions.top(); y < dimensions.bottom(); ++y)
|
||||
for(int x = dimensions.left(); x < dimensions.right(); ++x)
|
||||
updateTile(context, {x, y, model->getLevel()});
|
||||
|
||||
cachedLevel = model->getLevel();
|
||||
}
|
||||
|
||||
void MapViewCache::render(const std::shared_ptr<MapRendererContext> & context, Canvas & target)
|
||||
void MapViewCache::render(const std::shared_ptr<const IMapRendererContext> & context, Canvas & target)
|
||||
{
|
||||
Rect dimensions = model->getTilesTotalRect();
|
||||
|
||||
|
Reference in New Issue
Block a user