mapHandler.h 13 KB

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