CCreatureAnimation.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 unknown1 = readNormalNr(i,4); i+=4;
  60. totalInBlock = readNormalNr(i,4); i+=4;
  61. for (j=SEntries.size(); j<totalEntries+totalInBlock; j++)
  62. SEntries.push_back(SEntry());
  63. int unknown2 = readNormalNr(i,4); i+=4;
  64. int unknown3 = readNormalNr(i,4); i+=4;
  65. for (j=0; j<totalInBlock; j++)
  66. {
  67. for (int k=0;k<13;k++) Buffer[k]=FDef[i+k];
  68. i+=13;
  69. SEntries[totalEntries+j].name=Buffer;
  70. }
  71. for (j=0; j<totalInBlock; j++)
  72. {
  73. SEntries[totalEntries+j].offset = readNormalNr(i,4);
  74. int unknown4 = readNormalNr(i,4); i+=4;
  75. }
  76. //totalEntries+=totalInBlock;
  77. for(int hh=0; hh<totalInBlock; ++hh)
  78. {
  79. SEntries[totalEntries].group = z;
  80. ++totalEntries;
  81. }
  82. }
  83. for(j=0; j<SEntries.size(); ++j)
  84. {
  85. SEntries[j].name = SEntries[j].name.substr(0, SEntries[j].name.find('.')+4);
  86. }
  87. //init vars
  88. curFrame = 0;
  89. type = -1;
  90. frames = totalEntries;
  91. }
  92. int CCreatureAnimation::readNormalNr (int pos, int bytCon, unsigned char * str, bool cyclic)
  93. {
  94. int ret=0;
  95. int amp=1;
  96. if (str)
  97. {
  98. for (int i=0; i<bytCon; i++)
  99. {
  100. ret+=str[pos+i]*amp;
  101. amp*=256;
  102. }
  103. }
  104. else
  105. {
  106. for (int i=0; i<bytCon; i++)
  107. {
  108. ret+=FDef[pos+i]*amp;
  109. amp*=256;
  110. }
  111. }
  112. if(cyclic && bytCon<4 && ret>=amp/2)
  113. {
  114. ret = ret-amp;
  115. }
  116. return ret;
  117. }
  118. int CCreatureAnimation::nextFrameMiddle(SDL_Surface *dest, int x, int y, bool attacker, bool incrementFrame, bool yellowBorder, SDL_Rect * destRect)
  119. {
  120. return nextFrame(dest,x-fullWidth/2,y-fullHeight/2,attacker,incrementFrame,yellowBorder,destRect);
  121. }
  122. int CCreatureAnimation::nextFrame(SDL_Surface *dest, int x, int y, bool attacker, bool incrementFrame, bool yellowBorder, SDL_Rect * destRect)
  123. {
  124. if(dest->format->BytesPerPixel<3)
  125. return -1; //not enough depth
  126. //increasing frame numer
  127. int SIndex = -1;
  128. if(incrementFrame)
  129. {
  130. SIndex = curFrame++;
  131. if(type!=-1)
  132. {
  133. if(SEntries[curFrame].group!=type) //rewind
  134. {
  135. int j=-1; //first frame in displayed group
  136. for(int g=0; g<SEntries.size(); ++g)
  137. {
  138. if(SEntries[g].group==type && j==-1)
  139. {
  140. j = g;
  141. break;
  142. }
  143. }
  144. if(curFrame!=-1)
  145. curFrame = j;
  146. }
  147. }
  148. else
  149. {
  150. if(curFrame>=frames)
  151. curFrame = 0;
  152. }
  153. }
  154. else
  155. {
  156. SIndex = curFrame;
  157. }
  158. //frame number increased
  159. long BaseOffset,
  160. SpriteWidth, SpriteHeight, //sprite format
  161. LeftMargin, RightMargin, TopMargin,BottomMargin,
  162. i, add, FullHeight,FullWidth,
  163. TotalRowLength, // length of read segment
  164. RowAdd;
  165. unsigned char SegmentType, SegmentLength;
  166. i=BaseOffset=SEntries[SIndex].offset;
  167. int prSize=readNormalNr(i,4,FDef);i+=4;
  168. int defType2 = readNormalNr(i,4,FDef);i+=4;
  169. FullWidth = readNormalNr(i,4,FDef);i+=4;
  170. FullHeight = readNormalNr(i,4,FDef);i+=4;
  171. SpriteWidth = readNormalNr(i,4,FDef);i+=4;
  172. SpriteHeight = readNormalNr(i,4,FDef);i+=4;
  173. LeftMargin = readNormalNr(i,4,FDef);i+=4;
  174. TopMargin = readNormalNr(i,4,FDef);i+=4;
  175. RightMargin = FullWidth - SpriteWidth - LeftMargin;
  176. BottomMargin = FullHeight - SpriteHeight - TopMargin;
  177. add = 4 - FullWidth%4;
  178. int BaseOffsetor = BaseOffset = i;
  179. int ftcp = 0;
  180. if (defType2==1) //as it should be allways in creature animations
  181. {
  182. if (TopMargin>0)
  183. {
  184. for (int i=0;i<TopMargin;i++)
  185. {
  186. ftcp+=FullWidth+add;
  187. }
  188. }
  189. RLEntries = new int[SpriteHeight];
  190. for (int i=0;i<SpriteHeight;i++)
  191. {
  192. RLEntries[i]=readNormalNr(BaseOffset,4,FDef);BaseOffset+=4;
  193. }
  194. for (int i=0;i<SpriteHeight;i++)
  195. {
  196. BaseOffset=BaseOffsetor+RLEntries[i];
  197. if (LeftMargin>0)
  198. {
  199. ftcp+=LeftMargin;
  200. }
  201. TotalRowLength=0;
  202. do
  203. {
  204. SegmentType=FDef[BaseOffset++];
  205. SegmentLength=FDef[BaseOffset++];
  206. if (SegmentType==0xFF)
  207. {
  208. for (int k=0;k<=SegmentLength;k++)
  209. {
  210. int xB = (attacker ? ftcp%(FullWidth+add) : (FullWidth+add) - ftcp%(FullWidth+add) - 1) + x;
  211. int yB = ftcp/(FullWidth+add) + y;
  212. if(xB>=0 && yB>=0 && xB<dest->w && yB<dest->h)
  213. {
  214. if(!destRect || (destRect->x <= xB && destRect->x + destRect->w > xB && destRect->y <= yB && destRect->y + destRect->h > yB))
  215. putPixel(dest, xB + yB*dest->w, palette[FDef[BaseOffset+k]], FDef[BaseOffset+k], yellowBorder);
  216. }
  217. ftcp++; //increment pos
  218. if ((TotalRowLength+k+1)>=SpriteWidth)
  219. break;
  220. }
  221. BaseOffset+=SegmentLength+1;////
  222. TotalRowLength+=SegmentLength+1;
  223. }
  224. else
  225. {
  226. for (int k=0;k<SegmentLength+1;k++)
  227. {
  228. int xB = (attacker ? ftcp%(FullWidth+add) : (FullWidth+add) - ftcp%(FullWidth+add) - 1) + x;
  229. int yB = ftcp/(FullWidth+add) + y;
  230. if(xB>=0 && yB>=0 && xB<dest->w && yB<dest->h)
  231. {
  232. if(!destRect || (destRect->x <= xB && destRect->x + destRect->w > xB && destRect->y <= yB && destRect->y + destRect->h > yB))
  233. putPixel(dest, xB + yB*dest->w, palette[SegmentType], SegmentType, yellowBorder);
  234. }
  235. ftcp++; //increment pos
  236. }
  237. TotalRowLength+=SegmentLength+1;
  238. }
  239. }while(TotalRowLength<SpriteWidth);
  240. RowAdd=SpriteWidth-TotalRowLength;
  241. if (RightMargin>0)
  242. {
  243. ftcp+=RightMargin;
  244. }
  245. if (add>0)
  246. {
  247. ftcp+=add+RowAdd;
  248. }
  249. }
  250. delete [] RLEntries;
  251. RLEntries = NULL;
  252. if (BottomMargin>0)
  253. {
  254. ftcp += BottomMargin * (FullWidth+add);
  255. }
  256. }
  257. //SDL_UpdateRect(dest, x, y, FullWidth+add, FullHeight);
  258. return 0;
  259. }
  260. int CCreatureAnimation::framesInGroup(int group) const
  261. {
  262. int ret = 0; //number of frames in given group
  263. for(int g=0; g<SEntries.size(); ++g)
  264. {
  265. if(SEntries[g].group == group)
  266. ++ret;
  267. }
  268. return ret;
  269. }
  270. CCreatureAnimation::~CCreatureAnimation()
  271. {
  272. delete [] FDef;
  273. if (RWEntries)
  274. delete [] RWEntries;
  275. if (RLEntries)
  276. delete [] RLEntries;
  277. }
  278. void CCreatureAnimation::putPixel(SDL_Surface * dest, const int & ftcp, const BMPPalette & color, const unsigned char & palc, const bool & yellowBorder) const
  279. {
  280. if(palc!=0)
  281. {
  282. Uint8 * p = (Uint8*)dest->pixels + ftcp*3;
  283. if(palc > 7) //normal color
  284. {
  285. p[0] = color.B;
  286. p[1] = color.G;
  287. p[2] = color.R;
  288. }
  289. else if(yellowBorder && (palc == 6 || palc == 7)) //dark yellow border
  290. {
  291. p[0] = 0;
  292. p[1] = 0xff;
  293. p[2] = 0xff;
  294. }
  295. else if(yellowBorder && (palc == 5)) //yellow border
  296. {
  297. p[0] = color.B;
  298. p[1] = color.G;
  299. p[2] = color.R;
  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. }