CAnimation.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485
  1. #include "StdInc.h"
  2. #include <SDL_image.h>
  3. #include "../lib/Filesystem/CResourceLoader.h"
  4. #include "../lib/Filesystem/ISimpleResourceLoader.h"
  5. #include "../lib/JsonNode.h"
  6. #include "CBitmapHandler.h"
  7. #include "Graphics.h"
  8. #include "CAnimation.h"
  9. #include "UIFramework/SDL_Extensions.h"
  10. #include "UIFramework/SDL_Pixels.h"
  11. /*
  12. * CAnimation.cpp, part of VCMI engine
  13. *
  14. * Authors: listed in file AUTHORS in main folder
  15. *
  16. * License: GNU General Public License v2.0 or later
  17. * Full text of license available in license.txt file, in main folder
  18. *
  19. */
  20. typedef std::map <size_t, std::vector <JsonNode> > source_map;
  21. typedef std::map<size_t, IImage* > image_map;
  22. typedef std::map<size_t, image_map > group_map;
  23. class SDLImageLoader
  24. {
  25. SDLImage * image;
  26. ui8 * lineStart;
  27. ui8 * position;
  28. public:
  29. //load size raw pixels from data
  30. inline void Load(size_t size, const ui8 * data);
  31. //set size pixels to color
  32. inline void Load(size_t size, ui8 color=0);
  33. inline void EndLine();
  34. //init image with these sizes and palette
  35. inline void init(Point SpriteSize, Point Margins, Point FullSize, SDL_Color *pal);
  36. SDLImageLoader(SDLImage * Img);
  37. ~SDLImageLoader();
  38. };
  39. class CompImageLoader
  40. {
  41. CompImage * image;
  42. ui8 *position;
  43. ui8 *entry;
  44. ui32 currentLine;
  45. inline ui8 typeOf(ui8 color);
  46. inline void NewEntry(ui8 color, size_t size);
  47. inline void NewEntry(const ui8 * &data, size_t size);
  48. public:
  49. //load size raw pixels from data
  50. inline void Load(size_t size, const ui8 * data);
  51. //set size pixels to color
  52. inline void Load(size_t size, ui8 color=0);
  53. inline void EndLine();
  54. //init image with these sizes and palette
  55. inline void init(Point SpriteSize, Point Margins, Point FullSize, SDL_Color *pal);
  56. CompImageLoader(CompImage * Img);
  57. ~CompImageLoader();
  58. };
  59. // Extremely simple file cache. TODO: smarter, more general solution
  60. class CFileCache
  61. {
  62. static const int cacheSize = 50; //Max number of cached files
  63. struct FileData
  64. {
  65. ResourceID name;
  66. size_t size;
  67. ui8 * data;
  68. ui8 * getCopy()
  69. {
  70. ui8 * ret = new ui8[size];
  71. std::copy(data, data + size, ret);
  72. return ret;
  73. }
  74. FileData():
  75. size(0),
  76. data(nullptr)
  77. {}
  78. ~FileData()
  79. {
  80. delete [] data;
  81. }
  82. };
  83. std::list<FileData> cache;
  84. public:
  85. ui8 * getCachedFile(ResourceID && rid)
  86. {
  87. BOOST_FOREACH(auto & file, cache)
  88. {
  89. if (file.name == rid)
  90. return file.getCopy();
  91. }
  92. // Still here? Cache miss
  93. if (cache.size() > cacheSize)
  94. cache.pop_front();
  95. cache.push_back(FileData());
  96. auto data = CResourceHandler::get()->loadData(rid);
  97. cache.back().name = ResourceID(rid);
  98. cache.back().size = data.second;
  99. cache.back().data = data.first.release();
  100. return cache.back().getCopy();
  101. }
  102. };
  103. static CFileCache animationCache;
  104. /*************************************************************************
  105. * DefFile, class used for def loading *
  106. *************************************************************************/
  107. CDefFile::CDefFile(std::string Name):
  108. data(NULL),
  109. palette(NULL)
  110. {
  111. //First 8 colors in def palette used for transparency
  112. static SDL_Color H3Palette[8] =
  113. {
  114. { 0, 0, 0, 0},// 100% - transparency
  115. { 0, 0, 0, 64},// 75% - shadow border,
  116. { 0, 0, 0, 128},// TODO: find exact value
  117. { 0, 0, 0, 128},// TODO: for transparency
  118. { 0, 0, 0, 128},// 50% - shadow body
  119. { 0, 0, 0, 0},// 100% - selection highlight
  120. { 0, 0, 0, 128},// 50% - shadow body below selection
  121. { 0, 0, 0, 64} // 75% - shadow border below selection
  122. };
  123. data = animationCache.getCachedFile(ResourceID(std::string("SPRITES/") + Name, EResType::ANIMATION));
  124. palette = new SDL_Color[256];
  125. int it = 0;
  126. ui32 type = read_le_u32(data + it);
  127. it+=4;
  128. //int width = read_le_u32(data + it); it+=4;//not used
  129. //int height = read_le_u32(data + it); it+=4;
  130. it+=8;
  131. ui32 totalBlocks = read_le_u32(data + it);
  132. it+=4;
  133. for (ui32 i= 0; i<256; i++)
  134. {
  135. palette[i].r = data[it++];
  136. palette[i].g = data[it++];
  137. palette[i].b = data[it++];
  138. palette[i].unused = 255;
  139. }
  140. if (type == 71 || type == 64)//Buttons/buildings don't have shadows\semi-transparency
  141. memset(palette, 0, sizeof(SDL_Color)*2);
  142. else
  143. memcpy(palette, H3Palette, sizeof(SDL_Color)*8);//initialize shadow\selection colors
  144. for (ui32 i=0; i<totalBlocks; i++)
  145. {
  146. size_t blockID = read_le_u32(data + it);
  147. it+=4;
  148. size_t totalEntries = read_le_u32(data + it);
  149. it+=12;
  150. //8 unknown bytes - skipping
  151. //13 bytes for name of every frame in this block - not used, skipping
  152. it+= 13 * totalEntries;
  153. for (ui32 j=0; j<totalEntries; j++)
  154. {
  155. size_t currOffset = read_le_u32(data + it);
  156. offset[blockID].push_back(currOffset);
  157. it += 4;
  158. }
  159. }
  160. }
  161. template<class ImageLoader>
  162. void CDefFile::loadFrame(size_t frame, size_t group, ImageLoader &loader) const
  163. {
  164. std::map<size_t, std::vector <size_t> >::const_iterator it;
  165. it = offset.find(group);
  166. assert (it != offset.end());
  167. const ui8 * FDef = data+it->second[frame];
  168. const SSpriteDef sd = * reinterpret_cast<const SSpriteDef *>(FDef);
  169. SSpriteDef sprite;
  170. sprite.format = read_le_u32(&sd.format);
  171. sprite.fullWidth = read_le_u32(&sd.fullWidth);
  172. sprite.fullHeight = read_le_u32(&sd.fullHeight);
  173. sprite.width = read_le_u32(&sd.width);
  174. sprite.height = read_le_u32(&sd.height);
  175. sprite.leftMargin = read_le_u32(&sd.leftMargin);
  176. sprite.topMargin = read_le_u32(&sd.topMargin);
  177. ui32 currentOffset = sizeof(SSpriteDef);
  178. ui32 BaseOffset = sizeof(SSpriteDef);
  179. loader.init(Point(sprite.width, sprite.height),
  180. Point(sprite.leftMargin, sprite.topMargin),
  181. Point(sprite.fullWidth, sprite.fullHeight), palette);
  182. switch (sprite.format)
  183. {
  184. case 0:
  185. {
  186. //pixel data is not compressed, copy data to surface
  187. for (ui32 i=0; i<sprite.height; i++)
  188. {
  189. loader.Load(sprite.width, FDef[currentOffset]);
  190. currentOffset += sprite.width;
  191. loader.EndLine();
  192. }
  193. break;
  194. }
  195. case 1:
  196. {
  197. //for each line we have offset of pixel data
  198. const ui32 * RWEntriesLoc = reinterpret_cast<const ui32 *>(FDef+currentOffset);
  199. currentOffset += sizeof(ui32) * sprite.height;
  200. for (ui32 i=0; i<sprite.height; i++)
  201. {
  202. //get position of the line
  203. currentOffset=BaseOffset + read_le_u32(RWEntriesLoc + i);
  204. ui32 TotalRowLength = 0;
  205. while (TotalRowLength<sprite.width)
  206. {
  207. ui8 type=FDef[currentOffset++];
  208. ui32 length=FDef[currentOffset++] + 1;
  209. if (type==0xFF)//Raw data
  210. {
  211. loader.Load(length, FDef + currentOffset);
  212. currentOffset+=length;
  213. }
  214. else// RLE
  215. {
  216. loader.Load(length, type);
  217. }
  218. TotalRowLength += length;
  219. }
  220. loader.EndLine();
  221. }
  222. break;
  223. }
  224. case 2:
  225. {
  226. currentOffset = BaseOffset + read_le_u16(FDef + BaseOffset);
  227. for (ui32 i=0; i<sprite.height; i++)
  228. {
  229. ui32 TotalRowLength=0;
  230. while (TotalRowLength<sprite.width)
  231. {
  232. ui8 SegmentType=FDef[currentOffset++];
  233. ui8 code = SegmentType / 32;
  234. ui8 length = (SegmentType & 31) + 1;
  235. if (code==7)//Raw data
  236. {
  237. loader.Load(length, FDef[currentOffset]);
  238. currentOffset += length;
  239. }
  240. else//RLE
  241. {
  242. loader.Load(length, code);
  243. }
  244. TotalRowLength+=length;
  245. }
  246. loader.EndLine();
  247. }
  248. break;
  249. }
  250. case 3:
  251. {
  252. for (ui32 i=0; i<sprite.height; i++)
  253. {
  254. currentOffset = BaseOffset + read_le_u16(FDef + BaseOffset+i*2*(sprite.width/32));
  255. ui32 TotalRowLength=0;
  256. while (TotalRowLength<sprite.width)
  257. {
  258. ui8 segment = FDef[currentOffset++];
  259. ui8 code = segment / 32;
  260. ui8 length = (segment & 31) + 1;
  261. if (code==7)//Raw data
  262. {
  263. loader.Load(length, FDef + currentOffset);
  264. currentOffset += length;
  265. }
  266. else//RLE
  267. {
  268. loader.Load(length, code);
  269. }
  270. TotalRowLength += length;
  271. }
  272. loader.EndLine();
  273. }
  274. break;
  275. }
  276. default:
  277. tlog0<<"Error: unsupported format of def file: "<<sprite.format<<"\n";
  278. break;
  279. }
  280. };
  281. CDefFile::~CDefFile()
  282. {
  283. delete[] data;
  284. delete[] palette;
  285. }
  286. const std::map<size_t, size_t > CDefFile::getEntries() const
  287. {
  288. std::map<size_t, size_t > ret;
  289. for (std::map<size_t, std::vector <size_t> >::const_iterator mapIt = offset.begin(); mapIt!=offset.end(); ++mapIt)
  290. ret[mapIt->first] = mapIt->second.size();
  291. return ret;
  292. }
  293. /*************************************************************************
  294. * Classes for image loaders - helpers for loading from def files *
  295. *************************************************************************/
  296. SDLImageLoader::SDLImageLoader(SDLImage * Img):
  297. image(Img),
  298. lineStart(NULL),
  299. position(NULL)
  300. {
  301. }
  302. void SDLImageLoader::init(Point SpriteSize, Point Margins, Point FullSize, SDL_Color *pal)
  303. {
  304. //Init image
  305. image->surf = SDL_CreateRGBSurface(SDL_SWSURFACE, SpriteSize.x, SpriteSize.y, 8, 0, 0, 0, 0);
  306. image->margins = Margins;
  307. image->fullSize = FullSize;
  308. //Prepare surface
  309. SDL_SetColors(image->surf, pal, 0, 256);
  310. SDL_LockSurface(image->surf);
  311. lineStart = position = (ui8*)image->surf->pixels;
  312. }
  313. inline void SDLImageLoader::Load(size_t size, const ui8 * data)
  314. {
  315. if (size)
  316. {
  317. memcpy((void *)position, data, size);
  318. position += size;
  319. }
  320. }
  321. inline void SDLImageLoader::Load(size_t size, ui8 color)
  322. {
  323. if (size)
  324. {
  325. memset((void *)position, color, size);
  326. position += size;
  327. }
  328. }
  329. inline void SDLImageLoader::EndLine()
  330. {
  331. lineStart += image->surf->pitch;
  332. position = lineStart;
  333. }
  334. SDLImageLoader::~SDLImageLoader()
  335. {
  336. SDL_UnlockSurface(image->surf);
  337. SDL_SetColorKey(image->surf, SDL_SRCCOLORKEY, 0);
  338. //TODO: RLE if compressed and bpp>1
  339. }
  340. ////////////////////////////////////////////////////////////////////////////////
  341. CompImageLoader::CompImageLoader(CompImage * Img):
  342. image(Img),
  343. position(NULL),
  344. entry(NULL),
  345. currentLine(0)
  346. {
  347. }
  348. void CompImageLoader::init(Point SpriteSize, Point Margins, Point FullSize, SDL_Color *pal)
  349. {
  350. image->sprite = Rect(Margins, SpriteSize);
  351. image->fullSize = FullSize;
  352. if (SpriteSize.x && SpriteSize.y)
  353. {
  354. image->palette = new SDL_Color[256];
  355. memcpy((void*)image->palette, (void*)pal, 256*sizeof(SDL_Color));
  356. //Allocate enought space for worst possible case, c-style malloc used due to resizing after load
  357. image->surf = (ui8*)malloc(SpriteSize.x*SpriteSize.y*3);
  358. image->line = new ui32[SpriteSize.y+1];
  359. image->line[0] = 0;
  360. position = image->surf;
  361. }
  362. }
  363. inline void CompImageLoader::NewEntry(ui8 color, size_t size)
  364. {
  365. assert(color != 0xff);
  366. assert(size && size<256);
  367. entry = position;
  368. entry[0] = color;
  369. entry[1] = size;
  370. position +=2;
  371. }
  372. inline void CompImageLoader::NewEntry(const ui8 * &data, size_t size)
  373. {
  374. assert(size && size<256);
  375. entry = position;
  376. entry[0] = 0xff;
  377. entry[1] = size;
  378. position +=2;
  379. memcpy(position, data, size);
  380. position+=size;
  381. data+=size;
  382. }
  383. inline ui8 CompImageLoader::typeOf(ui8 color)
  384. {
  385. if (color == 0)
  386. return 0;
  387. if (image->palette[color].unused != 255)
  388. return 1;
  389. return 2;
  390. }
  391. inline void CompImageLoader::Load(size_t size, const ui8 * data)
  392. {
  393. while (size)
  394. {
  395. //Try to compress data
  396. while(true)
  397. {
  398. ui8 color = data[0];
  399. if (color != 0xff)
  400. {
  401. size_t runLength = 1;
  402. while (runLength < size && color == data[runLength])
  403. runLength++;
  404. if (runLength > 1 && runLength < 255)//Row of one color found - use RLE
  405. {
  406. Load(runLength, color);
  407. data += runLength;
  408. size -= runLength;
  409. if (!size)
  410. return;
  411. }
  412. else
  413. break;
  414. }
  415. else
  416. break;
  417. }
  418. //Select length for new raw entry
  419. size_t runLength = 1;
  420. ui8 color = data[0];
  421. ui8 type = typeOf(color);
  422. ui8 color2;
  423. ui8 type2;
  424. if (size > 1)
  425. {
  426. do
  427. {
  428. color2 = data[runLength];
  429. type2 = typeOf(color2);
  430. runLength++;
  431. }
  432. //While we have data of this type and different colors
  433. while ((runLength < size) && (type == type2) && ( (color2 != 0xff) || (color2 != color)));
  434. }
  435. size -= runLength;
  436. //add data to last entry
  437. if (entry && entry[0] == 0xff && type == typeOf(entry[2]))
  438. {
  439. size_t toCopy = std::min<size_t>(runLength, 255 - entry[1]);
  440. runLength -= toCopy;
  441. entry[1] += toCopy;
  442. memcpy(position, data, toCopy);
  443. data+=toCopy;
  444. position+=toCopy;
  445. }
  446. //Create new entries
  447. while (runLength > 255)
  448. {
  449. NewEntry(data, 255);
  450. runLength -= 255;
  451. }
  452. if (runLength)
  453. NewEntry(data, runLength);
  454. }
  455. }
  456. inline void CompImageLoader::Load(size_t size, ui8 color)
  457. {
  458. if (!size)
  459. return;
  460. if (color==0xff)
  461. {
  462. ui8* tmpbuf = new ui8[size];
  463. memset((void*)tmpbuf, color, size);
  464. Load(size, tmpbuf);
  465. delete [] tmpbuf;
  466. return;
  467. }
  468. //Current entry is RLE with same color as new block
  469. if (entry && entry[0] == color)
  470. {
  471. size_t toCopy = std::min<size_t>(size, 255 - entry[1]);
  472. size -= toCopy;
  473. entry[1] += toCopy;
  474. }
  475. //Create new entries
  476. while (size > 255)
  477. {
  478. NewEntry(color, 255);
  479. size -= 255;
  480. }
  481. if (size)
  482. NewEntry(color, size);
  483. }
  484. void CompImageLoader::EndLine()
  485. {
  486. currentLine++;
  487. image->line[currentLine] = position - image->surf;
  488. entry = NULL;
  489. }
  490. CompImageLoader::~CompImageLoader()
  491. {
  492. if (!image->surf)
  493. return;
  494. ui8* newPtr = (ui8*)realloc((void*)image->surf, position - image->surf);
  495. if (newPtr)
  496. image->surf = newPtr;
  497. }
  498. /*************************************************************************
  499. * Classes for images, support loading from file and drawing on surface *
  500. *************************************************************************/
  501. IImage::IImage():
  502. refCount(1)
  503. {
  504. }
  505. bool IImage::decreaseRef()
  506. {
  507. refCount--;
  508. return refCount <= 0;
  509. }
  510. void IImage::increaseRef()
  511. {
  512. refCount++;
  513. }
  514. SDLImage::SDLImage(CDefFile *data, size_t frame, size_t group, bool compressed):
  515. surf(NULL)
  516. {
  517. SDLImageLoader loader(this);
  518. data->loadFrame(frame, group, loader);
  519. }
  520. SDLImage::SDLImage(SDL_Surface * from, bool extraRef):
  521. margins(0,0)
  522. {
  523. surf = from;
  524. if (extraRef)
  525. surf->refcount++;
  526. fullSize.x = surf->w;
  527. fullSize.y = surf->h;
  528. }
  529. SDLImage::SDLImage(std::string filename, bool compressed):
  530. margins(0,0)
  531. {
  532. surf = BitmapHandler::loadBitmap(filename);
  533. if (surf == NULL)
  534. {
  535. tlog1 << "Error: failed to load image "<<filename<<"\n";
  536. }
  537. else
  538. {
  539. fullSize.x = surf->w;
  540. fullSize.y = surf->h;
  541. }
  542. if (compressed)
  543. {
  544. SDL_Surface *temp = surf;
  545. // add RLE flag
  546. if (surf->format->palette)
  547. {
  548. const SDL_Color &c = temp->format->palette->colors[0];
  549. SDL_SetColorKey(temp, (SDL_SRCCOLORKEY | SDL_RLEACCEL),
  550. SDL_MapRGB(temp -> format, c.r, c.g, c.b));
  551. }
  552. else
  553. SDL_SetColorKey(temp, SDL_RLEACCEL, 0);
  554. // convert surface to enable RLE
  555. surf = SDL_ConvertSurface(temp, temp->format, temp->flags);
  556. SDL_FreeSurface(temp);
  557. }
  558. }
  559. void SDLImage::draw(SDL_Surface *where, int posX, int posY, Rect *src, ui8 rotation) const
  560. {
  561. if (!surf)
  562. return;
  563. Rect sourceRect(margins.x, margins.y, surf->w, surf->h);
  564. //TODO: rotation and scaling
  565. if (src)
  566. {
  567. sourceRect = sourceRect & *src;
  568. }
  569. Rect destRect(posX, posY, surf->w, surf->h);
  570. destRect += sourceRect.topLeft();
  571. sourceRect -= margins;
  572. CSDL_Ext::blitSurface(surf, &sourceRect, where, &destRect);
  573. }
  574. void SDLImage::playerColored(PlayerColor player)
  575. {
  576. graphics->blueToPlayersAdv(surf, player);
  577. }
  578. int SDLImage::width() const
  579. {
  580. return fullSize.x;
  581. }
  582. int SDLImage::height() const
  583. {
  584. return fullSize.y;
  585. }
  586. SDLImage::~SDLImage()
  587. {
  588. SDL_FreeSurface(surf);
  589. }
  590. CompImage::CompImage(const CDefFile *data, size_t frame, size_t group):
  591. surf(NULL),
  592. line(NULL),
  593. palette(NULL)
  594. {
  595. CompImageLoader loader(this);
  596. data->loadFrame(frame, group, loader);
  597. }
  598. CompImage::CompImage(SDL_Surface * surf)
  599. {
  600. //TODO
  601. assert(0);
  602. }
  603. void CompImage::draw(SDL_Surface *where, int posX, int posY, Rect *src, ui8 alpha) const
  604. {
  605. int rotation = 0; //TODO
  606. //rotation & 2 = horizontal rotation
  607. //rotation & 4 = vertical rotation
  608. if (!surf)
  609. return;
  610. Rect sourceRect(sprite);
  611. //TODO: rotation and scaling
  612. if (src)
  613. sourceRect = sourceRect & *src;
  614. //Limit source rect to sizes of surface
  615. sourceRect = sourceRect & Rect(0, 0, where->w, where->h);
  616. //Starting point on SDL surface
  617. Point dest(posX+sourceRect.x, posY+sourceRect.y);
  618. if (rotation & 2)
  619. dest.y += sourceRect.h;
  620. if (rotation & 4)
  621. dest.x += sourceRect.w;
  622. sourceRect -= sprite.topLeft();
  623. for (int currY = 0; currY <sourceRect.h; currY++)
  624. {
  625. ui8* data = surf + line[currY+sourceRect.y];
  626. ui8 type = *(data++);
  627. ui8 size = *(data++);
  628. int currX = sourceRect.x;
  629. //Skip blocks until starting position reached
  630. while ( currX > size )
  631. {
  632. currX -= size;
  633. if (type == 0xff)
  634. data += size;
  635. type = *(data++);
  636. size = *(data++);
  637. }
  638. //This block will be shown partially - calculate size\position
  639. size -= currX;
  640. if (type == 0xff)
  641. data += currX;
  642. currX = 0;
  643. ui8 bpp = where->format->BytesPerPixel;
  644. //Calculate position for blitting: pixels + Y + X
  645. ui8* blitPos = (ui8*) where->pixels;
  646. if (rotation & 4)
  647. blitPos += (dest.y - currY) * where->pitch;
  648. else
  649. blitPos += (dest.y + currY) * where->pitch;
  650. blitPos += dest.x * bpp;
  651. //Blit blocks that must be fully visible
  652. while (currX + size < sourceRect.w)
  653. {
  654. //blit block, pointers will be modified if needed
  655. BlitBlockWithBpp(bpp, type, size, data, blitPos, alpha, rotation & 2);
  656. currX += size;
  657. type = *(data++);
  658. size = *(data++);
  659. }
  660. //Blit last, semi-visible block
  661. size = sourceRect.w - currX;
  662. BlitBlockWithBpp(bpp, type, size, data, blitPos, alpha, rotation & 2);
  663. }
  664. }
  665. #define CASEBPP(x,y) case x: BlitBlock<x,y>(type, size, data, dest, alpha); break
  666. //FIXME: better way to get blitter
  667. void CompImage::BlitBlockWithBpp(ui8 bpp, ui8 type, ui8 size, ui8 *&data, ui8 *&dest, ui8 alpha, bool rotated) const
  668. {
  669. assert(bpp>1 && bpp<5);
  670. if (rotated)
  671. switch (bpp)
  672. {
  673. CASEBPP(2,1);
  674. CASEBPP(3,1);
  675. CASEBPP(4,1);
  676. }
  677. else
  678. switch (bpp)
  679. {
  680. CASEBPP(2,1);
  681. CASEBPP(3,1);
  682. CASEBPP(4,1);
  683. }
  684. }
  685. #undef CASEBPP
  686. //Blit one block from RLE-d surface
  687. template<int bpp, int dir>
  688. void CompImage::BlitBlock(ui8 type, ui8 size, ui8 *&data, ui8 *&dest, ui8 alpha) const
  689. {
  690. //Raw data
  691. if (type == 0xff)
  692. {
  693. ui8 color = *data;
  694. if (alpha != 255)//Per-surface alpha is set
  695. {
  696. for (size_t i=0; i<size; i++)
  697. {
  698. SDL_Color col = palette[*(data++)];
  699. col.unused = (ui32)col.unused*alpha/255;
  700. ColorPutter<bpp, 1>::PutColorAlpha(dest, col);
  701. }
  702. return;
  703. }
  704. if (palette[color].unused == 255)
  705. {
  706. //Put row of RGB data
  707. for (size_t i=0; i<size; i++)
  708. ColorPutter<bpp, 1>::PutColor(dest, palette[*(data++)]);
  709. }
  710. else
  711. {
  712. //Put row of RGBA data
  713. for (size_t i=0; i<size; i++)
  714. ColorPutter<bpp, 1>::PutColorAlpha(dest, palette[*(data++)]);
  715. }
  716. }
  717. //RLE-d sequence
  718. else
  719. {
  720. if (alpha != 255 && palette[type].unused !=0)//Per-surface alpha is set
  721. {
  722. SDL_Color col = palette[type];
  723. col.unused = (int)col.unused*(255-alpha)/255;
  724. for (size_t i=0; i<size; i++)
  725. ColorPutter<bpp, 1>::PutColorAlpha(dest, col);
  726. return;
  727. }
  728. switch (palette[type].unused)
  729. {
  730. case 0:
  731. {
  732. //Skip row
  733. dest += size*bpp;
  734. break;
  735. }
  736. case 255:
  737. {
  738. //Put RGB row
  739. ColorPutter<bpp, 1>::PutColorRow(dest, palette[type], size);
  740. break;
  741. }
  742. default:
  743. {
  744. //Put RGBA row
  745. for (size_t i=0; i<size; i++)
  746. ColorPutter<bpp, 1>::PutColorAlpha(dest, palette[type]);
  747. break;
  748. }
  749. }
  750. }
  751. }
  752. void CompImage::playerColored(PlayerColor player)
  753. {
  754. SDL_Color *pal = NULL;
  755. if(player < PlayerColor::PLAYER_LIMIT)
  756. {
  757. pal = graphics->playerColorPalette + 32*player.getNum();
  758. }
  759. else if(player == PlayerColor::NEUTRAL)
  760. {
  761. pal = graphics->neutralColorPalette;
  762. }
  763. else
  764. assert(0);
  765. for(int i=0; i<32; ++i)
  766. {
  767. palette[224+i].r = pal[i].r;
  768. palette[224+i].g = pal[i].g;
  769. palette[224+i].b = pal[i].b;
  770. palette[224+i].unused = pal[i].unused;
  771. }
  772. }
  773. int CompImage::width() const
  774. {
  775. return fullSize.x;
  776. }
  777. int CompImage::height() const
  778. {
  779. return fullSize.y;
  780. }
  781. CompImage::~CompImage()
  782. {
  783. free(surf);
  784. delete [] line;
  785. delete [] palette;
  786. }
  787. /*************************************************************************
  788. * CAnimation for animations handling, can load part of file if needed *
  789. *************************************************************************/
  790. IImage * CAnimation::getFromExtraDef(std::string filename)
  791. {
  792. size_t pos = filename.find(':');
  793. if (pos == -1)
  794. return NULL;
  795. CAnimation anim(filename.substr(0, pos));
  796. pos++;
  797. size_t frame = atoi(filename.c_str()+pos);
  798. size_t group = 0;
  799. pos = filename.find(':', pos);
  800. if (pos != -1)
  801. {
  802. group = frame;
  803. frame = atoi(filename.c_str()+pos);
  804. }
  805. anim.load(frame ,group);
  806. IImage * ret = anim.images[group][frame];
  807. anim.images.clear();
  808. return ret;
  809. }
  810. bool CAnimation::loadFrame(CDefFile * file, size_t frame, size_t group)
  811. {
  812. if (size(group) <= frame)
  813. {
  814. printError(frame, group, "LoadFrame");
  815. return false;
  816. }
  817. IImage *image = getImage(frame, group, false);
  818. if (image)
  819. {
  820. image->increaseRef();
  821. return true;
  822. }
  823. //try to get image from def
  824. if (source[group][frame].getType() == JsonNode::DATA_NULL)
  825. {
  826. if (compressed)
  827. images[group][frame] = new CompImage(file, frame, group);
  828. else
  829. images[group][frame] = new SDLImage(file, frame, group);
  830. }
  831. else //load from separate file
  832. {
  833. std::string filename = source[group][frame].Struct().find("file")->second.String();
  834. IImage * img = getFromExtraDef(filename);
  835. if (!img)
  836. img = new SDLImage(filename, compressed);
  837. images[group][frame] = img;
  838. return true;
  839. }
  840. return false;
  841. }
  842. bool CAnimation::unloadFrame(size_t frame, size_t group)
  843. {
  844. IImage *image = getImage(frame, group, false);
  845. if (image)
  846. {
  847. //decrease ref count for image and delete if needed
  848. if (image->decreaseRef())
  849. {
  850. delete image;
  851. images[group].erase(frame);
  852. }
  853. if (images[group].empty())
  854. images.erase(group);
  855. return true;
  856. }
  857. return false;
  858. }
  859. void CAnimation::init(CDefFile * file)
  860. {
  861. if (file)
  862. {
  863. const std::map<size_t, size_t> defEntries = file->getEntries();
  864. for (std::map<size_t, size_t>::const_iterator mapIt = defEntries.begin(); mapIt!=defEntries.end(); ++mapIt)
  865. source[mapIt->first].resize(mapIt->second);
  866. }
  867. auto & configList = CResourceHandler::get()->getResourcesWithName(
  868. ResourceID(std::string("SPRITES/") + name, EResType::TEXT));
  869. BOOST_FOREACH(auto & entry, configList)
  870. {
  871. auto stream = entry.getLoader()->load(entry.getResourceName());
  872. std::unique_ptr<ui8[]> textData(new ui8[stream->getSize()]);
  873. stream->read(textData.get(), stream->getSize());
  874. const JsonNode config((char*)textData.get(), stream->getSize());
  875. std::string basepath;
  876. basepath = config["basepath"].String();
  877. BOOST_FOREACH(const JsonNode &group, config["sequences"].Vector())
  878. {
  879. size_t groupID = group["group"].Float();//TODO: string-to-value conversion("moving" -> MOVING)
  880. source[groupID].clear();
  881. BOOST_FOREACH(const JsonNode &frame, group["frames"].Vector())
  882. {
  883. source[groupID].push_back(JsonNode());
  884. std::string filename = frame.String();
  885. source[groupID].back()["file"].String() = basepath + filename;
  886. }
  887. }
  888. BOOST_FOREACH(const JsonNode &node, config["images"].Vector())
  889. {
  890. size_t group = node["group"].Float();
  891. size_t frame = node["frame"].Float();
  892. if (source[group].size() <= frame)
  893. source[group].resize(frame+1);
  894. source[group][frame] = node;
  895. std::string filename = node["file"].String();
  896. source[group][frame]["file"].String() = basepath + filename;
  897. }
  898. }
  899. }
  900. CDefFile * CAnimation::getFile() const
  901. {
  902. ResourceID identifier(std::string("SPRITES/") + name, EResType::ANIMATION);
  903. if (CResourceHandler::get()->existsResource(identifier))
  904. return new CDefFile(name);
  905. return NULL;
  906. }
  907. void CAnimation::printError(size_t frame, size_t group, std::string type) const
  908. {
  909. tlog0 << type <<" error: Request for frame not present in CAnimation!\n"
  910. <<"\tFile name: "<<name<<" Group: "<<group<<" Frame: "<<frame<<"\n";
  911. }
  912. CAnimation::CAnimation(std::string Name, bool Compressed):
  913. name(Name),
  914. compressed(Compressed)
  915. {
  916. size_t dotPos = name.find_last_of('.');
  917. if ( dotPos!=-1 )
  918. name.erase(dotPos);
  919. std::transform(name.begin(), name.end(), name.begin(), toupper);
  920. CDefFile * file = getFile();
  921. init(file);
  922. delete file;
  923. loadedAnims.insert(this);
  924. }
  925. CAnimation::CAnimation():
  926. name(""),
  927. compressed(false)
  928. {
  929. init(NULL);
  930. loadedAnims.insert(this);
  931. }
  932. CAnimation::~CAnimation()
  933. {
  934. if (!images.empty())
  935. {
  936. tlog2<<"Warning: not all frames were unloaded from "<<name<<"\n";
  937. for (group_map::iterator group = images.begin(); group != images.end(); ++group )
  938. for (image_map::iterator image = group->second.begin(); image != group->second.end(); ++image )
  939. delete image->second;
  940. }
  941. loadedAnims.erase(this);
  942. }
  943. void CAnimation::setCustom(std::string filename, size_t frame, size_t group)
  944. {
  945. if (source[group].size() <= frame)
  946. source[group].resize(frame+1);
  947. source[group][frame]["file"].String() = filename;
  948. //FIXME: update image if already loaded
  949. }
  950. IImage * CAnimation::getImage(size_t frame, size_t group, bool verbose) const
  951. {
  952. group_map::const_iterator groupIter = images.find(group);
  953. if (groupIter != images.end())
  954. {
  955. image_map::const_iterator imageIter = groupIter->second.find(frame);
  956. if (imageIter != groupIter->second.end())
  957. return imageIter->second;
  958. }
  959. if (verbose)
  960. printError(frame, group, "GetImage");
  961. return NULL;
  962. }
  963. void CAnimation::load()
  964. {
  965. CDefFile * file = getFile();
  966. for (source_map::iterator group = source.begin(); group != source.end(); ++group )
  967. for (size_t image=0; image < group->second.size(); image++)
  968. loadFrame(file, image, group->first);
  969. delete file;
  970. }
  971. void CAnimation::unload()
  972. {
  973. for (source_map::iterator group = source.begin(); group != source.end(); ++group )
  974. for (size_t image=0; image < group->second.size(); image++)
  975. unloadFrame(image, group->first);
  976. }
  977. void CAnimation::loadGroup(size_t group)
  978. {
  979. CDefFile * file = getFile();
  980. if (vstd::contains(source, group))
  981. for (size_t image=0; image < source[group].size(); image++)
  982. loadFrame(file, image, group);
  983. delete file;
  984. }
  985. void CAnimation::unloadGroup(size_t group)
  986. {
  987. if (vstd::contains(source, group))
  988. for (size_t image=0; image < source[group].size(); image++)
  989. unloadFrame(image, group);
  990. }
  991. void CAnimation::load(size_t frame, size_t group)
  992. {
  993. CDefFile * file = getFile();
  994. loadFrame(file, frame, group);
  995. delete file;
  996. }
  997. void CAnimation::unload(size_t frame, size_t group)
  998. {
  999. unloadFrame(frame, group);
  1000. }
  1001. size_t CAnimation::size(size_t group) const
  1002. {
  1003. source_map::const_iterator iter = source.find(group);
  1004. if (iter != source.end())
  1005. return iter->second.size();
  1006. return 0;
  1007. }
  1008. std::set<CAnimation*> CAnimation::loadedAnims;
  1009. void CAnimation::getAnimInfo()
  1010. {
  1011. tlog1<<"Animation stats: Loaded "<<loadedAnims.size()<<" total\n";
  1012. for (std::set<CAnimation*>::iterator it = loadedAnims.begin(); it != loadedAnims.end(); it++)
  1013. {
  1014. CAnimation * anim = *it;
  1015. tlog1<<"Name: "<<anim->name<<" Groups: "<<anim->images.size();
  1016. if (!anim->images.empty())
  1017. tlog1<<", "<<anim->images.begin()->second.size()<<" image loaded in group "<< anim->images.begin()->first;
  1018. tlog1<<"\n";
  1019. }
  1020. }
  1021. CAnimImage::CAnimImage(std::string name, size_t Frame, size_t Group, int x, int y, ui8 Flags):
  1022. frame(Frame),
  1023. group(Group),
  1024. player(-1),
  1025. flags(Flags)
  1026. {
  1027. pos.x += x;
  1028. pos.y += y;
  1029. anim = new CAnimation(name);
  1030. init();
  1031. }
  1032. CAnimImage::CAnimImage(CAnimation *Anim, size_t Frame, size_t Group, int x, int y, ui8 Flags):
  1033. anim(Anim),
  1034. frame(Frame),
  1035. group(Group),
  1036. player(-1),
  1037. flags(Flags)
  1038. {
  1039. pos.x += x;
  1040. pos.y += y;
  1041. init();
  1042. }
  1043. size_t CAnimImage::size()
  1044. {
  1045. return anim->size(group);
  1046. }
  1047. void CAnimImage::init()
  1048. {
  1049. anim->load(frame, group);
  1050. if (flags & CShowableAnim::BASE)
  1051. anim->load(0,group);
  1052. IImage *img = anim->getImage(frame, group);
  1053. if (img)
  1054. {
  1055. pos.w = img->width();
  1056. pos.h = img->height();
  1057. }
  1058. }
  1059. CAnimImage::~CAnimImage()
  1060. {
  1061. anim->unload(frame, group);
  1062. if (flags & CShowableAnim::BASE)
  1063. anim->unload(0,group);
  1064. delete anim;
  1065. }
  1066. void CAnimImage::showAll()
  1067. {
  1068. /* IImage *img;
  1069. if ( flags & CShowableAnim::BASE && frame != 0)
  1070. if ((img = anim->getImage(0, group)))
  1071. img->draw(to, pos.x, pos.y);
  1072. if ((img = anim->getImage(frame, group)))
  1073. img->draw(to, pos.x, pos.y);
  1074. */
  1075. }
  1076. void CAnimImage::setFrame(size_t Frame, size_t Group)
  1077. {
  1078. if (frame == Frame && group==Group)
  1079. return;
  1080. if (anim->size(Group) > Frame)
  1081. {
  1082. anim->load(Frame, Group);
  1083. anim->unload(frame, group);
  1084. frame = Frame;
  1085. group = Group;
  1086. IImage *img = anim->getImage(frame, group);
  1087. if (img)
  1088. {
  1089. if (flags & CShowableAnim::PLAYER_COLORED)
  1090. img->playerColored(player);
  1091. pos.w = img->width();
  1092. pos.h = img->height();
  1093. }
  1094. }
  1095. else
  1096. tlog1 << "Error: accessing unavailable frame " << Group << ":" << Frame << " in CAnimation!\n";
  1097. }
  1098. void CAnimImage::playerColored(PlayerColor currPlayer)
  1099. {
  1100. player = currPlayer;
  1101. flags |= CShowableAnim::PLAYER_COLORED;
  1102. anim->getImage(frame, group)->playerColored(player);
  1103. if (flags & CShowableAnim::BASE)
  1104. anim->getImage(0, group)->playerColored(player);
  1105. }
  1106. CShowableAnim::CShowableAnim(int x, int y, std::string name, ui8 Flags, ui32 Delay, size_t Group):
  1107. anim(name, Flags & USE_RLE),
  1108. group(Group),
  1109. frame(0),
  1110. first(0),
  1111. frameDelay(Delay),
  1112. value(0),
  1113. flags(Flags),
  1114. xOffset(0),
  1115. yOffset(0),
  1116. alpha(255)
  1117. {
  1118. anim.loadGroup(group);
  1119. last = anim.size(group);
  1120. pos.w = anim.getImage(0, group)->width();
  1121. pos.h = anim.getImage(0, group)->height();
  1122. pos.x+= x;
  1123. pos.y+= y;
  1124. }
  1125. CShowableAnim::~CShowableAnim()
  1126. {
  1127. anim.unloadGroup(group);
  1128. }
  1129. void CShowableAnim::setAlpha(ui32 alphaValue)
  1130. {
  1131. alpha = std::min<ui32>(alphaValue, 255);
  1132. }
  1133. bool CShowableAnim::set(size_t Group, size_t from, size_t to)
  1134. {
  1135. size_t max = anim.size(Group);
  1136. if (to < max)
  1137. max = to;
  1138. if (max < from || max == 0)
  1139. return false;
  1140. anim.load(Group);
  1141. anim.unload(group);
  1142. group = Group;
  1143. frame = first = from;
  1144. last = max;
  1145. value = 0;
  1146. return true;
  1147. }
  1148. bool CShowableAnim::set(size_t Group)
  1149. {
  1150. if (anim.size(Group)== 0)
  1151. return false;
  1152. if (group != Group)
  1153. {
  1154. anim.loadGroup(Group);
  1155. anim.unloadGroup(group);
  1156. first = 0;
  1157. group = Group;
  1158. last = anim.size(Group);
  1159. }
  1160. frame = value = 0;
  1161. return true;
  1162. }
  1163. void CShowableAnim::reset()
  1164. {
  1165. value = 0;
  1166. frame = first;
  1167. if (callback)
  1168. callback();
  1169. }
  1170. void CShowableAnim::clipRect(int posX, int posY, int width, int height)
  1171. {
  1172. xOffset = posX;
  1173. yOffset = posY;
  1174. pos.w = width;
  1175. pos.h = height;
  1176. }
  1177. void CShowableAnim::show()
  1178. {
  1179. //* if ( flags & BASE && frame != first)
  1180. //* blitImage(first, group, to);
  1181. //* blitImage(frame, group, to);
  1182. if ((flags & PLAY_ONCE) && frame + 1 == last)
  1183. return;
  1184. if ( ++value == frameDelay )
  1185. {
  1186. value = 0;
  1187. if ( ++frame >= last)
  1188. reset();
  1189. }
  1190. }
  1191. void CShowableAnim::showAll()
  1192. {
  1193. //* if ( flags & BASE && frame != first)
  1194. //* blitImage(first, group, to);
  1195. //* blitImage(frame, group, to);
  1196. }
  1197. void CShowableAnim::blitImage(size_t frame, size_t group, SDL_Surface *to)
  1198. {
  1199. assert(to);
  1200. Rect src( xOffset, yOffset, pos.w, pos.h);
  1201. IImage * img = anim.getImage(frame, group);
  1202. if (img)
  1203. img->draw(to, pos.x-xOffset, pos.y-yOffset, &src, alpha);
  1204. }
  1205. void CShowableAnim::rotate(bool on, bool vertical)
  1206. {
  1207. ui8 flag = vertical? VERTICAL_FLIP:HORIZONTAL_FLIP;
  1208. if (on)
  1209. flags |= flag;
  1210. else
  1211. flags &= ~flag;
  1212. }
  1213. CCreatureAnim::CCreatureAnim(int x, int y, std::string name, Rect picPos, ui8 flags, EAnimType type):
  1214. CShowableAnim(x,y,name,flags,4,type)
  1215. {
  1216. xOffset = picPos.x;
  1217. yOffset = picPos.y;
  1218. if (picPos.w)
  1219. pos.w = picPos.w;
  1220. if (picPos.h)
  1221. pos.h = picPos.h;
  1222. };
  1223. void CCreatureAnim::loopPreview(bool warMachine)
  1224. {
  1225. std::vector<EAnimType> available;
  1226. static const EAnimType creaPreviewList[] = {HOLDING, HITTED, DEFENCE, ATTACK_FRONT, CAST_FRONT};
  1227. static const EAnimType machPreviewList[] = {HOLDING, MOVING, SHOOT_UP, SHOOT_FRONT, SHOOT_DOWN};
  1228. auto & previewList = warMachine ? machPreviewList : creaPreviewList;
  1229. for (size_t i=0; i<ARRAY_COUNT(previewList); i++)
  1230. if (anim.size(previewList[i]))
  1231. available.push_back(previewList[i]);
  1232. size_t rnd = rand()%(available.size()*2);
  1233. if (rnd >= available.size())
  1234. {
  1235. EAnimType type;
  1236. if ( anim.size(MOVING) == 0 )//no moving animation present
  1237. type = HOLDING;
  1238. else
  1239. type = MOVING;
  1240. //display this anim for ~1 second (time is random, but it looks good)
  1241. for (size_t i=0; i< 12/anim.size(type) + 1; i++)
  1242. addLast(type);
  1243. }
  1244. else
  1245. addLast(available[rnd]);
  1246. }
  1247. void CCreatureAnim::addLast(EAnimType newType)
  1248. {
  1249. if (type != MOVING && newType == MOVING)//starting moving - play init sequence
  1250. {
  1251. queue.push( MOVE_START );
  1252. }
  1253. else if (type == MOVING && newType != MOVING )//previous anim was moving - finish it
  1254. {
  1255. queue.push( MOVE_END );
  1256. }
  1257. if (newType == TURN_L || newType == TURN_R)
  1258. queue.push(newType);
  1259. queue.push(newType);
  1260. }
  1261. void CCreatureAnim::reset()
  1262. {
  1263. //if we are in the middle of rotation - set flag
  1264. if (type == TURN_L && !queue.empty() && queue.front() == TURN_L)
  1265. rotate(true);
  1266. if (type == TURN_R && !queue.empty() && queue.front() == TURN_R)
  1267. rotate(false);
  1268. while (!queue.empty())
  1269. {
  1270. EAnimType at = queue.front();
  1271. queue.pop();
  1272. if (set(at))
  1273. return;
  1274. }
  1275. if (callback)
  1276. callback();
  1277. while (!queue.empty())
  1278. {
  1279. EAnimType at = queue.front();
  1280. queue.pop();
  1281. if (set(at))
  1282. return;
  1283. }
  1284. set(HOLDING);
  1285. }
  1286. void CCreatureAnim::startPreview(bool warMachine)
  1287. {
  1288. callback = boost::bind(&CCreatureAnim::loopPreview, this, warMachine);
  1289. }
  1290. void CCreatureAnim::clearAndSet(EAnimType type)
  1291. {
  1292. while (!queue.empty())
  1293. queue.pop();
  1294. set(type);
  1295. }