graphics.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. //code is copied from vcmiclient/Graphics.cpp with minimal changes
  11. #include "StdInc.h"
  12. #include "graphics.h"
  13. #include <vcmi/Entity.h>
  14. #include <vcmi/ArtifactService.h>
  15. #include <vcmi/CreatureService.h>
  16. #include <vcmi/FactionService.h>
  17. #include <vcmi/HeroTypeService.h>
  18. #include <vcmi/SkillService.h>
  19. #include <vcmi/spells/Service.h>
  20. #include "../lib/filesystem/Filesystem.h"
  21. #include "../lib/filesystem/CBinaryReader.h"
  22. #include "Animation.h"
  23. #include "../lib/CThreadHelper.h"
  24. #include "../lib/CModHandler.h"
  25. #include "../lib/VCMI_Lib.h"
  26. #include "../CCallback.h"
  27. #include "../lib/CGeneralTextHandler.h"
  28. #include "BitmapHandler.h"
  29. #include "../lib/JsonNode.h"
  30. #include "../lib/CStopWatch.h"
  31. #include "../lib/mapObjectConstructors/AObjectTypeHandler.h"
  32. #include "../lib/mapObjectConstructors/CObjectClassesHandler.h"
  33. #include "../lib/mapObjects/CGObjectInstance.h"
  34. #include "../lib/mapObjects/ObjectTemplate.h"
  35. #include "../lib/CHeroHandler.h"
  36. Graphics * graphics = nullptr;
  37. void Graphics::loadPaletteAndColors()
  38. {
  39. auto textFile = CResourceHandler::get()->load(ResourceID("DATA/PLAYERS.PAL"))->readAll();
  40. std::string pals((char*)textFile.first.get(), textFile.second);
  41. playerColorPalette.resize(256);
  42. playerColors.resize(PlayerColor::PLAYER_LIMIT_I);
  43. int startPoint = 24; //beginning byte; used to read
  44. for(int i = 0; i < 256; ++i)
  45. {
  46. QColor col;
  47. col.setRed(pals[startPoint++]);
  48. col.setGreen(pals[startPoint++]);
  49. col.setBlue(pals[startPoint++]);
  50. col.setAlpha(255);
  51. startPoint++;
  52. playerColorPalette[i] = col.rgba();
  53. }
  54. neutralColorPalette.resize(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. QColor col;
  60. col.setRed(reader.readUInt8());
  61. col.setGreen(reader.readUInt8());
  62. col.setBlue(reader.readUInt8());
  63. col.setAlpha(255);
  64. reader.readUInt8(); // this is "flags" entry, not alpha
  65. neutralColorPalette[i] = col.rgba();
  66. }
  67. //colors initialization
  68. QColor colors[] = {
  69. {0xff,0, 0, 255},
  70. {0x31,0x52,0xff,255},
  71. {0x9c,0x73,0x52,255},
  72. {0x42,0x94,0x29,255},
  73. {0xff,0x84,0, 255},
  74. {0x8c,0x29,0xa5,255},
  75. {0x09,0x9c,0xa5,255},
  76. {0xc6,0x7b,0x8c,255}};
  77. for(int i=0;i<8;i++)
  78. {
  79. playerColors[i] = colors[i].rgba();
  80. }
  81. //gray
  82. neutralColor = qRgba(0x84, 0x84, 0x84, 0xFF);
  83. }
  84. Graphics::Graphics()
  85. {
  86. #if 0
  87. std::vector<Task> tasks; //preparing list of graphics to load
  88. tasks += std::bind(&Graphics::loadFonts,this);
  89. tasks += std::bind(&Graphics::loadPaletteAndColors,this);
  90. tasks += std::bind(&Graphics::initializeBattleGraphics,this);
  91. tasks += std::bind(&Graphics::loadErmuToPicture,this);
  92. tasks += std::bind(&Graphics::initializeImageLists,this);
  93. CThreadHelper th(&tasks,std::max((ui32)1,boost::thread::hardware_concurrency()));
  94. th.run();
  95. #else
  96. loadPaletteAndColors();
  97. initializeImageLists();
  98. #endif
  99. //(!) do not load any CAnimation here
  100. }
  101. Graphics::~Graphics()
  102. {
  103. }
  104. void Graphics::load()
  105. {
  106. loadHeroAnimations();
  107. loadHeroFlagAnimations();
  108. }
  109. void Graphics::loadHeroAnimations()
  110. {
  111. for(auto & elem : VLC->heroh->classes.objects)
  112. {
  113. for(auto templ : VLC->objtypeh->getHandlerFor(Obj::HERO, elem->getIndex())->getTemplates())
  114. {
  115. if(!heroAnimations.count(templ->animationFile))
  116. heroAnimations[templ->animationFile] = loadHeroAnimation(templ->animationFile);
  117. }
  118. }
  119. boatAnimations[0] = loadHeroAnimation("AB01_.DEF");
  120. boatAnimations[1] = loadHeroAnimation("AB02_.DEF");
  121. boatAnimations[2] = loadHeroAnimation("AB03_.DEF");
  122. mapObjectAnimations["AB01_.DEF"] = boatAnimations[0];
  123. mapObjectAnimations["AB02_.DEF"] = boatAnimations[1];
  124. mapObjectAnimations["AB03_.DEF"] = boatAnimations[2];
  125. }
  126. void Graphics::loadHeroFlagAnimations()
  127. {
  128. static const std::vector<std::string> HERO_FLAG_ANIMATIONS =
  129. {
  130. "AF00", "AF01","AF02","AF03",
  131. "AF04", "AF05","AF06","AF07"
  132. };
  133. static const std::vector< std::vector<std::string> > BOAT_FLAG_ANIMATIONS =
  134. {
  135. {
  136. "ABF01L", "ABF01G", "ABF01R", "ABF01D",
  137. "ABF01B", "ABF01P", "ABF01W", "ABF01K"
  138. },
  139. {
  140. "ABF02L", "ABF02G", "ABF02R", "ABF02D",
  141. "ABF02B", "ABF02P", "ABF02W", "ABF02K"
  142. },
  143. {
  144. "ABF03L", "ABF03G", "ABF03R", "ABF03D",
  145. "ABF03B", "ABF03P", "ABF03W", "ABF03K"
  146. }
  147. };
  148. for(const auto & name : HERO_FLAG_ANIMATIONS)
  149. heroFlagAnimations.push_back(loadHeroFlagAnimation(name));
  150. for(int i = 0; i < BOAT_FLAG_ANIMATIONS.size(); i++)
  151. for(const auto & name : BOAT_FLAG_ANIMATIONS[i])
  152. boatFlagAnimations[i].push_back(loadHeroFlagAnimation(name));
  153. }
  154. std::shared_ptr<Animation> Graphics::loadHeroFlagAnimation(const std::string & name)
  155. {
  156. //first - group number to be rotated, second - group number after rotation
  157. static const std::vector<std::pair<int,int> > rotations =
  158. {
  159. {6,10}, {7,11}, {8,12}, {1,13},
  160. {2,14}, {3,15}
  161. };
  162. std::shared_ptr<Animation> anim = std::make_shared<Animation>(name);
  163. anim->preload();
  164. for(const auto & rotation : rotations)
  165. {
  166. const int sourceGroup = rotation.first;
  167. const int targetGroup = rotation.second;
  168. anim->createFlippedGroup(sourceGroup, targetGroup);
  169. }
  170. return anim;
  171. }
  172. std::shared_ptr<Animation> Graphics::loadHeroAnimation(const std::string &name)
  173. {
  174. //first - group number to be rotated, second - group number after rotation
  175. static const std::vector<std::pair<int,int> > rotations =
  176. {
  177. {6,10}, {7,11}, {8,12}, {1,13},
  178. {2,14}, {3,15}
  179. };
  180. std::shared_ptr<Animation> anim = std::make_shared<Animation>(name);
  181. anim->preload();
  182. for(const auto & rotation : rotations)
  183. {
  184. const int sourceGroup = rotation.first;
  185. const int targetGroup = rotation.second;
  186. anim->createFlippedGroup(sourceGroup, targetGroup);
  187. }
  188. return anim;
  189. }
  190. void Graphics::blueToPlayersAdv(QImage * sur, PlayerColor player)
  191. {
  192. if(sur->format() == QImage::Format_Indexed8)
  193. {
  194. auto palette = sur->colorTable();
  195. if(player < PlayerColor::PLAYER_LIMIT)
  196. {
  197. for(int i = 0; i < 32; ++i)
  198. palette[224 + i] = playerColorPalette[player.getNum() * 32 + i];
  199. }
  200. else if(player == PlayerColor::NEUTRAL)
  201. {
  202. palette = neutralColorPalette;
  203. }
  204. else
  205. {
  206. logGlobal->error("Wrong player id in blueToPlayersAdv (%s)!", player.getStr());
  207. return;
  208. }
  209. //FIXME: not all player colored images have player palette at last 32 indexes
  210. //NOTE: following code is much more correct but still not perfect (bugged with status bar)
  211. sur->setColorTable(palette);
  212. }
  213. else
  214. {
  215. //TODO: implement. H3 method works only for images with palettes.
  216. // Add some kind of player-colored overlay?
  217. // Or keep palette approach here and replace only colors of specific value(s)
  218. // Or just wait for OpenGL support?
  219. logGlobal->warn("Image must have palette to be player-colored!");
  220. }
  221. }
  222. std::shared_ptr<Animation> Graphics::getAnimation(const CGObjectInstance* obj)
  223. {
  224. if(obj->ID == Obj::HERO)
  225. return getHeroAnimation(obj->appearance);
  226. return getAnimation(obj->appearance);
  227. }
  228. std::shared_ptr<Animation> Graphics::getHeroAnimation(const std::shared_ptr<const ObjectTemplate> info)
  229. {
  230. if(info->animationFile.empty())
  231. {
  232. logGlobal->warn("Def name for hero (%d,%d) is empty!", info->id, info->subid);
  233. return std::shared_ptr<Animation>();
  234. }
  235. std::shared_ptr<Animation> ret = loadHeroAnimation(info->animationFile);
  236. //already loaded
  237. if(ret)
  238. {
  239. ret->preload();
  240. return ret;
  241. }
  242. ret = std::make_shared<Animation>(info->animationFile);
  243. heroAnimations[info->animationFile] = ret;
  244. ret->preload();
  245. return ret;
  246. }
  247. std::shared_ptr<Animation> Graphics::getAnimation(const std::shared_ptr<const ObjectTemplate> info)
  248. {
  249. if(info->animationFile.empty())
  250. {
  251. logGlobal->warn("Def name for obj (%d,%d) is empty!", info->id, info->subid);
  252. return std::shared_ptr<Animation>();
  253. }
  254. std::shared_ptr<Animation> ret = mapObjectAnimations[info->animationFile];
  255. //already loaded
  256. if(ret)
  257. {
  258. ret->preload();
  259. return ret;
  260. }
  261. ret = std::make_shared<Animation>(info->animationFile);
  262. mapObjectAnimations[info->animationFile] = ret;
  263. ret->preload();
  264. return ret;
  265. }
  266. void Graphics::addImageListEntry(size_t index, size_t group, const std::string & listName, const std::string & imageName)
  267. {
  268. if (!imageName.empty())
  269. {
  270. JsonNode entry;
  271. if(group != 0)
  272. entry["group"].Integer() = group;
  273. entry["frame"].Integer() = index;
  274. entry["file"].String() = imageName;
  275. imageLists["SPRITES/" + listName]["images"].Vector().push_back(entry);
  276. }
  277. }
  278. void Graphics::addImageListEntries(const EntityService * service)
  279. {
  280. auto cb = std::bind(&Graphics::addImageListEntry, this, _1, _2, _3, _4);
  281. auto loopCb = [&](const Entity * entity, bool & stop)
  282. {
  283. entity->registerIcons(cb);
  284. };
  285. service->forEachBase(loopCb);
  286. }
  287. void Graphics::initializeImageLists()
  288. {
  289. addImageListEntries(VLC->creatures());
  290. addImageListEntries(VLC->heroTypes());
  291. addImageListEntries(VLC->artifacts());
  292. addImageListEntries(VLC->factions());
  293. addImageListEntries(VLC->spells());
  294. addImageListEntries(VLC->skills());
  295. }