Graphics.cpp 13 KB

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