diff --git a/source/glest_game/graphics/renderer.cpp b/source/glest_game/graphics/renderer.cpp index 39db08e3..074c7e16 100644 --- a/source/glest_game/graphics/renderer.cpp +++ b/source/glest_game/graphics/renderer.cpp @@ -3786,9 +3786,17 @@ void Renderer::renderUnitTitles(Font2D *font, Vec3f color) { VisibleQuadContainerCache & Renderer::getQuadCache(bool updateOnDirtyFrame) { if(game != NULL && game->getWorld() != NULL) { const World *world= game->getWorld(); - if(updateOnDirtyFrame == true && world->getFrameCount() != quadCache.cacheFrame) { + if(updateOnDirtyFrame == true && + (world->getFrameCount() != quadCache.cacheFrame || + visibleQuad != quadCache.lastVisibleQuad)) { + // Dump cached info - quadCache.clearCacheData(); + if(visibleQuad != quadCache.lastVisibleQuad) { + quadCache.clearCacheData(); + } + else { + quadCache.clearVolatileCacheData(); + } // Unit calculations for(int i = 0; i < world->getFactionCount(); ++i) { @@ -3804,37 +3812,40 @@ VisibleQuadContainerCache & Renderer::getQuadCache(bool updateOnDirtyFrame) { } } - // Object calculations - const Map *map= world->getMap(); - PosQuadIterator pqi(map, visibleQuad, Map::cellScale); - while(pqi.next()){ - const Vec2i &pos= pqi.getPos(); - if(map->isInside(pos)) { - const Vec2i &mapPos = Map::toSurfCoords(pos); + if(visibleQuad != quadCache.lastVisibleQuad) { + // Object calculations + const Map *map= world->getMap(); + PosQuadIterator pqi(map, visibleQuad, Map::cellScale); + while(pqi.next()){ + const Vec2i &pos= pqi.getPos(); + if(map->isInside(pos)) { + const Vec2i &mapPos = Map::toSurfCoords(pos); - //quadCache.visibleCellList.push_back(mapPos); + //quadCache.visibleCellList.push_back(mapPos); - SurfaceCell *sc = map->getSurfaceCell(mapPos); - Object *o = sc->getObject(); - bool isExplored = (sc->isExplored(world->getThisTeamIndex()) && o != NULL); - //bool isVisible = (sc->isVisible(thisTeamIndex) && o!=NULL); - bool isVisible = true; + SurfaceCell *sc = map->getSurfaceCell(mapPos); + Object *o = sc->getObject(); + bool isExplored = (sc->isExplored(world->getThisTeamIndex()) && o != NULL); + //bool isVisible = (sc->isVisible(thisTeamIndex) && o!=NULL); + bool isVisible = true; - if(isExplored == true && isVisible == true) { - quadCache.visibleObjectList.push_back(o); + if(isExplored == true && isVisible == true) { + quadCache.visibleObjectList.push_back(o); + } } } - } - Quad2i scaledQuad = visibleQuad / Map::cellScale; - PosQuadIterator pqis(map, scaledQuad); - while(pqis.next()) { - const Vec2i &pos= pqis.getPos(); - if(map->isInside(pos)) { - quadCache.visibleScaledCellList.push_back(pos); + Quad2i scaledQuad = visibleQuad / Map::cellScale; + PosQuadIterator pqis(map, scaledQuad); + while(pqis.next()) { + const Vec2i &pos= pqis.getPos(); + if(map->isInside(pos)) { + quadCache.visibleScaledCellList.push_back(pos); + } } - } + quadCache.lastVisibleQuad = visibleQuad; + } quadCache.cacheFrame = world->getFrameCount(); } } diff --git a/source/glest_game/graphics/renderer.h b/source/glest_game/graphics/renderer.h index de085f81..0bcbf3f5 100644 --- a/source/glest_game/graphics/renderer.h +++ b/source/glest_game/graphics/renderer.h @@ -153,6 +153,7 @@ protected: inVisibleUnitList = obj.inVisibleUnitList; //visibleCellList = obj.visibleCellList; visibleScaledCellList = obj.visibleScaledCellList; + lastVisibleQuad = obj.lastVisibleQuad; } public: @@ -172,13 +173,19 @@ public: //bool operator()(const RenderEntity &lhs,const RenderEntity &rhs) const; void clearCacheData() { + clearVolatileCacheData(); + visibleObjectList.clear(); - visibleUnitList.clear(); - inVisibleUnitList.clear(); //visibleCellList.clear(); visibleScaledCellList.clear(); } + void clearVolatileCacheData() { + visibleUnitList.clear(); + inVisibleUnitList.clear(); + } + int cacheFrame; + Quad2i lastVisibleQuad; std::vector visibleObjectList; std::vector visibleUnitList; std::vector inVisibleUnitList; diff --git a/source/shared_lib/include/graphics/math_util.h b/source/shared_lib/include/graphics/math_util.h index 047ce2f7..0df72935 100644 --- a/source/shared_lib/include/graphics/math_util.h +++ b/source/shared_lib/include/graphics/math_util.h @@ -155,6 +155,22 @@ public: return false; } + bool operator !=(const Quad2 &v) const { + if(p[0] != v.p[0]) { + return true; + } + if(p[1] != v.p[1]) { + return true; + } + if(p[2] != v.p[2]) { + return true; + } + if(p[3] != v.p[3]) { + return true; + } + return false; + } + Rect2 computeBoundingRect() const{ return Rect2i( min(p[0].x, p[1].x),