maphandler.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 ObjectRect
  26. {
  27. const CGObjectInstance * obj;
  28. QRect rect;
  29. ObjectRect(const CGObjectInstance * obj_, QRect rect_);
  30. ~ObjectRect();
  31. };
  32. using TileObjects = std::vector<ObjectRect>; //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 BitmapHolder
  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. BitmapHolder(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. BitmapHolder 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> tileObjects; //informations about map tiles
  61. std::map<const CGObjectInstance *, std::set<int3>> tilesCache; //set of tiles beloging to object
  62. const CMap * map = nullptr;
  63. void initObjectRects();
  64. void initTerrainGraphics();
  65. QRgb getTileColor(int x, int y, int z);
  66. std::shared_ptr<QImage> getObjectImage(const CGObjectInstance * obj);
  67. public:
  68. MapHandler();
  69. ~MapHandler() = default;
  70. void reset(const CMap * Map);
  71. void drawTerrainTile(QPainter & painter, int x, int y, int z);
  72. /// draws a river segment on current tile
  73. void drawRiver(QPainter & painter, int x, int y, int z);
  74. /// draws a road segment on current tile
  75. void drawRoad(QPainter & painter, int x, int y, int z);
  76. std::set<int3> invalidate(const CGObjectInstance *); //invalidates object rects
  77. void invalidateObjects(); //invalidates all objects on the map
  78. const std::set<int3> & getTilesUnderObject(const CGObjectInstance *) const;
  79. //get objects at position
  80. std::vector<ObjectRect> & getObjects(const int3 & tile);
  81. std::vector<ObjectRect> & getObjects(int x, int y, int z);
  82. //returns set of tiles to draw
  83. std::set<int3> removeObject(const CGObjectInstance * object);
  84. std::set<int3> addObject(const CGObjectInstance * object);
  85. /// draws all objects on current tile (higher-level logic, unlike other draw*** methods)
  86. void drawObjects(QPainter & painter, int x, int y, int z, const std::set<const CGObjectInstance *> & locked);
  87. void drawObjectAt(QPainter & painter, const CGObjectInstance * object, int x, int y);
  88. void drawMinimapTile(QPainter & painter, int x, int y, int z);
  89. static bool compareObjectBlitOrder(const CGObjectInstance * a, const CGObjectInstance * b);
  90. };