Browse Source

* minor improvements

mateuszb 15 years ago
parent
commit
94b289dab0
8 changed files with 67 additions and 64 deletions
  1. 5 23
      hch/CDefHandler.cpp
  2. 4 5
      hch/CDefHandler.h
  3. 7 1
      hch/CObjectHandler.cpp
  4. 2 1
      hch/CObjectHandler.h
  5. 14 6
      lib/CGameState.cpp
  6. 3 3
      lib/map.cpp
  7. 31 24
      mapHandler.cpp
  8. 1 1
      mapHandler.h

+ 5 - 23
hch/CDefHandler.cpp

@@ -48,26 +48,8 @@ CDefEssential::~CDefEssential()
 	for(size_t i=0; i < ourImages.size(); ++i)
 		SDL_FreeSurface(ourImages[i].bitmap);
 }
-// Note: this function is unused
-void CDefHandler::openDef(std::string name)
-{
-	int andame;
-	std::ifstream * is = new std::ifstream();
-	is -> open(name.c_str(),std::ios::binary);
-	is->seekg(0,std::ios::end); // na koniec
-	andame = is->tellg();  // read length
-	is->seekg(0,std::ios::beg); // wracamy na poczatek
-	unsigned char * FDef = new unsigned char[andame]; // allocate memory
-	is->read((char*)FDef, andame); // read map file to buffer
-	is->close();
-	delete is;
-
-	openFromMemory(FDef, name);
-
-	delete [] FDef;
-}
 
-void CDefHandler::openFromMemory(unsigned char *table, std::string name)
+void CDefHandler::openFromMemory(unsigned char *table, const std::string & name)
 {
 	BMPPalette palette[256];
 	SDefEntry &de = * reinterpret_cast<SDefEntry *>(table);
@@ -150,8 +132,8 @@ SDL_Surface * CDefHandler::getSprite (int SIndex, const unsigned char * FDef, co
 	SDL_Surface * ret=NULL;
 
 	unsigned int BaseOffset, 
-		SpriteWidth, SpriteHeight, //format sprite'a
-		TotalRowLength,			// dlugosc przeczytanego segmentu
+		SpriteWidth, SpriteHeight, //format of sprite
+		TotalRowLength,			// length of read segment
 		add, FullHeight,FullWidth,
 		RowAdd,					//, NextSpriteOffset; //TODO use me
 		prSize,
@@ -393,7 +375,7 @@ CDefEssential * CDefHandler::essentialize()
 	return ret;
 }
 
-CDefHandler * CDefHandler::giveDef(std::string defName)
+CDefHandler * CDefHandler::giveDef(const std::string & defName)
 {
 	unsigned char * data = spriteh->giveFile(defName);
 	if(!data)
@@ -404,7 +386,7 @@ CDefHandler * CDefHandler::giveDef(std::string defName)
 	delete [] data;
 	return nh;
 }
-CDefEssential * CDefHandler::giveDefEss(std::string defName)
+CDefEssential * CDefHandler::giveDefEss(const std::string & defName)
 {
 	CDefEssential * ret;
 	CDefHandler * temp = giveDef(defName);

+ 4 - 5
hch/CDefHandler.h

@@ -88,14 +88,13 @@ public:
 
 	CDefHandler(); //c-tor
 	~CDefHandler(); //d-tor
-	SDL_Surface * getSprite (int SIndex, const unsigned char * FDef, const BMPPalette * palette) const; //zapisuje klatke o zadanym numerze do "testtt.bmp"
-	void openDef(std::string name);
+	SDL_Surface * getSprite (int SIndex, const unsigned char * FDef, const BMPPalette * palette) const; //saves picture with given number to "testtt.bmp"
 	static void expand(unsigned char N,unsigned char & BL, unsigned char & BR);
-	void openFromMemory(unsigned char * table, std::string name);
+	void openFromMemory(unsigned char * table, const std::string & name);
 	CDefEssential * essentialize();
 
-	static CDefHandler * giveDef(std::string defName);
-	static CDefEssential * giveDefEss(std::string defName);
+	static CDefHandler * giveDef(const std::string & defName);
+	static CDefEssential * giveDefEss(const std::string & defName);
 };
 
 class CDefEssential //DefHandler with images only

+ 7 - 1
hch/CObjectHandler.cpp

@@ -234,7 +234,6 @@ CGObjectInstance::CGObjectInstance(): animPhaseShift(rand()%0xff)
 	//state = new CLuaObjectScript();
 	ID = subID = id = -1;
 	defInfo = NULL;
-	info = NULL;
 	tempOwner = 254;
 	blockVisit = false;
 }
@@ -448,6 +447,13 @@ ui8 CGObjectInstance::getPassableness() const
 	return 0;
 }
 
+bool CGObjectInstance::hasShadowAt( int x, int y ) const
+{
+	if( (defInfo->shadowCoverage[y] >> (7-(x) )) & 1 )
+		return true;
+	return false;
+}
+
 static int lowestSpeed(const CGHeroInstance * chi)
 {
 	if(!chi->army.slots.size())

+ 2 - 1
hch/CObjectHandler.h

@@ -143,7 +143,6 @@ public:
 	si32 ID, subID; //normal ID (this one from OH3 maps ;]) - eg. town=98; hero=34
 	si32 id;//number of object in CObjectHandler's vector		
 	CGDefInfo * defInfo;
-	CSpecObjInfo * info;
 	ui8 animPhaseShift;
 
 	ui8 tempOwner;
@@ -161,6 +160,7 @@ public:
 	int3 getVisitableOffset() const; //returns (x,y,0) offset to first visitable tile from bottom right obj tile (0,0,0) (h3m pos)
 	bool blockingAt(int x, int y) const; //returns true if object is blocking location (x, y) form left top tile of image (x, y in tiles)
 	bool coveringAt(int x, int y) const; //returns true if object covers with picture location (x, y) form left top tile of maximal possible image (8 x 6 tiles) (x, y in tiles)
+	bool hasShadowAt(int x, int y) const;//returns true if object covers with shadow location (x, y) form left top tile of maximal possible image (8 x 6 tiles) (x, y in tiles)
 	std::set<int3> getBlockedPos() const; //returns set of positions blocked by this object
 	bool operator<(const CGObjectInstance & cmp) const;  //screen printing priority comparing
 	CGObjectInstance();
@@ -357,6 +357,7 @@ public:
 class DLL_EXPORT CGDwelling : public CArmedInstance
 {
 public:
+	CSpecObjInfo * info; //h3m info about dewlling
 	std::vector<std::pair<ui32, std::vector<ui32> > > creatures; //creatures[level] -> <vector of alternative ids (base creature and upgrades, creatures amount>
 
 	template <typename Handler> void serialize(Handler &h, const int version)

+ 14 - 6
lib/CGameState.cpp

@@ -347,7 +347,6 @@ static CGObjectInstance * createObject(int id, int subid, int3 pos, int owner)
 	nobj->pos = pos;
 	//nobj->state = NULL;//new CLuaObjectScript();
 	nobj->tempOwner = owner;
-	nobj->info = NULL;
 	nobj->defInfo->id = id;
 	nobj->defInfo->subid = subid;
 
@@ -1061,7 +1060,8 @@ std::pair<int,int> CGameState::pickObject (CGObjectInstance *obj)
 	case 216: //random dwelling
 		{
 			int faction = ran()%F_NUMBER;
-			CCreGen2ObjInfo* info = static_cast<CCreGen2ObjInfo*>(obj->info);
+			CGDwelling * dwl = static_cast<CGDwelling*>(obj);
+			CCreGen2ObjInfo* info = static_cast<CCreGen2ObjInfo*>(dwl->info);
 			if (info->asCastle)
 			{
 				for(unsigned int i=0;i<map->objects.size();i++)
@@ -1094,12 +1094,15 @@ std::pair<int,int> CGameState::pickObject (CGObjectInstance *obj)
 				if(VLC->objh->cregens[i]==cid)
 					return std::pair<int,int>(17,i); 
 			tlog3 << "Cannot find a dwelling for creature "<< cid << std::endl;
-			return std::pair<int,int>(17,0); 
+			return std::pair<int,int>(17,0);
+			delete dwl->info;
+			dwl->info = NULL;
 		}
 	case 217:
 		{
 			int faction = ran()%F_NUMBER;
-			CCreGenObjInfo* info = static_cast<CCreGenObjInfo*>(obj->info);
+			CGDwelling * dwl = static_cast<CGDwelling*>(obj);
+			CCreGenObjInfo* info = static_cast<CCreGenObjInfo*>(dwl->info);
 			if (info->asCastle)
 			{
 				for(unsigned int i=0;i<map->objects.size();i++)
@@ -1131,11 +1134,14 @@ std::pair<int,int> CGameState::pickObject (CGObjectInstance *obj)
 				if(VLC->objh->cregens[i]==cid)
 					return std::pair<int,int>(17,i); 
 			tlog3 << "Cannot find a dwelling for creature "<<cid <<std::endl;
-			return std::pair<int,int>(17,0); 
+			return std::pair<int,int>(17,0);
+			delete dwl->info;
+			dwl->info = NULL;
 		}
 	case 218:
 		{
-			CCreGen3ObjInfo* info = static_cast<CCreGen3ObjInfo*>(obj->info);
+			CGDwelling * dwl = static_cast<CGDwelling*>(obj);
+			CCreGen3ObjInfo* info = static_cast<CCreGen3ObjInfo*>(dwl->info);
 			int level = ((info->maxLevel-info->minLevel) ? (ran()%(info->maxLevel-info->minLevel)+info->minLevel) : (info->minLevel));
 			int cid = VLC->townh->towns[obj->subID].basicCreatures[level];
 			for(unsigned int i=0;i<VLC->objh->cregens.size();i++)
@@ -1143,6 +1149,8 @@ std::pair<int,int> CGameState::pickObject (CGObjectInstance *obj)
 					return std::pair<int,int>(17,i); 
 			tlog3 << "Cannot find a dwelling for creature "<<cid <<std::endl;
 			return std::pair<int,int>(17,0); 
+			delete dwl->info;
+			dwl->info = NULL;
 		}
 	}
 	return std::pair<int,int>(-1,-1);

+ 3 - 3
lib/map.cpp

@@ -1756,7 +1756,7 @@ void Mapa::readObjects( const unsigned char * bufor, int &i)
 					spec->asCastle = true;
 				}
 				nobj->setOwner(spec->player);
-				nobj->info = spec;
+				static_cast<CGDwelling*>(nobj)->info = spec;
 				break;
 			}
 		case 216:
@@ -1778,7 +1778,7 @@ void Mapa::readObjects( const unsigned char * bufor, int &i)
 				spec->minLevel = bufor[i]; ++i;
 				spec->maxLevel = bufor[i]; ++i;
 				nobj->setOwner(spec->player);
-				nobj->info = spec;
+				static_cast<CGDwelling*>(nobj)->info = spec;
 				break;
 			}
 		case 218:
@@ -1794,7 +1794,7 @@ void Mapa::readObjects( const unsigned char * bufor, int &i)
 				if(spec->minLevel<1)
 					spec->minLevel = 1;
 				nobj->setOwner(spec->player);
-				nobj->info = spec;
+				static_cast<CGDwelling*>(nobj)->info = spec;
 				break;
 			}
 		case 215:

+ 31 - 24
mapHandler.cpp

@@ -438,7 +438,7 @@ void CMapHandler::init()
 	for (int i = 0; i < 8 ; i++)
 	{
 		TerrainTile2 &t = ttiles[24+i][0][0];
-		tlog0 << t.objects.front().first->defInfo->name << ' ';
+		//tlog0 << t.objects.front().first->defInfo->name << ' ';
 
 	}
 }
@@ -665,7 +665,10 @@ void CMapHandler::terrainRect(int3 top_tile, unsigned char anim, const std::vect
 					if(color < 8 || color==255)
 						CSDL_Ext::setPlayerColor(bitmap, color);
 
-					CSDL_Ext::blit8bppAlphaTo24bpp(bitmap,&pp,extSurf,&sr2);
+					if( obj->hasShadowAt(obj->pos.x - (top_tile.x + bx), top_tile.y + by - obj->pos.y + 5) )
+						CSDL_Ext::blit8bppAlphaTo24bpp(bitmap,&pp,extSurf,&sr2);
+					else
+						SDL_BlitSurface(bitmap,&pp,extSurf,&sr2);
 				}
 			}
 			//objects blitted
@@ -1150,51 +1153,55 @@ unsigned char CMapHandler::getHeroFrameNum(unsigned char dir, bool isMoving) con
 {
 	if(isMoving)
 	{
-		switch(dir)
+		static const unsigned char frame [] = {-1, 10, 5, 6, 7, 8, 9, 12, 11};
+		return frame[dir];
+		/*switch(dir)
 		{
 		case 1:
-			return 10;
+		return 10;
 		case 2:
-			return 5;
+		return 5;
 		case 3:
-			return 6;
+		return 6;
 		case 4:
-			return 7;
+		return 7;
 		case 5:
-			return 8;
+		return 8;
 		case 6:
-			return 9;
+		return 9;
 		case 7:
-			return 12;
+		return 12;
 		case 8:
-			return 11;
+		return 11;
 		default:
-			throw std::string("Something very wrong1.");
-		}
+		throw std::string("Something very wrong1.");
+		}*/
 	}
 	else //if(isMoving)
 	{
-		switch(dir)
+		static const unsigned char frame [] = {-1, 13, 0, 1, 2, 3, 4, 15, 14};
+		return frame[dir];
+		/*switch(dir)
 		{
 		case 1:
-			return 13;
+		return 13;
 		case 2:
-			return 0;
+		return 0;
 		case 3:
-			return 1;
+		return 1;
 		case 4:
-			return 2;
+		return 2;
 		case 5:
-			return 3;
+		return 3;
 		case 6:
-			return 4;
+		return 4;
 		case 7:
-			return 15;
+		return 15;
 		case 8:
-			return 14;
+		return 14;
 		default:
-			throw std::string("Something very wrong2.");
-		}
+		throw std::string("Something very wrong2.");
+		}*/
 	}
 }
 

+ 1 - 1
mapHandler.h

@@ -134,7 +134,7 @@ public:
 	void updateWater();
 	unsigned char getHeroFrameNum(unsigned char dir, bool isMoving) const; //terrainRect helper function
 	void validateRectTerr(SDL_Rect * val, const SDL_Rect * ext); //terrainRect helper
-	static unsigned char getDir(const int3 & a, const int3 & b); //returns direction number in range 0 - 7 (0 is left top, clockwise) [direction: form a to b]
+	static unsigned char getDir(const int3 & a, const int3 & b);  //returns direction number in range 0 - 7 (0 is left top, clockwise) [direction: form a to b]
 
 };