MapViewCache.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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)
  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. oldCacheEntry = newCacheEntry;
  74. tilesUpToDate[cacheX][cacheY] = false;
  75. }
  76. void MapViewCache::update(const std::shared_ptr<const IMapRendererContext> & context)
  77. {
  78. Rect dimensions = model->getTilesTotalRect();
  79. if (dimensions.w != terrainChecksum.shape()[0] || dimensions.h != terrainChecksum.shape()[1])
  80. {
  81. boost::multi_array<TileChecksum, 2> newCache;
  82. newCache.resize(boost::extents[dimensions.w][dimensions.h]);
  83. std::swap(newCache, terrainChecksum);
  84. }
  85. for(int y = dimensions.top(); y < dimensions.bottom(); ++y)
  86. for(int x = dimensions.left(); x < dimensions.right(); ++x)
  87. updateTile(context, {x, y, model->getLevel()});
  88. cachedLevel = model->getLevel();
  89. }
  90. void MapViewCache::render(const std::shared_ptr<const IMapRendererContext> & context, Canvas & target, bool fullRedraw)
  91. {
  92. bool mapMoved = (cachedPosition != model->getMapViewCenter());
  93. bool lazyUpdate = !mapMoved && !fullRedraw;
  94. Rect dimensions = model->getTilesTotalRect();
  95. if (dimensions.w != tilesUpToDate.shape()[0] || dimensions.h != tilesUpToDate.shape()[1])
  96. {
  97. boost::multi_array<bool, 2> newCache;
  98. newCache.resize(boost::extents[dimensions.w][dimensions.h]);
  99. std::swap(newCache, tilesUpToDate);
  100. }
  101. for(int y = dimensions.top(); y < dimensions.bottom(); ++y)
  102. {
  103. for(int x = dimensions.left(); x < dimensions.right(); ++x)
  104. {
  105. int cacheX = (terrainChecksum.shape()[0] + x) % terrainChecksum.shape()[0];
  106. int cacheY = (terrainChecksum.shape()[1] + y) % terrainChecksum.shape()[1];
  107. if (lazyUpdate && tilesUpToDate[cacheX][cacheY])
  108. continue;
  109. int3 tile(x, y, model->getLevel());
  110. Canvas source = getTile(tile);
  111. Rect targetRect = model->getTargetTileArea(tile);
  112. target.draw(source, targetRect.topLeft());
  113. tilesUpToDate[cacheX][cacheY] = true;
  114. }
  115. }
  116. if (context->showOverlay())
  117. {
  118. for(int y = dimensions.top(); y < dimensions.bottom(); ++y)
  119. {
  120. for(int x = dimensions.left(); x < dimensions.right(); ++x)
  121. {
  122. int3 tile(x, y, model->getLevel());
  123. Rect targetRect = model->getTargetTileArea(tile);
  124. auto overlay = getOverlayImageForTile(context, tile);
  125. if (overlay)
  126. {
  127. Point position = targetRect.center() - overlay->dimensions() / 2;
  128. target.draw(overlay, position);
  129. }
  130. }
  131. }
  132. }
  133. cachedPosition = model->getMapViewCenter();
  134. }