mirror of
https://github.com/vcmi/vcmi.git
synced 2025-08-15 20:03:15 +02:00
Attempt to improve caching
This commit is contained in:
@@ -528,13 +528,11 @@ void CAdvMapInt::showAll(SDL_Surface * to)
|
|||||||
activeMapPanel->showAll(to);
|
activeMapPanel->showAll(to);
|
||||||
|
|
||||||
minimap->showAll(to);
|
minimap->showAll(to);
|
||||||
|
terrain->showAll(to);
|
||||||
show(to);
|
show(to);
|
||||||
|
|
||||||
|
|
||||||
resdatabar->showAll(to);
|
resdatabar->showAll(to);
|
||||||
|
|
||||||
statusbar->show(to);
|
statusbar->show(to);
|
||||||
|
|
||||||
LOCPLINT->cingconsole->show(to);
|
LOCPLINT->cingconsole->show(to);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -55,21 +55,28 @@ MapView::MapView(const Point & offset, const Point & dimensions)
|
|||||||
pos.h = dimensions.y;
|
pos.h = dimensions.y;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MapView::show(SDL_Surface * to)
|
void MapView::render(Canvas & target, bool fullUpdate)
|
||||||
{
|
{
|
||||||
Canvas target(to);
|
|
||||||
Canvas targetClipped(target, pos);
|
Canvas targetClipped(target, pos);
|
||||||
|
|
||||||
CSDL_Ext::CClipRectGuard guard(to, pos);
|
|
||||||
|
|
||||||
controller->update(GH.mainFPSmng->getElapsedMilliseconds());
|
controller->update(GH.mainFPSmng->getElapsedMilliseconds());
|
||||||
tilesCache->update(controller->getContext());
|
tilesCache->update(controller->getContext());
|
||||||
tilesCache->render(controller->getContext(), targetClipped);
|
tilesCache->render(controller->getContext(), targetClipped, fullUpdate);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MapView::show(SDL_Surface * to)
|
||||||
|
{
|
||||||
|
Canvas target(to);
|
||||||
|
CSDL_Ext::CClipRectGuard guard(to, pos);
|
||||||
|
render(target, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MapView::showAll(SDL_Surface * to)
|
void MapView::showAll(SDL_Surface * to)
|
||||||
{
|
{
|
||||||
show(to);
|
Canvas target(to);
|
||||||
|
CSDL_Ext::CClipRectGuard guard(to, pos);
|
||||||
|
render(target, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<const MapViewModel> MapView::getModel() const
|
std::shared_ptr<const MapViewModel> MapView::getModel() const
|
||||||
|
@@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
#include "../gui/CIntObject.h"
|
#include "../gui/CIntObject.h"
|
||||||
|
|
||||||
|
class Canvas;
|
||||||
class MapViewController;
|
class MapViewController;
|
||||||
class MapViewModel;
|
class MapViewModel;
|
||||||
class MapViewCache;
|
class MapViewCache;
|
||||||
@@ -24,6 +25,7 @@ class MapView : public CIntObject
|
|||||||
|
|
||||||
std::shared_ptr<MapViewModel> createModel(const Point & dimensions) const;
|
std::shared_ptr<MapViewModel> createModel(const Point & dimensions) const;
|
||||||
|
|
||||||
|
void render(Canvas & target, bool fullUpdate);
|
||||||
public:
|
public:
|
||||||
std::shared_ptr<const MapViewModel> getModel() const;
|
std::shared_ptr<const MapViewModel> getModel() const;
|
||||||
std::shared_ptr<MapViewController> getController();
|
std::shared_ptr<MapViewController> getController();
|
||||||
|
@@ -37,6 +37,7 @@ MapViewCache::MapViewCache(const std::shared_ptr<MapViewModel> & model)
|
|||||||
|
|
||||||
Point visibleSize = model->getTilesVisibleDimensions();
|
Point visibleSize = model->getTilesVisibleDimensions();
|
||||||
terrainChecksum.resize(boost::extents[visibleSize.x][visibleSize.y]);
|
terrainChecksum.resize(boost::extents[visibleSize.x][visibleSize.y]);
|
||||||
|
tilesUpToDate.resize(boost::extents[visibleSize.x][visibleSize.y]);
|
||||||
}
|
}
|
||||||
|
|
||||||
Canvas MapViewCache::getTile(const int3 & coordinates)
|
Canvas MapViewCache::getTile(const int3 & coordinates)
|
||||||
@@ -88,6 +89,7 @@ void MapViewCache::updateTile(const std::shared_ptr<const IMapRendererContext> &
|
|||||||
}
|
}
|
||||||
|
|
||||||
oldCacheEntry = newCacheEntry;
|
oldCacheEntry = newCacheEntry;
|
||||||
|
tilesUpToDate[cacheX][cacheY] = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MapViewCache::update(const std::shared_ptr<const IMapRendererContext> & context)
|
void MapViewCache::update(const std::shared_ptr<const IMapRendererContext> & context)
|
||||||
@@ -108,18 +110,36 @@ void MapViewCache::update(const std::shared_ptr<const IMapRendererContext> & con
|
|||||||
cachedLevel = model->getLevel();
|
cachedLevel = model->getLevel();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MapViewCache::render(const std::shared_ptr<const IMapRendererContext> & context, Canvas & target)
|
void MapViewCache::render(const std::shared_ptr<const IMapRendererContext> & context, Canvas & target, bool fullRedraw)
|
||||||
{
|
{
|
||||||
|
bool mapMoved = (cachedPosition != model->getMapViewCenter());
|
||||||
|
bool lazyUpdate = !mapMoved && !fullRedraw;
|
||||||
|
|
||||||
Rect dimensions = model->getTilesTotalRect();
|
Rect dimensions = model->getTilesTotalRect();
|
||||||
|
|
||||||
|
if (dimensions.w != tilesUpToDate.shape()[0] || dimensions.h != tilesUpToDate.shape()[1])
|
||||||
|
{
|
||||||
|
boost::multi_array<bool, 2> newCache;
|
||||||
|
newCache.resize(boost::extents[dimensions.w][dimensions.h]);
|
||||||
|
std::swap(newCache, tilesUpToDate);
|
||||||
|
}
|
||||||
|
|
||||||
for(int y = dimensions.top(); y < dimensions.bottom(); ++y)
|
for(int y = dimensions.top(); y < dimensions.bottom(); ++y)
|
||||||
{
|
{
|
||||||
for(int x = dimensions.left(); x < dimensions.right(); ++x)
|
for(int x = dimensions.left(); x < dimensions.right(); ++x)
|
||||||
{
|
{
|
||||||
|
int cacheX = (terrainChecksum.shape()[0] + x) % terrainChecksum.shape()[0];
|
||||||
|
int cacheY = (terrainChecksum.shape()[1] + y) % terrainChecksum.shape()[1];
|
||||||
|
|
||||||
|
if (lazyUpdate && tilesUpToDate[cacheX][cacheY])
|
||||||
|
continue;
|
||||||
|
|
||||||
int3 tile(x, y, model->getLevel());
|
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());
|
||||||
|
|
||||||
|
tilesUpToDate[cacheX][cacheY] = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -141,4 +161,6 @@ void MapViewCache::render(const std::shared_ptr<const IMapRendererContext> & con
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cachedPosition = model->getMapViewCenter();
|
||||||
}
|
}
|
||||||
|
@@ -9,9 +9,7 @@
|
|||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
VCMI_LIB_NAMESPACE_BEGIN
|
#include "../../lib/Point.h"
|
||||||
class int3;
|
|
||||||
VCMI_LIB_NAMESPACE_END
|
|
||||||
|
|
||||||
class IImage;
|
class IImage;
|
||||||
class CAnimation;
|
class CAnimation;
|
||||||
@@ -37,6 +35,9 @@ class MapViewCache
|
|||||||
};
|
};
|
||||||
|
|
||||||
boost::multi_array<TileChecksum, 2> terrainChecksum;
|
boost::multi_array<TileChecksum, 2> terrainChecksum;
|
||||||
|
boost::multi_array<bool, 2> tilesUpToDate;
|
||||||
|
|
||||||
|
Point cachedPosition;
|
||||||
int cachedLevel;
|
int cachedLevel;
|
||||||
|
|
||||||
std::shared_ptr<MapViewModel> model;
|
std::shared_ptr<MapViewModel> model;
|
||||||
@@ -60,5 +61,5 @@ public:
|
|||||||
void update(const std::shared_ptr<const IMapRendererContext> & context);
|
void update(const std::shared_ptr<const IMapRendererContext> & context);
|
||||||
|
|
||||||
/// renders updated terrain cache onto provided canvas
|
/// renders updated terrain cache onto provided canvas
|
||||||
void render(const std::shared_ptr<const IMapRendererContext> &context, Canvas & target);
|
void render(const std::shared_ptr<const IMapRendererContext> &context, Canvas & target, bool fullRedraw);
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user