[SDL] Fancy Fonts anyone????

Patricia Curtis patricia.curtis at gmail.com
Thu Jul 26 23:00:59 PDT 2007


Hi Guys
      A while back someone asked how to get a black outline arround
text, well here is some code to do just that, this code renders the
text with the outline colour, with one pixel offsets in 8 directions.
it also will add a drop shadow using my fade routine that i posted a
couple of weeks ago. Im using TTF_RenderUNICODE_Blended as my games
can be translated, but teh calls can be replaced with
TTF_RenderBlended.

// gScreen is just my screen surface;
// Dropshaddow use a value between 1.0 and 0.0
// d being distance (depending on the font 1 maybe even 2 if the font
is very curvy)

int	BevelXOffsets[]	=	{-1,-1,-1, 0, 1,1,1,0,-1};
int	BevelYOffsets[]	=	{ 1, 0,-1,-1,-1,0,1,1, 1};

void	Blit8Directions(int x,int y,int d,int Start,int End)
{
	SDL_Rect	Dest;		
	int	vx,vy;	
	int	tx;
	int	ty;
	for(int b=Start;b<End;b++)
	{					
		Dest.x		=	x+(BevelXOffsets[b]*d);
		Dest.y		=	y+(BevelYOffsets[b]*d);				
		SDL_BlitSurface(TTF_Surface,NULL,gScreen,&Dest);
	}	
}

void	OutLineText(int x, int y ,int FontNum,int String,int
OutLineColor,int FontColor,double DropShaddow)
{
	SDL_Rect	Dest;
	SDL_Color	OutLineColour	=	{(OutLineColor>>16)&255,(OutLineColor>>8)&255,OutLineColor&255};
	SDL_Color	FontColour		=	{(FontColor>>16)&255,(FontColor>>8)&255,FontColor&255};
	if(DropShaddow>0)
	{
		TTF_Surface	=	TTF_RenderUNICODE_Blended(Fonts[FontNum],(Uint16*)StringTable[String],ColourBlack);
		BlatFade(TTF_Surface,DropShaddow);	
		Dest.x		=	x+3;
		Dest.y		=	y+3;
		SDL_BlitSurface(TTF_Surface,NULL,gScreen,&Dest);	
		SDL_FreeSurface(TTF_Surface);
	}				
	TTF_Surface=TTF_RenderUNICODE_Blended(Fonts[FontNum],(Uint16*)StringTable[String],OutLineColour);
	Blit8Directions(x,y,1,0,8);
	SDL_FreeSurface(TTF_Surface);
	TTF_Surface=TTF_RenderUNICODE_Blended(Fonts[FontNum],(Uint16*)StringTable[String],FontColour);
	Dest.x		=	x;
	Dest.y		=	y;		
	SDL_BlitSurface(TTF_Surface,NULL,gScreen,&Dest);	
	SDL_FreeSurface(TTF_Surface);
}


Now here is a little fancier text this one uses the same functions a
before but this time it puts the outline 2 pixels out followed by a 1
pixel highlight and lowlight (not quite photoshop but it works), then
the final text is replaced by a texture (bitmap on another surface).
Its great for options screens and menus.

void Textuize(SDL_Surface * Src,SDL_Surface * Texture)
{
	Uint8 Sbpp = Src->format->BytesPerPixel;
	Uint8 Tbpp = Texture->format->BytesPerPixel;	
	Uint8 *Sbits;
	Uint8 *Tbits;	
	int	x,y;
	int	tx	=	0;
	int	ty	=	0;
	Uint32	Pixels;	
	Uint32	Alpha;		
	for(y=0;y<Src->h;y++)
	{
		tx=0;
		for(x=0;x<Src->w;x++)
		{
			Sbits = ((Uint8 *)Src->pixels)+y*Src->pitch+x*Sbpp;
			Tbits = ((Uint8 *)Texture->pixels)+ty*Texture->pitch+tx*Tbpp;
			Pixels	=	*((Uint32 *)(Sbits));
			Alpha	=	(Pixels&TMASK);
			if((Pixels & CMASK)!=0)
			{
				Pixels = *((Uint32 *)(Tbits));				
			}
			*((Uint32 *)(Sbits)) = (Pixels & CMASK)|Alpha;
			tx++;
			if(tx==Texture->w)
			{
				tx=0;
			}			
		}
		ty++;
		if(ty==Texture->h)
		{
			ty=0;
		}
	}
}



void	SpecialEffectText(int x, int y ,int FontNum,int String,int
OutLineColor,int FontColor,int HiLiteColor,int LowLiteColor,double
DropShaddow,int Pattern)
{
	SDL_Rect	Dest;
	SDL_Color	OutLineColour	=	{(OutLineColor&0x0ff0000>>16)&255,(OutLineColor&0x000ff00>>8)&255,OutLineColor&255};
	SDL_Color	FontColour	=	{(FontColor&0x0ff0000>>16)&255,(FontColor&0x000ff00>>8)&255,FontColor&255};
	SDL_Color	HiLiteColour	=	{(HiLiteColor&0x0ff0000>>16)&255,(HiLiteColor&0x000ff00>>8)&255,HiLiteColor&255};
	SDL_Color	LowLiteColour	=	{(LowLiteColor&0x0ff0000>>16)&255,(LowLiteColor&0x000ff00>>8)&255,LowLiteColor&255};
	if(DropShaddow>0)
	{
		TTF_Surface	=	TTF_RenderUNICODE_Blended(Fonts[FontNum],(Uint16*)StringTable[String],ColourBlack);
		BlatFade(TTF_Surface,DropShaddow);	
		Dest.x		=	x+3;
		Dest.y		=	y+3;
		SDL_BlitSurface(TTF_Surface,NULL,gScreen,&Dest);	
		SDL_FreeSurface(TTF_Surface);
	}													
	int	Distance	=	2;	
	TTF_Surface=TTF_RenderUNICODE_Blended(Fonts[FontNum],(Uint16*)StringTable[String],OutLineColour);
	Blit8Directions(x,y,Distance,0,8);
	SDL_FreeSurface(TTF_Surface);
	Distance--;
	TTF_Surface=TTF_RenderUNICODE_Blended(Fonts[FontNum],(Uint16*)StringTable[String],LowLiteColour);
	Blit8Directions(x,y,Distance,3,9);
	SDL_FreeSurface(TTF_Surface);	
	TTF_Surface=TTF_RenderUNICODE_Blended(Fonts[FontNum],(Uint16*)StringTable[String],HiLiteColour);
	Blit8Directions(x,y,Distance,1,3);
	SDL_FreeSurface(TTF_Surface);	
	TTF_Surface=TTF_RenderUNICODE_Blended(Fonts[FontNum],(Uint16*)StringTable[String],FontColour);
	Dest.x		=	x;
	Dest.y		=	y;		
	Textuize(TTF_Surface,TTF_Texture[Pattern]);	
	SDL_BlitSurface(TTF_Surface,NULL,gScreen,&Dest);	
	SDL_FreeSurface(TTF_Surface);
}

Anyhow i hope its usefull to you. Any problems contact me.

Trish xxxx


More information about the SDL mailing list