| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- #include "CCreatureAnimation.h"
- #include "../hch/CLodHandler.h"
- int CCreatureAnimation::getType() const
- {
- return type;
- }
- void CCreatureAnimation::setType(int type)
- {
- this->type = type;
- internalFrame = 0;
- if(type!=-1)
- {
- curFrame = frameGroups[type][0];
- }
- else
- {
- if(curFrame>=frames)
- {
- curFrame = 0;
- }
- }
- }
- CCreatureAnimation::CCreatureAnimation(std::string name) : RLEntries(NULL), internalFrame(0), once(false)
- {
- FDef = CDefHandler::Spriteh->giveFile(name); //load main file
- //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++)
- {
- std::vector<int> frameIDs;
- int group = readNormalNr(i,4); i+=4; //block ID
- totalInBlock = readNormalNr(i,4); i+=4;
- for (j=SEntries.size(); j<totalEntries+totalInBlock; j++)
- {
- SEntries.push_back(SEntry());
- SEntries[j].group = group;
- frameIDs.push_back(j);
- }
- int unknown2 = readNormalNr(i,4); i+=4; //TODO use me
- int unknown3 = readNormalNr(i,4); i+=4; //TODO use me
- i+=13*totalInBlock; //ommiting names
- for (j=0; j<totalInBlock; j++)
- {
- SEntries[totalEntries+j].offset = readNormalNr(i,4); i+=4;
- }
- //totalEntries+=totalInBlock;
- for(int hh=0; hh<totalInBlock; ++hh)
- {
- ++totalEntries;
- }
- frameGroups[group] = frameIDs;
- }
- //init vars
- curFrame = 0;
- type = -1;
- frames = totalEntries;
- RLEntries = new int[fullHeight];
- }
- int CCreatureAnimation::readNormalNr (int pos, int bytCon, unsigned char * str) const
- {
- int ret=0;
- int amp=1;
- if (str)
- {
- for (int i=0; i<bytCon; i++)
- {
- ret+=str[pos+i]*amp;
- amp<<=8; //amp*=256;
- }
- }
- else
- {
- for (int i=0; i<bytCon; i++)
- {
- ret+=FDef[pos+i]*amp;
- amp<<=8; //amp*=256;
- }
- }
- return ret;
- }
- int CCreatureAnimation::nextFrameMiddle(SDL_Surface *dest, int x, int y, bool attacker, unsigned char animCount, bool incrementFrame, bool yellowBorder, bool blueBorder, SDL_Rect * destRect)
- {
- return nextFrame(dest, x-fullWidth/2, y-fullHeight/2, attacker, animCount, incrementFrame, yellowBorder, blueBorder, destRect);
- }
- void CCreatureAnimation::incrementFrame()
- {
- curFrame = frameGroups[type][(internalFrame++)%frameGroups[type].size()];
- if(type!=-1)
- {
- if(internalFrame == frameGroups[type].size()) //rewind
- {
- if(once)
- {
- type = 2;
- once = false;
- curFrame = frameGroups[2][0];
- }
- else
- {
- curFrame = frameGroups[type][0];
- }
- }
- }
- else
- {
- if(curFrame>=frames)
- curFrame = 0;
- }
- }
- int CCreatureAnimation::getFrame() const
- {
- return curFrame;
- }
- bool CCreatureAnimation::onLastFrameInGroup()
- {
- if(internalFrame == frameGroups[type].size() - 1)
- return true;
- return false;
- }
- 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)
- {
- if(dest->format->BytesPerPixel<3)
- return -1; //not enough depth
- //increasing frame numer
- int SIndex = curFrame;
- if(IncrementFrame)
- incrementFrame();
- //frame number increased
- long BaseOffset,
- SpriteWidth, SpriteHeight, //sprite format
- LeftMargin, RightMargin, TopMargin,BottomMargin,
- i, FullHeight,FullWidth,
- TotalRowLength; // length of read segment
- unsigned char SegmentType, SegmentLength;
-
- i=BaseOffset=SEntries[SIndex].offset;
- int prSize=readNormalNr(i,4,FDef);i+=4;//TODO use me
- 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)
- {
- ftcp+=FullWidth * TopMargin;
- }
- memcpy(RLEntries, FDef+BaseOffset, SpriteHeight*sizeof(int));
- BaseOffset += sizeof(int) * SpriteHeight;
- 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++];
- unsigned char aCountMod = (animCount & 0x20) ? ((animCount & 0x1e)>>1)<<4 : 0x0f - ((animCount & 0x1e)>>1)<<4;
- if (SegmentType==0xFF)
- {
- for (int k=0;k<=SegmentLength;k++)
- {
- int xB = (attacker ? ftcp%FullWidth : FullWidth - ftcp%FullWidth - 1) + x;
- int yB = ftcp/FullWidth + y;
- 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))
- putPixel(dest, xB + yB*dest->w, palette[FDef[BaseOffset+k]], FDef[BaseOffset+k], yellowBorder, blueBorder, aCountMod);
- }
- ftcp++; //increment pos
- if ((TotalRowLength+k+1)>=SpriteWidth)
- break;
- }
- BaseOffset+=SegmentLength+1;////
- TotalRowLength+=SegmentLength+1;
- }
- else
- {
- for (int k=0;k<SegmentLength+1;k++)
- {
- int xB = (attacker ? ftcp%FullWidth : FullWidth - ftcp%FullWidth - 1) + x;
- int yB = ftcp/FullWidth + y;
- 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))
- putPixel(dest, xB + yB*dest->w, palette[SegmentType], SegmentType, yellowBorder, blueBorder, aCountMod);
- }
- ftcp++; //increment pos
- }
- TotalRowLength+=SegmentLength+1;
- }
- }while(TotalRowLength<SpriteWidth);
- if (RightMargin>0)
- {
- ftcp+=RightMargin;
- }
- }
- if (BottomMargin>0)
- {
- ftcp += BottomMargin * FullWidth;
- }
- }
- return 0;
- }
- int CCreatureAnimation::framesInGroup(int group) const
- {
- if(frameGroups.find(group) == frameGroups.end())
- return 0;
- return frameGroups.find(group)->second.size();
- }
- CCreatureAnimation::~CCreatureAnimation()
- {
- delete [] FDef;
- if (RLEntries)
- delete [] RLEntries;
- }
- inline void CCreatureAnimation::putPixel(
- SDL_Surface * dest,
- const int & ftcp,
- const BMPPalette & color,
- const unsigned char & palc,
- const bool & yellowBorder,
- const bool & blueBorder,
- const unsigned char & animCount
- ) const
- {
- if(palc!=0)
- {
- Uint8 * p = (Uint8*)dest->pixels + ftcp*3;
- if(palc > 7) //normal color
- {
- p[0] = color.B;
- p[1] = color.G;
- p[2] = color.R;
- }
- else if((yellowBorder || blueBorder) && (palc == 6 || palc == 7)) //dark yellow border
- {
- 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;
- }
- }
- else if((yellowBorder || blueBorder) && (palc == 5)) //yellow border
- {
- 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;
- }
- }
- 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;
- }
- }
- }
|