mapHandler.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 * cacheWorldViewEntry(EMapCacheType type, intptr_t key, SDL_Surface * entry);
  167. intptr_t genKey(intptr_t realPtr, ui8 mod);
  168. };
  169. /// helper struct to pass around resolved bitmaps of an object; surfaces can be nullptr if object doesn't have bitmap of that type
  170. struct AnimBitmapHolder
  171. {
  172. SDL_Surface * objBitmap; // main object bitmap
  173. SDL_Surface * flagBitmap; // flag bitmap for the object (probably only for heroes and boats with heroes)
  174. bool isMoving; // indicates if the object is moving (again, heroes/boats only)
  175. AnimBitmapHolder(SDL_Surface * objBitmap_ = nullptr, SDL_Surface * flagBitmap_ = nullptr, bool moving = false)
  176. : objBitmap(objBitmap_),
  177. flagBitmap(flagBitmap_),
  178. isMoving(moving)
  179. {}
  180. };
  181. class CMapBlitter
  182. {
  183. protected:
  184. const int FRAMES_PER_MOVE_ANIM_GROUP = 8;
  185. CMapHandler * parent; // ptr to enclosing map handler; generally for legacy reasons, probably could/should be refactored out of here
  186. int tileSize; // size of a tile drawn on map [in pixels]
  187. int halfTileSizeCeil; // half of the tile size, rounded up
  188. int3 tileCount; // number of tiles in current viewport
  189. int3 topTile; // top-left tile of the viewport
  190. int3 initPos; // starting drawing position [in pixels]
  191. int3 pos; // current position [in tiles]
  192. int3 realPos; // current position [in pixels]
  193. Rect realTileRect; // default rect based on current pos: [realPos.x, realPos.y, tileSize, tileSize]
  194. Rect defaultTileRect; // default rect based on 0: [0, 0, tileSize, tileSize]
  195. const MapDrawingInfo * info; // data for drawing passed from outside
  196. /// general drawing method, called internally by more specialized ones
  197. virtual void drawElement(EMapCacheType cacheType, SDL_Surface * sourceSurf, SDL_Rect * sourceRect,
  198. SDL_Surface * targetSurf, SDL_Rect * destRect, bool alphaBlit = false, ui8 rotationInfo = 0u) const = 0;
  199. // first drawing pass
  200. /// draws terrain bitmap (or custom bitmap if applicable) on current tile
  201. virtual void drawTileTerrain(SDL_Surface * targetSurf, const TerrainTile & tinfo, const TerrainTile2 & tile) const;
  202. /// draws a river segment on current tile
  203. virtual void drawRiver(SDL_Surface * targetSurf, const TerrainTile & tinfo) const;
  204. /// draws a road segment on current tile
  205. virtual void drawRoad(SDL_Surface * targetSurf, const TerrainTile & tinfo, const TerrainTile * tinfoUpper) const;
  206. /// draws all objects on current tile (higher-level logic, unlike other draw*** methods)
  207. virtual void drawObjects(SDL_Surface * targetSurf, const TerrainTile2 & tile) const;
  208. virtual void drawObject(SDL_Surface * targetSurf, SDL_Surface * sourceSurf, SDL_Rect * sourceRect, bool moving) const;
  209. virtual void drawHeroFlag(SDL_Surface * targetSurf, SDL_Surface * sourceSurf, SDL_Rect * sourceRect, SDL_Rect * destRect, bool moving) const;
  210. // second drawing pass
  211. /// current tile: draws overlay over the map, used to draw world view icons
  212. virtual void drawTileOverlay(SDL_Surface * targetSurf, const TerrainTile2 & tile) const = 0;
  213. /// draws fog of war on current tile
  214. virtual void drawFow(SDL_Surface * targetSurf) const;
  215. /// draws map border frame on current position
  216. virtual void drawFrame(SDL_Surface * targetSurf) const;
  217. /// draws additional icons (for VIEW_AIR, VIEW_EARTH spells atm)
  218. virtual void drawOverlayEx(SDL_Surface * targetSurf);
  219. // third drawing pass
  220. /// custom post-processing, if needed (used by puzzle view)
  221. virtual void postProcessing(SDL_Surface * targetSurf) const {}
  222. // misc methods
  223. /// initializes frame-drawing (called at the start of every redraw)
  224. virtual void init(const MapDrawingInfo * drawingInfo) = 0;
  225. /// calculates clip region for map viewport
  226. virtual SDL_Rect clip(SDL_Surface * targetSurf) const = 0;
  227. virtual ui8 getHeroFrameNum(ui8 dir, bool isMoving) const;
  228. ///returns appropriate bitmap and info if alpha blitting is necessary
  229. virtual std::pair<SDL_Surface *, bool> getVisBitmap() const;
  230. virtual ui8 getPhaseShift(const CGObjectInstance *object) const;
  231. virtual bool canDrawObject(const CGObjectInstance * obj) const;
  232. virtual bool canDrawCurrentTile() const;
  233. // internal helper methods to choose correct bitmap(s) for object; called internally by findObjectBitmap
  234. AnimBitmapHolder findHeroBitmap(const CGHeroInstance * hero, int anim) const;
  235. AnimBitmapHolder findBoatBitmap(const CGBoat * hero, int anim) const;
  236. SDL_Surface * findFlagBitmap(const CGHeroInstance * obj, int anim, const PlayerColor * color, int indexOffset) const;
  237. SDL_Surface * findHeroFlagBitmap(const CGHeroInstance * obj, int anim, const PlayerColor * color, int indexOffset) const;
  238. SDL_Surface * findBoatFlagBitmap(const CGBoat * obj, int anim, const PlayerColor * color, int indexOffset, ui8 dir) const;
  239. SDL_Surface * findFlagBitmapInternal(const CDefEssential * def, int anim, int indexOffset, ui8 dir, bool moving) const;
  240. int findAnimIndexByGroup(const CDefEssential * def, int groupNum) const;
  241. public:
  242. CMapBlitter(CMapHandler * p) : parent(p) {}
  243. virtual ~CMapBlitter(){}
  244. void blit(SDL_Surface * targetSurf, const MapDrawingInfo * info);
  245. /// helper method that chooses correct bitmap(s) for given object
  246. AnimBitmapHolder findObjectBitmap(const CGObjectInstance * obj, int anim) const;
  247. };
  248. class CMapNormalBlitter : public CMapBlitter
  249. {
  250. protected:
  251. void drawElement(EMapCacheType cacheType, SDL_Surface * sourceSurf, SDL_Rect * sourceRect,
  252. SDL_Surface * targetSurf, SDL_Rect * destRect, bool alphaBlit = false, ui8 rotationInfo = 0u) const override;
  253. void drawTileOverlay(SDL_Surface * targetSurf,const TerrainTile2 & tile) const override {}
  254. void init(const MapDrawingInfo * info) override;
  255. SDL_Rect clip(SDL_Surface * targetSurf) const override;
  256. public:
  257. CMapNormalBlitter(CMapHandler * parent);
  258. virtual ~CMapNormalBlitter(){}
  259. };
  260. class CMapWorldViewBlitter : public CMapBlitter
  261. {
  262. private:
  263. IImage * objectToIcon(Obj id, si32 subId, PlayerColor owner) const;
  264. protected:
  265. void drawElement(EMapCacheType cacheType, SDL_Surface * sourceSurf, SDL_Rect * sourceRect,
  266. SDL_Surface * targetSurf, SDL_Rect * destRect, bool alphaBlit = false, ui8 rotationInfo = 0u) const override;
  267. void drawTileOverlay(SDL_Surface * targetSurf, const TerrainTile2 & tile) const override;
  268. void drawHeroFlag(SDL_Surface * targetSurf, SDL_Surface * sourceSurf, SDL_Rect * sourceRect, SDL_Rect * destRect, bool moving) const override;
  269. void drawObject(SDL_Surface * targetSurf, SDL_Surface * sourceSurf, SDL_Rect * sourceRect, bool moving) const override;
  270. void drawFrame(SDL_Surface * targetSurf) const override {}
  271. void drawOverlayEx(SDL_Surface * targetSurf) override;
  272. void init(const MapDrawingInfo * info) override;
  273. SDL_Rect clip(SDL_Surface * targetSurf) const override;
  274. // ui8 getHeroFrameNum(ui8 dir, bool isMoving) const override { return 0u; }
  275. ui8 getPhaseShift(const CGObjectInstance *object) const override { return 0u; }
  276. void drawScaledRotatedElement(EMapCacheType type, SDL_Surface * baseSurf, SDL_Surface * targetSurf, ui8 rotation,
  277. float scale, SDL_Rect * dstRect, SDL_Rect * srcRect = nullptr) const;
  278. void calculateWorldViewCameraPos();
  279. public:
  280. CMapWorldViewBlitter(CMapHandler * parent);
  281. virtual ~CMapWorldViewBlitter(){}
  282. };
  283. class CMapPuzzleViewBlitter : public CMapNormalBlitter
  284. {
  285. std::vector<int> unblittableObjects;
  286. void drawObjects(SDL_Surface * targetSurf, const TerrainTile2 & tile) const override;
  287. void drawFow(SDL_Surface * targetSurf) const override {} // skipping FoW in puzzle view
  288. void postProcessing(SDL_Surface * targetSurf) const override;
  289. bool canDrawObject(const CGObjectInstance * obj) const override;
  290. bool canDrawCurrentTile() const override { return true; }
  291. public:
  292. CMapPuzzleViewBlitter(CMapHandler * parent);
  293. };
  294. CMapCache cache;
  295. CMapBlitter * normalBlitter;
  296. CMapBlitter * worldViewBlitter;
  297. CMapBlitter * puzzleViewBlitter;
  298. std::map<int, std::pair<int3, CFadeAnimation*>> fadeAnims;
  299. int fadeAnimCounter;
  300. CMapBlitter * resolveBlitter(const MapDrawingInfo * info) const;
  301. bool updateObjectsFade();
  302. bool startObjectFade(TerrainTileObject & obj, bool in, int3 pos);
  303. public:
  304. PseudoV< PseudoV< PseudoV<TerrainTile2> > > ttiles; //informations about map tiles
  305. int3 sizes; //map size (x = width, y = height, z = number of levels)
  306. const CMap * map;
  307. // Max number of tiles that will fit in the map screen. Tiles
  308. // can be partial on each edges.
  309. int tilesW;
  310. int tilesH;
  311. // size of each side of the frame around the whole map, in tiles
  312. int frameH;
  313. int frameW;
  314. // Coord in pixels of the top left corner of the top left tile to
  315. // draw. Values range is [-31..0]. A negative value
  316. // implies that part of the tile won't be displayed.
  317. int offsetX;
  318. int offsetY;
  319. //std::set<int> usedHeroes;
  320. std::vector<std::vector<SDL_Surface *> > terrainGraphics; // [terrain id] [view type] [rotation type]
  321. std::vector<CDefEssential *> roadDefs;
  322. std::vector<CDefEssential *> staticRiverDefs;
  323. std::vector<std::vector<std::vector<ui8> > > hideBitmap; //specifies number of graphic that should be used to fully hide a tile
  324. mutable std::map<const CGObjectInstance*, ui8> animationPhase;
  325. CMapHandler(); //c-tor
  326. ~CMapHandler(); //d-tor
  327. void getTerrainDescr(const int3 &pos, std::string & out, bool terName); //if tername == false => empty string when tile is clear
  328. CGObjectInstance * createObject(int id, int subid, int3 pos, int owner=254); //creates a new object with a certain id and subid
  329. bool printObject(const CGObjectInstance * obj, bool fadein = false); //puts appropriate things to tiles, so obj will be visible on map
  330. 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)
  331. bool removeObject(CGObjectInstance * obj, bool fadeout = false); //removes object from each place in VCMI (I hope)
  332. void init();
  333. void calculateBlockedPos();
  334. void initObjectRects();
  335. void borderAndTerrainBitmapInit();
  336. void roadsRiverTerrainInit();
  337. void prepareFOWDefs();
  338. EMapAnimRedrawStatus drawTerrainRectNew(SDL_Surface * targetSurface, const MapDrawingInfo * info, bool redrawOnlyAnim = false);
  339. void updateWater();
  340. void validateRectTerr(SDL_Rect * val, const SDL_Rect * ext); //terrainRect helper
  341. 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]
  342. /// determines if the map is ready to handle new hero movement (not available during fading animations)
  343. bool canStartHeroMovement();
  344. void discardWorldViewCache();
  345. static bool compareObjectBlitOrder(const CGObjectInstance * a, const CGObjectInstance * b);
  346. };