mapHandler.h 15 KB

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