CAnimation.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  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 "../hch/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. static SDL_Color H3Palette[8] = {{ 0, 0, 0, 255},
  250. { 0, 0, 0, 192},
  251. /* 5 shadow colors */ { 0, 0, 0, 128},
  252. { 0, 0, 0, 64},
  253. { 0, 0, 0, 32},
  254. {255, 255, 0, 255},
  255. /* 3 selection highlight color */{255, 255, 0, 255},
  256. {255, 255, 0, 255}};
  257. data = spriteh->giveFile(Name, FILE_ANIMATION, &datasize);
  258. if (!data)
  259. {
  260. tlog0<<"Error: file "<< Name <<" not found\n";
  261. return;
  262. }
  263. colors = new BMPPalette[256];
  264. int it = 0;
  265. type = readNormalNr(data, it); it+=4;
  266. //int width = readNormalNr(data, it); it+=4;//not used
  267. //int height = readNormalNr(data, it); it+=4;
  268. it+=8;
  269. unsigned int totalBlocks = readNormalNr(data, it);
  270. it+=4;
  271. for (unsigned int i= 0; i<256; i++)
  272. {
  273. colors[i].R = data[it++];
  274. colors[i].G = data[it++];
  275. colors[i].B = data[it++];
  276. colors[i].F = 0;
  277. }
  278. memcpy(colors, H3Palette, (type == 66)? 32:20);//initialize shadow\selection colors
  279. offList.insert(datasize);
  280. for (unsigned int i=0; i<totalBlocks; i++)
  281. {
  282. size_t blockID = readNormalNr(data, it);
  283. it+=4;
  284. size_t totalEntries = readNormalNr(data, it);
  285. it+=12;
  286. //8 unknown bytes - skipping
  287. //13 bytes for name of every frame in this block - not used, skipping
  288. it+= 13 * totalEntries;
  289. offset.resize(std::max(blockID+1, offset.size()));
  290. for (unsigned int j=0; j<totalEntries; j++)
  291. {
  292. size_t currOffset = readNormalNr(data, it);
  293. offset[blockID].push_back(currOffset);
  294. offList.insert(currOffset);
  295. it += 4;
  296. }
  297. }
  298. }
  299. unsigned char * CDefFile::getFrame(size_t frame, size_t group) const
  300. {
  301. if (offset.size() > group)
  302. {
  303. if (offset[group].size() > frame)
  304. {
  305. size_t offs = offset[group][frame];
  306. std::set<size_t>::iterator it = offList.find(offs);
  307. if (it == offList.end() || ++it == offList.end())
  308. tlog0<<"Error: offset not found!\n";
  309. size_t size = *it - offs;
  310. unsigned char * ret = new unsigned char[size];
  311. memcpy(ret, data+offs, size);
  312. return ret;
  313. }
  314. }
  315. return NULL;
  316. }
  317. CDefFile::~CDefFile()
  318. {
  319. offset.clear();
  320. offList.clear();
  321. delete[] data;
  322. delete[] colors;
  323. }
  324. bool CDefFile::loaded() const
  325. {
  326. return data != NULL;
  327. }
  328. /*************************************************************************
  329. * CAnimation for animations handling, can load part of file if needed *
  330. *************************************************************************/
  331. CAnimation::AnimEntry::AnimEntry():
  332. surf(NULL),
  333. source(0),
  334. refCount(0),
  335. data(NULL),
  336. dataSize(0)
  337. {
  338. }
  339. bool CAnimation::loadFrame(CDefFile * file, size_t frame, size_t group)
  340. {
  341. if (groupSize(group) <= frame)
  342. {
  343. printError(frame, group, "LoadFrame");
  344. return false;
  345. }
  346. AnimEntry &e = entries[group][frame];
  347. if (e.surf || e.data)
  348. {
  349. e.refCount++;
  350. return true;
  351. }
  352. if (e.source & 6)//load frame with SDL_Image
  353. {
  354. int size;
  355. unsigned char * pic = NULL;
  356. std::ostringstream str;
  357. if ( e.source & 2 )
  358. str << name << '#' << (group+1) << '#' << (frame+1); // file#12#34.*
  359. else
  360. str << name << '#' << (frame+1);//file#34.*
  361. pic = spriteh->giveFile(str.str(), FILE_GRAPHICS, &size);
  362. if (pic)
  363. {
  364. if (compressed)
  365. {
  366. e.data = pic;
  367. e.dataSize = size;
  368. }
  369. else
  370. {
  371. e.surf = IMG_Load_RW( SDL_RWFromMem((void*)pic, size), 1);
  372. delete [] pic;
  373. }
  374. }
  375. }
  376. else if (file && e.source & 1)//try to get image from def
  377. {
  378. if (compressed)
  379. e.data = file->getFrame(frame, group);
  380. else
  381. e.surf = file->loadFrame(frame, group);
  382. }
  383. if (!(e.surf || e.data))
  384. return false;//failed to load
  385. e.refCount++;
  386. return true;
  387. }
  388. bool CAnimation::unloadFrame(size_t frame, size_t group)
  389. {
  390. if (groupSize(group) > frame && entries[group][frame].refCount)
  391. {
  392. AnimEntry &e = entries[group][frame];
  393. if (--e.refCount)//not last ref
  394. return true;
  395. SDL_FreeSurface(e.surf);
  396. delete [] e.data;
  397. e.surf = NULL;
  398. e.data = NULL;
  399. return true;
  400. }
  401. return false;
  402. }
  403. void CAnimation::decompress(AnimEntry &entry)
  404. {
  405. if (entry.source & 6)//load frame with SDL_Image
  406. entry.surf = IMG_Load_RW( SDL_RWFromMem((void*)entry.data, entry.dataSize), 1);
  407. else if (entry.source & 1)
  408. entry.surf = CDefFile::loadFrame(entry.data, defPalette);
  409. }
  410. void CAnimation::init(CDefFile * file)
  411. {
  412. if (compressed)
  413. defPalette = file->getPalette();
  414. for (size_t group = 0; ; group++)
  415. {
  416. std::vector<AnimEntry> toAdd;
  417. for (size_t frame = 0; ; frame++)
  418. {
  419. unsigned char res=0;
  420. {
  421. std::ostringstream str;
  422. str << name << '#' << (group+1) << '#' << (frame+1); // format: file#12#34.*
  423. if (spriteh->haveFile(str.str()))
  424. res |= 2;
  425. }
  426. if (group == 0)
  427. {
  428. std::ostringstream str;
  429. str << name << '#' << (frame+1);// format: file#34.*
  430. if ( spriteh->haveFile(str.str()))
  431. res |=4;
  432. }
  433. if (file)//we have def too. try to get image from def
  434. {
  435. if (file->haveFrame(frame, group))
  436. res |=1;
  437. }
  438. if (res)
  439. {
  440. toAdd.push_back(AnimEntry());
  441. toAdd.back().source = res;
  442. }
  443. else
  444. break;
  445. }
  446. if (!toAdd.empty())
  447. {
  448. entries.push_back(toAdd);
  449. }
  450. else
  451. {
  452. entries.resize(entries.size()+1);
  453. if (group > 21)
  454. break;//FIXME: crude workaround: if some groups are not present
  455. //(common for creatures) parser will exit before reaching them
  456. }
  457. }
  458. }
  459. CDefFile * CAnimation::getFile() const
  460. {
  461. CDefFile * file = new CDefFile(name);
  462. if (!file->loaded())
  463. {
  464. delete file;
  465. return NULL;
  466. }
  467. return file;
  468. }
  469. void CAnimation::printError(size_t frame, size_t group, std::string type) const
  470. {
  471. tlog0 << type <<" error: Request for frame not present in CAnimation!\n"
  472. <<"\tFile name: "<<name<<" Group: "<<group<<" Frame: "<<frame<<"\n";
  473. }
  474. CAnimation::CAnimation(std::string Name, bool Compressed):
  475. name(Name),
  476. compressed(Compressed),
  477. defPalette(NULL)
  478. {
  479. std::transform(name.begin(), name.end(), name.begin(), (int(*)(int))toupper);
  480. int dotPos = name.find_last_of('.');
  481. if ( dotPos != -1 )
  482. name.erase(dotPos);
  483. CDefFile * file = getFile();
  484. init(file);
  485. delete file;
  486. }
  487. CAnimation::CAnimation():
  488. name(""),
  489. compressed(false),
  490. defPalette(NULL)
  491. {
  492. }
  493. CAnimation::~CAnimation()
  494. {
  495. delete [] defPalette;
  496. for (size_t i = 0; i < entries.size(); i++)
  497. for (size_t j = 0; j < entries.at(i).size(); j++)
  498. {
  499. delete [] entries[i][j].data;
  500. if (entries[i][j].surf)
  501. SDL_FreeSurface(entries[i][j].surf);
  502. }
  503. }
  504. void CAnimation::add(SDL_Surface * surf, bool shared, size_t group)
  505. {
  506. if (!surf)
  507. return;
  508. if (entries.size() <= group)
  509. entries.resize(group+1);
  510. if (shared)
  511. surf->refcount++;
  512. entries[group].push_back(AnimEntry());
  513. entries[group].back().refCount = 1;
  514. entries[group].back().surf = surf;
  515. }
  516. void CAnimation::removeDecompressed(size_t frame, size_t group)
  517. {
  518. AnimEntry &e = entries[group][frame];
  519. if (e.surf && e.data)
  520. {
  521. SDL_FreeSurface(e.surf);
  522. e.surf = NULL;
  523. }
  524. }
  525. SDL_Surface * CAnimation::image(size_t frame)
  526. {
  527. size_t group=0;
  528. while (group<entries.size() && frame > entries[group].size())
  529. frame -= entries[group].size();
  530. if (group <entries.size() && frame < entries[group].size())
  531. return image(frame, group);
  532. return NULL;
  533. }
  534. SDL_Surface * CAnimation::image(size_t frame, size_t group)
  535. {
  536. if ( groupSize(group) > frame )
  537. {
  538. AnimEntry &e = entries[group][frame];
  539. if (!e.surf && e.data)
  540. decompress(e);
  541. return e.surf;
  542. }
  543. printError(frame, group, "GetImage");
  544. return NULL;
  545. }
  546. void CAnimation::clear()
  547. {
  548. unload();
  549. entries.clear();
  550. }
  551. void CAnimation::load()
  552. {
  553. CDefFile * file = getFile();
  554. for (size_t group = 0; group<entries.size(); group++)
  555. for (size_t frame = 0; frame<entries[group].size(); frame++)
  556. loadFrame(file, frame, group);
  557. delete file;
  558. }
  559. void CAnimation::unload()
  560. {
  561. for (size_t group = 0; group<entries.size(); group++)
  562. for (size_t frame = 0; frame<entries[group].size(); frame++)
  563. unloadFrame(frame, group);
  564. }
  565. void CAnimation::loadGroup(size_t group)
  566. {
  567. CDefFile * file = getFile();
  568. for (size_t frame = 0; frame<groupSize(group); frame++)
  569. loadFrame(file, frame, group);
  570. delete file;
  571. }
  572. void CAnimation::unloadGroup(size_t group)
  573. {
  574. for (size_t frame = 0; frame<groupSize(group); frame++)
  575. unloadFrame(frame, group);
  576. }
  577. void CAnimation::load(size_t frame, size_t group)
  578. {
  579. CDefFile * file = getFile();
  580. loadFrame(file, frame, group);
  581. delete file;
  582. }
  583. void CAnimation::unload(size_t frame, size_t group)
  584. {
  585. unloadFrame(frame, group);
  586. }
  587. void CAnimation::load(std::vector <std::pair <size_t, size_t> > frames)
  588. {
  589. CDefFile * file = getFile();
  590. for (size_t i=0; i<frames.size(); i++)
  591. loadFrame(file, frames[i].second, frames[i].first);
  592. delete file;
  593. }
  594. void CAnimation::unload(std::vector <std::pair <size_t, size_t> > frames)
  595. {
  596. for (size_t i=0; i<frames.size(); i++)
  597. unloadFrame(frames[i].second, frames[i].first);
  598. }
  599. void CAnimation::fixButtonPos()
  600. {
  601. if ( groupSize(0) > 1 )
  602. std::swap(entries[0][1].surf, entries[0][0].surf);
  603. }
  604. size_t CAnimation::groupSize(size_t group) const
  605. {
  606. if (entries.size() > group)
  607. return entries[group].size();
  608. return 0;
  609. }
  610. size_t CAnimation::size() const
  611. {
  612. size_t ret=0;
  613. for (size_t i=0; i<entries.size(); i++)
  614. {
  615. ret += entries[i].size();
  616. }
  617. return ret;
  618. }
  619. /*
  620. CAnimImage::CAnimImage(int x, int y, std::string name, size_t Frame, size_t Group):
  621. anim(name),
  622. frame(Frame),
  623. group(Group)
  624. {
  625. anim.load(frame, group);
  626. pos.w = anim.image(frame, group)->w;
  627. pos.h = anim.image(frame, group)->h;
  628. }
  629. CAnimImage::~CAnimImage()
  630. {
  631. }
  632. void CAnimImage::show(SDL_Surface *to)
  633. {
  634. blitAtLoc(anim.image(frame, group), 0,0, to);
  635. }
  636. void CAnimImage::setFrame(size_t Frame, size_t Group)
  637. {
  638. if (frame == Frame && group==Group)
  639. return;
  640. if (anim.groupSize(Group) > Frame)
  641. {
  642. anim.unload(frame, group);
  643. anim.load(Frame, Group);
  644. frame = Frame;
  645. group = Group;
  646. }
  647. }
  648. */
  649. CShowableAnim::CShowableAnim(int x, int y, std::string name, unsigned char Flags, unsigned int Delay, size_t Group):
  650. anim(name, Flags),
  651. group(Group),
  652. frame(0),
  653. first(0),
  654. flags(Flags),
  655. frameDelay(Delay),
  656. value(0),
  657. xOffset(0),
  658. yOffset(0)
  659. {
  660. pos.x+=x;
  661. pos.y+=y;
  662. anim.loadGroup(group);
  663. last = anim.groupSize(group);
  664. pos.w = anim.image(0, group)->w;
  665. pos.h = anim.image(0, group)->h;
  666. }
  667. CShowableAnim::~CShowableAnim()
  668. {
  669. }
  670. bool CShowableAnim::set(size_t Group, size_t from, size_t to)
  671. {
  672. size_t max = anim.groupSize(Group);
  673. if (max>to)
  674. max = to;
  675. if (max < from || max == 0)
  676. return false;
  677. anim.load(Group);
  678. anim.unload(group);
  679. group = Group;
  680. frame = first = from;
  681. last = max;
  682. value = 0;
  683. return true;
  684. }
  685. bool CShowableAnim::set(size_t Group)
  686. {
  687. if (anim.groupSize(Group)== 0)
  688. return false;
  689. if (group != Group)
  690. {
  691. anim.loadGroup(Group);
  692. anim.unloadGroup(group);
  693. first = 0;
  694. group = Group;
  695. last = anim.groupSize(Group);
  696. }
  697. frame = value = 0;
  698. return true;
  699. }
  700. void CShowableAnim::reset()
  701. {
  702. value = 0;
  703. frame = first;
  704. if (callback)
  705. callback();
  706. }
  707. void CShowableAnim::movePic( int byX, int byY)
  708. {
  709. xOffset += byX;
  710. yOffset += byY;
  711. }
  712. void CShowableAnim::show(SDL_Surface *to)
  713. {
  714. if ( flags & FLAG_BASE && frame != first)
  715. blitImage(anim.image(first, group), to);
  716. blitImage(anim.image(frame, group), to);
  717. if ( ++value == frameDelay )
  718. {
  719. value = 0;
  720. if (flags & FLAG_COMPRESSED)
  721. anim.removeDecompressed(frame, group);
  722. if ( ++frame == last)
  723. reset();
  724. }
  725. }
  726. void CShowableAnim::showAll(SDL_Surface *to)
  727. {
  728. show(to);
  729. }
  730. void CShowableAnim::blitImage(SDL_Surface *what, SDL_Surface *to)
  731. {
  732. assert(what);
  733. //TODO: SDL RLE?
  734. SDL_Rect dstRect=genRect(pos.h, pos.w, pos.x, pos.y);
  735. SDL_Rect srcRect;
  736. srcRect.x = xOffset;
  737. srcRect.y = yOffset;
  738. srcRect.w = pos.w;
  739. srcRect.h = pos.h;
  740. /*if ( flags & FLAG_ROTATED )
  741. {} //TODO: rotate surface
  742. else */
  743. if (flags & FLAG_ALPHA && what->format->BytesPerPixel == 1) //alpha on 8-bit surf - use custom blitter
  744. CSDL_Ext::blit8bppAlphaTo24bpp(what, &srcRect, to, &dstRect);
  745. else
  746. CSDL_Ext::blitSurface(what, &srcRect, to, &dstRect);
  747. }
  748. void CShowableAnim::rotate(bool on)
  749. {
  750. if (on)
  751. flags |= FLAG_ROTATED;
  752. else
  753. flags &= ~FLAG_ROTATED;
  754. }
  755. CCreatureAnim::CCreatureAnim(int x, int y, std::string name, unsigned char flags, EAnimType type):
  756. CShowableAnim(x,y,name,flags,3,type)
  757. {
  758. if (flags & FLAG_PREVIEW)
  759. callback = boost::bind(&CCreatureAnim::loopPreview,this);
  760. };
  761. void CCreatureAnim::loopPreview()
  762. {
  763. std::vector<EAnimType> available;
  764. if (anim.groupSize(ANIM_HOLDING))
  765. available.push_back(ANIM_HOLDING);
  766. if (anim.groupSize(ANIM_HITTED))
  767. available.push_back(ANIM_HITTED);
  768. if (anim.groupSize(ANIM_DEFENCE))
  769. available.push_back(ANIM_DEFENCE);
  770. if (anim.groupSize(ANIM_ATTACK_FRONT))
  771. available.push_back(ANIM_ATTACK_FRONT);
  772. if (anim.groupSize(ANIM_CAST_FRONT))
  773. available.push_back(ANIM_CAST_FRONT);
  774. size_t rnd = rand()%(available.size()*2);
  775. if (rnd >= available.size())
  776. {
  777. if ( anim.groupSize(ANIM_MOVING) == 0 )//no moving animation present
  778. addLast( ANIM_HOLDING );
  779. else
  780. addLast( ANIM_MOVING ) ;
  781. }
  782. else
  783. addLast(available[rnd]);
  784. }
  785. void CCreatureAnim::addLast(EAnimType newType)
  786. {
  787. if (type != ANIM_MOVING && newType == ANIM_MOVING)//starting moving - play init sequence
  788. {
  789. queue.push( ANIM_MOVE_START );
  790. }
  791. else if (type == ANIM_MOVING && newType != ANIM_MOVING )//previous anim was moving - finish it
  792. {
  793. queue.push( ANIM_MOVE_END);
  794. }
  795. if (newType == ANIM_TURN_L || newType == ANIM_TURN_R)
  796. queue.push(newType);
  797. queue.push(newType);
  798. }
  799. void CCreatureAnim::reset()
  800. {
  801. //if we are in the middle of rotation - set flag
  802. if (type == ANIM_TURN_L && !queue.empty() && queue.front() == ANIM_TURN_L)
  803. flags |= FLAG_ROTATED;
  804. if (type == ANIM_TURN_R && !queue.empty() && queue.front() == ANIM_TURN_R)
  805. flags &= ~FLAG_ROTATED;
  806. while (!queue.empty())//FIXME: remove dublication
  807. {
  808. EAnimType at = queue.front();
  809. queue.pop();
  810. if (set(at))
  811. return;
  812. }
  813. if (callback)
  814. callback();
  815. while (!queue.empty())
  816. {
  817. EAnimType at = queue.front();
  818. queue.pop();
  819. if (set(at))
  820. return;
  821. }
  822. tlog0<<"Warning: next sequence is not found for animation!\n";
  823. }
  824. void CCreatureAnim::clearAndSet(EAnimType type)
  825. {
  826. while (!queue.empty())
  827. queue.pop();
  828. set(type);
  829. }