maphandler.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 "StdInc.h"
  13. #include "../lib/int3.h"
  14. #include "Animation.h"
  15. #include <QImage>
  16. #include <QPixmap>
  17. #include <QRect>
  18. VCMI_LIB_NAMESPACE_BEGIN
  19. class CGObjectInstance;
  20. class CGHeroInstance;
  21. class CGBoat;
  22. class CMap;
  23. class PlayerColor;
  24. VCMI_LIB_NAMESPACE_END
  25. struct TileObject
  26. {
  27. CGObjectInstance *obj;
  28. QRect rect;
  29. TileObject(CGObjectInstance *obj_, QRect rect_);
  30. ~TileObject();
  31. };
  32. using TileObjects = std::vector<TileObject>; //pointers to objects being on this tile with rects to be easier to blit this tile on screen
  33. class MapHandler
  34. {
  35. public:
  36. struct AnimBitmapHolder
  37. {
  38. std::shared_ptr<QImage> objBitmap; // main object bitmap
  39. std::shared_ptr<QImage> flagBitmap; // flag bitmap for the object (probably only for heroes and boats with heroes)
  40. AnimBitmapHolder(std::shared_ptr<QImage> objBitmap_ = nullptr, std::shared_ptr<QImage> flagBitmap_ = nullptr)
  41. : objBitmap(objBitmap_),
  42. flagBitmap(flagBitmap_)
  43. {}
  44. };
  45. private:
  46. int index(int x, int y, int z) const;
  47. int index(const int3 &) const;
  48. std::shared_ptr<QImage> findFlagBitmapInternal(std::shared_ptr<Animation> animation, int anim, int group, ui8 dir, bool moving) const;
  49. std::shared_ptr<QImage> findFlagBitmap(const CGHeroInstance * obj, int anim, const PlayerColor color, int group) const;
  50. AnimBitmapHolder findObjectBitmap(const CGObjectInstance * obj, int anim, int group = 0) const;
  51. //FIXME: unique_ptr should be enough, but fails to compile in MSVS 2013
  52. typedef std::map<std::string, std::shared_ptr<Animation>> TFlippedAnimations; //[type, rotation]
  53. typedef std::map<std::string, std::vector<std::shared_ptr<QImage>>> TFlippedCache;//[type, view type, rotation]
  54. TFlippedAnimations terrainAnimations;//[terrain type, rotation]
  55. TFlippedCache terrainImages;//[terrain type, view type, rotation]
  56. TFlippedAnimations roadAnimations;//[road type, rotation]
  57. TFlippedCache roadImages;//[road type, view type, rotation]
  58. TFlippedAnimations riverAnimations;//[river type, rotation]
  59. TFlippedCache riverImages;//[river type, view type, rotation]
  60. std::vector<TileObjects> ttiles; //informations about map tiles
  61. int3 sizes; //map size (x = width, y = height, z = number of levels)
  62. const CMap * map;
  63. enum class EMapCacheType : char
  64. {
  65. TERRAIN, OBJECTS, ROADS, RIVERS, FOW, HEROES, HERO_FLAGS, FRAME, AFTER_LAST
  66. };
  67. void initObjectRects();
  68. void initTerrainGraphics();
  69. QRgb getTileColor(int x, int y, int z);
  70. QPolygon lockBitMask;
  71. public:
  72. MapHandler();
  73. ~MapHandler() = default;
  74. void reset(const CMap * Map);
  75. void updateWater();
  76. void drawTerrainTile(QPainter & painter, int x, int y, int z);
  77. /// draws a river segment on current tile
  78. void drawRiver(QPainter & painter, int x, int y, int z);
  79. /// draws a road segment on current tile
  80. void drawRoad(QPainter & painter, int x, int y, int z);
  81. void invalidate(int x, int y, int z); //invalidates all objects in particular tile
  82. void invalidate(CGObjectInstance *); //invalidates object rects
  83. void invalidate(const std::vector<int3> &); //invalidates all tiles
  84. void invalidateObjects(); //invalidates all objects on the map
  85. std::vector<int3> getTilesUnderObject(CGObjectInstance *) const;
  86. /// draws all objects on current tile (higher-level logic, unlike other draw*** methods)
  87. void drawObjects(QPainter & painter, int x, int y, int z, const std::set<const CGObjectInstance *> & locked);
  88. void drawObject(QPainter & painter, const TileObject & object);
  89. void drawObjectAt(QPainter & painter, const CGObjectInstance * object, int x, int y);
  90. std::vector<TileObject> & getObjects(int x, int y, int z);
  91. void drawMinimapTile(QPainter & painter, int x, int y, int z);
  92. static bool compareObjectBlitOrder(const CGObjectInstance * a, const CGObjectInstance * b);
  93. };