CCreatureAnimation.cpp 7.8 KB

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