Animation.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. /*
  2. * Animation.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/CAnimation.cpp with minimal changes
  11. #include "StdInc.h"
  12. #include "Animation.h"
  13. #include "BitmapHandler.h"
  14. #include "../lib/vcmi_endian.h"
  15. #include "../lib/filesystem/Filesystem.h"
  16. #include "../lib/filesystem/ISimpleResourceLoader.h"
  17. #include "../lib/JsonNode.h"
  18. #include "../lib/CRandomGenerator.h"
  19. #include "../lib/VCMIDirs.h"
  20. typedef std::map<size_t, std::vector<JsonNode>> source_map;
  21. /// Class for def loading
  22. /// After loading will store general info (palette and frame offsets) and pointer to file itself
  23. class DefFile
  24. {
  25. private:
  26. struct SSpriteDef
  27. {
  28. ui32 size;
  29. ui32 format; /// format in which pixel data is stored
  30. ui32 fullWidth; /// full width and height of frame, including borders
  31. ui32 fullHeight;
  32. ui32 width; /// width and height of pixel data, borders excluded
  33. ui32 height;
  34. si32 leftMargin;
  35. si32 topMargin;
  36. };
  37. //offset[group][frame] - offset of frame data in file
  38. std::map<size_t, std::vector <size_t> > offset;
  39. std::unique_ptr<ui8[]> data;
  40. std::unique_ptr<QVector<QRgb>> palette;
  41. public:
  42. DefFile(std::string Name);
  43. ~DefFile();
  44. std::shared_ptr<QImage> loadFrame(size_t frame, size_t group) const;
  45. const std::map<size_t, size_t> getEntries() const;
  46. };
  47. class ImageLoader
  48. {
  49. QImage * image;
  50. ui8 * lineStart;
  51. ui8 * position;
  52. QPoint spriteSize, margins, fullSize;
  53. public:
  54. //load size raw pixels from data
  55. inline void Load(size_t size, const ui8 * data);
  56. //set size pixels to color
  57. inline void Load(size_t size, ui8 color=0);
  58. inline void EndLine();
  59. //init image with these sizes and palette
  60. inline void init(QPoint SpriteSize, QPoint Margins, QPoint FullSize);
  61. ImageLoader(QImage * Img);
  62. ~ImageLoader();
  63. };
  64. // Extremely simple file cache. TODO: smarter, more general solution
  65. class FileCache
  66. {
  67. static const int cacheSize = 50; //Max number of cached files
  68. struct FileData
  69. {
  70. ResourceID name;
  71. size_t size;
  72. std::unique_ptr<ui8[]> data;
  73. std::unique_ptr<ui8[]> getCopy()
  74. {
  75. auto ret = std::unique_ptr<ui8[]>(new ui8[size]);
  76. std::copy(data.get(), data.get() + size, ret.get());
  77. return ret;
  78. }
  79. FileData(ResourceID name_, size_t size_, std::unique_ptr<ui8[]> data_):
  80. name{std::move(name_)},
  81. size{size_},
  82. data{std::move(data_)}
  83. {}
  84. };
  85. std::deque<FileData> cache;
  86. public:
  87. std::unique_ptr<ui8[]> getCachedFile(ResourceID rid)
  88. {
  89. for(auto & file : cache)
  90. {
  91. if(file.name == rid)
  92. return file.getCopy();
  93. }
  94. // Still here? Cache miss
  95. if(cache.size() > cacheSize)
  96. cache.pop_front();
  97. auto data = CResourceHandler::get()->load(rid)->readAll();
  98. cache.emplace_back(std::move(rid), data.second, std::move(data.first));
  99. return cache.back().getCopy();
  100. }
  101. };
  102. enum class DefType : uint32_t
  103. {
  104. SPELL = 0x40,
  105. SPRITE = 0x41,
  106. CREATURE = 0x42,
  107. MAP = 0x43,
  108. MAP_HERO = 0x44,
  109. TERRAIN = 0x45,
  110. CURSOR = 0x46,
  111. INTERFACE = 0x47,
  112. SPRITE_FRAME = 0x48,
  113. BATTLE_HERO = 0x49
  114. };
  115. static FileCache animationCache;
  116. /*************************************************************************
  117. * DefFile, class used for def loading *
  118. *************************************************************************/
  119. DefFile::DefFile(std::string Name):
  120. data(nullptr)
  121. {
  122. #if 0
  123. static QRgba H3_ORIG_PALETTE[8] =
  124. {
  125. { 0, 255, 255, SDL_ALPHA_OPAQUE},
  126. {255, 150, 255, SDL_ALPHA_OPAQUE},
  127. {255, 100, 255, SDL_ALPHA_OPAQUE},
  128. {255, 50, 255, SDL_ALPHA_OPAQUE},
  129. {255, 0, 255, SDL_ALPHA_OPAQUE},
  130. {255, 255, 0, SDL_ALPHA_OPAQUE},
  131. {180, 0, 255, SDL_ALPHA_OPAQUE},
  132. { 0, 255, 0, SDL_ALPHA_OPAQUE}
  133. };
  134. #endif // 0
  135. //First 8 colors in def palette used for transparency
  136. static QRgb H3Palette[8] =
  137. {
  138. qRgba(0, 0, 0, 0), // 100% - transparency
  139. qRgba(0, 0, 0, 32), // 75% - shadow border,
  140. qRgba(0, 0, 0, 64), // TODO: find exact value
  141. qRgba(0, 0, 0, 128), // TODO: for transparency
  142. qRgba(0, 0, 0, 128), // 50% - shadow body
  143. qRgba(0, 0, 0, 0), // 100% - selection highlight
  144. qRgba(0, 0, 0, 128), // 50% - shadow body below selection
  145. qRgba(0, 0, 0, 64) // 75% - shadow border below selection
  146. };
  147. data = animationCache.getCachedFile(ResourceID(std::string("SPRITES/") + Name, EResType::ANIMATION));
  148. palette = std::make_unique<QVector<QRgb>>(256);
  149. int it = 0;
  150. ui32 type = read_le_u32(data.get() + it);
  151. it += 4;
  152. //int width = read_le_u32(data + it); it+=4;//not used
  153. //int height = read_le_u32(data + it); it+=4;
  154. it += 8;
  155. ui32 totalBlocks = read_le_u32(data.get() + it);
  156. it += 4;
  157. for (ui32 i= 0; i<256; i++)
  158. {
  159. ui8 c[3];
  160. c[0] = data[it++];
  161. c[1] = data[it++];
  162. c[2] = data[it++];
  163. (*palette)[i] = qRgba(c[0], c[1], c[2], 255);
  164. }
  165. switch(static_cast<DefType>(type))
  166. {
  167. case DefType::SPELL:
  168. (*palette)[0] = H3Palette[0];
  169. break;
  170. case DefType::SPRITE:
  171. case DefType::SPRITE_FRAME:
  172. for(ui32 i= 0; i<8; i++)
  173. (*palette)[i] = H3Palette[i];
  174. break;
  175. case DefType::CREATURE:
  176. (*palette)[0] = H3Palette[0];
  177. (*palette)[1] = H3Palette[1];
  178. (*palette)[4] = H3Palette[4];
  179. (*palette)[5] = H3Palette[5];
  180. (*palette)[6] = H3Palette[6];
  181. (*palette)[7] = H3Palette[7];
  182. break;
  183. case DefType::MAP:
  184. case DefType::MAP_HERO:
  185. (*palette)[0] = H3Palette[0];
  186. (*palette)[1] = H3Palette[1];
  187. (*palette)[4] = H3Palette[4];
  188. //5 = owner flag, handled separately
  189. break;
  190. case DefType::TERRAIN:
  191. (*palette)[0] = H3Palette[0];
  192. (*palette)[1] = H3Palette[1];
  193. (*palette)[2] = H3Palette[2];
  194. (*palette)[3] = H3Palette[3];
  195. (*palette)[4] = H3Palette[4];
  196. break;
  197. case DefType::CURSOR:
  198. (*palette)[0] = H3Palette[0];
  199. break;
  200. case DefType::INTERFACE:
  201. (*palette)[0] = H3Palette[0];
  202. (*palette)[1] = H3Palette[1];
  203. (*palette)[4] = H3Palette[4];
  204. //player colors handled separately
  205. //TODO: disallow colorizing other def types
  206. break;
  207. case DefType::BATTLE_HERO:
  208. (*palette)[0] = H3Palette[0];
  209. (*palette)[1] = H3Palette[1];
  210. (*palette)[4] = H3Palette[4];
  211. break;
  212. default:
  213. logAnim->error("Unknown def type %d in %s", type, Name);
  214. break;
  215. }
  216. for (ui32 i=0; i<totalBlocks; i++)
  217. {
  218. size_t blockID = read_le_u32(data.get() + it);
  219. it+=4;
  220. size_t totalEntries = read_le_u32(data.get() + it);
  221. it+=12;
  222. //8 unknown bytes - skipping
  223. //13 bytes for name of every frame in this block - not used, skipping
  224. it+= 13 * (int)totalEntries;
  225. for (ui32 j=0; j<totalEntries; j++)
  226. {
  227. size_t currOffset = read_le_u32(data.get() + it);
  228. offset[blockID].push_back(currOffset);
  229. it += 4;
  230. }
  231. }
  232. }
  233. std::shared_ptr<QImage> DefFile::loadFrame(size_t frame, size_t group) const
  234. {
  235. std::map<size_t, std::vector <size_t> >::const_iterator it;
  236. it = offset.find(group);
  237. assert (it != offset.end());
  238. const ui8 * FDef = data.get()+it->second[frame];
  239. const SSpriteDef sd = * reinterpret_cast<const SSpriteDef *>(FDef);
  240. SSpriteDef sprite;
  241. sprite.format = read_le_u32(&sd.format);
  242. sprite.fullWidth = read_le_u32(&sd.fullWidth);
  243. sprite.fullHeight = read_le_u32(&sd.fullHeight);
  244. sprite.width = read_le_u32(&sd.width);
  245. sprite.height = read_le_u32(&sd.height);
  246. sprite.leftMargin = read_le_u32(&sd.leftMargin);
  247. sprite.topMargin = read_le_u32(&sd.topMargin);
  248. ui32 currentOffset = sizeof(SSpriteDef);
  249. //special case for some "old" format defs (SGTWMTA.DEF and SGTWMTB.DEF)
  250. if(sprite.format == 1 && sprite.width > sprite.fullWidth && sprite.height > sprite.fullHeight)
  251. {
  252. sprite.leftMargin = 0;
  253. sprite.topMargin = 0;
  254. sprite.width = sprite.fullWidth;
  255. sprite.height = sprite.fullHeight;
  256. currentOffset -= 16;
  257. }
  258. const ui32 BaseOffset = currentOffset;
  259. std::shared_ptr<QImage> img = std::make_shared<QImage>(sprite.fullWidth, sprite.fullHeight, QImage::Format_Indexed8);
  260. if(!img)
  261. throw std::runtime_error("Image memory cannot be allocated");
  262. ImageLoader loader(img.get());
  263. loader.init(QPoint(sprite.width, sprite.height),
  264. QPoint(sprite.leftMargin, sprite.topMargin),
  265. QPoint(sprite.fullWidth, sprite.fullHeight));
  266. switch(sprite.format)
  267. {
  268. case 0:
  269. {
  270. //pixel data is not compressed, copy data to surface
  271. for(ui32 i=0; i<sprite.height; i++)
  272. {
  273. loader.Load(sprite.width, FDef + currentOffset);
  274. currentOffset += sprite.width;
  275. loader.EndLine();
  276. }
  277. break;
  278. }
  279. case 1:
  280. {
  281. //for each line we have offset of pixel data
  282. const ui32 * RWEntriesLoc = reinterpret_cast<const ui32 *>(FDef+currentOffset);
  283. currentOffset += sizeof(ui32) * sprite.height;
  284. for(ui32 i=0; i<sprite.height; i++)
  285. {
  286. //get position of the line
  287. currentOffset=BaseOffset + read_le_u32(RWEntriesLoc + i);
  288. ui32 TotalRowLength = 0;
  289. while(TotalRowLength<sprite.width)
  290. {
  291. ui8 segmentType = FDef[currentOffset++];
  292. ui32 length = FDef[currentOffset++] + 1;
  293. if(segmentType==0xFF)//Raw data
  294. {
  295. loader.Load(length, FDef + currentOffset);
  296. currentOffset+=length;
  297. }
  298. else// RLE
  299. {
  300. loader.Load(length, segmentType);
  301. }
  302. TotalRowLength += length;
  303. }
  304. loader.EndLine();
  305. }
  306. break;
  307. }
  308. case 2:
  309. {
  310. currentOffset = BaseOffset + read_le_u16(FDef + BaseOffset);
  311. for(ui32 i=0; i<sprite.height; i++)
  312. {
  313. ui32 TotalRowLength=0;
  314. while(TotalRowLength<sprite.width)
  315. {
  316. ui8 segment=FDef[currentOffset++];
  317. ui8 code = segment / 32;
  318. ui8 length = (segment & 31) + 1;
  319. if(code==7)//Raw data
  320. {
  321. loader.Load(length, FDef + currentOffset);
  322. currentOffset += length;
  323. }
  324. else//RLE
  325. {
  326. loader.Load(length, code);
  327. }
  328. TotalRowLength+=length;
  329. }
  330. loader.EndLine();
  331. }
  332. break;
  333. }
  334. case 3:
  335. {
  336. for(ui32 i=0; i<sprite.height; i++)
  337. {
  338. currentOffset = BaseOffset + read_le_u16(FDef + BaseOffset+i*2*(sprite.width/32));
  339. ui32 TotalRowLength=0;
  340. while(TotalRowLength<sprite.width)
  341. {
  342. ui8 segment = FDef[currentOffset++];
  343. ui8 code = segment / 32;
  344. ui8 length = (segment & 31) + 1;
  345. if(code==7)//Raw data
  346. {
  347. loader.Load(length, FDef + currentOffset);
  348. currentOffset += length;
  349. }
  350. else//RLE
  351. {
  352. loader.Load(length, code);
  353. }
  354. TotalRowLength += length;
  355. }
  356. loader.EndLine();
  357. }
  358. break;
  359. }
  360. default:
  361. logGlobal->error("Error: unsupported format of def file: %d", sprite.format);
  362. break;
  363. }
  364. img->setColorTable(*palette);
  365. return img;
  366. }
  367. DefFile::~DefFile() = default;
  368. const std::map<size_t, size_t > DefFile::getEntries() const
  369. {
  370. std::map<size_t, size_t > ret;
  371. for (auto & elem : offset)
  372. ret[elem.first] = elem.second.size();
  373. return ret;
  374. }
  375. /*************************************************************************
  376. * Classes for image loaders - helpers for loading from def files *
  377. *************************************************************************/
  378. ImageLoader::ImageLoader(QImage * Img):
  379. image(Img),
  380. lineStart(Img->bits()),
  381. position(Img->bits())
  382. {
  383. }
  384. void ImageLoader::init(QPoint SpriteSize, QPoint Margins, QPoint FullSize)
  385. {
  386. spriteSize = SpriteSize;
  387. margins = Margins;
  388. fullSize = FullSize;
  389. memset((void *)image->bits(), 0, fullSize.y() * image->bytesPerLine());
  390. lineStart = image->bits();
  391. lineStart += margins.y() * image->bytesPerLine() + margins.x();
  392. position = lineStart;
  393. }
  394. inline void ImageLoader::Load(size_t size, const ui8 * data)
  395. {
  396. if(size)
  397. {
  398. memcpy((void *)position, data, size);
  399. position += size;
  400. }
  401. }
  402. inline void ImageLoader::Load(size_t size, ui8 color)
  403. {
  404. if(size)
  405. {
  406. memset((void *)position, color, size);
  407. position += size;
  408. }
  409. }
  410. inline void ImageLoader::EndLine()
  411. {
  412. lineStart += image->bytesPerLine();
  413. position = lineStart;
  414. }
  415. ImageLoader::~ImageLoader()
  416. {
  417. }
  418. /*************************************************************************
  419. * Classes for images, support loading from file and drawing on surface *
  420. *************************************************************************/
  421. std::shared_ptr<QImage> Animation::getFromExtraDef(std::string filename)
  422. {
  423. size_t pos = filename.find(':');
  424. if(pos == -1)
  425. return nullptr;
  426. Animation anim(filename.substr(0, pos));
  427. pos++;
  428. size_t frame = atoi(filename.c_str()+pos);
  429. size_t group = 0;
  430. pos = filename.find(':', pos);
  431. if(pos != -1)
  432. {
  433. pos++;
  434. group = frame;
  435. frame = atoi(filename.c_str()+pos);
  436. }
  437. anim.load(frame ,group);
  438. auto ret = anim.images[group][frame];
  439. anim.images.clear();
  440. return ret;
  441. }
  442. bool Animation::loadFrame(size_t frame, size_t group)
  443. {
  444. if(size(group) <= frame)
  445. {
  446. printError(frame, group, "LoadFrame");
  447. return false;
  448. }
  449. auto image = getImage(frame, group, false);
  450. if(image)
  451. {
  452. return true;
  453. }
  454. //try to get image from def
  455. if(source[group][frame].getType() == JsonNode::JsonType::DATA_NULL)
  456. {
  457. if(defFile)
  458. {
  459. auto frameList = defFile->getEntries();
  460. if(vstd::contains(frameList, group) && frameList.at(group) > frame) // frame is present
  461. {
  462. images[group][frame] = defFile->loadFrame(frame, group);
  463. return true;
  464. }
  465. }
  466. return false;
  467. // still here? image is missing
  468. printError(frame, group, "LoadFrame");
  469. images[group][frame] = std::make_shared<QImage>("DEFAULT");
  470. }
  471. else //load from separate file
  472. {
  473. images[group][frame] = getFromExtraDef(source[group][frame]["file"].String());;
  474. return true;
  475. }
  476. return false;
  477. }
  478. bool Animation::unloadFrame(size_t frame, size_t group)
  479. {
  480. auto image = getImage(frame, group, false);
  481. if(image)
  482. {
  483. images[group].erase(frame);
  484. if(images[group].empty())
  485. images.erase(group);
  486. return true;
  487. }
  488. return false;
  489. }
  490. void Animation::init()
  491. {
  492. if(defFile)
  493. {
  494. const std::map<size_t, size_t> defEntries = defFile->getEntries();
  495. for (auto & defEntry : defEntries)
  496. source[defEntry.first].resize(defEntry.second);
  497. }
  498. #if 0 //this code is not used but maybe requred if there will be configurable sprites
  499. ResourceID resID(std::string("SPRITES/") + name, EResType::TEXT);
  500. //if(vstd::contains(graphics->imageLists, resID.getName()))
  501. //initFromJson(graphics->imageLists[resID.getName()]);
  502. auto configList = CResourceHandler::get()->getResourcesWithName(resID);
  503. for(auto & loader : configList)
  504. {
  505. auto stream = loader->load(resID);
  506. std::unique_ptr<ui8[]> textData(new ui8[stream->getSize()]);
  507. stream->read(textData.get(), stream->getSize());
  508. const JsonNode config((char*)textData.get(), stream->getSize());
  509. //initFromJson(config);
  510. }
  511. #endif
  512. }
  513. void Animation::printError(size_t frame, size_t group, std::string type) const
  514. {
  515. logGlobal->error("%s error: Request for frame not present in CAnimation! File name: %s, Group: %d, Frame: %d", type, name, group, frame);
  516. }
  517. Animation::Animation(std::string Name):
  518. name(Name),
  519. preloaded(false),
  520. defFile()
  521. {
  522. size_t dotPos = name.find_last_of('.');
  523. if( dotPos!=-1 )
  524. name.erase(dotPos);
  525. std::transform(name.begin(), name.end(), name.begin(), toupper);
  526. ResourceID resource(std::string("SPRITES/") + name, EResType::ANIMATION);
  527. if(CResourceHandler::get()->existsResource(resource))
  528. defFile = std::make_shared<DefFile>(name);
  529. init();
  530. if(source.empty())
  531. logAnim->error("Animation %s failed to load", Name);
  532. }
  533. Animation::Animation():
  534. name(""),
  535. preloaded(false),
  536. defFile()
  537. {
  538. init();
  539. }
  540. Animation::~Animation() = default;
  541. void Animation::duplicateImage(const size_t sourceGroup, const size_t sourceFrame, const size_t targetGroup)
  542. {
  543. if(!source.count(sourceGroup))
  544. {
  545. logAnim->error("Group %d missing in %s", sourceGroup, name);
  546. return;
  547. }
  548. if(source[sourceGroup].size() <= sourceFrame)
  549. {
  550. logAnim->error("Frame [%d %d] missing in %s", sourceGroup, sourceFrame, name);
  551. return;
  552. }
  553. //todo: clone actual loaded Image object
  554. JsonNode clone(source[sourceGroup][sourceFrame]);
  555. if(clone.getType() == JsonNode::JsonType::DATA_NULL)
  556. {
  557. std::string temp = name+":"+boost::lexical_cast<std::string>(sourceGroup)+":"+boost::lexical_cast<std::string>(sourceFrame);
  558. clone["file"].String() = temp;
  559. }
  560. source[targetGroup].push_back(clone);
  561. size_t index = source[targetGroup].size() - 1;
  562. if(preloaded)
  563. load(index, targetGroup);
  564. }
  565. void Animation::setCustom(std::string filename, size_t frame, size_t group)
  566. {
  567. if(source[group].size() <= frame)
  568. source[group].resize(frame+1);
  569. source[group][frame]["file"].String() = filename;
  570. //FIXME: update image if already loaded
  571. }
  572. std::shared_ptr<QImage> Animation::getImage(size_t frame, size_t group, bool verbose) const
  573. {
  574. auto groupIter = images.find(group);
  575. if(groupIter != images.end())
  576. {
  577. auto imageIter = groupIter->second.find(frame);
  578. if(imageIter != groupIter->second.end())
  579. return imageIter->second;
  580. }
  581. if(verbose)
  582. printError(frame, group, "GetImage");
  583. return nullptr;
  584. }
  585. void Animation::exportBitmaps(const QDir & path) const
  586. {
  587. if(images.empty())
  588. {
  589. logGlobal->error("Nothing to export, animation is empty");
  590. return;
  591. }
  592. QString actualPath = path.absolutePath() + "/" + QString::fromStdString(name);
  593. QDir().mkdir(actualPath);
  594. size_t counter = 0;
  595. for(const auto& groupPair : images)
  596. {
  597. size_t group = groupPair.first;
  598. for(const auto& imagePair : groupPair.second)
  599. {
  600. size_t frame = imagePair.first;
  601. const auto img = imagePair.second;
  602. QString filename = QString("%1_%2_%3.png").arg(QString::fromStdString(name)).arg(group).arg(frame);
  603. QString filePath = actualPath + "/" + filename;
  604. img->save(filePath, "PNG");
  605. counter++;
  606. }
  607. }
  608. logGlobal->info("Exported %d frames to %s", counter, actualPath.toStdString());
  609. }
  610. void Animation::load()
  611. {
  612. for (auto & elem : source)
  613. for (size_t image=0; image < elem.second.size(); image++)
  614. loadFrame(image, elem.first);
  615. }
  616. void Animation::unload()
  617. {
  618. for (auto & elem : source)
  619. for (size_t image=0; image < elem.second.size(); image++)
  620. unloadFrame(image, elem.first);
  621. }
  622. void Animation::preload()
  623. {
  624. if(!preloaded)
  625. {
  626. preloaded = true;
  627. load();
  628. }
  629. }
  630. void Animation::loadGroup(size_t group)
  631. {
  632. if(vstd::contains(source, group))
  633. for (size_t image=0; image < source[group].size(); image++)
  634. loadFrame(image, group);
  635. }
  636. void Animation::unloadGroup(size_t group)
  637. {
  638. if(vstd::contains(source, group))
  639. for (size_t image=0; image < source[group].size(); image++)
  640. unloadFrame(image, group);
  641. }
  642. void Animation::load(size_t frame, size_t group)
  643. {
  644. loadFrame(frame, group);
  645. }
  646. void Animation::unload(size_t frame, size_t group)
  647. {
  648. unloadFrame(frame, group);
  649. }
  650. size_t Animation::size(size_t group) const
  651. {
  652. auto iter = source.find(group);
  653. if(iter != source.end())
  654. return iter->second.size();
  655. return 0;
  656. }
  657. void Animation::horizontalFlip()
  658. {
  659. for(auto & group : images)
  660. for(auto & image : group.second)
  661. *image.second = image.second->transformed(QTransform::fromScale(-1, 1));
  662. }
  663. void Animation::verticalFlip()
  664. {
  665. for(auto & group : images)
  666. for(auto & image : group.second)
  667. *image.second = image.second->transformed(QTransform::fromScale(1, -1));
  668. }
  669. void Animation::playerColored(PlayerColor player)
  670. {
  671. #if 0 //can be required in image preview?
  672. for(auto & group : images)
  673. for(auto & image : group.second)
  674. image.second->playerColored(player);
  675. #endif
  676. }
  677. void Animation::createFlippedGroup(const size_t sourceGroup, const size_t targetGroup)
  678. {
  679. for(size_t frame = 0; frame < size(sourceGroup); ++frame)
  680. {
  681. duplicateImage(sourceGroup, frame, targetGroup);
  682. auto image = getImage(frame, targetGroup);
  683. *image = image->transformed(QTransform::fromScale(1, -1));
  684. }
  685. }