1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-02-17 13:41:07 +02:00

Use CAnimation fro custom battle effects

This commit is contained in:
AlexVinS 2017-09-05 17:21:44 +03:00
parent 882e279818
commit 78b86224a0
3 changed files with 61 additions and 68 deletions

View File

@ -21,6 +21,7 @@
#include "../CMusicHandler.h" #include "../CMusicHandler.h"
#include "../CPlayerInterface.h" #include "../CPlayerInterface.h"
#include "../Graphics.h" #include "../Graphics.h"
#include "../gui/CAnimation.h"
#include "../gui/CCursorHandler.h" #include "../gui/CCursorHandler.h"
#include "../gui/CGuiHandler.h" #include "../gui/CGuiHandler.h"
#include "../gui/SDL_Extensions.h" #include "../gui/SDL_Extensions.h"
@ -909,91 +910,82 @@ bool CSpellEffectAnimation::init()
const bool areaEffect = (!destTile.isValid() && x == -1 && y == -1); const bool areaEffect = (!destTile.isValid() && x == -1 && y == -1);
std::shared_ptr<CAnimation> animation = std::make_shared<CAnimation>(customAnim);
animation->preload();
if(Vflip)
animation->verticalFlip();
IImage * first = animation->getImage(0, 0, true);
if(!first)
{
endAnim();
return false;
}
if(areaEffect) //f.e. armageddon if(areaEffect) //f.e. armageddon
{ {
CDefHandler * anim = CDefHandler::giveDef(customAnim); for(int i=0; i * first->width() < owner->pos.w ; ++i)
for(int i=0; i * anim->width < owner->pos.w ; ++i)
{ {
for(int j=0; j * anim->height < owner->pos.h ; ++j) for(int j=0; j * first->height() < owner->pos.h ; ++j)
{ {
BattleEffect be; BattleEffect be;
be.effectID = ID; be.effectID = ID;
be.anim = CDefHandler::giveDef(customAnim); be.animation = animation;
if (Vflip)
{
for (auto & elem : be.anim->ourImages)
{
CSDL_Ext::VflipSurf(elem.bitmap);
}
}
be.currentFrame = 0; be.currentFrame = 0;
be.maxFrame = be.anim->ourImages.size();
be.x = i * anim->width + owner->pos.x; be.x = i * first->width() + owner->pos.x;
be.y = j * anim->height + owner->pos.y; be.y = j * first->height() + owner->pos.y;
be.position = BattleHex::INVALID; be.position = BattleHex::INVALID;
owner->battleEffects.push_back(be); owner->battleEffects.push_back(be);
} }
} }
delete anim;
} }
else // Effects targeted at a specific creature/hex. else // Effects targeted at a specific creature/hex.
{ {
const CStack * destStack = owner->getCurrentPlayerInterface()->cb->battleGetStackByPos(destTile, false);
Rect & tilePos = owner->bfield[destTile]->pos;
BattleEffect be;
be.effectID = ID;
be.animation = animation;
be.currentFrame = 0;
const CStack* destStack = owner->getCurrentPlayerInterface()->cb->battleGetStackByPos(destTile, false);
Rect & tilePos = owner->bfield[destTile]->pos;
BattleEffect be;
be.effectID = ID;
be.anim = CDefHandler::giveDef(customAnim);
if (Vflip) //todo: lightning anim frame count override
{
for (auto & elem : be.anim->ourImages)
{
CSDL_Ext::VflipSurf(elem.bitmap);
}
}
be.currentFrame = 0;
be.maxFrame = be.anim->ourImages.size();
//todo: lightning anim frame count override
// if(effect == 1) // if(effect == 1)
// be.maxFrame = 3; // be.maxFrame = 3;
if(x == -1) if(x == -1)
{ {
be.x = tilePos.x + tilePos.w/2 - be.anim->width/2; be.x = tilePos.x + tilePos.w/2 - first->width()/2;
} }
else
{
be.x = x;
}
if(y == -1)
{
if(alignToBottom)
be.y = tilePos.y + tilePos.h - first->height();
else else
{ be.y = tilePos.y - first->height()/2;
be.x = x; }
} else
{
be.y = y;
}
if(y == -1) // Correction for 2-hex creatures.
{ if(destStack != nullptr && destStack->doubleWide())
if(alignToBottom) be.x += (destStack->side == BattleSide::ATTACKER ? -1 : 1)*tilePos.w/2;
be.y = tilePos.y + tilePos.h - be.anim->height;
else
be.y = tilePos.y - be.anim->height/2;
}
else
{
be.y = y;
}
// Correction for 2-hex creatures. //Indicate if effect should be drawn on top of everything or just on top of the hex
if (destStack != nullptr && destStack->doubleWide()) be.position = destTile;
be.x += (destStack->side == BattleSide::ATTACKER ? -1 : 1)*tilePos.w/2;
//Indicate if effect should be drawn on top of everything or just on top of the hex
be.position = destTile;
owner->battleEffects.push_back(be);
owner->battleEffects.push_back(be);
} }
//battleEffects //battleEffects
return true; return true;
@ -1008,7 +1000,7 @@ void CSpellEffectAnimation::nextFrame()
{ {
elem.currentFrame += AnimationControls::getSpellEffectSpeed() * GH.mainFPSmng->getElapsedMilliseconds() / 1000; elem.currentFrame += AnimationControls::getSpellEffectSpeed() * GH.mainFPSmng->getElapsedMilliseconds() / 1000;
if(elem.currentFrame >= elem.maxFrame) if(elem.currentFrame >= elem.animation->size())
{ {
endAnim(); endAnim();
break; break;
@ -1038,7 +1030,6 @@ void CSpellEffectAnimation::endAnim()
for(auto & elem : toDel) for(auto & elem : toDel)
{ {
delete elem->anim;
owner->battleEffects.erase(elem); owner->battleEffects.erase(elem);
} }

View File

@ -3375,11 +3375,13 @@ void CBattleInterface::showBattleEffects(SDL_Surface *to, const std::vector<cons
for (auto & elem : battleEffects) for (auto & elem : battleEffects)
{ {
int currentFrame = floor(elem->currentFrame); int currentFrame = floor(elem->currentFrame);
currentFrame %= elem->anim->ourImages.size(); currentFrame %= elem->animation->size();
SDL_Surface *bitmapToBlit = elem->anim->ourImages[currentFrame].bitmap; IImage * img = elem->animation->getImage(currentFrame);
SDL_Rect temp_rect = genRect(bitmapToBlit->h, bitmapToBlit->w, elem->x, elem->y);
SDL_BlitSurface(bitmapToBlit, nullptr, to, &temp_rect); SDL_Rect temp_rect = genRect(img->height(), img->width(), elem->x, elem->y);
img->draw(to, &temp_rect, nullptr);
} }
} }

View File

@ -48,6 +48,7 @@ struct BattleHex;
struct InfoAboutHero; struct InfoAboutHero;
struct BattleAction; struct BattleAction;
class CBattleGameInterface; class CBattleGameInterface;
class CAnimation;
/// Small struct which contains information about the id of the attacked stack, the damage dealt,... /// Small struct which contains information about the id of the attacked stack, the damage dealt,...
struct StackAttackedInfo struct StackAttackedInfo
@ -67,8 +68,7 @@ struct BattleEffect
{ {
int x, y; //position on the screen int x, y; //position on the screen
float currentFrame; float currentFrame;
int maxFrame; std::shared_ptr<CAnimation> animation;
CDefHandler *anim; //animation to display
int effectID; //uniqueID equal ot ID of appropriate CSpellEffectAnim int effectID; //uniqueID equal ot ID of appropriate CSpellEffectAnim
BattleHex position; //Indicates if effect which hex the effect is drawn on BattleHex position; //Indicates if effect which hex the effect is drawn on
}; };