Graphics.cpp 12 KB

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