瀏覽代碼

Allow CAnimImage to share animation object.

AlexVinS 9 年之前
父節點
當前提交
3a52a3c663

+ 1 - 1
client/CPreGame.cpp

@@ -3662,7 +3662,7 @@ void CBonusSelection::updateBonusSelection()
 		if (picNumber != -1)
 			picName += ":" + boost::lexical_cast<std::string>(picNumber);
 
-		auto   anim = new CAnimation();
+		auto anim = std::make_shared<CAnimation>();
 		anim->setCustom(picName, 0);
 		bonusButton->setImage(anim);
 		const SDL_Color brightYellow = { 242, 226, 110, 0 };

+ 1 - 1
client/gui/CAnimation.cpp

@@ -1078,7 +1078,7 @@ CAnimation::~CAnimation()
 {
 	if (!images.empty())
 	{
-		//logGlobal->warnStream()<<"Warning: not all frames were unloaded from "<<name;
+		logGlobal->warnStream()<<"Warning: not all frames were unloaded from "<<name;
 		for (auto & elem : images)
 			for (auto & _image : elem.second)
 				delete _image.second;

+ 5 - 4
client/widgets/Buttons.cpp

@@ -272,10 +272,11 @@ void CButton::setIndex(size_t index, bool playerColoredButton)
 	if (index == currentImage || index>=imageNames.size())
 		return;
 	currentImage = index;
-	setImage(new CAnimation(imageNames[index]), playerColoredButton);
+	auto anim = std::make_shared<CAnimation>(imageNames[index]);
+	setImage(anim, playerColoredButton);
 }
 
-void CButton::setImage(CAnimation* anim, bool playerColoredButton, int animFlags)
+void CButton::setImage(std::shared_ptr<CAnimation> anim, bool playerColoredButton, int animFlags)
 {
 	OBJ_CONSTRUCTION_CAPTURING_ALL;
 
@@ -294,7 +295,7 @@ void CButton::setPlayerColor(PlayerColor player)
 void CButton::showAll(SDL_Surface * to)
 {
 	CIntObject::showAll(to);
-	
+
 	if (borderColor && borderColor->a == 0)
 		CSDL_Ext::drawBorder(to, pos.x-1, pos.y-1, pos.w+2, pos.h+2, int3(borderColor->r, borderColor->g, borderColor->b));
 }
@@ -453,7 +454,7 @@ CVolumeSlider::CVolumeSlider(const Point &position, const std::string &defName,
 	helpHandlers(help)
 {
 	OBJ_CONSTRUCTION_CAPTURING_ALL;
-	animImage = new CAnimImage(new CAnimation(defName), 0, 0, position.x, position.y),
+	animImage = new CAnimImage(std::make_shared<CAnimation>(defName), 0, 0, position.x, position.y),
 	assert(!defName.empty());
 	addUsedEvents(LCLICK | RCLICK | WHEEL);
 	pos.x += position.x;

+ 1 - 1
client/widgets/Buttons.h

@@ -110,7 +110,7 @@ public:
 
 	/// Appearance modifiers
 	void setIndex(size_t index, bool playerColoredButton=false);
-	void setImage(CAnimation* anim, bool playerColoredButton=false, int animFlags=0);
+	void setImage(std::shared_ptr<CAnimation> anim, bool playerColoredButton=false, int animFlags=0);
 	void setPlayerColor(PlayerColor player);
 
 	/// CIntObject overrides

+ 3 - 4
client/widgets/Images.cpp

@@ -210,7 +210,7 @@ void CFilledTexture::showAll(SDL_Surface *to)
 	CSDL_Ext::fillTexture(to, texture);
 }
 
-CAnimImage::CAnimImage(std::string name, size_t Frame, size_t Group, int x, int y, ui8 Flags):
+CAnimImage::CAnimImage(const std::string & name, size_t Frame, size_t Group, int x, int y, ui8 Flags):
 	frame(Frame),
 	group(Group),
 	player(-1),
@@ -218,11 +218,11 @@ CAnimImage::CAnimImage(std::string name, size_t Frame, size_t Group, int x, int
 {
 	pos.x += x;
 	pos.y += y;
-	anim = new CAnimation(name);
+	anim = std::make_shared<CAnimation>(name);
 	init();
 }
 
-CAnimImage::CAnimImage(CAnimation *Anim, size_t Frame, size_t Group, int x, int y, ui8 Flags):
+CAnimImage::CAnimImage(std::shared_ptr<CAnimation> Anim, size_t Frame, size_t Group, int x, int y, ui8 Flags):
 	anim(Anim),
 	frame(Frame),
 	group(Group),
@@ -259,7 +259,6 @@ CAnimImage::~CAnimImage()
 	anim->unload(frame, group);
 	if (flags & CShowableAnim::BASE)
 		anim->unload(0,group);
-	delete anim;
 }
 
 void CAnimImage::showAll(SDL_Surface * to)

+ 3 - 3
client/widgets/Images.h

@@ -70,7 +70,7 @@ public:
 class CAnimImage: public CIntObject
 {
 private:
-	CAnimation* anim;
+	std::shared_ptr<CAnimation> anim;
 	//displayed frame/group
 	size_t frame;
 	size_t group;
@@ -82,8 +82,8 @@ private:
 public:
 	bool visible;
 
-	CAnimImage(std::string name, size_t Frame, size_t Group=0, int x=0, int y=0, ui8 Flags=0);
-	CAnimImage(CAnimation* anim, size_t Frame, size_t Group=0, int x=0, int y=0, ui8 Flags=0);
+	CAnimImage(const std::string & name, size_t Frame, size_t Group=0, int x=0, int y=0, ui8 Flags=0);
+	CAnimImage(std::shared_ptr<CAnimation> Anim, size_t Frame, size_t Group=0, int x=0, int y=0, ui8 Flags=0);
 	~CAnimImage();//d-tor
 
 	//size of animation

+ 10 - 3
client/windows/CSpellWindow.cpp

@@ -174,7 +174,7 @@ CSpellWindow::CSpellWindow(const CGHeroInstance * _myHero, CPlayerInterface * _m
 	leftCorner = BitmapHandler::loadBitmap("SpelTrnL.bmp", true);
 	rightCorner = BitmapHandler::loadBitmap("SpelTrnR.bmp", true);
 
-	spells = new CAnimation("Spells.def");
+	spells = std::make_shared<CAnimation>("Spells");
 
 	spellTab = CDefHandler::giveDef("SpelTab.def");
 	schools = CDefHandler::giveDef("Schools.def");
@@ -247,10 +247,9 @@ CSpellWindow::CSpellWindow(const CGHeroInstance * _myHero, CPlayerInterface * _m
 
 CSpellWindow::~CSpellWindow()
 {
+	spells->unload();
 	SDL_FreeSurface(leftCorner);
 	SDL_FreeSurface(rightCorner);
-	spells->unload();
-	delete spells;
 	delete spellTab;
 	delete schools;
 	for(auto & elem : schoolBorders)
@@ -523,6 +522,12 @@ CSpellWindow::SpellArea::SpellArea(SDL_Rect pos, CSpellWindow * owner)
 	icon = nullptr;
 }
 
+CSpellWindow::SpellArea::~SpellArea()
+{
+	if(icon)
+		icon->decreaseRef();
+}
+
 void CSpellWindow::SpellArea::clickLeft(tribool down, bool previousState)
 {
 	if(!down && mySpell)
@@ -818,6 +823,8 @@ void CSpellWindow::SpellArea::showAll(SDL_Surface * to)
 
 void CSpellWindow::SpellArea::setSpell(const CSpell * spell)
 {
+	if(icon)
+		icon->decreaseRef();
 	icon = nullptr;
 	mySpell = spell;
 	if(mySpell)

+ 2 - 2
client/windows/CSpellWindow.h

@@ -37,7 +37,7 @@ private:
 		IImage * icon;
 
 		SpellArea(SDL_Rect pos, CSpellWindow * owner);
-
+		~SpellArea();
 		void setSpell(const CSpell * spell);
 
 		void clickLeft(tribool down, bool previousState) override;
@@ -66,7 +66,7 @@ private:
 
 	SDL_Rect lCorner, rCorner;
 
-	CAnimation * spells; //pictures of spells
+	std::shared_ptr<CAnimation> spells; //pictures of spells
 
 	CDefHandler	* spellTab, //school select
 		* schools, //schools' pictures