CMinimap.cpp 8.0 KB

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