maphandler.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * maphandler.h, 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. #pragma once
  11. //code is copied from vcmiclient/mapHandler.h with minimal changes
  12. #include "../lib/int3.h"
  13. #include "Animation.h"
  14. #include <QImage>
  15. #include <QPixmap>
  16. #include <QRect>
  17. VCMI_LIB_NAMESPACE_BEGIN
  18. class CGObjectInstance;
  19. class CGHeroInstance;
  20. class CGBoat;
  21. class CMap;
  22. class PlayerColor;
  23. VCMI_LIB_NAMESPACE_END
  24. struct ObjectRect
  25. {
  26. const CGObjectInstance * obj;
  27. QRect rect;
  28. ObjectRect(const CGObjectInstance * obj_, QRect rect_);
  29. ~ObjectRect();
  30. };
  31. using TileObjects = std::vector<ObjectRect>; //pointers to objects being on this tile with rects to be easier to blit this tile on screen
  32. class MapHandler
  33. {
  34. public:
  35. struct BitmapHolder
  36. {
  37. std::shared_ptr<QImage> objBitmap; // main object bitmap
  38. std::shared_ptr<QImage> flagBitmap; // flag bitmap for the object (probably only for heroes and boats with heroes)
  39. BitmapHolder(std::shared_ptr<QImage> objBitmap_ = nullptr, std::shared_ptr<QImage> flagBitmap_ = nullptr)
  40. : objBitmap(objBitmap_),
  41. flagBitmap(flagBitmap_)
  42. {}
  43. };
  44. private:
  45. int index(int x, int y, int z) const;
  46. int index(const int3 &) const;
  47. std::shared_ptr<QImage> findFlagBitmapInternal(std::shared_ptr<Animation> animation, int anim, int group, ui8 dir, bool moving) const;
  48. std::shared_ptr<QImage> findFlagBitmap(const CGHeroInstance * obj, int anim, const PlayerColor color, int group) const;
  49. BitmapHolder findObjectBitmap(const CGObjectInstance * obj, int anim, int group = 0) const;
  50. //FIXME: unique_ptr should be enough, but fails to compile in MSVS 2013
  51. typedef std::map<std::string, std::shared_ptr<Animation>> TFlippedAnimations; //[type, rotation]
  52. typedef std::map<std::string, std::vector<std::shared_ptr<QImage>>> TFlippedCache;//[type, view type, rotation]
  53. TFlippedAnimations terrainAnimations;//[terrain type, rotation]
  54. TFlippedCache terrainImages;//[terrain type, view type, rotation]
  55. TFlippedAnimations roadAnimations;//[road type, rotation]
  56. TFlippedCache roadImages;//[road type, view type, rotation]
  57. TFlippedAnimations riverAnimations;//[river type, rotation]
  58. TFlippedCache riverImages;//[river type, view type, rotation]
  59. std::vector<TileObjects> tileObjects; //information about map tiles
  60. std::map<const CGObjectInstance *, std::set<int3>> tilesCache; //set of tiles belonging to object
  61. const CMap * map = nullptr;
  62. void initObjectRects();
  63. void initTerrainGraphics();
  64. QRgb getTileColor(int x, int y, int z);
  65. std::shared_ptr<QImage> getObjectImage(const CGObjectInstance * obj);
  66. public:
  67. MapHandler();
  68. ~MapHandler() = default;
  69. void reset(const CMap * Map);
  70. void drawTerrainTile(QPainter & painter, int x, int y, int z, QPointF offset);
  71. /// draws a river segment on current tile
  72. void drawRiver(QPainter & painter, int x, int y, int z, QPointF offset);
  73. /// draws a road segment on current tile
  74. void drawRoad(QPainter & painter, int x, int y, int z, QPointF offset);
  75. std::set<int3> invalidate(const CGObjectInstance *); //invalidates object rects
  76. void invalidateObjects(); //invalidates all objects on the map
  77. const std::set<int3> & getTilesUnderObject(const CGObjectInstance *) const;
  78. //get objects at position
  79. std::vector<ObjectRect> & getObjects(const int3 & tile);
  80. std::vector<ObjectRect> & getObjects(int x, int y, int z);
  81. //returns set of tiles to draw
  82. std::set<int3> removeObject(const CGObjectInstance * object);
  83. std::set<int3> addObject(const CGObjectInstance * object);
  84. /// draws all objects on current tile (higher-level logic, unlike other draw*** methods)
  85. void drawObjects(QPainter & painter, int x, int y, int z, QPointF offset, const std::set<const CGObjectInstance *> & locked);
  86. void drawObjectAt(QPainter & painter, const CGObjectInstance * object, int x, int y, QPointF offset);
  87. void drawMinimapTile(QPainter & painter, int x, int y, int z);
  88. static bool compareObjectBlitOrder(const CGObjectInstance * a, const CGObjectInstance * b);
  89. };