graphics.cpp 8.9 KB

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