MapViewCache.cpp 5.2 KB

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