mapHandler.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. SDL_Surface * terbitmap; //bitmap of terrain
  73. std::vector<TerrainTileObject> objects; //pointers to objects being on this tile with rects to be easier to blit this tile on screen
  74. TerrainTile2();
  75. };
  76. struct MapDrawingInfo
  77. {
  78. bool scaled;
  79. int3 &topTile; // top-left tile in viewport [in tiles]
  80. const std::vector< std::vector< std::vector<ui8> > > * visibilityMap;
  81. SDL_Rect * drawBounds; // map rect drawing bounds on screen
  82. std::shared_ptr<CAnimation> icons; // holds overlay icons for world view mode
  83. float scale; // map scale for world view mode (only if scaled == true)
  84. bool otherheroAnim;
  85. ui8 anim;
  86. ui8 heroAnim;
  87. int3 movement; // used for smooth map movement
  88. bool puzzleMode;
  89. int3 grailPos; // location of grail for puzzle mode [in tiles]
  90. const std::vector<ObjectPosInfo> * additionalIcons;
  91. bool showAllTerrain; //for expert viewEarth
  92. MapDrawingInfo(int3 &topTile_, const std::vector< std::vector< std::vector<ui8> > > * visibilityMap_, SDL_Rect * drawBounds_, std::shared_ptr<CAnimation> icons_ = nullptr)
  93. : scaled(false),
  94. topTile(topTile_),
  95. visibilityMap(visibilityMap_),
  96. drawBounds(drawBounds_),
  97. icons(icons_),
  98. scale(1.0f),
  99. otherheroAnim(false),
  100. anim(0u),
  101. heroAnim(0u),
  102. movement(int3()),
  103. puzzleMode(false),
  104. grailPos(int3()),
  105. additionalIcons(nullptr),
  106. showAllTerrain(false)
  107. {}
  108. ui8 getHeroAnim() const { return otherheroAnim ? anim : heroAnim; }
  109. };
  110. template <typename T> class PseudoV
  111. {
  112. public:
  113. PseudoV() : offset(0) { }
  114. PseudoV(std::vector<T> &src, int rest, int before, int after, const T& fill) : offset(before)
  115. {
  116. inver.resize(before + rest + after);
  117. for(int i=0; i<before;i++)
  118. inver[i] = fill;
  119. for(int i=0;i<src.size();i++)
  120. inver[offset+i] = src[i];
  121. for(int i=src.size(); i<src.size()+after;i++)
  122. inver[offset+i] = fill;
  123. }
  124. inline T & operator[](const int & n)
  125. {
  126. return inver[n+offset];
  127. }
  128. inline const T & operator[](const int & n) const
  129. {
  130. return inver[n+offset];
  131. }
  132. void resize(int rest, int before, int after)
  133. {
  134. inver.resize(before + rest + after);
  135. offset=before;
  136. }
  137. int size() const
  138. {
  139. return inver.size();
  140. }
  141. private:
  142. int offset;
  143. std::vector<T> inver;
  144. };
  145. class CMapHandler
  146. {
  147. enum class EMapCacheType
  148. {
  149. TERRAIN, TERRAIN_CUSTOM, OBJECTS, ROADS, RIVERS, FOW, HEROES, HERO_FLAGS, FRAME
  150. };
  151. /// temporarily caches rescaled sdl surfaces for map world view redrawing
  152. class CMapCache
  153. {
  154. std::map<EMapCacheType, std::map<intptr_t, SDL_Surface *>> data;
  155. float worldViewCachedScale;
  156. public:
  157. /// destroys all cached data (frees surfaces)
  158. void discardWorldViewCache();
  159. /// updates scale and determines if currently cached data is still valid
  160. void updateWorldViewScale(float scale);
  161. void removeFromWorldViewCache(EMapCacheType type, intptr_t key);
  162. /// asks for cached data; @returns cached surface or nullptr if data is not in cache
  163. SDL_Surface * requestWorldViewCache(EMapCacheType type, intptr_t key);
  164. /// asks for cached data; @returns cached data if found, new scaled surface otherwise
  165. SDL_Surface * requestWorldViewCacheOrCreate(EMapCacheType type, intptr_t key, SDL_Surface * fullSurface, float scale);
  166. SDL_Surface * requestWorldViewCacheOrCreate(EMapCacheType type, intptr_t key, const IImage * fullSurface, float scale);
  167. SDL_Surface * cacheWorldViewEntry(EMapCacheType type, intptr_t key, SDL_Surface * entry);
  168. intptr_t genKey(intptr_t realPtr, ui8 mod);
  169. };
  170. /// helper struct to pass around resolved bitmaps of an object; surfaces can be nullptr if object doesn't have bitmap of that type
  171. struct AnimBitmapHolder
  172. {
  173. IImage * objBitmap; // main object bitmap
  174. IImage * flagBitmap; // flag bitmap for the object (probably only for heroes and boats with heroes)
  175. bool isMoving; // indicates if the object is moving (again, heroes/boats only)
  176. AnimBitmapHolder(IImage * objBitmap_ = nullptr, IImage * flagBitmap_ = nullptr, bool moving = false)
  177. : objBitmap(objBitmap_),
  178. flagBitmap(flagBitmap_),
  179. isMoving(moving)
  180. {}
  181. };
  182. class CMapBlitter
  183. {
  184. protected:
  185. const int FRAMES_PER_MOVE_ANIM_GROUP = 8;
  186. CMapHandler * parent; // ptr to enclosing map handler; generally for legacy reasons, probably could/should be refactored out of here
  187. int tileSize; // size of a tile drawn on map [in pixels]
  188. int halfTileSizeCeil; // half of the tile size, rounded up
  189. int3 tileCount; // number of tiles in current viewport
  190. int3 topTile; // top-left tile of the viewport
  191. int3 initPos; // starting drawing position [in pixels]
  192. int3 pos; // current position [in tiles]
  193. int3 realPos; // current position [in pixels]
  194. Rect realTileRect; // default rect based on current pos: [realPos.x, realPos.y, tileSize, tileSize]
  195. Rect defaultTileRect; // default rect based on 0: [0, 0, tileSize, tileSize]
  196. const MapDrawingInfo * info; // data for drawing passed from outside
  197. /// general drawing method, called internally by more specialized ones
  198. virtual void drawElement(EMapCacheType cacheType, SDL_Surface * sourceSurf, SDL_Rect * sourceRect,
  199. SDL_Surface * targetSurf, SDL_Rect * destRect, bool alphaBlit = false, ui8 rotationInfo = 0u) const = 0;
  200. //todo: support rotation
  201. virtual void drawElement(EMapCacheType cacheType, const IImage * source, SDL_Rect * sourceRect, SDL_Surface * targetSurf, SDL_Rect * destRect) const = 0;
  202. // first drawing pass
  203. /// draws terrain bitmap (or custom bitmap if applicable) on current tile
  204. virtual void drawTileTerrain(SDL_Surface * targetSurf, const TerrainTile & tinfo, const TerrainTile2 & tile) const;
  205. /// draws a river segment on current tile
  206. virtual void drawRiver(SDL_Surface * targetSurf, const TerrainTile & tinfo) const;
  207. /// draws a road segment on current tile
  208. virtual void drawRoad(SDL_Surface * targetSurf, const TerrainTile & tinfo, const TerrainTile * tinfoUpper) const;
  209. /// draws all objects on current tile (higher-level logic, unlike other draw*** methods)
  210. virtual void drawObjects(SDL_Surface * targetSurf, const TerrainTile2 & tile) const;
  211. virtual void drawObject(SDL_Surface * targetSurf, const IImage * source, SDL_Rect * sourceRect, bool moving) const;
  212. virtual void drawHeroFlag(SDL_Surface * targetSurf, const IImage * source, SDL_Rect * sourceRect, SDL_Rect * destRect, bool moving) const;
  213. // second drawing pass
  214. /// current tile: draws overlay over the map, used to draw world view icons
  215. virtual void drawTileOverlay(SDL_Surface * targetSurf, const TerrainTile2 & tile) const = 0;
  216. /// draws fog of war on current tile
  217. virtual void drawFow(SDL_Surface * targetSurf) const;
  218. /// draws map border frame on current position
  219. virtual void drawFrame(SDL_Surface * targetSurf) const;
  220. /// draws additional icons (for VIEW_AIR, VIEW_EARTH spells atm)
  221. virtual void drawOverlayEx(SDL_Surface * targetSurf);
  222. // third drawing pass
  223. /// custom post-processing, if needed (used by puzzle view)
  224. virtual void postProcessing(SDL_Surface * targetSurf) const {}
  225. // misc methods
  226. /// initializes frame-drawing (called at the start of every redraw)
  227. virtual void init(const MapDrawingInfo * drawingInfo) = 0;
  228. /// calculates clip region for map viewport
  229. virtual SDL_Rect clip(SDL_Surface * targetSurf) const = 0;
  230. virtual ui8 getHeroFrameGroup(ui8 dir, bool isMoving) const;
  231. ///returns appropriate bitmap and info if alpha blitting is necessary
  232. virtual std::pair<SDL_Surface *, bool> getVisBitmap() const;
  233. virtual ui8 getPhaseShift(const CGObjectInstance *object) const;
  234. virtual bool canDrawObject(const CGObjectInstance * obj) const;
  235. virtual bool canDrawCurrentTile() const;
  236. // internal helper methods to choose correct bitmap(s) for object; called internally by findObjectBitmap
  237. AnimBitmapHolder findHeroBitmap(const CGHeroInstance * hero, int anim) const;
  238. AnimBitmapHolder findBoatBitmap(const CGBoat * hero, int anim) const;
  239. IImage * findFlagBitmap(const CGHeroInstance * obj, int anim, const PlayerColor * color, int group) const;
  240. IImage * findHeroFlagBitmap(const CGHeroInstance * obj, int anim, const PlayerColor * color, int group) const;
  241. IImage * findBoatFlagBitmap(const CGBoat * obj, int anim, const PlayerColor * color, int group, ui8 dir) const;
  242. IImage * findFlagBitmapInternal(std::shared_ptr<CAnimation> animation, int anim, int group, ui8 dir, bool moving) const;
  243. int findAnimIndexByGroup(const CDefEssential * def, int groupNum) const; //deprecated
  244. public:
  245. CMapBlitter(CMapHandler * p) : parent(p) {}
  246. virtual ~CMapBlitter(){}
  247. void blit(SDL_Surface * targetSurf, const MapDrawingInfo * info);
  248. /// helper method that chooses correct bitmap(s) for given object
  249. AnimBitmapHolder findObjectBitmap(const CGObjectInstance * obj, int anim) const;
  250. };
  251. class CMapNormalBlitter : public CMapBlitter
  252. {
  253. protected:
  254. void drawElement(EMapCacheType cacheType, SDL_Surface * sourceSurf, SDL_Rect * sourceRect,
  255. SDL_Surface * targetSurf, SDL_Rect * destRect, bool alphaBlit = false, ui8 rotationInfo = 0u) const override;
  256. void drawElement(EMapCacheType cacheType, const IImage * source, SDL_Rect * sourceRect, SDL_Surface * targetSurf, SDL_Rect * destRect) const override;
  257. void drawTileOverlay(SDL_Surface * targetSurf,const TerrainTile2 & tile) const override {}
  258. void init(const MapDrawingInfo * info) override;
  259. SDL_Rect clip(SDL_Surface * targetSurf) const override;
  260. public:
  261. CMapNormalBlitter(CMapHandler * parent);
  262. virtual ~CMapNormalBlitter(){}
  263. };
  264. class CMapWorldViewBlitter : public CMapBlitter
  265. {
  266. private:
  267. IImage * objectToIcon(Obj id, si32 subId, PlayerColor owner) const;
  268. protected:
  269. void drawElement(EMapCacheType cacheType, SDL_Surface * sourceSurf, SDL_Rect * sourceRect,
  270. SDL_Surface * targetSurf, SDL_Rect * destRect, bool alphaBlit = false, ui8 rotationInfo = 0u) const override;
  271. void drawElement(EMapCacheType cacheType, const IImage * source, SDL_Rect * sourceRect, SDL_Surface * targetSurf, SDL_Rect * destRect) const override;
  272. void drawTileOverlay(SDL_Surface * targetSurf, const TerrainTile2 & tile) const override;
  273. void drawHeroFlag(SDL_Surface * targetSurf, const IImage * source, SDL_Rect * sourceRect, SDL_Rect * destRect, bool moving) const override;
  274. void drawObject(SDL_Surface * targetSurf, const IImage * source, SDL_Rect * sourceRect, bool moving) const override;
  275. void drawFrame(SDL_Surface * targetSurf) const override {}
  276. void drawOverlayEx(SDL_Surface * targetSurf) override;
  277. void init(const MapDrawingInfo * info) override;
  278. SDL_Rect clip(SDL_Surface * targetSurf) const override;
  279. ui8 getPhaseShift(const CGObjectInstance *object) const override { return 0u; }
  280. void drawScaledRotatedElement(EMapCacheType type, SDL_Surface * baseSurf, SDL_Surface * targetSurf, ui8 rotation,
  281. float scale, SDL_Rect * dstRect, SDL_Rect * srcRect = nullptr) const;
  282. void calculateWorldViewCameraPos();
  283. public:
  284. CMapWorldViewBlitter(CMapHandler * parent);
  285. virtual ~CMapWorldViewBlitter(){}
  286. };
  287. class CMapPuzzleViewBlitter : public CMapNormalBlitter
  288. {
  289. std::vector<int> unblittableObjects;
  290. void drawObjects(SDL_Surface * targetSurf, const TerrainTile2 & tile) const override;
  291. void drawFow(SDL_Surface * targetSurf) const override {} // skipping FoW in puzzle view
  292. void postProcessing(SDL_Surface * targetSurf) const override;
  293. bool canDrawObject(const CGObjectInstance * obj) const override;
  294. bool canDrawCurrentTile() const override { return true; }
  295. public:
  296. CMapPuzzleViewBlitter(CMapHandler * parent);
  297. };
  298. CMapCache cache;
  299. CMapBlitter * normalBlitter;
  300. CMapBlitter * worldViewBlitter;
  301. CMapBlitter * puzzleViewBlitter;
  302. std::map<int, std::pair<int3, CFadeAnimation*>> fadeAnims;
  303. int fadeAnimCounter;
  304. CMapBlitter * resolveBlitter(const MapDrawingInfo * info) const;
  305. bool updateObjectsFade();
  306. bool startObjectFade(TerrainTileObject & obj, bool in, int3 pos);
  307. public:
  308. PseudoV< PseudoV< PseudoV<TerrainTile2> > > ttiles; //informations about map tiles
  309. int3 sizes; //map size (x = width, y = height, z = number of levels)
  310. const CMap * map;
  311. // Max number of tiles that will fit in the map screen. Tiles
  312. // can be partial on each edges.
  313. int tilesW;
  314. int tilesH;
  315. // size of each side of the frame around the whole map, in tiles
  316. int frameH;
  317. int frameW;
  318. // Coord in pixels of the top left corner of the top left tile to
  319. // draw. Values range is [-31..0]. A negative value
  320. // implies that part of the tile won't be displayed.
  321. int offsetX;
  322. int offsetY;
  323. //std::set<int> usedHeroes;
  324. std::vector<std::vector<SDL_Surface *> > terrainGraphics; // [terrain id] [view type] [rotation type]
  325. std::vector<CDefEssential *> roadDefs;
  326. std::vector<CDefEssential *> staticRiverDefs;
  327. std::vector<std::vector<std::vector<ui8> > > hideBitmap; //specifies number of graphic that should be used to fully hide a tile
  328. mutable std::map<const CGObjectInstance*, ui8> animationPhase;
  329. CMapHandler(); //c-tor
  330. ~CMapHandler(); //d-tor
  331. void getTerrainDescr(const int3 &pos, std::string & out, bool terName); //if tername == false => empty string when tile is clear
  332. CGObjectInstance * createObject(int id, int subid, int3 pos, int owner=254); //creates a new object with a certain id and subid
  333. bool printObject(const CGObjectInstance * obj, bool fadein = false); //puts appropriate things to tiles, so obj will be visible on map
  334. 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)
  335. bool removeObject(CGObjectInstance * obj, bool fadeout = false); //removes object from each place in VCMI (I hope)
  336. void init();
  337. void calculateBlockedPos();
  338. void initObjectRects();
  339. void borderAndTerrainBitmapInit();
  340. void roadsRiverTerrainInit();
  341. void prepareFOWDefs();
  342. EMapAnimRedrawStatus drawTerrainRectNew(SDL_Surface * targetSurface, const MapDrawingInfo * info, bool redrawOnlyAnim = false);
  343. void updateWater();
  344. void validateRectTerr(SDL_Rect * val, const SDL_Rect * ext); //terrainRect helper
  345. static ui8 getDir(const int3 & a, const int3 & b); //returns direction number in range 0 - 7 (0 is left top, clockwise) [direction: form a to b]
  346. /// determines if the map is ready to handle new hero movement (not available during fading animations)
  347. bool canStartHeroMovement();
  348. void discardWorldViewCache();
  349. static bool compareObjectBlitOrder(const CGObjectInstance * a, const CGObjectInstance * b);
  350. };