CCreatureAnimation.cpp 9.4 KB

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