Graphics.cpp 14 KB

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