1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-24 08:32:34 +02:00

another piece of battles (and CCreatureAnimation)

This commit is contained in:
mateuszb 2008-03-05 17:01:41 +00:00
parent ffd3742e3d
commit 4236c65512
5 changed files with 153 additions and 26 deletions

View File

@ -11,6 +11,20 @@ extern SDL_Surface * screen;
CBattleInterface::CBattleInterface(CCreatureSet * army1, CCreatureSet * army2, int3 tile, CGHeroInstance *hero1, CGHeroInstance *hero2) : printCellBorders(true)
{
//initializing armies
this->army1 = army1;
this->army2 = army2;
for(std::map<int,std::pair<CCreature*,int> >::iterator i=army1->slots.begin(); i!=army1->slots.end(); ++i)
{
//creAnim1.push_back(new CCreatureAnimation(i->second.first->animDefName));
creAnim1.push_back(new CCreatureAnimation(i->second.first->animDefName));
creAnim1[creAnim1.size()-1]->setType(2);
}
for(std::map<int,std::pair<CCreature*,int> >::iterator i=army2->slots.begin(); i!=army2->slots.end(); ++i)
{
creAnim2.push_back(new CCreatureAnimation(i->second.first->animDefName));
creAnim2[creAnim2.size()-1]->setType(2);
}
//preparing menu background and terrain
std::vector< std::string > & backref = CGI->mh->battleBacks[ CGI->mh->ttiles[tile.x][tile.y][tile.z].terType ];
background = CGI->bitmaph->loadBitmap(backref[ rand() % backref.size()] );
@ -80,6 +94,11 @@ CBattleInterface::~CBattleInterface()
SDL_FreeSurface(cellBorder);
SDL_FreeSurface(cellShade);
for(int g=0; g<creAnim1.size(); ++g)
delete creAnim1[g];
for(int g=0; g<creAnim2.size(); ++g)
delete creAnim2[g];
}
void CBattleInterface::activate()
@ -142,8 +161,16 @@ void CBattleInterface::show(SDL_Surface * to)
bConsoleDown->show(to);
//showing hero animations
attackingHero->show(to);
defendingHero->show(to);
if(attackingHero)
attackingHero->show(to);
if(defendingHero)
defendingHero->show(to);
//showing units //a lot of work...
creAnim1[0]->nextFrame(to, -94, -139);
//units shown
CSDL_Ext::update();
}
@ -211,8 +238,8 @@ void CBattleHero::show(SDL_Surface *to)
{
CSDL_Ext::blit8bppAlphaTo24bpp(flag->ourImages[flagAnim].bitmap, NULL, screen, &genRect(flag->ourImages[flagAnim].bitmap->h, flag->ourImages[flagAnim].bitmap->w, 31, 39));
}
++flagAnimCount;
if(flagAnimCount%4==0)
//++flagAnimCount;
//if(flagAnimCount%4==0)
{
++flagAnim;
flagAnim %= flag->ourImages.size();

View File

@ -41,6 +41,8 @@ private:
* bWait, * bDefence, * bConsoleUp, * bConsoleDown;
CBattleHero * attackingHero, * defendingHero;
SDL_Surface * cellBorder, * cellShade;
CCreatureSet * army1, * army2; //fighting armies
std::vector< CCreatureAnimation * > creAnim1, creAnim2; //animations of creatures from fighting armies
public:
CBattleInterface(CCreatureSet * army1, CCreatureSet * army2, int3 tile, CGHeroInstance *hero1, CGHeroInstance *hero2); //c-tor
~CBattleInterface(); //d-tor

View File

@ -1873,15 +1873,18 @@ void CPlayerInterface::actionFinished(Action action)//occurs AFTER every action
void CPlayerInterface::activeStack(int stackID) //called when it's turn of that stack
{
unsigned int av=0;
while(true)
{
++av;
SDL_Event sEvent;
while (SDL_PollEvent(&sEvent)) //wait for event...
{
LOCPLINT->handleEvent(&sEvent);
}
for(int i=0;i<objsToBlit.size();i++)
objsToBlit[i]->show();
if(av%3==0)
for(int i=0;i<objsToBlit.size();i++)
objsToBlit[i]->show();
//SDL_Flip(ekran);
CSDL_Ext::update(ekran);

View File

@ -337,26 +337,69 @@ void CCreatureHandler::loadCreatures()
boost::assign::insert(bigImgs)(i-2,smi->ourImages[i].bitmap);
}
delete smi;
//
//loading unit animation def names
std::ifstream inp("config/CREDEFS.TXT", std::ios::in | std::ios::binary); //this file is not in lod
inp.seekg(0,std::ios::end); // na koniec
int andame2 = inp.tellg(); // read length
inp.seekg(0,std::ios::beg); // wracamy na poczatek
char * bufor = new char[andame2]; // allocate memory
inp.read((char*)bufor, andame2); // read map file to buffer
inp.close();
buf = std::string(bufor);
delete [andame2] bufor;
i = 0; //buf iterator
hmcr = 0;
for(i; i<andame2; ++i) //omitting rubbish
{
if(buf[i]=='\r')
break;
}
i+=2;
for(int s=0; s<creatures.size()-16; ++s)
{
int befi=i;
std::string rub;
for(i; i<andame2; ++i)
{
if(buf[i]==' ')
break;
}
rub = buf.substr(befi, i-befi);
++i;
befi=i;
for(i; i<andame2; ++i)
{
if(buf[i]=='\r')
break;
}
std::string defName = buf.substr(befi, i-befi);
creatures[s].animDefName = defName;
}
loadAnimationInfo();
}
void CCreatureHandler::loadAnimationInfo()
{
//std::string buf = CGameInfo::mainObj->bitmaph->getTextFile("CRANIM.TXT");
//int andame = buf.size();
//int i=0; //buf iterator
//hmcr=0;
//for(i; i<andame; ++i)
//{
// if(buf[i]=='\r')
// ++hmcr;
// if(hmcr==2)
// break;
//}
//i+=2;
//for(int dd=0; dd<creatures.size(); ++dd)
//{
// loadUnitAnimInfo(creatures[dd], buf, i);
//}
std::string buf = CGameInfo::mainObj->bitmaph->getTextFile("CRANIM.TXT");
int andame = buf.size();
int i=0; //buf iterator
int hmcr=0;
for(i; i<andame; ++i)
{
if(buf[i]=='\r')
++hmcr;
if(hmcr==2)
break;
}
i+=2;
for(int dd=0; dd<creatures.size()-16; ++dd)
{
loadUnitAnimInfo(creatures[dd], buf, i);
}
return;
}
@ -568,9 +611,31 @@ void CCreatureAnimation::setType(int type)
{
this->type = type;
curFrame = 0;
if(type!=-1)
{
if(SEntries[curFrame].group!=type) //rewind
{
int j=-1; //first frame in displayed group
for(int g=0; g<SEntries.size(); ++g)
{
if(SEntries[g].group==type && j==-1)
{
j = g;
break;
}
}
if(curFrame!=-1)
curFrame = j;
}
}
else
{
if(curFrame>=frames)
curFrame = 0;
}
}
CCreatureAnimation::CCreatureAnimation(std::string name)
CCreatureAnimation::CCreatureAnimation(std::string name) : RLEntries(NULL), RWEntries(NULL)
{
//load main file
std::string data2 = CGI->spriteh->getTextFile(name);
@ -683,8 +748,28 @@ int CCreatureAnimation::nextFrame(SDL_Surface *dest, int x, int y)
return -1; //not enough depth
int SIndex = curFrame++; //TODO: finish
if(curFrame>=frames)
curFrame = 0;
if(type!=-1)
{
if(SEntries[curFrame].group!=type) //rewind
{
int j=-1; //first frame in displayed group
for(int g=0; g<SEntries.size(); ++g)
{
if(SEntries[g].group==type && j==-1)
{
j = g;
break;
}
}
if(curFrame!=-1)
curFrame = j;
}
}
else
{
if(curFrame>=frames)
curFrame = 0;
}
long BaseOffset,
SpriteWidth, SpriteHeight, //format sprite'a
@ -839,3 +924,12 @@ int CCreatureAnimation::nextFrame(SDL_Surface *dest, int x, int y)
return 0;
}
CCreatureAnimation::~CCreatureAnimation()
{
delete [] FDef;
if (RWEntries)
delete [] RWEntries;
if (RLEntries)
delete [] RLEntries;
}

View File

@ -19,6 +19,7 @@ public:
int level; // 0 - unknown
std::string abilityText; //description of abilities
std::string abilityRefs; //references to abilities, in textformat
std::string animDefName;
int idNumber;
///animation info
@ -90,7 +91,7 @@ private:
int type; //type of animation being displayed (-1 - whole animation, >0 - specified part [default: -1])
public:
CCreatureAnimation(std::string name); //c-tor
//~CCreatureAnimation(); //d-tor //not necessery ATM
~CCreatureAnimation(); //d-tor //not necessery ATM
void setType(int type); //sets type of animation and cleares framecount
int getType() const; //returns type of animation