Graphics.cpp 15 KB

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