MapViewCache.cpp 7.4 KB

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