1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-14 02:33:51 +02:00

Use CAnimation in CBattleInterfaceClasses

This commit is contained in:
AlexVinS 2017-09-05 16:44:27 +03:00
parent 69c6643893
commit 882e279818
5 changed files with 88 additions and 55 deletions

View File

@ -23,6 +23,7 @@
#include "../CPlayerInterface.h" #include "../CPlayerInterface.h"
#include "../CVideoHandler.h" #include "../CVideoHandler.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"
@ -263,7 +264,10 @@ CBattleInterface::CBattleInterface(const CCreatureSet *army1, const CCreatureSet
battleImage = hero1->type->heroClass->imageBattleMale; battleImage = hero1->type->heroClass->imageBattleMale;
attackingHero = new CBattleHero(battleImage, false, hero1->tempOwner, hero1->tempOwner == curInt->playerID ? hero1 : nullptr, this); attackingHero = new CBattleHero(battleImage, false, hero1->tempOwner, hero1->tempOwner == curInt->playerID ? hero1 : nullptr, this);
attackingHero->pos = genRect(attackingHero->dh->ourImages[0].bitmap->h, attackingHero->dh->ourImages[0].bitmap->w, pos.x - 43, pos.y - 19);
IImage * img = attackingHero->animation->getImage(0, 0, true);
if(img)
attackingHero->pos = genRect(img->height(), img->width(), pos.x - 43, pos.y - 19);
} }
else else
{ {
@ -278,7 +282,10 @@ CBattleInterface::CBattleInterface(const CCreatureSet *army1, const CCreatureSet
battleImage = hero2->type->heroClass->imageBattleMale; battleImage = hero2->type->heroClass->imageBattleMale;
defendingHero = new CBattleHero(battleImage, true, hero2->tempOwner, hero2->tempOwner == curInt->playerID ? hero2 : nullptr, this); defendingHero = new CBattleHero(battleImage, true, hero2->tempOwner, hero2->tempOwner == curInt->playerID ? hero2 : nullptr, this);
defendingHero->pos = genRect(defendingHero->dh->ourImages[0].bitmap->h, defendingHero->dh->ourImages[0].bitmap->w, pos.x + 693, pos.y - 19);
IImage * img = defendingHero->animation->getImage(0, 0, true);
if(img)
defendingHero->pos = genRect(img->height(), img->width(), pos.x + 693, pos.y - 19);
} }
else else
{ {

View File

@ -13,13 +13,13 @@
#include "CBattleInterface.h" #include "CBattleInterface.h"
#include "../CBitmapHandler.h" #include "../CBitmapHandler.h"
#include "../CDefHandler.h"
#include "../CGameInfo.h" #include "../CGameInfo.h"
#include "../CMessage.h" #include "../CMessage.h"
#include "../CMusicHandler.h" #include "../CMusicHandler.h"
#include "../CPlayerInterface.h" #include "../CPlayerInterface.h"
#include "../CVideoHandler.h" #include "../CVideoHandler.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"
@ -129,13 +129,18 @@ CBattleConsole::CBattleConsole() : lastShown(-1), alterTxt(""), whoSetAlter(0)
void CBattleHero::show(SDL_Surface * to) void CBattleHero::show(SDL_Surface * to)
{ {
IImage * flagFrame = flagAnimation->getImage(flagAnim, 0, true);
if(!flagFrame)
return;
//animation of flag //animation of flag
SDL_Rect temp_rect; SDL_Rect temp_rect;
if(flip) if(flip)
{ {
temp_rect = genRect( temp_rect = genRect(
flag->ourImages[flagAnim].bitmap->h, flagFrame->height(),
flag->ourImages[flagAnim].bitmap->w, flagFrame->width(),
pos.x + 61, pos.x + 61,
pos.y + 39); pos.y + 39);
@ -143,28 +148,30 @@ void CBattleHero::show(SDL_Surface * to)
else else
{ {
temp_rect = genRect( temp_rect = genRect(
flag->ourImages[flagAnim].bitmap->h, flagFrame->height(),
flag->ourImages[flagAnim].bitmap->w, flagFrame->width(),
pos.x + 72, pos.x + 72,
pos.y + 39); pos.y + 39);
} }
CSDL_Ext::blit8bppAlphaTo24bpp(
flag->ourImages[flagAnim].bitmap, flagFrame->draw(screen, &temp_rect, nullptr); //FIXME: why screen?
nullptr,
screen,
&temp_rect);
//animation of hero //animation of hero
SDL_Rect rect = pos; SDL_Rect rect = pos;
CSDL_Ext::blit8bppAlphaTo24bpp(dh->ourImages[currentFrame].bitmap, nullptr, to, &rect);
if ( ++animCount == 4 ) IImage * heroFrame = animation->getImage(currentFrame, phase, true);
if(!heroFrame)
return;
heroFrame->draw(to, &rect, nullptr);
if(++animCount >= 4)
{ {
animCount = 0; animCount = 0;
if ( ++flagAnim >= flag->ourImages.size()) if(++flagAnim >= flagAnimation->size(0))
flagAnim = 0; flagAnim = 0;
if ( ++currentFrame >= lastFrame) if(++currentFrame >= lastFrame)
switchToNextPhase(); switchToNextPhase();
} }
} }
@ -190,7 +197,13 @@ void CBattleHero::clickLeft(tribool down, bool previousState)
if(myOwner->spellDestSelectMode) //we are casting a spell if(myOwner->spellDestSelectMode) //we are casting a spell
return; return;
if(myHero != nullptr && !down && myOwner->myTurn && myOwner->getCurrentPlayerInterface()->cb->battleCanCastSpell(myHero, ECastingMode::HERO_CASTING) == ESpellCastProblem::OK) //check conditions if(boost::logic::indeterminate(down))
return;
if(!myHero || down || !myOwner->myTurn)
return;
if(myOwner->getCurrentPlayerInterface()->cb->battleCanCastSpell(myHero, ECastingMode::HERO_CASTING) == ESpellCastProblem::OK) //check conditions
{ {
for(int it=0; it<GameConstants::BFIELD_SIZE; ++it) //do nothing when any hex is hovered - hero's animation overlaps battlefield for(int it=0; it<GameConstants::BFIELD_SIZE; ++it) //do nothing when any hex is hovered - hero's animation overlaps battlefield
{ {
@ -205,6 +218,9 @@ void CBattleHero::clickLeft(tribool down, bool previousState)
void CBattleHero::clickRight(tribool down, bool previousState) void CBattleHero::clickRight(tribool down, bool previousState)
{ {
if(boost::logic::indeterminate(down))
return;
Point windowPosition; Point windowPosition;
windowPosition.x = (!flip) ? myOwner->pos.topLeft().x + 1 : myOwner->pos.topRight().x - 79; windowPosition.x = (!flip) ? myOwner->pos.topLeft().x + 1 : myOwner->pos.topRight().x - 79;
windowPosition.y = myOwner->pos.y + 135; windowPosition.y = myOwner->pos.y + 135;
@ -220,24 +236,19 @@ void CBattleHero::clickRight(tribool down, bool previousState)
void CBattleHero::switchToNextPhase() void CBattleHero::switchToNextPhase()
{ {
if (phase != nextPhase) if(phase != nextPhase)
{ {
phase = nextPhase; phase = nextPhase;
//find first and last frames of our animation firstFrame = 0;
for (firstFrame = 0;
firstFrame < dh->ourImages.size() && dh->ourImages[firstFrame].groupNumber != phase;
firstFrame++);
for (lastFrame = firstFrame; lastFrame = animation->size(phase);
lastFrame < dh->ourImages.size() && dh->ourImages[lastFrame].groupNumber == phase;
lastFrame++);
} }
currentFrame = firstFrame; currentFrame = firstFrame;
} }
CBattleHero::CBattleHero(const std::string & defName, bool flipG, PlayerColor player, const CGHeroInstance * hero, const CBattleInterface * owner): CBattleHero::CBattleHero(const std::string & animationPath, bool flipG, PlayerColor player, const CGHeroInstance * hero, const CBattleInterface * owner):
flip(flipG), flip(flipG),
myHero(hero), myHero(hero),
myOwner(owner), myOwner(owner),
@ -246,39 +257,25 @@ CBattleHero::CBattleHero(const std::string & defName, bool flipG, PlayerColor pl
flagAnim(0), flagAnim(0),
animCount(0) animCount(0)
{ {
dh = CDefHandler::giveDef( defName ); animation = std::make_shared<CAnimation>(animationPath);
for(auto & elem : dh->ourImages) //transforming images animation->preload();
{ if(flipG)
if(flip) animation->verticalFlip();
{
SDL_Surface * hlp = CSDL_Ext::verticalFlip(elem.bitmap);
SDL_FreeSurface(elem.bitmap);
elem.bitmap = hlp;
}
CSDL_Ext::alphaTransform(elem.bitmap);
}
if(flip) if(flip)
flag = CDefHandler::giveDef("CMFLAGR.DEF"); flagAnimation = std::make_shared<CAnimation>("CMFLAGR");
else else
flag = CDefHandler::giveDef("CMFLAGL.DEF"); flagAnimation = std::make_shared<CAnimation>("CMFLAGL");
flagAnimation->preload();
flagAnimation->playerColored(player);
//coloring flag and adding transparency
for(auto & elem : flag->ourImages)
{
CSDL_Ext::alphaTransform(elem.bitmap);
graphics->blueToPlayersAdv(elem.bitmap, player);
}
addUsedEvents(LCLICK | RCLICK | HOVER); addUsedEvents(LCLICK | RCLICK | HOVER);
switchToNextPhase(); switchToNextPhase();
} }
CBattleHero::~CBattleHero() CBattleHero::~CBattleHero() = default;
{
delete dh;
delete flag;
}
CBattleOptionsWindow::CBattleOptionsWindow(const SDL_Rect & position, CBattleInterface *owner) CBattleOptionsWindow::CBattleOptionsWindow(const SDL_Rect & position, CBattleInterface *owner)
{ {

View File

@ -14,7 +14,6 @@
#include "../windows/CWindowObject.h" #include "../windows/CWindowObject.h"
struct SDL_Surface; struct SDL_Surface;
class CDefHandler;
class CGHeroInstance; class CGHeroInstance;
class CBattleInterface; class CBattleInterface;
class CPicture; class CPicture;
@ -53,19 +52,24 @@ class CBattleHero : public CIntObject
void switchToNextPhase(); void switchToNextPhase();
public: public:
bool flip; //false if it's attacking hero, true otherwise bool flip; //false if it's attacking hero, true otherwise
CDefHandler *dh, *flag; //animation and flag
std::shared_ptr<CAnimation> animation;
std::shared_ptr<CAnimation> flagAnimation;
const CGHeroInstance * myHero; //this animation's hero instance const CGHeroInstance * myHero; //this animation's hero instance
const CBattleInterface * myOwner; //battle interface to which this animation is assigned const CBattleInterface * myOwner; //battle interface to which this animation is assigned
int phase; //stage of animation int phase; //stage of animation
int nextPhase; //stage of animation to be set after current phase is fully displayed int nextPhase; //stage of animation to be set after current phase is fully displayed
int currentFrame, firstFrame, lastFrame; //frame of animation int currentFrame, firstFrame, lastFrame; //frame of animation
ui8 flagAnim, animCount; //for flag animation
size_t flagAnim;
ui8 animCount; //for flag animation
void show(SDL_Surface * to) override; //prints next frame of animation to to void show(SDL_Surface * to) override; //prints next frame of animation to to
void setPhase(int newPhase); //sets phase of hero animation void setPhase(int newPhase); //sets phase of hero animation
void hover(bool on) override; void hover(bool on) override;
void clickLeft(tribool down, bool previousState) override; //call-in void clickLeft(tribool down, bool previousState) override; //call-in
void clickRight(tribool down, bool previousState) override; //call-in void clickRight(tribool down, bool previousState) override; //call-in
CBattleHero(const std::string &defName, bool filpG, PlayerColor player, const CGHeroInstance *hero, const CBattleInterface *owner); CBattleHero(const std::string & animationPath, bool filpG, PlayerColor player, const CGHeroInstance * hero, const CBattleInterface * owner);
~CBattleHero(); ~CBattleHero();
}; };

View File

@ -29,7 +29,7 @@ typedef std::map <size_t, std::vector <JsonNode> > source_map;
typedef std::map<size_t, IImage* > image_map; typedef std::map<size_t, IImage* > image_map;
typedef std::map<size_t, image_map > group_map; typedef std::map<size_t, image_map > group_map;
/// Class for def loading, methods are based on CDefHandler /// Class for def loading
/// After loading will store general info (palette and frame offsets) and pointer to file itself /// After loading will store general info (palette and frame offsets) and pointer to file itself
class CDefFile class CDefFile
{ {
@ -1605,6 +1605,27 @@ size_t CAnimation::size(size_t group) const
return 0; return 0;
} }
void CAnimation::horizontalFlip()
{
for(auto & group : images)
for(auto & image : group.second)
image.second->horizontalFlip();
}
void CAnimation::verticalFlip()
{
for(auto & group : images)
for(auto & image : group.second)
image.second->verticalFlip();
}
void CAnimation::playerColored(PlayerColor player)
{
for(auto & group : images)
for(auto & image : group.second)
image.second->playerColored(player);
}
float CFadeAnimation::initialCounter() const float CFadeAnimation::initialCounter() const
{ {
if (fadingMode == EMode::OUT) if (fadingMode == EMode::OUT)

View File

@ -127,6 +127,10 @@ public:
//total count of frames in group (including not loaded) //total count of frames in group (including not loaded)
size_t size(size_t group=0) const; size_t size(size_t group=0) const;
void horizontalFlip();
void verticalFlip();
void playerColored(PlayerColor player);
}; };
const float DEFAULT_DELTA = 0.05f; const float DEFAULT_DELTA = 0.05f;