1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-26 03:52:01 +02:00

Fixed positioning of battle effects / animations in non 800x600 resolutions.

This commit is contained in:
Michał W. Urbańczyk 2009-09-21 08:29:41 +00:00
parent 22a74ea781
commit bff3abe122
5 changed files with 95 additions and 67 deletions

View File

@ -139,8 +139,8 @@ bool CSpellEffectAnim::init()
be.anim = CDefHandler::giveDef(graphics->battleACToDef[effect][0]); be.anim = CDefHandler::giveDef(graphics->battleACToDef[effect][0]);
be.frame = 0; be.frame = 0;
be.maxFrame = be.anim->ourImages.size(); be.maxFrame = be.anim->ourImages.size();
be.x = i * anim->width; be.x = i * anim->width + owner->pos.x;
be.y = j * anim->height; be.y = j * anim->height + owner->pos.y;
owner->battleEffects.push_back(be); owner->battleEffects.push_back(be);
} }
@ -327,8 +327,8 @@ void CReverseAnim::nextFrame()
const CStack * curs = LOCPLINT->cb->battleGetStackByID(stackID, false); const CStack * curs = LOCPLINT->cb->battleGetStackByID(stackID, false);
std::pair <int, int> coords = CBattleHex::getXYUnitAnim(hex, owner->creDir[stackID], curs, owner); Point coords = CBattleHex::getXYUnitAnim(hex, owner->creDir[stackID], curs, owner);
owner->creAnims[stackID]->pos.x = coords.first; owner->creAnims[stackID]->pos.x = coords.x;
//creAnims[stackID]->pos.y = coords.second; //creAnims[stackID]->pos.y = coords.second;
if(curs->hasFeatureOfType(StackFeature::DOUBLE_WIDE)) if(curs->hasFeatureOfType(StackFeature::DOUBLE_WIDE))
@ -523,18 +523,18 @@ bool CBattleStackMoved::init()
} }
bool twoTiles = movedStack->hasFeatureOfType(StackFeature::DOUBLE_WIDE); bool twoTiles = movedStack->hasFeatureOfType(StackFeature::DOUBLE_WIDE);
std::pair<int, int> begPosition = CBattleHex::getXYUnitAnim(curStackPos, movedStack->attackerOwned, movedStack, owner); Point begPosition = CBattleHex::getXYUnitAnim(curStackPos, movedStack->attackerOwned, movedStack, owner);
std::pair<int, int> endPosition = CBattleHex::getXYUnitAnim(destHex, movedStack->attackerOwned, movedStack, owner); Point endPosition = CBattleHex::getXYUnitAnim(destHex, movedStack->attackerOwned, movedStack, owner);
int mutPos = BattleInfo::mutualPosition(curStackPos, destHex); int mutPos = BattleInfo::mutualPosition(curStackPos, destHex);
//reverse unit if necessary //reverse unit if necessary
if((begPosition.first > endPosition.first) && owner->creDir[stackID] == true) if((begPosition.x > endPosition.x) && owner->creDir[stackID] == true)
{ {
owner->addNewAnim(new CReverseAnim(owner, stackID, curStackPos, true)); owner->addNewAnim(new CReverseAnim(owner, stackID, curStackPos, true));
return false; return false;
} }
else if ((begPosition.first < endPosition.first) && owner->creDir[stackID] == false) else if ((begPosition.x < endPosition.x) && owner->creDir[stackID] == false)
{ {
owner->addNewAnim(new CReverseAnim(owner, stackID, curStackPos, true)); owner->addNewAnim(new CReverseAnim(owner, stackID, curStackPos, true));
return false; return false;
@ -554,9 +554,10 @@ bool CBattleStackMoved::init()
if(mutPos == -1 && movedStack->hasFeatureOfType(StackFeature::FLYING)) if(mutPos == -1 && movedStack->hasFeatureOfType(StackFeature::FLYING))
{ {
steps *= distance; steps *= distance;
steps /= 2; //to make animation faster
stepX = (endPosition.first - (float)begPosition.first)/steps; stepX = (endPosition.x - (float)begPosition.x)/steps;
stepY = (endPosition.second - (float)begPosition.second)/steps; stepY = (endPosition.y - (float)begPosition.y)/steps;
} }
else else
{ {
@ -623,14 +624,13 @@ void CBattleStackMoved::endAnim()
owner->addNewAnim(new CBattleMoveEnd(owner, stackID, destHex)); owner->addNewAnim(new CBattleMoveEnd(owner, stackID, destHex));
} }
std::pair <int, int> coords = CBattleHex::getXYUnitAnim(destHex, owner->creDir[stackID], movedStack, owner); Point coords = CBattleHex::getXYUnitAnim(destHex, owner->creDir[stackID], movedStack, owner);
owner->creAnims[stackID]->pos.x = coords.first; owner->creAnims[stackID]->pos = coords;
if(!endMoving && twoTiles && bool(movedStack->attackerOwned) && (owner->creDir[stackID] != bool(movedStack->attackerOwned) )) //big attacker creature is reversed if(!endMoving && twoTiles && bool(movedStack->attackerOwned) && (owner->creDir[stackID] != bool(movedStack->attackerOwned) )) //big attacker creature is reversed
owner->creAnims[stackID]->pos.x -= 44; owner->creAnims[stackID]->pos.x -= 44;
else if(!endMoving && twoTiles && (! bool(movedStack->attackerOwned) ) && (owner->creDir[stackID] != bool(movedStack->attackerOwned) )) //big defender creature is reversed else if(!endMoving && twoTiles && (! bool(movedStack->attackerOwned) ) && (owner->creDir[stackID] != bool(movedStack->attackerOwned) )) //big defender creature is reversed
owner->creAnims[stackID]->pos.x += 44; owner->creAnims[stackID]->pos.x += 44;
owner->creAnims[stackID]->pos.y = coords.second;
owner->creAnims[stackID]->pos += LOCPLINT->battleInt->pos;
} }
if(owner->moveSh >= 0) if(owner->moveSh >= 0)
@ -929,30 +929,30 @@ bool CShootingAnim::init()
spi.frameNum = 0; spi.frameNum = 0;
spi.spin = CGI->creh->idToProjectileSpin[spi.creID]; spi.spin = CGI->creh->idToProjectileSpin[spi.creID];
std::pair<int, int> xycoord = CBattleHex::getXYUnitAnim(shooter->position, true, shooter, owner); Point xycoord = CBattleHex::getXYUnitAnim(shooter->position, true, shooter, owner);
std::pair<int, int> destcoord = CBattleHex::getXYUnitAnim(dest, false, attackedStack, owner); Point destcoord = CBattleHex::getXYUnitAnim(dest, false, attackedStack, owner);
destcoord.first += 250; destcoord.second += 210; //TODO: find a better place to shoot destcoord.x += 250; destcoord.y += 210; //TODO: find a better place to shoot
if(projectileAngle > straightAngle) //upper shot if(projectileAngle > straightAngle) //upper shot
{ {
spi.x = xycoord.first + 200 + shooter->creature->upperRightMissleOffsetX; spi.x = xycoord.x + 200 + shooter->creature->upperRightMissleOffsetX;
spi.y = xycoord.second + 100 - shooter->creature->upperRightMissleOffsetY; spi.y = xycoord.y + 100 - shooter->creature->upperRightMissleOffsetY;
} }
else if(projectileAngle < -straightAngle) //lower shot else if(projectileAngle < -straightAngle) //lower shot
{ {
spi.x = xycoord.first + 200 + shooter->creature->lowerRightMissleOffsetX; spi.x = xycoord.x + 200 + shooter->creature->lowerRightMissleOffsetX;
spi.y = xycoord.second + 150 - shooter->creature->lowerRightMissleOffsetY; spi.y = xycoord.y + 150 - shooter->creature->lowerRightMissleOffsetY;
} }
else //straight shot else //straight shot
{ {
spi.x = xycoord.first + 200 + shooter->creature->rightMissleOffsetX; spi.x = xycoord.x + 200 + shooter->creature->rightMissleOffsetX;
spi.y = xycoord.second + 125 - shooter->creature->rightMissleOffsetY; spi.y = xycoord.y + 125 - shooter->creature->rightMissleOffsetY;
} }
spi.lastStep = sqrt((float)((destcoord.first - spi.x)*(destcoord.first - spi.x) + (destcoord.second - spi.y) * (destcoord.second - spi.y))) / 40; spi.lastStep = sqrt((float)((destcoord.x - spi.x)*(destcoord.x - spi.x) + (destcoord.y - spi.y) * (destcoord.y - spi.y))) / 40;
if(spi.lastStep == 0) if(spi.lastStep == 0)
spi.lastStep = 1; spi.lastStep = 1;
spi.dx = (destcoord.first - spi.x) / spi.lastStep; spi.dx = (destcoord.x - spi.x) / spi.lastStep;
spi.dy = (destcoord.second - spi.y) / spi.lastStep; spi.dy = (destcoord.y - spi.y) / spi.lastStep;
//set starting frame //set starting frame
if(spi.spin) if(spi.spin)
{ {
@ -1048,6 +1048,8 @@ CBattleInterface::CBattleInterface(CCreatureSet * army1, CCreatureSet * army2, C
siegeH = new SiegeHelper(town, this); siegeH = new SiegeHelper(town, this);
} }
LOCPLINT->battleInt = this;
//initializing armies //initializing armies
this->army1 = army1; this->army1 = army1;
this->army2 = army2; this->army2 = army2;
@ -1534,7 +1536,7 @@ void CBattleInterface::show(SDL_Surface * to)
{ {
for(size_t v=0; v<stackDeadByHex[b].size(); ++v) for(size_t v=0; v<stackDeadByHex[b].size(); ++v)
{ {
creAnims[stackDeadByHex[b][v]]->nextFrame(to, creAnims[stackDeadByHex[b][v]]->pos.x + pos.x, creAnims[stackDeadByHex[b][v]]->pos.y + pos.y, creDir[stackDeadByHex[b][v]], animCount, false); //increment always when moving, never if stack died creAnims[stackDeadByHex[b][v]]->nextFrame(to, creAnims[stackDeadByHex[b][v]]->pos.x, creAnims[stackDeadByHex[b][v]]->pos.y, creDir[stackDeadByHex[b][v]], animCount, false); //increment always when moving, never if stack died
} }
} }
for(int b=0; b<BFIELD_SIZE; ++b) //showing alive stacks for(int b=0; b<BFIELD_SIZE; ++b) //showing alive stacks
@ -1557,7 +1559,7 @@ void CBattleInterface::show(SDL_Surface * to)
for(std::list<SBattleEffect>::iterator it = battleEffects.begin(); it!=battleEffects.end(); ++it) for(std::list<SBattleEffect>::iterator it = battleEffects.begin(); it!=battleEffects.end(); ++it)
{ {
SDL_Surface * bitmapToBlit = it->anim->ourImages[(it->frame)%it->anim->ourImages.size()].bitmap; SDL_Surface * bitmapToBlit = it->anim->ourImages[(it->frame)%it->anim->ourImages.size()].bitmap;
SDL_BlitSurface(bitmapToBlit, NULL, to, &genRect(bitmapToBlit->h, bitmapToBlit->w, pos.x + it->x, pos.y + it->y)); SDL_BlitSurface(bitmapToBlit, NULL, to, &genRect(bitmapToBlit->h, bitmapToBlit->w, it->x, it->y));
} }
} }
@ -1589,6 +1591,8 @@ void CBattleInterface::show(SDL_Surface * to)
//showing queue //showing queue
if(!bresult) if(!bresult)
queue->showAll(to); queue->showAll(to);
else
queue->blitBg(to); //blit only background, stacks are deleted
} }
//printing border around interface //printing border around interface
@ -2050,7 +2054,7 @@ void CBattleInterface::newStack(int stackID)
{ {
const CStack * newStack = LOCPLINT->cb->battleGetStackByID(stackID); const CStack * newStack = LOCPLINT->cb->battleGetStackByID(stackID);
std::pair <int, int> coords = CBattleHex::getXYUnitAnim(newStack->position, newStack->owner == attackingHeroInstance->tempOwner, newStack, this);; Point coords = CBattleHex::getXYUnitAnim(newStack->position, newStack->owner == attackingHeroInstance->tempOwner, newStack, this);;
if(newStack->position < 0) //turret if(newStack->position < 0) //turret
{ {
@ -2062,7 +2066,7 @@ void CBattleInterface::newStack(int stackID)
creAnims[stackID] = new CCreatureAnimation(newStack->creature->animDefName); creAnims[stackID] = new CCreatureAnimation(newStack->creature->animDefName);
} }
creAnims[stackID]->setType(2); creAnims[stackID]->setType(2);
creAnims[stackID]->pos = Rect(coords.first, coords.second, creAnims[newStack->ID]->fullWidth, creAnims[newStack->ID]->fullHeight) + pos; creAnims[stackID]->pos = Rect(coords.x, coords.y, creAnims[newStack->ID]->fullWidth, creAnims[newStack->ID]->fullHeight);
creDir[stackID] = newStack->attackerOwned; creDir[stackID] = newStack->attackerOwned;
} }
@ -2453,12 +2457,12 @@ void CBattleInterface::spellCast(SpellCast * sc)
{ //common ice bolt and magic arrow part { //common ice bolt and magic arrow part
//initial variables //initial variables
std::string animToDisplay; std::string animToDisplay;
std::pair<int, int> srccoord = sc->side ? std::make_pair(770, 60) : std::make_pair(30, 60); Point srccoord = (sc->side ? Point(770, 60) : Point(30, 60)) + pos;
std::pair<int, int> destcoord = CBattleHex::getXYUnitAnim(sc->tile, !sc->side, LOCPLINT->cb->battleGetStackByPos(sc->tile), this); //position attacked by arrow Point destcoord = CBattleHex::getXYUnitAnim(sc->tile, !sc->side, LOCPLINT->cb->battleGetStackByPos(sc->tile), this); //position attacked by arrow
destcoord.first += 250; destcoord.second += 240; destcoord.x += 250; destcoord.y += 240;
//animation angle //animation angle
float angle = atan2(float(destcoord.first - srccoord.first), float(destcoord.second - srccoord.second)); float angle = atan2(float(destcoord.x - srccoord.x), float(destcoord.y - srccoord.y));
//choosing animation by angle //choosing animation by angle
if(angle > 1.50) if(angle > 1.50)
@ -2474,14 +2478,14 @@ void CBattleInterface::spellCast(SpellCast * sc)
//displaying animation //displaying animation
CDefEssential * animDef = CDefHandler::giveDefEss(animToDisplay); CDefEssential * animDef = CDefHandler::giveDefEss(animToDisplay);
int steps = sqrt((float)((destcoord.first - srccoord.first)*(destcoord.first - srccoord.first) + (destcoord.second - srccoord.second) * (destcoord.second - srccoord.second))) / 40; int steps = sqrt((float)((destcoord.x - srccoord.x)*(destcoord.x - srccoord.x) + (destcoord.y - srccoord.y) * (destcoord.y - srccoord.y))) / 40;
if(steps <= 0) if(steps <= 0)
steps = 1; steps = 1;
int dx = (destcoord.first - srccoord.first - animDef->ourImages[0].bitmap->w)/steps, dy = (destcoord.second - srccoord.second - animDef->ourImages[0].bitmap->h)/steps; int dx = (destcoord.x - srccoord.x - animDef->ourImages[0].bitmap->w)/steps, dy = (destcoord.y - srccoord.y - animDef->ourImages[0].bitmap->h)/steps;
delete animDef; delete animDef;
addNewAnim(new CSpellEffectAnim(this, animToDisplay, srccoord.first, srccoord.second, dx, dy)); addNewAnim(new CSpellEffectAnim(this, animToDisplay, srccoord.x, srccoord.y, dx, dy));
break; //for 15 and 16 cases break; //for 15 and 16 cases
} }
@ -3093,8 +3097,9 @@ CBattleHero::~CBattleHero()
delete flag; delete flag;
} }
std::pair<int, int> CBattleHex::getXYUnitAnim(const int & hexNum, const bool & attacker, const CStack * stack, const CBattleInterface * cbi) Point CBattleHex::getXYUnitAnim(const int & hexNum, const bool & attacker, const CStack * stack, const CBattleInterface * cbi)
{ {
Point ret(-500, -500); //returned value
if(stack->position < 0) //creatures in turrets if(stack->position < 0) //creatures in turrets
{ {
const CCreature & turretCreature = CGI->creh->creatures[ CGI->creh->factionToTurretCreature[cbi->siegeH->town->town->typeID] ]; const CCreature & turretCreature = CGI->creh->creatures[ CGI->creh->factionToTurretCreature[cbi->siegeH->town->town->typeID] ];
@ -3103,42 +3108,43 @@ std::pair<int, int> CBattleHex::getXYUnitAnim(const int & hexNum, const bool & a
switch(stack->position) switch(stack->position)
{ {
case -2: //keep case -2: //keep
return std::make_pair(505 + xShift, -66); ret = Point(505 + xShift, -66);
break; break;
case -3: //lower turret case -3: //lower turret
return std::make_pair(368 + xShift, 304); ret = Point(368 + xShift, 304);
break; break;
case -4: //upper turret case -4: //upper turret
return std::make_pair(339 + xShift, -192); ret = Point(339 + xShift, -192);
break; break;
} }
} }
std::pair<int, int> ret = std::make_pair(-500, -500); //returned value
ret.second = -139 + 42 * (hexNum/BFIELD_WIDTH); //counting y
//counting x
if(attacker)
{
ret.first = -160 + 22 * ( ((hexNum/BFIELD_WIDTH) + 1)%2 ) + 44 * (hexNum % BFIELD_WIDTH);
}
else else
{ {
ret.first = -219 + 22 * ( ((hexNum/BFIELD_WIDTH) + 1)%2 ) + 44 * (hexNum % BFIELD_WIDTH); ret.y = -139 + 42 * (hexNum/BFIELD_WIDTH); //counting y
//counting x
if(attacker)
{
ret.x = -160 + 22 * ( ((hexNum/BFIELD_WIDTH) + 1)%2 ) + 44 * (hexNum % BFIELD_WIDTH);
}
else
{
ret.x = -219 + 22 * ( ((hexNum/BFIELD_WIDTH) + 1)%2 ) + 44 * (hexNum % BFIELD_WIDTH);
} }
//shifting position for double - hex creatures //shifting position for double - hex creatures
if(stack && stack->hasFeatureOfType(StackFeature::DOUBLE_WIDE)) if(stack && stack->hasFeatureOfType(StackFeature::DOUBLE_WIDE))
{ {
if(attacker) if(attacker)
{ {
ret.first -= 42; ret.x -= 42;
} }
else else
{ {
ret.first += 42; ret.x += 42;
}
} }
} }
//returning //returning
return ret; return ret + LOCPLINT->battleInt->pos;
} }
void CBattleHex::activate() void CBattleHex::activate()
{ {
@ -3802,6 +3808,13 @@ CStackQueue::~CStackQueue()
} }
void CStackQueue::showAll( SDL_Surface *to ) void CStackQueue::showAll( SDL_Surface *to )
{
blitBg(to);
CIntObject::showAll(to);
}
void CStackQueue::blitBg( SDL_Surface * to )
{ {
if(bg) if(bg)
{ {
@ -3810,7 +3823,6 @@ void CStackQueue::showAll( SDL_Surface *to )
blitAtLoc(bg, w, 0, to); blitAtLoc(bg, w, 0, to);
} }
} }
CIntObject::showAll(to);
} }
void CStackQueue::StackBox::showAll( SDL_Surface *to ) void CStackQueue::StackBox::showAll( SDL_Surface *to )

View File

@ -243,7 +243,7 @@ public:
//CStack * ourStack; //CStack * ourStack;
bool hovered, strictHovered; //for determining if hex is hovered by mouse (this is different problem than hex's graphic hovering) bool hovered, strictHovered; //for determining if hex is hovered by mouse (this is different problem than hex's graphic hovering)
CBattleInterface * myInterface; //interface that owns me CBattleInterface * myInterface; //interface that owns me
static std::pair<int, int> getXYUnitAnim(const int & hexNum, const bool & attacker, const CStack * creature, const CBattleInterface * cbi); //returns (x, y) of left top corner of animation static Point getXYUnitAnim(const int & hexNum, const bool & attacker, const CStack * creature, const CBattleInterface * cbi); //returns (x, y) of left top corner of animation
//for user interactions //for user interactions
void hover (bool on); void hover (bool on);
void activate(); void activate();
@ -365,6 +365,7 @@ public:
CStackQueue(bool Embedded); CStackQueue(bool Embedded);
~CStackQueue(); ~CStackQueue();
void update(); void update();
void blitBg( SDL_Surface * to );
//void showAll(SDL_Surface *to); //void showAll(SDL_Surface *to);
}; };

View File

@ -951,7 +951,7 @@ void CPlayerInterface::battleStart(CCreatureSet *army1, CCreatureSet *army2, int
SDL_Delay(20); SDL_Delay(20);
boost::unique_lock<boost::recursive_mutex> un(*pim); boost::unique_lock<boost::recursive_mutex> un(*pim);
battleInt = new CBattleInterface(army1, army2, hero1, hero2, genRect(600, 800, (conf.cc.resx - 800)/2, (conf.cc.resy - 600)/2)); /*battleInt = */new CBattleInterface(army1, army2, hero1, hero2, genRect(600, 800, (conf.cc.resx - 800)/2, (conf.cc.resy - 600)/2));
CGI->musich->playMusicFromSet(CGI->musich->battleMusics, -1); CGI->musich->playMusicFromSet(CGI->musich->battleMusics, -1);
GH.pushInt(battleInt); GH.pushInt(battleInt);
} }

View File

@ -287,10 +287,12 @@ void CSelectionScreen::changeSelection( const CMapInfo *to )
void CSelectionScreen::updateStartInfo( const CMapInfo * to ) void CSelectionScreen::updateStartInfo( const CMapInfo * to )
{ {
if(!to) return;
sInfo.mapname = to->filename;
sInfo.playerInfos.clear(); sInfo.playerInfos.clear();
if(!to)
return;
sInfo.playerInfos.resize(to->playerAmnt); sInfo.playerInfos.resize(to->playerAmnt);
sInfo.mapname = to->filename;
playerColor = -1; playerColor = -1;
int serialC=0; int serialC=0;

View File

@ -69,21 +69,28 @@ struct Point
:x(a.x),y(a.y) :x(a.x),y(a.y)
{} {}
Point operator+(const Point &b) const template<typename T>
Point operator+(const T &b) const
{ {
return Point(x+b.x,y+b.y); return Point(x+b.x,y+b.y);
} }
Point& operator+=(const Point &b)
template<typename T>
Point& operator+=(const T &b)
{ {
x += b.x; x += b.x;
y += b.y; y += b.y;
return *this; return *this;
} }
Point operator-(const Point &b) const
template<typename T>
Point operator-(const T &b) const
{ {
return Point(x+b.x,y+b.y); return Point(x+b.x,y+b.y);
} }
Point& operator-=(const Point &b)
template<typename T>
Point& operator-=(const T &b)
{ {
x -= b.x; x -= b.x;
y -= b.y; y -= b.y;
@ -159,6 +166,12 @@ struct Rect : public SDL_Rect
{ {
return Rect(x+p.x,y+p.y,w,h); return Rect(x+p.x,y+p.y,w,h);
} }
Rect& operator=(const Point &p) //assignment operator
{
x = p.x;
y = p.y;
return *this;
}
Rect& operator=(const Rect &p) //assignment operator Rect& operator=(const Rect &p) //assignment operator
{ {
x = p.x; x = p.x;