mapHandler.cpp 52 KB

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