mapHandler.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 CMap;
  17. class CGDefInfo;
  18. class CGObjectInstance;
  19. class CDefHandler;
  20. struct TerrainTile;
  21. struct SDL_Surface;
  22. struct SDL_Rect;
  23. class CDefEssential;
  24. enum class EWorldViewIcon
  25. {
  26. TOWN = 0,
  27. HERO,
  28. ARTIFACT,
  29. TELEPORT,
  30. GATE,
  31. MINE_WOOD,
  32. MINE_MERCURY,
  33. MINE_STONE,
  34. MINE_SULFUR,
  35. MINE_CRYSTAL,
  36. MINE_GEM,
  37. MINE_GOLD,
  38. RES_WOOD,
  39. RES_MERCURY,
  40. RES_STONE,
  41. RES_SULFUR,
  42. RES_CRYSTAL,
  43. RES_GEM,
  44. RES_GOLD,
  45. };
  46. struct TerrainTile2
  47. {
  48. SDL_Surface * terbitmap; //bitmap of terrain
  49. std::vector < std::pair<const CGObjectInstance*,SDL_Rect> > objects; //pointers to objects being on this tile with rects to be easier to blit this tile on screen
  50. TerrainTile2();
  51. };
  52. struct MapDrawingInfo
  53. {
  54. bool scaled;
  55. int3 &topTile; // top-left tile in viewport [in tiles]
  56. const std::vector< std::vector< std::vector<ui8> > > * visibilityMap;
  57. SDL_Rect * drawBounds; // map rect drawing bounds on screen
  58. CDefHandler * iconsDef; // holds overlay icons for world view mode
  59. float scale; // map scale for world view mode (only if scaled == true)
  60. bool otherheroAnim;
  61. ui8 anim;
  62. ui8 heroAnim;
  63. int3 movement; // used for smooth map movement
  64. bool puzzleMode;
  65. int3 grailPos; // location of grail for puzzle mode [in tiles]
  66. MapDrawingInfo(int3 &topTile_, const std::vector< std::vector< std::vector<ui8> > > * visibilityMap_, SDL_Rect * drawBounds_, CDefHandler * iconsDef_ = nullptr)
  67. : scaled(false),
  68. topTile(topTile_),
  69. visibilityMap(visibilityMap_),
  70. drawBounds(drawBounds_),
  71. iconsDef(iconsDef_),
  72. scale(1.0f),
  73. otherheroAnim(false),
  74. anim(0u),
  75. heroAnim(0u),
  76. movement(int3()),
  77. puzzleMode(false),
  78. grailPos(int3())
  79. {}
  80. ui8 getHeroAnim() const { return otherheroAnim ? anim : heroAnim; }
  81. };
  82. template <typename T> class PseudoV
  83. {
  84. public:
  85. PseudoV() : offset(0) { }
  86. PseudoV(std::vector<T> &src, int rest, int before, int after, const T& fill) : offset(before)
  87. {
  88. inver.resize(before + rest + after);
  89. for(int i=0; i<before;i++)
  90. inver[i] = fill;
  91. for(int i=0;i<src.size();i++)
  92. inver[offset+i] = src[i];
  93. for(int i=src.size(); i<src.size()+after;i++)
  94. inver[offset+i] = fill;
  95. }
  96. inline T & operator[](const int & n)
  97. {
  98. return inver[n+offset];
  99. }
  100. inline const T & operator[](const int & n) const
  101. {
  102. return inver[n+offset];
  103. }
  104. void resize(int rest, int before, int after)
  105. {
  106. inver.resize(before + rest + after);
  107. offset=before;
  108. }
  109. int size() const
  110. {
  111. return inver.size();
  112. }
  113. private:
  114. int offset;
  115. std::vector<T> inver;
  116. };
  117. class CMapHandler
  118. {
  119. enum class EMapCacheType
  120. {
  121. TERRAIN, TERRAIN_CUSTOM, OBJECTS, ROADS, RIVERS, FOW, HEROES, HERO_FLAGS, FRAME
  122. };
  123. /// temporarily caches rescaled sdl surfaces for map world view redrawing
  124. class CMapCache
  125. {
  126. std::map<EMapCacheType, std::map<intptr_t, SDL_Surface *>> data;
  127. float worldViewCachedScale;
  128. public:
  129. /// destroys all cached data (frees surfaces)
  130. void discardWorldViewCache();
  131. /// updates scale and determines if currently cached data is still valid
  132. void updateWorldViewScale(float scale);
  133. void removeFromWorldViewCache(EMapCacheType type, intptr_t key);
  134. /// asks for cached data; @returns cached surface or nullptr if data is not in cache
  135. SDL_Surface * requestWorldViewCache(EMapCacheType type, intptr_t key);
  136. /// asks for cached data; @returns cached data if found, new scaled surface otherwise
  137. SDL_Surface * requestWorldViewCacheOrCreate(EMapCacheType type, intptr_t key, SDL_Surface * fullSurface, float scale);
  138. SDL_Surface * cacheWorldViewEntry(EMapCacheType type, intptr_t key, SDL_Surface * entry);
  139. intptr_t genKey(intptr_t realPtr, ui8 mod);
  140. };
  141. class CMapBlitter
  142. {
  143. protected:
  144. CMapHandler * parent; // ptr to enclosing map handler; generally for legacy reasons, probably could/should be refactored out of here
  145. int tileSize; // size of a tile drawn on map [in pixels]
  146. int halfTileSizeCeil; // half of the tile size, rounded up
  147. int3 tileCount; // number of tiles in current viewport
  148. int3 topTile; // top-left tile of the viewport
  149. int3 initPos; // starting drawing position [in pixels]
  150. int3 pos; // current position [in tiles]
  151. int3 realPos; // current position [in pixels]
  152. Rect realTileRect; // default rect based on current pos: [realPos.x, realPos.y, tileSize, tileSize]
  153. Rect defaultTileRect; // default rect based on 0: [0, 0, tileSize, tileSize]
  154. const MapDrawingInfo * info; // data for drawing passed from outside
  155. /// general drawing method, called internally by more specialized ones
  156. virtual void drawElement(EMapCacheType cacheType, SDL_Surface * sourceSurf, SDL_Rect * sourceRect,
  157. SDL_Surface * targetSurf, SDL_Rect * destRect, bool alphaBlit = false, ui8 rotationInfo = 0u) const = 0;
  158. // first drawing pass
  159. /// draws terrain bitmap (or custom bitmap if applicable) on current tile
  160. virtual void drawTileTerrain(SDL_Surface * targetSurf, const TerrainTile & tinfo, const TerrainTile2 & tile) const;
  161. /// draws a river segment on current tile
  162. virtual void drawRiver(SDL_Surface * targetSurf, const TerrainTile & tinfo) const;
  163. /// draws a road segment on current tile
  164. virtual void drawRoad(SDL_Surface * targetSurf, const TerrainTile & tinfo, const TerrainTile * tinfoUpper) const;
  165. /// draws all objects on current tile (higher-level logic, unlike other draw*** methods)
  166. virtual void drawObjects(SDL_Surface * targetSurf, const TerrainTile2 & tile) const;
  167. /// current tile: draws non-hero object with given image/position
  168. virtual void drawNormalObject(SDL_Surface * targetSurf, SDL_Surface * sourceSurf, SDL_Rect * sourceRect) const;
  169. virtual void drawHeroFlag(SDL_Surface * targetSurf, SDL_Surface * sourceSurf, SDL_Rect * sourceRect, SDL_Rect * destRect, bool moving) const;
  170. virtual void drawHero(SDL_Surface * targetSurf, SDL_Surface * sourceSurf, SDL_Rect * sourceRect, bool moving) const;
  171. // second drawing pass
  172. /// current tile: draws overlay over the map, used to draw world view icons
  173. virtual void drawTileOverlay(SDL_Surface * targetSurf, const TerrainTile2 & tile) const = 0;
  174. /// draws fog of war on current tile
  175. virtual void drawFow(SDL_Surface * targetSurf) const;
  176. /// draws map border frame on current position
  177. virtual void drawFrame(SDL_Surface * targetSurf) const;
  178. // third drawing pass
  179. /// custom post-processing, if needed (used by puzzle view)
  180. virtual void postProcessing(SDL_Surface * targetSurf) const {}
  181. // misc methods
  182. /// initializes frame-drawing (called at the start of every redraw)
  183. virtual void init(const MapDrawingInfo * drawingInfo) = 0;
  184. /// calculates clip region for map viewport
  185. virtual SDL_Rect clip(SDL_Surface * targetSurf) const = 0;
  186. virtual ui8 getHeroFrameNum(ui8 dir, bool isMoving) const;
  187. ///returns appropriate bitmap and info if alpha blitting is necessary
  188. virtual std::pair<SDL_Surface *, bool> getVisBitmap() const;
  189. virtual ui8 getPhaseShift(const CGObjectInstance *object) const;
  190. virtual bool canDrawObject(const CGObjectInstance * obj) const;
  191. virtual bool canDrawCurrentTile() const;
  192. public:
  193. CMapBlitter(CMapHandler * p) : parent(p) {}
  194. virtual ~CMapBlitter(){}
  195. void blit(SDL_Surface * targetSurf, const MapDrawingInfo * info);
  196. };
  197. class CMapNormalBlitter : public CMapBlitter
  198. {
  199. protected:
  200. void drawElement(EMapCacheType cacheType, SDL_Surface * sourceSurf, SDL_Rect * sourceRect,
  201. SDL_Surface * targetSurf, SDL_Rect * destRect, bool alphaBlit = false, ui8 rotationInfo = 0u) const override;
  202. void drawTileOverlay(SDL_Surface * targetSurf,const TerrainTile2 & tile) const override {}
  203. void init(const MapDrawingInfo * info) override;
  204. SDL_Rect clip(SDL_Surface * targetSurf) const override;
  205. public:
  206. CMapNormalBlitter(CMapHandler * parent);
  207. virtual ~CMapNormalBlitter(){}
  208. };
  209. class CMapWorldViewBlitter : public CMapBlitter
  210. {
  211. protected:
  212. void drawElement(EMapCacheType cacheType, SDL_Surface * sourceSurf, SDL_Rect * sourceRect,
  213. SDL_Surface * targetSurf, SDL_Rect * destRect, bool alphaBlit = false, ui8 rotationInfo = 0u) const override;
  214. void drawTileOverlay(SDL_Surface * targetSurf, const TerrainTile2 & tile) const override;
  215. void drawNormalObject(SDL_Surface * targetSurf, SDL_Surface * sourceSurf, SDL_Rect * sourceRect) const override;
  216. void drawHeroFlag(SDL_Surface * targetSurf, SDL_Surface * sourceSurf, SDL_Rect * sourceRect, SDL_Rect * destRect, bool moving) const override;
  217. void drawHero(SDL_Surface * targetSurf, SDL_Surface * sourceSurf, SDL_Rect * sourceRect, bool moving) const;
  218. void drawFrame(SDL_Surface * targetSurf) const override {}
  219. void init(const MapDrawingInfo * info) override;
  220. SDL_Rect clip(SDL_Surface * targetSurf) const override;
  221. ui8 getHeroFrameNum(ui8 dir, bool isMoving) const override { return 0u; }
  222. ui8 getPhaseShift(const CGObjectInstance *object) const override { return 0u; }
  223. void drawScaledRotatedElement(EMapCacheType type, SDL_Surface * baseSurf, SDL_Surface * targetSurf, ui8 rotation,
  224. float scale, SDL_Rect * dstRect, SDL_Rect * srcRect = nullptr) const;
  225. void calculateWorldViewCameraPos();
  226. public:
  227. CMapWorldViewBlitter(CMapHandler * parent);
  228. virtual ~CMapWorldViewBlitter(){}
  229. };
  230. class CMapPuzzleViewBlitter : public CMapNormalBlitter
  231. {
  232. std::vector<int> unblittableObjects;
  233. void drawObjects(SDL_Surface * targetSurf, const TerrainTile2 & tile) const override;
  234. void drawFow(SDL_Surface * targetSurf) const override {} // skipping FoW in puzzle view
  235. void postProcessing(SDL_Surface * targetSurf) const override;
  236. bool canDrawObject(const CGObjectInstance * obj) const override;
  237. bool canDrawCurrentTile() const override { return true; }
  238. public:
  239. CMapPuzzleViewBlitter(CMapHandler * parent);
  240. };
  241. CMapCache cache;
  242. CMapBlitter * normalBlitter;
  243. CMapBlitter * worldViewBlitter;
  244. CMapBlitter * puzzleViewBlitter;
  245. CMapBlitter * resolveBlitter(const MapDrawingInfo * info) const;
  246. public:
  247. PseudoV< PseudoV< PseudoV<TerrainTile2> > > ttiles; //informations about map tiles
  248. int3 sizes; //map size (x = width, y = height, z = number of levels)
  249. const CMap * map;
  250. // Max number of tiles that will fit in the map screen. Tiles
  251. // can be partial on each edges.
  252. int tilesW;
  253. int tilesH;
  254. // size of each side of the frame around the whole map, in tiles
  255. int frameH;
  256. int frameW;
  257. // Coord in pixels of the top left corner of the top left tile to
  258. // draw. Values range is [-31..0]. A negative value
  259. // implies that part of the tile won't be displayed.
  260. int offsetX;
  261. int offsetY;
  262. //std::set<int> usedHeroes;
  263. std::vector<std::vector<SDL_Surface *> > terrainGraphics; // [terrain id] [view type] [rotation type]
  264. std::vector<CDefEssential *> roadDefs;
  265. std::vector<CDefEssential *> staticRiverDefs;
  266. std::vector<std::vector<std::vector<ui8> > > hideBitmap; //specifies number of graphic that should be used to fully hide a tile
  267. mutable std::map<const CGObjectInstance*, ui8> animationPhase;
  268. CMapHandler(); //c-tor
  269. ~CMapHandler(); //d-tor
  270. void getTerrainDescr(const int3 &pos, std::string & out, bool terName); //if tername == false => empty string when tile is clear
  271. CGObjectInstance * createObject(int id, int subid, int3 pos, int owner=254); //creates a new object with a certain id and subid
  272. bool printObject(const CGObjectInstance * obj); //puts appropriate things to ttiles, so obj will be visible on map
  273. bool hideObject(const CGObjectInstance * obj); //removes appropriate things from ttiles, so obj will be no longer visible on map (but still will exist)
  274. bool removeObject(CGObjectInstance * obj); //removes object from each place in VCMI (I hope)
  275. void init();
  276. void calculateBlockedPos();
  277. void initObjectRects();
  278. void borderAndTerrainBitmapInit();
  279. void roadsRiverTerrainInit();
  280. void prepareFOWDefs();
  281. void drawTerrainRectNew(SDL_Surface * targetSurface, const MapDrawingInfo * info);
  282. void updateWater();
  283. void validateRectTerr(SDL_Rect * val, const SDL_Rect * ext); //terrainRect helper
  284. 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]
  285. void discardWorldViewCache();
  286. static bool compareObjectBlitOrder(const CGObjectInstance * a, const CGObjectInstance * b);
  287. };