MapRendererContextState.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 "../CGameInfo.h"
  16. #include "../CPlayerInterface.h"
  17. #include "../adventureMap/AdventureMapInterface.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 CGI->mh->compareObjectBlitOrder(CGI->mh->getMap()->objects[left.getNum()], CGI->mh->getMap()->objects[right.getNum()]);
  24. }
  25. MapRendererContextState::MapRendererContextState()
  26. {
  27. auto mapSize = LOCPLINT->cb->getMapSize();
  28. objects.resize(boost::extents[mapSize.z][mapSize.x][mapSize.y]);
  29. logGlobal->debug("Loading map objects");
  30. for(const auto & obj : CGI->mh->getMap()->objects)
  31. addObject(obj);
  32. logGlobal->debug("Done loading map objects");
  33. }
  34. void MapRendererContextState::addObject(const CGObjectInstance * obj)
  35. {
  36. if(!obj)
  37. return;
  38. for(int fx = 0; fx < obj->getWidth(); ++fx)
  39. {
  40. for(int fy = 0; fy < obj->getHeight(); ++fy)
  41. {
  42. int3 currTile(obj->anchorPos().x - fx, obj->anchorPos().y - fy, obj->anchorPos().z);
  43. if(LOCPLINT->cb->isInTheMap(currTile) && obj->coveringAt(currTile))
  44. {
  45. auto & container = objects[currTile.z][currTile.x][currTile.y];
  46. container.push_back(obj->id);
  47. boost::range::sort(container, compareObjectBlitOrder);
  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(LOCPLINT->cb->isInTheMap(currTile))
  64. {
  65. auto & container = objects[currTile.z][currTile.x][currTile.y];
  66. container.push_back(object->id);
  67. boost::range::sort(container, compareObjectBlitOrder);
  68. }
  69. }
  70. }
  71. }
  72. void MapRendererContextState::removeObject(const CGObjectInstance * object)
  73. {
  74. for(int z = 0; z < LOCPLINT->cb->getMapSize().z; z++)
  75. for(int x = 0; x < LOCPLINT->cb->getMapSize().x; x++)
  76. for(int y = 0; y < LOCPLINT->cb->getMapSize().y; y++)
  77. vstd::erase(objects[z][x][y], object->id);
  78. }