mapHandler.cpp 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659
  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. // do we actually need to search through the whole map for this?
  1149. for (size_t i=0; i<map->width; i++)
  1150. {
  1151. for (size_t j=0; j<map->height; j++)
  1152. {
  1153. for (size_t k=0; k<(map->twoLevel ? 2 : 1); k++)
  1154. {
  1155. auto &objs = ttiles[i][j][k].objects;
  1156. for(size_t x=0; x < objs.size(); x++)
  1157. {
  1158. if (objs[x].obj && objs[x].obj->id == obj->id)
  1159. {
  1160. if (fadeout && ADVOPT.objectFading) // object should be faded == erase is delayed until the end of fadeout
  1161. {
  1162. if (startObjectFade(objs[x], false, int3(i, j, k)))
  1163. objs[x].obj = nullptr;
  1164. else
  1165. objs.erase(objs.begin() + x);
  1166. }
  1167. else
  1168. objs.erase(objs.begin() + x);
  1169. break;
  1170. }
  1171. }
  1172. }
  1173. }
  1174. }
  1175. return true;
  1176. }
  1177. bool CMapHandler::removeObject(CGObjectInstance *obj, bool fadeout /* = false */)
  1178. {
  1179. hideObject(obj, fadeout);
  1180. return true;
  1181. }
  1182. void CMapHandler::validateRectTerr(SDL_Rect * val, const SDL_Rect * ext)
  1183. {
  1184. if(ext)
  1185. {
  1186. if(val->x<0)
  1187. {
  1188. val->w += val->x;
  1189. val->x = ext->x;
  1190. }
  1191. else
  1192. {
  1193. val->x += ext->x;
  1194. }
  1195. if(val->y<0)
  1196. {
  1197. val->h += val->y;
  1198. val->y = ext->y;
  1199. }
  1200. else
  1201. {
  1202. val->y += ext->y;
  1203. }
  1204. if(val->x+val->w > ext->x+ext->w)
  1205. {
  1206. val->w = ext->x+ext->w-val->x;
  1207. }
  1208. if(val->y+val->h > ext->y+ext->h)
  1209. {
  1210. val->h = ext->y+ext->h-val->y;
  1211. }
  1212. //for sign problems
  1213. if(val->h > 20000 || val->w > 20000)
  1214. {
  1215. val->h = val->w = 0;
  1216. }
  1217. }
  1218. }
  1219. ui8 CMapHandler::getDir(const int3 &a, const int3 &b)
  1220. {
  1221. if(a.z!=b.z)
  1222. return -1; //error!
  1223. if(a.x==b.x+1 && a.y==b.y+1) //lt
  1224. return 0;
  1225. else if(a.x==b.x && a.y==b.y+1) //t
  1226. return 1;
  1227. else if(a.x==b.x-1 && a.y==b.y+1) //rt
  1228. return 2;
  1229. else if(a.x==b.x-1 && a.y==b.y) //r
  1230. return 3;
  1231. else if(a.x==b.x-1 && a.y==b.y-1) //rb
  1232. return 4;
  1233. else if(a.x==b.x && a.y==b.y-1) //b
  1234. return 5;
  1235. else if(a.x==b.x+1 && a.y==b.y-1) //lb
  1236. return 6;
  1237. else if(a.x==b.x+1 && a.y==b.y) //l
  1238. return 7;
  1239. return -2; //shouldn't happen
  1240. }
  1241. bool CMapHandler::canStartHeroMovement()
  1242. {
  1243. return fadeAnims.empty(); // don't allow movement during fade animation
  1244. }
  1245. void shiftColors(SDL_Surface *img, int from, int howMany) //shifts colors in palette
  1246. {
  1247. //works with at most 16 colors, if needed more -> increase values
  1248. assert(howMany < 16);
  1249. SDL_Color palette[16];
  1250. for(int i=0; i<howMany; ++i)
  1251. {
  1252. palette[(i+1)%howMany] =img->format->palette->colors[from + i];
  1253. }
  1254. SDL_SetColors(img,palette,from,howMany);
  1255. }
  1256. void CMapHandler::updateWater() //shift colors in palettes of water tiles
  1257. {
  1258. for(auto & elem : terrainGraphics[7])
  1259. {
  1260. shiftColors(elem,246, 9);
  1261. }
  1262. for(auto & elem : terrainGraphics[8])
  1263. {
  1264. shiftColors(elem,229, 12);
  1265. shiftColors(elem,242, 14);
  1266. }
  1267. for(auto & elem : staticRiverDefs[0]->ourImages)
  1268. {
  1269. shiftColors(elem.bitmap,183, 12);
  1270. shiftColors(elem.bitmap,195, 6);
  1271. }
  1272. for(auto & elem : staticRiverDefs[2]->ourImages)
  1273. {
  1274. shiftColors(elem.bitmap,228, 12);
  1275. shiftColors(elem.bitmap,183, 6);
  1276. shiftColors(elem.bitmap,240, 6);
  1277. }
  1278. for(auto & elem : staticRiverDefs[3]->ourImages)
  1279. {
  1280. shiftColors(elem.bitmap,240, 9);
  1281. }
  1282. }
  1283. CMapHandler::~CMapHandler()
  1284. {
  1285. delete graphics->FoWfullHide;
  1286. delete graphics->FoWpartialHide;
  1287. // if (fadingOffscreenBitmapSurface)
  1288. // delete fadingOffscreenBitmapSurface;
  1289. delete normalBlitter;
  1290. delete worldViewBlitter;
  1291. delete puzzleViewBlitter;
  1292. for(auto & elem : roadDefs)
  1293. delete elem;
  1294. for(auto & elem : staticRiverDefs)
  1295. delete elem;
  1296. for(auto & elem : terrainGraphics)
  1297. {
  1298. for(int j=0; j < elem.size(); ++j)
  1299. SDL_FreeSurface(elem[j]);
  1300. }
  1301. for (auto & elem : fadeAnims)
  1302. {
  1303. delete elem.second.second;
  1304. }
  1305. terrainGraphics.clear();
  1306. }
  1307. CMapHandler::CMapHandler()
  1308. {
  1309. frameW = frameH = 0;
  1310. graphics->FoWfullHide = nullptr;
  1311. graphics->FoWpartialHide = nullptr;
  1312. normalBlitter = new CMapNormalBlitter(this);
  1313. worldViewBlitter = new CMapWorldViewBlitter(this);
  1314. puzzleViewBlitter = new CMapPuzzleViewBlitter(this);
  1315. fadeAnimCounter = 0;
  1316. }
  1317. void CMapHandler::getTerrainDescr( const int3 &pos, std::string & out, bool terName )
  1318. {
  1319. out.clear();
  1320. TerrainTile2 & tt = ttiles[pos.x][pos.y][pos.z];
  1321. const TerrainTile &t = map->getTile(pos);
  1322. for(auto & elem : tt.objects)
  1323. {
  1324. if(elem.obj && elem.obj->ID == Obj::HOLE) //Hole
  1325. {
  1326. out = elem.obj->getObjectName();
  1327. return;
  1328. }
  1329. }
  1330. if(t.hasFavourableWinds())
  1331. out = CGI->objtypeh->getObjectName(Obj::FAVORABLE_WINDS);
  1332. else if(terName)
  1333. out = CGI->generaltexth->terrainNames[t.terType];
  1334. }
  1335. void CMapHandler::discardWorldViewCache()
  1336. {
  1337. cache.discardWorldViewCache();
  1338. }
  1339. void CMapHandler::CMapCache::discardWorldViewCache()
  1340. {
  1341. for (auto &cacheDataPair : data)
  1342. {
  1343. for (auto &cacheEntryPair : cacheDataPair.second)
  1344. {
  1345. if (cacheEntryPair.second)
  1346. {
  1347. SDL_FreeSurface(cacheEntryPair.second);
  1348. }
  1349. }
  1350. data[cacheDataPair.first].clear();
  1351. }
  1352. logGlobal->debugStream() << "Discarded world view cache";
  1353. }
  1354. void CMapHandler::CMapCache::updateWorldViewScale(float scale)
  1355. {
  1356. if (fabs(scale - worldViewCachedScale) > 0.001f)
  1357. discardWorldViewCache();
  1358. worldViewCachedScale = scale;
  1359. }
  1360. void CMapHandler::CMapCache::removeFromWorldViewCache(CMapHandler::EMapCacheType type, intptr_t key)
  1361. {
  1362. auto iter = data[type].find(key);
  1363. if (iter != data[type].end())
  1364. {
  1365. SDL_FreeSurface((*iter).second);
  1366. data[type].erase(iter);
  1367. }
  1368. }
  1369. SDL_Surface * CMapHandler::CMapCache::requestWorldViewCache(CMapHandler::EMapCacheType type, intptr_t key)
  1370. {
  1371. auto iter = data[type].find(key);
  1372. if (iter == data[type].end())
  1373. return nullptr;
  1374. return (*iter).second;
  1375. }
  1376. SDL_Surface * CMapHandler::CMapCache::requestWorldViewCacheOrCreate(CMapHandler::EMapCacheType type, intptr_t key, SDL_Surface * fullSurface, float scale)
  1377. {
  1378. auto cached = requestWorldViewCache(type, key);
  1379. if (cached)
  1380. return cached;
  1381. auto scaled = CSDL_Ext::scaleSurfaceFast(fullSurface, fullSurface->w * scale, fullSurface->h * scale);
  1382. if (scaled->format && scaled->format->palette) // fix color keying, because SDL loses it at this point
  1383. CSDL_Ext::setColorKey(scaled, scaled->format->palette->colors[0]);
  1384. return cacheWorldViewEntry(type, key, scaled);
  1385. }
  1386. SDL_Surface *CMapHandler::CMapCache::cacheWorldViewEntry(CMapHandler::EMapCacheType type, intptr_t key, SDL_Surface * entry)
  1387. {
  1388. if (!entry)
  1389. return nullptr;
  1390. if (requestWorldViewCache(type, key)) // valid cache already present, no need to do it again
  1391. return requestWorldViewCache(type, key);
  1392. data[type][key] = entry;
  1393. return entry;
  1394. }
  1395. intptr_t CMapHandler::CMapCache::genKey(intptr_t realPtr, ui8 mod)
  1396. {
  1397. return (intptr_t)(realPtr ^ (mod << (sizeof(intptr_t) - 2))); // maybe some cleaner method to pack rotation into cache key?
  1398. }
  1399. TerrainTile2::TerrainTile2()
  1400. :terbitmap(nullptr)
  1401. {}
  1402. bool CMapHandler::compareObjectBlitOrder(const CGObjectInstance * a, const CGObjectInstance * b)
  1403. {
  1404. if (!a)
  1405. return true;
  1406. if (!b)
  1407. return false;
  1408. if (a->appearance.printPriority != b->appearance.printPriority)
  1409. return a->appearance.printPriority > b->appearance.printPriority;
  1410. if(a->pos.y != b->pos.y)
  1411. return a->pos.y < b->pos.y;
  1412. if(b->ID==Obj::HERO && a->ID!=Obj::HERO)
  1413. return true;
  1414. if(b->ID!=Obj::HERO && a->ID==Obj::HERO)
  1415. return false;
  1416. if(!a->isVisitable() && b->isVisitable())
  1417. return true;
  1418. if(!b->isVisitable() && a->isVisitable())
  1419. return false;
  1420. if(a->pos.x < b->pos.x)
  1421. return true;
  1422. return false;
  1423. }
  1424. TerrainTileObject::TerrainTileObject(const CGObjectInstance * obj_, SDL_Rect rect_)
  1425. : obj(obj_),
  1426. rect(rect_),
  1427. fadeAnimKey(-1)
  1428. {
  1429. }
  1430. TerrainTileObject::~TerrainTileObject()
  1431. {
  1432. }