graphics.cpp 9.1 KB

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