MapViewCache.cpp 6.1 KB

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