CAnimation.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  1. #include <iostream>
  2. #include <sstream>
  3. #include <boost/foreach.hpp>
  4. #include <boost/bind.hpp>
  5. #include "SDL.h"
  6. #include "SDL_image.h"
  7. #include "CBitmapHandler.h"
  8. #include "CAnimation.h"
  9. #include "SDL_Extensions.h"
  10. #include "../lib/CLodHandler.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. extern DLL_EXPORT CLodHandler *spriteh;
  21. /*************************************************************************
  22. * DefFile, class used for def loading *
  23. *************************************************************************/
  24. bool CDefFile::haveFrame(size_t frame, size_t group) const
  25. {
  26. if (offset.size() > group)
  27. if (offset[group].size() > frame)
  28. return true;
  29. return false;
  30. }
  31. SDL_Surface * CDefFile::loadFrame(size_t frame, size_t group) const
  32. {
  33. if (haveFrame(frame, group))
  34. return loadFrame(( unsigned char * )data+offset[group][frame], colors);
  35. return NULL;
  36. }
  37. SDL_Surface * CDefFile::loadFrame (const unsigned char * FDef, const BMPPalette * palette)
  38. {
  39. SDL_Surface * ret=NULL;
  40. unsigned int DefFormat, // format in which pixel data of sprite is encoded
  41. FullHeight,FullWidth, // full width and height of sprite including borders
  42. SpriteWidth, SpriteHeight, // width and height of encoded sprite
  43. TotalRowLength,
  44. RowAdd, add,
  45. BaseOffset;
  46. int LeftMargin, RightMargin, // position of 1st stored in sprite pixel on surface
  47. TopMargin, BottomMargin;
  48. SSpriteDef sd = * reinterpret_cast<const SSpriteDef *>(FDef);
  49. DefFormat = SDL_SwapLE32(sd.defType2);
  50. FullWidth = SDL_SwapLE32(sd.FullWidth);
  51. FullHeight = SDL_SwapLE32(sd.FullHeight);
  52. SpriteWidth = SDL_SwapLE32(sd.SpriteWidth);
  53. SpriteHeight = SDL_SwapLE32(sd.SpriteHeight);
  54. LeftMargin = SDL_SwapLE32(sd.LeftMargin);
  55. TopMargin = SDL_SwapLE32(sd.TopMargin);
  56. RightMargin = FullWidth - SpriteWidth - LeftMargin;
  57. BottomMargin = FullHeight - SpriteHeight - TopMargin;
  58. if (LeftMargin<0)
  59. SpriteWidth+=LeftMargin;
  60. if (RightMargin<0)
  61. SpriteWidth+=RightMargin;
  62. // Note: this looks bogus because we allocate only FullWidth, not FullWidth+add
  63. add = 4 - FullWidth%4;
  64. if (add==4)
  65. add=0;
  66. ret = SDL_CreateRGBSurface(SDL_SWSURFACE, FullWidth, FullHeight, 8, 0, 0, 0, 0);
  67. BaseOffset = sizeof(SSpriteDef);
  68. int BaseOffsetor = BaseOffset;
  69. for (int i=0; i<256; ++i)
  70. {
  71. SDL_Color pr;
  72. pr.r = palette[i].R;
  73. pr.g = palette[i].G;
  74. pr.b = palette[i].B;
  75. pr.unused = palette[i].F;
  76. (*(ret->format->palette->colors+i))=pr;
  77. }
  78. // If there's a margin anywhere, just blank out the whole surface.
  79. if (TopMargin > 0 || BottomMargin > 0 || LeftMargin > 0 || RightMargin > 0)
  80. {
  81. memset( reinterpret_cast<char*>(ret->pixels), 0, FullHeight*FullWidth);
  82. }
  83. int current=0;//current pixel on output surface
  84. // Skip top margin
  85. if (TopMargin > 0)
  86. current += TopMargin*(FullWidth+add);
  87. switch (DefFormat)
  88. {
  89. //pixel data is not compressed, copy each line to surface
  90. case 0:
  91. {
  92. for (unsigned int i=0; i<SpriteHeight; i++)
  93. {
  94. if (LeftMargin>0)
  95. current += LeftMargin;
  96. memcpy(reinterpret_cast<char*>(ret->pixels)+current, &FDef[BaseOffset], SpriteWidth);
  97. current += SpriteWidth;
  98. BaseOffset += SpriteWidth;
  99. if (RightMargin>0)
  100. current += RightMargin;
  101. }
  102. }
  103. break;
  104. // RLE encoding:
  105. // read offset of pixel data of each line
  106. // for each line
  107. // read type and size
  108. // if type is 0xff
  109. // no encoding, copy to output
  110. // else
  111. // RLE: set size pixels to type
  112. // do this until all line is parsed
  113. case 1:
  114. {
  115. //for each line we have offset of pixel data
  116. const unsigned int * RWEntriesLoc = reinterpret_cast<const unsigned int *>(FDef+BaseOffset);
  117. BaseOffset += sizeof(int) * SpriteHeight;
  118. for (unsigned int i=0; i<SpriteHeight; i++)
  119. {
  120. BaseOffset=BaseOffsetor + SDL_SwapLE32(read_unaligned_u32(RWEntriesLoc + i));
  121. if (LeftMargin>0)
  122. current += LeftMargin;
  123. TotalRowLength=0;
  124. do
  125. {
  126. unsigned char SegmentType=FDef[BaseOffset++];
  127. unsigned int SegmentLength=FDef[BaseOffset++] + 1;
  128. if (SegmentType==0xFF)
  129. {
  130. memcpy(reinterpret_cast<char*>(ret->pixels)+current, FDef + BaseOffset, SegmentLength);
  131. BaseOffset+=SegmentLength;
  132. }
  133. else
  134. {
  135. memset(reinterpret_cast<char*>(ret->pixels)+current, SegmentType, SegmentLength);
  136. }
  137. current += SegmentLength;
  138. TotalRowLength += SegmentLength;
  139. }
  140. while (TotalRowLength<SpriteWidth);
  141. RowAdd=SpriteWidth-TotalRowLength;
  142. if (RightMargin>0)
  143. current += RightMargin;
  144. if (add>0)
  145. current += add+RowAdd;
  146. }
  147. }
  148. break;
  149. // Something like RLE
  150. // read base offset
  151. // for each line
  152. // read type, set code and value
  153. // if code is 7
  154. // copy value pixels
  155. // else
  156. // set value pixels to code
  157. case 2:
  158. {
  159. BaseOffset = BaseOffsetor + SDL_SwapLE16(read_unaligned_u16(FDef + BaseOffsetor));
  160. for (unsigned int i=0; i<SpriteHeight; i++)
  161. {
  162. if (LeftMargin>0)
  163. current += LeftMargin;
  164. TotalRowLength=0;
  165. do
  166. {
  167. unsigned char SegmentType=FDef[BaseOffset++];
  168. unsigned char code = SegmentType / 32;
  169. unsigned char value = (SegmentType & 31) + 1;
  170. if (code==7)
  171. {
  172. memcpy(reinterpret_cast<char*>(ret->pixels)+current, &FDef[BaseOffset], value);
  173. current += value;
  174. BaseOffset += value;
  175. }
  176. else
  177. {
  178. memset(reinterpret_cast<char*>(ret->pixels)+current, code, value);
  179. current += value;
  180. }
  181. TotalRowLength+=value;
  182. }
  183. while (TotalRowLength<SpriteWidth);
  184. if (RightMargin>0)
  185. current += RightMargin;
  186. RowAdd=SpriteWidth-TotalRowLength;
  187. if (add>0)
  188. current += add+RowAdd;
  189. }
  190. }
  191. break;
  192. //combo of 1st and 2nd:
  193. // offset for each line
  194. // code and value combined in 1 byte
  195. case 3:
  196. {
  197. for (unsigned int i=0; i<SpriteHeight; i++)
  198. {
  199. BaseOffset = BaseOffsetor + SDL_SwapLE16(read_unaligned_u16(FDef + BaseOffsetor+i*2*(SpriteWidth/32)));
  200. if (LeftMargin>0)
  201. current += LeftMargin;
  202. TotalRowLength=0;
  203. do
  204. {
  205. unsigned char SegmentType=FDef[BaseOffset++];
  206. unsigned char code = SegmentType / 32;
  207. unsigned char value = (SegmentType & 31) + 1;
  208. int len = std::min<unsigned int>(value, SpriteWidth - TotalRowLength) - std::max(0, -LeftMargin);
  209. amax(len, 0);
  210. if (code==7)
  211. {
  212. memcpy((ui8*)ret->pixels + current, FDef + BaseOffset, len);
  213. current += len;
  214. BaseOffset += len;
  215. }
  216. else
  217. {
  218. memset((ui8*)ret->pixels + current, code, len);
  219. current += len;
  220. }
  221. TotalRowLength+=( LeftMargin>=0 ? value : value+LeftMargin );
  222. }
  223. while (TotalRowLength<SpriteWidth);
  224. if (RightMargin>0)
  225. current += RightMargin;
  226. RowAdd=SpriteWidth-TotalRowLength;
  227. if (add>0)
  228. current += add+RowAdd;
  229. }
  230. }
  231. break;
  232. default:
  233. throw std::string("Unknown sprite format.");
  234. break;
  235. }
  236. SDL_Color *col = ret->format->palette->colors;
  237. Uint32 keycol = SDL_MapRGBA(ret->format, col[0].r, col[0].b, col[0].g, col[0].unused);
  238. SDL_SetColorKey(ret, SDL_SRCCOLORKEY, keycol);
  239. return ret;
  240. };
  241. BMPPalette * CDefFile::getPalette()
  242. {
  243. BMPPalette * ret = new BMPPalette[256];
  244. memcpy(ret, colors, sizeof(BMPPalette)*256);
  245. return ret;
  246. }
  247. CDefFile::CDefFile(std::string Name):data(NULL),colors(NULL)
  248. {
  249. //First 8 colors in def palette used for transparency
  250. static SDL_Color H3Palette[8] = {
  251. { 0, 0, 0, 255},// 100% - transparency
  252. { 0, 0, 0, 192},// 75% - shadow border,
  253. { 0, 0, 0, 128},// TODO: find exact value
  254. { 0, 0, 0, 128},// TODO: for transparency
  255. { 0, 0, 0, 128},// 50% - shadow body
  256. { 0, 0, 0, 255},// 100% - selection highlight
  257. { 0, 0, 0, 128},// 50% - shadow body below selection
  258. { 0, 0, 0, 192}};// 75% - shadow border below selection
  259. data = spriteh->giveFile(Name, FILE_ANIMATION, &datasize);
  260. if (!data)
  261. {
  262. tlog0<<"Error: file "<< Name <<" not found\n";
  263. return;
  264. }
  265. colors = new BMPPalette[256];
  266. int it = 0;
  267. type = readNormalNr(data, it); it+=4;
  268. //int width = readNormalNr(data, it); it+=4;//not used
  269. //int height = readNormalNr(data, it); it+=4;
  270. it+=8;
  271. unsigned int totalBlocks = readNormalNr(data, it);
  272. it+=4;
  273. for (unsigned int i= 0; i<256; i++)
  274. {
  275. colors[i].R = data[it++];
  276. colors[i].G = data[it++];
  277. colors[i].B = data[it++];
  278. colors[i].F = 0;
  279. }
  280. memcpy(colors, H3Palette, (type == 66)? 32:20);//initialize shadow\selection colors
  281. offList.insert(datasize);
  282. for (unsigned int i=0; i<totalBlocks; i++)
  283. {
  284. size_t blockID = readNormalNr(data, it);
  285. it+=4;
  286. size_t totalEntries = readNormalNr(data, it);
  287. it+=12;
  288. //8 unknown bytes - skipping
  289. //13 bytes for name of every frame in this block - not used, skipping
  290. it+= 13 * totalEntries;
  291. offset.resize(std::max(blockID+1, offset.size()));
  292. for (unsigned int j=0; j<totalEntries; j++)
  293. {
  294. size_t currOffset = readNormalNr(data, it);
  295. offset[blockID].push_back(currOffset);
  296. offList.insert(currOffset);
  297. it += 4;
  298. }
  299. }
  300. }
  301. unsigned char * CDefFile::getFrame(size_t frame, size_t group) const
  302. {
  303. if (offset.size() > group)
  304. {
  305. if (offset[group].size() > frame)
  306. {
  307. size_t offs = offset[group][frame];
  308. std::set<size_t>::iterator it = offList.find(offs);
  309. if (it == offList.end() || ++it == offList.end())
  310. tlog0<<"Error: offset not found!\n";
  311. size_t size = *it - offs;
  312. unsigned char * ret = new unsigned char[size];
  313. memcpy(ret, data+offs, size);
  314. return ret;
  315. }
  316. }
  317. return NULL;
  318. }
  319. CDefFile::~CDefFile()
  320. {
  321. offset.clear();
  322. offList.clear();
  323. delete[] data;
  324. delete[] colors;
  325. }
  326. bool CDefFile::loaded() const
  327. {
  328. return data != NULL;
  329. }
  330. /*************************************************************************
  331. * CAnimation for animations handling, can load part of file if needed *
  332. *************************************************************************/
  333. CAnimation::AnimEntry::AnimEntry():
  334. surf(NULL),
  335. source(0),
  336. refCount(0),
  337. data(NULL),
  338. dataSize(0)
  339. {
  340. }
  341. bool CAnimation::loadFrame(CDefFile * file, size_t frame, size_t group)
  342. {
  343. if (groupSize(group) <= frame)
  344. {
  345. printError(frame, group, "LoadFrame");
  346. return false;
  347. }
  348. AnimEntry &e = entries[group][frame];
  349. if (e.surf || e.data)
  350. {
  351. e.refCount++;
  352. return true;
  353. }
  354. if (e.source & 6)//load frame with SDL_Image
  355. {
  356. int size;
  357. unsigned char * pic = NULL;
  358. std::ostringstream str;
  359. if ( e.source & 2 )
  360. str << name << '#' << (group+1) << '#' << (frame+1); // file#12#34.*
  361. else
  362. str << name << '#' << (frame+1);//file#34.*
  363. pic = spriteh->giveFile(str.str(), FILE_GRAPHICS, &size);
  364. if (pic)
  365. {
  366. if (compressed)
  367. {
  368. e.data = pic;
  369. e.dataSize = size;
  370. }
  371. else
  372. {
  373. e.surf = IMG_Load_RW( SDL_RWFromMem((void*)pic, size), 1);
  374. delete [] pic;
  375. }
  376. }
  377. }
  378. else if (file && e.source & 1)//try to get image from def
  379. {
  380. if (compressed)
  381. e.data = file->getFrame(frame, group);
  382. else
  383. e.surf = file->loadFrame(frame, group);
  384. }
  385. if (!(e.surf || e.data))
  386. return false;//failed to load
  387. e.refCount++;
  388. return true;
  389. }
  390. bool CAnimation::unloadFrame(size_t frame, size_t group)
  391. {
  392. if (groupSize(group) > frame && entries[group][frame].refCount)
  393. {
  394. AnimEntry &e = entries[group][frame];
  395. if (--e.refCount)//not last ref
  396. return true;
  397. SDL_FreeSurface(e.surf);
  398. delete [] e.data;
  399. e.surf = NULL;
  400. e.data = NULL;
  401. return true;
  402. }
  403. return false;
  404. }
  405. void CAnimation::decompress(AnimEntry &entry)
  406. {
  407. if (entry.source & 6)//load frame with SDL_Image
  408. entry.surf = IMG_Load_RW( SDL_RWFromMem((void*)entry.data, entry.dataSize), 1);
  409. else if (entry.source & 1)
  410. entry.surf = CDefFile::loadFrame(entry.data, defPalette);
  411. }
  412. void CAnimation::init(CDefFile * file)
  413. {
  414. if (compressed)
  415. defPalette = file->getPalette();
  416. for (size_t group = 0; ; group++)
  417. {
  418. std::vector<AnimEntry> toAdd;
  419. for (size_t frame = 0; ; frame++)
  420. {
  421. unsigned char res=0;
  422. {
  423. std::ostringstream str;
  424. str << name << '#' << (group+1) << '#' << (frame+1); // format: file#12#34.*
  425. if (spriteh->haveFile(str.str()))
  426. res |= 2;
  427. }
  428. if (group == 0)
  429. {
  430. std::ostringstream str;
  431. str << name << '#' << (frame+1);// format: file#34.*
  432. if ( spriteh->haveFile(str.str()))
  433. res |=4;
  434. }
  435. if (file)//we have def too. try to get image from def
  436. {
  437. if (file->haveFrame(frame, group))
  438. res |=1;
  439. }
  440. if (res)
  441. {
  442. toAdd.push_back(AnimEntry());
  443. toAdd.back().source = res;
  444. }
  445. else
  446. break;
  447. }
  448. if (!toAdd.empty())
  449. {
  450. entries.push_back(toAdd);
  451. }
  452. else
  453. {
  454. entries.resize(entries.size()+1);
  455. if (group > 21)
  456. break;//FIXME: crude workaround: if some groups are not present
  457. //(common for creatures) parser will exit before reaching them
  458. }
  459. }
  460. }
  461. CDefFile * CAnimation::getFile() const
  462. {
  463. CDefFile * file = new CDefFile(name);
  464. if (!file->loaded())
  465. {
  466. delete file;
  467. return NULL;
  468. }
  469. return file;
  470. }
  471. void CAnimation::printError(size_t frame, size_t group, std::string type) const
  472. {
  473. tlog0 << type <<" error: Request for frame not present in CAnimation!\n"
  474. <<"\tFile name: "<<name<<" Group: "<<group<<" Frame: "<<frame<<"\n";
  475. }
  476. CAnimation::CAnimation(std::string Name, bool Compressed):
  477. name(Name),
  478. compressed(Compressed),
  479. defPalette(NULL)
  480. {
  481. std::transform(name.begin(), name.end(), name.begin(), (int(*)(int))toupper);
  482. int dotPos = name.find_last_of('.');
  483. if ( dotPos != -1 )
  484. name.erase(dotPos);
  485. CDefFile * file = getFile();
  486. init(file);
  487. delete file;
  488. }
  489. CAnimation::CAnimation():
  490. name(""),
  491. compressed(false),
  492. defPalette(NULL)
  493. {
  494. }
  495. CAnimation::~CAnimation()
  496. {
  497. delete [] defPalette;
  498. for (size_t i = 0; i < entries.size(); i++)
  499. for (size_t j = 0; j < entries.at(i).size(); j++)
  500. {
  501. delete [] entries[i][j].data;
  502. if (entries[i][j].surf)
  503. SDL_FreeSurface(entries[i][j].surf);
  504. }
  505. }
  506. void CAnimation::add(SDL_Surface * surf, bool shared, size_t group)
  507. {
  508. if (!surf)
  509. return;
  510. if (entries.size() <= group)
  511. entries.resize(group+1);
  512. if (shared)
  513. surf->refcount++;
  514. entries[group].push_back(AnimEntry());
  515. entries[group].back().refCount = 1;
  516. entries[group].back().surf = surf;
  517. }
  518. void CAnimation::removeDecompressed(size_t frame, size_t group)
  519. {
  520. AnimEntry &e = entries[group][frame];
  521. if (e.surf && e.data)
  522. {
  523. SDL_FreeSurface(e.surf);
  524. e.surf = NULL;
  525. }
  526. }
  527. SDL_Surface * CAnimation::image(size_t frame)
  528. {
  529. size_t group=0;
  530. while (group<entries.size() && frame > entries[group].size())
  531. frame -= entries[group].size();
  532. if (group <entries.size() && frame < entries[group].size())
  533. return image(frame, group);
  534. return NULL;
  535. }
  536. SDL_Surface * CAnimation::image(size_t frame, size_t group)
  537. {
  538. if ( groupSize(group) > frame )
  539. {
  540. AnimEntry &e = entries[group][frame];
  541. if (!e.surf && e.data)
  542. decompress(e);
  543. return e.surf;
  544. }
  545. printError(frame, group, "GetImage");
  546. return NULL;
  547. }
  548. void CAnimation::clear()
  549. {
  550. unload();
  551. entries.clear();
  552. }
  553. void CAnimation::load()
  554. {
  555. CDefFile * file = getFile();
  556. for (size_t group = 0; group<entries.size(); group++)
  557. for (size_t frame = 0; frame<entries[group].size(); frame++)
  558. loadFrame(file, frame, group);
  559. delete file;
  560. }
  561. void CAnimation::unload()
  562. {
  563. for (size_t group = 0; group<entries.size(); group++)
  564. for (size_t frame = 0; frame<entries[group].size(); frame++)
  565. unloadFrame(frame, group);
  566. }
  567. void CAnimation::loadGroup(size_t group)
  568. {
  569. CDefFile * file = getFile();
  570. for (size_t frame = 0; frame<groupSize(group); frame++)
  571. loadFrame(file, frame, group);
  572. delete file;
  573. }
  574. void CAnimation::unloadGroup(size_t group)
  575. {
  576. for (size_t frame = 0; frame<groupSize(group); frame++)
  577. unloadFrame(frame, group);
  578. }
  579. void CAnimation::load(size_t frame, size_t group)
  580. {
  581. CDefFile * file = getFile();
  582. loadFrame(file, frame, group);
  583. delete file;
  584. }
  585. void CAnimation::unload(size_t frame, size_t group)
  586. {
  587. unloadFrame(frame, group);
  588. }
  589. void CAnimation::load(std::vector <std::pair <size_t, size_t> > frames)
  590. {
  591. CDefFile * file = getFile();
  592. for (size_t i=0; i<frames.size(); i++)
  593. loadFrame(file, frames[i].second, frames[i].first);
  594. delete file;
  595. }
  596. void CAnimation::unload(std::vector <std::pair <size_t, size_t> > frames)
  597. {
  598. for (size_t i=0; i<frames.size(); i++)
  599. unloadFrame(frames[i].second, frames[i].first);
  600. }
  601. void CAnimation::fixButtonPos()
  602. {
  603. if ( groupSize(0) > 1 )
  604. std::swap(entries[0][1].surf, entries[0][0].surf);
  605. }
  606. size_t CAnimation::groupSize(size_t group) const
  607. {
  608. if (entries.size() > group)
  609. return entries[group].size();
  610. return 0;
  611. }
  612. size_t CAnimation::size() const
  613. {
  614. size_t ret=0;
  615. for (size_t i=0; i<entries.size(); i++)
  616. {
  617. ret += entries[i].size();
  618. }
  619. return ret;
  620. }
  621. /*
  622. CAnimImage::CAnimImage(int x, int y, std::string name, size_t Frame, size_t Group):
  623. anim(name),
  624. frame(Frame),
  625. group(Group)
  626. {
  627. anim.load(frame, group);
  628. pos.w = anim.image(frame, group)->w;
  629. pos.h = anim.image(frame, group)->h;
  630. }
  631. CAnimImage::~CAnimImage()
  632. {
  633. }
  634. void CAnimImage::show(SDL_Surface *to)
  635. {
  636. blitAtLoc(anim.image(frame, group), 0,0, to);
  637. }
  638. void CAnimImage::setFrame(size_t Frame, size_t Group)
  639. {
  640. if (frame == Frame && group==Group)
  641. return;
  642. if (anim.groupSize(Group) > Frame)
  643. {
  644. anim.unload(frame, group);
  645. anim.load(Frame, Group);
  646. frame = Frame;
  647. group = Group;
  648. }
  649. }
  650. */
  651. CShowableAnim::CShowableAnim(int x, int y, std::string name, unsigned char Flags, unsigned int Delay, size_t Group):
  652. anim(name, Flags),
  653. group(Group),
  654. frame(0),
  655. first(0),
  656. flags(Flags),
  657. frameDelay(Delay),
  658. value(0),
  659. xOffset(0),
  660. yOffset(0)
  661. {
  662. pos.x+=x;
  663. pos.y+=y;
  664. anim.loadGroup(group);
  665. last = anim.groupSize(group);
  666. pos.w = anim.image(0, group)->w;
  667. pos.h = anim.image(0, group)->h;
  668. }
  669. CShowableAnim::~CShowableAnim()
  670. {
  671. }
  672. bool CShowableAnim::set(size_t Group, size_t from, size_t to)
  673. {
  674. size_t max = anim.groupSize(Group);
  675. if (max>to)
  676. max = to;
  677. if (max < from || max == 0)
  678. return false;
  679. anim.load(Group);
  680. anim.unload(group);
  681. group = Group;
  682. frame = first = from;
  683. last = max;
  684. value = 0;
  685. return true;
  686. }
  687. bool CShowableAnim::set(size_t Group)
  688. {
  689. if (anim.groupSize(Group)== 0)
  690. return false;
  691. if (group != Group)
  692. {
  693. anim.loadGroup(Group);
  694. anim.unloadGroup(group);
  695. first = 0;
  696. group = Group;
  697. last = anim.groupSize(Group);
  698. }
  699. frame = value = 0;
  700. return true;
  701. }
  702. void CShowableAnim::reset()
  703. {
  704. value = 0;
  705. frame = first;
  706. if (callback)
  707. callback();
  708. }
  709. void CShowableAnim::movePic( int byX, int byY)
  710. {
  711. xOffset += byX;
  712. yOffset += byY;
  713. }
  714. void CShowableAnim::show(SDL_Surface *to)
  715. {
  716. if ( flags & FLAG_BASE && frame != first)
  717. blitImage(anim.image(first, group), to);
  718. blitImage(anim.image(frame, group), to);
  719. if ( ++value == frameDelay )
  720. {
  721. value = 0;
  722. if (flags & FLAG_COMPRESSED)
  723. anim.removeDecompressed(frame, group);
  724. if ( ++frame == last)
  725. reset();
  726. }
  727. }
  728. void CShowableAnim::showAll(SDL_Surface *to)
  729. {
  730. show(to);
  731. }
  732. void CShowableAnim::blitImage(SDL_Surface *what, SDL_Surface *to)
  733. {
  734. assert(what);
  735. //TODO: SDL RLE?
  736. SDL_Rect dstRect=genRect(pos.h, pos.w, pos.x, pos.y);
  737. SDL_Rect srcRect;
  738. srcRect.x = xOffset;
  739. srcRect.y = yOffset;
  740. srcRect.w = pos.w;
  741. srcRect.h = pos.h;
  742. /*if ( flags & FLAG_ROTATED )
  743. {} //TODO: rotate surface
  744. else */
  745. if (flags & FLAG_ALPHA && what->format->BytesPerPixel == 1) //alpha on 8-bit surf - use custom blitter
  746. CSDL_Ext::blit8bppAlphaTo24bpp(what, &srcRect, to, &dstRect);
  747. else
  748. CSDL_Ext::blitSurface(what, &srcRect, to, &dstRect);
  749. }
  750. void CShowableAnim::rotate(bool on)
  751. {
  752. if (on)
  753. flags |= FLAG_ROTATED;
  754. else
  755. flags &= ~FLAG_ROTATED;
  756. }
  757. CCreatureAnim::CCreatureAnim(int x, int y, std::string name, unsigned char flags, EAnimType type):
  758. CShowableAnim(x,y,name,flags,3,type)
  759. {
  760. if (flags & FLAG_PREVIEW)
  761. callback = boost::bind(&CCreatureAnim::loopPreview,this);
  762. };
  763. void CCreatureAnim::loopPreview()
  764. {
  765. std::vector<EAnimType> available;
  766. if (anim.groupSize(ANIM_HOLDING))
  767. available.push_back(ANIM_HOLDING);
  768. if (anim.groupSize(ANIM_HITTED))
  769. available.push_back(ANIM_HITTED);
  770. if (anim.groupSize(ANIM_DEFENCE))
  771. available.push_back(ANIM_DEFENCE);
  772. if (anim.groupSize(ANIM_ATTACK_FRONT))
  773. available.push_back(ANIM_ATTACK_FRONT);
  774. if (anim.groupSize(ANIM_CAST_FRONT))
  775. available.push_back(ANIM_CAST_FRONT);
  776. size_t rnd = rand()%(available.size()*2);
  777. if (rnd >= available.size())
  778. {
  779. if ( anim.groupSize(ANIM_MOVING) == 0 )//no moving animation present
  780. addLast( ANIM_HOLDING );
  781. else
  782. addLast( ANIM_MOVING ) ;
  783. }
  784. else
  785. addLast(available[rnd]);
  786. }
  787. void CCreatureAnim::addLast(EAnimType newType)
  788. {
  789. if (type != ANIM_MOVING && newType == ANIM_MOVING)//starting moving - play init sequence
  790. {
  791. queue.push( ANIM_MOVE_START );
  792. }
  793. else if (type == ANIM_MOVING && newType != ANIM_MOVING )//previous anim was moving - finish it
  794. {
  795. queue.push( ANIM_MOVE_END);
  796. }
  797. if (newType == ANIM_TURN_L || newType == ANIM_TURN_R)
  798. queue.push(newType);
  799. queue.push(newType);
  800. }
  801. void CCreatureAnim::reset()
  802. {
  803. //if we are in the middle of rotation - set flag
  804. if (type == ANIM_TURN_L && !queue.empty() && queue.front() == ANIM_TURN_L)
  805. flags |= FLAG_ROTATED;
  806. if (type == ANIM_TURN_R && !queue.empty() && queue.front() == ANIM_TURN_R)
  807. flags &= ~FLAG_ROTATED;
  808. while (!queue.empty())//FIXME: remove dublication
  809. {
  810. EAnimType at = queue.front();
  811. queue.pop();
  812. if (set(at))
  813. return;
  814. }
  815. if (callback)
  816. callback();
  817. while (!queue.empty())
  818. {
  819. EAnimType at = queue.front();
  820. queue.pop();
  821. if (set(at))
  822. return;
  823. }
  824. tlog0<<"Warning: next sequence is not found for animation!\n";
  825. }
  826. void CCreatureAnim::clearAndSet(EAnimType type)
  827. {
  828. while (!queue.empty())
  829. queue.pop();
  830. set(type);
  831. }