mapHandler.cpp 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707
  1. /*
  2. * mapHandler.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 "mapHandler.h"
  12. #include "CBitmapHandler.h"
  13. #include "gui/CAnimation.h"
  14. #include "gui/SDL_Extensions.h"
  15. #include "CGameInfo.h"
  16. #include "../lib/mapObjects/CGHeroInstance.h"
  17. #include "../lib/mapObjects/CObjectClassesHandler.h"
  18. #include "../lib/CGameState.h"
  19. #include "../lib/CHeroHandler.h"
  20. #include "../lib/CTownHandler.h"
  21. #include "Graphics.h"
  22. #include "../lib/mapping/CMap.h"
  23. #include "CDefHandler.h"
  24. #include "../lib/CConfigHandler.h"
  25. #include "../lib/CGeneralTextHandler.h"
  26. #include "../lib/GameConstants.h"
  27. #include "../lib/CStopWatch.h"
  28. #include "CMT.h"
  29. #include "../lib/CRandomGenerator.h"
  30. #define ADVOPT (conf.go()->ac)
  31. std::string nameFromType (int typ)
  32. {
  33. switch(ETerrainType(typ))
  34. {
  35. case ETerrainType::DIRT:
  36. return std::string("DIRTTL.DEF");
  37. case ETerrainType::SAND:
  38. return std::string("SANDTL.DEF");
  39. case ETerrainType::GRASS:
  40. return std::string("GRASTL.DEF");
  41. case ETerrainType::SNOW:
  42. return std::string("SNOWTL.DEF");
  43. case ETerrainType::SWAMP:
  44. return std::string("SWMPTL.DEF");
  45. case ETerrainType::ROUGH:
  46. return std::string("ROUGTL.DEF");
  47. case ETerrainType::SUBTERRANEAN:
  48. return std::string("SUBBTL.DEF");
  49. case ETerrainType::LAVA:
  50. return std::string("LAVATL.DEF");
  51. case ETerrainType::WATER:
  52. return std::string("WATRTL.DEF");
  53. case ETerrainType::ROCK:
  54. return std::string("ROCKTL.DEF");
  55. case ETerrainType::BORDER:
  56. //TODO use me
  57. break;
  58. default:
  59. //TODO do something here
  60. break;
  61. }
  62. return std::string();
  63. }
  64. static bool objectBlitOrderSorter(const TerrainTileObject & a, const TerrainTileObject & b)
  65. {
  66. return CMapHandler::compareObjectBlitOrder(a.obj, b.obj);
  67. }
  68. struct NeighborTilesInfo
  69. {
  70. bool d7, //789
  71. d8, //456
  72. d9, //123
  73. d4,
  74. d5,
  75. d6,
  76. d1,
  77. d2,
  78. d3;
  79. NeighborTilesInfo(const int3 & pos, const int3 & sizes, const std::vector< std::vector< std::vector<ui8> > > & visibilityMap)
  80. {
  81. auto getTile = [&](int dx, int dy)->bool
  82. {
  83. if ( dx + pos.x < 0 || dx + pos.x >= sizes.x
  84. || dy + pos.y < 0 || dy + pos.y >= sizes.y)
  85. return false;
  86. return visibilityMap[dx+pos.x][dy+pos.y][pos.z];
  87. };
  88. d7 = getTile(-1, -1); //789
  89. d8 = getTile( 0, -1); //456
  90. d9 = getTile(+1, -1); //123
  91. d4 = getTile(-1, 0);
  92. d5 = visibilityMap[pos.x][pos.y][pos.z];
  93. d6 = getTile(+1, 0);
  94. d1 = getTile(-1, +1);
  95. d2 = getTile( 0, +1);
  96. d3 = getTile(+1, +1);
  97. }
  98. bool areAllHidden() const
  99. {
  100. return !(d1 || d2 || d3 || d4 || d5 || d6 || d7 || d8 || d8 );
  101. }
  102. int getBitmapID() const
  103. {
  104. //NOTE: some images have unused in VCMI pair (same blockmap but a bit different look)
  105. // 0-1, 2-3, 4-5, 11-13, 12-14
  106. static const int visBitmaps[256] = {
  107. -1, 34, 4, 4, 22, 23, 4, 4, 36, 36, 38, 38, 47, 47, 38, 38, //16
  108. 3, 25, 12, 12, 3, 25, 12, 12, 9, 9, 6, 6, 9, 9, 6, 6, //32
  109. 35, 39, 48, 48, 41, 43, 48, 48, 36, 36, 38, 38, 47, 47, 38, 38, //48
  110. 26, 49, 28, 28, 26, 49, 28, 28, 9, 9, 6, 6, 9, 9, 6, 6, //64
  111. 0, 45, 29, 29, 24, 33, 29, 29, 37, 37, 7, 7, 50, 50, 7, 7, //80
  112. 13, 27, 44, 44, 13, 27, 44, 44, 8, 8, 10, 10, 8, 8, 10, 10, //96
  113. 0, 45, 29, 29, 24, 33, 29, 29, 37, 37, 7, 7, 50, 50, 7, 7, //112
  114. 13, 27, 44, 44, 13, 27, 44, 44, 8, 8, 10, 10, 8, 8, 10, 10, //128
  115. 15, 17, 30, 30, 16, 19, 30, 30, 46, 46, 40, 40, 32, 32, 40, 40, //144
  116. 2, 25, 12, 12, 2, 25, 12, 12, 9, 9, 6, 6, 9, 9, 6, 6, //160
  117. 18, 42, 31, 31, 20, 21, 31, 31, 46, 46, 40, 40, 32, 32, 40, 40, //176
  118. 26, 49, 28, 28, 26, 49, 28, 28, 9, 9, 6, 6, 9, 9, 6, 6, //192
  119. 0, 45, 29, 29, 24, 33, 29, 29, 37, 37, 7, 7, 50, 50, 7, 7, //208
  120. 13, 27, 44, 44, 13, 27, 44, 44, 8, 8, 10, 10, 8, 8, 10, 10, //224
  121. 0, 45, 29, 29, 24, 33, 29, 29, 37, 37, 7, 7, 50, 50, 7, 7, //240
  122. 13, 27, 44, 44, 13, 27, 44, 44, 8, 8, 10, 10, 8, 8, 10, 10 //256
  123. };
  124. return visBitmaps[d1 + d2 * 2 + d3 * 4 + d4 * 8 + d6 * 16 + d7 * 32 + d8 * 64 + d9 * 128]; // >=0 -> partial hide, <0 - full hide
  125. }
  126. };
  127. void CMapHandler::prepareFOWDefs()
  128. {
  129. graphics->FoWfullHide = CDefHandler::giveDef("TSHRC.DEF");
  130. graphics->FoWpartialHide = CDefHandler::giveDef("TSHRE.DEF");
  131. //adding necessary rotations
  132. static const int missRot [] = {22, 15, 2, 13, 12, 16, 28, 17, 20, 19, 7, 24, 26, 25, 30, 32, 27};
  133. Cimage nw;
  134. for(auto & elem : missRot)
  135. {
  136. nw = graphics->FoWpartialHide->ourImages[elem];
  137. nw.bitmap = CSDL_Ext::verticalFlip(nw.bitmap);
  138. graphics->FoWpartialHide->ourImages.push_back(nw);
  139. }
  140. //necessaary rotations added
  141. //alpha - transformation
  142. for(auto & elem : graphics->FoWpartialHide->ourImages)
  143. {
  144. CSDL_Ext::alphaTransform(elem.bitmap);
  145. }
  146. //initialization of type of full-hide image
  147. hideBitmap.resize(sizes.x);
  148. for (auto & elem : hideBitmap)
  149. {
  150. elem.resize(sizes.y);
  151. }
  152. for (auto & elem : hideBitmap)
  153. {
  154. for (int j = 0; j < sizes.y; ++j)
  155. {
  156. elem[j].resize(sizes.z);
  157. for(int k = 0; k < sizes.z; ++k)
  158. {
  159. elem[j][k] = CRandomGenerator::getDefault().nextInt(graphics->FoWfullHide->ourImages.size() - 1);
  160. }
  161. }
  162. }
  163. }
  164. EMapAnimRedrawStatus CMapHandler::drawTerrainRectNew(SDL_Surface * targetSurface, const MapDrawingInfo * info, bool redrawOnlyAnim /* = false */)
  165. {
  166. assert(info);
  167. bool hasActiveFade = updateObjectsFade();
  168. resolveBlitter(info)->blit(targetSurface, info);
  169. return hasActiveFade ? EMapAnimRedrawStatus::REDRAW_REQUESTED : EMapAnimRedrawStatus::OK;
  170. }
  171. void CMapHandler::roadsRiverTerrainInit()
  172. {
  173. //initializing road's and river's DefHandlers
  174. roadDefs.push_back(CDefHandler::giveDefEss("dirtrd.def"));
  175. roadDefs.push_back(CDefHandler::giveDefEss("gravrd.def"));
  176. roadDefs.push_back(CDefHandler::giveDefEss("cobbrd.def"));
  177. staticRiverDefs.push_back(CDefHandler::giveDefEss("clrrvr.def"));
  178. staticRiverDefs.push_back(CDefHandler::giveDefEss("icyrvr.def"));
  179. staticRiverDefs.push_back(CDefHandler::giveDefEss("mudrvr.def"));
  180. staticRiverDefs.push_back(CDefHandler::giveDefEss("lavrvr.def"));
  181. for(auto & elem : staticRiverDefs)
  182. {
  183. for(size_t h=0; h < elem->ourImages.size(); ++h)
  184. {
  185. CSDL_Ext::alphaTransform(elem->ourImages[h].bitmap);
  186. }
  187. }
  188. for(auto & elem : roadDefs)
  189. {
  190. for(size_t h=0; h < elem->ourImages.size(); ++h)
  191. {
  192. CSDL_Ext::alphaTransform(elem->ourImages[h].bitmap);
  193. }
  194. }
  195. // Create enough room for the whole map and its frame
  196. ttiles.resize(sizes.x, frameW, frameW);
  197. for (int i=0-frameW;i<ttiles.size()-frameW;i++)
  198. {
  199. ttiles[i].resize(sizes.y, frameH, frameH);
  200. }
  201. for (int i=0-frameW;i<ttiles.size()-frameW;i++)
  202. {
  203. for (int j=0-frameH;j<(int)sizes.y+frameH;j++)
  204. ttiles[i][j].resize(sizes.z, 0, 0);
  205. }
  206. }
  207. void CMapHandler::borderAndTerrainBitmapInit()
  208. {
  209. CDefHandler * bord = CDefHandler::giveDef("EDG.DEF");
  210. bord->notFreeImgs = true;
  211. terrainGraphics.resize(10);
  212. for (int i = 0; i < 10 ; i++)
  213. {
  214. CDefHandler *hlp = CDefHandler::giveDef(nameFromType(i));
  215. terrainGraphics[i].resize(hlp->ourImages.size());
  216. hlp->notFreeImgs = true;
  217. for(size_t j=0; j < hlp->ourImages.size(); ++j)
  218. terrainGraphics[i][j] = hlp->ourImages[j].bitmap;
  219. delete hlp;
  220. }
  221. for (int i=0-frameW; i<sizes.x+frameW; i++) //by width
  222. {
  223. for (int j=0-frameH; j<sizes.y+frameH;j++) //by height
  224. {
  225. for(int k=0; k<sizes.z; ++k) //by levles
  226. {
  227. if(i < 0 || i > (sizes.x-1) || j < 0 || j > (sizes.y-1))
  228. {
  229. int terBitmapNum = -1;
  230. auto & rand = CRandomGenerator::getDefault();
  231. if(i==-1 && j==-1)
  232. terBitmapNum = 16;
  233. else if(i==-1 && j==(sizes.y))
  234. terBitmapNum = 19;
  235. else if(i==(sizes.x) && j==-1)
  236. terBitmapNum = 17;
  237. else if(i==(sizes.x) && j==(sizes.y))
  238. terBitmapNum = 18;
  239. else if(j == -1 && i > -1 && i < sizes.x)
  240. terBitmapNum = rand.nextInt(22, 23);
  241. else if(i == -1 && j > -1 && j < sizes.y)
  242. terBitmapNum = rand.nextInt(33, 34);
  243. else if(j == sizes.y && i >-1 && i < sizes.x)
  244. terBitmapNum = rand.nextInt(29, 30);
  245. else if(i == sizes.x && j > -1 && j < sizes.y)
  246. terBitmapNum = rand.nextInt(25, 26);
  247. else
  248. terBitmapNum = rand.nextInt(15);
  249. if(terBitmapNum != -1)
  250. {
  251. ttiles[i][j][k].terbitmap = bord->ourImages[terBitmapNum].bitmap;
  252. continue;
  253. }
  254. }
  255. }
  256. }
  257. }
  258. delete bord;
  259. }
  260. void CMapHandler::initObjectRects()
  261. {
  262. //initializing objects / rects
  263. for(auto & elem : map->objects)
  264. {
  265. const CGObjectInstance *obj = elem;
  266. if( !obj
  267. || (obj->ID==Obj::HERO && static_cast<const CGHeroInstance*>(obj)->inTownGarrison) //garrisoned hero
  268. || (obj->ID==Obj::BOAT && static_cast<const CGBoat*>(obj)->hero)) //boat with hero (hero graphics is used)
  269. {
  270. continue;
  271. }
  272. std::shared_ptr<CAnimation> animation = graphics->getAnimation(obj);
  273. //no animation at all
  274. if(!animation)
  275. continue;
  276. //empty animation
  277. if(animation->size(0) == 0)
  278. continue;
  279. IImage * image = animation->getImage(0,0);
  280. for(int fx=0; fx < obj->getWidth(); ++fx)
  281. {
  282. for(int fy=0; fy < obj->getHeight(); ++fy)
  283. {
  284. int3 currTile(obj->pos.x - fx, obj->pos.y - fy, obj->pos.z);
  285. SDL_Rect cr;
  286. cr.w = 32;
  287. cr.h = 32;
  288. cr.x = image->width() - fx * 32 - 32;
  289. cr.y = image->height() - fy * 32 - 32;
  290. TerrainTileObject toAdd(obj,cr);
  291. if( map->isInTheMap(currTile) && // within map
  292. cr.x + cr.w > 0 && // image has data on this tile
  293. cr.y + cr.h > 0 &&
  294. obj->coveringAt(currTile.x, currTile.y) // object is visible here
  295. )
  296. {
  297. ttiles[currTile.x][currTile.y][currTile.z].objects.push_back(toAdd);
  298. }
  299. }
  300. }
  301. }
  302. for(int ix=0; ix<ttiles.size()-frameW; ++ix)
  303. {
  304. for(int iy=0; iy<ttiles[0].size()-frameH; ++iy)
  305. {
  306. for(int iz=0; iz<ttiles[0][0].size(); ++iz)
  307. {
  308. stable_sort(ttiles[ix][iy][iz].objects.begin(), ttiles[ix][iy][iz].objects.end(), objectBlitOrderSorter);
  309. }
  310. }
  311. }
  312. }
  313. void CMapHandler::init()
  314. {
  315. CStopWatch th;
  316. th.getDiff();
  317. // Size of visible terrain.
  318. int mapW = conf.go()->ac.advmapW;
  319. int mapH = conf.go()->ac.advmapH;
  320. //sizes of terrain
  321. sizes.x = map->width;
  322. sizes.y = map->height;
  323. sizes.z = map->twoLevel ? 2 : 1;
  324. // Total number of visible tiles. Subtract the center tile, then
  325. // compute the number of tiles on each side, and reassemble.
  326. int t1, t2;
  327. t1 = (mapW-32)/2;
  328. t2 = mapW - 32 - t1;
  329. tilesW = 1 + (t1+31)/32 + (t2+31)/32;
  330. t1 = (mapH-32)/2;
  331. t2 = mapH - 32 - t1;
  332. tilesH = 1 + (t1+31)/32 + (t2+31)/32;
  333. // Size of the frame around the map. In extremes positions, the
  334. // frame must not be on the center of the map, but right on the
  335. // edge of the center tile.
  336. frameW = (mapW+31) /32 / 2;
  337. frameH = (mapH+31) /32 / 2;
  338. offsetX = (mapW - (2*frameW+1)*32)/2;
  339. offsetY = (mapH - (2*frameH+1)*32)/2;
  340. prepareFOWDefs();
  341. roadsRiverTerrainInit(); //road's and river's DefHandlers; and simple values initialization
  342. borderAndTerrainBitmapInit();
  343. logGlobal->infoStream()<<"\tPreparing FoW, roads, rivers,borders: "<<th.getDiff();
  344. initObjectRects();
  345. logGlobal->infoStream()<<"\tMaking object rects: "<<th.getDiff();
  346. }
  347. CMapHandler::CMapBlitter *CMapHandler::resolveBlitter(const MapDrawingInfo * info) const
  348. {
  349. if (info->scaled)
  350. return worldViewBlitter;
  351. if (info->puzzleMode)
  352. return puzzleViewBlitter;
  353. return normalBlitter;
  354. }
  355. void CMapHandler::CMapNormalBlitter::drawElement(EMapCacheType cacheType, SDL_Surface * sourceSurf, SDL_Rect * sourceRect, SDL_Surface * targetSurf, SDL_Rect * destRect, bool alphaBlit, ui8 rotationInfo) const
  356. {
  357. if (rotationInfo != 0)
  358. {
  359. if (!sourceRect)
  360. {
  361. Rect sourceRect2(0, 0, sourceSurf->w, sourceSurf->h);
  362. if (alphaBlit)
  363. CSDL_Ext::getBlitterWithRotationAndAlpha(targetSurf)(sourceSurf, sourceRect2, targetSurf, *destRect, rotationInfo);
  364. else
  365. CSDL_Ext::getBlitterWithRotation(targetSurf)(sourceSurf, sourceRect2, targetSurf, *destRect, rotationInfo);
  366. }
  367. else
  368. {
  369. if (alphaBlit)
  370. CSDL_Ext::getBlitterWithRotationAndAlpha(targetSurf)(sourceSurf, *sourceRect, targetSurf, *destRect, rotationInfo);
  371. else
  372. CSDL_Ext::getBlitterWithRotation(targetSurf)(sourceSurf, *sourceRect, targetSurf, *destRect, rotationInfo);
  373. }
  374. }
  375. else
  376. {
  377. if (alphaBlit)
  378. CSDL_Ext::blit8bppAlphaTo24bpp(sourceSurf, sourceRect, targetSurf, destRect);
  379. else
  380. CSDL_Ext::blitSurface(sourceSurf, sourceRect, targetSurf, destRect);
  381. }
  382. }
  383. void CMapHandler::CMapNormalBlitter::drawElement(EMapCacheType cacheType, const IImage * source, SDL_Rect * sourceRect, SDL_Surface * targetSurf, SDL_Rect * destRect) const
  384. {
  385. source->draw(targetSurf, destRect, sourceRect);
  386. }
  387. void CMapHandler::CMapNormalBlitter::init(const MapDrawingInfo * drawingInfo)
  388. {
  389. info = drawingInfo;
  390. // Width and height of the portion of the map to process. Units in tiles.
  391. tileCount.x = parent->tilesW;
  392. tileCount.y = parent->tilesH;
  393. topTile = info->topTile;
  394. initPos.x = parent->offsetX + info->drawBounds->x;
  395. initPos.y = parent->offsetY + info->drawBounds->y;
  396. realTileRect = Rect(initPos.x, initPos.y, tileSize, tileSize);
  397. // If moving, we need to add an extra column/line
  398. if (info->movement.x != 0)
  399. {
  400. tileCount.x++;
  401. initPos.x += info->movement.x;
  402. if (info->movement.x > 0)
  403. {
  404. // Moving right. We still need to draw the old tile on the
  405. // left, so adjust our referential
  406. topTile.x--;
  407. initPos.x -= tileSize;
  408. }
  409. }
  410. if (info->movement.y != 0)
  411. {
  412. tileCount.y++;
  413. initPos.y += info->movement.y;
  414. if (info->movement.y > 0)
  415. {
  416. // Moving down. We still need to draw the tile on the top,
  417. // so adjust our referential.
  418. topTile.y--;
  419. initPos.y -= tileSize;
  420. }
  421. }
  422. // Reduce sizes if we go out of the full map.
  423. if (topTile.x < -parent->frameW)
  424. topTile.x = -parent->frameW;
  425. if (topTile.y < -parent->frameH)
  426. topTile.y = -parent->frameH;
  427. if (topTile.x + tileCount.x > parent->sizes.x + parent->frameW)
  428. tileCount.x = parent->sizes.x + parent->frameW - topTile.x;
  429. if (topTile.y + tileCount.y > parent->sizes.y + parent->frameH)
  430. tileCount.y = parent->sizes.y + parent->frameH - topTile.y;
  431. }
  432. SDL_Rect CMapHandler::CMapNormalBlitter::clip(SDL_Surface * targetSurf) const
  433. {
  434. SDL_Rect prevClip;
  435. SDL_GetClipRect(targetSurf, &prevClip);
  436. SDL_SetClipRect(targetSurf, info->drawBounds);
  437. return prevClip;
  438. }
  439. CMapHandler::CMapNormalBlitter::CMapNormalBlitter(CMapHandler * parent)
  440. : CMapBlitter(parent)
  441. {
  442. tileSize = 32;
  443. halfTileSizeCeil = 16;
  444. defaultTileRect = Rect(0, 0, tileSize, tileSize);
  445. }
  446. IImage * CMapHandler::CMapWorldViewBlitter::objectToIcon(Obj id, si32 subId, PlayerColor owner) const
  447. {
  448. int ownerIndex = 0;
  449. if(owner < PlayerColor::PLAYER_LIMIT)
  450. {
  451. ownerIndex = owner.getNum() * 19;
  452. }
  453. else if (owner == PlayerColor::NEUTRAL)
  454. {
  455. ownerIndex = PlayerColor::PLAYER_LIMIT.getNum() * 19;
  456. }
  457. switch(id)
  458. {
  459. case Obj::MONOLITH_ONE_WAY_ENTRANCE:
  460. case Obj::MONOLITH_ONE_WAY_EXIT:
  461. case Obj::MONOLITH_TWO_WAY:
  462. return info->icons->getImage((int)EWorldViewIcon::TELEPORT);
  463. case Obj::SUBTERRANEAN_GATE:
  464. return info->icons->getImage((int)EWorldViewIcon::GATE);
  465. case Obj::ARTIFACT:
  466. return info->icons->getImage((int)EWorldViewIcon::ARTIFACT);
  467. case Obj::TOWN:
  468. return info->icons->getImage((int)EWorldViewIcon::TOWN + ownerIndex);
  469. case Obj::HERO:
  470. return info->icons->getImage((int)EWorldViewIcon::HERO + ownerIndex);
  471. case Obj::MINE:
  472. return info->icons->getImage((int)EWorldViewIcon::MINE_WOOD + subId + ownerIndex);
  473. case Obj::RESOURCE:
  474. return info->icons->getImage((int)EWorldViewIcon::RES_WOOD + subId + ownerIndex);
  475. }
  476. return nullptr;
  477. }
  478. void CMapHandler::CMapWorldViewBlitter::calculateWorldViewCameraPos()
  479. {
  480. bool outsideLeft = topTile.x < 0;
  481. bool outsideTop = topTile.y < 0;
  482. bool outsideRight = std::max(0, topTile.x) + tileCount.x > parent->sizes.x;
  483. bool outsideBottom = std::max(0, topTile.y) + tileCount.y > parent->sizes.y;
  484. if (tileCount.x > parent->sizes.x)
  485. topTile.x = parent->sizes.x / 2 - tileCount.x / 2; // center viewport if the whole map can fit into the screen at once
  486. else if (outsideLeft)
  487. {
  488. if (outsideRight)
  489. topTile.x = parent->sizes.x / 2 - tileCount.x / 2;
  490. else
  491. topTile.x = 0;
  492. }
  493. else if (outsideRight)
  494. topTile.x = parent->sizes.x - tileCount.x;
  495. if (tileCount.y > parent->sizes.y)
  496. topTile.y = parent->sizes.y / 2 - tileCount.y / 2;
  497. else if (outsideTop)
  498. {
  499. if (outsideBottom)
  500. topTile.y = parent->sizes.y / 2 - tileCount.y / 2;
  501. else
  502. topTile.y = 0;
  503. }
  504. else if (outsideBottom)
  505. topTile.y = parent->sizes.y - tileCount.y;
  506. }
  507. void CMapHandler::CMapWorldViewBlitter::drawScaledRotatedElement(EMapCacheType type, SDL_Surface * baseSurf, SDL_Surface * targetSurf, ui8 rotation,
  508. float scale, SDL_Rect * dstRect, SDL_Rect * srcRect /*= nullptr*/) const
  509. {
  510. auto key = parent->cache.genKey((intptr_t)baseSurf, rotation);
  511. auto scaledSurf = parent->cache.requestWorldViewCache(type, key);
  512. if (scaledSurf) // blitting from cache
  513. {
  514. if (srcRect)
  515. {
  516. dstRect->w = srcRect->w;
  517. dstRect->h = srcRect->h;
  518. }
  519. CSDL_Ext::blitSurface(scaledSurf, srcRect, targetSurf, dstRect);
  520. }
  521. else // creating new
  522. {
  523. auto baseSurfRotated = CSDL_Ext::newSurface(baseSurf->w, baseSurf->h);
  524. if (!baseSurfRotated)
  525. return;
  526. Rect baseRect(0, 0, baseSurf->w, baseSurf->h);
  527. CSDL_Ext::getBlitterWithRotationAndAlpha(targetSurf)(baseSurf, baseRect, baseSurfRotated, baseRect, rotation);
  528. SDL_Surface * scaledSurf2 = CSDL_Ext::scaleSurfaceFast(baseSurfRotated, baseSurf->w * scale, baseSurf->h * scale);
  529. CSDL_Ext::blitSurface(scaledSurf2, srcRect, targetSurf, dstRect);
  530. parent->cache.cacheWorldViewEntry(type, key, scaledSurf2);
  531. }
  532. }
  533. void CMapHandler::CMapWorldViewBlitter::drawElement(EMapCacheType cacheType, SDL_Surface * sourceSurf, SDL_Rect * sourceRect, SDL_Surface * targetSurf, SDL_Rect * destRect, bool alphaBlit, ui8 rotationInfo) const
  534. {
  535. if (rotationInfo != 0)
  536. {
  537. drawScaledRotatedElement(cacheType, sourceSurf, targetSurf, rotationInfo, info->scale, destRect, sourceRect);
  538. }
  539. else
  540. {
  541. auto scaledSurf = parent->cache.requestWorldViewCacheOrCreate(cacheType, (intptr_t) sourceSurf, sourceSurf, info->scale);
  542. if (alphaBlit)
  543. CSDL_Ext::blit8bppAlphaTo24bpp(scaledSurf, sourceRect, targetSurf, destRect);
  544. else
  545. CSDL_Ext::blitSurface(scaledSurf, sourceRect, targetSurf, destRect);
  546. }
  547. }
  548. void CMapHandler::CMapWorldViewBlitter::drawElement(EMapCacheType cacheType, const IImage * source, SDL_Rect * sourceRect, SDL_Surface * targetSurf, SDL_Rect * destRect) const
  549. {
  550. SDL_Surface * scaledSurf = parent->cache.requestWorldViewCacheOrCreate(cacheType, (intptr_t) source, source, info->scale);
  551. if(scaledSurf->format->BitsPerPixel == 8)
  552. CSDL_Ext::blit8bppAlphaTo24bpp(scaledSurf, sourceRect, targetSurf, destRect);
  553. else
  554. CSDL_Ext::blitSurface(scaledSurf, sourceRect, targetSurf, destRect);
  555. }
  556. void CMapHandler::CMapWorldViewBlitter::drawTileOverlay(SDL_Surface * targetSurf, const TerrainTile2 & tile) const
  557. {
  558. auto drawIcon = [this,targetSurf](Obj id, si32 subId, PlayerColor owner)
  559. {
  560. IImage * wvIcon = this->objectToIcon(id, subId, owner);
  561. if(nullptr != wvIcon)
  562. {
  563. // centering icon on the object
  564. Point dest(realPos.x + tileSize / 2 - wvIcon->width() / 2, realPos.y + tileSize / 2 - wvIcon->height() / 2);
  565. wvIcon->draw(targetSurf, dest.x, dest.y);
  566. }
  567. };
  568. auto & objects = tile.objects;
  569. for(auto & object : objects)
  570. {
  571. const CGObjectInstance * obj = object.obj;
  572. const bool sameLevel = obj->pos.z == pos.z;
  573. const bool isVisible = (*info->visibilityMap)[pos.x][pos.y][pos.z];
  574. const bool isVisitable = obj->visitableAt(pos.x, pos.y);
  575. if(sameLevel && isVisible && isVisitable)
  576. drawIcon(obj->ID, obj->subID, obj->tempOwner);
  577. }
  578. }
  579. void CMapHandler::CMapWorldViewBlitter::drawOverlayEx(SDL_Surface * targetSurf)
  580. {
  581. if(nullptr == info->additionalIcons)
  582. return;
  583. const int3 bottomRight = pos + tileCount;
  584. for(const ObjectPosInfo & iconInfo : *(info->additionalIcons))
  585. {
  586. if( iconInfo.pos.z != pos.z)
  587. continue;
  588. if((iconInfo.pos.x < topTile.x) || (iconInfo.pos.y < topTile.y))
  589. continue;
  590. if((iconInfo.pos.x > bottomRight.x) || (iconInfo.pos.y > bottomRight.y))
  591. continue;
  592. realPos.x = initPos.x + (iconInfo.pos.x - topTile.x) * tileSize;
  593. realPos.y = initPos.x + (iconInfo.pos.y - topTile.y) * tileSize;
  594. IImage * wvIcon = this->objectToIcon(iconInfo.id, iconInfo.subId, iconInfo.owner);
  595. if(nullptr != wvIcon)
  596. {
  597. // centering icon on the object
  598. Point dest(realPos.x + tileSize / 2 - wvIcon->width() / 2, realPos.y + tileSize / 2 - wvIcon->height() / 2);
  599. wvIcon->draw(targetSurf, dest.x, dest.y);
  600. }
  601. }
  602. }
  603. void CMapHandler::CMapWorldViewBlitter::drawHeroFlag(SDL_Surface * targetSurf, const IImage * source, SDL_Rect * sourceRect, SDL_Rect * destRect, bool moving) const
  604. {
  605. if (moving)
  606. return;
  607. CMapBlitter::drawHeroFlag(targetSurf, source, sourceRect, destRect, false);
  608. }
  609. void CMapHandler::CMapWorldViewBlitter::drawObject(SDL_Surface * targetSurf, const IImage * source, SDL_Rect * sourceRect, bool moving) const
  610. {
  611. if (moving)
  612. return;
  613. Rect scaledSourceRect(sourceRect->x * info->scale, sourceRect->y * info->scale, sourceRect->w, sourceRect->h);
  614. CMapBlitter::drawObject(targetSurf, source, &scaledSourceRect, false);
  615. }
  616. void CMapHandler::CMapBlitter::drawTileTerrain(SDL_Surface * targetSurf, const TerrainTile & tinfo, const TerrainTile2 & tile) const
  617. {
  618. Rect destRect(realTileRect);
  619. if(tile.terbitmap) //if custom terrain graphic - use it
  620. drawElement(EMapCacheType::TERRAIN_CUSTOM, tile.terbitmap, nullptr, targetSurf, &destRect);
  621. else //use default terrain graphic
  622. drawElement(EMapCacheType::TERRAIN, parent->terrainGraphics[tinfo.terType][tinfo.terView],
  623. nullptr, targetSurf, &destRect, false, tinfo.extTileFlags % 4);
  624. }
  625. void CMapHandler::CMapWorldViewBlitter::init(const MapDrawingInfo * drawingInfo)
  626. {
  627. info = drawingInfo;
  628. parent->cache.updateWorldViewScale(info->scale);
  629. topTile = info->topTile;
  630. tileSize = (int) floorf(32.0f * info->scale);
  631. halfTileSizeCeil = (int)ceilf(tileSize / 2.0f);
  632. tileCount.x = (int) ceilf((float)info->drawBounds->w / tileSize);
  633. tileCount.y = (int) ceilf((float)info->drawBounds->h / tileSize);
  634. initPos.x = info->drawBounds->x;
  635. initPos.y = info->drawBounds->y;
  636. realTileRect = Rect(initPos.x, initPos.y, tileSize, tileSize);
  637. defaultTileRect = Rect(0, 0, tileSize, tileSize);
  638. calculateWorldViewCameraPos();
  639. }
  640. SDL_Rect CMapHandler::CMapWorldViewBlitter::clip(SDL_Surface * targetSurf) const
  641. {
  642. SDL_Rect prevClip;
  643. SDL_FillRect(targetSurf, info->drawBounds, SDL_MapRGB(targetSurf->format, 0, 0, 0));
  644. // makes the clip area smaller if the map is smaller than the screen frame
  645. // (actually, it could be made 1 tile bigger so that overlay icons on edge tiles could be drawn partly outside)
  646. Rect clipRect(std::max<int>(info->drawBounds->x, info->drawBounds->x - topTile.x * tileSize),
  647. std::max<int>(info->drawBounds->y, info->drawBounds->y - topTile.y * tileSize),
  648. std::min<int>(info->drawBounds->w, parent->sizes.x * tileSize),
  649. std::min<int>(info->drawBounds->h, parent->sizes.y * tileSize));
  650. SDL_GetClipRect(targetSurf, &prevClip);
  651. SDL_SetClipRect(targetSurf, &clipRect); //preventing blitting outside of that rect
  652. return prevClip;
  653. }
  654. CMapHandler::CMapWorldViewBlitter::CMapWorldViewBlitter(CMapHandler * parent)
  655. : CMapBlitter(parent)
  656. {
  657. }
  658. void CMapHandler::CMapPuzzleViewBlitter::drawObjects(SDL_Surface * targetSurf, const TerrainTile2 & tile) const
  659. {
  660. CMapBlitter::drawObjects(targetSurf, tile);
  661. // grail X mark
  662. if(pos.x == info->grailPos.x && pos.y == info->grailPos.y)
  663. {
  664. const IImage * mark = graphics->heroMoveArrows->getImage(0);
  665. mark->draw(targetSurf,realTileRect.x,realTileRect.y);
  666. }
  667. }
  668. void CMapHandler::CMapPuzzleViewBlitter::postProcessing(SDL_Surface * targetSurf) const
  669. {
  670. CSDL_Ext::applyEffect(targetSurf, info->drawBounds, static_cast<int>(!ADVOPT.puzzleSepia));
  671. }
  672. bool CMapHandler::CMapPuzzleViewBlitter::canDrawObject(const CGObjectInstance * obj) const
  673. {
  674. if (!CMapBlitter::canDrawObject(obj))
  675. return false;
  676. //don't print flaggable objects in puzzle mode
  677. if (obj->isVisitable())
  678. return false;
  679. if(std::find(unblittableObjects.begin(), unblittableObjects.end(), obj->ID) != unblittableObjects.end())
  680. return false;
  681. return true;
  682. }
  683. CMapHandler::CMapPuzzleViewBlitter::CMapPuzzleViewBlitter(CMapHandler * parent)
  684. : CMapNormalBlitter(parent)
  685. {
  686. unblittableObjects.push_back(Obj::HOLE);
  687. }
  688. void CMapHandler::CMapBlitter::drawFrame(SDL_Surface * targetSurf) const
  689. {
  690. Rect destRect(realTileRect);
  691. drawElement(EMapCacheType::FRAME, parent->ttiles[pos.x][pos.y][topTile.z].terbitmap, nullptr, targetSurf, &destRect);
  692. }
  693. void CMapHandler::CMapBlitter::drawOverlayEx(SDL_Surface * targetSurf)
  694. {
  695. //nothing to do here
  696. }
  697. void CMapHandler::CMapBlitter::drawHeroFlag(SDL_Surface * targetSurf, const IImage * source, SDL_Rect * sourceRect, SDL_Rect * destRect, bool moving) const
  698. {
  699. drawElement(EMapCacheType::HERO_FLAGS, source, sourceRect, targetSurf, destRect);
  700. }
  701. void CMapHandler::CMapBlitter::drawObject(SDL_Surface * targetSurf, const IImage * source, SDL_Rect * sourceRect, bool moving) const
  702. {
  703. Rect dstRect(realTileRect);
  704. drawElement(EMapCacheType::OBJECTS, source, sourceRect, targetSurf, &dstRect);
  705. }
  706. void CMapHandler::CMapBlitter::drawObjects(SDL_Surface * targetSurf, const TerrainTile2 & tile) const
  707. {
  708. auto & objects = tile.objects;
  709. for(auto & object : objects)
  710. {
  711. if (object.fadeAnimKey >= 0)
  712. {
  713. auto fadeIter = parent->fadeAnims.find(object.fadeAnimKey);
  714. if (fadeIter != parent->fadeAnims.end())
  715. {
  716. // this object is currently fading, so skip normal drawing
  717. Rect r2(realTileRect);
  718. CFadeAnimation * fade = (*fadeIter).second.second;
  719. fade->draw(targetSurf, nullptr, &r2);
  720. continue;
  721. }
  722. logGlobal->errorStream() << "Fading map object with missing fade anim : " << object.fadeAnimKey;
  723. continue;
  724. }
  725. const CGObjectInstance * obj = object.obj;
  726. if (!obj)
  727. {
  728. logGlobal->errorStream() << "Stray map object that isn't fading";
  729. continue;
  730. }
  731. if (!canDrawObject(obj))
  732. continue;
  733. auto objData = findObjectBitmap(obj, info->anim);
  734. if (objData.objBitmap)
  735. {
  736. Rect srcRect(object.rect.x, object.rect.y, tileSize, tileSize);
  737. drawObject(targetSurf, objData.objBitmap, &srcRect, objData.isMoving);
  738. if (objData.flagBitmap)
  739. {
  740. if (objData.isMoving)
  741. {
  742. srcRect.y += FRAMES_PER_MOVE_ANIM_GROUP * 2 - tileSize;
  743. Rect dstRect(realPos.x, realPos.y - tileSize / 2, tileSize, tileSize);
  744. drawHeroFlag(targetSurf, objData.flagBitmap, &srcRect, &dstRect, true);
  745. }
  746. else if (obj->pos.x == pos.x && obj->pos.y == pos.y)
  747. {
  748. Rect dstRect(realPos.x - 2 * tileSize, realPos.y - tileSize, 3 * tileSize, 2 * tileSize);
  749. drawHeroFlag(targetSurf, objData.flagBitmap, nullptr, &dstRect, false);
  750. }
  751. }
  752. }
  753. }
  754. }
  755. void CMapHandler::CMapBlitter::drawRoad(SDL_Surface * targetSurf, const TerrainTile & tinfo, const TerrainTile * tinfoUpper) const
  756. {
  757. if (tinfoUpper && tinfoUpper->roadType != ERoadType::NO_ROAD)
  758. {
  759. Rect source(0, tileSize / 2, tileSize, tileSize / 2);
  760. Rect dest(realPos.x, realPos.y, tileSize, tileSize / 2);
  761. drawElement(EMapCacheType::ROADS, parent->roadDefs[tinfoUpper->roadType - 1]->ourImages[tinfoUpper->roadDir].bitmap,
  762. &source, targetSurf, &dest, true, (tinfoUpper->extTileFlags >> 4) % 4);
  763. }
  764. if(tinfo.roadType != ERoadType::NO_ROAD) //print road from this tile
  765. {
  766. Rect source(0, 0, tileSize, halfTileSizeCeil);
  767. Rect dest(realPos.x, realPos.y + tileSize / 2, tileSize, tileSize / 2);
  768. drawElement(EMapCacheType::ROADS, parent->roadDefs[tinfo.roadType - 1]->ourImages[tinfo.roadDir].bitmap,
  769. &source, targetSurf, &dest, true, (tinfo.extTileFlags >> 4) % 4);
  770. }
  771. }
  772. void CMapHandler::CMapBlitter::drawRiver(SDL_Surface * targetSurf, const TerrainTile & tinfo) const
  773. {
  774. Rect destRect(realTileRect);
  775. drawElement(EMapCacheType::RIVERS, parent->staticRiverDefs[tinfo.riverType-1]->ourImages[tinfo.riverDir].bitmap,
  776. nullptr, targetSurf, &destRect, true, (tinfo.extTileFlags >> 2) % 4);
  777. }
  778. void CMapHandler::CMapBlitter::drawFow(SDL_Surface * targetSurf) const
  779. {
  780. Rect destRect(realTileRect);
  781. std::pair<SDL_Surface *, bool> hide = getVisBitmap();
  782. drawElement(EMapCacheType::FOW, hide.first, nullptr, targetSurf, &destRect, hide.second);
  783. }
  784. void CMapHandler::CMapBlitter::blit(SDL_Surface * targetSurf, const MapDrawingInfo * info)
  785. {
  786. init(info);
  787. auto prevClip = clip(targetSurf);
  788. pos = int3(0, 0, topTile.z);
  789. for (realPos.x = initPos.x, pos.x = topTile.x; pos.x < topTile.x + tileCount.x; pos.x++, realPos.x += tileSize)
  790. {
  791. if (pos.x < 0 || pos.x >= parent->sizes.x)
  792. continue;
  793. for (realPos.y = initPos.y, pos.y = topTile.y; pos.y < topTile.y + tileCount.y; pos.y++, realPos.y += tileSize)
  794. {
  795. if (pos.y < 0 || pos.y >= parent->sizes.y)
  796. continue;
  797. const bool isVisible = canDrawCurrentTile();
  798. realTileRect.x = realPos.x;
  799. realTileRect.y = realPos.y;
  800. const TerrainTile2 & tile = parent->ttiles[pos.x][pos.y][pos.z];
  801. const TerrainTile & tinfo = parent->map->getTile(pos);
  802. const TerrainTile * tinfoUpper = pos.y > 0 ? &parent->map->getTile(int3(pos.x, pos.y - 1, pos.z)) : nullptr;
  803. if(isVisible || info->showAllTerrain)
  804. {
  805. drawTileTerrain(targetSurf, tinfo, tile);
  806. if (tinfo.riverType)
  807. drawRiver(targetSurf, tinfo);
  808. drawRoad(targetSurf, tinfo, tinfoUpper);
  809. }
  810. if(isVisible)
  811. drawObjects(targetSurf, tile);
  812. }
  813. }
  814. for (realPos.x = initPos.x, pos.x = topTile.x; pos.x < topTile.x + tileCount.x; pos.x++, realPos.x += tileSize)
  815. {
  816. for (realPos.y = initPos.y, pos.y = topTile.y; pos.y < topTile.y + tileCount.y; pos.y++, realPos.y += tileSize)
  817. {
  818. realTileRect.x = realPos.x;
  819. realTileRect.y = realPos.y;
  820. if (pos.x < 0 || pos.x >= parent->sizes.x ||
  821. pos.y < 0 || pos.y >= parent->sizes.y)
  822. {
  823. drawFrame(targetSurf);
  824. }
  825. else
  826. {
  827. const TerrainTile2 & tile = parent->ttiles[pos.x][pos.y][pos.z];
  828. if (!(*info->visibilityMap)[pos.x][pos.y][topTile.z] && !info->showAllTerrain)
  829. drawFow(targetSurf);
  830. // overlay needs to be drawn over fow, because of artifacts-aura-like spells
  831. drawTileOverlay(targetSurf, tile);
  832. // drawDebugVisitables()
  833. if (settings["session"]["showBlock"].Bool())
  834. {
  835. if(parent->map->getTile(int3(pos.x, pos.y, pos.z)).blocked) //temporary hiding blocked positions
  836. {
  837. static SDL_Surface * block = nullptr;
  838. if (!block)
  839. block = BitmapHandler::loadBitmap("blocked");
  840. CSDL_Ext::blitSurface(block, nullptr, targetSurf, &realTileRect);
  841. }
  842. }
  843. if (settings["session"]["showVisit"].Bool())
  844. {
  845. if(parent->map->getTile(int3(pos.x, pos.y, pos.z)).visitable) //temporary hiding visitable positions
  846. {
  847. static SDL_Surface * visit = nullptr;
  848. if (!visit)
  849. visit = BitmapHandler::loadBitmap("visitable");
  850. CSDL_Ext::blitSurface(visit, nullptr, targetSurf, &realTileRect);
  851. }
  852. }
  853. }
  854. }
  855. }
  856. drawOverlayEx(targetSurf);
  857. // drawDebugGrid()
  858. if (settings["session"]["showGrid"].Bool())
  859. {
  860. for (realPos.x = initPos.x, pos.x = topTile.x; pos.x < topTile.x + tileCount.x; pos.x++, realPos.x += tileSize)
  861. {
  862. for (realPos.y = initPos.y, pos.y = topTile.y; pos.y < topTile.y + tileCount.y; pos.y++, realPos.y += tileSize)
  863. {
  864. const int3 color(0x555555, 0x555555, 0x555555);
  865. if (realPos.y >= info->drawBounds->y &&
  866. realPos.y < info->drawBounds->y + info->drawBounds->h)
  867. for(int i = 0; i < tileSize; i++)
  868. if (realPos.x + i >= info->drawBounds->x &&
  869. realPos.x + i < info->drawBounds->x + info->drawBounds->w)
  870. CSDL_Ext::SDL_PutPixelWithoutRefresh(targetSurf, realPos.x + i, realPos.y, color.x, color.y, color.z);
  871. if (realPos.x >= info->drawBounds->x &&
  872. realPos.x < info->drawBounds->x + info->drawBounds->w)
  873. for(int i = 0; i < tileSize; i++)
  874. if (realPos.y + i >= info->drawBounds->y &&
  875. realPos.y + i < info->drawBounds->y + info->drawBounds->h)
  876. CSDL_Ext::SDL_PutPixelWithoutRefresh(targetSurf, realPos.x, realPos.y + i, color.x, color.y, color.z);
  877. }
  878. }
  879. }
  880. postProcessing(targetSurf);
  881. SDL_SetClipRect(targetSurf, &prevClip);
  882. }
  883. CMapHandler::AnimBitmapHolder CMapHandler::CMapBlitter::findHeroBitmap(const CGHeroInstance * hero, int anim) const
  884. {
  885. if(hero && hero->moveDir && hero->type) //it's hero or boat
  886. {
  887. if(hero->tempOwner >= PlayerColor::PLAYER_LIMIT) //Neutral hero?
  888. {
  889. logGlobal->errorStream() << "A neutral hero (" << hero->name << ") at " << hero->pos << ". Should not happen!";
  890. return CMapHandler::AnimBitmapHolder();
  891. }
  892. //pick graphics of hero (or boat if hero is sailing)
  893. std::shared_ptr<CAnimation> animation;
  894. if (hero->boat)
  895. animation = graphics->boatAnimations[hero->boat->subID];
  896. else
  897. animation = graphics->heroAnimations[hero->appearance.animationFile];
  898. bool moving = !hero->isStanding;
  899. int group = getHeroFrameGroup(hero->moveDir, moving);
  900. if(animation->size(group) > 0)
  901. {
  902. int frame = anim % animation->size(group);
  903. IImage * heroImage = animation->getImage(frame, group);
  904. //get flag overlay only if we have main image
  905. IImage * flagImage = findFlagBitmap(hero, anim, &hero->tempOwner, group);
  906. return CMapHandler::AnimBitmapHolder(heroImage, flagImage, moving);
  907. }
  908. }
  909. return CMapHandler::AnimBitmapHolder();
  910. }
  911. CMapHandler::AnimBitmapHolder CMapHandler::CMapBlitter::findBoatBitmap(const CGBoat * boat, int anim) const
  912. {
  913. auto animation = graphics->boatAnimations.at(boat->subID);
  914. int group = getHeroFrameGroup(boat->direction, false);
  915. if(animation->size(group) > 0)
  916. return CMapHandler::AnimBitmapHolder(animation->getImage(anim % animation->size(group), group));
  917. else
  918. return CMapHandler::AnimBitmapHolder();
  919. }
  920. IImage * CMapHandler::CMapBlitter::findFlagBitmap(const CGHeroInstance * hero, int anim, const PlayerColor * color, int group) const
  921. {
  922. if (!hero)
  923. return nullptr;
  924. if (hero->boat)
  925. return findBoatFlagBitmap(hero->boat, anim, color, group, hero->moveDir);
  926. return findHeroFlagBitmap(hero, anim, color, group);
  927. }
  928. IImage * CMapHandler::CMapBlitter::findHeroFlagBitmap(const CGHeroInstance * hero, int anim, const PlayerColor * color, int group) const
  929. {
  930. return findFlagBitmapInternal(graphics->heroFlagAnimations.at(color->getNum()), anim, group, hero->moveDir, !hero->isStanding);
  931. }
  932. IImage * CMapHandler::CMapBlitter::findBoatFlagBitmap(const CGBoat * boat, int anim, const PlayerColor * color, int group, ui8 dir) const
  933. {
  934. int boatType = boat->subID;
  935. if(boatType < 0 || boatType >= graphics->boatFlagAnimations.size())
  936. {
  937. logGlobal->errorStream() << "Not supported boat subtype: " << boat->subID;
  938. return nullptr;
  939. }
  940. const auto & subtypeFlags = graphics->boatFlagAnimations.at(boatType);
  941. int colorIndex = color->getNum();
  942. if(colorIndex < 0 || colorIndex >= subtypeFlags.size())
  943. {
  944. logGlobal->errorStream() << "Invalid player color " << colorIndex;
  945. return nullptr;
  946. }
  947. return findFlagBitmapInternal(subtypeFlags.at(colorIndex), anim, group, dir, false);
  948. }
  949. IImage * CMapHandler::CMapBlitter::findFlagBitmapInternal(std::shared_ptr<CAnimation> animation, int anim, int group, ui8 dir, bool moving) const
  950. {
  951. if(moving)
  952. return animation->getImage(anim % animation->size(group), group);
  953. else
  954. return animation->getImage((anim / 4) % animation->size(group), group);
  955. }
  956. CMapHandler::AnimBitmapHolder CMapHandler::CMapBlitter::findObjectBitmap(const CGObjectInstance * obj, int anim) const
  957. {
  958. if (!obj)
  959. return CMapHandler::AnimBitmapHolder();
  960. if (obj->ID == Obj::HERO)
  961. return findHeroBitmap(static_cast<const CGHeroInstance*>(obj), anim);
  962. if (obj->ID == Obj::BOAT)
  963. return findBoatBitmap(static_cast<const CGBoat*>(obj), anim);
  964. // normal object
  965. std::shared_ptr<CAnimation> animation = graphics->getAnimation(obj);
  966. IImage * bitmap = animation->getImage((anim + getPhaseShift(obj)) % animation->size());
  967. bitmap->setFlagColor(obj->tempOwner);
  968. return CMapHandler::AnimBitmapHolder(bitmap);
  969. }
  970. ui8 CMapHandler::CMapBlitter::getPhaseShift(const CGObjectInstance *object) const
  971. {
  972. auto i = parent->animationPhase.find(object);
  973. if(i == parent->animationPhase.end())
  974. {
  975. ui8 ret = CRandomGenerator::getDefault().nextInt(254);
  976. parent->animationPhase[object] = ret;
  977. return ret;
  978. }
  979. return i->second;
  980. }
  981. bool CMapHandler::CMapBlitter::canDrawObject(const CGObjectInstance * obj) const
  982. {
  983. //checking if object has non-empty graphic on this tile
  984. return obj->ID == Obj::HERO || obj->coveringAt(pos.x, pos.y);
  985. }
  986. bool CMapHandler::CMapBlitter::canDrawCurrentTile() const
  987. {
  988. const NeighborTilesInfo neighbors(pos, parent->sizes, *info->visibilityMap);
  989. return !neighbors.areAllHidden();
  990. }
  991. ui8 CMapHandler::CMapBlitter::getHeroFrameGroup(ui8 dir, bool isMoving) const
  992. {
  993. if(isMoving)
  994. {
  995. static const ui8 frame [] = {0xff, 10, 5, 6, 7, 8, 9, 12, 11};
  996. return frame[dir];
  997. }
  998. else //if(isMoving)
  999. {
  1000. static const ui8 frame [] = {0xff, 13, 0, 1, 2, 3, 4, 15, 14};
  1001. return frame[dir];
  1002. }
  1003. }
  1004. std::pair<SDL_Surface *, bool> CMapHandler::CMapBlitter::getVisBitmap() const
  1005. {
  1006. const NeighborTilesInfo neighborInfo(pos, parent->sizes, *info->visibilityMap);
  1007. int retBitmapID = neighborInfo.getBitmapID();// >=0 -> partial hide, <0 - full hide
  1008. if (retBitmapID < 0)
  1009. {
  1010. retBitmapID = - parent->hideBitmap[pos.x][pos.y][pos.z] - 1; //fully hidden
  1011. }
  1012. if (retBitmapID >= 0)
  1013. {
  1014. return std::make_pair(graphics->FoWpartialHide->ourImages[retBitmapID].bitmap, true);
  1015. }
  1016. else
  1017. {
  1018. return std::make_pair(graphics->FoWfullHide->ourImages[-retBitmapID - 1].bitmap, false);
  1019. }
  1020. }
  1021. bool CMapHandler::updateObjectsFade()
  1022. {
  1023. for (auto iter = fadeAnims.begin(); iter != fadeAnims.end(); )
  1024. {
  1025. int3 pos = (*iter).second.first;
  1026. CFadeAnimation * anim = (*iter).second.second;
  1027. anim->update();
  1028. if (anim->isFading())
  1029. ++iter;
  1030. else // fade finished
  1031. {
  1032. auto &objs = ttiles[pos.x][pos.y][pos.z].objects;
  1033. for (auto objIter = objs.begin(); objIter != objs.end(); ++objIter)
  1034. {
  1035. if ((*objIter).fadeAnimKey == (*iter).first)
  1036. {
  1037. logAnim->traceStream() << "Fade anim finished for obj at " << pos << "; remaining: " << (fadeAnims.size() - 1);
  1038. if (anim->fadingMode == CFadeAnimation::EMode::OUT)
  1039. objs.erase(objIter); // if this was fadeout, remove the object from the map
  1040. else
  1041. (*objIter).fadeAnimKey = -1; // for fadein, just remove its connection to the finished fade
  1042. break;
  1043. }
  1044. }
  1045. delete (*iter).second.second;
  1046. iter = fadeAnims.erase(iter);
  1047. }
  1048. }
  1049. return !fadeAnims.empty();
  1050. }
  1051. bool CMapHandler::startObjectFade(TerrainTileObject & obj, bool in, int3 pos)
  1052. {
  1053. SDL_Surface * fadeBitmap;
  1054. assert(obj.obj);
  1055. auto objData = normalBlitter->findObjectBitmap(obj.obj, 0);
  1056. if (objData.objBitmap)
  1057. {
  1058. if (objData.isMoving) // ignore fading of moving objects (for now?)
  1059. {
  1060. logAnim->debugStream() << "Ignoring fade of moving object";
  1061. return false;
  1062. }
  1063. fadeBitmap = CSDL_Ext::newSurface(32, 32); // TODO cache these bitmaps instead of creating new ones?
  1064. Rect objSrcRect(obj.rect.x, obj.rect.y, 32, 32);
  1065. objData.objBitmap->draw(fadeBitmap,0,0,&objSrcRect);
  1066. if (objData.flagBitmap)
  1067. {
  1068. if (obj.obj->pos.x - 1 == pos.x && obj.obj->pos.y - 1 == pos.y) // -1 to draw flag in top-center instead of right-bottom; kind of a hack
  1069. {
  1070. Rect flagSrcRect(32, 0, 32, 32);
  1071. objData.flagBitmap->draw(fadeBitmap,0,0, &flagSrcRect);
  1072. }
  1073. }
  1074. auto anim = new CFadeAnimation();
  1075. anim->init(in ? CFadeAnimation::EMode::IN : CFadeAnimation::EMode::OUT, fadeBitmap, true);
  1076. fadeAnims[++fadeAnimCounter] = std::pair<int3, CFadeAnimation*>(pos, anim);
  1077. obj.fadeAnimKey = fadeAnimCounter;
  1078. logAnim->traceStream() << "Fade anim started for obj " << obj.obj->ID
  1079. << " at " << pos << "; anim count: " << fadeAnims.size();
  1080. return true;
  1081. }
  1082. return false;
  1083. }
  1084. bool CMapHandler::printObject(const CGObjectInstance *obj, bool fadein /* = false */)
  1085. {
  1086. auto animation = graphics->getAnimation(obj);
  1087. if(!animation)
  1088. return false;
  1089. IImage * bitmap = animation->getImage(0);
  1090. if(!bitmap)
  1091. return false;
  1092. const int tilesW = bitmap->width()/32;
  1093. const int tilesH = bitmap->height()/32;
  1094. for(int fx=0; fx<tilesW; ++fx)
  1095. {
  1096. for(int fy=0; fy<tilesH; ++fy)
  1097. {
  1098. SDL_Rect cr;
  1099. cr.w = 32;
  1100. cr.h = 32;
  1101. cr.x = fx*32;
  1102. cr.y = fy*32;
  1103. TerrainTileObject toAdd(obj, cr);
  1104. if((obj->pos.x + fx - tilesW+1)>=0 && (obj->pos.x + fx - tilesW+1)<ttiles.size()-frameW && (obj->pos.y + fy - tilesH+1)>=0 && (obj->pos.y + fy - tilesH+1)<ttiles[0].size()-frameH)
  1105. {
  1106. int3 pos(obj->pos.x + fx - tilesW + 1, obj->pos.y + fy - tilesH + 1, obj->pos.z);
  1107. TerrainTile2 & curt = ttiles[pos.x][pos.y][pos.z];
  1108. if (fadein && ADVOPT.objectFading)
  1109. {
  1110. startObjectFade(toAdd, true, pos);
  1111. }
  1112. auto i = curt.objects.begin();
  1113. for(; i != curt.objects.end(); i++)
  1114. {
  1115. if(objectBlitOrderSorter(toAdd, *i))
  1116. {
  1117. curt.objects.insert(i, toAdd);
  1118. i = curt.objects.begin(); //to validate and avoid adding it second time
  1119. break;
  1120. }
  1121. }
  1122. if(i == curt.objects.end())
  1123. curt.objects.insert(i, toAdd);
  1124. }
  1125. }
  1126. }
  1127. return true;
  1128. }
  1129. bool CMapHandler::hideObject(const CGObjectInstance *obj, bool fadeout /* = false */)
  1130. {
  1131. //optimized version which reveals weird bugs with missing def name
  1132. //auto pos = obj->pos;
  1133. //for (size_t i = pos.x; i > pos.x - obj->getWidth(); i--)
  1134. //{
  1135. // for (size_t j = pos.y; j > pos.y - obj->getHeight(); j--)
  1136. // {
  1137. // int3 t(i, j, pos.z);
  1138. // if (!map->isInTheMap(t))
  1139. // continue;
  1140. // auto &objs = ttiles[i][j][pos.z].objects;
  1141. // for (size_t x = 0; x < objs.size(); x++)
  1142. // {
  1143. // auto ourObj = objs[x].obj;
  1144. // if (ourObj && ourObj->id == obj->id)
  1145. // {
  1146. // if (fadeout && ADVOPT.objectFading) // object should be faded == erase is delayed until the end of fadeout
  1147. // {
  1148. // if (startObjectFade(objs[x], false, t))
  1149. // objs[x].obj = nullptr; //set original pointer to null
  1150. // else
  1151. // objs.erase(objs.begin() + x);
  1152. // }
  1153. // else
  1154. // objs.erase(objs.begin() + x);
  1155. // break;
  1156. // }
  1157. // }
  1158. // }
  1159. //}
  1160. for (size_t i = 0; i<map->width; i++)
  1161. {
  1162. for (size_t j = 0; j<map->height; j++)
  1163. {
  1164. for (size_t k = 0; k<(map->twoLevel ? 2 : 1); k++)
  1165. {
  1166. auto &objs = ttiles[i][j][k].objects;
  1167. for (size_t x = 0; x < objs.size(); x++)
  1168. {
  1169. if (objs[x].obj && objs[x].obj->id == obj->id)
  1170. {
  1171. if (fadeout && ADVOPT.objectFading) // object should be faded == erase is delayed until the end of fadeout
  1172. {
  1173. if (startObjectFade(objs[x], false, int3(i, j, k)))
  1174. objs[x].obj = nullptr;
  1175. else
  1176. objs.erase(objs.begin() + x);
  1177. }
  1178. else
  1179. objs.erase(objs.begin() + x);
  1180. break;
  1181. }
  1182. }
  1183. }
  1184. }
  1185. }
  1186. return true;
  1187. }
  1188. bool CMapHandler::removeObject(CGObjectInstance *obj, bool fadeout /* = false */)
  1189. {
  1190. hideObject(obj, fadeout);
  1191. return true;
  1192. }
  1193. void CMapHandler::validateRectTerr(SDL_Rect * val, const SDL_Rect * ext)
  1194. {
  1195. if(ext)
  1196. {
  1197. if(val->x<0)
  1198. {
  1199. val->w += val->x;
  1200. val->x = ext->x;
  1201. }
  1202. else
  1203. {
  1204. val->x += ext->x;
  1205. }
  1206. if(val->y<0)
  1207. {
  1208. val->h += val->y;
  1209. val->y = ext->y;
  1210. }
  1211. else
  1212. {
  1213. val->y += ext->y;
  1214. }
  1215. if(val->x+val->w > ext->x+ext->w)
  1216. {
  1217. val->w = ext->x+ext->w-val->x;
  1218. }
  1219. if(val->y+val->h > ext->y+ext->h)
  1220. {
  1221. val->h = ext->y+ext->h-val->y;
  1222. }
  1223. //for sign problems
  1224. if(val->h > 20000 || val->w > 20000)
  1225. {
  1226. val->h = val->w = 0;
  1227. }
  1228. }
  1229. }
  1230. ui8 CMapHandler::getDir(const int3 &a, const int3 &b)
  1231. {
  1232. if(a.z!=b.z)
  1233. return -1; //error!
  1234. if(a.x==b.x+1 && a.y==b.y+1) //lt
  1235. return 0;
  1236. else if(a.x==b.x && a.y==b.y+1) //t
  1237. return 1;
  1238. else if(a.x==b.x-1 && a.y==b.y+1) //rt
  1239. return 2;
  1240. else if(a.x==b.x-1 && a.y==b.y) //r
  1241. return 3;
  1242. else if(a.x==b.x-1 && a.y==b.y-1) //rb
  1243. return 4;
  1244. else if(a.x==b.x && a.y==b.y-1) //b
  1245. return 5;
  1246. else if(a.x==b.x+1 && a.y==b.y-1) //lb
  1247. return 6;
  1248. else if(a.x==b.x+1 && a.y==b.y) //l
  1249. return 7;
  1250. return -2; //shouldn't happen
  1251. }
  1252. bool CMapHandler::canStartHeroMovement()
  1253. {
  1254. return fadeAnims.empty(); // don't allow movement during fade animation
  1255. }
  1256. void shiftColors(SDL_Surface *img, int from, int howMany) //shifts colors in palette
  1257. {
  1258. //works with at most 16 colors, if needed more -> increase values
  1259. assert(howMany < 16);
  1260. SDL_Color palette[16];
  1261. for(int i=0; i<howMany; ++i)
  1262. {
  1263. palette[(i+1)%howMany] =img->format->palette->colors[from + i];
  1264. }
  1265. SDL_SetColors(img,palette,from,howMany);
  1266. }
  1267. void CMapHandler::updateWater() //shift colors in palettes of water tiles
  1268. {
  1269. for(auto & elem : terrainGraphics[7])
  1270. {
  1271. shiftColors(elem,246, 9);
  1272. }
  1273. for(auto & elem : terrainGraphics[8])
  1274. {
  1275. shiftColors(elem,229, 12);
  1276. shiftColors(elem,242, 14);
  1277. }
  1278. for(auto & elem : staticRiverDefs[0]->ourImages)
  1279. {
  1280. shiftColors(elem.bitmap,183, 12);
  1281. shiftColors(elem.bitmap,195, 6);
  1282. }
  1283. for(auto & elem : staticRiverDefs[2]->ourImages)
  1284. {
  1285. shiftColors(elem.bitmap,228, 12);
  1286. shiftColors(elem.bitmap,183, 6);
  1287. shiftColors(elem.bitmap,240, 6);
  1288. }
  1289. for(auto & elem : staticRiverDefs[3]->ourImages)
  1290. {
  1291. shiftColors(elem.bitmap,240, 9);
  1292. }
  1293. }
  1294. CMapHandler::~CMapHandler()
  1295. {
  1296. delete graphics->FoWfullHide;
  1297. delete graphics->FoWpartialHide;
  1298. // if (fadingOffscreenBitmapSurface)
  1299. // delete fadingOffscreenBitmapSurface;
  1300. delete normalBlitter;
  1301. delete worldViewBlitter;
  1302. delete puzzleViewBlitter;
  1303. for(auto & elem : roadDefs)
  1304. delete elem;
  1305. for(auto & elem : staticRiverDefs)
  1306. delete elem;
  1307. for(auto & elem : terrainGraphics)
  1308. {
  1309. for(int j=0; j < elem.size(); ++j)
  1310. SDL_FreeSurface(elem[j]);
  1311. }
  1312. for (auto & elem : fadeAnims)
  1313. {
  1314. delete elem.second.second;
  1315. }
  1316. terrainGraphics.clear();
  1317. }
  1318. CMapHandler::CMapHandler()
  1319. {
  1320. frameW = frameH = 0;
  1321. graphics->FoWfullHide = nullptr;
  1322. graphics->FoWpartialHide = nullptr;
  1323. normalBlitter = new CMapNormalBlitter(this);
  1324. worldViewBlitter = new CMapWorldViewBlitter(this);
  1325. puzzleViewBlitter = new CMapPuzzleViewBlitter(this);
  1326. fadeAnimCounter = 0;
  1327. }
  1328. void CMapHandler::getTerrainDescr( const int3 &pos, std::string & out, bool terName )
  1329. {
  1330. out.clear();
  1331. TerrainTile2 & tt = ttiles[pos.x][pos.y][pos.z];
  1332. const TerrainTile &t = map->getTile(pos);
  1333. for(auto & elem : tt.objects)
  1334. {
  1335. if(elem.obj && elem.obj->ID == Obj::HOLE) //Hole
  1336. {
  1337. out = elem.obj->getObjectName();
  1338. return;
  1339. }
  1340. }
  1341. if(t.hasFavorableWinds())
  1342. out = CGI->objtypeh->getObjectName(Obj::FAVORABLE_WINDS);
  1343. else if(terName)
  1344. {
  1345. out = CGI->generaltexth->terrainNames[t.terType];
  1346. if(t.getDiggingStatus(false) == EDiggingStatus::CAN_DIG)
  1347. {
  1348. out = boost::str(boost::format("%s %s") % out % CGI->generaltexth->allTexts[330]); /// digging ok
  1349. }
  1350. }
  1351. }
  1352. void CMapHandler::discardWorldViewCache()
  1353. {
  1354. cache.discardWorldViewCache();
  1355. }
  1356. void CMapHandler::CMapCache::discardWorldViewCache()
  1357. {
  1358. for (auto &cacheDataPair : data)
  1359. {
  1360. for (auto &cacheEntryPair : cacheDataPair.second)
  1361. {
  1362. if (cacheEntryPair.second)
  1363. {
  1364. SDL_FreeSurface(cacheEntryPair.second);
  1365. }
  1366. }
  1367. data[cacheDataPair.first].clear();
  1368. }
  1369. logGlobal->debugStream() << "Discarded world view cache";
  1370. }
  1371. void CMapHandler::CMapCache::updateWorldViewScale(float scale)
  1372. {
  1373. if (fabs(scale - worldViewCachedScale) > 0.001f)
  1374. discardWorldViewCache();
  1375. worldViewCachedScale = scale;
  1376. }
  1377. void CMapHandler::CMapCache::removeFromWorldViewCache(CMapHandler::EMapCacheType type, intptr_t key)
  1378. {
  1379. auto iter = data[type].find(key);
  1380. if (iter != data[type].end())
  1381. {
  1382. SDL_FreeSurface((*iter).second);
  1383. data[type].erase(iter);
  1384. }
  1385. }
  1386. SDL_Surface * CMapHandler::CMapCache::requestWorldViewCache(CMapHandler::EMapCacheType type, intptr_t key)
  1387. {
  1388. auto iter = data[type].find(key);
  1389. if (iter == data[type].end())
  1390. return nullptr;
  1391. return (*iter).second;
  1392. }
  1393. SDL_Surface * CMapHandler::CMapCache::requestWorldViewCacheOrCreate(CMapHandler::EMapCacheType type, intptr_t key, SDL_Surface * fullSurface, float scale)
  1394. {
  1395. auto cached = requestWorldViewCache(type, key);
  1396. if (cached)
  1397. return cached;
  1398. auto scaled = CSDL_Ext::scaleSurfaceFast(fullSurface, fullSurface->w * scale, fullSurface->h * scale);
  1399. if (scaled->format && scaled->format->palette) // fix color keying, because SDL loses it at this point
  1400. CSDL_Ext::setColorKey(scaled, scaled->format->palette->colors[0]);
  1401. return cacheWorldViewEntry(type, key, scaled);
  1402. }
  1403. SDL_Surface * CMapHandler::CMapCache::requestWorldViewCacheOrCreate(CMapHandler::EMapCacheType type, intptr_t key,const IImage * fullSurface, float scale)
  1404. {
  1405. auto cached = requestWorldViewCache(type, key);
  1406. if (cached)
  1407. return cached;
  1408. auto scaled = fullSurface->scaleFast(scale);
  1409. data[type][key] = scaled;
  1410. return scaled;
  1411. }
  1412. SDL_Surface *CMapHandler::CMapCache::cacheWorldViewEntry(CMapHandler::EMapCacheType type, intptr_t key, SDL_Surface * entry)
  1413. {
  1414. if (!entry)
  1415. return nullptr;
  1416. if (requestWorldViewCache(type, key)) // valid cache already present, no need to do it again
  1417. return requestWorldViewCache(type, key);
  1418. data[type][key] = entry;
  1419. return entry;
  1420. }
  1421. intptr_t CMapHandler::CMapCache::genKey(intptr_t realPtr, ui8 mod)
  1422. {
  1423. return (intptr_t)(realPtr ^ (mod << (sizeof(intptr_t) - 2))); // maybe some cleaner method to pack rotation into cache key?
  1424. }
  1425. TerrainTile2::TerrainTile2()
  1426. :terbitmap(nullptr)
  1427. {}
  1428. bool CMapHandler::compareObjectBlitOrder(const CGObjectInstance * a, const CGObjectInstance * b)
  1429. {
  1430. if (!a)
  1431. return true;
  1432. if (!b)
  1433. return false;
  1434. if (a->appearance.printPriority != b->appearance.printPriority)
  1435. return a->appearance.printPriority > b->appearance.printPriority;
  1436. if(a->pos.y != b->pos.y)
  1437. return a->pos.y < b->pos.y;
  1438. if(b->ID==Obj::HERO && a->ID!=Obj::HERO)
  1439. return true;
  1440. if(b->ID!=Obj::HERO && a->ID==Obj::HERO)
  1441. return false;
  1442. if(!a->isVisitable() && b->isVisitable())
  1443. return true;
  1444. if(!b->isVisitable() && a->isVisitable())
  1445. return false;
  1446. if(a->pos.x < b->pos.x)
  1447. return true;
  1448. return false;
  1449. }
  1450. TerrainTileObject::TerrainTileObject(const CGObjectInstance * obj_, SDL_Rect rect_)
  1451. : obj(obj_),
  1452. rect(rect_),
  1453. fadeAnimKey(-1)
  1454. {
  1455. }
  1456. TerrainTileObject::~TerrainTileObject()
  1457. {
  1458. }