mapHandler.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #pragma once
  2. #include "../lib/int3.h"
  3. #include "SDL.h"
  4. /*
  5. * mapHandler.h, part of VCMI engine
  6. *
  7. * Authors: listed in file AUTHORS in main folder
  8. *
  9. * License: GNU General Public License v2.0 or later
  10. * Full text of license available in license.txt file, in main folder
  11. *
  12. */
  13. class CGObjectInstance;
  14. class CGHeroInstance;
  15. class CMap;
  16. class CGDefInfo;
  17. class CGObjectInstance;
  18. class CDefHandler;
  19. struct TerrainTile;
  20. struct SDL_Surface;
  21. struct SDL_Rect;
  22. class CDefEssential;
  23. enum class EWorldViewIcon
  24. {
  25. TOWN = 0,
  26. HERO,
  27. ARTIFACT,
  28. TELEPORT,
  29. GATE,
  30. MINE_WOOD,
  31. MINE_MERCURY,
  32. MINE_STONE,
  33. MINE_SULFUR,
  34. MINE_CRYSTAL,
  35. MINE_GEM,
  36. MINE_GOLD,
  37. RES_WOOD,
  38. RES_MERCURY,
  39. RES_STONE,
  40. RES_SULFUR,
  41. RES_CRYSTAL,
  42. RES_GEM,
  43. RES_GOLD,
  44. };
  45. struct TerrainTile2
  46. {
  47. SDL_Surface * terbitmap; //bitmap of terrain
  48. 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
  49. TerrainTile2();
  50. };
  51. template <typename T> class PseudoV
  52. {
  53. public:
  54. PseudoV() : offset(0) { }
  55. PseudoV(std::vector<T> &src, int rest, int before, int after, const T& fill) : offset(before)
  56. {
  57. inver.resize(before + rest + after);
  58. for(int i=0; i<before;i++)
  59. inver[i] = fill;
  60. for(int i=0;i<src.size();i++)
  61. inver[offset+i] = src[i];
  62. for(int i=src.size(); i<src.size()+after;i++)
  63. inver[offset+i] = fill;
  64. }
  65. inline T & operator[](const int & n)
  66. {
  67. return inver[n+offset];
  68. }
  69. inline const T & operator[](const int & n) const
  70. {
  71. return inver[n+offset];
  72. }
  73. void resize(int rest, int before, int after)
  74. {
  75. inver.resize(before + rest + after);
  76. offset=before;
  77. }
  78. int size() const
  79. {
  80. return inver.size();
  81. }
  82. private:
  83. int offset;
  84. std::vector<T> inver;
  85. };
  86. class CMapHandler
  87. {
  88. enum class EMapCacheType
  89. {
  90. TERRAIN, TERRAIN_CUSTOM, OBJECTS, ROADS, RIVERS, FOW, HEROES, HERO_FLAGS
  91. };
  92. /// temporarily caches rescaled sdl surfaces for map world view redrawing
  93. class CMapCache
  94. {
  95. std::map<EMapCacheType, std::map<intptr_t, SDL_Surface *>> data;
  96. float worldViewCachedScale;
  97. public:
  98. /// destroys all cached data (frees surfaces)
  99. void discardWorldViewCache();
  100. /// updates scale and determines if currently cached data is still valid
  101. void updateWorldViewScale(float scale);
  102. void removeFromWorldViewCache(EMapCacheType type, intptr_t key);
  103. /// asks for cached data; @returns cached surface or nullptr if data is not in cache
  104. SDL_Surface * requestWorldViewCache(EMapCacheType type, intptr_t key);
  105. /// asks for cached data; @returns cached data if found, new scaled surface otherwise
  106. SDL_Surface * requestWorldViewCacheOrCreate(EMapCacheType type, intptr_t key, SDL_Surface * fullSurface, float scale);
  107. SDL_Surface * cacheWorldViewEntry(EMapCacheType type, intptr_t key, SDL_Surface * entry);
  108. intptr_t genKey(intptr_t realPtr, ui8 mod);
  109. };
  110. CMapCache cache;
  111. void drawWorldViewOverlay(int targetTilesX, int targetTilesY, int srx_init, int sry_init, CDefHandler * iconsDef,
  112. const std::vector< std::vector< std::vector<ui8> > > * visibilityMap, float scale, int targetTileSize,
  113. int3 top_tile, SDL_Surface * extSurf);
  114. void drawScaledRotatedElement(EMapCacheType type, SDL_Surface * baseSurf, SDL_Surface * targetSurf, ui8 rotation,
  115. float scale, SDL_Rect * dstRect, SDL_Rect * srcRect = nullptr);
  116. void calculateWorldViewCameraPos(int targetTilesX, int targetTilesY, int3 &top_tile);
  117. public:
  118. PseudoV< PseudoV< PseudoV<TerrainTile2> > > ttiles; //informations about map tiles
  119. int3 sizes; //map size (x = width, y = height, z = number of levels)
  120. const CMap * map;
  121. // Max number of tiles that will fit in the map screen. Tiles
  122. // can be partial on each edges.
  123. int tilesW;
  124. int tilesH;
  125. // size of each side of the frame around the whole map, in tiles
  126. int frameH;
  127. int frameW;
  128. // Coord in pixels of the top left corner of the top left tile to
  129. // draw. Values range is [-31..0]. A negative value
  130. // implies that part of the tile won't be displayed.
  131. int offsetX;
  132. int offsetY;
  133. //std::set<int> usedHeroes;
  134. std::vector<std::vector<SDL_Surface *> > terrainGraphics; // [terrain id] [view type] [rotation type]
  135. std::vector<CDefEssential *> roadDefs;
  136. std::vector<CDefEssential *> staticRiverDefs;
  137. std::vector<std::vector<std::vector<ui8> > > hideBitmap; //specifies number of graphic that should be used to fully hide a tile
  138. mutable std::map<const CGObjectInstance*, ui8> animationPhase;
  139. CMapHandler(); //c-tor
  140. ~CMapHandler(); //d-tor
  141. std::pair<SDL_Surface *, bool> getVisBitmap(const int3 & pos, const std::vector< std::vector< std::vector<ui8> > > & visibilityMap) const; //returns appropriate bitmap and info if alpha blitting is necessary
  142. ui8 getPhaseShift(const CGObjectInstance *object) const;
  143. void getTerrainDescr(const int3 &pos, std::string & out, bool terName); //if tername == false => empty string when tile is clear
  144. CGObjectInstance * createObject(int id, int subid, int3 pos, int owner=254); //creates a new object with a certain id and subid
  145. bool printObject(const CGObjectInstance * obj); //puts appropriate things to ttiles, so obj will be visible on map
  146. bool hideObject(const CGObjectInstance * obj); //removes appropriate things from ttiles, so obj will be no longer visible on map (but still will exist)
  147. bool removeObject(CGObjectInstance * obj); //removes object from each place in VCMI (I hope)
  148. void init();
  149. void calculateBlockedPos();
  150. void initObjectRects();
  151. void borderAndTerrainBitmapInit();
  152. void roadsRiverTerrainInit();
  153. void prepareFOWDefs();
  154. void terrainRect(int3 top_tile, ui8 anim, const std::vector< std::vector< std::vector<ui8> > > * visibilityMap, bool otherHeroAnim, ui8 heroAnim, SDL_Surface * extSurf, const SDL_Rect * extRect, int moveX, int moveY, bool puzzleMode, int3 grailPosRel) const;
  155. void terrainRectScaled(int3 top_tile, const std::vector< std::vector< std::vector<ui8> > > * visibilityMap, SDL_Surface * extSurf, const SDL_Rect * extRect, float scale, CDefHandler * iconsDef);
  156. void updateWater();
  157. ui8 getHeroFrameNum(ui8 dir, bool isMoving) const; //terrainRect helper function
  158. void validateRectTerr(SDL_Rect * val, const SDL_Rect * ext); //terrainRect helper
  159. 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]
  160. void discardWorldViewCache();
  161. static bool compareObjectBlitOrder(const CGObjectInstance * a, const CGObjectInstance * b);
  162. };