CAnimation.cpp 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483
  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 "gui/SDL_Extensions.h"
  10. #include "gui/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. logGlobal->errorStream()<<"Error: unsupported format of def file: "<<sprite.format;
  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. logGlobal->errorStream() << "Error: failed to load image "<<filename;
  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. logGlobal->errorStream() << type <<" error: Request for frame not present in CAnimation! "
  910. <<"\tFile name: "<<name<<" Group: "<<group<<" Frame: "<<frame;
  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. logGlobal->warnStream()<<"Warning: not all frames were unloaded from "<<name;
  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. logGlobal->errorStream()<<"Animation stats: Loaded "<<loadedAnims.size()<<" total";
  1012. for (std::set<CAnimation*>::iterator it = loadedAnims.begin(); it != loadedAnims.end(); it++)
  1013. {
  1014. CAnimation * anim = *it;
  1015. logGlobal->errorStream()<<"Name: "<<anim->name<<" Groups: "<<anim->images.size();
  1016. if (!anim->images.empty())
  1017. logGlobal->errorStream()<<", "<<anim->images.begin()->second.size()<<" image loaded in group "<< anim->images.begin()->first;
  1018. }
  1019. }
  1020. CAnimImage::CAnimImage(std::string name, size_t Frame, size_t Group, int x, int y, ui8 Flags):
  1021. frame(Frame),
  1022. group(Group),
  1023. player(-1),
  1024. flags(Flags)
  1025. {
  1026. pos.x += x;
  1027. pos.y += y;
  1028. anim = new CAnimation(name);
  1029. init();
  1030. }
  1031. CAnimImage::CAnimImage(CAnimation *Anim, size_t Frame, size_t Group, int x, int y, ui8 Flags):
  1032. anim(Anim),
  1033. frame(Frame),
  1034. group(Group),
  1035. player(-1),
  1036. flags(Flags)
  1037. {
  1038. pos.x += x;
  1039. pos.y += y;
  1040. init();
  1041. }
  1042. size_t CAnimImage::size()
  1043. {
  1044. return anim->size(group);
  1045. }
  1046. void CAnimImage::init()
  1047. {
  1048. anim->load(frame, group);
  1049. if (flags & CShowableAnim::BASE)
  1050. anim->load(0,group);
  1051. IImage *img = anim->getImage(frame, group);
  1052. if (img)
  1053. {
  1054. pos.w = img->width();
  1055. pos.h = img->height();
  1056. }
  1057. }
  1058. CAnimImage::~CAnimImage()
  1059. {
  1060. anim->unload(frame, group);
  1061. if (flags & CShowableAnim::BASE)
  1062. anim->unload(0,group);
  1063. delete anim;
  1064. }
  1065. void CAnimImage::showAll(SDL_Surface * to)
  1066. {
  1067. IImage *img;
  1068. if ( flags & CShowableAnim::BASE && frame != 0)
  1069. if ((img = anim->getImage(0, group)))
  1070. img->draw(to, pos.x, pos.y);
  1071. if ((img = anim->getImage(frame, group)))
  1072. img->draw(to, pos.x, pos.y);
  1073. }
  1074. void CAnimImage::setFrame(size_t Frame, size_t Group)
  1075. {
  1076. if (frame == Frame && group==Group)
  1077. return;
  1078. if (anim->size(Group) > Frame)
  1079. {
  1080. anim->load(Frame, Group);
  1081. anim->unload(frame, group);
  1082. frame = Frame;
  1083. group = Group;
  1084. IImage *img = anim->getImage(frame, group);
  1085. if (img)
  1086. {
  1087. if (flags & CShowableAnim::PLAYER_COLORED)
  1088. img->playerColored(player);
  1089. pos.w = img->width();
  1090. pos.h = img->height();
  1091. }
  1092. }
  1093. else
  1094. logGlobal->errorStream() << "Error: accessing unavailable frame " << Group << ":" << Frame << " in CAnimation!";
  1095. }
  1096. void CAnimImage::playerColored(PlayerColor currPlayer)
  1097. {
  1098. player = currPlayer;
  1099. flags |= CShowableAnim::PLAYER_COLORED;
  1100. anim->getImage(frame, group)->playerColored(player);
  1101. if (flags & CShowableAnim::BASE)
  1102. anim->getImage(0, group)->playerColored(player);
  1103. }
  1104. CShowableAnim::CShowableAnim(int x, int y, std::string name, ui8 Flags, ui32 Delay, size_t Group):
  1105. anim(name, Flags & USE_RLE),
  1106. group(Group),
  1107. frame(0),
  1108. first(0),
  1109. frameDelay(Delay),
  1110. value(0),
  1111. flags(Flags),
  1112. xOffset(0),
  1113. yOffset(0),
  1114. alpha(255)
  1115. {
  1116. anim.loadGroup(group);
  1117. last = anim.size(group);
  1118. pos.w = anim.getImage(0, group)->width();
  1119. pos.h = anim.getImage(0, group)->height();
  1120. pos.x+= x;
  1121. pos.y+= y;
  1122. }
  1123. CShowableAnim::~CShowableAnim()
  1124. {
  1125. anim.unloadGroup(group);
  1126. }
  1127. void CShowableAnim::setAlpha(ui32 alphaValue)
  1128. {
  1129. alpha = std::min<ui32>(alphaValue, 255);
  1130. }
  1131. bool CShowableAnim::set(size_t Group, size_t from, size_t to)
  1132. {
  1133. size_t max = anim.size(Group);
  1134. if (to < max)
  1135. max = to;
  1136. if (max < from || max == 0)
  1137. return false;
  1138. anim.load(Group);
  1139. anim.unload(group);
  1140. group = Group;
  1141. frame = first = from;
  1142. last = max;
  1143. value = 0;
  1144. return true;
  1145. }
  1146. bool CShowableAnim::set(size_t Group)
  1147. {
  1148. if (anim.size(Group)== 0)
  1149. return false;
  1150. if (group != Group)
  1151. {
  1152. anim.loadGroup(Group);
  1153. anim.unloadGroup(group);
  1154. first = 0;
  1155. group = Group;
  1156. last = anim.size(Group);
  1157. }
  1158. frame = value = 0;
  1159. return true;
  1160. }
  1161. void CShowableAnim::reset()
  1162. {
  1163. value = 0;
  1164. frame = first;
  1165. if (callback)
  1166. callback();
  1167. }
  1168. void CShowableAnim::clipRect(int posX, int posY, int width, int height)
  1169. {
  1170. xOffset = posX;
  1171. yOffset = posY;
  1172. pos.w = width;
  1173. pos.h = height;
  1174. }
  1175. void CShowableAnim::show(SDL_Surface * to)
  1176. {
  1177. if ( flags & BASE && frame != first)
  1178. blitImage(first, group, to);
  1179. blitImage(frame, group, to);
  1180. if ((flags & PLAY_ONCE) && frame + 1 == last)
  1181. return;
  1182. if ( ++value == frameDelay )
  1183. {
  1184. value = 0;
  1185. if ( ++frame >= last)
  1186. reset();
  1187. }
  1188. }
  1189. void CShowableAnim::showAll(SDL_Surface * to)
  1190. {
  1191. if ( flags & BASE && frame != first)
  1192. blitImage(first, group, to);
  1193. blitImage(frame, group, to);
  1194. }
  1195. void CShowableAnim::blitImage(size_t frame, size_t group, SDL_Surface *to)
  1196. {
  1197. assert(to);
  1198. Rect src( xOffset, yOffset, pos.w, pos.h);
  1199. IImage * img = anim.getImage(frame, group);
  1200. if (img)
  1201. img->draw(to, pos.x-xOffset, pos.y-yOffset, &src, alpha);
  1202. }
  1203. void CShowableAnim::rotate(bool on, bool vertical)
  1204. {
  1205. ui8 flag = vertical? VERTICAL_FLIP:HORIZONTAL_FLIP;
  1206. if (on)
  1207. flags |= flag;
  1208. else
  1209. flags &= ~flag;
  1210. }
  1211. CCreatureAnim::CCreatureAnim(int x, int y, std::string name, Rect picPos, ui8 flags, EAnimType type):
  1212. CShowableAnim(x,y,name,flags,4,type)
  1213. {
  1214. xOffset = picPos.x;
  1215. yOffset = picPos.y;
  1216. if (picPos.w)
  1217. pos.w = picPos.w;
  1218. if (picPos.h)
  1219. pos.h = picPos.h;
  1220. };
  1221. void CCreatureAnim::loopPreview(bool warMachine)
  1222. {
  1223. std::vector<EAnimType> available;
  1224. static const EAnimType creaPreviewList[] = {HOLDING, HITTED, DEFENCE, ATTACK_FRONT, CAST_FRONT};
  1225. static const EAnimType machPreviewList[] = {HOLDING, MOVING, SHOOT_UP, SHOOT_FRONT, SHOOT_DOWN};
  1226. auto & previewList = warMachine ? machPreviewList : creaPreviewList;
  1227. for (size_t i=0; i<ARRAY_COUNT(previewList); i++)
  1228. if (anim.size(previewList[i]))
  1229. available.push_back(previewList[i]);
  1230. size_t rnd = rand()%(available.size()*2);
  1231. if (rnd >= available.size())
  1232. {
  1233. EAnimType type;
  1234. if ( anim.size(MOVING) == 0 )//no moving animation present
  1235. type = HOLDING;
  1236. else
  1237. type = MOVING;
  1238. //display this anim for ~1 second (time is random, but it looks good)
  1239. for (size_t i=0; i< 12/anim.size(type) + 1; i++)
  1240. addLast(type);
  1241. }
  1242. else
  1243. addLast(available[rnd]);
  1244. }
  1245. void CCreatureAnim::addLast(EAnimType newType)
  1246. {
  1247. if (type != MOVING && newType == MOVING)//starting moving - play init sequence
  1248. {
  1249. queue.push( MOVE_START );
  1250. }
  1251. else if (type == MOVING && newType != MOVING )//previous anim was moving - finish it
  1252. {
  1253. queue.push( MOVE_END );
  1254. }
  1255. if (newType == TURN_L || newType == TURN_R)
  1256. queue.push(newType);
  1257. queue.push(newType);
  1258. }
  1259. void CCreatureAnim::reset()
  1260. {
  1261. //if we are in the middle of rotation - set flag
  1262. if (type == TURN_L && !queue.empty() && queue.front() == TURN_L)
  1263. rotate(true);
  1264. if (type == TURN_R && !queue.empty() && queue.front() == TURN_R)
  1265. rotate(false);
  1266. while (!queue.empty())
  1267. {
  1268. EAnimType at = queue.front();
  1269. queue.pop();
  1270. if (set(at))
  1271. return;
  1272. }
  1273. if (callback)
  1274. callback();
  1275. while (!queue.empty())
  1276. {
  1277. EAnimType at = queue.front();
  1278. queue.pop();
  1279. if (set(at))
  1280. return;
  1281. }
  1282. set(HOLDING);
  1283. }
  1284. void CCreatureAnim::startPreview(bool warMachine)
  1285. {
  1286. callback = boost::bind(&CCreatureAnim::loopPreview, this, warMachine);
  1287. }
  1288. void CCreatureAnim::clearAndSet(EAnimType type)
  1289. {
  1290. while (!queue.empty())
  1291. queue.pop();
  1292. set(type);
  1293. }