MapRendererContextState.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * MapRendererContext.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 "MapRendererContextState.h"
  12. #include "IMapRendererContext.h"
  13. #include "mapHandler.h"
  14. #include "../CPlayerInterface.h"
  15. #include "../GameInstance.h"
  16. #include "../adventureMap/AdventureMapInterface.h"
  17. #include "../../lib/callback/CCallback.h"
  18. #include "../../lib/mapObjects/CGHeroInstance.h"
  19. #include "../../lib/mapping/CMap.h"
  20. static bool compareObjectBlitOrder(ObjectInstanceID left, ObjectInstanceID right)
  21. {
  22. //FIXME: remove mh access
  23. return CMap::compareObjectBlitOrder(GAME->map().getMap()->getObject(left), GAME->map().getMap()->getObject(right));
  24. }
  25. MapRendererContextState::MapRendererContextState()
  26. : objects(GAME->interface()->cb->getMapSize())
  27. {
  28. logGlobal->debug("Loading map objects");
  29. for(const auto & obj : GAME->map().getMap()->getObjects())
  30. addObject(obj);
  31. logGlobal->debug("Done loading map objects");
  32. }
  33. void MapRendererContextState::addObject(const CGObjectInstance * obj)
  34. {
  35. if(!obj)
  36. return;
  37. for(int fx = 0; fx < obj->getWidth(); ++fx)
  38. {
  39. for(int fy = 0; fy < obj->getHeight(); ++fy)
  40. {
  41. int3 currTile(obj->anchorPos().x - fx, obj->anchorPos().y - fy, obj->anchorPos().z);
  42. if(GAME->interface()->cb->isInTheMap(currTile) && obj->coveringAt(currTile))
  43. {
  44. auto & container = objects[currTile];
  45. auto position = std::upper_bound(container.begin(), container.end(), obj->id, compareObjectBlitOrder);
  46. container.insert(position, obj->id);
  47. usedTiles[obj->id].push_back(currTile);
  48. }
  49. }
  50. }
  51. }
  52. void MapRendererContextState::addMovingObject(const CGObjectInstance * object, const int3 & tileFrom, const int3 & tileDest)
  53. {
  54. int xFrom = std::min(tileFrom.x, tileDest.x) - object->getWidth();
  55. int xDest = std::max(tileFrom.x, tileDest.x);
  56. int yFrom = std::min(tileFrom.y, tileDest.y) - object->getHeight();
  57. int yDest = std::max(tileFrom.y, tileDest.y);
  58. for(int x = xFrom; x <= xDest; ++x)
  59. {
  60. for(int y = yFrom; y <= yDest; ++y)
  61. {
  62. int3 currTile(x, y, object->anchorPos().z);
  63. if(GAME->interface()->cb->isInTheMap(currTile))
  64. {
  65. auto & container = objects[currTile];
  66. auto position = std::upper_bound(container.begin(), container.end(), object->id, compareObjectBlitOrder);
  67. container.insert(position, object->id);
  68. usedTiles[object->id].push_back(currTile);
  69. }
  70. }
  71. }
  72. }
  73. void MapRendererContextState::removeObject(const CGObjectInstance * object)
  74. {
  75. for (const auto & usedTile : usedTiles[object->id])
  76. vstd::erase(objects[usedTile], object->id);
  77. usedTiles.erase(object->id);
  78. }