2
0

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