Graphics.cpp 13 KB

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