MapRendererContextState.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 "../../CCallback.h"
  15. #include "../CPlayerInterface.h"
  16. #include "../adventureMap/AdventureMapInterface.h"
  17. #include "../../lib/mapObjects/CGHeroInstance.h"
  18. #include "../../lib/mapping/CMap.h"
  19. static bool compareObjectBlitOrder(ObjectInstanceID left, ObjectInstanceID right)
  20. {
  21. //FIXME: remove mh access
  22. return MAPHANDLER->compareObjectBlitOrder(MAPHANDLER->getMap()->objects[left.getNum()], MAPHANDLER->getMap()->objects[right.getNum()]);
  23. }
  24. MapRendererContextState::MapRendererContextState()
  25. {
  26. auto mapSize = LOCPLINT->cb->getMapSize();
  27. objects.resize(boost::extents[mapSize.z][mapSize.x][mapSize.y]);
  28. logGlobal->debug("Loading map objects");
  29. for(const auto & obj : MAPHANDLER->getMap()->objects)
  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(LOCPLINT->cb->isInTheMap(currTile) && obj->coveringAt(currTile))
  43. {
  44. auto & container = objects[currTile.z][currTile.x][currTile.y];
  45. auto position = std::upper_bound(container.begin(), container.end(), obj->id, compareObjectBlitOrder);
  46. container.insert(position, obj->id);
  47. }
  48. }
  49. }
  50. }
  51. void MapRendererContextState::addMovingObject(const CGObjectInstance * object, const int3 & tileFrom, const int3 & tileDest)
  52. {
  53. int xFrom = std::min(tileFrom.x, tileDest.x) - object->getWidth();
  54. int xDest = std::max(tileFrom.x, tileDest.x);
  55. int yFrom = std::min(tileFrom.y, tileDest.y) - object->getHeight();
  56. int yDest = std::max(tileFrom.y, tileDest.y);
  57. for(int x = xFrom; x <= xDest; ++x)
  58. {
  59. for(int y = yFrom; y <= yDest; ++y)
  60. {
  61. int3 currTile(x, y, object->anchorPos().z);
  62. if(LOCPLINT->cb->isInTheMap(currTile))
  63. {
  64. auto & container = objects[currTile.z][currTile.x][currTile.y];
  65. container.push_back(object->id);
  66. boost::range::sort(container, compareObjectBlitOrder);
  67. }
  68. }
  69. }
  70. }
  71. void MapRendererContextState::removeObject(const CGObjectInstance * object)
  72. {
  73. for(int z = 0; z < LOCPLINT->cb->getMapSize().z; z++)
  74. for(int x = 0; x < LOCPLINT->cb->getMapSize().x; x++)
  75. for(int y = 0; y < LOCPLINT->cb->getMapSize().y; y++)
  76. vstd::erase(objects[z][x][y], object->id);
  77. }