mapHandler.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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
  91. };
  92. /// temporarily caches rescaled sdl surfaces for map world view redrawing
  93. class CMapCache
  94. {
  95. std::map<EMapCacheType, std::map<int, 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, int key);
  103. /// asks for cached data; @returns nullptr if data is not in cache
  104. SDL_Surface * requestWorldViewCache(EMapCacheType type, int key);
  105. /// asks for cached data; @returns cached data if found, new scaled surface otherwise
  106. SDL_Surface * requestWorldViewCacheOrCreate(EMapCacheType type, int key, SDL_Surface * fullSurface, float scale);
  107. SDL_Surface * cacheWorldViewEntry(EMapCacheType type, int key, SDL_Surface * entry);
  108. };
  109. CMapCache cache;
  110. void drawWorldViewOverlay(int targetTilesX, int targetTilesY, int srx_init, int sry_init, CDefHandler * iconsDef, const std::vector< std::vector< std::vector<ui8> > > * visibilityMap, float scale, int targetTileSize, int3 top_tile, SDL_Surface * extSurf);
  111. void calculateWorldViewCameraPos(int targetTilesX, int targetTilesY, int3 &top_tile);
  112. public:
  113. PseudoV< PseudoV< PseudoV<TerrainTile2> > > ttiles; //informations about map tiles
  114. int3 sizes; //map size (x = width, y = height, z = number of levels)
  115. const CMap * map;
  116. // Max number of tiles that will fit in the map screen. Tiles
  117. // can be partial on each edges.
  118. int tilesW;
  119. int tilesH;
  120. // size of each side of the frame around the whole map, in tiles
  121. int frameH;
  122. int frameW;
  123. // Coord in pixels of the top left corner of the top left tile to
  124. // draw. Values range is [-31..0]. A negative value
  125. // implies that part of the tile won't be displayed.
  126. int offsetX;
  127. int offsetY;
  128. //std::set<int> usedHeroes;
  129. std::vector<std::vector<SDL_Surface *> > terrainGraphics; // [terrain id] [view type] [rotation type]
  130. std::vector<CDefEssential *> roadDefs;
  131. std::vector<CDefEssential *> staticRiverDefs;
  132. std::vector<std::vector<std::vector<ui8> > > hideBitmap; //specifies number of graphic that should be used to fully hide a tile
  133. mutable std::map<const CGObjectInstance*, ui8> animationPhase;
  134. CMapHandler(); //c-tor
  135. ~CMapHandler(); //d-tor
  136. 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
  137. ui8 getPhaseShift(const CGObjectInstance *object) const;
  138. void getTerrainDescr(const int3 &pos, std::string & out, bool terName); //if tername == false => empty string when tile is clear
  139. CGObjectInstance * createObject(int id, int subid, int3 pos, int owner=254); //creates a new object with a certain id and subid
  140. bool printObject(const CGObjectInstance * obj); //puts appropriate things to ttiles, so obj will be visible on map
  141. bool hideObject(const CGObjectInstance * obj); //removes appropriate things from ttiles, so obj will be no longer visible on map (but still will exist)
  142. bool removeObject(CGObjectInstance * obj); //removes object from each place in VCMI (I hope)
  143. void init();
  144. void calculateBlockedPos();
  145. void initObjectRects();
  146. void borderAndTerrainBitmapInit();
  147. void roadsRiverTerrainInit();
  148. void prepareFOWDefs();
  149. 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;
  150. 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);
  151. void updateWater();
  152. ui8 getHeroFrameNum(ui8 dir, bool isMoving) const; //terrainRect helper function
  153. void validateRectTerr(SDL_Rect * val, const SDL_Rect * ext); //terrainRect helper
  154. 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]
  155. void discardWorldViewCache();
  156. static bool compareObjectBlitOrder(const CGObjectInstance * a, const CGObjectInstance * b);
  157. };