2
0

MapViewCache.cpp 7.3 KB

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