CCreatureAnimation.cpp 9.4 KB

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