当前位置:   article > 正文

QGraphicsView实现简易地图8『缓存视口周边瓦片』

QGraphicsView实现简易地图8『缓存视口周边瓦片』

前文链接:QGraphicsView实现简易地图7『异步加载-多瓦片-无底图』
前7篇的地图加载,都采用最少瓦片数量的算法,即用最少数量的瓦片覆盖视口,以获得最快的加载速度。但是这样会带来一个问题,那就是每当移动地图时,视口周边的瓦片才会加载,这样会造成地图的延时甚至卡顿,而这会令用户感到非常反感。为此,需要在之前的算法上进行改进:加载覆盖视口的最少瓦片后,立即加载视口周边瓦片;加载过的瓦片离开视口后不再删除,以加快下次进入视口后的渲染。
1、动态演示效果

2、获取视口及周边瓦片代码:以视口宽高的一半向四周扩展

QRect CommonUtility::getViewAndAroundTileCoords(int tempTileTop, int tileLeft, int tempTileBottom, int tileRight, int level, vector<TileCoord> &vecTileCoord)
{
	// <1> 视口
	for (int row = tempTileTop; row <= tempTileBottom; ++row)
	{
		for (int col = tileLeft; col <= tileRight; ++col)
		{
			vecTileCoord.push_back(TileCoord(col, row));
		}
	}

	int mapSize = pow(2, level);
	int tileW = tileRight - tileLeft + 1;
	int tileH = tempTileBottom - tempTileTop + 1;
	int tempTileT, tempTileL, tempTileB, tempTileR;
	int tileT, tileL, tileB, tileR;

	// <2> 上侧
	tileT = tempTileT = qMax(tempTileTop - tileH / 2, 0);
	tempTileB = tempTileTop - 1;
	tempTileL = qMax(tileLeft - tileW / 2, 0);	
	tempTileR = qMin(tileRight + tileW / 2, mapSize - 1);
	for (int row = tempTileT; row <= tempTileB; ++row)
	{
		for (int col = tempTileL; col <= tempTileR; ++col)
		{
			vecTileCoord.push_back(TileCoord(col, row));
		}
	}

	// <3> 下侧
	tempTileT = tempTileBottom + 1;
	tileB = tempTileB = qMin(tempTileBottom + tileH / 2, mapSize - 1);
	tempTileL = qMax(tileLeft - tileW / 2, 0);
	tempTileR = qMin(tileRight + tileW / 2, mapSize - 1);
	for (int row = tempTileT; row <= tempTileB; ++row)
	{
		for (int col = tempTileL; col <= tempTileR; ++col)
		{
			vecTileCoord.push_back(TileCoord(col, row));
		}
	}

	// <4> 左侧
	tileL = tempTileL = qMax(tileLeft - tileW / 2, 0);
	tempTileR = tileLeft - 1;
	for (int row = tempTileTop; row <= tempTileBottom; ++row)
	{
		for (int col = tempTileL; col <= tempTileR; ++col)
		{
			vecTileCoord.push_back(TileCoord(col, row));
		}
	}

	// <5> 右侧
	tempTileL = tileRight + 1;
	tileR = tempTileR = qMin(tileRight + tileW / 2, mapSize - 1);
	for (int row = tempTileTop; row <= tempTileBottom; ++row)
	{
		for (int col = tempTileL; col <= tempTileR; ++col)
		{
			vecTileCoord.push_back(TileCoord(col, row));
		}
	}

	return QRect(tileL, tileT, (tileR - tileL + 1), (tileB - tileT + 1));
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67

  • 1

  • 1
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/539506
推荐阅读
相关标签
  

闽ICP备14008679号