Graphics.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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 "gui/CAnimation.h"
  8. #include <SDL_ttf.h>
  9. #include "../lib/CThreadHelper.h"
  10. #include "CGameInfo.h"
  11. #include "../lib/VCMI_Lib.h"
  12. #include "../CCallback.h"
  13. #include "../lib/CHeroHandler.h"
  14. #include "../lib/CTownHandler.h"
  15. #include "../lib/CGeneralTextHandler.h"
  16. #include "../lib/CCreatureHandler.h"
  17. #include "CBitmapHandler.h"
  18. #include "../lib/spells/CSpellHandler.h"
  19. #include "../lib/CGameState.h"
  20. #include "../lib/JsonNode.h"
  21. #include "../lib/vcmi_endian.h"
  22. #include "../lib/CStopWatch.h"
  23. #include "../lib/mapObjects/CObjectClassesHandler.h"
  24. #include "../lib/mapObjects/CObjectHandler.h"
  25. using namespace CSDL_Ext;
  26. #ifdef min
  27. #undef min
  28. #endif
  29. #ifdef max
  30. #undef max
  31. #endif
  32. /*
  33. * Graphics.cpp, part of VCMI engine
  34. *
  35. * Authors: listed in file AUTHORS in main folder
  36. *
  37. * License: GNU General Public License v2.0 or later
  38. * Full text of license available in license.txt file, in main folder
  39. *
  40. */
  41. Graphics * graphics = nullptr;
  42. void Graphics::loadPaletteAndColors()
  43. {
  44. auto textFile = CResourceHandler::get()->load(ResourceID("DATA/PLAYERS.PAL"))->readAll();
  45. std::string pals((char*)textFile.first.get(), textFile.second);
  46. playerColorPalette = new SDL_Color[256];
  47. neutralColor = new SDL_Color;
  48. playerColors = new SDL_Color[PlayerColor::PLAYER_LIMIT_I];
  49. int startPoint = 24; //beginning byte; used to read
  50. for(int i=0; i<256; ++i)
  51. {
  52. SDL_Color col;
  53. col.r = pals[startPoint++];
  54. col.g = pals[startPoint++];
  55. col.b = pals[startPoint++];
  56. col.a = SDL_ALPHA_OPAQUE;
  57. startPoint++;
  58. playerColorPalette[i] = col;
  59. }
  60. neutralColorPalette = new SDL_Color[32];
  61. auto stream = CResourceHandler::get()->load(ResourceID("config/NEUTRAL.PAL"));
  62. CBinaryReader reader(stream.get());
  63. for(int i=0; i<32; ++i)
  64. {
  65. neutralColorPalette[i].r = reader.readUInt8();
  66. neutralColorPalette[i].g = reader.readUInt8();
  67. neutralColorPalette[i].b = reader.readUInt8();
  68. reader.readUInt8(); // this is "flags" entry, not alpha
  69. neutralColorPalette[i].a = SDL_ALPHA_OPAQUE;
  70. }
  71. //colors initialization
  72. SDL_Color colors[] = {
  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. for(int i=0;i<8;i++)
  82. {
  83. playerColors[i] = colors[i];
  84. }
  85. //gray
  86. neutralColor->r = 0x84;
  87. neutralColor->g = 0x84;
  88. neutralColor->b = 0x84;
  89. neutralColor->a = SDL_ALPHA_OPAQUE;
  90. }
  91. void Graphics::initializeBattleGraphics()
  92. {
  93. const JsonNode config(ResourceID("config/battles_graphics.json"));
  94. // Reserve enough space for the terrains
  95. int idx = config["backgrounds"].Vector().size();
  96. battleBacks.resize(idx+1); // 1 to idx, 0 is unused
  97. idx = 1;
  98. for(const JsonNode &t : config["backgrounds"].Vector()) {
  99. battleBacks[idx].push_back(t.String());
  100. idx++;
  101. }
  102. //initialization of AC->def name mapping
  103. for(const JsonNode &ac : config["ac_mapping"].Vector()) {
  104. int ACid = ac["id"].Float();
  105. std::vector< std::string > toAdd;
  106. for(const JsonNode &defname : ac["defnames"].Vector()) {
  107. toAdd.push_back(defname.String());
  108. }
  109. battleACToDef[ACid] = toAdd;
  110. }
  111. }
  112. Graphics::Graphics()
  113. {
  114. #if 0
  115. std::vector<Task> tasks; //preparing list of graphics to load
  116. tasks += std::bind(&Graphics::loadFonts,this);
  117. tasks += std::bind(&Graphics::loadPaletteAndColors,this);
  118. tasks += std::bind(&Graphics::initializeBattleGraphics,this);
  119. tasks += std::bind(&Graphics::loadErmuToPicture,this);
  120. tasks += std::bind(&Graphics::initializeImageLists,this);
  121. CThreadHelper th(&tasks,std::max((ui32)1,boost::thread::hardware_concurrency()));
  122. th.run();
  123. #else
  124. loadFonts();
  125. loadPaletteAndColors();
  126. initializeBattleGraphics();
  127. loadErmuToPicture();
  128. initializeImageLists();
  129. #endif
  130. //(!) do not load any CAnimation here
  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. for(size_t frame = 0; frame < anim->size(sourceGroup); ++frame)
  200. {
  201. anim->duplicateImage(sourceGroup, frame, targetGroup);
  202. IImage * image = anim->getImage(frame, targetGroup);
  203. image->verticalFlip();
  204. }
  205. }
  206. return anim;
  207. }
  208. std::shared_ptr<CAnimation> Graphics::loadHeroAnimation(const std::string &name)
  209. {
  210. //first - group number to be rotated, second - group number after rotation
  211. static const std::vector<std::pair<int,int> > rotations =
  212. {
  213. {6,10}, {7,11}, {8,12}, {1,13},
  214. {2,14}, {3,15}
  215. };
  216. std::shared_ptr<CAnimation> anim = std::make_shared<CAnimation>(name);
  217. anim->preload();
  218. for(const auto & rotation : rotations)
  219. {
  220. const int sourceGroup = rotation.first;
  221. const int targetGroup = rotation.second;
  222. for(size_t frame = 0; frame < anim->size(sourceGroup); ++frame)
  223. {
  224. anim->duplicateImage(sourceGroup, frame, targetGroup);
  225. IImage * image = anim->getImage(frame, targetGroup);
  226. image->verticalFlip();
  227. }
  228. }
  229. return anim;
  230. }
  231. void Graphics::blueToPlayersAdv(SDL_Surface * sur, PlayerColor player)
  232. {
  233. if(sur->format->palette)
  234. {
  235. SDL_Color *palette = nullptr;
  236. if(player < PlayerColor::PLAYER_LIMIT)
  237. {
  238. palette = playerColorPalette + 32*player.getNum();
  239. }
  240. else if(player == PlayerColor::NEUTRAL)
  241. {
  242. palette = neutralColorPalette;
  243. }
  244. else
  245. {
  246. logGlobal->errorStream() << "Wrong player id in blueToPlayersAdv (" << player << ")!";
  247. return;
  248. }
  249. SDL_SetColors(sur, palette, 224, 32);
  250. }
  251. else
  252. {
  253. //TODO: implement. H3 method works only for images with palettes.
  254. // Add some kind of player-colored overlay?
  255. // Or keep palette approach here and replace only colors of specific value(s)
  256. // Or just wait for OpenGL support?
  257. logGlobal->warnStream() << "Image must have palette to be player-colored!";
  258. }
  259. }
  260. void Graphics::loadFogOfWar()
  261. {
  262. fogOfWarFullHide = std::make_shared<CAnimation>("TSHRC");
  263. fogOfWarFullHide->preload();
  264. fogOfWarPartialHide = std::make_shared<CAnimation>("TSHRE");
  265. fogOfWarPartialHide->preload();
  266. static const int rotations [] = {22, 15, 2, 13, 12, 16, 28, 17, 20, 19, 7, 24, 26, 25, 30, 32, 27};
  267. size_t size = fogOfWarPartialHide->size(0);//group size after next rotation
  268. for(const int rotation : rotations)
  269. {
  270. fogOfWarPartialHide->duplicateImage(0, rotation, 0);
  271. IImage * image = fogOfWarPartialHide->getImage(size, 0);
  272. image->verticalFlip();
  273. size++;
  274. }
  275. }
  276. void Graphics::loadFonts()
  277. {
  278. const JsonNode config(ResourceID("config/fonts.json"));
  279. const JsonVector & bmpConf = config["bitmap"].Vector();
  280. const JsonNode & ttfConf = config["trueType"];
  281. const JsonNode & hanConf = config["bitmapHan"];
  282. assert(bmpConf.size() == FONTS_NUMBER);
  283. for (size_t i=0; i<FONTS_NUMBER; i++)
  284. {
  285. std::string filename = bmpConf[i].String();
  286. if (!hanConf[filename].isNull())
  287. fonts[i] = new CBitmapHanFont(hanConf[filename]);
  288. else if (!ttfConf[filename].isNull()) // no ttf override
  289. fonts[i] = new CTrueTypeFont(ttfConf[filename]);
  290. else
  291. fonts[i] = new CBitmapFont(filename);
  292. }
  293. }
  294. std::shared_ptr<CAnimation> Graphics::getAnimation(const CGObjectInstance* obj)
  295. {
  296. return getAnimation(obj->appearance);
  297. }
  298. std::shared_ptr<CAnimation> Graphics::getAnimation(const ObjectTemplate & info)
  299. {
  300. //the only(?) invisible object
  301. if(info.id == Obj::EVENT)
  302. {
  303. return std::shared_ptr<CAnimation>();
  304. }
  305. if(info.animationFile.empty())
  306. {
  307. logGlobal->warnStream() << boost::format("Def name for obj (%d,%d) is empty!") % info.id % info.subid;
  308. return std::shared_ptr<CAnimation>();
  309. }
  310. std::shared_ptr<CAnimation> ret = mapObjectAnimations[info.animationFile];
  311. //already loaded
  312. if(ret)
  313. {
  314. ret->preload();
  315. return ret;
  316. }
  317. ret = std::make_shared<CAnimation>(info.animationFile);
  318. mapObjectAnimations[info.animationFile] = ret;
  319. ret->preload();
  320. return ret;
  321. }
  322. void Graphics::loadErmuToPicture()
  323. {
  324. //loading ERMU to picture
  325. const JsonNode config(ResourceID("config/ERMU_to_picture.json"));
  326. int etp_idx = 0;
  327. for(const JsonNode &etp : config["ERMU_to_picture"].Vector()) {
  328. int idx = 0;
  329. for(const JsonNode &n : etp.Vector()) {
  330. ERMUtoPicture[idx][etp_idx] = n.String();
  331. idx ++;
  332. }
  333. assert (idx == ARRAY_COUNT(ERMUtoPicture));
  334. etp_idx ++;
  335. }
  336. assert (etp_idx == 44);
  337. }
  338. void Graphics::addImageListEntry(size_t index, std::string listName, std::string imageName)
  339. {
  340. if (!imageName.empty())
  341. {
  342. JsonNode entry;
  343. entry["frame"].Float() = index;
  344. entry["file"].String() = imageName;
  345. imageLists["SPRITES/" + listName]["images"].Vector().push_back(entry);
  346. }
  347. }
  348. void Graphics::initializeImageLists()
  349. {
  350. for(const CCreature * creature : CGI->creh->creatures)
  351. {
  352. addImageListEntry(creature->iconIndex, "CPRSMALL", creature->smallIconName);
  353. addImageListEntry(creature->iconIndex, "TWCRPORT", creature->largeIconName);
  354. }
  355. for(const CHero * hero : CGI->heroh->heroes)
  356. {
  357. addImageListEntry(hero->imageIndex, "UN32", hero->iconSpecSmall);
  358. addImageListEntry(hero->imageIndex, "UN44", hero->iconSpecLarge);
  359. addImageListEntry(hero->imageIndex, "PORTRAITSLARGE", hero->portraitLarge);
  360. addImageListEntry(hero->imageIndex, "PORTRAITSSMALL", hero->portraitSmall);
  361. }
  362. for(const CArtifact * art : CGI->arth->artifacts)
  363. {
  364. addImageListEntry(art->iconIndex, "ARTIFACT", art->image);
  365. addImageListEntry(art->iconIndex, "ARTIFACTLARGE", art->large);
  366. }
  367. for(const CFaction * faction : CGI->townh->factions)
  368. {
  369. if (faction->town)
  370. {
  371. auto & info = faction->town->clientInfo;
  372. addImageListEntry(info.icons[0][0], "ITPT", info.iconLarge[0][0]);
  373. addImageListEntry(info.icons[0][1], "ITPT", info.iconLarge[0][1]);
  374. addImageListEntry(info.icons[1][0], "ITPT", info.iconLarge[1][0]);
  375. addImageListEntry(info.icons[1][1], "ITPT", info.iconLarge[1][1]);
  376. addImageListEntry(info.icons[0][0] + 2, "ITPA", info.iconSmall[0][0]);
  377. addImageListEntry(info.icons[0][1] + 2, "ITPA", info.iconSmall[0][1]);
  378. addImageListEntry(info.icons[1][0] + 2, "ITPA", info.iconSmall[1][0]);
  379. addImageListEntry(info.icons[1][1] + 2, "ITPA", info.iconSmall[1][1]);
  380. }
  381. }
  382. for(const CSpell * spell : CGI->spellh->objects)
  383. {
  384. addImageListEntry(spell->id, "SPELLS", spell->iconBook);
  385. addImageListEntry(spell->id+1, "SPELLINT", spell->iconEffect);
  386. addImageListEntry(spell->id, "SPELLBON", spell->iconScenarioBonus);
  387. addImageListEntry(spell->id, "SPELLSCR", spell->iconScroll);
  388. }
  389. }