CAnimation.cpp 34 KB

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