mapHandler.cpp 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706
  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. IImage * 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->icons->getImage((int)EWorldViewIcon::TELEPORT);
  486. case Obj::SUBTERRANEAN_GATE:
  487. return info->icons->getImage((int)EWorldViewIcon::GATE);
  488. case Obj::ARTIFACT:
  489. return info->icons->getImage((int)EWorldViewIcon::ARTIFACT);
  490. case Obj::TOWN:
  491. return info->icons->getImage((int)EWorldViewIcon::TOWN + ownerIndex);
  492. case Obj::HERO:
  493. return info->icons->getImage((int)EWorldViewIcon::HERO + ownerIndex);
  494. case Obj::MINE:
  495. return info->icons->getImage((int)EWorldViewIcon::MINE_WOOD + subId + ownerIndex);
  496. case Obj::RESOURCE:
  497. return info->icons->getImage((int)EWorldViewIcon::RES_WOOD + subId + ownerIndex);
  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. IImage * wvIcon = this->objectToIcon(id, subId, owner);
  576. if(nullptr != wvIcon)
  577. {
  578. // centering icon on the object
  579. Point dest(realPos.x + tileSize / 2 - wvIcon->width() / 2, realPos.y + tileSize / 2 - wvIcon->height() / 2);
  580. wvIcon->draw(targetSurf, dest.x, dest.y);
  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. IImage * wvIcon = this->objectToIcon(iconInfo.id, iconInfo.subId, iconInfo.owner);
  610. if(nullptr != wvIcon)
  611. {
  612. // centering icon on the object
  613. Point dest(realPos.x + tileSize / 2 - wvIcon->width() / 2, realPos.y + tileSize / 2 - wvIcon->height() / 2);
  614. wvIcon->draw(targetSurf, dest.x, dest.y);
  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. const IImage * mark = graphics->heroMoveArrows->getImage(0);
  680. mark->draw(targetSurf,realTileRect.x,realTileRect.y);
  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))
  749. {
  750. if (!obj->appearance.animationFile.empty())
  751. logGlobal->errorStream() << "Failed to load image " << obj->appearance.animationFile;
  752. else
  753. logGlobal->warnStream() << boost::format("Def name for obj %d (%d,%d) is empty!") % obj->id % obj->ID % obj->subID;
  754. continue;
  755. }
  756. if (!canDrawObject(obj))
  757. continue;
  758. auto objData = findObjectBitmap(obj, info->anim);
  759. if (objData.objBitmap)
  760. {
  761. Rect srcRect(object.rect.x, object.rect.y, tileSize, tileSize);
  762. drawObject(targetSurf, objData.objBitmap, &srcRect, objData.isMoving);
  763. if (objData.flagBitmap)
  764. {
  765. if (objData.isMoving)
  766. {
  767. srcRect.y += FRAMES_PER_MOVE_ANIM_GROUP * 2 - tileSize;
  768. Rect dstRect(realPos.x, realPos.y - tileSize / 2, tileSize, tileSize);
  769. drawHeroFlag(targetSurf, objData.flagBitmap, &srcRect, &dstRect, true);
  770. }
  771. else if (obj->pos.x == pos.x && obj->pos.y == pos.y)
  772. {
  773. Rect dstRect(realPos.x - 2 * tileSize, realPos.y - tileSize, 3 * tileSize, 2 * tileSize);
  774. drawHeroFlag(targetSurf, objData.flagBitmap, nullptr, &dstRect, false);
  775. }
  776. }
  777. }
  778. }
  779. }
  780. void CMapHandler::CMapBlitter::drawRoad(SDL_Surface * targetSurf, const TerrainTile & tinfo, const TerrainTile * tinfoUpper) const
  781. {
  782. if (tinfoUpper && tinfoUpper->roadType != ERoadType::NO_ROAD)
  783. {
  784. Rect source(0, tileSize / 2, tileSize, tileSize / 2);
  785. Rect dest(realPos.x, realPos.y, tileSize, tileSize / 2);
  786. drawElement(EMapCacheType::ROADS, parent->roadDefs[tinfoUpper->roadType - 1]->ourImages[tinfoUpper->roadDir].bitmap,
  787. &source, targetSurf, &dest, true, (tinfoUpper->extTileFlags >> 4) % 4);
  788. }
  789. if(tinfo.roadType != ERoadType::NO_ROAD) //print road from this tile
  790. {
  791. Rect source(0, 0, tileSize, halfTileSizeCeil);
  792. Rect dest(realPos.x, realPos.y + tileSize / 2, tileSize, tileSize / 2);
  793. drawElement(EMapCacheType::ROADS, parent->roadDefs[tinfo.roadType - 1]->ourImages[tinfo.roadDir].bitmap,
  794. &source, targetSurf, &dest, true, (tinfo.extTileFlags >> 4) % 4);
  795. }
  796. }
  797. void CMapHandler::CMapBlitter::drawRiver(SDL_Surface * targetSurf, const TerrainTile & tinfo) const
  798. {
  799. Rect destRect(realTileRect);
  800. drawElement(EMapCacheType::RIVERS, parent->staticRiverDefs[tinfo.riverType-1]->ourImages[tinfo.riverDir].bitmap,
  801. nullptr, targetSurf, &destRect, true, (tinfo.extTileFlags >> 2) % 4);
  802. }
  803. void CMapHandler::CMapBlitter::drawFow(SDL_Surface * targetSurf) const
  804. {
  805. Rect destRect(realTileRect);
  806. std::pair<SDL_Surface *, bool> hide = getVisBitmap();
  807. drawElement(EMapCacheType::FOW, hide.first, nullptr, targetSurf, &destRect, hide.second);
  808. }
  809. void CMapHandler::CMapBlitter::blit(SDL_Surface * targetSurf, const MapDrawingInfo * info)
  810. {
  811. init(info);
  812. auto prevClip = clip(targetSurf);
  813. pos = int3(0, 0, topTile.z);
  814. for (realPos.x = initPos.x, pos.x = topTile.x; pos.x < topTile.x + tileCount.x; pos.x++, realPos.x += tileSize)
  815. {
  816. if (pos.x < 0 || pos.x >= parent->sizes.x)
  817. continue;
  818. for (realPos.y = initPos.y, pos.y = topTile.y; pos.y < topTile.y + tileCount.y; pos.y++, realPos.y += tileSize)
  819. {
  820. if (pos.y < 0 || pos.y >= parent->sizes.y)
  821. continue;
  822. const bool isVisible = canDrawCurrentTile();
  823. realTileRect.x = realPos.x;
  824. realTileRect.y = realPos.y;
  825. const TerrainTile2 & tile = parent->ttiles[pos.x][pos.y][pos.z];
  826. const TerrainTile & tinfo = parent->map->getTile(pos);
  827. const TerrainTile * tinfoUpper = pos.y > 0 ? &parent->map->getTile(int3(pos.x, pos.y - 1, pos.z)) : nullptr;
  828. if(isVisible || info->showAllTerrain)
  829. {
  830. drawTileTerrain(targetSurf, tinfo, tile);
  831. if (tinfo.riverType)
  832. drawRiver(targetSurf, tinfo);
  833. drawRoad(targetSurf, tinfo, tinfoUpper);
  834. }
  835. if(isVisible)
  836. drawObjects(targetSurf, tile);
  837. }
  838. }
  839. for (realPos.x = initPos.x, pos.x = topTile.x; pos.x < topTile.x + tileCount.x; pos.x++, realPos.x += tileSize)
  840. {
  841. for (realPos.y = initPos.y, pos.y = topTile.y; pos.y < topTile.y + tileCount.y; pos.y++, realPos.y += tileSize)
  842. {
  843. realTileRect.x = realPos.x;
  844. realTileRect.y = realPos.y;
  845. if (pos.x < 0 || pos.x >= parent->sizes.x ||
  846. pos.y < 0 || pos.y >= parent->sizes.y)
  847. {
  848. drawFrame(targetSurf);
  849. }
  850. else
  851. {
  852. const TerrainTile2 & tile = parent->ttiles[pos.x][pos.y][pos.z];
  853. if (!(*info->visibilityMap)[pos.x][pos.y][topTile.z] && !info->showAllTerrain)
  854. drawFow(targetSurf);
  855. // overlay needs to be drawn over fow, because of artifacts-aura-like spells
  856. drawTileOverlay(targetSurf, tile);
  857. // drawDebugVisitables()
  858. if (settings["session"]["showBlock"].Bool())
  859. {
  860. if(parent->map->getTile(int3(pos.x, pos.y, pos.z)).blocked) //temporary hiding blocked positions
  861. {
  862. static SDL_Surface * block = nullptr;
  863. if (!block)
  864. block = BitmapHandler::loadBitmap("blocked");
  865. CSDL_Ext::blitSurface(block, nullptr, targetSurf, &realTileRect);
  866. }
  867. }
  868. if (settings["session"]["showVisit"].Bool())
  869. {
  870. if(parent->map->getTile(int3(pos.x, pos.y, pos.z)).visitable) //temporary hiding visitable positions
  871. {
  872. static SDL_Surface * visit = nullptr;
  873. if (!visit)
  874. visit = BitmapHandler::loadBitmap("visitable");
  875. CSDL_Ext::blitSurface(visit, nullptr, targetSurf, &realTileRect);
  876. }
  877. }
  878. }
  879. }
  880. }
  881. drawOverlayEx(targetSurf);
  882. // drawDebugGrid()
  883. if (settings["session"]["showGrid"].Bool())
  884. {
  885. for (realPos.x = initPos.x, pos.x = topTile.x; pos.x < topTile.x + tileCount.x; pos.x++, realPos.x += tileSize)
  886. {
  887. for (realPos.y = initPos.y, pos.y = topTile.y; pos.y < topTile.y + tileCount.y; pos.y++, realPos.y += tileSize)
  888. {
  889. const int3 color(0x555555, 0x555555, 0x555555);
  890. if (realPos.y >= info->drawBounds->y &&
  891. realPos.y < info->drawBounds->y + info->drawBounds->h)
  892. for(int i = 0; i < tileSize; i++)
  893. if (realPos.x + i >= info->drawBounds->x &&
  894. realPos.x + i < info->drawBounds->x + info->drawBounds->w)
  895. CSDL_Ext::SDL_PutPixelWithoutRefresh(targetSurf, realPos.x + i, realPos.y, color.x, color.y, color.z);
  896. if (realPos.x >= info->drawBounds->x &&
  897. realPos.x < info->drawBounds->x + info->drawBounds->w)
  898. for(int i = 0; i < tileSize; i++)
  899. if (realPos.y + i >= info->drawBounds->y &&
  900. realPos.y + i < info->drawBounds->y + info->drawBounds->h)
  901. CSDL_Ext::SDL_PutPixelWithoutRefresh(targetSurf, realPos.x, realPos.y + i, color.x, color.y, color.z);
  902. }
  903. }
  904. }
  905. postProcessing(targetSurf);
  906. SDL_SetClipRect(targetSurf, &prevClip);
  907. }
  908. CMapHandler::AnimBitmapHolder CMapHandler::CMapBlitter::findHeroBitmap(const CGHeroInstance * hero, int anim) const
  909. {
  910. if(hero && hero->moveDir && hero->type) //it's hero or boat
  911. {
  912. if(hero->tempOwner >= PlayerColor::PLAYER_LIMIT) //Neutral hero?
  913. {
  914. logGlobal->errorStream() << "A neutral hero (" << hero->name << ") at " << hero->pos << ". Should not happen!";
  915. return CMapHandler::AnimBitmapHolder();
  916. }
  917. //pick graphics of hero (or boat if hero is sailing)
  918. CDefEssential * def = nullptr;
  919. if (hero->boat)
  920. def = graphics->boatAnims[hero->boat->subID];
  921. else
  922. def = graphics->heroAnims[hero->appearance.animationFile];
  923. bool moving = !hero->isStanding;
  924. int framesOffset = moving ? anim % FRAMES_PER_MOVE_ANIM_GROUP : 0;
  925. int index = findAnimIndexByGroup(def, getHeroFrameNum(hero->moveDir, moving));
  926. if (index >= 0)
  927. {
  928. auto heroBitmap = def->ourImages[index + framesOffset].bitmap;
  929. auto flagBitmap = findFlagBitmap(hero, anim, &hero->tempOwner, index + 35);
  930. return CMapHandler::AnimBitmapHolder(heroBitmap, flagBitmap, moving);
  931. }
  932. }
  933. return CMapHandler::AnimBitmapHolder();
  934. }
  935. CMapHandler::AnimBitmapHolder CMapHandler::CMapBlitter::findBoatBitmap(const CGBoat * boat, int anim) const
  936. {
  937. auto def = graphics->boatAnims[boat->subID];
  938. int index = findAnimIndexByGroup(def, getHeroFrameNum(boat->direction, false));
  939. if (index < 0)
  940. return CMapHandler::AnimBitmapHolder();
  941. return CMapHandler::AnimBitmapHolder(def->ourImages[index].bitmap);
  942. }
  943. SDL_Surface * CMapHandler::CMapBlitter::findFlagBitmap(const CGHeroInstance * hero, int anim, const PlayerColor * color, int indexOffset) const
  944. {
  945. if (!hero)
  946. return nullptr;
  947. if (hero->boat)
  948. return findBoatFlagBitmap(hero->boat, anim, color, indexOffset, hero->moveDir);
  949. return findHeroFlagBitmap(hero, anim, color, indexOffset);
  950. }
  951. SDL_Surface * CMapHandler::CMapBlitter::findHeroFlagBitmap(const CGHeroInstance * hero, int anim, const PlayerColor * color, int indexOffset) const
  952. {
  953. return findFlagBitmapInternal(graphics->flags4[color->getNum()], anim, indexOffset, hero->moveDir, !hero->isStanding);
  954. }
  955. SDL_Surface * CMapHandler::CMapBlitter::findBoatFlagBitmap(const CGBoat * boat, int anim, const PlayerColor * color, int indexOffset, ui8 dir) const
  956. {
  957. std::vector<CDefEssential *> Graphics::*flg = nullptr;
  958. switch (boat->subID)
  959. {
  960. case 0: flg = &Graphics::flags1; break;
  961. case 1: flg = &Graphics::flags2; break;
  962. case 2: flg = &Graphics::flags3; break;
  963. default: logGlobal->errorStream() << "Not supported boat subtype: " << boat->subID; return nullptr;
  964. }
  965. return findFlagBitmapInternal((graphics->*flg)[color->getNum()], anim, indexOffset, dir, false);
  966. }
  967. SDL_Surface * CMapHandler::CMapBlitter::findFlagBitmapInternal(const CDefEssential * def, int anim, int indexOffset, ui8 dir, bool moving) const
  968. {
  969. if (moving)
  970. return def->ourImages[indexOffset + anim % FRAMES_PER_MOVE_ANIM_GROUP].bitmap;
  971. return def->ourImages[getHeroFrameNum(dir, false) * FRAMES_PER_MOVE_ANIM_GROUP + (anim / 4) % FRAMES_PER_MOVE_ANIM_GROUP].bitmap;
  972. }
  973. int CMapHandler::CMapBlitter::findAnimIndexByGroup(const CDefEssential * def, int groupNum) const
  974. {
  975. auto iter = std::find_if(def->ourImages.begin(), def->ourImages.end(), [&](const Cimage &img){ return img.groupNumber == groupNum; });
  976. if (iter == def->ourImages.end())
  977. return -1;
  978. return static_cast<int>(iter - def->ourImages.begin());
  979. }
  980. CMapHandler::AnimBitmapHolder CMapHandler::CMapBlitter::findObjectBitmap(const CGObjectInstance * obj, int anim) const
  981. {
  982. if (!obj)
  983. return CMapHandler::AnimBitmapHolder();
  984. if (obj->ID == Obj::HERO)
  985. return findHeroBitmap(static_cast<const CGHeroInstance*>(obj), anim);
  986. if (obj->ID == Obj::BOAT)
  987. return findBoatBitmap(static_cast<const CGBoat*>(obj), anim);
  988. // normal object
  989. const std::vector<Cimage> &ourImages = graphics->getDef(obj)->ourImages;
  990. SDL_Surface *bitmap = ourImages[(anim + getPhaseShift(obj)) % ourImages.size()].bitmap;
  991. //setting appropriate flag color
  992. const PlayerColor &color = obj->tempOwner;
  993. if(color < PlayerColor::PLAYER_LIMIT || color==PlayerColor::NEUTRAL)
  994. CSDL_Ext::setPlayerColor(bitmap, color);
  995. return CMapHandler::AnimBitmapHolder(bitmap);
  996. }
  997. ui8 CMapHandler::CMapBlitter::getPhaseShift(const CGObjectInstance *object) const
  998. {
  999. auto i = parent->animationPhase.find(object);
  1000. if(i == parent->animationPhase.end())
  1001. {
  1002. ui8 ret = CRandomGenerator::getDefault().nextInt(254);
  1003. parent->animationPhase[object] = ret;
  1004. return ret;
  1005. }
  1006. return i->second;
  1007. }
  1008. bool CMapHandler::CMapBlitter::canDrawObject(const CGObjectInstance * obj) const
  1009. {
  1010. //checking if object has non-empty graphic on this tile
  1011. return obj->ID == Obj::HERO || obj->coveringAt(pos.x, pos.y);
  1012. }
  1013. bool CMapHandler::CMapBlitter::canDrawCurrentTile() const
  1014. {
  1015. const NeighborTilesInfo neighbors(pos, parent->sizes, *info->visibilityMap);
  1016. return !neighbors.areAllHidden();
  1017. }
  1018. ui8 CMapHandler::CMapBlitter::getHeroFrameNum(ui8 dir, bool isMoving) const
  1019. {
  1020. if(isMoving)
  1021. {
  1022. static const ui8 frame [] = {0xff, 10, 5, 6, 7, 8, 9, 12, 11};
  1023. return frame[dir];
  1024. }
  1025. else //if(isMoving)
  1026. {
  1027. static const ui8 frame [] = {0xff, 13, 0, 1, 2, 3, 4, 15, 14};
  1028. return frame[dir];
  1029. }
  1030. }
  1031. std::pair<SDL_Surface *, bool> CMapHandler::CMapBlitter::getVisBitmap() const
  1032. {
  1033. const NeighborTilesInfo neighborInfo(pos, parent->sizes, *info->visibilityMap);
  1034. int retBitmapID = neighborInfo.getBitmapID();// >=0 -> partial hide, <0 - full hide
  1035. if (retBitmapID < 0)
  1036. {
  1037. retBitmapID = - parent->hideBitmap[pos.x][pos.y][pos.z] - 1; //fully hidden
  1038. }
  1039. if (retBitmapID >= 0)
  1040. {
  1041. return std::make_pair(graphics->FoWpartialHide->ourImages[retBitmapID].bitmap, true);
  1042. }
  1043. else
  1044. {
  1045. return std::make_pair(graphics->FoWfullHide->ourImages[-retBitmapID - 1].bitmap, false);
  1046. }
  1047. }
  1048. bool CMapHandler::updateObjectsFade()
  1049. {
  1050. for (auto iter = fadeAnims.begin(); iter != fadeAnims.end(); )
  1051. {
  1052. int3 pos = (*iter).second.first;
  1053. CFadeAnimation * anim = (*iter).second.second;
  1054. anim->update();
  1055. if (anim->isFading())
  1056. ++iter;
  1057. else // fade finished
  1058. {
  1059. auto &objs = ttiles[pos.x][pos.y][pos.z].objects;
  1060. for (auto objIter = objs.begin(); objIter != objs.end(); ++objIter)
  1061. {
  1062. if ((*objIter).fadeAnimKey == (*iter).first)
  1063. {
  1064. logAnim->traceStream() << "Fade anim finished for obj at " << pos << "; remaining: " << (fadeAnims.size() - 1);
  1065. if (anim->fadingMode == CFadeAnimation::EMode::OUT)
  1066. objs.erase(objIter); // if this was fadeout, remove the object from the map
  1067. else
  1068. (*objIter).fadeAnimKey = -1; // for fadein, just remove its connection to the finished fade
  1069. break;
  1070. }
  1071. }
  1072. delete (*iter).second.second;
  1073. iter = fadeAnims.erase(iter);
  1074. }
  1075. }
  1076. return !fadeAnims.empty();
  1077. }
  1078. bool CMapHandler::startObjectFade(TerrainTileObject & obj, bool in, int3 pos)
  1079. {
  1080. SDL_Surface * fadeBitmap;
  1081. assert(obj.obj);
  1082. auto objData = normalBlitter->findObjectBitmap(obj.obj, 0);
  1083. if (objData.objBitmap)
  1084. {
  1085. if (objData.isMoving) // ignore fading of moving objects (for now?)
  1086. {
  1087. logAnim->debugStream() << "Ignoring fade of moving object";
  1088. return false;
  1089. }
  1090. fadeBitmap = CSDL_Ext::newSurface(32, 32); // TODO cache these bitmaps instead of creating new ones?
  1091. Rect objSrcRect(obj.rect.x, obj.rect.y, 32, 32);
  1092. CSDL_Ext::blit8bppAlphaTo24bpp(objData.objBitmap, &objSrcRect, fadeBitmap, nullptr);
  1093. if (objData.flagBitmap)
  1094. {
  1095. 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
  1096. {
  1097. Rect flagSrcRect(32, 0, 32, 32);
  1098. CSDL_Ext::blitSurface(objData.flagBitmap, &flagSrcRect, fadeBitmap, nullptr);
  1099. }
  1100. }
  1101. auto anim = new CFadeAnimation();
  1102. anim->init(in ? CFadeAnimation::EMode::IN : CFadeAnimation::EMode::OUT, fadeBitmap, true);
  1103. fadeAnims[++fadeAnimCounter] = std::pair<int3, CFadeAnimation*>(pos, anim);
  1104. obj.fadeAnimKey = fadeAnimCounter;
  1105. logAnim->traceStream() << "Fade anim started for obj " << obj.obj->ID
  1106. << " at " << pos << "; anim count: " << fadeAnims.size();
  1107. return true;
  1108. }
  1109. return false;
  1110. }
  1111. bool CMapHandler::printObject(const CGObjectInstance *obj, bool fadein /* = false */)
  1112. {
  1113. if (!graphics->getDef(obj))
  1114. processDef(obj->appearance);
  1115. SDL_Surface *bitmap = graphics->getDef(obj)->ourImages[0].bitmap;
  1116. const int tilesW = bitmap->w/32;
  1117. const int tilesH = bitmap->h/32;
  1118. for(int fx=0; fx<tilesW; ++fx)
  1119. {
  1120. for(int fy=0; fy<tilesH; ++fy)
  1121. {
  1122. SDL_Rect cr;
  1123. cr.w = 32;
  1124. cr.h = 32;
  1125. cr.x = fx*32;
  1126. cr.y = fy*32;
  1127. TerrainTileObject toAdd(obj, cr);
  1128. 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)
  1129. {
  1130. int3 pos(obj->pos.x + fx - tilesW + 1, obj->pos.y + fy - tilesH + 1, obj->pos.z);
  1131. TerrainTile2 & curt = ttiles[pos.x][pos.y][pos.z];
  1132. if (fadein && ADVOPT.objectFading)
  1133. {
  1134. startObjectFade(toAdd, true, pos);
  1135. }
  1136. auto i = curt.objects.begin();
  1137. for(; i != curt.objects.end(); i++)
  1138. {
  1139. if(objectBlitOrderSorter(toAdd, *i))
  1140. {
  1141. curt.objects.insert(i, toAdd);
  1142. i = curt.objects.begin(); //to validate and avoid adding it second time
  1143. break;
  1144. }
  1145. }
  1146. if(i == curt.objects.end())
  1147. curt.objects.insert(i, toAdd);
  1148. }
  1149. }
  1150. }
  1151. return true;
  1152. }
  1153. bool CMapHandler::hideObject(const CGObjectInstance *obj, bool fadeout /* = false */)
  1154. {
  1155. //optimized version which reveals weird bugs with missing def name
  1156. //auto pos = obj->pos;
  1157. //for (size_t i = pos.x; i > pos.x - obj->getWidth(); i--)
  1158. //{
  1159. // for (size_t j = pos.y; j > pos.y - obj->getHeight(); j--)
  1160. // {
  1161. // int3 t(i, j, pos.z);
  1162. // if (!map->isInTheMap(t))
  1163. // continue;
  1164. // auto &objs = ttiles[i][j][pos.z].objects;
  1165. // for (size_t x = 0; x < objs.size(); x++)
  1166. // {
  1167. // auto ourObj = objs[x].obj;
  1168. // if (ourObj && ourObj->id == obj->id)
  1169. // {
  1170. // if (fadeout && ADVOPT.objectFading) // object should be faded == erase is delayed until the end of fadeout
  1171. // {
  1172. // if (startObjectFade(objs[x], false, t))
  1173. // objs[x].obj = nullptr; //set original pointer to null
  1174. // else
  1175. // objs.erase(objs.begin() + x);
  1176. // }
  1177. // else
  1178. // objs.erase(objs.begin() + x);
  1179. // break;
  1180. // }
  1181. // }
  1182. // }
  1183. //}
  1184. for (size_t i = 0; i<map->width; i++)
  1185. {
  1186. for (size_t j = 0; j<map->height; j++)
  1187. {
  1188. for (size_t k = 0; k<(map->twoLevel ? 2 : 1); k++)
  1189. {
  1190. auto &objs = ttiles[i][j][k].objects;
  1191. for (size_t x = 0; x < objs.size(); x++)
  1192. {
  1193. if (objs[x].obj && objs[x].obj->id == obj->id)
  1194. {
  1195. if (fadeout && ADVOPT.objectFading) // object should be faded == erase is delayed until the end of fadeout
  1196. {
  1197. if (startObjectFade(objs[x], false, int3(i, j, k)))
  1198. objs[x].obj = nullptr;
  1199. else
  1200. objs.erase(objs.begin() + x);
  1201. }
  1202. else
  1203. objs.erase(objs.begin() + x);
  1204. break;
  1205. }
  1206. }
  1207. }
  1208. }
  1209. }
  1210. return true;
  1211. }
  1212. bool CMapHandler::removeObject(CGObjectInstance *obj, bool fadeout /* = false */)
  1213. {
  1214. hideObject(obj, fadeout);
  1215. return true;
  1216. }
  1217. void CMapHandler::validateRectTerr(SDL_Rect * val, const SDL_Rect * ext)
  1218. {
  1219. if(ext)
  1220. {
  1221. if(val->x<0)
  1222. {
  1223. val->w += val->x;
  1224. val->x = ext->x;
  1225. }
  1226. else
  1227. {
  1228. val->x += ext->x;
  1229. }
  1230. if(val->y<0)
  1231. {
  1232. val->h += val->y;
  1233. val->y = ext->y;
  1234. }
  1235. else
  1236. {
  1237. val->y += ext->y;
  1238. }
  1239. if(val->x+val->w > ext->x+ext->w)
  1240. {
  1241. val->w = ext->x+ext->w-val->x;
  1242. }
  1243. if(val->y+val->h > ext->y+ext->h)
  1244. {
  1245. val->h = ext->y+ext->h-val->y;
  1246. }
  1247. //for sign problems
  1248. if(val->h > 20000 || val->w > 20000)
  1249. {
  1250. val->h = val->w = 0;
  1251. }
  1252. }
  1253. }
  1254. ui8 CMapHandler::getDir(const int3 &a, const int3 &b)
  1255. {
  1256. if(a.z!=b.z)
  1257. return -1; //error!
  1258. if(a.x==b.x+1 && a.y==b.y+1) //lt
  1259. return 0;
  1260. else if(a.x==b.x && a.y==b.y+1) //t
  1261. return 1;
  1262. else if(a.x==b.x-1 && a.y==b.y+1) //rt
  1263. return 2;
  1264. else if(a.x==b.x-1 && a.y==b.y) //r
  1265. return 3;
  1266. else if(a.x==b.x-1 && a.y==b.y-1) //rb
  1267. return 4;
  1268. else if(a.x==b.x && a.y==b.y-1) //b
  1269. return 5;
  1270. else if(a.x==b.x+1 && a.y==b.y-1) //lb
  1271. return 6;
  1272. else if(a.x==b.x+1 && a.y==b.y) //l
  1273. return 7;
  1274. return -2; //shouldn't happen
  1275. }
  1276. bool CMapHandler::canStartHeroMovement()
  1277. {
  1278. return fadeAnims.empty(); // don't allow movement during fade animation
  1279. }
  1280. void shiftColors(SDL_Surface *img, int from, int howMany) //shifts colors in palette
  1281. {
  1282. //works with at most 16 colors, if needed more -> increase values
  1283. assert(howMany < 16);
  1284. SDL_Color palette[16];
  1285. for(int i=0; i<howMany; ++i)
  1286. {
  1287. palette[(i+1)%howMany] =img->format->palette->colors[from + i];
  1288. }
  1289. SDL_SetColors(img,palette,from,howMany);
  1290. }
  1291. void CMapHandler::updateWater() //shift colors in palettes of water tiles
  1292. {
  1293. for(auto & elem : terrainGraphics[7])
  1294. {
  1295. shiftColors(elem,246, 9);
  1296. }
  1297. for(auto & elem : terrainGraphics[8])
  1298. {
  1299. shiftColors(elem,229, 12);
  1300. shiftColors(elem,242, 14);
  1301. }
  1302. for(auto & elem : staticRiverDefs[0]->ourImages)
  1303. {
  1304. shiftColors(elem.bitmap,183, 12);
  1305. shiftColors(elem.bitmap,195, 6);
  1306. }
  1307. for(auto & elem : staticRiverDefs[2]->ourImages)
  1308. {
  1309. shiftColors(elem.bitmap,228, 12);
  1310. shiftColors(elem.bitmap,183, 6);
  1311. shiftColors(elem.bitmap,240, 6);
  1312. }
  1313. for(auto & elem : staticRiverDefs[3]->ourImages)
  1314. {
  1315. shiftColors(elem.bitmap,240, 9);
  1316. }
  1317. }
  1318. CMapHandler::~CMapHandler()
  1319. {
  1320. delete graphics->FoWfullHide;
  1321. delete graphics->FoWpartialHide;
  1322. // if (fadingOffscreenBitmapSurface)
  1323. // delete fadingOffscreenBitmapSurface;
  1324. delete normalBlitter;
  1325. delete worldViewBlitter;
  1326. delete puzzleViewBlitter;
  1327. for(auto & elem : roadDefs)
  1328. delete elem;
  1329. for(auto & elem : staticRiverDefs)
  1330. delete elem;
  1331. for(auto & elem : terrainGraphics)
  1332. {
  1333. for(int j=0; j < elem.size(); ++j)
  1334. SDL_FreeSurface(elem[j]);
  1335. }
  1336. for (auto & elem : fadeAnims)
  1337. {
  1338. delete elem.second.second;
  1339. }
  1340. terrainGraphics.clear();
  1341. }
  1342. CMapHandler::CMapHandler()
  1343. {
  1344. frameW = frameH = 0;
  1345. graphics->FoWfullHide = nullptr;
  1346. graphics->FoWpartialHide = nullptr;
  1347. normalBlitter = new CMapNormalBlitter(this);
  1348. worldViewBlitter = new CMapWorldViewBlitter(this);
  1349. puzzleViewBlitter = new CMapPuzzleViewBlitter(this);
  1350. fadeAnimCounter = 0;
  1351. }
  1352. void CMapHandler::getTerrainDescr( const int3 &pos, std::string & out, bool terName )
  1353. {
  1354. out.clear();
  1355. TerrainTile2 & tt = ttiles[pos.x][pos.y][pos.z];
  1356. const TerrainTile &t = map->getTile(pos);
  1357. for(auto & elem : tt.objects)
  1358. {
  1359. if(elem.obj && elem.obj->ID == Obj::HOLE) //Hole
  1360. {
  1361. out = elem.obj->getObjectName();
  1362. return;
  1363. }
  1364. }
  1365. if(t.hasFavorableWinds())
  1366. out = CGI->objtypeh->getObjectName(Obj::FAVORABLE_WINDS);
  1367. else if(terName)
  1368. {
  1369. out = CGI->generaltexth->terrainNames[t.terType];
  1370. if(t.getDiggingStatus(false) == EDiggingStatus::CAN_DIG)
  1371. {
  1372. out = boost::str(boost::format("%s %s") % out % CGI->generaltexth->allTexts[330]); /// digging ok
  1373. }
  1374. }
  1375. }
  1376. void CMapHandler::discardWorldViewCache()
  1377. {
  1378. cache.discardWorldViewCache();
  1379. }
  1380. void CMapHandler::CMapCache::discardWorldViewCache()
  1381. {
  1382. for (auto &cacheDataPair : data)
  1383. {
  1384. for (auto &cacheEntryPair : cacheDataPair.second)
  1385. {
  1386. if (cacheEntryPair.second)
  1387. {
  1388. SDL_FreeSurface(cacheEntryPair.second);
  1389. }
  1390. }
  1391. data[cacheDataPair.first].clear();
  1392. }
  1393. logGlobal->debugStream() << "Discarded world view cache";
  1394. }
  1395. void CMapHandler::CMapCache::updateWorldViewScale(float scale)
  1396. {
  1397. if (fabs(scale - worldViewCachedScale) > 0.001f)
  1398. discardWorldViewCache();
  1399. worldViewCachedScale = scale;
  1400. }
  1401. void CMapHandler::CMapCache::removeFromWorldViewCache(CMapHandler::EMapCacheType type, intptr_t key)
  1402. {
  1403. auto iter = data[type].find(key);
  1404. if (iter != data[type].end())
  1405. {
  1406. SDL_FreeSurface((*iter).second);
  1407. data[type].erase(iter);
  1408. }
  1409. }
  1410. SDL_Surface * CMapHandler::CMapCache::requestWorldViewCache(CMapHandler::EMapCacheType type, intptr_t key)
  1411. {
  1412. auto iter = data[type].find(key);
  1413. if (iter == data[type].end())
  1414. return nullptr;
  1415. return (*iter).second;
  1416. }
  1417. SDL_Surface * CMapHandler::CMapCache::requestWorldViewCacheOrCreate(CMapHandler::EMapCacheType type, intptr_t key, SDL_Surface * fullSurface, float scale)
  1418. {
  1419. auto cached = requestWorldViewCache(type, key);
  1420. if (cached)
  1421. return cached;
  1422. auto scaled = CSDL_Ext::scaleSurfaceFast(fullSurface, fullSurface->w * scale, fullSurface->h * scale);
  1423. if (scaled->format && scaled->format->palette) // fix color keying, because SDL loses it at this point
  1424. CSDL_Ext::setColorKey(scaled, scaled->format->palette->colors[0]);
  1425. return cacheWorldViewEntry(type, key, scaled);
  1426. }
  1427. SDL_Surface *CMapHandler::CMapCache::cacheWorldViewEntry(CMapHandler::EMapCacheType type, intptr_t key, SDL_Surface * entry)
  1428. {
  1429. if (!entry)
  1430. return nullptr;
  1431. if (requestWorldViewCache(type, key)) // valid cache already present, no need to do it again
  1432. return requestWorldViewCache(type, key);
  1433. data[type][key] = entry;
  1434. return entry;
  1435. }
  1436. intptr_t CMapHandler::CMapCache::genKey(intptr_t realPtr, ui8 mod)
  1437. {
  1438. return (intptr_t)(realPtr ^ (mod << (sizeof(intptr_t) - 2))); // maybe some cleaner method to pack rotation into cache key?
  1439. }
  1440. TerrainTile2::TerrainTile2()
  1441. :terbitmap(nullptr)
  1442. {}
  1443. bool CMapHandler::compareObjectBlitOrder(const CGObjectInstance * a, const CGObjectInstance * b)
  1444. {
  1445. if (!a)
  1446. return true;
  1447. if (!b)
  1448. return false;
  1449. if (a->appearance.printPriority != b->appearance.printPriority)
  1450. return a->appearance.printPriority > b->appearance.printPriority;
  1451. if(a->pos.y != b->pos.y)
  1452. return a->pos.y < b->pos.y;
  1453. if(b->ID==Obj::HERO && a->ID!=Obj::HERO)
  1454. return true;
  1455. if(b->ID!=Obj::HERO && a->ID==Obj::HERO)
  1456. return false;
  1457. if(!a->isVisitable() && b->isVisitable())
  1458. return true;
  1459. if(!b->isVisitable() && a->isVisitable())
  1460. return false;
  1461. if(a->pos.x < b->pos.x)
  1462. return true;
  1463. return false;
  1464. }
  1465. TerrainTileObject::TerrainTileObject(const CGObjectInstance * obj_, SDL_Rect rect_)
  1466. : obj(obj_),
  1467. rect(rect_),
  1468. fadeAnimKey(-1)
  1469. {
  1470. }
  1471. TerrainTileObject::~TerrainTileObject()
  1472. {
  1473. }