Browse Source

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.

toneyisnow 5 years ago
parent
commit
0f3dabab9a

+ 4 - 0
client/CPlayerInterface.cpp

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

+ 6 - 0
client/battle/CBattleAnimations.cpp

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

+ 2 - 0
client/battle/CBattleAnimations.h

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

+ 13 - 0
client/battle/CCreatureAnimation.cpp

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

+ 3 - 0
client/battle/CCreatureAnimation.h

@@ -104,6 +104,9 @@ public:
 	bool incrementFrame(float timePassed);
 	void setBorderColor(SDL_Color palette);
 
+	// tint color effect
+	void shiftColor(ColorShifter* shifter);
+
 	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

+ 56 - 0
client/gui/CAnimation.cpp

@@ -11,6 +11,7 @@
 #include "CAnimation.h"
 
 #include <SDL_image.h>
+#include <SDL.h>
 
 #include "../CBitmapHandler.h"
 #include "../Graphics.h"
@@ -87,6 +88,9 @@ public:
 	SDLImage(SDL_Surface * from, bool extraRef);
 	~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, SDL_Rect * dest, SDL_Rect * src, ui8 alpha=255) const override;
 	std::shared_ptr<IImage> scaleFast(float scale) const override;
@@ -100,10 +104,15 @@ public:
 	void verticalFlip() override;
 
 	void shiftPalette(int from, int howMany) override;
+	void adjustPalette(ColorShifter * shifter) override;
+	void resetPalette() override;
 
 	void setBorderPallete(const BorderPallete & borderPallete) override;
 
 	friend class SDLImageLoader;
+
+private:
+	SDL_Palette * originalPalette;
 };
 
 class SDLImageLoader
@@ -552,6 +561,8 @@ SDLImage::SDLImage(CDefFile * data, size_t frame, size_t group)
 {
 	SDLImageLoader loader(this);
 	data->loadFrame(frame, group, loader);
+
+	savePalette();
 }
 
 SDLImage::SDLImage(SDL_Surface * from, bool extraRef)
@@ -560,6 +571,11 @@ SDLImage::SDLImage(SDL_Surface * from, bool extraRef)
 	fullSize(0, 0)
 {
 	surf = from;
+	if (surf == nullptr)
+		return;
+
+	savePalette();
+
 	if (extraRef)
 		surf->refcount++;
 	fullSize.x = surf->w;
@@ -578,6 +594,8 @@ SDLImage::SDLImage(const JsonNode & conf)
 	if(surf == nullptr)
 		return;
 
+	savePalette();
+
 	const JsonNode & jsonMargins = conf["margins"];
 
 	margins.x = jsonMargins["left"].Integer();
@@ -611,6 +629,7 @@ SDLImage::SDLImage(std::string filename)
 	}
 	else
 	{
+		savePalette();
 		fullSize.x = surf->w;
 		fullSize.y = surf->h;
 	}
@@ -736,6 +755,13 @@ void SDLImage::verticalFlip()
 	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)
 {
 	//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)
 {
 	if(surf->format->palette)
@@ -1014,6 +1058,18 @@ void CAnimation::duplicateImage(const size_t sourceGroup, const size_t sourceFra
 		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)
 {
 	if (source[group].size() <= frame)

+ 6 - 0
client/gui/CAnimation.h

@@ -16,6 +16,7 @@
 struct SDL_Surface;
 class JsonNode;
 class CDefFile;
+class ColorShifter;
 
 /*
  * Base class for images, can be used for non-animation pictures as well
@@ -44,6 +45,8 @@ public:
 
 	//only indexed bitmaps, 16 colors maximum
 	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
 	virtual void setBorderPallete(const BorderPallete & borderPallete) = 0;
@@ -98,6 +101,9 @@ public:
 	//and loads it if animation is preloaded
 	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.
 	void setCustom(std::string filename, size_t frame, size_t group=0);
 

+ 51 - 0
client/gui/SDL_Extensions.h

@@ -152,6 +152,57 @@ struct ColorPutter
 
 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
 {
 	/// helper that will safely set and un-set ClipRect for SDL_Surface

+ 1 - 1
lib/spells/effects/Clone.cpp

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