Graphics.cpp 13 KB

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