Hello,
I’d like to know how I could check if a pixel at a specifed (x, y) position
in an SDL_Surface is transparent. The SDL_Surface comes from a
TTF_RenderText function call by the way.
I’ve tried this but it doesn’t seem to work :
SDL_Surface *img = TTF_RenderText…(…);
// getpixel() taken from the doc
// at 'Chapter 2’
Uint32 pixel = getpixel(img, x, y);
if ( pixel == img->format->colorkey )
printf(“Pixel @ (%d, %d) is transparent\n”, x, y);
Can you help me ?
Thanks,
Damiano
Damiano escribi?:
I think you must lock the surface in order to use getPixel()
Hum, i?ve just read the docs and yes, you MUST lock the surface. Taken
from the docs:
Hello,
I’d like to know how I could check if a pixel at a specifed (x, y) position
in an SDL_Surface is transparent. The SDL_Surface comes from a
TTF_RenderText function call by the way.
I’ve tried this but it doesn’t seem to work :
SDL_Surface *img = TTF_RenderText…(…);
if ( SDL_MUSTLOCK(screen) )
if ( SDL_LockSurface(screen) < 0 ) {
fprintf(stderr, "Can't lock screen: %s\n", SDL_GetError());
return;
}
// getpixel() taken from the doc
// at ‘Chapter 2’
Uint32 pixel = getpixel(img, x, y);
if ( SDL_MUSTLOCK(screen) )
SDL_UnlockSurface(screen);
if ( pixel == img->format->colorkey )
printf(“Pixel @ (%d, %d) is transparent\n”, x, y);
}
Can you help me ?
Thanks,
Hope it works now.–
Ricardo Amores Hern?ndez
ICQ: 19463735
MSN: zheo_ (at) hotmail (dot) es
Hello,
The problem seems to come from the GetPixel() function because, with gdb on
Linux, I’ve got this :
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 1024 (LWP 28443)]
0x0804bbab in ScreenFiles::GetPixel(SDL_Surface*, int, int)
(this=0xbffff970,
surface=0x836e8d0, x=430, y=31) at ScreenFiles.cpp:196
196 return *(Uint32 *)p;
What is strange is that I have copied everything verbatim from the doc…
Here is my source :
Uint32 ScreenFiles::GetPixel(SDL_Surface *surface, int x, int y)
{
int bpp = surface->format->BytesPerPixel;
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
switch(bpp) {
case 1:
return *p;
case 2:
return *(Uint16 *)p;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
return p[0] << 16 | p[1] << 8 | p[2];
else
return p[0] | p[1] << 8 | p[2] << 16;
case 4:
return *(Uint32 *)p;
default:
return 0;
}
}
int ScreenFiles::LastLetterPixel(SDL_Surface *img)
{
SDL_LockSurface(img);
int xPos = MAX_FILENAME_WIDTH;
for (int x = MAX_FILENAME_WIDTH; x > 0; x–)
{
int y;
for ( y = 0; y < img->h; y++)
{
if ( GetPixel(img, x, y) != img->format->colorkey )
break;
}
if ( y == (img->h - 1))
{
xPos = x;
break;
}
}
SDL_UnlockSurface(img);
cout << "Last letter @ pixel : x = " << xPos << endl;
return xPos;
}
So, any idea ?
Thanks,
Damiano
Use SDL_GetRGBA() on the return value from getpixel(). Something like this:
int r, g, b, a;
Uint32 pixel = getpixel(img, x, y);
SDL_GetRGBA(pixel, img->format, &r, &b, &g, &a);
if (a == 255)
printf (“Pixel is completely opaque\n”);
else if (a == 0)
printf (“Pixel is completely transparent\n”);
else
printf (“Pixel is partially transparent\n”);
At 12:33 AM 8/13/2003 +0200, you wrote:>I’d like to know how I could check if a pixel at a specifed (x, y) position
in an SDL_Surface is transparent. The SDL_Surface comes from a
TTF_RenderText function call by the way.
I’ve tried this but it doesn’t seem to work :
SDL_Surface *img = TTF_RenderText…(…);
// getpixel() taken from the doc
// at 'Chapter 2’
Uint32 pixel = getpixel(img, x, y);
if ( pixel == img->format->colorkey )
printf(“Pixel @ (%d, %d) is transparent\n”, x, y);