Browse Source

Mao object drawing converted to CAnimation

AlexVinS 9 years ago
parent
commit
186cd7a163
6 changed files with 509 additions and 111 deletions
  1. 194 24
      client/Graphics.cpp
  2. 37 10
      client/Graphics.h
  3. 130 9
      client/gui/CAnimation.cpp
  4. 25 0
      client/gui/CAnimation.h
  5. 102 54
      client/mapHandler.cpp
  6. 21 14
      client/mapHandler.h

+ 194 - 24
client/Graphics.cpp

@@ -165,7 +165,9 @@ void Graphics::load()
 	heroMoveArrows = std::make_shared<CAnimation>("ADAG");
 	heroMoveArrows->preload();
 
-	loadHeroAnims();
+	loadHeroAnims();//todo: remove
+	loadHeroAnimations();
+	loadHeroFlagAnimations();
 }
 
 void Graphics::loadHeroAnims()
@@ -191,6 +193,87 @@ void Graphics::loadHeroAnims()
 	boatAnims.push_back(loadHeroAnim("AB03_.DEF", rotations));
 }
 
+void Graphics::loadHeroAnimations()
+{
+	for(auto & elem : CGI->heroh->classes.heroClasses)
+	{
+		for (auto & templ : VLC->objtypeh->getHandlerFor(Obj::HERO, elem->id)->getTemplates())
+		{
+			if (!heroAnimations.count(templ.animationFile))
+				heroAnimations[templ.animationFile] = loadHeroAnimation(templ.animationFile);
+		}
+	}
+
+	boatAnimations[0] = loadHeroAnimation("AB01_.DEF");
+	boatAnimations[1] = loadHeroAnimation("AB02_.DEF");
+	boatAnimations[2] = loadHeroAnimation("AB03_.DEF");
+
+
+	mapObjectAnimations["AB01_.DEF"] = boatAnimations[0];
+	mapObjectAnimations["AB02_.DEF"] = boatAnimations[1];
+	mapObjectAnimations["AB03_.DEF"] = boatAnimations[2];
+}
+void Graphics::loadHeroFlagAnimations()
+{
+	static const std::vector<std::string> HERO_FLAG_ANIMATIONS =
+	{
+		"AF00", "AF01","AF02","AF03",
+		"AF04", "AF05","AF06","AF07"
+	};
+
+	static const std::vector< std::vector<std::string> > BOAT_FLAG_ANIMATIONS =
+	{
+		{
+			"ABF01L", "ABF01G", "ABF01R", "ABF01D",
+			"ABF01B", "ABF01P", "ABF01W", "ABF01K"
+		},
+		{
+			"ABF02L", "ABF02G", "ABF02R", "ABF02D",
+			"ABF02B", "ABF02P", "ABF02W", "ABF02K"
+		},
+		{
+			"ABF03L", "ABF03G", "ABF03R", "ABF03D",
+			"ABF03B", "ABF03P", "ABF03W", "ABF03K"
+		}
+	};
+
+	for(const auto & name : HERO_FLAG_ANIMATIONS)
+		heroFlagAnimations.push_back(loadHeroFlagAnimation(name));
+
+	for(int i = 0; i < BOAT_FLAG_ANIMATIONS.size(); i++)
+		for(const auto & name : BOAT_FLAG_ANIMATIONS[i])
+			boatFlagAnimations[i].push_back(loadHeroFlagAnimation(name));
+}
+
+std::shared_ptr<CAnimation> Graphics::loadHeroFlagAnimation(const std::string & name)
+{
+	//first - group number to be rotated, second - group number after rotation
+	static const std::vector<std::pair<int,int> > rotations =
+	{
+		{6,10}, {7,11}, {8,12}, {1,13},
+		{2,14}, {3,15}
+	};
+
+	std::shared_ptr<CAnimation> anim = std::make_shared<CAnimation>(name);
+	anim->preload();
+
+	for(const auto & rotation : rotations)
+	{
+        const int sourceGroup = rotation.first;
+        const int targetGroup = rotation.second;
+
+        for(size_t frame = 0; frame < anim->size(sourceGroup); ++frame)
+		{
+			anim->duplicateImage(sourceGroup, frame, targetGroup);
+
+			IImage * image = anim->getImage(frame, targetGroup);
+			image->verticalFlip();
+		}
+	}
+
+	return anim;
+}
+
 CDefEssential * Graphics::loadHeroAnim( const std::string &name, const std::vector<std::pair<int,int> > &rotations)
 {
 	CDefEssential *anim = CDefHandler::giveDefEss(name);
@@ -228,7 +311,68 @@ CDefEssential * Graphics::loadHeroAnim( const std::string &name, const std::vect
 	return anim;
 }
 
-void Graphics::loadHeroFlagsDetail(std::pair<std::vector<CDefEssential *> Graphics::*, std::vector<const char *> > &pr, bool mode)
+std::shared_ptr<CAnimation> Graphics::loadHeroAnimation(const std::string &name)
+{
+	//first - group number to be rotated, second - group number after rotation
+	static const std::vector<std::pair<int,int> > rotations =
+	{
+		{6,10}, {7,11}, {8,12}, {1,13},
+		{2,14}, {3,15}
+	};
+
+	std::shared_ptr<CAnimation> anim = std::make_shared<CAnimation>(name);
+	anim->preload();
+
+
+	for(const auto & rotation : rotations)
+	{
+        const int sourceGroup = rotation.first;
+        const int targetGroup = rotation.second;
+
+        for(size_t frame = 0; frame < anim->size(sourceGroup); ++frame)
+		{
+			anim->duplicateImage(sourceGroup, frame, targetGroup);
+
+			IImage * image = anim->getImage(frame, targetGroup);
+			image->verticalFlip();
+		}
+	}
+
+	//todo: apply following commented out optimizations or merge with loadHeroFlagAnimation
+
+//	int pom = 0; //how many groups has been rotated
+//	for(int o = 7; pom < 6; ++o)
+//	{
+//		for(int p = 0; p<6; p++)
+//		{
+//			IImage frame = anim->getImage()
+//			if(anim->ourImages[o].groupNumber == rotations[p].first)
+//			{
+//				for(int e=0; e<8; ++e)
+//				{
+//					Cimage nci;
+//					nci.bitmap = CSDL_Ext::verticalFlip(anim->ourImages[o+e].bitmap);
+//					nci.groupNumber = rotations[p].second;
+//					nci.imName = std::string();
+//					anim->ourImages.push_back(nci);
+//					if(pom>2) //we need only one frame for groups 13/14/15
+//						break;
+//				}
+//				if(pom<3) //there are eight frames of animtion of groups 6/7/8 so for speed we'll skip them
+//					o+=8;
+//				else //there is only one frame of 1/2/3
+//					o+=1;
+//				++pom;
+//				if(p==2 && pom<4) //group1 starts at index 1
+//					o = 1;
+//			}
+//		}
+//	}
+
+	return anim;
+}
+
+void Graphics::loadHeroFlagsDetail(std::pair<std::vector<CDefEssential *> Graphics::*, std::vector<const char *> > &pr)
 {
 	for(int i=0;i<8;i++)
 		(this->*pr.first).push_back(CDefHandler::giveDefEss(pr.second[i]));
@@ -259,24 +403,23 @@ void Graphics::loadHeroFlagsDetail(std::pair<std::vector<CDefEssential *> Graphi
 				}
 			}
 		}
-		if (mode)
+
+		for(size_t o=0; o<curImgs.size(); ++o)
 		{
-			for(size_t o=0; o<curImgs.size(); ++o)
+			if(curImgs[o].groupNumber==1 || curImgs[o].groupNumber==2 || curImgs[o].groupNumber==3)
 			{
-				if(curImgs[o].groupNumber==1 || curImgs[o].groupNumber==2 || curImgs[o].groupNumber==3)
+				for(int e=0; e<8; ++e)
 				{
-					for(int e=0; e<8; ++e)
-					{
-						Cimage nci;
-						nci.bitmap = CSDL_Ext::verticalFlip(curImgs[o+e].bitmap);
-						nci.groupNumber = 12 + curImgs[o].groupNumber;
-						nci.imName = std::string();
-						curImgs.push_back(nci);
-					}
-					o+=8;
+					Cimage nci;
+					nci.bitmap = CSDL_Ext::verticalFlip(curImgs[o+e].bitmap);
+					nci.groupNumber = 12 + curImgs[o].groupNumber;
+					nci.imName = std::string();
+					curImgs.push_back(nci);
 				}
+				o+=8;
 			}
 		}
+
 		for(auto & curImg : curImgs)
 		{
 			CSDL_Ext::setDefaultColorKey(curImg.bitmap);
@@ -313,19 +456,11 @@ void Graphics::loadHeroFlags()
 		}
 	};
 
-	#if 0
-	boost::thread_group grupa;
-	for(int g=3; g>=0; --g)
-	{
-		grupa.create_thread(std::bind(&Graphics::loadHeroFlagsDetail, this, std::ref(pr[g]), true));
-	}
-	grupa.join_all();
-	#else
 	for(auto p: pr)
 	{
-		loadHeroFlagsDetail(p,true);
+		loadHeroFlagsDetail(p);
 	}
-	#endif
+
 	logGlobal->infoStream() << "Loading and transforming heroes' flags: "<<th.getDiff();
 }
 
@@ -402,6 +537,41 @@ CDefEssential * Graphics::getDef( const ObjectTemplate & info )
 	return advmapobjGraphics[info.animationFile];
 }
 
+std::shared_ptr<CAnimation> Graphics::getAnimation(const CGObjectInstance* obj)
+{
+	return getAnimation(obj->appearance);
+}
+
+std::shared_ptr<CAnimation> Graphics::getAnimation(const ObjectTemplate & info)
+{
+	//the only(?) invisible object
+	if(info.id == Obj::EVENT)
+	{
+		return std::shared_ptr<CAnimation>();
+	}
+
+	if(info.animationFile.empty())
+	{
+		logGlobal->warnStream() << boost::format("Def name for obj (%d,%d) is empty!") % info.id % info.subid;
+		return std::shared_ptr<CAnimation>();
+	}
+
+	std::shared_ptr<CAnimation> ret = mapObjectAnimations[info.animationFile];
+
+	//already loaded
+	if(ret)
+	{
+		ret->preload();
+		return ret;
+	}
+
+	ret = std::make_shared<CAnimation>(info.animationFile);
+	mapObjectAnimations[info.animationFile] = ret;
+
+	ret->preload();
+	return ret;
+}
+
 void Graphics::loadErmuToPicture()
 {
 	//loading ERMU to picture

+ 37 - 10
client/Graphics.h

@@ -40,10 +40,21 @@ class Graphics
 
 	void initializeBattleGraphics();
 	void loadPaletteAndColors();
-	void loadHeroFlags();
-	void loadHeroFlagsDetail(std::pair<std::vector<CDefEssential *> Graphics::*, std::vector<const char *> > &pr, bool mode);
-	void loadHeroAnims();
-	CDefEssential *  loadHeroAnim(const std::string &name, const std::vector<std::pair<int,int> > &rotations);
+	void loadHeroFlags(); //deprecated
+	void loadHeroFlagsDetail(std::pair<std::vector<CDefEssential *> Graphics::*, std::vector<const char *> > &pr); //deprecated
+	void loadHeroAnims();//deprecated
+
+	CDefEssential *  loadHeroAnim(const std::string &name, const std::vector<std::pair<int,int> > &rotations);//deprecated
+
+	void loadHeroAnimations();
+	//loads animation and adds required rotated frames
+	std::shared_ptr<CAnimation> loadHeroAnimation(const std::string &name);
+
+	void loadHeroFlagAnimations();
+
+	//loads animation and adds required rotated frames
+	std::shared_ptr<CAnimation> loadHeroFlagAnimation(const std::string &name);
+
 	void loadErmuToPicture();
 
 	void loadFonts();
@@ -60,18 +71,31 @@ public:
 	SDL_Color * playerColorPalette; //palette to make interface colors good - array of size [256]
 	SDL_Color * neutralColorPalette;
 
-	std::vector<CDefEssential *> flags1, flags2, flags3, flags4; //flags blitted on heroes when ,
+	std::vector<CDefEssential *> flags1, flags2, flags3, flags4; //DEPRECATED flags blitted on heroes when ,
 	std::shared_ptr<CAnimation> heroMoveArrows;
-	std::map<std::string, CDefEssential *> heroAnims; // [hero class def name]  //added group 10: up - left, 11 - left and 12 - left down // 13 - up-left standing; 14 - left standing; 15 - left down standing
-	std::vector<CDefEssential *> boatAnims; // [boat type: 0 - 3]  //added group 10: up - left, 11 - left and 12 - left down // 13 - up-left standing; 14 - left standing; 15 - left down standing
+
+	// [hero class def name]  //added group 10: up - left, 11 - left and 12 - left down // 13 - up-left standing; 14 - left standing; 15 - left down standing
+	std::map< std::string, std::shared_ptr<CAnimation> > heroAnimations;
+	std::vector< std::shared_ptr<CAnimation> > heroFlagAnimations;
+
+	// [boat type: 0 .. 2]  //added group 10: up - left, 11 - left and 12 - left down // 13 - up-left standing; 14 - left standing; 15 - left down standing
+	std::array< std::shared_ptr<CAnimation>, 3> boatAnimations;
+
+	std::array< std::vector<std::shared_ptr<CAnimation> >, 3> boatFlagAnimations;
+
+	//all other objects (not hero or boat)
+	std::map< std::string, std::shared_ptr<CAnimation> > mapObjectAnimations;
+
+	std::map<std::string, CDefEssential *> heroAnims; // DEPRECATED [hero class def name]  //added group 10: up - left, 11 - left and 12 - left down // 13 - up-left standing; 14 - left standing; 15 - left down standing
+	std::vector<CDefEssential *> boatAnims; // DEPRECATED [boat type: 0 - 3]  //added group 10: up - left, 11 - left and 12 - left down // 13 - up-left standing; 14 - left standing; 15 - left down standing
 	CDefHandler * FoWfullHide; //for Fog of War
 	CDefHandler * FoWpartialHide; //for For of War
 
 	std::map<std::string, JsonNode> imageLists;
 
-	std::map<std::string, CDefEssential *> advmapobjGraphics;
-	CDefEssential * getDef(const CGObjectInstance * obj);
-	CDefEssential * getDef(const ObjectTemplate & info);
+	std::map<std::string, CDefEssential *> advmapobjGraphics; // DEPRECATED
+	CDefEssential * getDef(const CGObjectInstance * obj); // DEPRECATED
+	CDefEssential * getDef(const ObjectTemplate & info); // DEPRECATED
 	//towns
 	std::map<int, std::string> ERMUtoPicture[GameConstants::F_NUMBER]; //maps building ID to it's picture's name for each town type
 	//for battles
@@ -83,6 +107,9 @@ public:
 	void load();
 
 	void blueToPlayersAdv(SDL_Surface * sur, PlayerColor player); //replaces blue interface colour with a color of player
+
+	std::shared_ptr<CAnimation> getAnimation(const CGObjectInstance * obj);
+	std::shared_ptr<CAnimation> getAnimation(const ObjectTemplate & info);
 };
 
 extern Graphics * graphics;

+ 130 - 9
client/gui/CAnimation.cpp

@@ -572,6 +572,26 @@ IImage::IImage():
 
 }
 
+void IImage::draw(SDL_Surface * where, SDL_Rect * dest, SDL_Rect * src) const
+{
+	int x = 0, y = 0;
+	if(dest)
+	{
+		x = dest->x;
+		y = dest->y;
+	}
+
+	if(src)
+	{
+		Rect srcTemp(*src);
+		draw(where, x, y, &srcTemp);
+	}
+	else
+	{
+		draw(where, x, y);
+	}
+}
+
 bool IImage::decreaseRef()
 {
 	refCount--;
@@ -636,15 +656,45 @@ void SDLImage::draw(SDL_Surface *where, int posX, int posY, Rect *src, ui8 alpha
 	if (!surf)
 		return;
 
-	Rect sourceRect(margins.x, margins.y, surf->w, surf->h);
-	//TODO: rotation and scaling
-	if (src)
+	Rect destRect(posX, posY, surf->w, surf->h);
+
+	draw(where, &destRect, src);
+}
+
+void SDLImage::draw(SDL_Surface* where, SDL_Rect* dest, SDL_Rect* src) const
+{
+	if (!surf)
+		return;
+
+	Rect sourceRect(0, 0, surf->w, surf->h);
+
+	Point destShift(0,0);
+
+	if(src)
 	{
-		sourceRect = sourceRect & *src;
+		if(src->x < margins.x)
+			destShift.x += margins.x - src->x;
+
+		if(src->y < margins.y)
+			destShift.y += margins.y - src->y;
+
+		sourceRect = Rect(*src) & Rect(margins.x, margins.y, surf->w, surf->h);
+
+		sourceRect -= margins;
+	}
+	else
+		destShift = margins;
+
+	Rect destRect(margins.x, margins.y, surf->w, surf->h);
+
+	if(dest)
+	{
+		destRect = *dest;
+
+		destRect = destRect & Rect(destRect.x, destRect.y, sourceRect.w, sourceRect.h);
+
+		destRect += destShift;
 	}
-	Rect destRect(posX, posY, surf->w, surf->h);
-	destRect += sourceRect.topLeft();
-	sourceRect -= margins;
 
 	if(surf->format->BitsPerPixel == 8)
 	{
@@ -662,11 +712,32 @@ void SDLImage::draw(SDL_Surface *where, int posX, int posY, Rect *src, ui8 alpha
 	}
 }
 
+SDL_Surface * SDLImage::scaleFast(float scale) const
+{
+	//todo: margins
+	auto scaled = CSDL_Ext::scaleSurfaceFast(surf, surf->w * scale, surf->h * scale);
+
+	if (scaled->format && scaled->format->palette) // fix color keying, because SDL loses it at this point
+		CSDL_Ext::setColorKey(scaled, scaled->format->palette->colors[0]);
+	else if(scaled->format && scaled->format->Amask)
+		SDL_SetSurfaceBlendMode(scaled, SDL_BLENDMODE_BLEND);//just in case
+	else
+		CSDL_Ext::setDefaultColorKey(scaled);//just in case
+
+	return scaled;
+}
+
 void SDLImage::playerColored(PlayerColor player)
 {
 	graphics->blueToPlayersAdv(surf, player);
 }
 
+void SDLImage::setFlagColor(PlayerColor player)
+{
+	if(player < PlayerColor::PLAYER_LIMIT || player==PlayerColor::NEUTRAL)
+		CSDL_Ext::setPlayerColor(surf, player);
+}
+
 int SDLImage::width() const
 {
 	return fullSize.x;
@@ -677,6 +748,17 @@ int SDLImage::height() const
 	return fullSize.y;
 }
 
+void SDLImage::verticalFlip()
+{
+	SDL_Surface * flipped = CSDL_Ext::verticalFlip(surf);
+
+	SDL_FreeSurface(surf);
+
+	surf = flipped;
+
+	margins.x = fullSize.x - surf->w - margins.x;
+}
+
 SDLImage::~SDLImage()
 {
 	SDL_FreeSurface(surf);
@@ -769,6 +851,15 @@ void CompImage::draw(SDL_Surface *where, int posX, int posY, Rect *src, ui8 alph
 	}
 }
 
+SDL_Surface * CompImage::scaleFast(float scale) const
+{
+	//todo: CompImage::scaleFast
+
+	logAnim->error("CompImage::scaleFast is not implemented");
+
+    return CSDL_Ext::newSurface(width() * scale, height() * scale);
+}
+
 #define CASEBPP(x,y) case x: BlitBlock<x,y>(type, size, data, dest, alpha); break
 
 //FIXME: better way to get blitter
@@ -883,6 +974,11 @@ void CompImage::playerColored(PlayerColor player)
 	}
 }
 
+void CompImage::setFlagColor(PlayerColor player)
+{
+	logGlobal->error("CompImage::setFlagColor is not implemented");
+}
+
 int CompImage::width() const
 {
 	return fullSize.x;
@@ -900,6 +996,12 @@ CompImage::~CompImage()
 	delete [] palette;
 }
 
+void CompImage::verticalFlip()
+{
+	logGlobal->error("CompImage::verticalFlip is not implemented");
+}
+
+
 /*************************************************************************
  *  CAnimation for animations handling, can load part of file if needed  *
  *************************************************************************/
@@ -1105,6 +1207,22 @@ CAnimation::~CAnimation()
 	}
 }
 
+void CAnimation::duplicateImage(const size_t sourceGroup, const size_t sourceFrame, const size_t targetGroup)
+{
+	//todo: clone actual loaded Image object
+	JsonNode clone(source[sourceGroup][sourceFrame]);
+
+	if(clone.getType() == JsonNode::DATA_NULL)
+	{
+        clone["file"].String() = name+":"+boost::lexical_cast<std::string>(sourceGroup)+":"+boost::lexical_cast<std::string>(sourceFrame);
+	}
+
+	source[targetGroup].push_back(clone);
+
+	if(preloaded)
+		load(source[targetGroup].size()-1, targetGroup);
+}
+
 void CAnimation::setCustom(std::string filename, size_t frame, size_t group)
 {
 	if (source[group].size() <= frame)
@@ -1148,8 +1266,11 @@ void CAnimation::unload()
 
 void CAnimation::preload()
 {
-	preloaded = true;
-	load();
+	if(!preloaded)
+	{
+		preloaded = true;
+		load();
+	}
 }
 
 void CAnimation::loadGroup(size_t group)

+ 25 - 0
client/gui/CAnimation.h

@@ -63,6 +63,9 @@ public:
 
 	//draws image on surface "where" at position
 	virtual void draw(SDL_Surface *where, int posX=0, int posY=0, Rect *src=nullptr, ui8 alpha=255) const=0;
+	virtual void draw(SDL_Surface * where, SDL_Rect * dest, SDL_Rect * src) const;
+
+	virtual SDL_Surface * scaleFast(float scale) const = 0;
 
 	//decrease ref count, returns true if image can be deleted (refCount <= 0)
 	bool decreaseRef();
@@ -70,8 +73,15 @@ public:
 
 	//Change palette to specific player
 	virtual void playerColored(PlayerColor player)=0;
+
+	//set special color for flag
+	virtual void setFlagColor(PlayerColor player)=0;
+
 	virtual int width() const=0;
 	virtual int height() const=0;
+
+	virtual void verticalFlip() = 0;
+
 	IImage();
 	virtual ~IImage() {};
 };
@@ -99,11 +109,16 @@ public:
 	~SDLImage();
 
 	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) const override;
+	SDL_Surface * scaleFast(float scale) const override;
 
 	void playerColored(PlayerColor player) override;
+	void setFlagColor(PlayerColor player) override;
 	int width() const override;
 	int height() const override;
 
+	void verticalFlip() override;
+
 	friend class SDLImageLoader;
 };
 
@@ -146,10 +161,16 @@ public:
 	~CompImage();
 
 	void draw(SDL_Surface *where, int posX=0, int posY=0, Rect *src=nullptr, ui8 alpha=255) const override;
+
+	SDL_Surface * scaleFast(float scale) const override;
+
 	void playerColored(PlayerColor player) override;
+	void setFlagColor(PlayerColor player) override;
 	int width() const override;
 	int height() const override;
 
+	void verticalFlip() override;
+
 	friend class CompImageLoader;
 };
 
@@ -198,6 +219,10 @@ public:
 	CAnimation();
 	~CAnimation();
 
+	//duplicates frame at [sourceGroup, sourceFrame] as last frame in targetGroup
+	//and loads it if animation is preloaded
+	void duplicateImage(const size_t sourceGroup, const size_t sourceFrame, const size_t targetGroup);
+
 	//add custom surface to the selected position.
 	void setCustom(std::string filename, size_t frame, size_t group=0);
 

+ 102 - 54
client/mapHandler.cpp

@@ -292,7 +292,7 @@ void CMapHandler::borderAndTerrainBitmapInit()
 	delete bord;
 }
 
-static void processDef (const ObjectTemplate & objTempl)
+static void processDef (const ObjectTemplate & objTempl) //deprecated
 {
 	if(objTempl.id == Obj::EVENT)
 	{
@@ -333,12 +333,19 @@ void CMapHandler::initObjectRects()
 		{
 			continue;
 		}
-		if (!graphics->getDef(obj)) //try to load it
-			processDef(obj->appearance);
-		if (!graphics->getDef(obj)) // stil no graphics? exit
+
+		std::shared_ptr<CAnimation> animation = graphics->getAnimation(obj);
+
+		//no animation at all
+		if(!animation)
+			continue;
+
+		//empty animation
+		if(animation->size(0) == 0)
 			continue;
 
-		const SDL_Surface *bitmap = graphics->getDef(obj)->ourImages[0].bitmap;
+		IImage * image = animation->getImage(0,0);
+
 		for(int fx=0; fx < obj->getWidth(); ++fx)
 		{
 			for(int fy=0; fy < obj->getHeight(); ++fy)
@@ -347,8 +354,8 @@ void CMapHandler::initObjectRects()
 				SDL_Rect cr;
 				cr.w = 32;
 				cr.h = 32;
-				cr.x = bitmap->w - fx * 32 - 32;
-				cr.y = bitmap->h - fy * 32 - 32;
+				cr.x = image->width() - fx * 32 - 32;
+				cr.y = image->height() - fy * 32 - 32;
 				TerrainTileObject toAdd(obj,cr);
 
 
@@ -461,6 +468,11 @@ void CMapHandler::CMapNormalBlitter::drawElement(EMapCacheType cacheType, SDL_Su
 	}
 }
 
+void CMapHandler::CMapNormalBlitter::drawElement(EMapCacheType cacheType, const IImage * source, SDL_Rect * sourceRect, SDL_Surface * targetSurf, SDL_Rect * destRect) const
+{
+	source->draw(targetSurf, destRect, sourceRect);
+}
+
 void CMapHandler::CMapNormalBlitter::init(const MapDrawingInfo * drawingInfo)
 {
 	info = drawingInfo;
@@ -641,6 +653,16 @@ void CMapHandler::CMapWorldViewBlitter::drawElement(EMapCacheType cacheType, SDL
 	}
 }
 
+void CMapHandler::CMapWorldViewBlitter::drawElement(EMapCacheType cacheType, const IImage * source, SDL_Rect * sourceRect, SDL_Surface * targetSurf, SDL_Rect * destRect) const
+{
+	SDL_Surface * scaledSurf = parent->cache.requestWorldViewCacheOrCreate(cacheType, (intptr_t) source, source, info->scale);
+
+	if(scaledSurf->format->BitsPerPixel == 8)
+		CSDL_Ext::blit8bppAlphaTo24bpp(scaledSurf, sourceRect, targetSurf, destRect);
+	else
+		CSDL_Ext::blitSurface(scaledSurf, sourceRect, targetSurf, destRect);
+}
+
 void CMapHandler::CMapWorldViewBlitter::drawTileOverlay(SDL_Surface * targetSurf, const TerrainTile2 & tile) const
 {
 	auto drawIcon = [this,targetSurf](Obj id, si32 subId, PlayerColor owner)
@@ -701,21 +723,21 @@ void CMapHandler::CMapWorldViewBlitter::drawOverlayEx(SDL_Surface * targetSurf)
 	}
 }
 
-void CMapHandler::CMapWorldViewBlitter::drawHeroFlag(SDL_Surface * targetSurf, SDL_Surface * sourceSurf, SDL_Rect * sourceRect, SDL_Rect * destRect, bool moving) const
+void CMapHandler::CMapWorldViewBlitter::drawHeroFlag(SDL_Surface * targetSurf, const IImage * source, SDL_Rect * sourceRect, SDL_Rect * destRect, bool moving) const
 {
 	if (moving)
 		return;
 
-	CMapBlitter::drawHeroFlag(targetSurf, sourceSurf, sourceRect, destRect, false);
+	CMapBlitter::drawHeroFlag(targetSurf, source, sourceRect, destRect, false);
 }
 
-void CMapHandler::CMapWorldViewBlitter::drawObject(SDL_Surface * targetSurf, SDL_Surface * sourceSurf, SDL_Rect * sourceRect, bool moving) const
+void CMapHandler::CMapWorldViewBlitter::drawObject(SDL_Surface * targetSurf, const IImage * source, SDL_Rect * sourceRect, bool moving) const
 {
 	if (moving)
 		return;
 
 	Rect scaledSourceRect(sourceRect->x * info->scale, sourceRect->y * info->scale, sourceRect->w, sourceRect->h);
-	CMapBlitter::drawObject(targetSurf, sourceSurf, &scaledSourceRect, false);
+	CMapBlitter::drawObject(targetSurf, source, &scaledSourceRect, false);
 }
 
 void CMapHandler::CMapBlitter::drawTileTerrain(SDL_Surface * targetSurf, const TerrainTile & tinfo, const TerrainTile2 & tile) const
@@ -820,15 +842,15 @@ void CMapHandler::CMapBlitter::drawOverlayEx(SDL_Surface * targetSurf)
 //nothing to do here
 }
 
-void CMapHandler::CMapBlitter::drawHeroFlag(SDL_Surface * targetSurf, SDL_Surface * sourceSurf, SDL_Rect * sourceRect, SDL_Rect * destRect, bool moving) const
+void CMapHandler::CMapBlitter::drawHeroFlag(SDL_Surface * targetSurf, const IImage * source, SDL_Rect * sourceRect, SDL_Rect * destRect, bool moving) const
 {
-	drawElement(EMapCacheType::HERO_FLAGS, sourceSurf, sourceRect, targetSurf, destRect, false);
+	drawElement(EMapCacheType::HERO_FLAGS, source, sourceRect, targetSurf, destRect);
 }
 
-void CMapHandler::CMapBlitter::drawObject(SDL_Surface * targetSurf, SDL_Surface * sourceSurf, SDL_Rect * sourceRect, bool moving) const
+void CMapHandler::CMapBlitter::drawObject(SDL_Surface * targetSurf, const IImage * source, SDL_Rect * sourceRect, bool moving) const
 {
 	Rect dstRect(realTileRect);
-	drawElement(EMapCacheType::OBJECTS, sourceSurf, sourceRect, targetSurf, &dstRect, true);
+	drawElement(EMapCacheType::OBJECTS, source, sourceRect, targetSurf, &dstRect);
 }
 
 void CMapHandler::CMapBlitter::drawObjects(SDL_Surface * targetSurf, const TerrainTile2 & tile) const
@@ -1062,20 +1084,24 @@ CMapHandler::AnimBitmapHolder CMapHandler::CMapBlitter::findHeroBitmap(const CGH
 		}
 
 		//pick graphics of hero (or boat if hero is sailing)
-		CDefEssential * def = nullptr;
+		std::shared_ptr<CAnimation> animation;
 		if (hero->boat)
-			def = graphics->boatAnims[hero->boat->subID];
+			animation = graphics->boatAnimations[hero->boat->subID];
 		else
-			def = graphics->heroAnims[hero->appearance.animationFile];
+			animation = graphics->heroAnimations[hero->appearance.animationFile];
 
 		bool moving = !hero->isStanding;
-		int framesOffset = moving ? anim % FRAMES_PER_MOVE_ANIM_GROUP : 0;
-		int index = findAnimIndexByGroup(def, getHeroFrameNum(hero->moveDir, moving));
-		if (index >= 0)
+		int group = getHeroFrameGroup(hero->moveDir, moving);
+
+        if(animation->size(group) > 0)
 		{
-			auto heroBitmap = def->ourImages[index + framesOffset].bitmap;
-			auto flagBitmap = findFlagBitmap(hero, anim, &hero->tempOwner, index + 35);
-			return CMapHandler::AnimBitmapHolder(heroBitmap, flagBitmap, moving);
+			int frame = anim % animation->size(group);
+			IImage * heroImage = animation->getImage(frame, group);
+
+			//get flag overlay only if we have main image
+			IImage * flagImage = findFlagBitmap(hero, anim, &hero->tempOwner, group);
+
+			return CMapHandler::AnimBitmapHolder(heroImage, flagImage, moving);
 		}
 	}
 	return CMapHandler::AnimBitmapHolder();
@@ -1083,46 +1109,57 @@ CMapHandler::AnimBitmapHolder CMapHandler::CMapBlitter::findHeroBitmap(const CGH
 
 CMapHandler::AnimBitmapHolder CMapHandler::CMapBlitter::findBoatBitmap(const CGBoat * boat, int anim) const
 {
-	auto def = graphics->boatAnims[boat->subID];
-	int index = findAnimIndexByGroup(def, getHeroFrameNum(boat->direction, false));
-	if (index < 0)
+	auto animation = graphics->boatAnimations.at(boat->subID);
+	int group = getHeroFrameGroup(boat->direction, false);
+	if(animation->size(group) > 0)
+		return CMapHandler::AnimBitmapHolder(animation->getImage(anim % animation->size(group), group));
+	else
 		return CMapHandler::AnimBitmapHolder();
-	return CMapHandler::AnimBitmapHolder(def->ourImages[index].bitmap);
 }
 
-SDL_Surface * CMapHandler::CMapBlitter::findFlagBitmap(const CGHeroInstance * hero, int anim, const PlayerColor * color, int indexOffset) const
+IImage * CMapHandler::CMapBlitter::findFlagBitmap(const CGHeroInstance * hero, int anim, const PlayerColor * color, int group) const
 {
 	if (!hero)
 		return nullptr;
 
 	if (hero->boat)
-		return findBoatFlagBitmap(hero->boat, anim, color, indexOffset, hero->moveDir);
-	return findHeroFlagBitmap(hero, anim, color, indexOffset);
+		return findBoatFlagBitmap(hero->boat, anim, color, group, hero->moveDir);
+	return findHeroFlagBitmap(hero, anim, color, group);
 }
 
-SDL_Surface * CMapHandler::CMapBlitter::findHeroFlagBitmap(const CGHeroInstance * hero, int anim, const PlayerColor * color, int indexOffset) const
+IImage * CMapHandler::CMapBlitter::findHeroFlagBitmap(const CGHeroInstance * hero, int anim, const PlayerColor * color, int group) const
 {
-	return findFlagBitmapInternal(graphics->flags4[color->getNum()], anim, indexOffset, hero->moveDir, !hero->isStanding);
+	return findFlagBitmapInternal(graphics->heroFlagAnimations.at(color->getNum()), anim, group, hero->moveDir, !hero->isStanding);
 }
 
-SDL_Surface * CMapHandler::CMapBlitter::findBoatFlagBitmap(const CGBoat * boat, int anim, const PlayerColor * color, int indexOffset, ui8 dir) const
+IImage * CMapHandler::CMapBlitter::findBoatFlagBitmap(const CGBoat * boat, int anim, const PlayerColor * color, int group, ui8 dir) const
 {
-	std::vector<CDefEssential *> Graphics::*flg = nullptr;
-	switch (boat->subID)
+	int boatType = boat->subID;
+	if(boatType < 0 || boatType >= graphics->boatFlagAnimations.size())
+	{
+		logGlobal->errorStream() << "Not supported boat subtype: " << boat->subID;
+		return nullptr;
+	}
+
+	const auto & subtypeFlags = graphics->boatFlagAnimations.at(boatType);
+
+	int colorIndex = color->getNum();
+
+	if(colorIndex < 0 || colorIndex >= subtypeFlags.size())
 	{
-		case 0: flg = &Graphics::flags1; break;
-		case 1: flg = &Graphics::flags2; break;
-		case 2: flg = &Graphics::flags3; break;
-		default: logGlobal->errorStream() << "Not supported boat subtype: " << boat->subID; return nullptr;
+		logGlobal->errorStream() << "Invalid player color " << colorIndex;
+		return nullptr;
 	}
-	return findFlagBitmapInternal((graphics->*flg)[color->getNum()], anim, indexOffset, dir, false);
+
+	return findFlagBitmapInternal(subtypeFlags.at(colorIndex), anim, group, dir, false);
 }
 
-SDL_Surface * CMapHandler::CMapBlitter::findFlagBitmapInternal(const CDefEssential * def, int anim, int indexOffset, ui8 dir, bool moving) const
+IImage * CMapHandler::CMapBlitter::findFlagBitmapInternal(std::shared_ptr<CAnimation> animation, int anim, int group, ui8 dir, bool moving) const
 {
-	if (moving)
-		return def->ourImages[indexOffset + anim % FRAMES_PER_MOVE_ANIM_GROUP].bitmap;
-	return def->ourImages[getHeroFrameNum(dir, false) * FRAMES_PER_MOVE_ANIM_GROUP + (anim / 4) % FRAMES_PER_MOVE_ANIM_GROUP].bitmap;
+	if(moving)
+		return animation->getImage(anim % animation->size(group), group);
+	else
+		return animation->getImage((anim / 4) % animation->size(group), group);
 }
 
 int CMapHandler::CMapBlitter::findAnimIndexByGroup(const CDefEssential * def, int groupNum) const
@@ -1143,14 +1180,11 @@ CMapHandler::AnimBitmapHolder CMapHandler::CMapBlitter::findObjectBitmap(const C
 		return findBoatBitmap(static_cast<const CGBoat*>(obj), anim);
 
 	// normal object
+	std::shared_ptr<CAnimation> animation = graphics->getAnimation(obj);
+	IImage * bitmap = animation->getImage((anim + getPhaseShift(obj)) % animation->size());
 
-	const std::vector<Cimage> &ourImages = graphics->getDef(obj)->ourImages;
-	SDL_Surface *bitmap = ourImages[(anim + getPhaseShift(obj)) % ourImages.size()].bitmap;
+	bitmap->setFlagColor(obj->tempOwner);
 
-	//setting appropriate flag color
-	const PlayerColor &color = obj->tempOwner;
-	if(color < PlayerColor::PLAYER_LIMIT || color==PlayerColor::NEUTRAL)
-		CSDL_Ext::setPlayerColor(bitmap, color);
 	return CMapHandler::AnimBitmapHolder(bitmap);
 }
 
@@ -1179,7 +1213,7 @@ bool CMapHandler::CMapBlitter::canDrawCurrentTile() const
 	return !neighbors.areAllHidden();
 }
 
-ui8 CMapHandler::CMapBlitter::getHeroFrameNum(ui8 dir, bool isMoving) const
+ui8 CMapHandler::CMapBlitter::getHeroFrameGroup(ui8 dir, bool isMoving) const
 {
 	if(isMoving)
 	{
@@ -1263,13 +1297,14 @@ bool CMapHandler::startObjectFade(TerrainTileObject & obj, bool in, int3 pos)
 
 		fadeBitmap = CSDL_Ext::newSurface(32, 32); // TODO cache these bitmaps instead of creating new ones?
 		Rect objSrcRect(obj.rect.x, obj.rect.y, 32, 32);
-		CSDL_Ext::blit8bppAlphaTo24bpp(objData.objBitmap, &objSrcRect, fadeBitmap, nullptr);
+		objData.objBitmap->draw(fadeBitmap,0,0,&objSrcRect);
+
 		if (objData.flagBitmap)
 		{
 			if (obj.obj->pos.x - 1 == pos.x && obj.obj->pos.y - 1 == pos.y) // -1 to draw flag in top-center instead of right-bottom; kind of a hack
 			{
 				Rect flagSrcRect(32, 0, 32, 32);
-				CSDL_Ext::blitSurface(objData.flagBitmap, &flagSrcRect, fadeBitmap, nullptr);
+				objData.flagBitmap->draw(fadeBitmap,0,0, &flagSrcRect);
 			}
 		}
 		auto anim = new CFadeAnimation();
@@ -1647,6 +1682,19 @@ SDL_Surface * CMapHandler::CMapCache::requestWorldViewCacheOrCreate(CMapHandler:
 	return cacheWorldViewEntry(type, key, scaled);
 }
 
+SDL_Surface * CMapHandler::CMapCache::requestWorldViewCacheOrCreate(CMapHandler::EMapCacheType type, intptr_t key,const IImage * fullSurface, float scale)
+{
+	auto cached = requestWorldViewCache(type, key);
+	if (cached)
+		return cached;
+
+	auto scaled = fullSurface->scaleFast(scale);
+
+	data[type][key] = scaled;
+
+	return scaled;
+}
+
 SDL_Surface *CMapHandler::CMapCache::cacheWorldViewEntry(CMapHandler::EMapCacheType type, intptr_t key, SDL_Surface * entry)
 {
 	if (!entry)

+ 21 - 14
client/mapHandler.h

@@ -187,6 +187,7 @@ class CMapHandler
 		SDL_Surface * requestWorldViewCache(EMapCacheType type, intptr_t key);
 		/// asks for cached data; @returns cached data if found, new scaled surface otherwise
 		SDL_Surface * requestWorldViewCacheOrCreate(EMapCacheType type, intptr_t key, SDL_Surface * fullSurface, float scale);
+		SDL_Surface * requestWorldViewCacheOrCreate(EMapCacheType type, intptr_t key, const IImage * fullSurface, float scale);
 		SDL_Surface * cacheWorldViewEntry(EMapCacheType type, intptr_t key, SDL_Surface * entry);
 		intptr_t genKey(intptr_t realPtr, ui8 mod);
 	};
@@ -194,11 +195,11 @@ class CMapHandler
 	/// helper struct to pass around resolved bitmaps of an object; surfaces can be nullptr if object doesn't have bitmap of that type
 	struct AnimBitmapHolder
 	{
-		SDL_Surface * objBitmap; // main object bitmap
-		SDL_Surface * flagBitmap; // flag bitmap for the object (probably only for heroes and boats with heroes)
+		IImage * objBitmap; // main object bitmap
+		IImage * flagBitmap; // flag bitmap for the object (probably only for heroes and boats with heroes)
 		bool isMoving; // indicates if the object is moving (again, heroes/boats only)
 
-		AnimBitmapHolder(SDL_Surface * objBitmap_ = nullptr, SDL_Surface * flagBitmap_ = nullptr, bool moving = false)
+		AnimBitmapHolder(IImage * objBitmap_ = nullptr, IImage * flagBitmap_ = nullptr, bool moving = false)
 			: objBitmap(objBitmap_),
 			  flagBitmap(flagBitmap_),
 			  isMoving(moving)
@@ -226,6 +227,9 @@ class CMapHandler
 		virtual void drawElement(EMapCacheType cacheType, SDL_Surface * sourceSurf, SDL_Rect * sourceRect,
 								 SDL_Surface * targetSurf, SDL_Rect * destRect, bool alphaBlit = false, ui8 rotationInfo = 0u) const = 0;
 
+		//todo: support rotation
+		virtual void drawElement(EMapCacheType cacheType, const IImage * source, SDL_Rect * sourceRect, SDL_Surface * targetSurf, SDL_Rect * destRect) const = 0;
+
 		// first drawing pass
 
 		/// draws terrain bitmap (or custom bitmap if applicable) on current tile
@@ -236,8 +240,8 @@ class CMapHandler
 		virtual void drawRoad(SDL_Surface * targetSurf, const TerrainTile & tinfo, const TerrainTile * tinfoUpper) const;
 		/// draws all objects on current tile (higher-level logic, unlike other draw*** methods)
 		virtual void drawObjects(SDL_Surface * targetSurf, const TerrainTile2 & tile) const;
-		virtual void drawObject(SDL_Surface * targetSurf, SDL_Surface * sourceSurf, SDL_Rect * sourceRect, bool moving) const;
-		virtual void drawHeroFlag(SDL_Surface * targetSurf, SDL_Surface * sourceSurf, SDL_Rect * sourceRect, SDL_Rect * destRect, bool moving) const;
+		virtual void drawObject(SDL_Surface * targetSurf, const IImage * source, SDL_Rect * sourceRect, bool moving) const;
+		virtual void drawHeroFlag(SDL_Surface * targetSurf, const IImage * source, SDL_Rect * sourceRect, SDL_Rect * destRect, bool moving) const;
 
 		// second drawing pass
 
@@ -262,7 +266,7 @@ class CMapHandler
 		/// calculates clip region for map viewport
 		virtual SDL_Rect clip(SDL_Surface * targetSurf) const = 0;
 
-		virtual ui8 getHeroFrameNum(ui8 dir, bool isMoving) const;
+		virtual ui8 getHeroFrameGroup(ui8 dir, bool isMoving) const;
 		///returns appropriate bitmap and info if alpha blitting is necessary
 		virtual std::pair<SDL_Surface *, bool> getVisBitmap() const;
 		virtual ui8 getPhaseShift(const CGObjectInstance *object) const;
@@ -273,11 +277,11 @@ class CMapHandler
 		// internal helper methods to choose correct bitmap(s) for object; called internally by findObjectBitmap
 		AnimBitmapHolder findHeroBitmap(const CGHeroInstance * hero, int anim) const;
 		AnimBitmapHolder findBoatBitmap(const CGBoat * hero, int anim) const;
-		SDL_Surface * findFlagBitmap(const CGHeroInstance * obj, int anim, const PlayerColor * color, int indexOffset) const;
-		SDL_Surface * findHeroFlagBitmap(const CGHeroInstance * obj, int anim, const PlayerColor * color, int indexOffset) const;
-		SDL_Surface * findBoatFlagBitmap(const CGBoat * obj, int anim, const PlayerColor * color, int indexOffset, ui8 dir) const;
-		SDL_Surface * findFlagBitmapInternal(const CDefEssential * def, int anim, int indexOffset, ui8 dir, bool moving) const;
-		int findAnimIndexByGroup(const CDefEssential * def, int groupNum) const;
+		IImage * findFlagBitmap(const CGHeroInstance * obj, int anim, const PlayerColor * color, int group) const;
+		IImage * findHeroFlagBitmap(const CGHeroInstance * obj, int anim, const PlayerColor * color, int group) const;
+		IImage * findBoatFlagBitmap(const CGBoat * obj, int anim, const PlayerColor * color, int group, ui8 dir) const;
+		IImage * findFlagBitmapInternal(std::shared_ptr<CAnimation> animation, int anim, int group, ui8 dir, bool moving) const;
+		int findAnimIndexByGroup(const CDefEssential * def, int groupNum) const; //deprecated
 
 	public:
 		CMapBlitter(CMapHandler * p) : parent(p) {}
@@ -294,6 +298,8 @@ class CMapHandler
 		void drawElement(EMapCacheType cacheType, SDL_Surface * sourceSurf, SDL_Rect * sourceRect,
 						 SDL_Surface * targetSurf, SDL_Rect * destRect, bool alphaBlit = false, ui8 rotationInfo = 0u) const override;
 
+		void drawElement(EMapCacheType cacheType, const IImage * source, SDL_Rect * sourceRect, SDL_Surface * targetSurf, SDL_Rect * destRect) const override;
+
 		void drawTileOverlay(SDL_Surface * targetSurf,const TerrainTile2 & tile) const override {}
 		void init(const MapDrawingInfo * info) override;
 		SDL_Rect clip(SDL_Surface * targetSurf) const override;
@@ -310,15 +316,16 @@ class CMapHandler
 		void drawElement(EMapCacheType cacheType, SDL_Surface * sourceSurf, SDL_Rect * sourceRect,
 						 SDL_Surface * targetSurf, SDL_Rect * destRect, bool alphaBlit = false, ui8 rotationInfo = 0u) const override;
 
+		void drawElement(EMapCacheType cacheType, const IImage * source, SDL_Rect * sourceRect, SDL_Surface * targetSurf, SDL_Rect * destRect) const override;
+
 		void drawTileOverlay(SDL_Surface * targetSurf, const TerrainTile2 & tile) const override;
-		void drawHeroFlag(SDL_Surface * targetSurf, SDL_Surface * sourceSurf, SDL_Rect * sourceRect, SDL_Rect * destRect, bool moving) const override;
-		void drawObject(SDL_Surface * targetSurf, SDL_Surface * sourceSurf, SDL_Rect * sourceRect, bool moving) const override;
+		void drawHeroFlag(SDL_Surface * targetSurf, const IImage * source, SDL_Rect * sourceRect, SDL_Rect * destRect, bool moving) const override;
+		void drawObject(SDL_Surface * targetSurf, const IImage * source, SDL_Rect * sourceRect, bool moving) const override;
 		void drawFrame(SDL_Surface * targetSurf) const override {}
 		void drawOverlayEx(SDL_Surface * targetSurf) override;
 		void init(const MapDrawingInfo * info) override;
 		SDL_Rect clip(SDL_Surface * targetSurf) const override;
 
-//		ui8 getHeroFrameNum(ui8 dir, bool isMoving) const override { return 0u; }
 		ui8 getPhaseShift(const CGObjectInstance *object) const override { return 0u; }
 
 		void drawScaledRotatedElement(EMapCacheType type, SDL_Surface * baseSurf, SDL_Surface * targetSurf, ui8 rotation,