CCreatureAnimation.cpp 8.4 KB

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