Browse Source

Attempt to improve caching

Ivan Savenko 2 years ago
parent
commit
0282e6dad0

+ 1 - 3
client/adventureMap/CAdvMapInt.cpp

@@ -528,13 +528,11 @@ void CAdvMapInt::showAll(SDL_Surface * to)
 	activeMapPanel->showAll(to);
 
 	minimap->showAll(to);
+	terrain->showAll(to);
 	show(to);
 
-
 	resdatabar->showAll(to);
-
 	statusbar->show(to);
-
 	LOCPLINT->cingconsole->show(to);
 }
 

+ 12 - 5
client/mapRenderer/MapView.cpp

@@ -55,21 +55,28 @@ MapView::MapView(const Point & offset, const Point & dimensions)
 	pos.h = dimensions.y;
 }
 
-void MapView::show(SDL_Surface * to)
+void MapView::render(Canvas & target, bool fullUpdate)
 {
-	Canvas target(to);
 	Canvas targetClipped(target, pos);
 
-	CSDL_Ext::CClipRectGuard guard(to, pos);
 
 	controller->update(GH.mainFPSmng->getElapsedMilliseconds());
 	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)
 {
-	show(to);
+	Canvas target(to);
+	CSDL_Ext::CClipRectGuard guard(to, pos);
+	render(target, true);
 }
 
 std::shared_ptr<const MapViewModel> MapView::getModel() const

+ 2 - 0
client/mapRenderer/MapView.h

@@ -11,6 +11,7 @@
 
 #include "../gui/CIntObject.h"
 
+class Canvas;
 class MapViewController;
 class MapViewModel;
 class MapViewCache;
@@ -24,6 +25,7 @@ class MapView : public CIntObject
 
 	std::shared_ptr<MapViewModel> createModel(const Point & dimensions) const;
 
+	void render(Canvas & target, bool fullUpdate);
 public:
 	std::shared_ptr<const MapViewModel> getModel() const;
 	std::shared_ptr<MapViewController> getController();

+ 23 - 1
client/mapRenderer/MapViewCache.cpp

@@ -37,6 +37,7 @@ MapViewCache::MapViewCache(const std::shared_ptr<MapViewModel> & model)
 
 	Point visibleSize = model->getTilesVisibleDimensions();
 	terrainChecksum.resize(boost::extents[visibleSize.x][visibleSize.y]);
+	tilesUpToDate.resize(boost::extents[visibleSize.x][visibleSize.y]);
 }
 
 Canvas MapViewCache::getTile(const int3 & coordinates)
@@ -88,6 +89,7 @@ void MapViewCache::updateTile(const std::shared_ptr<const IMapRendererContext> &
 	}
 
 	oldCacheEntry = newCacheEntry;
+	tilesUpToDate[cacheX][cacheY] = false;
 }
 
 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();
 }
 
-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();
 
+	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 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());
 			Canvas source = getTile(tile);
 			Rect targetRect = model->getTargetTileArea(tile);
 			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();
 }

+ 5 - 4
client/mapRenderer/MapViewCache.h

@@ -9,9 +9,7 @@
  */
 #pragma once
 
-VCMI_LIB_NAMESPACE_BEGIN
-class int3;
-VCMI_LIB_NAMESPACE_END
+#include "../../lib/Point.h"
 
 class IImage;
 class CAnimation;
@@ -37,6 +35,9 @@ class MapViewCache
 	};
 
 	boost::multi_array<TileChecksum, 2> terrainChecksum;
+	boost::multi_array<bool, 2> tilesUpToDate;
+
+	Point cachedPosition;
 	int cachedLevel;
 
 	std::shared_ptr<MapViewModel> model;
@@ -60,5 +61,5 @@ public:
 	void update(const std::shared_ptr<const IMapRendererContext> & context);
 
 	/// 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);
 };