mapHandler.h 15 KB

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