CCreatureAnimation.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. {
  26. curFrame = j;
  27. }
  28. }
  29. }
  30. else
  31. {
  32. if(curFrame>=frames)
  33. {
  34. curFrame = 0;
  35. }
  36. }
  37. }
  38. CCreatureAnimation::CCreatureAnimation(std::string name) : RLEntries(NULL)
  39. {
  40. FDef = CDefHandler::Spriteh->giveFile(name); //load main file
  41. //init anim data
  42. int i,j, totalInBlock;
  43. char Buffer[13];
  44. defName=name;
  45. i = 0;
  46. DEFType = readNormalNr(i,4); i+=4;
  47. fullWidth = readNormalNr(i,4); i+=4;
  48. fullHeight = readNormalNr(i,4); i+=4;
  49. i=0xc;
  50. totalBlocks = readNormalNr(i,4); i+=4;
  51. i=0x10;
  52. for (int it=0;it<256;it++)
  53. {
  54. palette[it].R = FDef[i++];
  55. palette[it].G = FDef[i++];
  56. palette[it].B = FDef[i++];
  57. palette[it].F = 0;
  58. }
  59. i=0x310;
  60. totalEntries=0;
  61. for (int z=0; z<totalBlocks; z++)
  62. {
  63. int group = readNormalNr(i,4); i+=4; //block ID
  64. totalInBlock = readNormalNr(i,4); i+=4;
  65. for (j=SEntries.size(); j<totalEntries+totalInBlock; j++)
  66. {
  67. SEntries.push_back(SEntry());
  68. SEntries[j].group = group;
  69. }
  70. int unknown2 = readNormalNr(i,4); i+=4; //TODO use me
  71. int unknown3 = readNormalNr(i,4); i+=4; //TODO use me
  72. for (j=0; j<totalInBlock; j++)
  73. {
  74. for (int k=0;k<13;k++) Buffer[k]=FDef[i+k];
  75. i+=13;
  76. SEntries[totalEntries+j].name=Buffer;
  77. }
  78. for (j=0; j<totalInBlock; j++)
  79. {
  80. SEntries[totalEntries+j].offset = readNormalNr(i,4);
  81. int unknown4 = readNormalNr(i,4); i+=4; //TODO use me
  82. }
  83. //totalEntries+=totalInBlock;
  84. for(int hh=0; hh<totalInBlock; ++hh)
  85. {
  86. ++totalEntries;
  87. }
  88. }
  89. for(j=0; j<SEntries.size(); ++j)
  90. {
  91. SEntries[j].name = SEntries[j].name.substr(0, SEntries[j].name.find('.')+4);
  92. }
  93. //init vars
  94. curFrame = 0;
  95. type = -1;
  96. frames = totalEntries;
  97. RLEntries = new int[fullHeight];
  98. }
  99. int CCreatureAnimation::readNormalNr (int pos, int bytCon, unsigned char * str) const
  100. {
  101. int ret=0;
  102. int amp=1;
  103. if (str)
  104. {
  105. for (int i=0; i<bytCon; i++)
  106. {
  107. ret+=str[pos+i]*amp;
  108. amp<<=8; //amp*=256;
  109. }
  110. }
  111. else
  112. {
  113. for (int i=0; i<bytCon; i++)
  114. {
  115. ret+=FDef[pos+i]*amp;
  116. amp<<=8; //amp*=256;
  117. }
  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(size_t 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;//TODO use me
  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. ftcp+=FullWidth * TopMargin;
  188. }
  189. memcpy(RLEntries, FDef+BaseOffset, SpriteHeight*sizeof(int));
  190. BaseOffset += sizeof(int) * SpriteHeight;
  191. for (int i=0;i<SpriteHeight;i++)
  192. {
  193. BaseOffset=BaseOffsetor+RLEntries[i];
  194. if (LeftMargin>0)
  195. {
  196. ftcp+=LeftMargin;
  197. }
  198. TotalRowLength=0;
  199. do
  200. {
  201. SegmentType=FDef[BaseOffset++];
  202. SegmentLength=FDef[BaseOffset++];
  203. if (SegmentType==0xFF)
  204. {
  205. for (int k=0;k<=SegmentLength;k++)
  206. {
  207. int xB = (attacker ? ftcp%FullWidth : FullWidth - ftcp%FullWidth - 1) + x;
  208. int yB = ftcp/FullWidth + y;
  209. if(xB>=0 && yB>=0 && xB<dest->w && yB<dest->h)
  210. {
  211. if(!destRect || (destRect->x <= xB && destRect->x + destRect->w > xB && destRect->y <= yB && destRect->y + destRect->h > yB))
  212. putPixel(dest, xB + yB*dest->w, palette[FDef[BaseOffset+k]], FDef[BaseOffset+k], yellowBorder);
  213. }
  214. ftcp++; //increment pos
  215. if ((TotalRowLength+k+1)>=SpriteWidth)
  216. break;
  217. }
  218. BaseOffset+=SegmentLength+1;////
  219. TotalRowLength+=SegmentLength+1;
  220. }
  221. else
  222. {
  223. for (int k=0;k<SegmentLength+1;k++)
  224. {
  225. int xB = (attacker ? ftcp%FullWidth : FullWidth - ftcp%FullWidth - 1) + x;
  226. int yB = ftcp/FullWidth + y;
  227. if(xB>=0 && yB>=0 && xB<dest->w && yB<dest->h)
  228. {
  229. if(!destRect || (destRect->x <= xB && destRect->x + destRect->w > xB && destRect->y <= yB && destRect->y + destRect->h > yB))
  230. putPixel(dest, xB + yB*dest->w, palette[SegmentType], SegmentType, yellowBorder);
  231. }
  232. ftcp++; //increment pos
  233. }
  234. TotalRowLength+=SegmentLength+1;
  235. }
  236. }while(TotalRowLength<SpriteWidth);
  237. if (RightMargin>0)
  238. {
  239. ftcp+=RightMargin;
  240. }
  241. }
  242. if (BottomMargin>0)
  243. {
  244. ftcp += BottomMargin * FullWidth;
  245. }
  246. }
  247. return 0;
  248. }
  249. int CCreatureAnimation::framesInGroup(int group) const
  250. {
  251. int ret = 0; //number of frames in given group
  252. for(size_t g=0; g<SEntries.size(); ++g)
  253. {
  254. if(SEntries[g].group == group)
  255. ++ret;
  256. }
  257. return ret;
  258. }
  259. CCreatureAnimation::~CCreatureAnimation()
  260. {
  261. delete [] FDef;
  262. if (RLEntries)
  263. delete [] RLEntries;
  264. }
  265. inline void CCreatureAnimation::putPixel(
  266. SDL_Surface * dest,
  267. const int & ftcp,
  268. const BMPPalette & color,
  269. const unsigned char & palc,
  270. const bool & yellowBorder
  271. ) const {
  272. if(palc!=0)
  273. {
  274. Uint8 * p = (Uint8*)dest->pixels + ftcp*3;
  275. if(palc > 7) //normal color
  276. {
  277. p[0] = color.B;
  278. p[1] = color.G;
  279. p[2] = color.R;
  280. }
  281. else if(yellowBorder && (palc == 6 || palc == 7)) //dark yellow border
  282. {
  283. p[0] = 0;
  284. p[1] = 0xff;
  285. p[2] = 0xff;
  286. }
  287. else if(yellowBorder && (palc == 5)) //yellow border
  288. {
  289. p[0] = color.B;
  290. p[1] = color.G;
  291. p[2] = color.R;
  292. }
  293. else if(palc < 5) //shadow
  294. {
  295. Uint16 alpha;
  296. switch(color.G)
  297. {
  298. case 0:
  299. alpha = 128;
  300. break;
  301. case 50:
  302. alpha = 50+32;
  303. break;
  304. case 100:
  305. alpha = 100+64;
  306. break;
  307. case 125:
  308. alpha = 125+64;
  309. break;
  310. case 128:
  311. alpha = 128+64;
  312. break;
  313. case 150:
  314. alpha = 150+64;
  315. break;
  316. default:
  317. alpha = 255;
  318. break;
  319. }
  320. //alpha counted
  321. p[0] = (p[0] * alpha)>>8;
  322. p[1] = (p[1] * alpha)>>8;
  323. p[2] = (p[2] * alpha)>>8;
  324. }
  325. }
  326. }