maphandler.h 3.8 KB

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