2008-06-07 20:16:52 +03:00
|
|
|
#include "CCreatureAnimation.h"
|
|
|
|
#include "../hch/CLodHandler.h"
|
2009-05-09 19:18:27 +03:00
|
|
|
#include "../lib/VCMI_Lib.h"
|
2009-09-16 13:09:08 +03:00
|
|
|
#include <assert.h>
|
2009-04-15 17:03:31 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* CCreatureAnimation.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2008-06-07 20:16:52 +03:00
|
|
|
int CCreatureAnimation::getType() const
|
|
|
|
{
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCreatureAnimation::setType(int type)
|
|
|
|
{
|
2009-09-16 13:09:08 +03:00
|
|
|
assert(framesInGroup(type) > 0 && "Bad type for void CCreatureAnimation::setType(int type)!");
|
2008-06-07 20:16:52 +03:00
|
|
|
this->type = type;
|
2009-02-20 15:06:20 +02:00
|
|
|
internalFrame = 0;
|
2008-06-07 20:16:52 +03:00
|
|
|
if(type!=-1)
|
|
|
|
{
|
2009-02-20 15:06:20 +02:00
|
|
|
curFrame = frameGroups[type][0];
|
2008-06-07 20:16:52 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-12-23 15:59:03 +02:00
|
|
|
if(curFrame>=frames)
|
|
|
|
{
|
2008-06-07 20:16:52 +03:00
|
|
|
curFrame = 0;
|
2008-12-23 15:59:03 +02:00
|
|
|
}
|
2008-06-07 20:16:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-18 15:50:00 +03:00
|
|
|
CCreatureAnimation::CCreatureAnimation(std::string name) : internalFrame(0), once(false)
|
2008-06-07 20:16:52 +03:00
|
|
|
{
|
2009-05-09 19:18:27 +03:00
|
|
|
FDef = spriteh->giveFile(name); //load main file
|
2008-06-07 20:16:52 +03:00
|
|
|
|
|
|
|
//init anim data
|
|
|
|
int i,j, totalInBlock;
|
|
|
|
char Buffer[13];
|
|
|
|
defName=name;
|
|
|
|
i = 0;
|
|
|
|
DEFType = readNormalNr(i,4); i+=4;
|
|
|
|
fullWidth = readNormalNr(i,4); i+=4;
|
|
|
|
fullHeight = readNormalNr(i,4); i+=4;
|
|
|
|
i=0xc;
|
|
|
|
totalBlocks = readNormalNr(i,4); i+=4;
|
|
|
|
|
|
|
|
i=0x10;
|
|
|
|
for (int it=0;it<256;it++)
|
|
|
|
{
|
|
|
|
palette[it].R = FDef[i++];
|
|
|
|
palette[it].G = FDef[i++];
|
|
|
|
palette[it].B = FDef[i++];
|
|
|
|
palette[it].F = 0;
|
|
|
|
}
|
|
|
|
i=0x310;
|
|
|
|
totalEntries=0;
|
|
|
|
for (int z=0; z<totalBlocks; z++)
|
|
|
|
{
|
2009-02-20 15:06:20 +02:00
|
|
|
std::vector<int> frameIDs;
|
2008-08-07 03:38:44 +03:00
|
|
|
int group = readNormalNr(i,4); i+=4; //block ID
|
2008-06-07 20:16:52 +03:00
|
|
|
totalInBlock = readNormalNr(i,4); i+=4;
|
|
|
|
for (j=SEntries.size(); j<totalEntries+totalInBlock; j++)
|
2008-08-07 03:38:44 +03:00
|
|
|
{
|
2008-06-07 20:16:52 +03:00
|
|
|
SEntries.push_back(SEntry());
|
2008-08-07 03:38:44 +03:00
|
|
|
SEntries[j].group = group;
|
2009-02-20 15:06:20 +02:00
|
|
|
frameIDs.push_back(j);
|
2008-08-07 03:38:44 +03:00
|
|
|
}
|
2008-12-21 21:17:35 +02:00
|
|
|
int unknown2 = readNormalNr(i,4); i+=4; //TODO use me
|
|
|
|
int unknown3 = readNormalNr(i,4); i+=4; //TODO use me
|
2009-02-20 15:06:20 +02:00
|
|
|
i+=13*totalInBlock; //ommiting names
|
2008-06-07 20:16:52 +03:00
|
|
|
for (j=0; j<totalInBlock; j++)
|
|
|
|
{
|
2009-02-20 15:06:20 +02:00
|
|
|
SEntries[totalEntries+j].offset = readNormalNr(i,4); i+=4;
|
2008-06-07 20:16:52 +03:00
|
|
|
}
|
|
|
|
//totalEntries+=totalInBlock;
|
|
|
|
for(int hh=0; hh<totalInBlock; ++hh)
|
|
|
|
{
|
|
|
|
++totalEntries;
|
|
|
|
}
|
2009-02-20 15:06:20 +02:00
|
|
|
frameGroups[group] = frameIDs;
|
2008-06-07 20:16:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//init vars
|
|
|
|
curFrame = 0;
|
|
|
|
type = -1;
|
|
|
|
frames = totalEntries;
|
|
|
|
}
|
|
|
|
|
2008-11-12 20:59:33 +02:00
|
|
|
int CCreatureAnimation::readNormalNr (int pos, int bytCon, unsigned char * str) const
|
2008-06-07 20:16:52 +03:00
|
|
|
{
|
|
|
|
int ret=0;
|
|
|
|
int amp=1;
|
|
|
|
if (str)
|
|
|
|
{
|
|
|
|
for (int i=0; i<bytCon; i++)
|
|
|
|
{
|
|
|
|
ret+=str[pos+i]*amp;
|
2008-11-12 20:59:33 +02:00
|
|
|
amp<<=8; //amp*=256;
|
2008-06-07 20:16:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (int i=0; i<bytCon; i++)
|
|
|
|
{
|
|
|
|
ret+=FDef[pos+i]*amp;
|
2008-11-12 20:59:33 +02:00
|
|
|
amp<<=8; //amp*=256;
|
2008-06-07 20:16:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2009-02-21 17:43:28 +02:00
|
|
|
int CCreatureAnimation::nextFrameMiddle(SDL_Surface *dest, int x, int y, bool attacker, unsigned char animCount, bool incrementFrame, bool yellowBorder, bool blueBorder, SDL_Rect * destRect)
|
2008-06-07 20:16:52 +03:00
|
|
|
{
|
2009-02-21 17:43:28 +02:00
|
|
|
return nextFrame(dest, x-fullWidth/2, y-fullHeight/2, attacker, animCount, incrementFrame, yellowBorder, blueBorder, destRect);
|
2008-06-07 20:16:52 +03:00
|
|
|
}
|
2008-08-07 03:38:44 +03:00
|
|
|
void CCreatureAnimation::incrementFrame()
|
2008-06-07 20:16:52 +03:00
|
|
|
{
|
2009-03-12 20:06:02 +02:00
|
|
|
if(type!=-1) //when a specific part of animation is played
|
2008-06-07 20:16:52 +03:00
|
|
|
{
|
2009-03-19 21:04:46 +02:00
|
|
|
++internalFrame;
|
2009-02-20 15:06:20 +02:00
|
|
|
if(internalFrame == frameGroups[type].size()) //rewind
|
2008-06-07 20:16:52 +03:00
|
|
|
{
|
2009-03-19 21:04:46 +02:00
|
|
|
internalFrame = 0;
|
2009-03-12 20:06:02 +02:00
|
|
|
if(once) //playing animation once - return to standing animation
|
2009-02-21 17:43:28 +02:00
|
|
|
{
|
|
|
|
type = 2;
|
|
|
|
once = false;
|
|
|
|
curFrame = frameGroups[2][0];
|
|
|
|
}
|
2009-03-12 20:06:02 +02:00
|
|
|
else //
|
2009-02-21 17:43:28 +02:00
|
|
|
{
|
|
|
|
curFrame = frameGroups[type][0];
|
|
|
|
}
|
2008-06-07 20:16:52 +03:00
|
|
|
}
|
2009-03-19 21:04:46 +02:00
|
|
|
curFrame = frameGroups[type][internalFrame];
|
2008-06-07 20:16:52 +03:00
|
|
|
}
|
2009-03-12 20:06:02 +02:00
|
|
|
else //when whole animation is played
|
2008-06-07 20:16:52 +03:00
|
|
|
{
|
2009-03-12 20:06:02 +02:00
|
|
|
++curFrame;
|
2008-08-07 03:38:44 +03:00
|
|
|
if(curFrame>=frames)
|
|
|
|
curFrame = 0;
|
2008-06-07 20:16:52 +03:00
|
|
|
}
|
2008-08-07 03:38:44 +03:00
|
|
|
}
|
2008-09-24 19:25:14 +03:00
|
|
|
|
|
|
|
int CCreatureAnimation::getFrame() const
|
|
|
|
{
|
|
|
|
return curFrame;
|
|
|
|
}
|
|
|
|
|
2009-03-05 19:57:26 +02:00
|
|
|
bool CCreatureAnimation::onLastFrameInGroup()
|
|
|
|
{
|
|
|
|
if(internalFrame == frameGroups[type].size() - 1)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-02-21 17:43:28 +02:00
|
|
|
void CCreatureAnimation::playOnce(int type)
|
|
|
|
{
|
|
|
|
setType(type);
|
|
|
|
once = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CCreatureAnimation::nextFrame(SDL_Surface *dest, int x, int y, bool attacker, unsigned char animCount, bool IncrementFrame, bool yellowBorder, bool blueBorder, SDL_Rect * destRect)
|
2008-08-07 03:38:44 +03:00
|
|
|
{
|
|
|
|
if(dest->format->BytesPerPixel<3)
|
|
|
|
return -1; //not enough depth
|
|
|
|
|
|
|
|
//increasing frame numer
|
|
|
|
int SIndex = curFrame;
|
|
|
|
if(IncrementFrame)
|
|
|
|
incrementFrame();
|
2008-06-07 20:16:52 +03:00
|
|
|
//frame number increased
|
|
|
|
|
|
|
|
long BaseOffset,
|
|
|
|
SpriteWidth, SpriteHeight, //sprite format
|
|
|
|
LeftMargin, RightMargin, TopMargin,BottomMargin,
|
2008-09-26 17:16:01 +03:00
|
|
|
i, FullHeight,FullWidth,
|
|
|
|
TotalRowLength; // length of read segment
|
2008-06-07 20:16:52 +03:00
|
|
|
unsigned char SegmentType, SegmentLength;
|
|
|
|
|
|
|
|
i=BaseOffset=SEntries[SIndex].offset;
|
2008-12-21 21:17:35 +02:00
|
|
|
int prSize=readNormalNr(i,4,FDef);i+=4;//TODO use me
|
2008-06-07 20:16:52 +03:00
|
|
|
int defType2 = readNormalNr(i,4,FDef);i+=4;
|
|
|
|
FullWidth = readNormalNr(i,4,FDef);i+=4;
|
|
|
|
FullHeight = readNormalNr(i,4,FDef);i+=4;
|
|
|
|
SpriteWidth = readNormalNr(i,4,FDef);i+=4;
|
|
|
|
SpriteHeight = readNormalNr(i,4,FDef);i+=4;
|
|
|
|
LeftMargin = readNormalNr(i,4,FDef);i+=4;
|
|
|
|
TopMargin = readNormalNr(i,4,FDef);i+=4;
|
|
|
|
RightMargin = FullWidth - SpriteWidth - LeftMargin;
|
|
|
|
BottomMargin = FullHeight - SpriteHeight - TopMargin;
|
|
|
|
|
|
|
|
int BaseOffsetor = BaseOffset = i;
|
|
|
|
|
|
|
|
int ftcp = 0;
|
|
|
|
|
|
|
|
if (defType2==1) //as it should be allways in creature animations
|
|
|
|
{
|
|
|
|
if (TopMargin>0)
|
|
|
|
{
|
2008-11-12 20:59:33 +02:00
|
|
|
ftcp+=FullWidth * TopMargin;
|
2008-06-07 20:16:52 +03:00
|
|
|
}
|
2009-09-18 15:50:00 +03:00
|
|
|
int * RLEntries = (int*)(FDef+BaseOffset);
|
2008-11-12 20:59:33 +02:00
|
|
|
BaseOffset += sizeof(int) * SpriteHeight;
|
2008-06-07 20:16:52 +03:00
|
|
|
for (int i=0;i<SpriteHeight;i++)
|
|
|
|
{
|
|
|
|
BaseOffset=BaseOffsetor+RLEntries[i];
|
|
|
|
if (LeftMargin>0)
|
|
|
|
{
|
|
|
|
ftcp+=LeftMargin;
|
|
|
|
}
|
|
|
|
TotalRowLength=0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
SegmentType=FDef[BaseOffset++];
|
|
|
|
SegmentLength=FDef[BaseOffset++];
|
2009-02-21 17:43:28 +02:00
|
|
|
unsigned char aCountMod = (animCount & 0x20) ? ((animCount & 0x1e)>>1)<<4 : 0x0f - ((animCount & 0x1e)>>1)<<4;
|
2008-06-07 20:16:52 +03:00
|
|
|
if (SegmentType==0xFF)
|
|
|
|
{
|
|
|
|
for (int k=0;k<=SegmentLength;k++)
|
|
|
|
{
|
2008-09-26 17:16:01 +03:00
|
|
|
int xB = (attacker ? ftcp%FullWidth : FullWidth - ftcp%FullWidth - 1) + x;
|
|
|
|
int yB = ftcp/FullWidth + y;
|
2008-06-07 20:16:52 +03:00
|
|
|
if(xB>=0 && yB>=0 && xB<dest->w && yB<dest->h)
|
|
|
|
{
|
|
|
|
if(!destRect || (destRect->x <= xB && destRect->x + destRect->w > xB && destRect->y <= yB && destRect->y + destRect->h > yB))
|
2009-02-21 17:43:28 +02:00
|
|
|
putPixel(dest, xB + yB*dest->w, palette[FDef[BaseOffset+k]], FDef[BaseOffset+k], yellowBorder, blueBorder, aCountMod);
|
2008-06-07 20:16:52 +03:00
|
|
|
}
|
|
|
|
ftcp++; //increment pos
|
|
|
|
if ((TotalRowLength+k+1)>=SpriteWidth)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
BaseOffset+=SegmentLength+1;////
|
|
|
|
TotalRowLength+=SegmentLength+1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (int k=0;k<SegmentLength+1;k++)
|
|
|
|
{
|
2008-09-26 17:16:01 +03:00
|
|
|
int xB = (attacker ? ftcp%FullWidth : FullWidth - ftcp%FullWidth - 1) + x;
|
|
|
|
int yB = ftcp/FullWidth + y;
|
2008-06-07 20:16:52 +03:00
|
|
|
if(xB>=0 && yB>=0 && xB<dest->w && yB<dest->h)
|
|
|
|
{
|
|
|
|
if(!destRect || (destRect->x <= xB && destRect->x + destRect->w > xB && destRect->y <= yB && destRect->y + destRect->h > yB))
|
2009-02-21 17:43:28 +02:00
|
|
|
putPixel(dest, xB + yB*dest->w, palette[SegmentType], SegmentType, yellowBorder, blueBorder, aCountMod);
|
2008-06-07 20:16:52 +03:00
|
|
|
}
|
|
|
|
ftcp++; //increment pos
|
|
|
|
}
|
|
|
|
TotalRowLength+=SegmentLength+1;
|
|
|
|
}
|
|
|
|
}while(TotalRowLength<SpriteWidth);
|
|
|
|
if (RightMargin>0)
|
|
|
|
{
|
|
|
|
ftcp+=RightMargin;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (BottomMargin>0)
|
|
|
|
{
|
2008-09-26 17:16:01 +03:00
|
|
|
ftcp += BottomMargin * FullWidth;
|
2008-06-07 20:16:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CCreatureAnimation::framesInGroup(int group) const
|
|
|
|
{
|
2009-02-20 15:06:20 +02:00
|
|
|
if(frameGroups.find(group) == frameGroups.end())
|
|
|
|
return 0;
|
|
|
|
return frameGroups.find(group)->second.size();
|
2008-06-07 20:16:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CCreatureAnimation::~CCreatureAnimation()
|
|
|
|
{
|
|
|
|
delete [] FDef;
|
|
|
|
}
|
|
|
|
|
2008-12-21 21:17:35 +02:00
|
|
|
inline void CCreatureAnimation::putPixel(
|
2009-08-01 15:54:05 +03:00
|
|
|
SDL_Surface * dest,
|
|
|
|
const int & ftcp,
|
|
|
|
const BMPPalette & color,
|
|
|
|
const unsigned char & palc,
|
|
|
|
const bool & yellowBorder,
|
|
|
|
const bool & blueBorder,
|
|
|
|
const unsigned char & animCount
|
2009-02-20 15:06:20 +02:00
|
|
|
) const
|
|
|
|
{
|
2008-12-23 15:59:03 +02:00
|
|
|
if(palc!=0)
|
|
|
|
{
|
2009-05-17 06:34:41 +03:00
|
|
|
Uint8 * p = (Uint8*)dest->pixels + ftcp*dest->format->BytesPerPixel;
|
2008-06-07 20:16:52 +03:00
|
|
|
if(palc > 7) //normal color
|
|
|
|
{
|
|
|
|
p[0] = color.B;
|
|
|
|
p[1] = color.G;
|
|
|
|
p[2] = color.R;
|
|
|
|
}
|
2009-02-21 17:43:28 +02:00
|
|
|
else if((yellowBorder || blueBorder) && (palc == 6 || palc == 7)) //dark yellow border
|
2008-06-07 20:16:52 +03:00
|
|
|
{
|
2009-02-21 17:43:28 +02:00
|
|
|
if(blueBorder)
|
|
|
|
{
|
|
|
|
p[0] = 0x0f + animCount;
|
|
|
|
p[1] = 0x0f + animCount;
|
|
|
|
p[2] = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
p[0] = 0;
|
|
|
|
p[1] = 0x0f + animCount;
|
|
|
|
p[2] = 0x0f + animCount;
|
|
|
|
}
|
2008-06-07 20:16:52 +03:00
|
|
|
}
|
2009-02-21 17:43:28 +02:00
|
|
|
else if((yellowBorder || blueBorder) && (palc == 5)) //yellow border
|
2008-06-07 20:16:52 +03:00
|
|
|
{
|
2009-02-21 17:43:28 +02:00
|
|
|
if(blueBorder)
|
|
|
|
{
|
|
|
|
p[0] = color.R - 0xf0 + animCount;
|
|
|
|
p[1] = color.G - 0xf0 + animCount;
|
|
|
|
p[2] = color.B;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
p[0] = color.B;
|
|
|
|
p[1] = color.G - 0xf0 + animCount;
|
|
|
|
p[2] = color.R - 0xf0 + animCount;
|
|
|
|
}
|
2008-06-07 20:16:52 +03:00
|
|
|
}
|
|
|
|
else if(palc < 5) //shadow
|
|
|
|
{
|
|
|
|
Uint16 alpha;
|
|
|
|
switch(color.G)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
alpha = 128;
|
|
|
|
break;
|
|
|
|
case 50:
|
|
|
|
alpha = 50+32;
|
|
|
|
break;
|
|
|
|
case 100:
|
|
|
|
alpha = 100+64;
|
|
|
|
break;
|
|
|
|
case 125:
|
|
|
|
alpha = 125+64;
|
|
|
|
break;
|
|
|
|
case 128:
|
|
|
|
alpha = 128+64;
|
|
|
|
break;
|
|
|
|
case 150:
|
|
|
|
alpha = 150+64;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
alpha = 255;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
//alpha counted
|
|
|
|
p[0] = (p[0] * alpha)>>8;
|
|
|
|
p[1] = (p[1] * alpha)>>8;
|
|
|
|
p[2] = (p[2] * alpha)>>8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|