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