Graphics.cpp 14 KB

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