MapView.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * MapView.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 "MapView.h"
  12. #include "MapRenderer.h"
  13. #include "mapHandler.h"
  14. #include "CAdvMapInt.h"
  15. #include "../CGameInfo.h"
  16. #include "../CMusicHandler.h"
  17. #include "../CPlayerInterface.h"
  18. #include "../gui/CGuiHandler.h"
  19. #include "../render/CAnimation.h"
  20. #include "../render/CFadeAnimation.h"
  21. #include "../render/Canvas.h"
  22. #include "../render/Colors.h"
  23. #include "../render/Graphics.h"
  24. #include "../render/IImage.h"
  25. #include "../renderSDL/SDL_Extensions.h"
  26. #include "../../CCallback.h"
  27. #include "../../lib/CConfigHandler.h"
  28. #include "../../lib/CGeneralTextHandler.h"
  29. #include "../../lib/CRandomGenerator.h"
  30. #include "../../lib/CStopWatch.h"
  31. #include "../../lib/Color.h"
  32. #include "../../lib/RiverHandler.h"
  33. #include "../../lib/RoadHandler.h"
  34. #include "../../lib/TerrainHandler.h"
  35. #include "../../lib/mapObjects/CGHeroInstance.h"
  36. #include "../../lib/mapObjects/CObjectClassesHandler.h"
  37. #include "../../lib/mapping/CMap.h"
  38. MapCache::~MapCache() = default;
  39. MapCache::MapCache(const std::shared_ptr<MapViewModel> & model)
  40. : model(model)
  41. , context(new MapRendererContext())
  42. , mapRenderer(new MapRenderer(*context))
  43. {
  44. terrain = std::make_unique<Canvas>(model->getCacheDimensionsPixels());
  45. }
  46. Canvas MapCache::getTile(const int3 & coordinates)
  47. {
  48. return Canvas(*terrain, model->getCacheTileArea(coordinates));
  49. }
  50. void MapCache::updateTile(const int3 & coordinates)
  51. {
  52. Canvas target = getTile(coordinates);
  53. mapRenderer->renderTile(*context, target, coordinates);
  54. }
  55. void MapCache::update(uint32_t timeDelta)
  56. {
  57. context->advanceAnimations(timeDelta);
  58. context->setTileSize(model->getSingleTileSize());
  59. Rect dimensions = model->getTilesTotalRect();
  60. for(int y = dimensions.top(); y < dimensions.bottom(); ++y)
  61. for(int x = dimensions.left(); x < dimensions.right(); ++x)
  62. updateTile({x, y, model->getLevel()});
  63. }
  64. void MapCache::render(Canvas & target)
  65. {
  66. update(GH.mainFPSmng->getElapsedMilliseconds());
  67. Rect dimensions = model->getTilesTotalRect();
  68. for(int y = dimensions.top(); y < dimensions.bottom(); ++y)
  69. {
  70. for(int x = dimensions.left(); x < dimensions.right(); ++x)
  71. {
  72. int3 tile( x,y,model->getLevel());
  73. Canvas source = getTile(tile);
  74. Rect targetRect = model->getTargetTileArea(tile);
  75. target.draw(source, targetRect.topLeft());
  76. }
  77. }
  78. }
  79. std::shared_ptr<MapViewModel> MapView::createModel(const Point & dimensions) const
  80. {
  81. auto result = std::make_shared<MapViewModel>();
  82. result->setLevel(0);
  83. result->setTileSize(Point(32,32));
  84. result->setViewCenter(Point(0,0));
  85. result->setViewDimensions(dimensions);
  86. return result;
  87. }
  88. MapView::MapView(const Point & offset, const Point & dimensions)
  89. : model(createModel(dimensions))
  90. , tilesCache(new MapCache(model))
  91. {
  92. pos += offset;
  93. pos.w = dimensions.x;
  94. pos.h = dimensions.y;
  95. }
  96. void MapView::show(SDL_Surface * to)
  97. {
  98. Canvas target(to);
  99. Canvas targetClipped(target, pos);
  100. CSDL_Ext::CClipRectGuard guard(to, pos);
  101. tilesCache->render(targetClipped);
  102. }
  103. void MapView::showAll(SDL_Surface * to)
  104. {
  105. show(to);
  106. }
  107. void MapRendererContext::advanceAnimations(uint32_t ms)
  108. {
  109. animationTime += ms;
  110. }
  111. int3 MapRendererContext::getMapSize() const
  112. {
  113. return LOCPLINT->cb->getMapSize();
  114. }
  115. bool MapRendererContext::isInMap(const int3 & coordinates) const
  116. {
  117. return LOCPLINT->cb->isInTheMap(coordinates);
  118. }
  119. const TerrainTile & MapRendererContext::getMapTile(const int3 & coordinates) const
  120. {
  121. return CGI->mh->map->getTile(coordinates);
  122. }
  123. MapRendererContext::ObjectsVector MapRendererContext::getAllObjects() const
  124. {
  125. return CGI->mh->map->objects;
  126. }
  127. const CGObjectInstance * MapRendererContext::getObject(ObjectInstanceID objectID) const
  128. {
  129. return CGI->mh->map->objects.at(objectID.getNum());
  130. }
  131. bool MapRendererContext::isVisible(const int3 & coordinates) const
  132. {
  133. return LOCPLINT->cb->isVisible(coordinates) || settings["session"]["spectate"].Bool();
  134. }
  135. void MapRendererContext::setTileSize(const Point & dimensions)
  136. {
  137. tileSize = dimensions;
  138. }
  139. const CGPath * MapRendererContext::currentPath() const
  140. {
  141. const auto * hero = adventureInt->curHero();
  142. if(!hero)
  143. return nullptr;
  144. if(!LOCPLINT->paths.hasPath(hero))
  145. return nullptr;
  146. return &LOCPLINT->paths.getPath(hero);
  147. }
  148. uint32_t MapRendererContext::getAnimationPeriod() const
  149. {
  150. // H3 timing for adventure map objects animation is 180 ms
  151. // Terrain animations also use identical interval, however those are only present in HotA and/or HD Mod
  152. // TODO: duration of fade-in/fade-out for teleport, entering/leaving boat, removal of objects
  153. // TOOD: duration of hero movement animation, frame timing of hero movement animation, effect of hero speed option
  154. // TOOD: duration of enemy hero movement animation, frame timing of enemy hero movement animation, effect of enemy hero speed option
  155. return 180;
  156. }
  157. uint32_t MapRendererContext::getAnimationTime() const
  158. {
  159. return animationTime;
  160. }
  161. Point MapRendererContext::getTileSize() const
  162. {
  163. return Point(32, 32);
  164. }
  165. bool MapRendererContext::showGrid() const
  166. {
  167. return true; // settings["session"]["showGrid"].Bool();
  168. }
  169. void MapView::setViewCenter(const int3 & position)
  170. {
  171. model->setViewCenter(Point(position.x, position.y) * model->getSingleTileSize());
  172. model->setLevel(position.z);
  173. }
  174. void MapView::setViewCenter(const Point & position, int level)
  175. {
  176. model->setViewCenter(position);
  177. model->setLevel(level);
  178. }
  179. void MapView::setTileSize(const Point & tileSize)
  180. {
  181. model->setTileSize(tileSize);
  182. }
  183. std::shared_ptr<const MapViewModel> MapView::getModel() const
  184. {
  185. return model;
  186. }
  187. void MapViewModel::setTileSize(const Point & newValue)
  188. {
  189. tileSize = newValue;
  190. }
  191. void MapViewModel::setViewCenter(const Point & newValue)
  192. {
  193. viewCenter = newValue;
  194. }
  195. void MapViewModel::setViewDimensions(const Point & newValue)
  196. {
  197. viewDimensions = newValue;
  198. }
  199. void MapViewModel::setLevel(int newLevel)
  200. {
  201. mapLevel = newLevel;
  202. }
  203. Point MapViewModel::getSingleTileSize() const
  204. {
  205. return tileSize;
  206. }
  207. Point MapViewModel::getMapViewCenter() const
  208. {
  209. return viewCenter;
  210. }
  211. Point MapViewModel::getPixelsVisibleDimensions() const
  212. {
  213. return viewDimensions;
  214. }
  215. int MapViewModel::getLevel() const
  216. {
  217. return mapLevel;
  218. }
  219. Point MapViewModel::getTilesVisibleDimensions() const
  220. {
  221. // total number of potentially visible tiles is:
  222. // 1) number of completely visible tiles
  223. // 2) additional tile that might be partially visible from left/top size
  224. // 3) additional tile that might be partially visible from right/bottom size
  225. return {
  226. getPixelsVisibleDimensions().x / getSingleTileSize().x + 2,
  227. getPixelsVisibleDimensions().y / getSingleTileSize().y + 2,
  228. };
  229. }
  230. Rect MapViewModel::getTilesTotalRect() const
  231. {
  232. return Rect(
  233. Point(getTileAtPoint(Point(0,0))),
  234. getTilesVisibleDimensions()
  235. );
  236. }
  237. int3 MapViewModel::getTileCenter() const
  238. {
  239. return getTileAtPoint(getMapViewCenter());
  240. }
  241. int3 MapViewModel::getTileAtPoint(const Point & position) const
  242. {
  243. Point topLeftOffset = getMapViewCenter() - getPixelsVisibleDimensions() / 2;
  244. Point absolutePosition = position + topLeftOffset;
  245. // NOTE: using division via double in order to use std::floor
  246. // which rounds to negative infinity and not towards zero (like integer division)
  247. return {
  248. static_cast<int>(std::floor(static_cast<double>(absolutePosition.x) / getSingleTileSize().x)),
  249. static_cast<int>(std::floor(static_cast<double>(absolutePosition.y) / getSingleTileSize().y)),
  250. getLevel()
  251. };
  252. }
  253. Point MapViewModel::getCacheDimensionsPixels() const
  254. {
  255. return getTilesVisibleDimensions() * getSingleTileSize();
  256. }
  257. Rect MapViewModel::getCacheTileArea(const int3 & coordinates) const
  258. {
  259. assert(mapLevel == coordinates.z);
  260. assert(getTilesVisibleDimensions().x + coordinates.x >= 0);
  261. assert(getTilesVisibleDimensions().y + coordinates.y >= 0);
  262. Point tileIndex{
  263. (getTilesVisibleDimensions().x + coordinates.x) % getTilesVisibleDimensions().x,
  264. (getTilesVisibleDimensions().y + coordinates.y) % getTilesVisibleDimensions().y
  265. };
  266. return Rect(tileIndex * tileSize, tileSize);
  267. }
  268. Rect MapViewModel::getTargetTileArea(const int3 & coordinates) const
  269. {
  270. Point topLeftOffset = getMapViewCenter() - getPixelsVisibleDimensions() / 2;
  271. Point tilePosAbsolute = Point(coordinates) * tileSize;
  272. Point tilePosRelative = tilePosAbsolute - topLeftOffset;
  273. return {
  274. tilePosRelative,
  275. tileSize
  276. };
  277. }