CCreatureAnimation.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. #include "CCreatureAnimation.h"
  2. #include "../hch/CLodHandler.h"
  3. #include "../lib/VCMI_Lib.h"
  4. #include <assert.h>
  5. /*
  6. * CCreatureAnimation.cpp, part of VCMI engine
  7. *
  8. * Authors: listed in file AUTHORS in main folder
  9. *
  10. * License: GNU General Public License v2.0 or later
  11. * Full text of license available in license.txt file, in main folder
  12. *
  13. */
  14. int CCreatureAnimation::getType() const
  15. {
  16. return type;
  17. }
  18. void CCreatureAnimation::setType(int type)
  19. {
  20. assert(framesInGroup(type) > 0 && "Bad type for void CCreatureAnimation::setType(int type)!");
  21. this->type = type;
  22. internalFrame = 0;
  23. if(type!=-1)
  24. {
  25. curFrame = frameGroups[type][0];
  26. }
  27. else
  28. {
  29. if(curFrame>=frames)
  30. {
  31. curFrame = 0;
  32. }
  33. }
  34. }
  35. CCreatureAnimation::CCreatureAnimation(std::string name) : internalFrame(0), once(false)
  36. {
  37. FDef = spriteh->giveFile(name); //load main file
  38. //init anim data
  39. int i,j, totalInBlock;
  40. char Buffer[13];
  41. defName=name;
  42. i = 0;
  43. DEFType = readNormalNr(i,4); i+=4;
  44. fullWidth = readNormalNr(i,4); i+=4;
  45. fullHeight = readNormalNr(i,4); i+=4;
  46. i=0xc;
  47. totalBlocks = readNormalNr(i,4); i+=4;
  48. i=0x10;
  49. for (int it=0;it<256;it++)
  50. {
  51. palette[it].R = FDef[i++];
  52. palette[it].G = FDef[i++];
  53. palette[it].B = FDef[i++];
  54. palette[it].F = 0;
  55. }
  56. i=0x310;
  57. totalEntries=0;
  58. for (int z=0; z<totalBlocks; z++)
  59. {
  60. std::vector<int> frameIDs;
  61. int group = readNormalNr(i,4); i+=4; //block ID
  62. totalInBlock = readNormalNr(i,4); i+=4;
  63. for (j=SEntries.size(); j<totalEntries+totalInBlock; j++)
  64. {
  65. SEntries.push_back(SEntry());
  66. SEntries[j].group = group;
  67. frameIDs.push_back(j);
  68. }
  69. int unknown2 = readNormalNr(i,4); i+=4; //TODO use me
  70. int unknown3 = readNormalNr(i,4); i+=4; //TODO use me
  71. i+=13*totalInBlock; //ommiting names
  72. for (j=0; j<totalInBlock; j++)
  73. {
  74. SEntries[totalEntries+j].offset = readNormalNr(i,4); i+=4;
  75. }
  76. //totalEntries+=totalInBlock;
  77. for(int hh=0; hh<totalInBlock; ++hh)
  78. {
  79. ++totalEntries;
  80. }
  81. frameGroups[group] = frameIDs;
  82. }
  83. //init vars
  84. curFrame = 0;
  85. type = -1;
  86. frames = totalEntries;
  87. }
  88. int CCreatureAnimation::readNormalNr (int pos, int bytCon, unsigned char * str) const
  89. {
  90. int ret=0;
  91. int amp=1;
  92. if (str)
  93. {
  94. for (int i=0; i<bytCon; i++)
  95. {
  96. ret+=str[pos+i]*amp;
  97. amp<<=8; //amp*=256;
  98. }
  99. }
  100. else
  101. {
  102. for (int i=0; i<bytCon; i++)
  103. {
  104. ret+=FDef[pos+i]*amp;
  105. amp<<=8; //amp*=256;
  106. }
  107. }
  108. return ret;
  109. }
  110. int CCreatureAnimation::nextFrameMiddle(SDL_Surface *dest, int x, int y, bool attacker, unsigned char animCount, bool incrementFrame, bool yellowBorder, bool blueBorder, SDL_Rect * destRect)
  111. {
  112. return nextFrame(dest, x-fullWidth/2, y-fullHeight/2, attacker, animCount, incrementFrame, yellowBorder, blueBorder, destRect);
  113. }
  114. void CCreatureAnimation::incrementFrame()
  115. {
  116. if(type!=-1) //when a specific part of animation is played
  117. {
  118. ++internalFrame;
  119. if(internalFrame == frameGroups[type].size()) //rewind
  120. {
  121. internalFrame = 0;
  122. if(once) //playing animation once - return to standing animation
  123. {
  124. type = 2;
  125. once = false;
  126. curFrame = frameGroups[2][0];
  127. }
  128. else //
  129. {
  130. curFrame = frameGroups[type][0];
  131. }
  132. }
  133. curFrame = frameGroups[type][internalFrame];
  134. }
  135. else //when whole animation is played
  136. {
  137. ++curFrame;
  138. if(curFrame>=frames)
  139. curFrame = 0;
  140. }
  141. }
  142. int CCreatureAnimation::getFrame() const
  143. {
  144. return curFrame;
  145. }
  146. bool CCreatureAnimation::onFirstFrameInGroup()
  147. {
  148. return internalFrame == 0;
  149. }
  150. bool CCreatureAnimation::onLastFrameInGroup()
  151. {
  152. if(internalFrame == frameGroups[type].size() - 1)
  153. return true;
  154. return false;
  155. }
  156. void CCreatureAnimation::playOnce(int type)
  157. {
  158. setType(type);
  159. once = true;
  160. }
  161. int CCreatureAnimation::nextFrame(SDL_Surface *dest, int x, int y, bool attacker, unsigned char animCount, bool IncrementFrame, bool yellowBorder, bool blueBorder, SDL_Rect * destRect)
  162. {
  163. if(dest->format->BytesPerPixel<3)
  164. return -1; //not enough depth
  165. //increasing frame numer
  166. int SIndex = curFrame;
  167. if(IncrementFrame)
  168. incrementFrame();
  169. //frame number increased
  170. long BaseOffset,
  171. SpriteWidth, SpriteHeight, //sprite format
  172. LeftMargin, RightMargin, TopMargin,BottomMargin,
  173. i, FullHeight,FullWidth,
  174. TotalRowLength; // length of read segment
  175. unsigned char SegmentType, SegmentLength;
  176. i=BaseOffset=SEntries[SIndex].offset;
  177. int prSize=readNormalNr(i,4,FDef);i+=4;//TODO use me
  178. int defType2 = readNormalNr(i,4,FDef);i+=4;
  179. FullWidth = readNormalNr(i,4,FDef);i+=4;
  180. FullHeight = readNormalNr(i,4,FDef);i+=4;
  181. SpriteWidth = readNormalNr(i,4,FDef);i+=4;
  182. SpriteHeight = readNormalNr(i,4,FDef);i+=4;
  183. LeftMargin = readNormalNr(i,4,FDef);i+=4;
  184. TopMargin = readNormalNr(i,4,FDef);i+=4;
  185. RightMargin = FullWidth - SpriteWidth - LeftMargin;
  186. BottomMargin = FullHeight - SpriteHeight - TopMargin;
  187. int BaseOffsetor = BaseOffset = i;
  188. int ftcp = 0;
  189. if (defType2==1) //as it should be allways in creature animations
  190. {
  191. if (TopMargin>0)
  192. {
  193. ftcp+=FullWidth * TopMargin;
  194. }
  195. int * RLEntries = (int*)(FDef+BaseOffset);
  196. BaseOffset += sizeof(int) * SpriteHeight;
  197. for (int i=0;i<SpriteHeight;i++)
  198. {
  199. BaseOffset=BaseOffsetor+RLEntries[i];
  200. if (LeftMargin>0)
  201. {
  202. ftcp+=LeftMargin;
  203. }
  204. TotalRowLength=0;
  205. do
  206. {
  207. SegmentType=FDef[BaseOffset++];
  208. SegmentLength=FDef[BaseOffset++];
  209. unsigned char aCountMod = (animCount & 0x20) ? ((animCount & 0x1e)>>1)<<4 : 0x0f - ((animCount & 0x1e)>>1)<<4;
  210. if (SegmentType==0xFF)
  211. {
  212. for (int k=0;k<=SegmentLength;k++)
  213. {
  214. int xB = (attacker ? ftcp%FullWidth : FullWidth - ftcp%FullWidth - 1) + x;
  215. int yB = ftcp/FullWidth + y;
  216. if(xB>=0 && yB>=0 && xB<dest->w && yB<dest->h)
  217. {
  218. if(!destRect || (destRect->x <= xB && destRect->x + destRect->w > xB && destRect->y <= yB && destRect->y + destRect->h > yB))
  219. putPixel(dest, xB + yB*dest->w, palette[FDef[BaseOffset+k]], FDef[BaseOffset+k], yellowBorder, blueBorder, aCountMod);
  220. }
  221. ftcp++; //increment pos
  222. if ((TotalRowLength+k+1)>=SpriteWidth)
  223. break;
  224. }
  225. BaseOffset+=SegmentLength+1;////
  226. TotalRowLength+=SegmentLength+1;
  227. }
  228. else
  229. {
  230. for (int k=0;k<SegmentLength+1;k++)
  231. {
  232. int xB = (attacker ? ftcp%FullWidth : FullWidth - ftcp%FullWidth - 1) + x;
  233. int yB = ftcp/FullWidth + y;
  234. if(xB>=0 && yB>=0 && xB<dest->w && yB<dest->h)
  235. {
  236. if(!destRect || (destRect->x <= xB && destRect->x + destRect->w > xB && destRect->y <= yB && destRect->y + destRect->h > yB))
  237. putPixel(dest, xB + yB*dest->w, palette[SegmentType], SegmentType, yellowBorder, blueBorder, aCountMod);
  238. }
  239. ftcp++; //increment pos
  240. }
  241. TotalRowLength+=SegmentLength+1;
  242. }
  243. }while(TotalRowLength<SpriteWidth);
  244. if (RightMargin>0)
  245. {
  246. ftcp+=RightMargin;
  247. }
  248. }
  249. if (BottomMargin>0)
  250. {
  251. ftcp += BottomMargin * FullWidth;
  252. }
  253. }
  254. return 0;
  255. }
  256. int CCreatureAnimation::framesInGroup(int group) const
  257. {
  258. if(frameGroups.find(group) == frameGroups.end())
  259. return 0;
  260. return frameGroups.find(group)->second.size();
  261. }
  262. CCreatureAnimation::~CCreatureAnimation()
  263. {
  264. delete [] FDef;
  265. }
  266. inline void CCreatureAnimation::putPixel(
  267. SDL_Surface * dest,
  268. const int & ftcp,
  269. const BMPPalette & color,
  270. const unsigned char & palc,
  271. const bool & yellowBorder,
  272. const bool & blueBorder,
  273. const unsigned char & animCount
  274. ) const
  275. {
  276. if(palc!=0)
  277. {
  278. Uint8 * p = (Uint8*)dest->pixels + ftcp*dest->format->BytesPerPixel;
  279. if(palc > 7) //normal color
  280. {
  281. p[0] = color.B;
  282. p[1] = color.G;
  283. p[2] = color.R;
  284. }
  285. else if((yellowBorder || blueBorder) && (palc == 6 || palc == 7)) //dark yellow border
  286. {
  287. if(blueBorder)
  288. {
  289. p[0] = 0x0f + animCount;
  290. p[1] = 0x0f + animCount;
  291. p[2] = 0;
  292. }
  293. else
  294. {
  295. p[0] = 0;
  296. p[1] = 0x0f + animCount;
  297. p[2] = 0x0f + animCount;
  298. }
  299. }
  300. else if((yellowBorder || blueBorder) && (palc == 5)) //yellow border
  301. {
  302. if(blueBorder)
  303. {
  304. p[0] = color.R - 0xf0 + animCount;
  305. p[1] = color.G - 0xf0 + animCount;
  306. p[2] = color.B;
  307. }
  308. else
  309. {
  310. p[0] = color.B;
  311. p[1] = color.G - 0xf0 + animCount;
  312. p[2] = color.R - 0xf0 + animCount;
  313. }
  314. }
  315. else if(palc < 5) //shadow
  316. {
  317. Uint16 alpha;
  318. switch(color.G)
  319. {
  320. case 0:
  321. alpha = 128;
  322. break;
  323. case 50:
  324. alpha = 50+32;
  325. break;
  326. case 100:
  327. alpha = 100+64;
  328. break;
  329. case 125:
  330. alpha = 125+64;
  331. break;
  332. case 128:
  333. alpha = 128+64;
  334. break;
  335. case 150:
  336. alpha = 150+64;
  337. break;
  338. default:
  339. alpha = 255;
  340. break;
  341. }
  342. //alpha counted
  343. p[0] = (p[0] * alpha)>>8;
  344. p[1] = (p[1] * alpha)>>8;
  345. p[2] = (p[2] * alpha)>>8;
  346. }
  347. }
  348. }