[SDL] Trouble (I think) with alpha channel using SDL_ttf

Mikael Eriksson mikael_miffe_eriksson at yahoo.se
Tue Apr 17 08:46:33 PDT 2007


On Tue, Apr 03, 2007 at 12:35:34PM -0400, David Bruce wrote:
> Hi,
> 
> I've taken over a project that has a function (see below) that is supposed to 
> return a SDL_Surface* containing text with a black outline.

Here is what I use to create outline text. Hope it helps.

typedef unsigned char * byte;
SDL_Surface *Text::render() {
	SDL_Color c;
	c.r = c.g = c.b = 255;
	SDL_Surface *tmp = TTF_RenderUTF8_Blended( fonts[size], text.c_str(), c );
	if ( !tmp ) {
		fprintf( stderr, "TTF: %s.\n", TTF_GetError() );
		SDL_Quit();
		exit( 1 );
	}

	SDL_Surface *surf = SDL_CreateRGBSurface( SDL_SWSURFACE, tmp->w+2, tmp->h+2, 32, 0xff, 0xff00, 0xff0000, 0xff000000 );
	for ( int x = 0; x < surf->w; x++ ) {
		for ( int y = 0; y < surf->h; y++ ) {
			byte *dr = (byte*)surf->pixels + surf->pitch * y + x * 4 + 0;
			byte *dg = (byte*)surf->pixels + surf->pitch * y + x * 4 + 1;
			byte *db = (byte*)surf->pixels + surf->pitch * y + x * 4 + 2;
			byte *da = (byte*)surf->pixels + surf->pitch * y + x * 4 + 3;

			if ( x > 0 && x < surf->w - 1 && y > 0 && y < surf->h - 1 ) {
				byte sa = *((byte*)tmp->pixels + tmp->pitch * (y-1) + (x-1) * 4 + 3);
				*dr = *dg = *db = sa;
			} else {
				*dr = *dg = *db = 0;
			}
			*da = 0;

			int r = 1;
			int fromx = x-r;
			if ( fromx < 0 ) fromx = 0;
			int tox = x+r+1;
			if ( tox > surf->w ) tox = surf->w;
			int fromy = y-r;
			if ( fromy < 0 ) fromy = 0;
			int toy = y+r+1;
			if ( toy > surf->h ) toy = surf->h;

			byte ma = 0;
			for ( int tx = fromx; tx < tox; tx++ ) {
				if ( tx > 0 && tx < surf->w - 1 ) {
					for ( int ty = fromy; ty < toy; ty++ ) {
						if ( ty > 0 && ty < surf->h - 1 ) {
							byte sa = *((byte*)tmp->pixels + tmp->pitch * (ty-1) + (tx-1) * 4 + 3);
							if ( sa > ma ) ma = sa;
						}
					}
				}
			}
			*da = ma;
		}
	}

	SDL_FreeSurface( tmp );

	return surf;
}


More information about the SDL mailing list