From 896d41e4b073046c59933ea8829474bd82b9c506 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Sat, 31 Dec 2016 22:53:42 +0300 Subject: Improve special layers draw speed in same way like fringe layer. --- src/resources/map/speciallayer.cpp | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) (limited to 'src/resources/map/speciallayer.cpp') diff --git a/src/resources/map/speciallayer.cpp b/src/resources/map/speciallayer.cpp index c1ca865d0..b19e665f1 100644 --- a/src/resources/map/speciallayer.cpp +++ b/src/resources/map/speciallayer.cpp @@ -36,6 +36,7 @@ SpecialLayer::SpecialLayer(const std::string &name, MemoryCounter(), mName(name), mTiles(new MapItem*[width * height]), + mCache(new int[width * height]), mWidth(width), mHeight(height) { @@ -47,6 +48,7 @@ SpecialLayer::~SpecialLayer() for (int f = 0; f < mWidth * mHeight; f ++) delete2(mTiles[f]) delete [] mTiles; + delete [] mCache; } MapItem* SpecialLayer::getTile(const int x, const int y) const @@ -107,9 +109,10 @@ void SpecialLayer::addRoad(const Path &road) else item->setType(MapItemType::ROAD); } + updateCache(); } -void SpecialLayer::clean() const +void SpecialLayer::clean() { if (!mTiles) return; @@ -120,6 +123,7 @@ void SpecialLayer::clean() const if (item) item->setType(MapItemType::EMPTY); } + updateCache(); } void SpecialLayer::draw(Graphics *const graphics, int startX, int startY, @@ -158,3 +162,26 @@ int SpecialLayer::calcMemoryLocal() const return static_cast(sizeof(SpecialLayer) + sizeof(MapItem) * mWidth * mHeight); } + +void SpecialLayer::updateCache() +{ + for (int y = 0; y < mHeight; y ++) + { + const int y2 = y * mWidth; + for (int x = 0; x < mWidth; x ++) + { + int c = 10000; + for (int f = x + 1; f < mWidth; f ++) + { + MapItem *const item = mTiles[f + y2]; + if (item != nullptr && + item->mType != MapItemType::EMPTY) + { + c = f - x - 1; + break; + } + } + mCache[x + y2] = c; + } + } +} -- cgit v1.2.3-70-g09d2