CCreatureAnimation.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. #include "StdInc.h"
  2. #include "CCreatureAnimation.h"
  3. #include "../../lib/CLodHandler.h"
  4. #include "../../lib/VCMI_Lib.h"
  5. #include "../../lib/vcmi_endian.h"
  6. #include "../SDL_Extensions.h"
  7. /*
  8. * CCreatureAnimation.cpp, part of VCMI engine
  9. *
  10. * Authors: listed in file AUTHORS in main folder
  11. *
  12. * License: GNU General Public License v2.0 or later
  13. * Full text of license available in license.txt file, in main folder
  14. *
  15. */
  16. CCreatureAnim::EAnimType CCreatureAnimation::getType() const
  17. {
  18. return type;
  19. }
  20. void CCreatureAnimation::setType(CCreatureAnim::EAnimType type)
  21. {
  22. assert(framesInGroup(type) > 0 && "Bad type for void CCreatureAnimation::setType(int type)!");
  23. this->type = type;
  24. internalFrame = 0;
  25. if(type!=-1)
  26. {
  27. curFrame = frameGroups[type][0];
  28. }
  29. else
  30. {
  31. if(curFrame>=frames)
  32. {
  33. curFrame = 0;
  34. }
  35. }
  36. }
  37. CCreatureAnimation::CCreatureAnimation(std::string name) : internalFrame(0), once(false)
  38. {
  39. FDef = spriteh->giveFile(name, FILE_ANIMATION); //load main file
  40. //init anim data
  41. int i,j, totalInBlock;
  42. defName=name;
  43. i = 0;
  44. DEFType = read_le_u32(FDef + i); i+=4;
  45. fullWidth = read_le_u32(FDef + i); i+=4;
  46. fullHeight = read_le_u32(FDef + i); i+=4;
  47. i=0xc;
  48. totalBlocks = read_le_u32(FDef + i); 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. std::vector<int> frameIDs;
  62. int group = read_le_u32(FDef + i); i+=4; //block ID
  63. totalInBlock = read_le_u32(FDef + i); i+=4;
  64. for (j=SEntries.size(); j<totalEntries+totalInBlock; j++)
  65. {
  66. SEntries.push_back(SEntry());
  67. SEntries[j].group = group;
  68. frameIDs.push_back(j);
  69. }
  70. /*int unknown2 = read_le_u32(FDef + i);*/ i+=4; //TODO use me
  71. /*int unknown3 = read_le_u32(FDef + i);*/ i+=4; //TODO use me
  72. i+=13*totalInBlock; //omitting names
  73. for (j=0; j<totalInBlock; j++)
  74. {
  75. SEntries[totalEntries+j].offset = read_le_u32(FDef + i); i+=4;
  76. }
  77. //totalEntries+=totalInBlock;
  78. for(int hh=0; hh<totalInBlock; ++hh)
  79. {
  80. ++totalEntries;
  81. }
  82. frameGroups[group] = frameIDs;
  83. }
  84. //init vars
  85. curFrame = 0;
  86. type = CCreatureAnim::WHOLE_ANIM;
  87. frames = totalEntries;
  88. }
  89. int CCreatureAnimation::nextFrameMiddle(SDL_Surface *dest, int x, int y, bool attacker, ui8 animCount, bool incrementFrame, bool yellowBorder, bool blueBorder, SDL_Rect * destRect)
  90. {
  91. return nextFrame(dest, x-fullWidth/2, y-fullHeight/2, attacker, animCount, incrementFrame, yellowBorder, blueBorder, destRect);
  92. }
  93. void CCreatureAnimation::incrementFrame()
  94. {
  95. if(type!=-1) //when a specific part of animation is played
  96. {
  97. ++internalFrame;
  98. if(internalFrame == frameGroups[type].size()) //rewind
  99. {
  100. internalFrame = 0;
  101. if(once) //playing animation once - return to standing animation
  102. {
  103. type = CCreatureAnim::HOLDING;
  104. once = false;
  105. curFrame = frameGroups[2][0];
  106. }
  107. else //
  108. {
  109. curFrame = frameGroups[type][0];
  110. }
  111. }
  112. curFrame = frameGroups[type][internalFrame];
  113. }
  114. else //when whole animation is played
  115. {
  116. ++curFrame;
  117. if(curFrame>=frames)
  118. curFrame = 0;
  119. }
  120. }
  121. int CCreatureAnimation::getFrame() const
  122. {
  123. return curFrame;
  124. }
  125. int CCreatureAnimation::getAnimationFrame() const
  126. {
  127. return internalFrame;
  128. }
  129. bool CCreatureAnimation::onFirstFrameInGroup()
  130. {
  131. return internalFrame == 0;
  132. }
  133. bool CCreatureAnimation::onLastFrameInGroup()
  134. {
  135. if(internalFrame == frameGroups[type].size() - 1)
  136. return true;
  137. return false;
  138. }
  139. void CCreatureAnimation::playOnce( CCreatureAnim::EAnimType type )
  140. {
  141. setType(type);
  142. once = true;
  143. }
  144. template<int bpp>
  145. int CCreatureAnimation::nextFrameT(SDL_Surface * dest, int x, int y, bool attacker, ui8 animCount, bool IncrementFrame /*= true*/, bool yellowBorder /*= false*/, bool blueBorder /*= false*/, SDL_Rect * destRect /*= NULL*/)
  146. {
  147. //increasing frame number
  148. int SIndex = curFrame;
  149. if (IncrementFrame)
  150. incrementFrame();
  151. #if 0
  152. long SpriteWidth, SpriteHeight, //sprite format
  153. LeftMargin, RightMargin, TopMargin,BottomMargin,
  154. i, FullHeight,
  155. #endif
  156. ui8 SegmentType, SegmentLength;
  157. ui32 i;
  158. i = SEntries[SIndex].offset;
  159. /*int prSize = read_le_u32(FDef + i);*/ i += 4; //TODO use me
  160. const ui32 defType2 = read_le_u32(FDef + i); i += 4;
  161. const ui32 FullWidth = read_le_u32(FDef + i); i += 4;
  162. const ui32 FullHeight = read_le_u32(FDef + i); i += 4;
  163. const ui32 SpriteWidth = read_le_u32(FDef + i); i += 4;
  164. const ui32 SpriteHeight = read_le_u32(FDef + i); i += 4;
  165. const int LeftMargin = read_le_u32(FDef + i); i += 4;
  166. const int TopMargin = read_le_u32(FDef + i); i += 4;
  167. const int RightMargin = FullWidth - SpriteWidth - LeftMargin;
  168. const int BottomMargin = FullHeight - SpriteHeight - TopMargin;
  169. if (defType2 == 1) //as it should be always in creature animations
  170. {
  171. const int BaseOffsetor = i;
  172. int ftcp = 0;
  173. if (TopMargin > 0)
  174. {
  175. ftcp += FullWidth * TopMargin;
  176. }
  177. ui32 *RLEntries = (ui32 *)(FDef + BaseOffsetor);
  178. for (int i = 0; i < SpriteHeight; i++)
  179. {
  180. int BaseOffset = BaseOffsetor + read_le_u32(RLEntries + i);
  181. int TotalRowLength; // length of read segment
  182. if (LeftMargin > 0)
  183. {
  184. ftcp += LeftMargin;
  185. }
  186. TotalRowLength = 0;
  187. // Note: Bug fixed (Rev 2115): The implementation of omitting lines was false.
  188. // We've to calculate several things so not showing/putting pixels should suffice.
  189. int yB = ftcp / FullWidth + y;
  190. do
  191. {
  192. SegmentType = FDef[BaseOffset++];
  193. SegmentLength = FDef[BaseOffset++];
  194. const int remainder = ftcp % FullWidth;
  195. int xB = (attacker ? remainder : FullWidth - remainder - 1) + x;
  196. const ui8 aCountMod = (animCount & 0x20) ? ((animCount & 0x1e) >> 1) << 4 : (0x0f - ((animCount & 0x1e) >> 1)) << 4;
  197. for (int k = 0; k <= SegmentLength; k++)
  198. {
  199. if(xB >= 0 && xB < dest->w && yB >= 0 && yB < dest->h)
  200. {
  201. if(!destRect || (destRect->x <= xB && destRect->x + destRect->w > xB && destRect->y <= yB && destRect->y + destRect->h > yB))
  202. {
  203. const ui8 colorNr = SegmentType == 0xff ? FDef[BaseOffset+k] : SegmentType;
  204. putPixel<bpp>(dest, xB, yB, palette[colorNr], colorNr, yellowBorder, blueBorder, aCountMod);
  205. }
  206. }
  207. ftcp++; //increment pos
  208. if(attacker)
  209. xB++;
  210. else
  211. xB--;
  212. if ( SegmentType == 0xFF && TotalRowLength+k+1 >= SpriteWidth )
  213. break;
  214. }
  215. if (SegmentType == 0xFF)
  216. {
  217. BaseOffset += SegmentLength+1;
  218. }
  219. TotalRowLength+=SegmentLength+1;
  220. } while(TotalRowLength < SpriteWidth);
  221. if (RightMargin > 0)
  222. {
  223. ftcp += RightMargin;
  224. }
  225. }
  226. if (BottomMargin > 0)
  227. {
  228. ftcp += BottomMargin * FullWidth;
  229. }
  230. }
  231. return 0;
  232. }
  233. int CCreatureAnimation::nextFrame(SDL_Surface *dest, int x, int y, bool attacker, ui8 animCount, bool IncrementFrame, bool yellowBorder, bool blueBorder, SDL_Rect * destRect)
  234. {
  235. switch(dest->format->BytesPerPixel)
  236. {
  237. case 2: return nextFrameT<2>(dest, x, y, attacker, animCount, IncrementFrame, yellowBorder, blueBorder, destRect);
  238. case 3: return nextFrameT<3>(dest, x, y, attacker, animCount, IncrementFrame, yellowBorder, blueBorder, destRect);
  239. case 4: return nextFrameT<4>(dest, x, y, attacker, animCount, IncrementFrame, yellowBorder, blueBorder, destRect);
  240. default:
  241. tlog1 << (int)dest->format->BitsPerPixel << " bpp is not supported!!!\n";
  242. return -1;
  243. }
  244. }
  245. int CCreatureAnimation::framesInGroup(CCreatureAnim::EAnimType group) const
  246. {
  247. if(frameGroups.find(group) == frameGroups.end())
  248. return 0;
  249. return frameGroups.find(group)->second.size();
  250. }
  251. CCreatureAnimation::~CCreatureAnimation()
  252. {
  253. delete [] FDef;
  254. }
  255. template<int bpp>
  256. inline void CCreatureAnimation::putPixel(
  257. SDL_Surface * dest,
  258. const int & ftcpX,
  259. const int & ftcpY,
  260. const BMPPalette & color,
  261. const ui8 & palc,
  262. const bool & yellowBorder,
  263. const bool & blueBorder,
  264. const ui8 & animCount
  265. ) const
  266. {
  267. if(palc!=0)
  268. {
  269. Uint8 * p = (Uint8*)dest->pixels + ftcpX*dest->format->BytesPerPixel + ftcpY*dest->pitch;
  270. if(palc > 7) //normal color
  271. {
  272. ColorPutter<bpp, 0>::PutColor(p, color.R, color.G, color.B);
  273. }
  274. else if((yellowBorder || blueBorder) && (palc == 6 || palc == 7)) //selection highlight
  275. {
  276. if(blueBorder)
  277. ColorPutter<bpp, 0>::PutColor(p, 0, 0x0f + animCount, 0x0f + animCount);
  278. else
  279. ColorPutter<bpp, 0>::PutColor(p, 0x0f + animCount, 0x0f + animCount, 0);
  280. }
  281. else if (palc == 5) //selection highlight or transparent
  282. {
  283. if(blueBorder)
  284. ColorPutter<bpp, 0>::PutColor(p, color.B, color.G - 0xf0 + animCount, color.R - 0xf0 + animCount); //shouldn't it be reversed? its bgr instead of rgb
  285. else if (yellowBorder)
  286. ColorPutter<bpp, 0>::PutColor(p, color.R - 0xf0 + animCount, color.G - 0xf0 + animCount, color.B);
  287. }
  288. else //shadow
  289. {
  290. //determining transparency value, 255 or 0 should be already filtered
  291. static Uint16 colToAlpha[8] = {255,192,128,128,128,255,128,192};
  292. Uint16 alpha = colToAlpha[palc];
  293. if(bpp != 3 && bpp != 4)
  294. {
  295. ColorPutter<bpp, 0>::PutColor(p, 0, 0, 0, alpha);
  296. }
  297. else
  298. {
  299. p[0] = (p[0] * alpha)>>8;
  300. p[1] = (p[1] * alpha)>>8;
  301. p[2] = (p[2] * alpha)>>8;
  302. }
  303. }
  304. }
  305. }