Sfoglia il codice sorgente

battle improvements

mateuszb 17 anni fa
parent
commit
d106fdcf2a

+ 23 - 5
CBattleInterface.cpp

@@ -13,7 +13,7 @@ extern SDL_Surface * screen;
 SDL_Surface * CBattleInterface::cellBorder, * CBattleInterface::cellShade;
 
 CBattleInterface::CBattleInterface(CCreatureSet * army1, CCreatureSet * army2, CGHeroInstance *hero1, CGHeroInstance *hero2) 
-: printCellBorders(true), attackingHeroInstance(hero1), defendingHeroInstance(hero2)
+: printCellBorders(true), attackingHeroInstance(hero1), defendingHeroInstance(hero2), animCount(0)
 {
 	//initializing armies
 	this->army1 = army1;
@@ -142,6 +142,7 @@ void CBattleInterface::deactivate()
 
 void CBattleInterface::show(SDL_Surface * to)
 {
+	++animCount;
 	if(!to) //"evaluating" to
 		to = screen;
 
@@ -162,7 +163,7 @@ void CBattleInterface::show(SDL_Surface * to)
 	//printing hovered cell
 	for(int b=0; b<187; ++b)
 	{
-		if(bfield[b].hovered)
+		if(bfield[b].strictHovered && bfield[b].hovered)
 		{
 			int x = 14 + ((b/17)%2==0 ? 22 : 0) + 44*(b%17);
 			int y = 86 + 42 * (b/17);
@@ -194,7 +195,7 @@ void CBattleInterface::show(SDL_Surface * to)
 	for(std::map<int, CCreatureAnimation*>::iterator j=creAnims.begin(); j!=creAnims.end(); ++j)
 	{
 		std::pair <int, int> coords = CBattleHex::getXYUnitAnim(stacks[j->first].position, stacks[j->first].owner == attackingHeroInstance->tempOwner);
-		j->second->nextFrame(to, coords.first, coords.second, stacks[j->first].owner == attackingHeroInstance->tempOwner);
+		j->second->nextFrame(to, coords.first, coords.second, stacks[j->first].owner == attackingHeroInstance->tempOwner, animCount%2==0);
 	}
 	//units shown
 
@@ -343,11 +344,13 @@ std::pair<int, int> CBattleHex::getXYUnitAnim(int hexNum, bool attacker)
 void CBattleHex::activate()
 {
 	Hoverable::activate();
+	MotionInterested::activate();
 }
 
 void CBattleHex::deactivate()
 {
-	Hoverable::activate();
+	Hoverable::deactivate();
+	MotionInterested::deactivate();
 }
 
 void CBattleHex::hover(bool on)
@@ -356,6 +359,21 @@ void CBattleHex::hover(bool on)
 	Hoverable::hover(on);
 }
 
-CBattleHex::CBattleHex() : myNumber(-1), accesible(true), hovered(false)
+CBattleHex::CBattleHex() : myNumber(-1), accesible(true), hovered(false), strictHovered(false)
 {
 }
+
+void CBattleHex::mouseMoved(SDL_MouseMotionEvent &sEvent)
+{
+	if(CBattleInterface::cellShade)
+	{
+		if(CSDL_Ext::SDL_GetPixel(CBattleInterface::cellShade, sEvent.x-pos.x, sEvent.y-pos.y) == 0) //hovered pixel is outside hex
+		{
+			strictHovered = false;
+		}
+		else //hovered pixel is inside hex
+		{
+			strictHovered = true;
+		}
+	}
+}

+ 4 - 2
CBattleInterface.h

@@ -22,18 +22,19 @@ public:
 	~CBattleHero(); //d-tor
 };
 
-class CBattleHex : public Hoverable
+class CBattleHex : public Hoverable, public MotionInterested
 {
 public:
 	unsigned int myNumber;
 	bool accesible;
 	//CStack * ourStack;
-	bool hovered;
+	bool hovered, strictHovered;
 	static std::pair<int, int> getXYUnitAnim(int hexNum, bool attacker); //returns (x, y) of left top corner of animation
 	//for user interactions
 	void hover (bool on);
 	void activate();
 	void deactivate();
+	void mouseMoved (SDL_MouseMotionEvent & sEvent);
 	CBattleHex();
 };
 
@@ -52,6 +53,7 @@ private:
 	CCreatureSet * army1, * army2; //fighting armies
 	CGHeroInstance * attackingHeroInstance, * defendingHeroInstance;
 	std::map< int, CCreatureAnimation * > creAnims; //animations of creatures from fighting armies (order like in BattleInfo's stacks)
+	unsigned char animCount;
 
 public:
 	CBattleInterface(CCreatureSet * army1, CCreatureSet * army2, CGHeroInstance *hero1, CGHeroInstance *hero2); //c-tor

+ 5 - 5
CCastleInterface.cpp

@@ -114,7 +114,7 @@ void CBuildingRect::mouseMoved (SDL_MouseMotionEvent & sEvent)
 {
 	if(area)
 	{
-		if(CSDL_Ext::SDL_GetPixel(area,sEvent.x-pos.x,sEvent.y-pos.y) == 0) //najechany piksel jest poza polem
+		if(CSDL_Ext::SDL_GetPixel(area,sEvent.x-pos.x,sEvent.y-pos.y) == 0) //hovered pixel is inside this building
 		{
 			if(LOCPLINT->castleInt->hBuild == this)
 			{
@@ -122,11 +122,11 @@ void CBuildingRect::mouseMoved (SDL_MouseMotionEvent & sEvent)
 				LOCPLINT->statusbar->clear();
 			}
 		}
-		else //w polu
+		else //inside the area of this building
 		{
-			if(LOCPLINT->castleInt->hBuild) //jakis budynek jest zaznaczony
+			if(LOCPLINT->castleInt->hBuild) //a building is hovered
 			{
-				if((*LOCPLINT->castleInt->hBuild)<(*this)) //ustawiamy sie, jesli jestesmy na wierzchu
+				if((*LOCPLINT->castleInt->hBuild)<(*this)) //set if we are on top
 				{
 					LOCPLINT->castleInt->hBuild = this;
 					if(CGI->buildh->buildings[str->townID][str->ID] && CGI->buildh->buildings[str->townID][str->ID]->name.length())
@@ -135,7 +135,7 @@ void CBuildingRect::mouseMoved (SDL_MouseMotionEvent & sEvent)
 						LOCPLINT->statusbar->print(str->name);
 				}
 			}
-			else //nie ma budynku, wiec damy nasz
+			else //no building hovered
 			{
 				LOCPLINT->castleInt->hBuild = this;
 				if(CGI->buildh->buildings[str->townID][str->ID] && CGI->buildh->buildings[str->townID][str->ID]->name.length())

+ 3 - 3
CPlayerInterface.cpp

@@ -1876,16 +1876,16 @@ void CPlayerInterface::actionFinished(Action action)//occurs AFTER every action
 
 void CPlayerInterface::activeStack(int stackID) //called when it's turn of that stack
 {
-	unsigned int av=0;
+	unsigned char showCount = 0;
 	while(true)
 	{
-		++av;
+		++showCount;
 		SDL_Event sEvent;
 		while (SDL_PollEvent(&sEvent))  //wait for event...
 		{
 			LOCPLINT->handleEvent(&sEvent);
 		}
-		if(av%3==0)
+		if(showCount%2==0)
 			for(int i=0;i<objsToBlit.size();i++)
 				objsToBlit[i]->show();
 		//SDL_Flip(ekran);

+ 39 - 28
hch/CCreatureHandler.cpp

@@ -743,34 +743,42 @@ int CCreatureAnimation::readNormalNr (int pos, int bytCon, unsigned char * str,
 	return ret;
 }
 
-int CCreatureAnimation::nextFrame(SDL_Surface *dest, int x, int y, bool attacker)
+int CCreatureAnimation::nextFrame(SDL_Surface *dest, int x, int y, bool attacker, bool incrementFrame)
 {
 	if(dest->format->BytesPerPixel<3)
 		return -1; //not enough depth
 
 	//increasing frame numer
-	int SIndex = curFrame++;
-	if(type!=-1)
+	int SIndex = -1;
+	if(incrementFrame)
 	{
-		if(SEntries[curFrame].group!=type) //rewind
+		SIndex = curFrame++;
+		if(type!=-1)
 		{
-			int j=-1; //first frame in displayed group
-			for(int g=0; g<SEntries.size(); ++g)
+			if(SEntries[curFrame].group!=type) //rewind
 			{
-				if(SEntries[g].group==type && j==-1)
+				int j=-1; //first frame in displayed group
+				for(int g=0; g<SEntries.size(); ++g)
 				{
-					j = g;
-					break;
+					if(SEntries[g].group==type && j==-1)
+					{
+						j = g;
+						break;
+					}
 				}
+				if(curFrame!=-1)
+					curFrame = j;
 			}
-			if(curFrame!=-1)
-				curFrame = j;
+		}
+		else
+		{
+			if(curFrame>=frames)
+				curFrame = 0;
 		}
 	}
 	else
 	{
-		if(curFrame>=frames)
-			curFrame = 0;
+		SIndex = curFrame;
 	}
 	//frame number increased
 
@@ -781,8 +789,6 @@ int CCreatureAnimation::nextFrame(SDL_Surface *dest, int x, int y, bool attacker
 		TotalRowLength, // dlugosc przeczytanego segmentu
 		NextSpriteOffset, RowAdd;
 	unsigned char SegmentType, SegmentLength;
-
-	std::string FTemp;
 	
 	i=BaseOffset=SEntries[SIndex].offset;
 	int prSize=readNormalNr(i,4,FDef);i+=4;
@@ -819,8 +825,7 @@ int CCreatureAnimation::nextFrame(SDL_Surface *dest, int x, int y, bool attacker
 		{
 			for (int i=0;i<TopMargin;i++)
 			{
-				for (int j=0;j<FullWidth+add;j++)
-					ftcp++;
+				ftcp+=FullWidth+add;
 			}
 		}
 		RLEntries = new int[SpriteHeight];
@@ -833,8 +838,7 @@ int CCreatureAnimation::nextFrame(SDL_Surface *dest, int x, int y, bool attacker
 			BaseOffset=BaseOffsetor+RLEntries[i];
 			if (LeftMargin>0)
 			{
-				for (int j=0;j<LeftMargin;j++)
-					ftcp++;
+				ftcp+=LeftMargin;
 			}
 			TotalRowLength=0;
 			do
@@ -846,6 +850,7 @@ int CCreatureAnimation::nextFrame(SDL_Surface *dest, int x, int y, bool attacker
 					for (int k=0;k<=SegmentLength;k++)
 					{
 						((char*)(hlps->pixels))[ftcp++]=FDef[BaseOffset+k];
+						//putPixel(dest, ftcp++, palette[FDef[BaseOffset+k]], FDef[BaseOffset+k]);
 						if ((TotalRowLength+k+1)>=SpriteWidth)
 							break;
 					}
@@ -857,6 +862,7 @@ int CCreatureAnimation::nextFrame(SDL_Surface *dest, int x, int y, bool attacker
 					for (int k=0;k<SegmentLength+1;k++)
 					{
 						((char*)(hlps->pixels))[ftcp++]=SegmentType;
+						//putPixel(dest, ftcp++, palette[SegmentType], SegmentType);
 					}
 					TotalRowLength+=SegmentLength+1;
 				}
@@ -864,24 +870,18 @@ int CCreatureAnimation::nextFrame(SDL_Surface *dest, int x, int y, bool attacker
 			RowAdd=SpriteWidth-TotalRowLength;
 			if (RightMargin>0)
 			{
-				for (int j=0;j<RightMargin;j++)
-					ftcp++;
+				ftcp+=RightMargin;
 			}
 			if (add>0)
 			{
-				for (int j=0;j<add+RowAdd;j++)
-					ftcp++;
+				ftcp+=add+RowAdd;
 			}
 		}
 		delete [] RLEntries;
 		RLEntries = NULL;
 		if (BottomMargin>0)
 		{
-			for (int i=0;i<BottomMargin;i++)
-			{
-				for (int j=0;j<FullWidth+add;j++)
-					ftcp++;
-			}
+			ftcp += BottomMargin * (FullWidth+add);
 		}
 	}
 	if(!attacker)
@@ -941,3 +941,14 @@ CCreatureAnimation::~CCreatureAnimation()
 	if (RLEntries)
 		delete [] RLEntries;
 }
+
+void CCreatureAnimation::putPixel(SDL_Surface * dest, const int & ftcp, const BMPPalette & color, const unsigned char & palc)
+{
+	if(palc!=0)
+	{
+		Uint8 * p = (Uint8*)dest->pixels + ftcp*3 ;
+		p[0] = color.R;
+		p[1] = color.G;
+		p[2] = color.B;
+	}
+}

+ 2 - 1
hch/CCreatureHandler.h

@@ -82,6 +82,7 @@ private:
 	char id[2];
 	std::string defName, curDir;
 	int readNormalNr (int pos, int bytCon, unsigned char * str=NULL, bool cyclic=false);
+	void putPixel(SDL_Surface * dest, const int & ftcp, const BMPPalette & color, const unsigned char & palc);
 
 	////////////
 
@@ -96,7 +97,7 @@ public:
 	void setType(int type); //sets type of animation and cleares framecount
 	int getType() const; //returns type of animation
 
-	int nextFrame(SDL_Surface * dest, int x, int y, bool attacker); //0 - success, any other - error //print next 
+	int nextFrame(SDL_Surface * dest, int x, int y, bool attacker, bool incrementFrame = true); //0 - success, any other - error //print next 
 };
 
 #endif //CCREATUREHANDLER_H