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