CAnimation.cpp 34 KB

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