CMinimap.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * CMinimap.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CMinimap.h"
  12. #include "CAdvMapInt.h"
  13. #include "../widgets/Images.h"
  14. #include "../CGameInfo.h"
  15. #include "../CPlayerInterface.h"
  16. #include "../gui/CGuiHandler.h"
  17. #include "../render/Colors.h"
  18. #include "../renderSDL/SDL_PixelAccess.h"
  19. #include "../../CCallback.h"
  20. #include "../../lib/CGeneralTextHandler.h"
  21. #include "../../lib/TerrainHandler.h"
  22. #include "../../lib/mapObjects/CGHeroInstance.h"
  23. #include "../../lib/mapping/CMapDefines.h"
  24. #include <SDL_surface.h>
  25. const SDL_Color & CMinimapInstance::getTileColor(const int3 & pos)
  26. {
  27. const TerrainTile * tile = LOCPLINT->cb->getTile(pos, false);
  28. // if tile is not visible it will be black on minimap
  29. if(!tile)
  30. return Colors::BLACK;
  31. // if object at tile is owned - it will be colored as its owner
  32. for (const CGObjectInstance *obj : tile->blockingObjects)
  33. {
  34. //heroes will be blitted later
  35. switch (obj->ID)
  36. {
  37. case Obj::HERO:
  38. case Obj::PRISON:
  39. continue;
  40. }
  41. PlayerColor player = obj->getOwner();
  42. if(player == PlayerColor::NEUTRAL)
  43. return *graphics->neutralColor;
  44. else
  45. if (player < PlayerColor::PLAYER_LIMIT)
  46. return graphics->playerColors[player.getNum()];
  47. }
  48. // else - use terrain color (blocked version or normal)
  49. const auto & colorPair = parent->colors.find(tile->terType->getId())->second;
  50. if (tile->blocked && (!tile->visitable))
  51. return colorPair.second;
  52. else
  53. return colorPair.first;
  54. }
  55. void CMinimapInstance::tileToPixels (const int3 &tile, int &x, int &y, int toX, int toY)
  56. {
  57. int3 mapSizes = LOCPLINT->cb->getMapSize();
  58. double stepX = double(pos.w) / mapSizes.x;
  59. double stepY = double(pos.h) / mapSizes.y;
  60. x = static_cast<int>(toX + stepX * tile.x);
  61. y = static_cast<int>(toY + stepY * tile.y);
  62. }
  63. void CMinimapInstance::blitTileWithColor(const SDL_Color &color, const int3 &tile, SDL_Surface *to, int toX, int toY)
  64. {
  65. //coordinates of rectangle on minimap representing this tile
  66. // begin - first to blit, end - first NOT to blit
  67. int xBegin, yBegin, xEnd, yEnd;
  68. tileToPixels (tile, xBegin, yBegin, toX, toY);
  69. tileToPixels (int3 (tile.x + 1, tile.y + 1, tile.z), xEnd, yEnd, toX, toY);
  70. for (int y=yBegin; y<yEnd; y++)
  71. {
  72. uint8_t *ptr = (uint8_t*)to->pixels + y * to->pitch + xBegin * minimap->format->BytesPerPixel;
  73. for (int x=xBegin; x<xEnd; x++)
  74. ColorPutter<4, 1>::PutColor(ptr, color);
  75. }
  76. }
  77. void CMinimapInstance::refreshTile(const int3 &tile)
  78. {
  79. blitTileWithColor(getTileColor(int3(tile.x, tile.y, level)), tile, minimap, 0, 0);
  80. }
  81. void CMinimapInstance::drawScaled(int level)
  82. {
  83. int3 mapSizes = LOCPLINT->cb->getMapSize();
  84. //size of one map tile on our minimap
  85. double stepX = double(pos.w) / mapSizes.x;
  86. double stepY = double(pos.h) / mapSizes.y;
  87. double currY = 0;
  88. for (int y=0; y<mapSizes.y; y++, currY += stepY)
  89. {
  90. double currX = 0;
  91. for (int x=0; x<mapSizes.x; x++, currX += stepX)
  92. {
  93. const SDL_Color &color = getTileColor(int3(x,y,level));
  94. //coordinates of rectangle on minimap representing this tile
  95. // begin - first to blit, end - first NOT to blit
  96. int xBegin = static_cast<int>(currX);
  97. int yBegin = static_cast<int>(currY);
  98. int xEnd = static_cast<int>(currX + stepX);
  99. int yEnd = static_cast<int>(currY + stepY);
  100. for (int y=yBegin; y<yEnd; y++)
  101. {
  102. uint8_t *ptr = (uint8_t*)minimap->pixels + y * minimap->pitch + xBegin * minimap->format->BytesPerPixel;
  103. for (int x=xBegin; x<xEnd; x++)
  104. ColorPutter<4, 1>::PutColor(ptr, color);
  105. }
  106. }
  107. }
  108. }
  109. CMinimapInstance::CMinimapInstance(CMinimap *Parent, int Level):
  110. parent(Parent),
  111. minimap(CSDL_Ext::createSurfaceWithBpp<4>(parent->pos.w, parent->pos.h)),
  112. level(Level)
  113. {
  114. pos.w = parent->pos.w;
  115. pos.h = parent->pos.h;
  116. drawScaled(level);
  117. }
  118. CMinimapInstance::~CMinimapInstance()
  119. {
  120. SDL_FreeSurface(minimap);
  121. }
  122. void CMinimapInstance::showAll(SDL_Surface * to)
  123. {
  124. blitAtLoc(minimap, 0, 0, to);
  125. //draw heroes
  126. std::vector <const CGHeroInstance *> heroes = LOCPLINT->cb->getHeroesInfo(false); //TODO: do we really need separate function for drawing heroes?
  127. for(auto & hero : heroes)
  128. {
  129. int3 position = hero->visitablePos();
  130. if(position.z == level)
  131. {
  132. const SDL_Color & color = graphics->playerColors[hero->getOwner().getNum()];
  133. blitTileWithColor(color, position, to, pos.x, pos.y);
  134. }
  135. }
  136. }
  137. std::map<TerrainId, std::pair<SDL_Color, SDL_Color> > CMinimap::loadColors()
  138. {
  139. std::map<TerrainId, std::pair<SDL_Color, SDL_Color> > ret;
  140. for(const auto & terrain : CGI->terrainTypeHandler->objects)
  141. {
  142. SDL_Color normal = CSDL_Ext::toSDL(terrain->minimapUnblocked);
  143. SDL_Color blocked = CSDL_Ext::toSDL(terrain->minimapBlocked);
  144. ret[terrain->getId()] = std::make_pair(normal, blocked);
  145. }
  146. return ret;
  147. }
  148. CMinimap::CMinimap(const Rect & position)
  149. : CIntObject(LCLICK | RCLICK | HOVER | MOVE, position.topLeft()),
  150. level(0),
  151. colors(loadColors())
  152. {
  153. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  154. pos.w = position.w;
  155. pos.h = position.h;
  156. aiShield = std::make_shared<CPicture>("AIShield");
  157. aiShield->disable();
  158. }
  159. int3 CMinimap::translateMousePosition()
  160. {
  161. // 0 = top-left corner, 1 = bottom-right corner
  162. double dx = double(GH.getCursorPosition().x - pos.x) / pos.w;
  163. double dy = double(GH.getCursorPosition().y - pos.y) / pos.h;
  164. int3 mapSizes = LOCPLINT->cb->getMapSize();
  165. int3 tile ((si32)(mapSizes.x * dx), (si32)(mapSizes.y * dy), level);
  166. return tile;
  167. }
  168. void CMinimap::moveAdvMapSelection()
  169. {
  170. int3 newLocation = translateMousePosition();
  171. adventureInt->centerOn(newLocation);
  172. if (!(adventureInt->active & GENERAL))
  173. GH.totalRedraw(); //redraw this as well as inactive adventure map
  174. else
  175. redraw();//redraw only this
  176. }
  177. void CMinimap::clickLeft(tribool down, bool previousState)
  178. {
  179. if(down)
  180. moveAdvMapSelection();
  181. }
  182. void CMinimap::clickRight(tribool down, bool previousState)
  183. {
  184. adventureInt->handleRightClick(CGI->generaltexth->zelp[291].second, down);
  185. }
  186. void CMinimap::hover(bool on)
  187. {
  188. if(on)
  189. GH.statusbar->write(CGI->generaltexth->zelp[291].first);
  190. else
  191. GH.statusbar->clear();
  192. }
  193. void CMinimap::mouseMoved(const Point & cursorPosition)
  194. {
  195. if(mouseState(MouseButton::LEFT))
  196. moveAdvMapSelection();
  197. }
  198. void CMinimap::showAll(SDL_Surface * to)
  199. {
  200. CIntObject::showAll(to);
  201. if(minimap)
  202. {
  203. int3 mapSizes = LOCPLINT->cb->getMapSize();
  204. int3 tileCountOnScreen = adventureInt->terrain.tileCountOnScreen();
  205. //draw radar
  206. Rect oldClip;
  207. Rect radar =
  208. {
  209. si16(adventureInt->position.x * pos.w / mapSizes.x + pos.x),
  210. si16(adventureInt->position.y * pos.h / mapSizes.y + pos.y),
  211. ui16(tileCountOnScreen.x * pos.w / mapSizes.x),
  212. ui16(tileCountOnScreen.y * pos.h / mapSizes.y)
  213. };
  214. if(adventureInt->mode == EAdvMapMode::WORLD_VIEW)
  215. {
  216. // adjusts radar so that it doesn't go out of map in world view mode (since there's no frame)
  217. radar.x = std::min<int>(std::max(pos.x, radar.x), pos.x + pos.w - radar.w);
  218. radar.y = std::min<int>(std::max(pos.y, radar.y), pos.y + pos.h - radar.h);
  219. if(radar.x < pos.x && radar.y < pos.y)
  220. return; // whole map is visible at once, no point in redrawing border
  221. }
  222. CSDL_Ext::getClipRect(to, oldClip);
  223. CSDL_Ext::setClipRect(to, pos);
  224. CSDL_Ext::drawDashedBorder(to, radar, Colors::PURPLE);
  225. CSDL_Ext::setClipRect(to, oldClip);
  226. }
  227. }
  228. void CMinimap::update()
  229. {
  230. if(aiShield->recActions & UPDATE) //AI turn is going on. There is no need to update minimap
  231. return;
  232. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  233. minimap = std::make_shared<CMinimapInstance>(this, level);
  234. redraw();
  235. }
  236. void CMinimap::setLevel(int newLevel)
  237. {
  238. level = newLevel;
  239. update();
  240. }
  241. void CMinimap::setAIRadar(bool on)
  242. {
  243. if(on)
  244. {
  245. aiShield->enable();
  246. minimap.reset();
  247. }
  248. else
  249. {
  250. aiShield->disable();
  251. update();
  252. }
  253. // this my happen during AI turn when this interface is inactive
  254. // force redraw in order to properly update interface
  255. GH.totalRedraw();
  256. }
  257. void CMinimap::hideTile(const int3 &pos)
  258. {
  259. if(minimap)
  260. minimap->refreshTile(pos);
  261. }
  262. void CMinimap::showTile(const int3 &pos)
  263. {
  264. if(minimap)
  265. minimap->refreshTile(pos);
  266. }