MapViewCache.cpp 6.2 KB

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