Forráskód Böngészése

try to use CAnimation for Hero path

AlexVinS 9 éve
szülő
commit
3dddbcf2e8

+ 1 - 1
client/CMT.cpp

@@ -154,7 +154,7 @@ void init()
 		logGlobal->infoStream()<<"Screen handler: "<<pomtime.getDiff();
 		pomtime.getDiff();
 
-		graphics->loadHeroAnims();
+		graphics->load();
 		logGlobal->infoStream()<<"\tMain graphics: "<<pomtime.getDiff();
 		logGlobal->infoStream()<<"Initializing game graphics: "<<tmh.getDiff();
 

+ 10 - 6
client/Graphics.cpp

@@ -5,6 +5,7 @@
 #include "../lib/filesystem/CBinaryReader.h"
 #include "CDefHandler.h"
 #include "gui/SDL_Extensions.h"
+#include "gui/CAnimation.h"
 #include <SDL_ttf.h>
 #include "../lib/CThreadHelper.h"
 #include "CGameInfo.h"
@@ -145,7 +146,6 @@ Graphics::Graphics()
 	tasks += std::bind(&Graphics::loadErmuToPicture,this);
 	tasks += std::bind(&Graphics::initializeImageLists,this);
 	tasks += GET_DEF_ESS(resources32,"RESOURCE.DEF");
-	tasks += GET_DEF_ESS(heroMoveArrows,"ADAG.DEF");
 
 	CThreadHelper th(&tasks,std::max((ui32)1,boost::thread::hardware_concurrency()));
 	th.run();
@@ -157,13 +157,17 @@ Graphics::Graphics()
 	loadErmuToPicture();
 	initializeImageLists();
 	resources32 = CDefHandler::giveDefEss("RESOURCE.DEF");
-	heroMoveArrows = CDefHandler::giveDefEss("ADAG.DEF");
 	#endif
 
-	for(auto & elem : heroMoveArrows->ourImages)
-	{
-		CSDL_Ext::alphaTransform(elem.bitmap);
-	}
+	//(!) do not load any CAnimation here
+}
+
+void Graphics::load()
+{
+	heroMoveArrows = std::make_shared<CAnimation>("ADAG");
+	heroMoveArrows->preload();
+
+	loadHeroAnims();
 }
 
 void Graphics::loadHeroAnims()

+ 18 - 14
client/Graphics.h

@@ -38,20 +38,31 @@ class Graphics
 {
 	void addImageListEntry(size_t index, std::string listName, std::string imageName);
 
+	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 loadErmuToPicture();
+
+	void loadFonts();
+	void initializeImageLists();
+
 public:
 	//Fonts
 	static const int FONTS_NUMBER = 9;
 	IFont * fonts[FONTS_NUMBER];
-	
+
 	//various graphics
 	SDL_Color * playerColors; //array [8]
 	SDL_Color * neutralColor;
 	SDL_Color * playerColorPalette; //palette to make interface colors good - array of size [256]
-	SDL_Color * neutralColorPalette; 
+	SDL_Color * neutralColorPalette;
 
 	std::vector<CDefEssential *> flags1, flags2, flags3, flags4; //flags blitted on heroes when ,
 	CDefEssential * resources32; //resources 32x32
-	CDefEssential * heroMoveArrows;
+	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
 	CDefHandler * FoWfullHide; //for Fog of War
@@ -68,18 +79,11 @@ public:
 	std::vector< std::vector< std::string > > battleBacks; //battleBacks[terType] - vector of possible names for certain terrain type
 	std::map< int, std::vector < std::string > > battleACToDef; //maps AC format to vector of appropriate def names
 	//functions
-	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 loadErmuToPicture();
-	void blueToPlayersAdv(SDL_Surface * sur, PlayerColor player); //replaces blue interface colour with a color of player
+	Graphics();
 
-	void loadFonts();
-	void initializeImageLists();
+	void load();
+
+	void blueToPlayersAdv(SDL_Surface * sur, PlayerColor player); //replaces blue interface colour with a color of player
 };
 
 extern Graphics * graphics;

+ 15 - 3
client/gui/CAnimation.cpp

@@ -635,6 +635,7 @@ void SDLImage::draw(SDL_Surface *where, int posX, int posY, Rect *src, ui8 rotat
 {
 	if (!surf)
 		return;
+
 	Rect sourceRect(margins.x, margins.y, surf->w, surf->h);
 	//TODO: rotation and scaling
 	if (src)
@@ -1056,7 +1057,8 @@ void CAnimation::printError(size_t frame, size_t group, std::string type) const
 
 CAnimation::CAnimation(std::string Name, bool Compressed):
 	name(Name),
-	compressed(Compressed)
+	compressed(Compressed),
+	preloaded(false)
 {
 	size_t dotPos = name.find_last_of('.');
 	if ( dotPos!=-1 )
@@ -1069,14 +1071,18 @@ CAnimation::CAnimation(std::string Name, bool Compressed):
 
 CAnimation::CAnimation():
 	name(""),
-	compressed(false)
+	compressed(false),
+	preloaded(false)
 {
 	init(nullptr);
 }
 
 CAnimation::~CAnimation()
 {
-	if (!images.empty())
+	if(preloaded)
+		unload();
+
+	if(!images.empty())
 	{
 		logGlobal->warnStream()<<"Warning: not all frames were unloaded from "<<name;
 		for (auto & elem : images)
@@ -1126,6 +1132,12 @@ void CAnimation::unload()
 
 }
 
+void CAnimation::preload()
+{
+	preloaded = true;
+	load();
+}
+
 void CAnimation::loadGroup(size_t group)
 {
 	CDefFile * file = getFile();

+ 3 - 0
client/gui/CAnimation.h

@@ -169,6 +169,8 @@ private:
 	//if true all frames will be stored in compressed (RLE) state
 	const bool compressed;
 
+	bool preloaded;
+
 	//loader, will be called by load(), require opened def file for loading from it. Returns true if image is loaded
 	bool loadFrame(CDefFile * file, size_t frame, size_t group);
 
@@ -204,6 +206,7 @@ public:
 	//all available frames
 	void load  ();
 	void unload();
+	void preload();
 
 	//all frames from group
 	void loadGroup  (size_t group);

+ 2 - 2
client/mapHandler.cpp

@@ -778,8 +778,8 @@ void CMapHandler::CMapPuzzleViewBlitter::drawObjects(SDL_Surface * targetSurf, c
 	// grail X mark
 	if(pos.x == info->grailPos.x && pos.y == info->grailPos.y)
 	{
-		Rect destRect(realTileRect);
-		CSDL_Ext::blit8bppAlphaTo24bpp(graphics->heroMoveArrows->ourImages[0].bitmap, nullptr, targetSurf, &destRect);
+		const IImage * mark = graphics->heroMoveArrows->getImage(0);
+		mark->draw(targetSurf,realTileRect.x,realTileRect.y);
 	}
 }
 

+ 18 - 25
client/windows/CAdvmapInterface.cpp

@@ -227,13 +227,14 @@ void CTerrainRect::showPath(const SDL_Rect * extRect, SDL_Surface * to)
 			pn+=25;
 		if (pn>=0)
 		{
-			CDefEssential * arrows = graphics->heroMoveArrows;
+			const IImage * arrow = graphics->heroMoveArrows->getImage(pn);
+
 			int x = 32*(curPos.x-adventureInt->position.x)+CGI->mh->offsetX + pos.x,
 				y = 32*(curPos.y-adventureInt->position.y)+CGI->mh->offsetY + pos.y;
 			if (x< -32 || y< -32 || x>pos.w || y>pos.h)
 				continue;
-			int hvx = (x+arrows->ourImages[pn].bitmap->w)-(pos.x+pos.w),
-				hvy = (y+arrows->ourImages[pn].bitmap->h)-(pos.y+pos.h);
+			int hvx = (x + arrow->width())  - (pos.x + pos.w),
+				hvy = (y + arrow->height()) - (pos.y + pos.h);
 
 			SDL_Rect prevClip;
 			SDL_GetClipRect(to, &prevClip);
@@ -243,52 +244,44 @@ void CTerrainRect::showPath(const SDL_Rect * extRect, SDL_Surface * to)
 			{
 				if (hvx<0 && hvy<0)
 				{
-					Rect dstRect = genRect(32, 32, x + moveX, y + moveY);
-					CSDL_Ext::blit8bppAlphaTo24bpp(arrows->ourImages[pn].bitmap, nullptr, to, &dstRect);
+					arrow->draw(to, x + moveX, y + moveY);
 				}
 				else if(hvx<0)
 				{
-					Rect srcRect = genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w, 0, 0);
-					Rect dstRect = genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w, x + moveX, y + moveY);
-					CSDL_Ext::blit8bppAlphaTo24bpp(arrows->ourImages[pn].bitmap, &srcRect, to, &dstRect);
+					Rect srcRect = genRect(arrow->height() - hvy, arrow->width(), 0, 0);
+					arrow->draw(to, x + moveX, y + moveY, &srcRect);
 				}
 				else if (hvy<0)
 				{
-					Rect srcRect = genRect(arrows->ourImages[pn].bitmap->h, arrows->ourImages[pn].bitmap->w-hvx, 0, 0);
-					Rect dstRect = genRect(arrows->ourImages[pn].bitmap->h, arrows->ourImages[pn].bitmap->w-hvx, x + moveX, y + moveY);
-					CSDL_Ext::blit8bppAlphaTo24bpp(arrows->ourImages[pn].bitmap, &srcRect, to, &dstRect);
+					Rect srcRect = genRect(arrow->height(), arrow->width() - hvx, 0, 0);
+					arrow->draw(to, x + moveX, y + moveY, &srcRect);
 				}
 				else
 				{
-					Rect srcRect = genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w-hvx, 0, 0);
-					Rect dstRect = genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w-hvx, x + moveX, y + moveY);
-					CSDL_Ext::blit8bppAlphaTo24bpp(arrows->ourImages[pn].bitmap, &srcRect, to, &dstRect);
+					Rect srcRect = genRect(arrow->height() - hvy, arrow->width() - hvx, 0, 0);
+					arrow->draw(to, x + moveX, y + moveY, &srcRect);
 				}
 			}
 			else //standard version
 			{
 				if (hvx<0 && hvy<0)
 				{
-					Rect dstRect = genRect(32, 32, x, y);
-					CSDL_Ext::blit8bppAlphaTo24bpp(arrows->ourImages[pn].bitmap, nullptr, to, &dstRect);
+					arrow->draw(to, x, y);
 				}
 				else if(hvx<0)
 				{
-					Rect srcRect = genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w, 0, 0);
-					Rect dstRect = genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w, x, y);
-					CSDL_Ext::blit8bppAlphaTo24bpp(arrows->ourImages[pn].bitmap, &srcRect, to, &dstRect);
+					Rect srcRect = genRect(arrow->height() - hvy, arrow->width(), 0, 0);
+					arrow->draw(to, x, y, &srcRect);
 				}
 				else if (hvy<0)
 				{
-					Rect srcRect = genRect(arrows->ourImages[pn].bitmap->h, arrows->ourImages[pn].bitmap->w-hvx, 0, 0);
-					Rect dstRect = genRect(arrows->ourImages[pn].bitmap->h, arrows->ourImages[pn].bitmap->w-hvx, x, y);
-					CSDL_Ext::blit8bppAlphaTo24bpp(arrows->ourImages[pn].bitmap, &srcRect, to, &dstRect);
+					Rect srcRect = genRect(arrow->height(), arrow->width() - hvx, 0, 0);
+					arrow->draw(to, x, y, &srcRect);
 				}
 				else
 				{
-					Rect srcRect = genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w-hvx, 0, 0);
-					Rect dstRect = genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w-hvx, x, y);
-					CSDL_Ext::blit8bppAlphaTo24bpp(arrows->ourImages[pn].bitmap, &srcRect, to, &dstRect);
+					Rect srcRect = genRect(arrow->height() - hvy, arrow->width() - hvx, 0, 0);
+					arrow->draw(to, x, y, &srcRect);
 				}
 			}
 			SDL_SetClipRect(to, &prevClip);