Browse Source

centralize key color management

AlexVinS 11 years ago
parent
commit
638dac90af

+ 2 - 2
client/CAdvmapInterface.cpp

@@ -294,7 +294,7 @@ void CResDataBar::clickRight(tribool down, bool previousState)
 CResDataBar::CResDataBar(const std::string &defname, int x, int y, int offx, int offy, int resdist, int datedist)
 {
 	bg = BitmapHandler::loadBitmap(defname);
-	SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
+	CSDL_Ext::setDefaultColorKey(bg);	
 	graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
 	pos = genRect(bg->h, bg->w, pos.x+x, pos.y+y);
 
@@ -313,7 +313,7 @@ CResDataBar::CResDataBar(const std::string &defname, int x, int y, int offx, int
 CResDataBar::CResDataBar()
 {
 	bg = BitmapHandler::loadBitmap(ADVOPT.resdatabarG);
-	SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
+	CSDL_Ext::setDefaultColorKey(bg);	
 	graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
 	pos = genRect(bg->h,bg->w,ADVOPT.resdatabarX,ADVOPT.resdatabarY);
 

+ 1 - 3
client/CAnimation.cpp

@@ -639,9 +639,7 @@ SDLImage::SDLImage(std::string filename, bool compressed):
 		#else
 		if (surf->format->palette)
 		{
-			const SDL_Color &c = temp->format->palette->colors[0];
-			uint32_t key = SDL_MapRGBA(temp -> format, c.r, c.g, c.b,c.a);
-			SDL_SetColorKey(temp, SDL_TRUE, key);
+			CSDL_Ext::setColorKey(temp,temp->format->palette->colors[0]);
 		}
 		SDL_SetSurfaceRLE(temp, SDL_RLEACCEL);		
 		#endif		

+ 3 - 10
client/CBitmapHandler.cpp

@@ -121,14 +121,7 @@ SDL_Surface * BitmapHandler::loadBitmapFromDir(std::string path, std::string fna
 		{
 			if(ret->format->BytesPerPixel == 1  &&  setKey)
 			{
-				const SDL_Color &c = ret->format->palette->colors[0];
-				#ifdef VCMI_SDL1
-				uint32_t key = SDL_MapRGB(ret->format, c.r, c.g, c.b); 
-				#else
-				uint32_t key = SDL_MapRGBA(ret->format, c.r, c.g, c.b, c.a); 
-				#endif
-				
-				SDL_SetColorKey(ret,SDL_SRCCOLORKEY,key);		
+				CSDL_Ext::setColorKey(ret,ret->format->palette->colors[0]);
 			}
 		}
 		else
@@ -160,7 +153,7 @@ SDL_Surface * BitmapHandler::loadBitmapFromDir(std::string path, std::string fna
 		}
 	}
 
-	// When modifyin anything here please check two use cases:
+	// When modifying anything here please check two use cases:
 	// 1) Vampire mansion in Necropolis (not 1st color is transparent)
 	// 2) Battle background when fighting on grass/dirt, topmost sky part (NO transparent color)
 	// 3) New objects that may use 24-bit images for icons (e.g. witchking arts)
@@ -176,7 +169,7 @@ SDL_Surface * BitmapHandler::loadBitmapFromDir(std::string path, std::string fna
 	}
 	else // always set
 	{
-		SDL_SetColorKey(ret, SDL_SRCCOLORKEY, colorID);
+		CSDL_Ext::setDefaultColorKey(ret);
 	}
 	return ret;
 }

+ 2 - 7
client/battle/CBattleInterface.cpp

@@ -363,13 +363,8 @@ CBattleInterface::CBattleInterface(const CCreatureSet * army1, const CCreatureSe
 		{
 			idToObstacle[ID] = CDefHandler::giveDef(elem->getInfo().defName);
 			for(auto & _n : idToObstacle[ID]->ourImages)
-			{
-				#ifdef VCMI_SDL1
-				uint32_t key = SDL_MapRGB(_n.bitmap->format, 0, 255, 255); 
-				#else
-				uint32_t key = SDL_MapRGBA(_n.bitmap->format, 0, 255, 255, 0); 
-				#endif				
-				SDL_SetColorKey(_n.bitmap, SDL_SRCCOLORKEY, key);
+			{		
+				CSDL_Ext::setDefaultColorKey(_n.bitmap);
 			}
 		}
 		else if(elem->obstacleType == CObstacleInstance::ABSOLUTE_OBSTACLE)

+ 31 - 0
client/gui/SDL_Extensions.cpp

@@ -13,6 +13,7 @@ const SDL_Color Colors::YELLOW = { 229, 215, 123, 0 };
 const SDL_Color Colors::WHITE = { 255, 243, 222, 0 };
 const SDL_Color Colors::METALLIC_GOLD = { 173, 142, 66, 0 };
 const SDL_Color Colors::GREEN = { 0, 255, 0, 0 };
+const SDL_Color Colors::DEFAULT_KEY_COLOR = {0, 255, 255, 0};
 
 #if (SDL_MAJOR_VERSION == 2)
 void SDL_UpdateRect(SDL_Surface *surface, int x, int y, int w, int h)
@@ -1004,6 +1005,36 @@ void CSDL_Ext::stopTextInput()
 	#endif	
 }
 
+STRONG_INLINE static uint32_t mapColor(SDL_Surface * surface, SDL_Color color)
+{
+	#ifdef VCMI_SDL1
+	return SDL_MapRGB(surface->format, color.r, color.g, color.b); 
+	#else
+	return SDL_MapRGBA(surface->format, color.r, color.g, color.b, color.a); 
+	#endif		
+}
+
+void CSDL_Ext::setColorKey(SDL_Surface * surface, SDL_Color color)
+{
+	uint32_t key = mapColor(surface,color);
+	SDL_SetColorKey(surface, SDL_SRCCOLORKEY, key);	
+}
+
+void CSDL_Ext::setDefaultColorKey(SDL_Surface * surface)
+{	
+	setColorKey(surface, Colors::DEFAULT_KEY_COLOR);
+}
+
+void CSDL_Ext::setDefaultColorKeyPresize(SDL_Surface * surface)
+{
+	uint32_t key = mapColor(surface,Colors::DEFAULT_KEY_COLOR);
+	auto & color = surface->format->palette->colors[key];
+
+	// set color key only if exactly such color was found
+	if (color.r == Colors::DEFAULT_KEY_COLOR.r && color.g == Colors::DEFAULT_KEY_COLOR.g && color.b == Colors::DEFAULT_KEY_COLOR.b)
+		SDL_SetColorKey(surface, SDL_SRCCOLORKEY, key);	
+}
+
 
 
 template SDL_Surface * CSDL_Ext::createSurfaceWithBpp<2>(int, int);

+ 9 - 0
client/gui/SDL_Extensions.h

@@ -128,6 +128,9 @@ public:
 
 	/** green color used for in-game console */
 	static const SDL_Color GREEN;
+	
+	/** default key color for all 8 & 24 bit graphics */
+	static const SDL_Color DEFAULT_KEY_COLOR;
 };
 
 //MSVC gives an error when calling abs with ui64 -> we add template that will match calls with unsigned arg and return it
@@ -266,4 +269,10 @@ namespace CSDL_Ext
 	
 	void startTextInput(SDL_Rect * where);
 	void stopTextInput();
+	
+	void setColorKey(SDL_Surface * surface, SDL_Color color);
+	///set key-color to 0,255,255
+	void setDefaultColorKey(SDL_Surface * surface);
+	///set key-color to 0,255,255 only if it exactly mapped
+	void setDefaultColorKeyPresize(SDL_Surface * surface);
 }