MapViewCache.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * MapViewCache.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "MapViewCache.h"
  12. #include "IMapRendererContext.h"
  13. #include "MapRenderer.h"
  14. #include "MapViewModel.h"
  15. #include "../render/CAnimation.h"
  16. #include "../render/Canvas.h"
  17. #include "../render/IImage.h"
  18. #include "../../lib/mapObjects/CObjectHandler.h"
  19. MapViewCache::~MapViewCache() = default;
  20. MapViewCache::MapViewCache(const std::shared_ptr<MapViewModel> & model)
  21. : model(model)
  22. , cachedLevel(0)
  23. , mapRenderer(new MapRenderer())
  24. , iconsStorage(new CAnimation("VwSymbol"))
  25. , intermediate(new Canvas(Point(32, 32)))
  26. , terrain(new Canvas(model->getCacheDimensionsPixels()))
  27. {
  28. iconsStorage->preload();
  29. for(size_t i = 0; i < iconsStorage->size(); ++i)
  30. iconsStorage->getImage(i)->setBlitMode(EImageBlitMode::COLORKEY);
  31. Point visibleSize = model->getTilesVisibleDimensions();
  32. terrainChecksum.resize(boost::extents[visibleSize.x][visibleSize.y]);
  33. tilesUpToDate.resize(boost::extents[visibleSize.x][visibleSize.y]);
  34. }
  35. Canvas MapViewCache::getTile(const int3 & coordinates)
  36. {
  37. return Canvas(*terrain, model->getCacheTileArea(coordinates));
  38. }
  39. std::shared_ptr<IImage> MapViewCache::getOverlayImageForTile(const std::shared_ptr<IMapRendererContext> & context, const int3 & coordinates)
  40. {
  41. size_t imageIndex = context->overlayImageIndex(coordinates);
  42. if(imageIndex < iconsStorage->size())
  43. return iconsStorage->getImage(imageIndex);
  44. return nullptr;
  45. }
  46. void MapViewCache::invalidate(const std::shared_ptr<IMapRendererContext> & context, const int3 & tile)
  47. {
  48. int cacheX = (terrainChecksum.shape()[0] + tile.x) % terrainChecksum.shape()[0];
  49. int cacheY = (terrainChecksum.shape()[1] + tile.y) % terrainChecksum.shape()[1];
  50. auto & entry = terrainChecksum[cacheX][cacheY];
  51. if(entry.tileX == tile.x && entry.tileY == tile.y)
  52. entry = TileChecksum{};
  53. }
  54. void MapViewCache::invalidate(const std::shared_ptr<IMapRendererContext> & context, const ObjectInstanceID & object)
  55. {
  56. for(size_t cacheY = 0; cacheY < terrainChecksum.shape()[1]; ++cacheY)
  57. {
  58. for(size_t cacheX = 0; cacheX < terrainChecksum.shape()[0]; ++cacheX)
  59. {
  60. auto & entry = terrainChecksum[cacheX][cacheY];
  61. int3 tile(entry.tileX, entry.tileY, cachedLevel);
  62. if(context->isInMap(tile) && vstd::contains(context->getObjects(tile), object))
  63. entry = TileChecksum{};
  64. }
  65. }
  66. }
  67. void MapViewCache::updateTile(const std::shared_ptr<IMapRendererContext> & context, const int3 & coordinates)
  68. {
  69. int cacheX = (terrainChecksum.shape()[0] + coordinates.x) % terrainChecksum.shape()[0];
  70. int cacheY = (terrainChecksum.shape()[1] + coordinates.y) % terrainChecksum.shape()[1];
  71. auto & oldCacheEntry = terrainChecksum[cacheX][cacheY];
  72. TileChecksum newCacheEntry;
  73. newCacheEntry.tileX = coordinates.x;
  74. newCacheEntry.tileY = coordinates.y;
  75. newCacheEntry.checksum = mapRenderer->getTileChecksum(*context, coordinates);
  76. if(cachedLevel == coordinates.z && oldCacheEntry == newCacheEntry && !context->tileAnimated(coordinates))
  77. return;
  78. Canvas target = getTile(coordinates);
  79. if(model->getSingleTileSize() == Point(32, 32))
  80. {
  81. mapRenderer->renderTile(*context, target, coordinates);
  82. }
  83. else
  84. {
  85. mapRenderer->renderTile(*context, *intermediate, coordinates);
  86. target.drawScaled(*intermediate, Point(0, 0), model->getSingleTileSize());
  87. }
  88. if(context->filterGrayscale())
  89. target.applyGrayscale();
  90. oldCacheEntry = newCacheEntry;
  91. tilesUpToDate[cacheX][cacheY] = false;
  92. }
  93. void MapViewCache::update(const std::shared_ptr<IMapRendererContext> & context)
  94. {
  95. Rect dimensions = model->getTilesTotalRect();
  96. if(dimensions.w != terrainChecksum.shape()[0] || dimensions.h != terrainChecksum.shape()[1])
  97. {
  98. boost::multi_array<TileChecksum, 2> newCache;
  99. newCache.resize(boost::extents[dimensions.w][dimensions.h]);
  100. terrainChecksum.resize(boost::extents[dimensions.w][dimensions.h]);
  101. terrainChecksum = newCache;
  102. }
  103. if(dimensions.w != tilesUpToDate.shape()[0] || dimensions.h != tilesUpToDate.shape()[1])
  104. {
  105. boost::multi_array<bool, 2> newCache;
  106. newCache.resize(boost::extents[dimensions.w][dimensions.h]);
  107. tilesUpToDate.resize(boost::extents[dimensions.w][dimensions.h]);
  108. tilesUpToDate = newCache;
  109. }
  110. for(int y = dimensions.top(); y < dimensions.bottom(); ++y)
  111. for(int x = dimensions.left(); x < dimensions.right(); ++x)
  112. updateTile(context, {x, y, model->getLevel()});
  113. cachedLevel = model->getLevel();
  114. }
  115. void MapViewCache::render(const std::shared_ptr<IMapRendererContext> & context, Canvas & target, bool fullRedraw)
  116. {
  117. bool mapMoved = (cachedPosition != model->getMapViewCenter());
  118. bool lazyUpdate = !mapMoved && !fullRedraw;
  119. Rect dimensions = model->getTilesTotalRect();
  120. for(int y = dimensions.top(); y < dimensions.bottom(); ++y)
  121. {
  122. for(int x = dimensions.left(); x < dimensions.right(); ++x)
  123. {
  124. int cacheX = (terrainChecksum.shape()[0] + x) % terrainChecksum.shape()[0];
  125. int cacheY = (terrainChecksum.shape()[1] + y) % terrainChecksum.shape()[1];
  126. int3 tile(x, y, model->getLevel());
  127. if(lazyUpdate && tilesUpToDate[cacheX][cacheY])
  128. continue;
  129. Canvas source = getTile(tile);
  130. Rect targetRect = model->getTargetTileArea(tile);
  131. target.draw(source, targetRect.topLeft());
  132. tilesUpToDate[cacheX][cacheY] = true;
  133. }
  134. }
  135. if(context->showOverlay())
  136. {
  137. for(int y = dimensions.top(); y < dimensions.bottom(); ++y)
  138. {
  139. for(int x = dimensions.left(); x < dimensions.right(); ++x)
  140. {
  141. int3 tile(x, y, model->getLevel());
  142. Rect targetRect = model->getTargetTileArea(tile);
  143. auto overlay = getOverlayImageForTile(context, tile);
  144. if(overlay)
  145. {
  146. Point position = targetRect.center() - overlay->dimensions() / 2;
  147. target.draw(overlay, position);
  148. }
  149. }
  150. }
  151. }
  152. cachedPosition = model->getMapViewCenter();
  153. }