graphics.cpp 8.9 KB

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