CAnimation.cpp 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505
  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(nullptr),
  109. palette(nullptr)
  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 (auto 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(nullptr),
  299. position(nullptr)
  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(nullptr),
  344. entry(nullptr),
  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 = nullptr;
  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(nullptr)
  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 == nullptr)
  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(nullptr),
  592. line(nullptr),
  593. palette(nullptr)
  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 = nullptr;
  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 nullptr;
  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 (file)
  827. {
  828. auto frameList = file->getEntries();
  829. if (vstd::contains(frameList, group) && frameList.at(group) > frame) // frame is present
  830. {
  831. if (compressed)
  832. images[group][frame] = new CompImage(file, frame, group);
  833. else
  834. images[group][frame] = new SDLImage(file, frame, group);
  835. return true;
  836. }
  837. }
  838. // still here? image is missing
  839. printError(frame, group, "LoadFrame");
  840. images[group][frame] = new SDLImage("DEFAULT", compressed);
  841. }
  842. else //load from separate file
  843. {
  844. std::string filename = source[group][frame].Struct().find("file")->second.String();
  845. IImage * img = getFromExtraDef(filename);
  846. if (!img)
  847. img = new SDLImage(filename, compressed);
  848. images[group][frame] = img;
  849. return true;
  850. }
  851. return false;
  852. }
  853. bool CAnimation::unloadFrame(size_t frame, size_t group)
  854. {
  855. IImage *image = getImage(frame, group, false);
  856. if (image)
  857. {
  858. //decrease ref count for image and delete if needed
  859. if (image->decreaseRef())
  860. {
  861. delete image;
  862. images[group].erase(frame);
  863. }
  864. if (images[group].empty())
  865. images.erase(group);
  866. return true;
  867. }
  868. return false;
  869. }
  870. void CAnimation::initFromJson(const JsonNode & config)
  871. {
  872. std::string basepath;
  873. basepath = config["basepath"].String();
  874. BOOST_FOREACH(const JsonNode &group, config["sequences"].Vector())
  875. {
  876. size_t groupID = group["group"].Float();//TODO: string-to-value conversion("moving" -> MOVING)
  877. source[groupID].clear();
  878. BOOST_FOREACH(const JsonNode &frame, group["frames"].Vector())
  879. {
  880. source[groupID].push_back(JsonNode());
  881. std::string filename = frame.String();
  882. source[groupID].back()["file"].String() = basepath + filename;
  883. }
  884. }
  885. BOOST_FOREACH(const JsonNode &node, config["images"].Vector())
  886. {
  887. size_t group = node["group"].Float();
  888. size_t frame = node["frame"].Float();
  889. if (source[group].size() <= frame)
  890. source[group].resize(frame+1);
  891. source[group][frame] = node;
  892. std::string filename = node["file"].String();
  893. source[group][frame]["file"].String() = basepath + filename;
  894. }
  895. }
  896. void CAnimation::init(CDefFile * file)
  897. {
  898. if (file)
  899. {
  900. const std::map<size_t, size_t> defEntries = file->getEntries();
  901. for (std::map<size_t, size_t>::const_iterator mapIt = defEntries.begin(); mapIt!=defEntries.end(); ++mapIt)
  902. source[mapIt->first].resize(mapIt->second);
  903. }
  904. ResourceID resID(std::string("SPRITES/") + name, EResType::TEXT);
  905. if (vstd::contains(graphics->imageLists, resID.getName()))
  906. initFromJson(graphics->imageLists[resID.getName()]);
  907. auto & configList = CResourceHandler::get()->getResourcesWithName(resID);
  908. BOOST_FOREACH(auto & entry, configList)
  909. {
  910. auto stream = entry.getLoader()->load(entry.getResourceName());
  911. std::unique_ptr<ui8[]> textData(new ui8[stream->getSize()]);
  912. stream->read(textData.get(), stream->getSize());
  913. const JsonNode config((char*)textData.get(), stream->getSize());
  914. initFromJson(config);
  915. }
  916. }
  917. CDefFile * CAnimation::getFile() const
  918. {
  919. ResourceID identifier(std::string("SPRITES/") + name, EResType::ANIMATION);
  920. if (CResourceHandler::get()->existsResource(identifier))
  921. return new CDefFile(name);
  922. return nullptr;
  923. }
  924. void CAnimation::printError(size_t frame, size_t group, std::string type) const
  925. {
  926. logGlobal->errorStream() << type <<" error: Request for frame not present in CAnimation! "
  927. <<"\tFile name: "<<name<<" Group: "<<group<<" Frame: "<<frame;
  928. }
  929. CAnimation::CAnimation(std::string Name, bool Compressed):
  930. name(Name),
  931. compressed(Compressed)
  932. {
  933. size_t dotPos = name.find_last_of('.');
  934. if ( dotPos!=-1 )
  935. name.erase(dotPos);
  936. std::transform(name.begin(), name.end(), name.begin(), toupper);
  937. CDefFile * file = getFile();
  938. init(file);
  939. delete file;
  940. loadedAnims.insert(this);
  941. }
  942. CAnimation::CAnimation():
  943. name(""),
  944. compressed(false)
  945. {
  946. init(nullptr);
  947. loadedAnims.insert(this);
  948. }
  949. CAnimation::~CAnimation()
  950. {
  951. if (!images.empty())
  952. {
  953. logGlobal->warnStream()<<"Warning: not all frames were unloaded from "<<name;
  954. for (group_map::iterator group = images.begin(); group != images.end(); ++group )
  955. for (image_map::iterator image = group->second.begin(); image != group->second.end(); ++image )
  956. delete image->second;
  957. }
  958. loadedAnims.erase(this);
  959. }
  960. void CAnimation::setCustom(std::string filename, size_t frame, size_t group)
  961. {
  962. if (source[group].size() <= frame)
  963. source[group].resize(frame+1);
  964. source[group][frame]["file"].String() = filename;
  965. //FIXME: update image if already loaded
  966. }
  967. IImage * CAnimation::getImage(size_t frame, size_t group, bool verbose) const
  968. {
  969. group_map::const_iterator groupIter = images.find(group);
  970. if (groupIter != images.end())
  971. {
  972. image_map::const_iterator imageIter = groupIter->second.find(frame);
  973. if (imageIter != groupIter->second.end())
  974. return imageIter->second;
  975. }
  976. if (verbose)
  977. printError(frame, group, "GetImage");
  978. return nullptr;
  979. }
  980. void CAnimation::load()
  981. {
  982. CDefFile * file = getFile();
  983. for (source_map::iterator group = source.begin(); group != source.end(); ++group )
  984. for (size_t image=0; image < group->second.size(); image++)
  985. loadFrame(file, image, group->first);
  986. delete file;
  987. }
  988. void CAnimation::unload()
  989. {
  990. for (source_map::iterator group = source.begin(); group != source.end(); ++group )
  991. for (size_t image=0; image < group->second.size(); image++)
  992. unloadFrame(image, group->first);
  993. }
  994. void CAnimation::loadGroup(size_t group)
  995. {
  996. CDefFile * file = getFile();
  997. if (vstd::contains(source, group))
  998. for (size_t image=0; image < source[group].size(); image++)
  999. loadFrame(file, image, group);
  1000. delete file;
  1001. }
  1002. void CAnimation::unloadGroup(size_t group)
  1003. {
  1004. if (vstd::contains(source, group))
  1005. for (size_t image=0; image < source[group].size(); image++)
  1006. unloadFrame(image, group);
  1007. }
  1008. void CAnimation::load(size_t frame, size_t group)
  1009. {
  1010. CDefFile * file = getFile();
  1011. loadFrame(file, frame, group);
  1012. delete file;
  1013. }
  1014. void CAnimation::unload(size_t frame, size_t group)
  1015. {
  1016. unloadFrame(frame, group);
  1017. }
  1018. size_t CAnimation::size(size_t group) const
  1019. {
  1020. source_map::const_iterator iter = source.find(group);
  1021. if (iter != source.end())
  1022. return iter->second.size();
  1023. return 0;
  1024. }
  1025. std::set<CAnimation*> CAnimation::loadedAnims;
  1026. void CAnimation::getAnimInfo()
  1027. {
  1028. logGlobal->errorStream()<<"Animation stats: Loaded "<<loadedAnims.size()<<" total";
  1029. for (std::set<CAnimation*>::iterator it = loadedAnims.begin(); it != loadedAnims.end(); it++)
  1030. {
  1031. CAnimation * anim = *it;
  1032. logGlobal->errorStream()<<"Name: "<<anim->name<<" Groups: "<<anim->images.size();
  1033. if (!anim->images.empty())
  1034. logGlobal->errorStream()<<", "<<anim->images.begin()->second.size()<<" image loaded in group "<< anim->images.begin()->first;
  1035. }
  1036. }
  1037. CAnimImage::CAnimImage(std::string name, size_t Frame, size_t Group, int x, int y, ui8 Flags):
  1038. frame(Frame),
  1039. group(Group),
  1040. player(-1),
  1041. flags(Flags)
  1042. {
  1043. pos.x += x;
  1044. pos.y += y;
  1045. anim = new CAnimation(name);
  1046. init();
  1047. }
  1048. CAnimImage::CAnimImage(CAnimation *Anim, size_t Frame, size_t Group, int x, int y, ui8 Flags):
  1049. anim(Anim),
  1050. frame(Frame),
  1051. group(Group),
  1052. player(-1),
  1053. flags(Flags)
  1054. {
  1055. pos.x += x;
  1056. pos.y += y;
  1057. init();
  1058. }
  1059. size_t CAnimImage::size()
  1060. {
  1061. return anim->size(group);
  1062. }
  1063. void CAnimImage::init()
  1064. {
  1065. anim->load(frame, group);
  1066. if (flags & CShowableAnim::BASE)
  1067. anim->load(0,group);
  1068. IImage *img = anim->getImage(frame, group);
  1069. if (img)
  1070. {
  1071. pos.w = img->width();
  1072. pos.h = img->height();
  1073. }
  1074. }
  1075. CAnimImage::~CAnimImage()
  1076. {
  1077. anim->unload(frame, group);
  1078. if (flags & CShowableAnim::BASE)
  1079. anim->unload(0,group);
  1080. delete anim;
  1081. }
  1082. void CAnimImage::showAll(SDL_Surface * to)
  1083. {
  1084. IImage *img;
  1085. if ( flags & CShowableAnim::BASE && frame != 0)
  1086. if ((img = anim->getImage(0, group)))
  1087. img->draw(to, pos.x, pos.y);
  1088. if ((img = anim->getImage(frame, group)))
  1089. img->draw(to, pos.x, pos.y);
  1090. }
  1091. void CAnimImage::setFrame(size_t Frame, size_t Group)
  1092. {
  1093. if (frame == Frame && group==Group)
  1094. return;
  1095. if (anim->size(Group) > Frame)
  1096. {
  1097. anim->load(Frame, Group);
  1098. anim->unload(frame, group);
  1099. frame = Frame;
  1100. group = Group;
  1101. IImage *img = anim->getImage(frame, group);
  1102. if (img)
  1103. {
  1104. if (flags & CShowableAnim::PLAYER_COLORED)
  1105. img->playerColored(player);
  1106. pos.w = img->width();
  1107. pos.h = img->height();
  1108. }
  1109. }
  1110. else
  1111. logGlobal->errorStream() << "Error: accessing unavailable frame " << Group << ":" << Frame << " in CAnimation!";
  1112. }
  1113. void CAnimImage::playerColored(PlayerColor currPlayer)
  1114. {
  1115. player = currPlayer;
  1116. flags |= CShowableAnim::PLAYER_COLORED;
  1117. anim->getImage(frame, group)->playerColored(player);
  1118. if (flags & CShowableAnim::BASE)
  1119. anim->getImage(0, group)->playerColored(player);
  1120. }
  1121. CShowableAnim::CShowableAnim(int x, int y, std::string name, ui8 Flags, ui32 Delay, size_t Group):
  1122. anim(name, Flags & USE_RLE),
  1123. group(Group),
  1124. frame(0),
  1125. first(0),
  1126. frameDelay(Delay),
  1127. value(0),
  1128. flags(Flags),
  1129. xOffset(0),
  1130. yOffset(0),
  1131. alpha(255)
  1132. {
  1133. anim.loadGroup(group);
  1134. last = anim.size(group);
  1135. pos.w = anim.getImage(0, group)->width();
  1136. pos.h = anim.getImage(0, group)->height();
  1137. pos.x+= x;
  1138. pos.y+= y;
  1139. }
  1140. CShowableAnim::~CShowableAnim()
  1141. {
  1142. anim.unloadGroup(group);
  1143. }
  1144. void CShowableAnim::setAlpha(ui32 alphaValue)
  1145. {
  1146. alpha = std::min<ui32>(alphaValue, 255);
  1147. }
  1148. bool CShowableAnim::set(size_t Group, size_t from, size_t to)
  1149. {
  1150. size_t max = anim.size(Group);
  1151. if (to < max)
  1152. max = to;
  1153. if (max < from || max == 0)
  1154. return false;
  1155. anim.load(Group);
  1156. anim.unload(group);
  1157. group = Group;
  1158. frame = first = from;
  1159. last = max;
  1160. value = 0;
  1161. return true;
  1162. }
  1163. bool CShowableAnim::set(size_t Group)
  1164. {
  1165. if (anim.size(Group)== 0)
  1166. return false;
  1167. if (group != Group)
  1168. {
  1169. anim.loadGroup(Group);
  1170. anim.unloadGroup(group);
  1171. first = 0;
  1172. group = Group;
  1173. last = anim.size(Group);
  1174. }
  1175. frame = value = 0;
  1176. return true;
  1177. }
  1178. void CShowableAnim::reset()
  1179. {
  1180. value = 0;
  1181. frame = first;
  1182. if (callback)
  1183. callback();
  1184. }
  1185. void CShowableAnim::clipRect(int posX, int posY, int width, int height)
  1186. {
  1187. xOffset = posX;
  1188. yOffset = posY;
  1189. pos.w = width;
  1190. pos.h = height;
  1191. }
  1192. void CShowableAnim::show(SDL_Surface * to)
  1193. {
  1194. if ( flags & BASE && frame != first)
  1195. blitImage(first, group, to);
  1196. blitImage(frame, group, to);
  1197. if ((flags & PLAY_ONCE) && frame + 1 == last)
  1198. return;
  1199. if ( ++value == frameDelay )
  1200. {
  1201. value = 0;
  1202. if ( ++frame >= last)
  1203. reset();
  1204. }
  1205. }
  1206. void CShowableAnim::showAll(SDL_Surface * to)
  1207. {
  1208. if ( flags & BASE && frame != first)
  1209. blitImage(first, group, to);
  1210. blitImage(frame, group, to);
  1211. }
  1212. void CShowableAnim::blitImage(size_t frame, size_t group, SDL_Surface *to)
  1213. {
  1214. assert(to);
  1215. Rect src( xOffset, yOffset, pos.w, pos.h);
  1216. IImage * img = anim.getImage(frame, group);
  1217. if (img)
  1218. img->draw(to, pos.x-xOffset, pos.y-yOffset, &src, alpha);
  1219. }
  1220. void CShowableAnim::rotate(bool on, bool vertical)
  1221. {
  1222. ui8 flag = vertical? VERTICAL_FLIP:HORIZONTAL_FLIP;
  1223. if (on)
  1224. flags |= flag;
  1225. else
  1226. flags &= ~flag;
  1227. }
  1228. CCreatureAnim::CCreatureAnim(int x, int y, std::string name, Rect picPos, ui8 flags, EAnimType type):
  1229. CShowableAnim(x,y,name,flags,4,type)
  1230. {
  1231. xOffset = picPos.x;
  1232. yOffset = picPos.y;
  1233. if (picPos.w)
  1234. pos.w = picPos.w;
  1235. if (picPos.h)
  1236. pos.h = picPos.h;
  1237. };
  1238. void CCreatureAnim::loopPreview(bool warMachine)
  1239. {
  1240. std::vector<EAnimType> available;
  1241. static const EAnimType creaPreviewList[] = {HOLDING, HITTED, DEFENCE, ATTACK_FRONT, CAST_FRONT};
  1242. static const EAnimType machPreviewList[] = {HOLDING, MOVING, SHOOT_UP, SHOOT_FRONT, SHOOT_DOWN};
  1243. auto & previewList = warMachine ? machPreviewList : creaPreviewList;
  1244. for (size_t i=0; i<ARRAY_COUNT(previewList); i++)
  1245. if (anim.size(previewList[i]))
  1246. available.push_back(previewList[i]);
  1247. size_t rnd = rand()%(available.size()*2);
  1248. if (rnd >= available.size())
  1249. {
  1250. EAnimType type;
  1251. if ( anim.size(MOVING) == 0 )//no moving animation present
  1252. type = HOLDING;
  1253. else
  1254. type = MOVING;
  1255. //display this anim for ~1 second (time is random, but it looks good)
  1256. for (size_t i=0; i< 12/anim.size(type) + 1; i++)
  1257. addLast(type);
  1258. }
  1259. else
  1260. addLast(available[rnd]);
  1261. }
  1262. void CCreatureAnim::addLast(EAnimType newType)
  1263. {
  1264. if (type != MOVING && newType == MOVING)//starting moving - play init sequence
  1265. {
  1266. queue.push( MOVE_START );
  1267. }
  1268. else if (type == MOVING && newType != MOVING )//previous anim was moving - finish it
  1269. {
  1270. queue.push( MOVE_END );
  1271. }
  1272. if (newType == TURN_L || newType == TURN_R)
  1273. queue.push(newType);
  1274. queue.push(newType);
  1275. }
  1276. void CCreatureAnim::reset()
  1277. {
  1278. //if we are in the middle of rotation - set flag
  1279. if (type == TURN_L && !queue.empty() && queue.front() == TURN_L)
  1280. rotate(true);
  1281. if (type == TURN_R && !queue.empty() && queue.front() == TURN_R)
  1282. rotate(false);
  1283. while (!queue.empty())
  1284. {
  1285. EAnimType at = queue.front();
  1286. queue.pop();
  1287. if (set(at))
  1288. return;
  1289. }
  1290. if (callback)
  1291. callback();
  1292. while (!queue.empty())
  1293. {
  1294. EAnimType at = queue.front();
  1295. queue.pop();
  1296. if (set(at))
  1297. return;
  1298. }
  1299. set(HOLDING);
  1300. }
  1301. void CCreatureAnim::startPreview(bool warMachine)
  1302. {
  1303. callback = std::bind(&CCreatureAnim::loopPreview, this, warMachine);
  1304. }
  1305. void CCreatureAnim::clearAndSet(EAnimType type)
  1306. {
  1307. while (!queue.empty())
  1308. queue.pop();
  1309. set(type);
  1310. }