2
0

CCreatureAnimation.cpp 7.8 KB

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