Graphics.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. #include "StdInc.h"
  2. #include "Graphics.h"
  3. #include "../lib/filesystem/Filesystem.h"
  4. #include "../lib/filesystem/CBinaryReader.h"
  5. #include "CDefHandler.h"
  6. #include "gui/SDL_Extensions.h"
  7. #include <SDL_ttf.h>
  8. #include "../lib/CThreadHelper.h"
  9. #include "CGameInfo.h"
  10. #include "../lib/VCMI_Lib.h"
  11. #include "../CCallback.h"
  12. #include "../lib/CHeroHandler.h"
  13. #include "../lib/CTownHandler.h"
  14. #include "../lib/CObjectHandler.h"
  15. #include "../lib/CGeneralTextHandler.h"
  16. #include "../lib/CCreatureHandler.h"
  17. #include "CBitmapHandler.h"
  18. #include "../lib/CObjectHandler.h"
  19. #include "../lib/CDefObjInfoHandler.h"
  20. #include "../lib/CGameState.h"
  21. #include "../lib/JsonNode.h"
  22. #include "../lib/vcmi_endian.h"
  23. #include "../lib/GameConstants.h"
  24. #include "../lib/CStopWatch.h"
  25. using namespace boost::assign;
  26. using namespace CSDL_Ext;
  27. #ifdef min
  28. #undef min
  29. #endif
  30. #ifdef max
  31. #undef max
  32. #endif
  33. /*
  34. * Graphics.cpp, part of VCMI engine
  35. *
  36. * Authors: listed in file AUTHORS in main folder
  37. *
  38. * License: GNU General Public License v2.0 or later
  39. * Full text of license available in license.txt file, in main folder
  40. *
  41. */
  42. Graphics * graphics = nullptr;
  43. void Graphics::loadPaletteAndColors()
  44. {
  45. auto textFile = CResourceHandler::get()->load(ResourceID("DATA/PLAYERS.PAL"))->readAll();
  46. std::string pals((char*)textFile.first.get(), textFile.second);
  47. playerColorPalette = new SDL_Color[256];
  48. neutralColor = new SDL_Color;
  49. playerColors = new SDL_Color[PlayerColor::PLAYER_LIMIT_I];
  50. int startPoint = 24; //beginning byte; used to read
  51. for(int i=0; i<256; ++i)
  52. {
  53. SDL_Color col;
  54. col.r = pals[startPoint++];
  55. col.g = pals[startPoint++];
  56. col.b = pals[startPoint++];
  57. col.unused = 255;
  58. startPoint++;
  59. playerColorPalette[i] = col;
  60. }
  61. neutralColorPalette = new SDL_Color[32];
  62. auto stream = CResourceHandler::get()->load(ResourceID("config/NEUTRAL.PAL"));
  63. CBinaryReader reader(stream.get());
  64. for(int i=0; i<32; ++i)
  65. {
  66. neutralColorPalette[i].r = reader.readUInt8();
  67. neutralColorPalette[i].g = reader.readUInt8();
  68. neutralColorPalette[i].b = reader.readUInt8();
  69. neutralColorPalette[i].unused = reader.readUInt8();
  70. neutralColorPalette[i].unused = !neutralColorPalette[i].unused;
  71. }
  72. //colors initialization
  73. int3 kolory[] = {int3(0xff,0,0),int3(0x31,0x52,0xff),int3(0x9c,0x73,0x52),int3(0x42,0x94,0x29),
  74. int3(0xff,0x84,0x0),int3(0x8c,0x29,0xa5),int3(0x09,0x9c,0xa5),int3(0xc6,0x7b,0x8c)};
  75. for(int i=0;i<8;i++)
  76. {
  77. playerColors[i].r = kolory[i].x;
  78. playerColors[i].g = kolory[i].y;
  79. playerColors[i].b = kolory[i].z;
  80. playerColors[i].unused = 255;
  81. }
  82. neutralColor->r = 0x84; neutralColor->g = 0x84; neutralColor->b = 0x84; neutralColor->unused = 255;//gray
  83. }
  84. void Graphics::initializeBattleGraphics()
  85. {
  86. const JsonNode config(ResourceID("config/battles_graphics.json"));
  87. // Reserve enough space for the terrains
  88. int idx = config["backgrounds"].Vector().size();
  89. battleBacks.resize(idx+1); // 1 to idx, 0 is unused
  90. idx = 1;
  91. for(const JsonNode &t : config["backgrounds"].Vector()) {
  92. battleBacks[idx].push_back(t.String());
  93. idx++;
  94. }
  95. //initialization of AC->def name mapping
  96. for(const JsonNode &ac : config["ac_mapping"].Vector()) {
  97. int ACid = ac["id"].Float();
  98. std::vector< std::string > toAdd;
  99. for(const JsonNode &defname : ac["defnames"].Vector()) {
  100. toAdd.push_back(defname.String());
  101. }
  102. battleACToDef[ACid] = toAdd;
  103. }
  104. spellEffectsPics = CDefHandler::giveDefEss("SpellInt.def");
  105. }
  106. Graphics::Graphics()
  107. {
  108. std::vector<Task> tasks; //preparing list of graphics to load
  109. tasks += boost::bind(&Graphics::loadFonts,this);
  110. tasks += boost::bind(&Graphics::loadPaletteAndColors,this);
  111. tasks += boost::bind(&Graphics::loadHeroFlags,this);
  112. tasks += boost::bind(&Graphics::initializeBattleGraphics,this);
  113. tasks += boost::bind(&Graphics::loadErmuToPicture,this);
  114. tasks += boost::bind(&Graphics::initializeImageLists,this);
  115. tasks += GET_DEF_ESS(resources32,"RESOURCE.DEF");
  116. tasks += GET_DEF_ESS(spellscr,"SPELLSCR.DEF");
  117. tasks += GET_DEF_ESS(heroMoveArrows,"ADAG.DEF");
  118. CThreadHelper th(&tasks,std::max((ui32)1,boost::thread::hardware_concurrency()));
  119. th.run();
  120. for(auto & elem : heroMoveArrows->ourImages)
  121. {
  122. CSDL_Ext::alphaTransform(elem.bitmap);
  123. }
  124. }
  125. void Graphics::loadHeroAnims()
  126. {
  127. std::vector<std::pair<int,int> > rotations; //first - group number to be rotated1, second - group number after rotation1
  128. rotations += std::make_pair(6,10), std::make_pair(7,11), std::make_pair(8,12), std::make_pair(1,13),
  129. std::make_pair(2,14), std::make_pair(3,15);
  130. for(auto & elem : CGI->heroh->classes.heroClasses)
  131. {
  132. const CHeroClass * hc = elem;
  133. if (!vstd::contains(heroAnims, hc->imageMapFemale))
  134. heroAnims[hc->imageMapFemale] = loadHeroAnim(hc->imageMapFemale, rotations);
  135. if (!vstd::contains(heroAnims, hc->imageMapMale))
  136. heroAnims[hc->imageMapMale] = loadHeroAnim(hc->imageMapMale, rotations);
  137. }
  138. boatAnims.push_back(loadHeroAnim("AB01_.DEF", rotations));
  139. boatAnims.push_back(loadHeroAnim("AB02_.DEF", rotations));
  140. boatAnims.push_back(loadHeroAnim("AB03_.DEF", rotations));
  141. }
  142. CDefEssential * Graphics::loadHeroAnim( const std::string &name, const std::vector<std::pair<int,int> > &rotations)
  143. {
  144. CDefEssential *anim = CDefHandler::giveDefEss(name);
  145. int pom = 0; //how many groups has been rotated
  146. for(int o=7; pom<6; ++o)
  147. {
  148. for(int p=0;p<6;p++)
  149. {
  150. if(anim->ourImages[o].groupNumber == rotations[p].first)
  151. {
  152. for(int e=0; e<8; ++e)
  153. {
  154. Cimage nci;
  155. nci.bitmap = CSDL_Ext::rotate01(anim->ourImages[o+e].bitmap);
  156. nci.groupNumber = rotations[p].second;
  157. nci.imName = std::string();
  158. anim->ourImages.push_back(nci);
  159. if(pom>2) //we need only one frame for groups 13/14/15
  160. break;
  161. }
  162. if(pom<3) //there are eight frames of animtion of groups 6/7/8 so for speed we'll skip them
  163. o+=8;
  164. else //there is only one frame of 1/2/3
  165. o+=1;
  166. ++pom;
  167. if(p==2 && pom<4) //group1 starts at index 1
  168. o = 1;
  169. }
  170. }
  171. }
  172. for(auto & elem : anim->ourImages)
  173. {
  174. CSDL_Ext::alphaTransform(elem.bitmap);
  175. }
  176. return anim;
  177. }
  178. void Graphics::loadHeroFlagsDetail(std::pair<std::vector<CDefEssential *> Graphics::*, std::vector<const char *> > &pr, bool mode)
  179. {
  180. for(int i=0;i<8;i++)
  181. (this->*pr.first).push_back(CDefHandler::giveDefEss(pr.second[i]));
  182. std::vector<std::pair<int,int> > rotations; //first - group number to be rotated1, second - group number after rotation1
  183. rotations += std::make_pair(6,10), std::make_pair(7,11), std::make_pair(8,12);
  184. for(int q=0; q<8; ++q)
  185. {
  186. std::vector<Cimage> &curImgs = (this->*pr.first)[q]->ourImages;
  187. for(size_t o=0; o<curImgs.size(); ++o)
  188. {
  189. for(auto & rotation : rotations)
  190. {
  191. if(curImgs[o].groupNumber==rotation.first)
  192. {
  193. for(int e=0; e<8; ++e)
  194. {
  195. Cimage nci;
  196. nci.bitmap = CSDL_Ext::rotate01(curImgs[o+e].bitmap);
  197. nci.groupNumber = rotation.second;
  198. nci.imName = std::string();
  199. curImgs.push_back(nci);
  200. }
  201. o+=8;
  202. }
  203. }
  204. }
  205. if (mode)
  206. {
  207. for(size_t o=0; o<curImgs.size(); ++o)
  208. {
  209. if(curImgs[o].groupNumber==1 || curImgs[o].groupNumber==2 || curImgs[o].groupNumber==3)
  210. {
  211. for(int e=0; e<8; ++e)
  212. {
  213. Cimage nci;
  214. nci.bitmap = CSDL_Ext::rotate01(curImgs[o+e].bitmap);
  215. nci.groupNumber = 12 + curImgs[o].groupNumber;
  216. nci.imName = std::string();
  217. curImgs.push_back(nci);
  218. }
  219. o+=8;
  220. }
  221. }
  222. }
  223. for(auto & curImg : curImgs)
  224. {
  225. SDL_SetColorKey(curImg.bitmap, SDL_SRCCOLORKEY,
  226. SDL_MapRGB(curImg.bitmap->format, 0, 255, 255)
  227. );
  228. }
  229. }
  230. }
  231. void Graphics::loadHeroFlags()
  232. {
  233. using namespace boost::assign;
  234. CStopWatch th;
  235. std::pair<std::vector<CDefEssential *> Graphics::*, std::vector<const char *> > pr[4];
  236. pr[0].first = &Graphics::flags1;
  237. pr[0].second+=("ABF01L.DEF"),("ABF01G.DEF"),("ABF01R.DEF"),("ABF01D.DEF"),("ABF01B.DEF"),
  238. ("ABF01P.DEF"),("ABF01W.DEF"),("ABF01K.DEF");
  239. pr[1].first = &Graphics::flags2;
  240. pr[1].second+=("ABF02L.DEF"),("ABF02G.DEF"),("ABF02R.DEF"),("ABF02D.DEF"),("ABF02B.DEF"),
  241. ("ABF02P.DEF"),("ABF02W.DEF"),("ABF02K.DEF");
  242. pr[2].first = &Graphics::flags3;
  243. pr[2].second+=("ABF03L.DEF"),("ABF03G.DEF"),("ABF03R.DEF"),("ABF03D.DEF"),("ABF03B.DEF"),
  244. ("ABF03P.DEF"),("ABF03W.DEF"),("ABF03K.DEF");
  245. pr[3].first = &Graphics::flags4;
  246. pr[3].second+=("AF00.DEF"),("AF01.DEF"),("AF02.DEF"),("AF03.DEF"),("AF04.DEF"),
  247. ("AF05.DEF"),("AF06.DEF"),("AF07.DEF");
  248. boost::thread_group grupa;
  249. for(int g=3; g>=0; --g)
  250. {
  251. grupa.create_thread(boost::bind(&Graphics::loadHeroFlagsDetail, this, boost::ref(pr[g]), true));
  252. }
  253. grupa.join_all();
  254. logGlobal->infoStream() << "Loading and transforming heroes' flags: "<<th.getDiff();
  255. }
  256. void Graphics::blueToPlayersAdv(SDL_Surface * sur, PlayerColor player)
  257. {
  258. // if(player==1) //it is actually blue...
  259. // return;
  260. if(sur->format->BitsPerPixel == 8)
  261. {
  262. SDL_Color *palette = nullptr;
  263. if(player < PlayerColor::PLAYER_LIMIT)
  264. {
  265. palette = playerColorPalette + 32*player.getNum();
  266. }
  267. else if(player == PlayerColor::NEUTRAL)
  268. {
  269. palette = neutralColorPalette;
  270. }
  271. else
  272. {
  273. logGlobal->errorStream() << "Wrong player id in blueToPlayersAdv (" << player << ")!";
  274. return;
  275. }
  276. SDL_SetColors(sur, palette, 224, 32);
  277. //for(int i=0; i<32; ++i)
  278. //{
  279. // sur->format->palette->colors[224+i] = palette[i];
  280. //}
  281. }
  282. else if(sur->format->BitsPerPixel == 24) //should never happen in general
  283. {
  284. for(int y=0; y<sur->h; ++y)
  285. {
  286. for(int x=0; x<sur->w; ++x)
  287. {
  288. Uint8* cp = (Uint8*)sur->pixels + y*sur->pitch + x*3;
  289. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  290. {
  291. if(cp[2]>cp[1] && cp[2]>cp[0])
  292. {
  293. std::vector<long long int> sort1;
  294. sort1.push_back(cp[0]);
  295. sort1.push_back(cp[1]);
  296. sort1.push_back(cp[2]);
  297. std::vector< std::pair<long long int, Uint8*> > sort2;
  298. sort2.push_back(std::make_pair(graphics->playerColors[player.getNum()].r, &(cp[0])));
  299. sort2.push_back(std::make_pair(graphics->playerColors[player.getNum()].g, &(cp[1])));
  300. sort2.push_back(std::make_pair(graphics->playerColors[player.getNum()].b, &(cp[2])));
  301. std::sort(sort1.begin(), sort1.end());
  302. if(sort2[0].first>sort2[1].first)
  303. std::swap(sort2[0], sort2[1]);
  304. if(sort2[1].first>sort2[2].first)
  305. std::swap(sort2[1], sort2[2]);
  306. if(sort2[0].first>sort2[1].first)
  307. std::swap(sort2[0], sort2[1]);
  308. for(int hh=0; hh<3; ++hh)
  309. {
  310. (*sort2[hh].second) = (sort1[hh] + sort2[hh].first)/2.2;
  311. }
  312. }
  313. }
  314. else
  315. {
  316. if(
  317. (/*(mode==0) && (cp[0]>cp[1]) && (cp[0]>cp[2])) ||
  318. ((mode==1) &&*/ (cp[2]<45) && (cp[0]>80) && (cp[1]<70) && ((cp[0]-cp[1])>40))
  319. )
  320. {
  321. std::vector<long long int> sort1;
  322. sort1.push_back(cp[2]);
  323. sort1.push_back(cp[1]);
  324. sort1.push_back(cp[0]);
  325. std::vector< std::pair<long long int, Uint8*> > sort2;
  326. sort2.push_back(std::make_pair(graphics->playerColors[player.getNum()].r, &(cp[2])));
  327. sort2.push_back(std::make_pair(graphics->playerColors[player.getNum()].g, &(cp[1])));
  328. sort2.push_back(std::make_pair(graphics->playerColors[player.getNum()].b, &(cp[0])));
  329. std::sort(sort1.begin(), sort1.end());
  330. if(sort2[0].first>sort2[1].first)
  331. std::swap(sort2[0], sort2[1]);
  332. if(sort2[1].first>sort2[2].first)
  333. std::swap(sort2[1], sort2[2]);
  334. if(sort2[0].first>sort2[1].first)
  335. std::swap(sort2[0], sort2[1]);
  336. for(int hh=0; hh<3; ++hh)
  337. {
  338. (*sort2[hh].second) = (sort1[hh]*0.8 + sort2[hh].first)/2;
  339. }
  340. }
  341. }
  342. }
  343. }
  344. }
  345. }
  346. void Graphics::loadFonts()
  347. {
  348. const JsonNode config(ResourceID("config/fonts.json"));
  349. const JsonVector & bmpConf = config["bitmap"].Vector();
  350. const JsonNode & ttfConf = config["trueType"];
  351. assert(bmpConf.size() == FONTS_NUMBER);
  352. for (size_t i=0; i<FONTS_NUMBER; i++)
  353. {
  354. std::string filename = bmpConf[i].String();
  355. if (ttfConf[filename].isNull()) // no ttf override
  356. fonts[i] = new CBitmapFont(filename);
  357. else
  358. fonts[i] = new CTrueTypeFont(ttfConf[filename]);
  359. }
  360. }
  361. CDefEssential * Graphics::getDef( const CGObjectInstance * obj )
  362. {
  363. return advmapobjGraphics[obj->defInfo->name];
  364. }
  365. CDefEssential * Graphics::getDef( const CGDefInfo * info )
  366. {
  367. return advmapobjGraphics[info->name];
  368. }
  369. void Graphics::loadErmuToPicture()
  370. {
  371. //loading ERMU to picture
  372. const JsonNode config(ResourceID("config/ERMU_to_picture.json"));
  373. int etp_idx = 0;
  374. for(const JsonNode &etp : config["ERMU_to_picture"].Vector()) {
  375. int idx = 0;
  376. for(const JsonNode &n : etp.Vector()) {
  377. ERMUtoPicture[idx][etp_idx] = n.String();
  378. idx ++;
  379. }
  380. assert (idx == ARRAY_COUNT(ERMUtoPicture));
  381. etp_idx ++;
  382. }
  383. assert (etp_idx == 44);
  384. }
  385. void Graphics::addImageListEntry(size_t index, std::string listName, std::string imageName)
  386. {
  387. if (!imageName.empty())
  388. {
  389. if (!CResourceHandler::get()->existsResource(ResourceID("SPRITES/" + imageName, EResType::IMAGE)))
  390. logGlobal->errorStream() << "Required image " << "SPRITES/" << imageName << " is missing!";
  391. JsonNode entry;
  392. entry["frame"].Float() = index;
  393. entry["file"].String() = imageName;
  394. imageLists["SPRITES/" + listName]["images"].Vector().push_back(entry);
  395. }
  396. }
  397. void Graphics::initializeImageLists()
  398. {
  399. for(const CCreature * creature : CGI->creh->creatures)
  400. {
  401. addImageListEntry(creature->iconIndex, "CPRSMALL", creature->smallIconName);
  402. addImageListEntry(creature->iconIndex, "TWCRPORT", creature->largeIconName);
  403. }
  404. for(const CHero * hero : CGI->heroh->heroes)
  405. {
  406. addImageListEntry(hero->imageIndex, "UN32", hero->iconSpecSmall);
  407. addImageListEntry(hero->imageIndex, "UN44", hero->iconSpecLarge);
  408. addImageListEntry(hero->imageIndex, "PORTRAITSLARGE", hero->portraitLarge);
  409. addImageListEntry(hero->imageIndex, "PORTRAITSSMALL", hero->portraitSmall);
  410. }
  411. for(const CArtifact * art : CGI->arth->artifacts)
  412. {
  413. addImageListEntry(art->iconIndex, "ARTIFACT", art->image);
  414. addImageListEntry(art->iconIndex, "ARTIFACTLARGE", art->large);
  415. }
  416. for(const CFaction * faction : CGI->townh->factions)
  417. {
  418. if (faction->town)
  419. {
  420. auto & info = faction->town->clientInfo;
  421. addImageListEntry(info.icons[0][0], "ITPT", info.iconLarge[0][0]);
  422. addImageListEntry(info.icons[0][1], "ITPT", info.iconLarge[0][1]);
  423. addImageListEntry(info.icons[1][0], "ITPT", info.iconLarge[1][0]);
  424. addImageListEntry(info.icons[1][1], "ITPT", info.iconLarge[1][1]);
  425. addImageListEntry(info.icons[0][0] + 2, "ITPA", info.iconSmall[0][0]);
  426. addImageListEntry(info.icons[0][1] + 2, "ITPA", info.iconSmall[0][1]);
  427. addImageListEntry(info.icons[1][0] + 2, "ITPA", info.iconSmall[1][0]);
  428. addImageListEntry(info.icons[1][1] + 2, "ITPA", info.iconSmall[1][1]);
  429. }
  430. }
  431. }