mapHandler.h 14 KB

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