mapHandler.cpp 50 KB

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