MapViewCache.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 "MapRenderer.h"
  13. #include "MapViewModel.h"
  14. #include "../render/CAnimation.h"
  15. #include "../render/Canvas.h"
  16. #include "../render/IImage.h"
  17. #include "../../lib/mapObjects/CObjectHandler.h"
  18. MapViewCache::~MapViewCache() = default;
  19. MapViewCache::MapViewCache(const std::shared_ptr<MapViewModel> & model)
  20. : model(model)
  21. , mapRenderer(new MapRenderer())
  22. , iconsStorage(new CAnimation("VwSymbol"))
  23. , intermediate(new Canvas(Point(32,32)))
  24. , terrain(new Canvas(model->getCacheDimensionsPixels()))
  25. {
  26. iconsStorage->preload();
  27. for (size_t i = 0; i < iconsStorage->size(); ++i)
  28. iconsStorage->getImage(i)->setBlitMode(EImageBlitMode::COLORKEY);
  29. }
  30. Canvas MapViewCache::getTile(const int3 & coordinates)
  31. {
  32. return Canvas(*terrain, model->getCacheTileArea(coordinates));
  33. }
  34. std::shared_ptr<IImage> MapViewCache::getOverlayImageForTile(const std::shared_ptr<MapRendererContext> & context, const int3 & coordinates)
  35. {
  36. if (!context->isVisible(coordinates))
  37. return nullptr;
  38. for(const auto & objectID : context->getObjects(coordinates))
  39. {
  40. const auto * object = context->getObject(objectID);
  41. if (!object->visitableAt(coordinates.x, coordinates.y))
  42. continue;
  43. size_t ownerIndex = PlayerColor::PLAYER_LIMIT.getNum() * static_cast<size_t>(EWorldViewIcon::ICONS_PER_PLAYER);
  44. if (object->tempOwner.isValidPlayer())
  45. ownerIndex = object->tempOwner.getNum() * static_cast<size_t>(EWorldViewIcon::ICONS_PER_PLAYER);
  46. switch (object->ID)
  47. {
  48. case Obj::MONOLITH_ONE_WAY_ENTRANCE:
  49. case Obj::MONOLITH_ONE_WAY_EXIT:
  50. case Obj::MONOLITH_TWO_WAY:
  51. return iconsStorage->getImage(ownerIndex + static_cast<size_t>(EWorldViewIcon::TELEPORT));
  52. case Obj::SUBTERRANEAN_GATE:
  53. return iconsStorage->getImage(ownerIndex + static_cast<size_t>(EWorldViewIcon::GATE));
  54. case Obj::ARTIFACT:
  55. return iconsStorage->getImage(ownerIndex + static_cast<size_t>(EWorldViewIcon::ARTIFACT));
  56. case Obj::TOWN:
  57. return iconsStorage->getImage(ownerIndex + static_cast<size_t>(EWorldViewIcon::TOWN));
  58. case Obj::HERO:
  59. return iconsStorage->getImage(ownerIndex + static_cast<size_t>(EWorldViewIcon::HERO));
  60. case Obj::MINE:
  61. return iconsStorage->getImage(ownerIndex + static_cast<size_t>(EWorldViewIcon::MINE_WOOD) + object->subID);
  62. case Obj::RESOURCE:
  63. return iconsStorage->getImage(ownerIndex + static_cast<size_t>(EWorldViewIcon::RES_WOOD) + object->subID);
  64. }
  65. }
  66. return nullptr;
  67. }
  68. void MapViewCache::updateTile(const std::shared_ptr<MapRendererContext> & context, const int3 & coordinates)
  69. {
  70. Canvas target = getTile(coordinates);
  71. if(model->getSingleTileSize() == Point(32, 32))
  72. {
  73. mapRenderer->renderTile(*context, target, coordinates);
  74. }
  75. else
  76. {
  77. mapRenderer->renderTile(*context, *intermediate, coordinates);
  78. target.drawScaled(*intermediate, Point(0, 0), model->getSingleTileSize());
  79. }
  80. }
  81. void MapViewCache::update(const std::shared_ptr<MapRendererContext> & context)
  82. {
  83. Rect dimensions = model->getTilesTotalRect();
  84. for(int y = dimensions.top(); y < dimensions.bottom(); ++y)
  85. for(int x = dimensions.left(); x < dimensions.right(); ++x)
  86. updateTile(context, {x, y, model->getLevel()});
  87. }
  88. void MapViewCache::render(const std::shared_ptr<MapRendererContext> & context, Canvas & target)
  89. {
  90. Rect dimensions = model->getTilesTotalRect();
  91. for(int y = dimensions.top(); y < dimensions.bottom(); ++y)
  92. {
  93. for(int x = dimensions.left(); x < dimensions.right(); ++x)
  94. {
  95. int3 tile(x, y, model->getLevel());
  96. Canvas source = getTile(tile);
  97. Rect targetRect = model->getTargetTileArea(tile);
  98. target.draw(source, targetRect.topLeft());
  99. if (context->showOverlay())
  100. {
  101. auto overlay = getOverlayImageForTile(context, tile);
  102. if (overlay)
  103. {
  104. Point position = targetRect.center() - overlay->dimensions() / 2;
  105. target.draw(overlay, position);
  106. }
  107. }
  108. }
  109. }
  110. }