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 = (internalFrame + 1) % frameGroups[type].size();
  108. curFrame = frameGroups[type][internalFrame];
  109. if(internalFrame == frameGroups[type].size()) //rewind
  110. {
  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. }
  123. else //when whole animation is played
  124. {
  125. ++curFrame;
  126. if(curFrame>=frames)
  127. curFrame = 0;
  128. }
  129. }
  130. int CCreatureAnimation::getFrame() const
  131. {
  132. return curFrame;
  133. }
  134. bool CCreatureAnimation::onLastFrameInGroup()
  135. {
  136. if(internalFrame == frameGroups[type].size() - 1)
  137. return true;
  138. return false;
  139. }
  140. void CCreatureAnimation::playOnce(int type)
  141. {
  142. setType(type);
  143. once = true;
  144. }
  145. int CCreatureAnimation::nextFrame(SDL_Surface *dest, int x, int y, bool attacker, unsigned char animCount, bool IncrementFrame, bool yellowBorder, bool blueBorder, SDL_Rect * destRect)
  146. {
  147. if(dest->format->BytesPerPixel<3)
  148. return -1; //not enough depth
  149. //increasing frame numer
  150. int SIndex = curFrame;
  151. if(IncrementFrame)
  152. incrementFrame();
  153. //frame number increased
  154. long BaseOffset,
  155. SpriteWidth, SpriteHeight, //sprite format
  156. LeftMargin, RightMargin, TopMargin,BottomMargin,
  157. i, FullHeight,FullWidth,
  158. TotalRowLength; // length of read segment
  159. unsigned char SegmentType, SegmentLength;
  160. i=BaseOffset=SEntries[SIndex].offset;
  161. int prSize=readNormalNr(i,4,FDef);i+=4;//TODO use me
  162. int defType2 = readNormalNr(i,4,FDef);i+=4;
  163. FullWidth = readNormalNr(i,4,FDef);i+=4;
  164. FullHeight = readNormalNr(i,4,FDef);i+=4;
  165. SpriteWidth = readNormalNr(i,4,FDef);i+=4;
  166. SpriteHeight = readNormalNr(i,4,FDef);i+=4;
  167. LeftMargin = readNormalNr(i,4,FDef);i+=4;
  168. TopMargin = readNormalNr(i,4,FDef);i+=4;
  169. RightMargin = FullWidth - SpriteWidth - LeftMargin;
  170. BottomMargin = FullHeight - SpriteHeight - TopMargin;
  171. int BaseOffsetor = BaseOffset = i;
  172. int ftcp = 0;
  173. if (defType2==1) //as it should be allways in creature animations
  174. {
  175. if (TopMargin>0)
  176. {
  177. ftcp+=FullWidth * TopMargin;
  178. }
  179. memcpy(RLEntries, FDef+BaseOffset, SpriteHeight*sizeof(int));
  180. BaseOffset += sizeof(int) * SpriteHeight;
  181. for (int i=0;i<SpriteHeight;i++)
  182. {
  183. BaseOffset=BaseOffsetor+RLEntries[i];
  184. if (LeftMargin>0)
  185. {
  186. ftcp+=LeftMargin;
  187. }
  188. TotalRowLength=0;
  189. do
  190. {
  191. SegmentType=FDef[BaseOffset++];
  192. SegmentLength=FDef[BaseOffset++];
  193. unsigned char aCountMod = (animCount & 0x20) ? ((animCount & 0x1e)>>1)<<4 : 0x0f - ((animCount & 0x1e)>>1)<<4;
  194. if (SegmentType==0xFF)
  195. {
  196. for (int k=0;k<=SegmentLength;k++)
  197. {
  198. int xB = (attacker ? ftcp%FullWidth : FullWidth - ftcp%FullWidth - 1) + x;
  199. int yB = ftcp/FullWidth + y;
  200. if(xB>=0 && yB>=0 && xB<dest->w && yB<dest->h)
  201. {
  202. if(!destRect || (destRect->x <= xB && destRect->x + destRect->w > xB && destRect->y <= yB && destRect->y + destRect->h > yB))
  203. putPixel(dest, xB + yB*dest->w, palette[FDef[BaseOffset+k]], FDef[BaseOffset+k], yellowBorder, blueBorder, aCountMod);
  204. }
  205. ftcp++; //increment pos
  206. if ((TotalRowLength+k+1)>=SpriteWidth)
  207. break;
  208. }
  209. BaseOffset+=SegmentLength+1;////
  210. TotalRowLength+=SegmentLength+1;
  211. }
  212. else
  213. {
  214. for (int k=0;k<SegmentLength+1;k++)
  215. {
  216. int xB = (attacker ? ftcp%FullWidth : FullWidth - ftcp%FullWidth - 1) + x;
  217. int yB = ftcp/FullWidth + y;
  218. if(xB>=0 && yB>=0 && xB<dest->w && yB<dest->h)
  219. {
  220. if(!destRect || (destRect->x <= xB && destRect->x + destRect->w > xB && destRect->y <= yB && destRect->y + destRect->h > yB))
  221. putPixel(dest, xB + yB*dest->w, palette[SegmentType], SegmentType, yellowBorder, blueBorder, aCountMod);
  222. }
  223. ftcp++; //increment pos
  224. }
  225. TotalRowLength+=SegmentLength+1;
  226. }
  227. }while(TotalRowLength<SpriteWidth);
  228. if (RightMargin>0)
  229. {
  230. ftcp+=RightMargin;
  231. }
  232. }
  233. if (BottomMargin>0)
  234. {
  235. ftcp += BottomMargin * FullWidth;
  236. }
  237. }
  238. return 0;
  239. }
  240. int CCreatureAnimation::framesInGroup(int group) const
  241. {
  242. if(frameGroups.find(group) == frameGroups.end())
  243. return 0;
  244. return frameGroups.find(group)->second.size();
  245. }
  246. CCreatureAnimation::~CCreatureAnimation()
  247. {
  248. delete [] FDef;
  249. if (RLEntries)
  250. delete [] RLEntries;
  251. }
  252. inline void CCreatureAnimation::putPixel(
  253. SDL_Surface * dest,
  254. const int & ftcp,
  255. const BMPPalette & color,
  256. const unsigned char & palc,
  257. const bool & yellowBorder,
  258. const bool & blueBorder,
  259. const unsigned char & animCount
  260. ) const
  261. {
  262. if(palc!=0)
  263. {
  264. Uint8 * p = (Uint8*)dest->pixels + ftcp*3;
  265. if(palc > 7) //normal color
  266. {
  267. p[0] = color.B;
  268. p[1] = color.G;
  269. p[2] = color.R;
  270. }
  271. else if((yellowBorder || blueBorder) && (palc == 6 || palc == 7)) //dark yellow border
  272. {
  273. if(blueBorder)
  274. {
  275. p[0] = 0x0f + animCount;
  276. p[1] = 0x0f + animCount;
  277. p[2] = 0;
  278. }
  279. else
  280. {
  281. p[0] = 0;
  282. p[1] = 0x0f + animCount;
  283. p[2] = 0x0f + animCount;
  284. }
  285. }
  286. else if((yellowBorder || blueBorder) && (palc == 5)) //yellow border
  287. {
  288. if(blueBorder)
  289. {
  290. p[0] = color.R - 0xf0 + animCount;
  291. p[1] = color.G - 0xf0 + animCount;
  292. p[2] = color.B;
  293. }
  294. else
  295. {
  296. p[0] = color.B;
  297. p[1] = color.G - 0xf0 + animCount;
  298. p[2] = color.R - 0xf0 + animCount;
  299. }
  300. }
  301. else if(palc < 5) //shadow
  302. {
  303. Uint16 alpha;
  304. switch(color.G)
  305. {
  306. case 0:
  307. alpha = 128;
  308. break;
  309. case 50:
  310. alpha = 50+32;
  311. break;
  312. case 100:
  313. alpha = 100+64;
  314. break;
  315. case 125:
  316. alpha = 125+64;
  317. break;
  318. case 128:
  319. alpha = 128+64;
  320. break;
  321. case 150:
  322. alpha = 150+64;
  323. break;
  324. default:
  325. alpha = 255;
  326. break;
  327. }
  328. //alpha counted
  329. p[0] = (p[0] * alpha)>>8;
  330. p[1] = (p[1] * alpha)>>8;
  331. p[2] = (p[2] * alpha)>>8;
  332. }
  333. }
  334. }