2
0

MapViewCache.cpp 5.1 KB

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