mapHandler.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. #pragma once
  2. #include "../lib/int3.h"
  3. #include "../lib/spells/ViewSpellInt.h"
  4. #include "gui/Geometries.h"
  5. #include "SDL.h"
  6. /*
  7. * mapHandler.h, part of VCMI engine
  8. *
  9. * Authors: listed in file AUTHORS in main folder
  10. *
  11. * License: GNU General Public License v2.0 or later
  12. * Full text of license available in license.txt file, in main folder
  13. *
  14. */
  15. class CGObjectInstance;
  16. class CGHeroInstance;
  17. class CGBoat;
  18. class CMap;
  19. class CGDefInfo;
  20. class CDefHandler;
  21. struct TerrainTile;
  22. struct SDL_Surface;
  23. struct SDL_Rect;
  24. class CDefEssential;
  25. class CAnimation;
  26. class IImage;
  27. class CFadeAnimation;
  28. class PlayerColor;
  29. enum class EWorldViewIcon
  30. {
  31. TOWN = 0,
  32. HERO,
  33. ARTIFACT,
  34. TELEPORT,
  35. GATE,
  36. MINE_WOOD,
  37. MINE_MERCURY,
  38. MINE_STONE,
  39. MINE_SULFUR,
  40. MINE_CRYSTAL,
  41. MINE_GEM,
  42. MINE_GOLD,
  43. RES_WOOD,
  44. RES_MERCURY,
  45. RES_STONE,
  46. RES_SULFUR,
  47. RES_CRYSTAL,
  48. RES_GEM,
  49. RES_GOLD,
  50. };
  51. enum class EMapObjectFadingType
  52. {
  53. NONE,
  54. IN,
  55. OUT
  56. };
  57. enum class EMapAnimRedrawStatus
  58. {
  59. OK,
  60. REDRAW_REQUESTED // map blitter requests quick redraw due to current animation
  61. };
  62. struct TerrainTileObject
  63. {
  64. const CGObjectInstance *obj;
  65. SDL_Rect rect;
  66. int fadeAnimKey;
  67. TerrainTileObject(const CGObjectInstance *obj_, SDL_Rect rect_);
  68. ~TerrainTileObject();
  69. };
  70. struct TerrainTile2
  71. {
  72. std::vector<TerrainTileObject> objects; //pointers to objects being on this tile with rects to be easier to blit this tile on screen
  73. };
  74. struct MapDrawingInfo
  75. {
  76. bool scaled;
  77. int3 &topTile; // top-left tile in viewport [in tiles]
  78. const std::vector< std::vector< std::vector<ui8> > > * visibilityMap;
  79. SDL_Rect * drawBounds; // map rect drawing bounds on screen
  80. std::shared_ptr<CAnimation> icons; // holds overlay icons for world view mode
  81. float scale; // map scale for world view mode (only if scaled == true)
  82. bool otherheroAnim;
  83. ui8 anim;
  84. ui8 heroAnim;
  85. int3 movement; // used for smooth map movement
  86. bool puzzleMode;
  87. int3 grailPos; // location of grail for puzzle mode [in tiles]
  88. const std::vector<ObjectPosInfo> * additionalIcons;
  89. bool showAllTerrain; //for expert viewEarth
  90. MapDrawingInfo(int3 &topTile_, const std::vector< std::vector< std::vector<ui8> > > * visibilityMap_, SDL_Rect * drawBounds_, std::shared_ptr<CAnimation> icons_ = nullptr)
  91. : scaled(false),
  92. topTile(topTile_),
  93. visibilityMap(visibilityMap_),
  94. drawBounds(drawBounds_),
  95. icons(icons_),
  96. scale(1.0f),
  97. otherheroAnim(false),
  98. anim(0u),
  99. heroAnim(0u),
  100. movement(int3()),
  101. puzzleMode(false),
  102. grailPos(int3()),
  103. additionalIcons(nullptr),
  104. showAllTerrain(false)
  105. {}
  106. ui8 getHeroAnim() const { return otherheroAnim ? anim : heroAnim; }
  107. };
  108. template <typename T> class PseudoV
  109. {
  110. public:
  111. PseudoV() : offset(0) { }
  112. inline T & operator[](const int & n)
  113. {
  114. return inver[n+offset];
  115. }
  116. inline const T & operator[](const int & n) const
  117. {
  118. return inver[n+offset];
  119. }
  120. void resize(int rest, int before, int after)
  121. {
  122. inver.resize(before + rest + after);
  123. offset=before;
  124. }
  125. int size() const
  126. {
  127. return inver.size();
  128. }
  129. private:
  130. int offset;
  131. std::vector<T> inver;
  132. };
  133. class CMapHandler
  134. {
  135. enum class EMapCacheType : ui8
  136. {
  137. TERRAIN, OBJECTS, ROADS, RIVERS, FOW, HEROES, HERO_FLAGS, FRAME, AFTER_LAST
  138. };
  139. /// temporarily caches rescaled sdl surfaces for map world view redrawing
  140. class CMapCache
  141. {
  142. std::array< std::map<intptr_t, SDL_Surface *>, (ui8)EMapCacheType::AFTER_LAST> data;
  143. float worldViewCachedScale;
  144. public:
  145. /// destroys all cached data (frees surfaces)
  146. void discardWorldViewCache();
  147. /// updates scale and determines if currently cached data is still valid
  148. void updateWorldViewScale(float scale);
  149. /// asks for cached data; @returns cached surface or nullptr if data is not in cache
  150. SDL_Surface * requestWorldViewCache(EMapCacheType type, intptr_t key);
  151. /// asks for cached data; @returns cached data if found, new scaled surface otherwise
  152. SDL_Surface * requestWorldViewCacheOrCreate(EMapCacheType type, intptr_t key, SDL_Surface * fullSurface, float scale);
  153. SDL_Surface * requestWorldViewCacheOrCreate(EMapCacheType type, intptr_t key, const IImage * fullSurface, float scale);
  154. SDL_Surface * cacheWorldViewEntry(EMapCacheType type, intptr_t key, SDL_Surface * entry);
  155. intptr_t genKey(intptr_t realPtr, ui8 mod);
  156. };
  157. /// helper struct to pass around resolved bitmaps of an object; surfaces can be nullptr if object doesn't have bitmap of that type
  158. struct AnimBitmapHolder
  159. {
  160. IImage * objBitmap; // main object bitmap
  161. IImage * flagBitmap; // flag bitmap for the object (probably only for heroes and boats with heroes)
  162. bool isMoving; // indicates if the object is moving (again, heroes/boats only)
  163. AnimBitmapHolder(IImage * objBitmap_ = nullptr, IImage * flagBitmap_ = nullptr, bool moving = false)
  164. : objBitmap(objBitmap_),
  165. flagBitmap(flagBitmap_),
  166. isMoving(moving)
  167. {}
  168. };
  169. class CMapBlitter
  170. {
  171. protected:
  172. const int FRAMES_PER_MOVE_ANIM_GROUP = 8;
  173. CMapHandler * parent; // ptr to enclosing map handler; generally for legacy reasons, probably could/should be refactored out of here
  174. int tileSize; // size of a tile drawn on map [in pixels]
  175. int halfTileSizeCeil; // half of the tile size, rounded up
  176. int3 tileCount; // number of tiles in current viewport
  177. int3 topTile; // top-left tile of the viewport
  178. int3 initPos; // starting drawing position [in pixels]
  179. int3 pos; // current position [in tiles]
  180. int3 realPos; // current position [in pixels]
  181. Rect realTileRect; // default rect based on current pos: [realPos.x, realPos.y, tileSize, tileSize]
  182. Rect defaultTileRect; // default rect based on 0: [0, 0, tileSize, tileSize]
  183. const MapDrawingInfo * info; // data for drawing passed from outside
  184. /// general drawing method, called internally by more specialized ones
  185. virtual void drawElement(EMapCacheType cacheType, const IImage * source, SDL_Rect * sourceRect, SDL_Surface * targetSurf, SDL_Rect * destRect) const = 0;
  186. // first drawing pass
  187. /// draws terrain bitmap (or custom bitmap if applicable) on current tile
  188. virtual void drawTileTerrain(SDL_Surface * targetSurf, const TerrainTile & tinfo, const TerrainTile2 & tile) const;
  189. /// draws a river segment on current tile
  190. virtual void drawRiver(SDL_Surface * targetSurf, const TerrainTile & tinfo) const;
  191. /// draws a road segment on current tile
  192. virtual void drawRoad(SDL_Surface * targetSurf, const TerrainTile & tinfo, const TerrainTile * tinfoUpper) const;
  193. /// draws all objects on current tile (higher-level logic, unlike other draw*** methods)
  194. virtual void drawObjects(SDL_Surface * targetSurf, const TerrainTile2 & tile) const;
  195. virtual void drawObject(SDL_Surface * targetSurf, const IImage * source, SDL_Rect * sourceRect, bool moving) const;
  196. virtual void drawHeroFlag(SDL_Surface * targetSurf, const IImage * source, SDL_Rect * sourceRect, SDL_Rect * destRect, bool moving) const;
  197. // second drawing pass
  198. /// current tile: draws overlay over the map, used to draw world view icons
  199. virtual void drawTileOverlay(SDL_Surface * targetSurf, const TerrainTile2 & tile) const = 0;
  200. /// draws fog of war on current tile
  201. virtual void drawFow(SDL_Surface * targetSurf) const;
  202. /// draws map border frame on current position
  203. virtual void drawFrame(SDL_Surface * targetSurf) const;
  204. /// draws additional icons (for VIEW_AIR, VIEW_EARTH spells atm)
  205. virtual void drawOverlayEx(SDL_Surface * targetSurf);
  206. // third drawing pass
  207. /// custom post-processing, if needed (used by puzzle view)
  208. virtual void postProcessing(SDL_Surface * targetSurf) const {}
  209. // misc methods
  210. /// initializes frame-drawing (called at the start of every redraw)
  211. virtual void init(const MapDrawingInfo * drawingInfo) = 0;
  212. /// calculates clip region for map viewport
  213. virtual SDL_Rect clip(SDL_Surface * targetSurf) const = 0;
  214. virtual ui8 getHeroFrameGroup(ui8 dir, bool isMoving) const;
  215. virtual ui8 getPhaseShift(const CGObjectInstance *object) const;
  216. virtual bool canDrawObject(const CGObjectInstance * obj) const;
  217. virtual bool canDrawCurrentTile() const;
  218. // internal helper methods to choose correct bitmap(s) for object; called internally by findObjectBitmap
  219. AnimBitmapHolder findHeroBitmap(const CGHeroInstance * hero, int anim) const;
  220. AnimBitmapHolder findBoatBitmap(const CGBoat * hero, int anim) const;
  221. IImage * findFlagBitmap(const CGHeroInstance * obj, int anim, const PlayerColor * color, int group) const;
  222. IImage * findHeroFlagBitmap(const CGHeroInstance * obj, int anim, const PlayerColor * color, int group) const;
  223. IImage * findBoatFlagBitmap(const CGBoat * obj, int anim, const PlayerColor * color, int group, ui8 dir) const;
  224. IImage * findFlagBitmapInternal(std::shared_ptr<CAnimation> animation, int anim, int group, ui8 dir, bool moving) const;
  225. public:
  226. CMapBlitter(CMapHandler * p) : parent(p) {}
  227. virtual ~CMapBlitter(){}
  228. void blit(SDL_Surface * targetSurf, const MapDrawingInfo * info);
  229. /// helper method that chooses correct bitmap(s) for given object
  230. AnimBitmapHolder findObjectBitmap(const CGObjectInstance * obj, int anim) const;
  231. };
  232. class CMapNormalBlitter : public CMapBlitter
  233. {
  234. protected:
  235. void drawElement(EMapCacheType cacheType, const IImage * source, SDL_Rect * sourceRect, SDL_Surface * targetSurf, SDL_Rect * destRect) const override;
  236. void drawTileOverlay(SDL_Surface * targetSurf,const TerrainTile2 & tile) const override {}
  237. void init(const MapDrawingInfo * info) override;
  238. SDL_Rect clip(SDL_Surface * targetSurf) const override;
  239. public:
  240. CMapNormalBlitter(CMapHandler * parent);
  241. virtual ~CMapNormalBlitter(){}
  242. };
  243. class CMapWorldViewBlitter : public CMapBlitter
  244. {
  245. private:
  246. IImage * objectToIcon(Obj id, si32 subId, PlayerColor owner) const;
  247. protected:
  248. void drawElement(EMapCacheType cacheType, const IImage * source, SDL_Rect * sourceRect, SDL_Surface * targetSurf, SDL_Rect * destRect) const override;
  249. void drawTileOverlay(SDL_Surface * targetSurf, const TerrainTile2 & tile) const override;
  250. void drawHeroFlag(SDL_Surface * targetSurf, const IImage * source, SDL_Rect * sourceRect, SDL_Rect * destRect, bool moving) const override;
  251. void drawObject(SDL_Surface * targetSurf, const IImage * source, SDL_Rect * sourceRect, bool moving) const override;
  252. void drawFrame(SDL_Surface * targetSurf) const override {}
  253. void drawOverlayEx(SDL_Surface * targetSurf) override;
  254. void init(const MapDrawingInfo * info) override;
  255. SDL_Rect clip(SDL_Surface * targetSurf) const override;
  256. ui8 getPhaseShift(const CGObjectInstance *object) const override { return 0u; }
  257. void calculateWorldViewCameraPos();
  258. public:
  259. CMapWorldViewBlitter(CMapHandler * parent);
  260. virtual ~CMapWorldViewBlitter(){}
  261. };
  262. class CMapPuzzleViewBlitter : public CMapNormalBlitter
  263. {
  264. std::vector<int> unblittableObjects;
  265. void drawObjects(SDL_Surface * targetSurf, const TerrainTile2 & tile) const override;
  266. void drawFow(SDL_Surface * targetSurf) const override {} // skipping FoW in puzzle view
  267. void postProcessing(SDL_Surface * targetSurf) const override;
  268. bool canDrawObject(const CGObjectInstance * obj) const override;
  269. bool canDrawCurrentTile() const override { return true; }
  270. public:
  271. CMapPuzzleViewBlitter(CMapHandler * parent);
  272. };
  273. CMapCache cache;
  274. CMapBlitter * normalBlitter;
  275. CMapBlitter * worldViewBlitter;
  276. CMapBlitter * puzzleViewBlitter;
  277. std::map<int, std::pair<int3, CFadeAnimation*>> fadeAnims;
  278. int fadeAnimCounter;
  279. CMapBlitter * resolveBlitter(const MapDrawingInfo * info) const;
  280. bool updateObjectsFade();
  281. bool startObjectFade(TerrainTileObject & obj, bool in, int3 pos);
  282. void initObjectRects();
  283. void initBorderGraphics();
  284. void initTerrainGraphics();
  285. void prepareFOWDefs();
  286. public:
  287. PseudoV< PseudoV< PseudoV<TerrainTile2> > > ttiles; //informations about map tiles
  288. int3 sizes; //map size (x = width, y = height, z = number of levels)
  289. const CMap * map;
  290. // Max number of tiles that will fit in the map screen. Tiles
  291. // can be partial on each edges.
  292. int tilesW;
  293. int tilesH;
  294. // size of each side of the frame around the whole map, in tiles
  295. int frameH;
  296. int frameW;
  297. // Coord in pixels of the top left corner of the top left tile to
  298. // draw. Values range is [-31..0]. A negative value
  299. // implies that part of the tile won't be displayed.
  300. int offsetX;
  301. int offsetY;
  302. //terrain graphics
  303. typedef std::vector<std::array<std::unique_ptr<CAnimation>, 4>> TFlippedAnimations; //[type, rotation]
  304. typedef std::vector<std::vector<std::array<IImage *, 4>>> TFlippedCache;//[type, view type, rotation]
  305. TFlippedAnimations terrainAnimations;//[terrain type, rotation]
  306. TFlippedCache terrainImages;//[terrain type, view type, rotation]
  307. TFlippedAnimations roadAnimations;//[road type, rotation]
  308. TFlippedCache roadImages;//[road type, view type, rotation]
  309. TFlippedAnimations riverAnimations;//[river type, rotation]
  310. TFlippedCache riverImages;//[river type, view type, rotation]
  311. //Fog of War cache (not owned)
  312. std::vector<const IImage *> FoWfullHide;
  313. std::vector<std::vector<std::vector<ui8> > > hideBitmap; //frame indexes (in FoWfullHide) of graphic that should be used to fully hide a tile
  314. std::vector<const IImage *> FoWpartialHide;
  315. //edge graphics
  316. std::unique_ptr<CAnimation> egdeAnimation;
  317. std::vector<const IImage *> egdeImages;//cache of links to egdeAnimation (for faster access)
  318. PseudoV< PseudoV< PseudoV <ui8> > > edgeFrames; //frame indexes (in egdeImages) of tile outside of map
  319. mutable std::map<const CGObjectInstance*, ui8> animationPhase;
  320. CMapHandler(); //c-tor
  321. ~CMapHandler(); //d-tor
  322. void getTerrainDescr(const int3 &pos, std::string & out, bool terName); //if tername == false => empty string when tile is clear
  323. bool printObject(const CGObjectInstance * obj, bool fadein = false); //puts appropriate things to tiles, so obj will be visible on map
  324. bool hideObject(const CGObjectInstance * obj, bool fadeout = false); //removes appropriate things from ttiles, so obj will be no longer visible on map (but still will exist)
  325. void init();
  326. EMapAnimRedrawStatus drawTerrainRectNew(SDL_Surface * targetSurface, const MapDrawingInfo * info, bool redrawOnlyAnim = false);
  327. void updateWater();
  328. /// determines if the map is ready to handle new hero movement (not available during fading animations)
  329. bool canStartHeroMovement();
  330. void discardWorldViewCache();
  331. static bool compareObjectBlitOrder(const CGObjectInstance * a, const CGObjectInstance * b);
  332. };