1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-26 22:57:00 +02:00

Initialize the change:

1. Make color shifter for CAnimation, and use that in the clone effect.
2. Update the original position of the cloned object.
This commit is contained in:
toneyisnow 2020-01-25 01:21:26 -08:00
parent 8341ba3c27
commit 0f3dabab9a
9 changed files with 142 additions and 1 deletions

View File

@ -739,6 +739,10 @@ void CPlayerInterface::battleUnitsChanged(const std::vector<UnitChanges> & units
if(unit->alive() && animation->isDead()) if(unit->alive() && animation->isDead())
animation->setType(CCreatureAnim::HOLDING); animation->setType(CCreatureAnim::HOLDING);
if (unit->isClone())
{
animation->shiftColor(ColorShifterDeepBlue::create());
}
//TODO: handle more cases //TODO: handle more cases
} }
break; break;

View File

@ -90,6 +90,12 @@ CBattleStackAnimation::CBattleStackAnimation(CBattleInterface * owner, const CSt
assert(myAnim); assert(myAnim);
} }
void CBattleStackAnimation::shiftColor(ColorShifter * shifter)
{
assert(myAnim);
myAnim->shiftColor(shifter);
}
void CAttackAnimation::nextFrame() void CAttackAnimation::nextFrame()
{ {
if(myAnim->getType() != group) if(myAnim->getType() != group)

View File

@ -43,6 +43,8 @@ public:
const CStack * stack; //id of stack whose animation it is const CStack * stack; //id of stack whose animation it is
CBattleStackAnimation(CBattleInterface * _owner, const CStack * _stack); CBattleStackAnimation(CBattleInterface * _owner, const CStack * _stack);
void shiftColor(ColorShifter * shifter);
}; };
/// This class is responsible for managing the battle attack animation /// This class is responsible for managing the battle attack animation

View File

@ -142,6 +142,19 @@ void CCreatureAnimation::setType(CCreatureAnim::EAnimType type)
play(); play();
} }
void CCreatureAnimation::shiftColor(ColorShifter * shifter)
{
if (forward)
{
forward->shiftColor(shifter);
}
if (reverse)
{
reverse->shiftColor(shifter);
}
}
CCreatureAnimation::CCreatureAnimation(const std::string & name_, TSpeedController controller) CCreatureAnimation::CCreatureAnimation(const std::string & name_, TSpeedController controller)
: name(name_), : name(name_),
speed(0.1), speed(0.1),

View File

@ -104,6 +104,9 @@ public:
bool incrementFrame(float timePassed); bool incrementFrame(float timePassed);
void setBorderColor(SDL_Color palette); void setBorderColor(SDL_Color palette);
// tint color effect
void shiftColor(ColorShifter* shifter);
float getCurrentFrame() const; // Gets the current frame ID relative to frame group. float getCurrentFrame() const; // Gets the current frame ID relative to frame group.
void playOnce(CCreatureAnim::EAnimType type); //plays once given stage of animation, then resets to 2 void playOnce(CCreatureAnim::EAnimType type); //plays once given stage of animation, then resets to 2

View File

@ -11,6 +11,7 @@
#include "CAnimation.h" #include "CAnimation.h"
#include <SDL_image.h> #include <SDL_image.h>
#include <SDL.h>
#include "../CBitmapHandler.h" #include "../CBitmapHandler.h"
#include "../Graphics.h" #include "../Graphics.h"
@ -87,6 +88,9 @@ public:
SDLImage(SDL_Surface * from, bool extraRef); SDLImage(SDL_Surface * from, bool extraRef);
~SDLImage(); ~SDLImage();
// Keep the original palette, in order to do color switching operation
void savePalette();
void draw(SDL_Surface * where, int posX=0, int posY=0, Rect *src=nullptr, ui8 alpha=255) const override; void draw(SDL_Surface * where, int posX=0, int posY=0, Rect *src=nullptr, ui8 alpha=255) const override;
void draw(SDL_Surface * where, SDL_Rect * dest, SDL_Rect * src, ui8 alpha=255) const override; void draw(SDL_Surface * where, SDL_Rect * dest, SDL_Rect * src, ui8 alpha=255) const override;
std::shared_ptr<IImage> scaleFast(float scale) const override; std::shared_ptr<IImage> scaleFast(float scale) const override;
@ -100,10 +104,15 @@ public:
void verticalFlip() override; void verticalFlip() override;
void shiftPalette(int from, int howMany) override; void shiftPalette(int from, int howMany) override;
void adjustPalette(ColorShifter * shifter) override;
void resetPalette() override;
void setBorderPallete(const BorderPallete & borderPallete) override; void setBorderPallete(const BorderPallete & borderPallete) override;
friend class SDLImageLoader; friend class SDLImageLoader;
private:
SDL_Palette * originalPalette;
}; };
class SDLImageLoader class SDLImageLoader
@ -552,6 +561,8 @@ SDLImage::SDLImage(CDefFile * data, size_t frame, size_t group)
{ {
SDLImageLoader loader(this); SDLImageLoader loader(this);
data->loadFrame(frame, group, loader); data->loadFrame(frame, group, loader);
savePalette();
} }
SDLImage::SDLImage(SDL_Surface * from, bool extraRef) SDLImage::SDLImage(SDL_Surface * from, bool extraRef)
@ -560,6 +571,11 @@ SDLImage::SDLImage(SDL_Surface * from, bool extraRef)
fullSize(0, 0) fullSize(0, 0)
{ {
surf = from; surf = from;
if (surf == nullptr)
return;
savePalette();
if (extraRef) if (extraRef)
surf->refcount++; surf->refcount++;
fullSize.x = surf->w; fullSize.x = surf->w;
@ -578,6 +594,8 @@ SDLImage::SDLImage(const JsonNode & conf)
if(surf == nullptr) if(surf == nullptr)
return; return;
savePalette();
const JsonNode & jsonMargins = conf["margins"]; const JsonNode & jsonMargins = conf["margins"];
margins.x = jsonMargins["left"].Integer(); margins.x = jsonMargins["left"].Integer();
@ -611,6 +629,7 @@ SDLImage::SDLImage(std::string filename)
} }
else else
{ {
savePalette();
fullSize.x = surf->w; fullSize.x = surf->w;
fullSize.y = surf->h; fullSize.y = surf->h;
} }
@ -736,6 +755,13 @@ void SDLImage::verticalFlip()
surf = flipped; surf = flipped;
} }
// Keep the original palette, in order to do color switching operation
void SDLImage::savePalette()
{
originalPalette = new SDL_Palette();
memcpy(originalPalette, surf->format->palette, sizeof(SDL_Palette));
}
void SDLImage::shiftPalette(int from, int howMany) void SDLImage::shiftPalette(int from, int howMany)
{ {
//works with at most 16 colors, if needed more -> increase values //works with at most 16 colors, if needed more -> increase values
@ -753,6 +779,24 @@ void SDLImage::shiftPalette(int from, int howMany)
} }
} }
void SDLImage::adjustPalette(ColorShifter * shifter)
{
SDL_Palette* palette = surf->format->palette;
for (int i = 0; i < 255; i++)
{
palette->colors[i] = shifter->shiftColor(originalPalette->colors[i]);
}
}
void SDLImage::resetPalette()
{
SDL_Palette * pal = new SDL_Palette();
memcpy(pal, originalPalette, sizeof(SDL_Palette));
// Always keept the original palette not changed, copy a new palette to assign to surface
SDL_SetPaletteColors(surf->format->palette, originalPalette->colors, 0, 255);
}
void SDLImage::setBorderPallete(const IImage::BorderPallete & borderPallete) void SDLImage::setBorderPallete(const IImage::BorderPallete & borderPallete)
{ {
if(surf->format->palette) if(surf->format->palette)
@ -1014,6 +1058,18 @@ void CAnimation::duplicateImage(const size_t sourceGroup, const size_t sourceFra
load(index, targetGroup); load(index, targetGroup);
} }
void CAnimation::shiftColor(ColorShifter * shifter)
{
for (auto groupIter = images.begin(); groupIter != images.end(); groupIter++)
{
for (auto frameIter = groupIter->second.begin(); frameIter != groupIter->second.end(); frameIter++)
{
std::shared_ptr<IImage> image = frameIter->second;
image->adjustPalette(shifter);
}
}
}
void CAnimation::setCustom(std::string filename, size_t frame, size_t group) void CAnimation::setCustom(std::string filename, size_t frame, size_t group)
{ {
if (source[group].size() <= frame) if (source[group].size() <= frame)

View File

@ -16,6 +16,7 @@
struct SDL_Surface; struct SDL_Surface;
class JsonNode; class JsonNode;
class CDefFile; class CDefFile;
class ColorShifter;
/* /*
* Base class for images, can be used for non-animation pictures as well * Base class for images, can be used for non-animation pictures as well
@ -44,6 +45,8 @@ public:
//only indexed bitmaps, 16 colors maximum //only indexed bitmaps, 16 colors maximum
virtual void shiftPalette(int from, int howMany) = 0; virtual void shiftPalette(int from, int howMany) = 0;
virtual void adjustPalette(ColorShifter * shifter) = 0;
virtual void resetPalette() = 0;
//only indexed bitmaps, colors 5,6,7 must be special //only indexed bitmaps, colors 5,6,7 must be special
virtual void setBorderPallete(const BorderPallete & borderPallete) = 0; virtual void setBorderPallete(const BorderPallete & borderPallete) = 0;
@ -98,6 +101,9 @@ public:
//and loads it if animation is preloaded //and loads it if animation is preloaded
void duplicateImage(const size_t sourceGroup, const size_t sourceFrame, const size_t targetGroup); void duplicateImage(const size_t sourceGroup, const size_t sourceFrame, const size_t targetGroup);
// adjust the color of the animation, used in battle spell effects, e.g. Cloned objects
void shiftColor(ColorShifter* shifter);
//add custom surface to the selected position. //add custom surface to the selected position.
void setCustom(std::string filename, size_t frame, size_t group=0); void setCustom(std::string filename, size_t frame, size_t group=0);

View File

@ -152,6 +152,57 @@ struct ColorPutter
typedef void (*BlitterWithRotationVal)(SDL_Surface *src,SDL_Rect srcRect, SDL_Surface * dst, SDL_Rect dstRect, ui8 rotation); typedef void (*BlitterWithRotationVal)(SDL_Surface *src,SDL_Rect srcRect, SDL_Surface * dst, SDL_Rect dstRect, ui8 rotation);
class ColorShifter
{
public:
virtual SDL_Color shiftColor(SDL_Color clr) = 0;
};
class ColorShifterLightBlue : public ColorShifter
{
public:
static ColorShifterLightBlue* create()
{
return new ColorShifterLightBlue();
}
SDL_Color shiftColor(SDL_Color clr) override
{
clr.b = clr.b + (255 - clr.b) / 2;
return clr;
}
};
class ColorShifterDeepBlue : public ColorShifter
{
public:
static ColorShifterDeepBlue* create()
{
return new ColorShifterDeepBlue();
}
SDL_Color shiftColor(SDL_Color clr) override
{
clr.b = 255;
return clr;
}
};
class ColorShifterDeepRed : public ColorShifter
{
public:
static ColorShifterDeepRed* create()
{
return new ColorShifterDeepRed();
}
SDL_Color shiftColor(SDL_Color clr) override
{
clr.r = 255;
return clr;
}
};
namespace CSDL_Ext namespace CSDL_Ext
{ {
/// helper that will safely set and un-set ClipRect for SDL_Surface /// helper that will safely set and un-set ClipRect for SDL_Surface

View File

@ -51,7 +51,7 @@ void Clone::apply(BattleStateProxy * battleState, RNG & rng, const Mechanics * m
if(clonedStack->getCount() < 1) if(clonedStack->getCount() < 1)
continue; continue;
auto hex = m->cb->getAvaliableHex(clonedStack->creatureId(), m->casterSide); auto hex = m->cb->getAvaliableHex(clonedStack->creatureId(), m->casterSide, clonedStack->getPosition());
if(!hex.isValid()) if(!hex.isValid())
{ {